Skip to content

Commit 80a0564

Browse files
refactor: playcontroller use treecontroller
1 parent e16b396 commit 80a0564

1 file changed

Lines changed: 20 additions & 39 deletions

File tree

src/hooks/usePlayController/usePlayController.ts

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
import { AllStats } from '../useStats'
1010
import { PlayedGame } from 'src/types/play'
1111
import { Chess, Piece, SQUARES } from 'chess.ts'
12+
import { useTreeController } from '../useTreeController'
1213
import { useMemo, useState, useCallback, useEffect } from 'react'
1314

1415
const nullFen = new Chess().fen()
@@ -41,6 +42,8 @@ export const usePlayController = (id: string, config: PlayGameConfig) => {
4142
const [gameTree, setGameTree] = useState<GameTree>(
4243
() => new GameTree(config.startFen || nullFen),
4344
)
45+
const controller = useTreeController(gameTree, config.player)
46+
4447
const [treeVersion, setTreeVersion] = useState<number>(0)
4548
const [resigned, setResigned] = useState<boolean>(false)
4649

@@ -54,11 +57,6 @@ export const usePlayController = (id: string, config: PlayGameConfig) => {
5457
const [blackClock, setBlackClock] = useState<number>(initialClockValue)
5558
const [lastMoveTime, setLastMoveTime] = useState<number>(0)
5659

57-
const [currentNode, setCurrentNode] = useState<GameNode>(gameTree.getRoot())
58-
const [orientation, setOrientation] = useState<'white' | 'black'>(
59-
config.player,
60-
)
61-
6260
const moveList = useMemo(
6361
() => gameTree.toMoveArray(),
6462
[gameTree, treeVersion],
@@ -121,9 +119,9 @@ export const usePlayController = (id: string, config: PlayGameConfig) => {
121119
const playerActive = toPlay == config.player
122120

123121
const { availableMoves, pieces } = useMemo(() => {
124-
if (!currentNode) return { availableMoves: [], pieces: {} }
122+
if (!controller.currentNode) return { availableMoves: [], pieces: {} }
125123

126-
const chess = new Chess(currentNode.fen)
124+
const chess = new Chess(controller.currentNode.fen)
127125
const verboseMoves = chess.moves({ verbose: true })
128126

129127
const cantMove = !playerActive || game.termination
@@ -146,24 +144,7 @@ export const usePlayController = (id: string, config: PlayGameConfig) => {
146144
}
147145

148146
return { availableMoves, pieces }
149-
}, [currentNode, playerActive, game.termination, treeVersion])
150-
151-
const goToNode = useCallback((node: GameNode) => setCurrentNode(node), [])
152-
const goToNextNode = useCallback(() => {
153-
if (currentNode?.mainChild) setCurrentNode(currentNode.mainChild)
154-
}, [currentNode])
155-
const goToPreviousNode = useCallback(() => {
156-
if (currentNode?.parent) setCurrentNode(currentNode.parent)
157-
}, [currentNode])
158-
const goToRootNode = useCallback(
159-
() => setCurrentNode(gameTree.getRoot()),
160-
[gameTree],
161-
)
162-
163-
const plyCount = useMemo(
164-
() => gameTree.getMainLine().length,
165-
[gameTree, treeVersion],
166-
)
147+
}, [controller.currentNode, playerActive, game.termination, treeVersion])
167148

168149
const updateClock = useCallback(
169150
(overrideTime: number | undefined = undefined): number => {
@@ -228,30 +209,30 @@ export const usePlayController = (id: string, config: PlayGameConfig) => {
228209
(moveUci: string) => {
229210
const newNode = gameTree.addMoveToMainLine(moveUci)
230211
if (newNode) {
231-
setCurrentNode(newNode)
212+
controller.setCurrentNode(newNode)
232213
// Force re-render by incrementing tree version
233214
setTreeVersion((prev) => prev + 1)
234215
}
235216
},
236-
[gameTree],
217+
[gameTree, controller],
237218
)
238219

239220
const addMoveWithTime = useCallback(
240221
(moveUci: string, moveTime: number) => {
241222
const newNode = gameTree.addMoveToMainLine(moveUci, moveTime)
242223
if (newNode) {
243-
setCurrentNode(newNode)
224+
controller.setCurrentNode(newNode)
244225
// Force re-render by incrementing tree version
245226
setTreeVersion((prev) => prev + 1)
246227
}
247228
},
248-
[gameTree],
229+
[gameTree, controller],
249230
)
250231

251232
const reset = () => {
252233
const newTree = new GameTree(config.startFen || nullFen)
253234
setGameTree(newTree)
254-
setCurrentNode(newTree.getRoot())
235+
controller.setCurrentNode(newTree.getRoot())
255236
setResigned(false)
256237
setLastMoveTime(0)
257238
setWhiteClock(initialClockValue)
@@ -275,7 +256,7 @@ export const usePlayController = (id: string, config: PlayGameConfig) => {
275256
return {
276257
game,
277258
gameTree,
278-
currentNode,
259+
currentNode: controller.currentNode,
279260
player: config.player,
280261
playType: config.playType,
281262
maiaVersion: config.maiaVersion,
@@ -292,14 +273,14 @@ export const usePlayController = (id: string, config: PlayGameConfig) => {
292273
lastMoveTime,
293274
stats,
294275

295-
setCurrentNode,
296-
goToNode,
297-
goToNextNode,
298-
goToPreviousNode,
299-
goToRootNode,
300-
plyCount,
301-
orientation,
302-
setOrientation,
276+
setCurrentNode: controller.setCurrentNode,
277+
goToNode: controller.goToNode,
278+
goToNextNode: controller.goToNextNode,
279+
goToPreviousNode: controller.goToPreviousNode,
280+
goToRootNode: controller.goToRootNode,
281+
plyCount: controller.plyCount,
282+
orientation: controller.orientation,
283+
setOrientation: controller.setOrientation,
303284

304285
addMove,
305286
addMoveWithTime,

0 commit comments

Comments
 (0)