Skip to content

Commit d46cf61

Browse files
feat: add variations + free analysis
1 parent a65e1af commit d46cf61

File tree

2 files changed

+48
-10
lines changed

2 files changed

+48
-10
lines changed

src/components/Board/ClientGameBoard.tsx

+41-10
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
2+
import { Chess } from 'chess.ts'
23
import { defaults } from 'chessground/state'
34
import type { Key } from 'chessground/types'
45
import Chessground from '@react-chess/chessground'
6+
import type { DrawBrushes, DrawShape } from 'chessground/draw'
7+
import { ClientBaseGame, Check, GameNode, Color } from 'src/types'
58
import {
9+
useMemo,
610
Dispatch,
7-
SetStateAction,
811
useCallback,
9-
useContext,
10-
useMemo,
12+
SetStateAction,
13+
useEffect,
1114
} from 'react'
12-
import type { DrawBrushes, DrawShape } from 'chessground/draw'
13-
14-
import { BaseGame, Check } from 'src/types'
15-
import { ClientGameControllerContext } from 'src/contexts'
1615

1716
interface Props {
18-
game: BaseGame
17+
game: ClientBaseGame
1918
moves?: Map<string, string[]>
19+
currentNode: GameNode
20+
orientation: Color
21+
goToNode: (node: GameNode) => void
2022
setCurrentMove?: (move: [string, string] | null) => void
2123
setCurrentSquare?: Dispatch<SetStateAction<Key | null>>
2224
move?: {
@@ -32,19 +34,48 @@ export const ClientGameBoard: React.FC<Props> = ({
3234
game,
3335
moves,
3436
move,
37+
currentNode,
38+
orientation,
39+
goToNode,
3540
setCurrentMove,
3641
setCurrentSquare,
3742
shapes,
3843
brushes,
3944
}: Props) => {
40-
const { currentNode, orientation } = useContext(ClientGameControllerContext)
45+
useEffect(() => {
46+
console.log(currentNode)
47+
}, [currentNode])
4148

4249
const after = useCallback(
4350
(from: string, to: string) => {
51+
if (!game.tree || !currentNode) return
52+
4453
if (setCurrentMove) setCurrentMove([from, to])
4554
if (setCurrentSquare) setCurrentSquare(null)
55+
56+
const chess = new Chess(currentNode.fen) // Use currentNode.fen
57+
58+
const moveAttempt = chess.move({ from: from, to: to })
59+
60+
if (moveAttempt) {
61+
const newFen = chess.fen()
62+
const moveString = from + to
63+
const san = moveAttempt.san
64+
65+
if (currentNode.mainChild?.move === moveString) {
66+
goToNode(currentNode.mainChild)
67+
} else {
68+
const newVariation = game.tree.addVariation(
69+
currentNode,
70+
newFen,
71+
moveString,
72+
san,
73+
)
74+
goToNode(newVariation)
75+
}
76+
}
4677
},
47-
[setCurrentMove, setCurrentSquare],
78+
[setCurrentMove, setCurrentSquare, currentNode, game.tree, goToNode],
4879
)
4980

5081
const currentMove = useMemo(() => {

src/pages/analysis/client/[...id].tsx

+7
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import {
4949
AnalyzedGame,
5050
MaiaEvaluation,
5151
StockfishEvaluation,
52+
GameNode,
5253
} from 'src/types'
5354
import { useClientAnalysisController, useClientGameController } from 'src/hooks'
5455
import {
@@ -398,6 +399,9 @@ const Analysis: React.FC<Props> = ({
398399
shapes={
399400
hoverArrow ? [...arrows, hoverArrow] : [...arrows]
400401
}
402+
currentNode={controller.currentNode as GameNode}
403+
orientation={controller.orientation}
404+
goToNode={controller.goToNode}
401405
/>
402406
</div>
403407
<VerticalEvaluationBar
@@ -557,6 +561,9 @@ const Analysis: React.FC<Props> = ({
557561
moves={moves}
558562
setCurrentSquare={setCurrentSquare}
559563
shapes={hoverArrow ? [...arrows, hoverArrow] : [...arrows]}
564+
currentNode={controller.currentNode as GameNode}
565+
orientation={controller.orientation}
566+
goToNode={controller.goToNode}
560567
/>
561568
</div>
562569
<div className="flex h-auto w-full flex-col gap-1">

0 commit comments

Comments
 (0)