@@ -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 )
0 commit comments