Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/hooks/usePlayController/useHandBrainController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ export const useHandBrainController = (

useEffect(() => {
let canceled = false
let moveTimeout: ReturnType<typeof setTimeout> | undefined

const makeMaiaMove = async () => {
const maiaClock =
Expand Down Expand Up @@ -160,8 +161,12 @@ export const useHandBrainController = (
return
}

setTimeout(
moveTimeout = setTimeout(
() => {
if (canceled) {
return
}

const moveTime = controller.updateClock()

const chess = new Chess(controller.currentNode.fen)
Expand All @@ -183,6 +188,10 @@ export const useHandBrainController = (
makeMaiaMove()
return () => {
canceled = true

if (moveTimeout) {
clearTimeout(moveTimeout)
}
}
}
}, [
Expand Down
42 changes: 29 additions & 13 deletions src/hooks/usePlayController/usePlayController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export const usePlayController = (id: string, config: PlayGameConfig) => {

const [treeVersion, setTreeVersion] = useState<number>(0)
const [resigned, setResigned] = useState<boolean>(false)
const [timeExpired, setTimeExpired] = useState<Color | null>(null)

const [baseMinutes, incrementSeconds] =
config.timeControl == 'unlimited'
Expand Down Expand Up @@ -89,15 +90,15 @@ export const usePlayController = (id: string, config: PlayGameConfig) => {
const turn = lastNode.turn
const chess = gameTree.toChess()

const termination = resigned
? Math.min(whiteClock, blackClock) > 0
const termination = timeExpired
? computeTimeTermination(chess, timeExpired)
: resigned
? ({
result: turn == 'w' ? '0-1' : '1-0',
winner: turn == 'w' ? 'black' : 'white',
type: 'resign',
} as Termination)
: computeTimeTermination(chess, turn == 'w' ? 'white' : 'black')
: computeTermination(chess)
: computeTermination(chess)

const moves = []
const rootNode = gameTree.getRoot()
Expand Down Expand Up @@ -131,7 +132,7 @@ export const usePlayController = (id: string, config: PlayGameConfig) => {
tree: gameTree,
turn: turn == 'b' ? 'black' : 'white',
}
}, [gameTree, treeVersion, resigned, whiteClock, blackClock, id])
}, [gameTree, treeVersion, resigned, timeExpired, whiteClock, blackClock, id])

const toPlay: Color | null = game.termination ? null : game.turn
const playerActive = toPlay == config.player
Expand Down Expand Up @@ -199,27 +200,41 @@ export const usePlayController = (id: string, config: PlayGameConfig) => {
],
)

const expireOnTime = useCallback((color: Color) => {
const now = Date.now()

if (color == 'white') {
setWhiteClock(0)
} else {
setBlackClock(0)
}

setLastMoveTime(now)
setTimeExpired(color)
}, [])

useEffect(() => {
if (
playerActive &&
!game.termination &&
moveList.length > 1 &&
config.timeControl != 'unlimited'
config.timeControl != 'unlimited' &&
toPlay
) {
const timeRemaining = config.player == 'white' ? whiteClock : blackClock
const activeColor = toPlay
const timeRemaining = activeColor == 'white' ? whiteClock : blackClock
const timeout = setTimeout(() => {
updateClock()
setResigned(true)
expireOnTime(activeColor)
}, timeRemaining)

return () => clearTimeout(timeout)
}
}, [
expireOnTime,
game.termination,
blackClock,
moveList.length,
config.player,
playerActive,
config.timeControl,
updateClock,
toPlay,
whiteClock,
])

Expand Down Expand Up @@ -253,6 +268,7 @@ export const usePlayController = (id: string, config: PlayGameConfig) => {
const newTree = new GameTree(config.startFen || nullFen)
setGameTree(newTree)
setResigned(false)
setTimeExpired(null)
setLastMoveTime(0)
setWhiteClock(initialClockValue)
setBlackClock(initialClockValue)
Expand Down
11 changes: 10 additions & 1 deletion src/hooks/usePlayController/useVsMaiaController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const useVsMaiaPlayController = (

useEffect(() => {
let canceled = false
let moveTimeout: ReturnType<typeof setTimeout> | undefined

const makeMaiaMove = async () => {
if (
Expand Down Expand Up @@ -73,7 +74,11 @@ export const useVsMaiaPlayController = (
const minimumDelayMs = 200 + Math.random() * 100
const delayMs = Math.max(moveDelay * 1000, minimumDelayMs)

setTimeout(() => {
moveTimeout = setTimeout(() => {
if (canceled) {
return
}

const moveTime = controller.updateClock()

const chess = new Chess(controller.currentNode.fen)
Expand All @@ -100,6 +105,10 @@ export const useVsMaiaPlayController = (

return () => {
canceled = true

if (moveTimeout) {
clearTimeout(moveTimeout)
}
}
}, [
controller.game.id,
Expand Down
Loading