@@ -66,53 +66,21 @@ import {
66
66
} from "./BoardSettings.jsx"
67
67
68
68
export function Game ( ) {
69
+ let stompClient = useContext ( StompContext )
70
+ let auth = useAuthStore ( state => state . auth )
71
+ let navigate = useNavigate ( )
72
+ let { gameId} = useParams ( )
69
73
let [ gameState , setGameState ] = useState ( initialState ( ) )
74
+ let queueStatus = gameState . queueStatus
75
+ let timesetting = gameState . timesetting
70
76
let sidebarWidth = useLayoutStore ( state => state . sidebarWidth . game )
71
- return (
72
- < div
73
- style = { { width : vw ( ) - sidebarWidth } }
74
- className = "h-full" >
75
- < BoardSettings gameId = { gameState . id } black = { gameState . black } white = { gameState . white } />
76
- < Board gameState = { gameState } setGameState = { setGameState } />
77
- < GamePanel gameState = { gameState } setGameState = { setGameState } />
78
- </ div >
79
- )
80
- }
81
-
82
- function Board ( { gameState, setGameState} ) {
83
- let [ cursor_x , setCursor_x ] = useState ( - 1 )
84
- let [ cursor_y , setCursor_y ] = useState ( - 1 )
77
+ let gameStateRef = useRef ( )
78
+ gameStateRef . current = gameState
79
+ let intervalIdRef = useRef ( )
85
80
let timeRemaining = useTimeoutStore ( state => state . timeRemaining )
86
- let timesetting = gameState . timesetting
87
81
let setTimeRemaining = useTimeoutStore ( state => state . setTimeRemaining )
88
82
let timeRemainingRef = useRef ( )
89
83
timeRemainingRef . current = timeRemaining
90
- let gameStateRef = useRef ( )
91
- gameStateRef . current = gameState
92
- let [ ctrlKeyDown , setCtrlKeyDown ] = useState ( false )
93
- let zoom = useViewStateStore ( state => state . zoom )
94
- let { gameId} = useParams ( )
95
- let navigate = useNavigate ( )
96
- let stompClient = useContext ( StompContext )
97
- let auth = useAuthStore ( state => state . auth )
98
- let cursorXref = useRef ( )
99
- cursorXref . current = cursor_x
100
- let cursorYref = useRef ( )
101
- cursorYref . current = cursor_y
102
- let id = gameState . id
103
- let lastMove = gameState . lastMove
104
- let queueStatus = gameState . queueStatus
105
- let myColor = gameState . myColor
106
- let counting = isCounting ( gameState )
107
- let board = gameState . board
108
- let [ forbidden_x , forbidden_y ] = gameState . forbidden
109
- let canvasRef = useRef ( )
110
- let dragging = useLayoutStore ( state => state . dragging )
111
- let muted = useMuteStore ( state => state . muted )
112
- let howler = useRef ( )
113
- let end = gameHasEnded ( gameState )
114
- let showMoveNumbers = ctrlKeyDown && ( isKibitz ( gameState , auth ) || end )
115
- let intervalIdRef = useRef ( )
116
84
117
85
let resetCountdown = useCallback ( ( ) => {
118
86
if ( intervalIdRef . current ) {
@@ -135,7 +103,6 @@ function Board({gameState, setGameState}) {
135
103
} , 100 )
136
104
}
137
105
} , 1000 )
138
-
139
106
} , [ setTimeRemaining , timesetting , stompClient ] )
140
107
141
108
useEffect ( ( ) => {
@@ -147,6 +114,74 @@ function Board({gameState, setGameState}) {
147
114
}
148
115
} , [ resetCountdown ] )
149
116
117
+ useEffect ( ( ) => {
118
+ let sub = stompClient . subscribe ( "/topic/move/" + gameId , ( message ) => {
119
+ let move = JSON . parse ( message . body )
120
+ let newState = addMove ( gameStateRef . current , move )
121
+ setGameState ( newState )
122
+ resetCountdown ( )
123
+ } )
124
+ return ( ) => {
125
+ sub . unsubscribe ( )
126
+ }
127
+ } , [ gameStateRef , setGameState , stompClient , gameId , resetCountdown ] )
128
+
129
+ useEffect ( ( ) => {
130
+ if ( queueStatus === "up_to_date" ) {
131
+ return
132
+ }
133
+ doTry ( async ( ) => {
134
+ let game = await tfetch ( "/api/game/" + gameId , {
135
+ headers : {
136
+ "Authorization" : "Bearer " + auth . token ,
137
+ } ,
138
+ } )
139
+ setGameState ( createGameState ( game , auth ) )
140
+ } , ( ) => navigate ( base + "/lobby" ) )
141
+ } , [ setGameState , queueStatus , auth , gameId , navigate ] )
142
+
143
+ if ( ! gameState . board . length ) {
144
+ return < div > Loading...</ div >
145
+ }
146
+
147
+ return (
148
+ < div
149
+ style = { { width : vw ( ) - sidebarWidth } }
150
+ className = "h-full" >
151
+ < BoardSettings gameId = { gameId } black = { gameState . black } white = { gameState . white } />
152
+ < Board
153
+ resetCountdown = { resetCountdown }
154
+ timeRemaining = { timeRemaining }
155
+ gameState = { gameState }
156
+ setGameState = { setGameState } />
157
+ < GamePanel gameState = { gameState } setGameState = { setGameState } />
158
+ </ div >
159
+ )
160
+ }
161
+
162
+ function Board ( { gameState, setGameState, resetCountdown, timeRemaining} ) {
163
+ let [ cursor_x , setCursor_x ] = useState ( - 1 )
164
+ let [ cursor_y , setCursor_y ] = useState ( - 1 )
165
+ let [ ctrlKeyDown , setCtrlKeyDown ] = useState ( false )
166
+ let zoom = useViewStateStore ( state => state . zoom )
167
+ let auth = useAuthStore ( state => state . auth )
168
+ let stompClient = useContext ( StompContext )
169
+ let cursorXref = useRef ( )
170
+ cursorXref . current = cursor_x
171
+ let cursorYref = useRef ( )
172
+ cursorYref . current = cursor_y
173
+ let lastMove = gameState . lastMove
174
+ let myColor = gameState . myColor
175
+ let counting = isCounting ( gameState )
176
+ let board = gameState . board
177
+ let [ forbidden_x , forbidden_y ] = gameState . forbidden
178
+ let canvasRef = useRef ( )
179
+ let dragging = useLayoutStore ( state => state . dragging )
180
+ let muted = useMuteStore ( state => state . muted )
181
+ let howler = useRef ( )
182
+ let end = gameHasEnded ( gameState )
183
+ let showMoveNumbers = ctrlKeyDown && ( isKibitz ( gameState , auth ) || end )
184
+
150
185
let playClickSound = useCallback ( ( ) => {
151
186
if ( muted ) {
152
187
return
@@ -336,7 +371,7 @@ function Board({gameState, setGameState}) {
336
371
...move ,
337
372
color : myColor ,
338
373
n : gameState . moves . length ,
339
- } ) )
374
+ } ) )
340
375
}
341
376
resetCountdown ( )
342
377
playClickSound ( )
@@ -366,7 +401,7 @@ function Board({gameState, setGameState}) {
366
401
if ( showMoveNumbers ) {
367
402
paintMoveNumbers ( context , board )
368
403
} else if ( ! counting && ! end ) {
369
- paintLastMove ( context , lastMove , timeRemainingRef . current )
404
+ paintLastMove ( context , lastMove , timeRemaining )
370
405
} else if ( lastMove && ! lastMove . action ) {
371
406
paintNumber ( context , lastMove . x , lastMove . y , lastMove . n + 1 , lastMove . color )
372
407
}
@@ -388,34 +423,7 @@ function Board({gameState, setGameState}) {
388
423
if ( ! showMoveNumbers && ! counting && ! end ) {
389
424
paintShadow ( context , cursor_x , cursor_y , currentColor ( gameState ) )
390
425
}
391
- } , [ gameState , context , cursor_x , cursor_y , ctrlKeyDown , canvasRef , auth , board , counting , forbidden_x , forbidden_y , lastMove , isCursorInBounds , getCountingGroup , showMoveNumbers , end ] )
392
-
393
- useEffect ( ( ) => {
394
- if ( id === gameId && queueStatus === "up_to_date" ) {
395
- return
396
- }
397
- doTry ( async ( ) => {
398
- let game = await tfetch ( "/api/game/" + gameId , {
399
- headers : {
400
- "Authorization" : "Bearer " + auth . token ,
401
- } ,
402
- } )
403
- setGameState ( createGameState ( game , auth ) )
404
- } , ( ) => navigate ( base + "/lobby" ) )
405
- } , [ setGameState , queueStatus , auth , id , gameId , navigate ] )
406
-
407
- useEffect ( ( ) => {
408
- let sub = stompClient . subscribe ( "/topic/move/" + gameId , ( message ) => {
409
- let move = JSON . parse ( message . body )
410
- setGameState ( addMove ( gameState , move ) )
411
- resetCountdown ( )
412
- } )
413
- return sub . unsubscribe
414
- } , [ gameState , setGameState , stompClient , gameId , resetCountdown ] )
415
-
416
- if ( ! board . length ) {
417
- return < div > Loading...</ div >
418
- }
426
+ } , [ gameState , timeRemaining , context , cursor_x , cursor_y , ctrlKeyDown , canvasRef , auth , board , counting , forbidden_x , forbidden_y , lastMove , isCursorInBounds , getCountingGroup , showMoveNumbers , end ] )
419
427
420
428
return (
421
429
< div className = "grid h-full" >
0 commit comments