@@ -79,6 +79,9 @@ export function Game() {
79
79
function Board ( { gameState, setGameState} ) {
80
80
let [ cursor_x , setCursor_x ] = useState ( - 1 )
81
81
let [ cursor_y , setCursor_y ] = useState ( - 1 )
82
+ let [ countdown , setCountdown ] = useState ( 9 )
83
+ let countdownRef = useRef ( )
84
+ countdownRef . current = countdown
82
85
let [ ctrlKeyDown , setCtrlKeyDown ] = useState ( false )
83
86
let zoom = useViewStateStore ( state => state . zoom )
84
87
let { gameId} = useParams ( )
@@ -100,6 +103,29 @@ function Board({gameState, setGameState}) {
100
103
let dragging = useLayoutStore ( state => state . dragging )
101
104
let muted = useMuteStore ( state => state . muted )
102
105
let howler = useRef ( )
106
+ let showMoveNumbers = ctrlKeyDown && ( isKibitz ( gameState , auth ) || gameHasEnded ( gameState ) )
107
+ let intervalIdRef = useRef ( )
108
+
109
+ let resetCountdown = useCallback ( ( ) => {
110
+ if ( intervalIdRef . current ) {
111
+ clearInterval ( intervalIdRef . current )
112
+ }
113
+ setCountdown ( 10 )
114
+ intervalIdRef . current = setInterval ( ( ) => {
115
+ setCountdown ( countdown => countdown - 1 )
116
+ } , 1000 )
117
+
118
+ } , [ ] )
119
+
120
+ useEffect ( ( ) => {
121
+ resetCountdown ( )
122
+ return ( ) => {
123
+ if ( intervalIdRef . current ) {
124
+ clearInterval ( intervalIdRef . current )
125
+ }
126
+ }
127
+ } , [ resetCountdown ] )
128
+
103
129
let playClickSound = useCallback ( ( ) => {
104
130
if ( muted ) {
105
131
return
@@ -164,8 +190,6 @@ function Board({gameState, setGameState}) {
164
190
return has
165
191
} , [ gameState , counting , board , isCursorInBounds ] )
166
192
167
- let showMoveNumbers = ctrlKeyDown && ( isKibitz ( gameState , auth ) || gameHasEnded ( gameState ) )
168
-
169
193
let context = useMemo ( ( ) => {
170
194
let dim = board . length
171
195
if ( ! dim ) {
@@ -224,6 +248,12 @@ function Board({gameState, setGameState}) {
224
248
}
225
249
} , [ board , canvasRef , zoom ] )
226
250
251
+ useEffect ( ( ) => {
252
+ if ( ! showMoveNumbers ) {
253
+ paintLastMove ( context , lastMove , countdown )
254
+ }
255
+ } , [ showMoveNumbers , context , lastMove , countdown ] )
256
+
227
257
let onMouseMove = useCallback ( ( e ) => {
228
258
if ( dragging ) {
229
259
return
@@ -285,12 +315,13 @@ function Board({gameState, setGameState}) {
285
315
if ( ! isSelfPlay ( gameState ) ) { // can't add early in self play; myColor is 0
286
316
setGameState ( addMove ( gameState , { ...move , color : myColor } ) ) // early add move
287
317
}
318
+ resetCountdown ( )
288
319
playClickSound ( )
289
320
stompClient . publish ( {
290
321
destination : "/app/game/move" ,
291
322
body : JSON . stringify ( move ) ,
292
323
} )
293
- } , [ gameState , setGameState , auth , board , stompClient , counting , forbidden_x , forbidden_y , myColor , playClickSound , isCursorInBounds , showMoveNumbers ] )
324
+ } , [ gameState , setGameState , auth , board , stompClient , counting , forbidden_x , forbidden_y , myColor , playClickSound , isCursorInBounds , showMoveNumbers , resetCountdown ] )
294
325
295
326
useEffect ( ( ) => {
296
327
if ( ! board . length ) {
@@ -312,7 +343,7 @@ function Board({gameState, setGameState}) {
312
343
if ( showMoveNumbers ) {
313
344
paintMoveNumbers ( context , board )
314
345
} else {
315
- paintLastMove ( context , lastMove )
346
+ paintLastMove ( context , lastMove , countdownRef . current )
316
347
}
317
348
if ( currentPlayer ( gameState ) !== auth . name ) {
318
349
return
@@ -352,9 +383,10 @@ function Board({gameState, setGameState}) {
352
383
let sub = stompClient . subscribe ( "/topic/move/" + gameId , ( message ) => {
353
384
let move = JSON . parse ( message . body )
354
385
setGameState ( addMove ( gameState , move ) )
386
+ resetCountdown ( )
355
387
} )
356
388
return sub . unsubscribe
357
- } , [ gameState , setGameState , stompClient , gameId ] )
389
+ } , [ gameState , setGameState , stompClient , gameId , resetCountdown ] )
358
390
359
391
if ( ! board . length ) {
360
392
return < div > Loading...</ div >
0 commit comments