@@ -60,10 +60,23 @@ import {
60
60
createGameState ,
61
61
} from "./state.js"
62
62
63
- export const Game = ( ) => {
63
+ export function Game ( ) {
64
+ let [ gameState , setGameState ] = useState ( initialState ( ) )
65
+ let sidebarWidth = useLayoutStore ( state => state . sidebarWidth . game )
66
+ return (
67
+ < div
68
+ style = { { width : vw ( ) - sidebarWidth } }
69
+ className = "h-full" >
70
+ < MuteIcon />
71
+ < Board gameState = { gameState } setGameState = { setGameState } />
72
+ < GamePanel gameState = { gameState } />
73
+ </ div >
74
+ )
75
+ }
76
+
77
+ function Board ( { gameState, setGameState} ) {
64
78
let [ cursor_x , setCursor_x ] = useState ( - 1 )
65
79
let [ cursor_y , setCursor_y ] = useState ( - 1 )
66
- let [ gameState , setGameState ] = useState ( initialState ( ) )
67
80
let zoom = useViewStateStore ( state => state . zoom )
68
81
let { gameId} = useParams ( )
69
82
let toggleChatState = useChatStore ( state => state . toggleChatState )
@@ -80,10 +93,8 @@ export const Game = () => {
80
93
let [ forbidden_x , forbidden_y ] = gameState . forbidden
81
94
let canvasRef = useRef ( )
82
95
let countingGroup = ! gameHasEnded ( gameState ) && counting ? getCountingGroup ( board , cursor_x , cursor_y ) : undefined
83
- let sidebarWidth = useLayoutStore ( state => state . sidebarWidth . game )
84
96
let dragging = useLayoutStore ( state => state . dragging )
85
97
let muted = useMuteStore ( state => state . muted )
86
- let setMuteState = useMuteStore ( ( state ) => state . setMuted )
87
98
let howler = useRef ( )
88
99
let playClickSound = useCallback ( ( ) => {
89
100
if ( muted ) {
@@ -224,15 +235,7 @@ export const Game = () => {
224
235
destination : "/app/game/move" ,
225
236
body : JSON . stringify ( move ) ,
226
237
} )
227
- } , [ gameState , context , auth , board , stompClient , counting , forbidden_x , forbidden_y , movesLength , myColor , playClickSound ] )
228
-
229
- let onMuteClick = useCallback ( ( ) => {
230
- if ( muted ) {
231
- setMuteState ( false )
232
- } else {
233
- setMuteState ( true )
234
- }
235
- } , [ setMuteState , muted ] )
238
+ } , [ gameState , setGameState , toggleChatState , context , auth , board , stompClient , counting , forbidden_x , forbidden_y , movesLength , myColor , playClickSound ] )
236
239
237
240
useEffect ( ( ) => {
238
241
if ( ! board . length ) {
@@ -286,7 +289,7 @@ export const Game = () => {
286
289
setGameState ( createGameState ( game , auth ) )
287
290
toggleChatState ( )
288
291
} , ( ) => navigate ( base + "/lobby" ) )
289
- } , [ queueStatus , auth , id , gameId , navigate ] )
292
+ } , [ setGameState , toggleChatState , queueStatus , auth , id , gameId , navigate ] )
290
293
291
294
useEffect ( ( ) => {
292
295
let sub = stompClient . subscribe ( "/topic/move/" + gameId , ( message ) => {
@@ -295,43 +298,23 @@ export const Game = () => {
295
298
toggleChatState ( )
296
299
} )
297
300
return sub . unsubscribe
298
- } , [ gameState , stompClient , gameId ] )
301
+ } , [ gameState , setGameState , toggleChatState , stompClient , gameId ] )
299
302
300
303
if ( ! board . length ) {
301
304
return < div > Loading...</ div >
302
305
}
303
306
304
307
return (
305
- < div
306
- style = { { width : ( vw ( ) - sidebarWidth ) + "px" } }
307
- className = "h-full" >
308
- < div className = "grid h-full" >
309
- < canvas className = "place-self-center" ref = { canvasRef }
310
- onMouseLeave = { ( ) => {
311
- setCursor_x ( - 1 )
312
- setCursor_y ( - 1 )
313
- } }
314
- onMouseMove = { onMouseMove }
315
- onClick = { onClick }
316
- width = { context . width } height = { context . width } >
317
- </ canvas >
318
- </ div >
319
- < div className = "absolute left-2 top-2" >
320
- < button onClick = { onMuteClick } >
321
- < IconContext . Provider value = { {
322
- size : "1.5em" ,
323
- className : "pl-[4px]" ,
324
- } } >
325
- { muted && (
326
- < FaVolumeMute />
327
- ) }
328
- { ! muted && (
329
- < FaVolumeUp />
330
- ) }
331
- </ IconContext . Provider >
332
- </ button >
333
- </ div >
334
- < GamePanel gameState = { gameState } />
308
+ < div className = "grid h-full" >
309
+ < canvas className = "place-self-center" ref = { canvasRef }
310
+ onMouseLeave = { ( ) => {
311
+ setCursor_x ( - 1 )
312
+ setCursor_y ( - 1 )
313
+ } }
314
+ onMouseMove = { onMouseMove }
315
+ onClick = { onClick }
316
+ width = { context . width } height = { context . width } >
317
+ </ canvas >
335
318
</ div >
336
319
)
337
320
}
@@ -357,3 +340,20 @@ function getCountingGroup(board, cursor_x, cursor_y) {
357
340
}
358
341
return has
359
342
}
343
+
344
+ function MuteIcon ( ) {
345
+ let toggleMuted = useMuteStore ( ( state ) => state . toggleMuted )
346
+ let muted = useMuteStore ( state => state . muted )
347
+ return (
348
+ < div className = "absolute left-2 top-2" >
349
+ < button onClick = { toggleMuted } >
350
+ < IconContext . Provider value = { {
351
+ size : "1.5em" ,
352
+ className : "pl-[4px]" ,
353
+ } } >
354
+ { muted ? < FaVolumeMute /> : < FaVolumeUp /> }
355
+ </ IconContext . Provider >
356
+ </ button >
357
+ </ div >
358
+ )
359
+ }
0 commit comments