@@ -37,6 +37,7 @@ import {
37
37
createGameState ,
38
38
isCounting ,
39
39
gameHasEnded ,
40
+ isCurrentlyPlacingHandicapStones ,
40
41
} from "./state.js"
41
42
import {
42
43
Board ,
@@ -70,30 +71,30 @@ export function Game() {
70
71
cursorYref . current = cursor_y
71
72
let canvasRef = useRef ( )
72
73
let zoom = useViewStateStore ( state => state . zoom )
74
+ let counting = isCounting ( gameState )
75
+ let gameEnded = gameHasEnded ( gameState )
76
+ let placingHandicap = isCurrentlyPlacingHandicapStones ( gameState )
77
+ let movesLength = gameState . moves . length
73
78
74
79
let resetCountdown = useCallback ( ( ) => {
75
- if ( intervalIdRef . current ) {
76
- clearInterval ( intervalIdRef . current )
80
+ let intervalId = intervalIdRef . current
81
+ if ( intervalId ) {
82
+ window . clearInterval ( intervalId )
83
+ }
84
+ if ( placingHandicap || counting || gameEnded ) {
85
+ return
77
86
}
78
87
setTimeRemaining ( timesetting )
79
88
if ( ! timesetting ) {
80
89
return
81
90
}
82
91
intervalIdRef . current = setInterval ( ( ) => {
83
- let gameState = gameStateRef . current
84
- if ( ! gameState ) {
85
- return
86
- }
87
- if ( isCounting ( gameState ) || gameHasEnded ( gameState ) ) {
88
- if ( intervalIdRef . current ) {
89
- clearInterval ( intervalIdRef . current )
90
- }
91
- return
92
- }
93
92
let t = timeRemainingRef . current - 1
94
93
setTimeRemaining ( t )
95
94
if ( t <= 0 ) {
96
- clearInterval ( intervalIdRef . current )
95
+ if ( intervalId ) {
96
+ window . clearInterval ( intervalId )
97
+ }
97
98
window . setTimeout ( ( ) => {
98
99
stompClient . publish ( {
99
100
destination : "/app/game/move" ,
@@ -102,7 +103,11 @@ export function Game() {
102
103
} , Math . trunc ( Math . random ( ) * 200 ) )
103
104
}
104
105
} , 1000 )
105
- } , [ setTimeRemaining , timesetting , stompClient ] )
106
+ } , [ setTimeRemaining , timesetting , stompClient , counting , gameEnded , placingHandicap ] )
107
+
108
+ useEffect ( ( ) => {
109
+ resetCountdown ( )
110
+ } , [ resetCountdown , movesLength ] )
106
111
107
112
useEffect ( ( ) => {
108
113
resetCountdown ( )
@@ -118,7 +123,6 @@ export function Game() {
118
123
let move = JSON . parse ( message . body )
119
124
let newState = addMove ( gameStateRef . current , move )
120
125
setGameState ( newState )
121
- resetCountdown ( )
122
126
} )
123
127
return ( ) => {
124
128
sub . unsubscribe ( )
0 commit comments