Skip to content

Commit 31efe08

Browse files
chore: clean up tree logic
1 parent f5d8935 commit 31efe08

17 files changed

Lines changed: 473 additions & 488 deletions

File tree

src/api/train.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,9 @@ export const fetchPuzzle = async () => {
6161
for (let i = 1; i < moves.length; i++) {
6262
const move = moves[i]
6363
if (move.uci && move.san) {
64-
currentNode = gameTree.addMainMove(
65-
currentNode,
66-
move.board,
67-
move.uci,
68-
move.san,
69-
)
64+
currentNode = gameTree
65+
.getLastMainlineNode()
66+
.addChild(move.board, move.uci, move.san, true)
7067
}
7168
}
7269

src/api/turing.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,9 @@ export const fetchTuringGame = async () => {
4646
for (let i = 1; i < moves.length; i++) {
4747
const move = moves[i]
4848
if (move.uci && move.san) {
49-
currentNode = gameTree.addMainMove(
50-
currentNode,
51-
move.board,
52-
move.uci,
53-
move.san,
54-
)
49+
currentNode = gameTree
50+
.getLastMainlineNode()
51+
.addChild(move.board, move.uci, move.san, true)
5552
}
5653
}
5754

src/components/Analysis/StreamAnalysis.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export const StreamAnalysis: React.FC<Props> = ({
9292
} else {
9393
// For stream analysis, ALWAYS create variations for player moves
9494
// This preserves the live game mainline and allows exploration
95-
const newVariation = game.tree.addVariation(
95+
const newVariation = game.tree.addVariationNode(
9696
analysisController.currentNode,
9797
newFen,
9898
moveString,

src/components/Board/GameBoard.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export const GameBoard: React.FC<Props> = ({
6666
if (currentNode.mainChild?.move === moveString) {
6767
goToNode(currentNode.mainChild)
6868
} else {
69-
const newVariation = game.tree.addVariation(
69+
const newVariation = game.tree.addVariationNode(
7070
currentNode,
7171
newFen,
7272
moveString,

src/hooks/useAnalysisController/useAnalysisController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ export const useAnalysisController = (
475475
const moveResult = chess.move(currentMistake.bestMove, { sloppy: true })
476476

477477
if (moveResult) {
478-
const newVariation = game.tree.addVariation(
478+
const newVariation = game.tree.addVariationNode(
479479
controller.currentNode,
480480
chess.fen(),
481481
currentMistake.bestMove,

src/hooks/useOpeningDrillController/useOpeningDrillController.ts

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,9 @@ const parsePgnToTree = (pgn: string, gameTree: GameTree): GameNode | null => {
6161
if (existingChild) {
6262
currentNode = existingChild
6363
} else {
64-
const newNode = gameTree.addMainMove(
65-
currentNode,
66-
chess.fen(),
67-
moveUci,
68-
moveObj.san,
69-
)
64+
const newNode = gameTree
65+
.getLastMainlineNode()
66+
.addChild(chess.fen(), moveUci, moveObj.san)
7067
if (newNode) {
7168
currentNode = newNode
7269
} else {
@@ -810,12 +807,9 @@ export const useOpeningDrillController = (
810807
try {
811808
const moveObj = chess.move(moveUci, { sloppy: true })
812809
if (moveObj) {
813-
const newNode = gameTree.addMainMove(
814-
currentNode,
815-
chess.fen(),
816-
moveUci,
817-
moveObj.san,
818-
)
810+
const newNode = gameTree
811+
.getLastMainlineNode()
812+
.addChild(chess.fen(), moveUci, moveObj.san)
819813
if (newNode) {
820814
currentNode = newNode
821815
finalNode = newNode
@@ -834,12 +828,10 @@ export const useOpeningDrillController = (
834828
try {
835829
const moveObj = chess.move(moveUci, { sloppy: true })
836830
if (moveObj) {
837-
const newNode = gameTree.addMainMove(
838-
currentNode,
839-
chess.fen(),
840-
moveUci,
841-
moveObj.san,
842-
)
831+
const newNode = gameTree
832+
.getLastMainlineNode()
833+
.addChild(currentNode, chess.fen(), moveUci, true, moveObj.san)
834+
843835
if (newNode) {
844836
currentNode = newNode
845837
finalNode = newNode

src/hooks/usePlayController/usePlayController.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,27 @@ export const usePlayController = (id: string, config: PlayGameConfig) => {
226226

227227
const addMoveWithTime = useCallback(
228228
(moveUci: string, moveTime: number) => {
229-
const newNode = controller.tree.addMoveToMainLine(moveUci, moveTime)
230-
if (newNode) {
231-
console.log('old node', controller.currentNode.fen)
232-
console.log('new move', moveUci)
233-
console.log('new node', newNode.fen)
234-
controller.setCurrentNode(newNode)
235-
setTreeVersion((prev) => prev + 1)
229+
const lastNode = controller.tree.getLastMainlineNode()
230+
const board = new Chess(lastNode.fen)
231+
const result = board.move(moveUci, { sloppy: true })
232+
233+
if (result) {
234+
const newNode = lastNode.addChild(
235+
board.fen(),
236+
moveUci,
237+
result.san,
238+
true,
239+
undefined,
240+
moveTime,
241+
)
242+
243+
if (newNode) {
244+
console.log('old node', controller.currentNode.fen)
245+
console.log('new move', moveUci)
246+
console.log('new node', newNode.fen)
247+
controller.setCurrentNode(newNode)
248+
setTreeVersion((prev) => prev + 1)
249+
}
236250
}
237251
},
238252
[controller.tree, controller],

src/hooks/useTrainingController/useTrainingController.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,9 @@ const buildTrainingGameTree = (game: PuzzleGame): GameTree => {
1616
for (let i = 1; i < game.moves.length; i++) {
1717
const move = game.moves[i]
1818
if (move.uci && move.san) {
19-
currentNode = tree.addMainMove(
20-
currentNode,
21-
move.board,
22-
move.uci,
23-
move.san,
24-
)
19+
currentNode = tree
20+
.getLastMainlineNode()
21+
.addChild(move.board, move.uci, move.san, true),
2522
}
2623
}
2724

src/hooks/useTuringController/useTuringController.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ const buildTuringGameTree = (game: TuringGame): GameTree => {
3434
const move = game.moves[i]
3535
const uci = move.uci || (move.lastMove ? move.lastMove.join('') : undefined)
3636
if (uci && move.san) {
37-
currentNode = tree.addMainMove(currentNode, move.board, uci, move.san)
37+
currentNode = tree
38+
.getLastMainlineNode()
39+
.addChild(move.board, uci, move.san, true)
3840
}
3941
}
4042

src/lib/common.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,10 @@ export function buildGameTreeFromMoveList(moves: any[], initialFen: string) {
88
const move = moves[i]
99

1010
if (move.lastMove) {
11-
const [from, to] = move.lastMove
12-
currentNode = tree.addMainMove(
13-
currentNode,
14-
move.board,
15-
from + to,
16-
move.san || '',
17-
)
11+
const [from, to, promotion] = move.lastMove
12+
currentNode = tree
13+
.getLastMainlineNode()
14+
.addChild(move.board, from + to + promotion || '', move.san || '', true)
1815
}
1916
}
2017

0 commit comments

Comments
 (0)