Skip to content

Commit 4f86c1c

Browse files
Merge pull request #270 from CSSLab/codex/maia-flagging-timeouts
Fix Maia timeout losses
2 parents 498d9cb + 866315f commit 4f86c1c

3 files changed

Lines changed: 49 additions & 15 deletions

File tree

src/hooks/usePlayController/useHandBrainController.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ export const useHandBrainController = (
129129

130130
useEffect(() => {
131131
let canceled = false
132+
let moveTimeout: ReturnType<typeof setTimeout> | undefined
132133

133134
const makeMaiaMove = async () => {
134135
const maiaClock =
@@ -160,8 +161,12 @@ export const useHandBrainController = (
160161
return
161162
}
162163

163-
setTimeout(
164+
moveTimeout = setTimeout(
164165
() => {
166+
if (canceled) {
167+
return
168+
}
169+
165170
const moveTime = controller.updateClock()
166171

167172
const chess = new Chess(controller.currentNode.fen)
@@ -183,6 +188,10 @@ export const useHandBrainController = (
183188
makeMaiaMove()
184189
return () => {
185190
canceled = true
191+
192+
if (moveTimeout) {
193+
clearTimeout(moveTimeout)
194+
}
186195
}
187196
}
188197
}, [

src/hooks/usePlayController/usePlayController.ts

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export const usePlayController = (id: string, config: PlayGameConfig) => {
6161

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

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

92-
const termination = resigned
93-
? Math.min(whiteClock, blackClock) > 0
93+
const termination = timeExpired
94+
? computeTimeTermination(chess, timeExpired)
95+
: resigned
9496
? ({
9597
result: turn == 'w' ? '0-1' : '1-0',
9698
winner: turn == 'w' ? 'black' : 'white',
9799
type: 'resign',
98100
} as Termination)
99-
: computeTimeTermination(chess, turn == 'w' ? 'white' : 'black')
100-
: computeTermination(chess)
101+
: computeTermination(chess)
101102

102103
const moves = []
103104
const rootNode = gameTree.getRoot()
@@ -131,7 +132,7 @@ export const usePlayController = (id: string, config: PlayGameConfig) => {
131132
tree: gameTree,
132133
turn: turn == 'b' ? 'black' : 'white',
133134
}
134-
}, [gameTree, treeVersion, resigned, whiteClock, blackClock, id])
135+
}, [gameTree, treeVersion, resigned, timeExpired, whiteClock, blackClock, id])
135136

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

203+
const expireOnTime = useCallback((color: Color) => {
204+
const now = Date.now()
205+
206+
if (color == 'white') {
207+
setWhiteClock(0)
208+
} else {
209+
setBlackClock(0)
210+
}
211+
212+
setLastMoveTime(now)
213+
setTimeExpired(color)
214+
}, [])
215+
202216
useEffect(() => {
203217
if (
204-
playerActive &&
218+
!game.termination &&
205219
moveList.length > 1 &&
206-
config.timeControl != 'unlimited'
220+
config.timeControl != 'unlimited' &&
221+
toPlay
207222
) {
208-
const timeRemaining = config.player == 'white' ? whiteClock : blackClock
223+
const activeColor = toPlay
224+
const timeRemaining = activeColor == 'white' ? whiteClock : blackClock
209225
const timeout = setTimeout(() => {
210-
updateClock()
211-
setResigned(true)
226+
expireOnTime(activeColor)
212227
}, timeRemaining)
213228

214229
return () => clearTimeout(timeout)
215230
}
216231
}, [
232+
expireOnTime,
233+
game.termination,
217234
blackClock,
218235
moveList.length,
219-
config.player,
220-
playerActive,
221236
config.timeControl,
222-
updateClock,
237+
toPlay,
223238
whiteClock,
224239
])
225240

@@ -253,6 +268,7 @@ export const usePlayController = (id: string, config: PlayGameConfig) => {
253268
const newTree = new GameTree(config.startFen || nullFen)
254269
setGameTree(newTree)
255270
setResigned(false)
271+
setTimeExpired(null)
256272
setLastMoveTime(0)
257273
setWhiteClock(initialClockValue)
258274
setBlackClock(initialClockValue)

src/hooks/usePlayController/useVsMaiaController.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export const useVsMaiaPlayController = (
3333

3434
useEffect(() => {
3535
let canceled = false
36+
let moveTimeout: ReturnType<typeof setTimeout> | undefined
3637

3738
const makeMaiaMove = async () => {
3839
if (
@@ -73,7 +74,11 @@ export const useVsMaiaPlayController = (
7374
const minimumDelayMs = 200 + Math.random() * 100
7475
const delayMs = Math.max(moveDelay * 1000, minimumDelayMs)
7576

76-
setTimeout(() => {
77+
moveTimeout = setTimeout(() => {
78+
if (canceled) {
79+
return
80+
}
81+
7782
const moveTime = controller.updateClock()
7883

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

101106
return () => {
102107
canceled = true
108+
109+
if (moveTimeout) {
110+
clearTimeout(moveTimeout)
111+
}
103112
}
104113
}, [
105114
controller.game.id,

0 commit comments

Comments
 (0)