1
+ import {
2
+ twJoin ,
3
+ } from "tailwind-merge"
1
4
import {
2
5
useEffect ,
3
6
useCallback ,
@@ -100,7 +103,6 @@ function Board({gameState, setGameState}) {
100
103
let board = gameState . board
101
104
let [ forbidden_x , forbidden_y ] = gameState . forbidden
102
105
let canvasRef = useRef ( )
103
- let countingGroup = ! gameHasEnded ( gameState ) && counting ? getCountingGroup ( board , cursor_x , cursor_y ) : undefined
104
106
let dragging = useLayoutStore ( state => state . dragging )
105
107
let muted = useMuteStore ( state => state . muted )
106
108
let howler = useRef ( )
@@ -138,6 +140,32 @@ function Board({gameState, setGameState}) {
138
140
}
139
141
} , [ setCtrlKeyDown ] )
140
142
143
+ let isCursorInBounds = useCallback ( ( ) => {
144
+ let dim = board . length
145
+ let x = cursorXref . current
146
+ let y = cursorYref . current
147
+ return x >= 0 && x < dim && y >= 0 && y < dim
148
+ } , [ board . length ] )
149
+
150
+ let getCountingGroup = useCallback ( ( ) => {
151
+ if ( gameHasEnded ( gameState ) ) {
152
+ return undefined
153
+ }
154
+ if ( ! gameState . counting ) {
155
+ return undefined
156
+ }
157
+ if ( ! isCursorInBounds ( ) ) {
158
+ return undefined
159
+ }
160
+ let x = cursorXref . current
161
+ let y = cursorYref . current
162
+ let { has, hasStone} = board [ x ] [ y ]
163
+ if ( ! hasStone ) {
164
+ return undefined
165
+ }
166
+ return has
167
+ } , [ gameState , board , isCursorInBounds ] )
168
+
141
169
let context = useMemo ( ( ) => {
142
170
let dim = board . length
143
171
if ( ! dim ) {
@@ -190,15 +218,12 @@ function Board({gameState, setGameState}) {
190
218
ctrlKeyDownRef,
191
219
cursorXref,
192
220
cursorYref,
193
- isCursorInBounds : function ( x , y ) {
194
- return x >= 0 && x < dim && y >= 0 && y < dim
195
- } ,
196
221
hoshis : hoshis ,
197
222
stoneRadius : getRadius ( step * 0.475 ) ,
198
223
territoryRadius : getRadius ( step * 0.125 ) ,
199
224
hoshiRadius : getRadius ( step * 0.0625 ) ,
200
225
}
201
- } , [ board . length , canvasRef , zoom ] )
226
+ } , [ board , canvasRef , zoom ] )
202
227
203
228
let onMouseMove = useCallback ( ( e ) => {
204
229
if ( dragging ) {
@@ -207,10 +232,11 @@ function Board({gameState, setGameState}) {
207
232
if ( ! board . length ) {
208
233
return
209
234
}
235
+ let dim = board . length
210
236
setCtrlKeyDown ( e . ctrlKey )
211
237
let cursor_x = Math . round ( ( e . nativeEvent . offsetX - context . margin ) / context . step )
212
238
let cursor_y = Math . round ( ( e . nativeEvent . offsetY - context . margin ) / context . step )
213
- if ( context . isCursorInBounds ( cursor_x , cursor_y ) ) {
239
+ if ( cursor_x >= 0 && cursor_x < dim && cursor_y >= 0 && cursor_y < dim ) {
214
240
setCursor_x ( cursor_x + 0 )
215
241
setCursor_y ( cursor_y + 0 )
216
242
} else {
@@ -219,16 +245,16 @@ function Board({gameState, setGameState}) {
219
245
}
220
246
} , [ context , board . length , dragging ] )
221
247
222
- let onClick = useCallback ( ( e ) => {
248
+ let onClick = useCallback ( ( ) => {
249
+ let cursor_x = cursorXref . current
250
+ let cursor_y = cursorYref . current
223
251
if ( gameHasEnded ( gameState ) ) {
224
252
return
225
253
}
226
254
if ( ! board . length ) {
227
255
return
228
256
}
229
- let cursor_x = Math . round ( ( e . nativeEvent . offsetX - context . margin ) / context . step )
230
- let cursor_y = Math . round ( ( e . nativeEvent . offsetY - context . margin ) / context . step )
231
- if ( ! context . isCursorInBounds ( cursor_x , cursor_y ) ) {
257
+ if ( ! isCursorInBounds ( ) ) {
232
258
return
233
259
}
234
260
if ( counting ) {
@@ -259,7 +285,7 @@ function Board({gameState, setGameState}) {
259
285
destination : "/app/game/move" ,
260
286
body : JSON . stringify ( move ) ,
261
287
} )
262
- } , [ gameState , setGameState , context , auth , board , stompClient , counting , forbidden_x , forbidden_y , movesLength , myColor , playClickSound ] )
288
+ } , [ gameState , setGameState , auth , board , stompClient , counting , forbidden_x , forbidden_y , movesLength , myColor , playClickSound , isCursorInBounds ] )
263
289
264
290
useEffect ( ( ) => {
265
291
if ( ! board . length ) {
@@ -275,7 +301,7 @@ function Board({gameState, setGameState}) {
275
301
paintGrid ( context )
276
302
let showMoveNumbers = ctrlKeyDownRef . current && ( isKibitz ( gameState , auth ) || gameHasEnded ( gameState ) )
277
303
if ( counting && ! isReviewing ( gameState ) ) {
278
- paintStonesCounting ( context , board , countingGroup )
304
+ paintStonesCounting ( context , board , getCountingGroup ( ) )
279
305
if ( showMoveNumbers ) {
280
306
paintMoveNumbers ( context , board )
281
307
}
@@ -290,7 +316,7 @@ function Board({gameState, setGameState}) {
290
316
if ( currentPlayer ( gameState ) !== auth . name ) {
291
317
return
292
318
}
293
- if ( ! context . isCursorInBounds ( cursor_x , cursor_y ) ) {
319
+ if ( ! isCursorInBounds ( ) ) {
294
320
return
295
321
}
296
322
if ( board [ cursor_y ] [ cursor_x ] . hasStone ) {
@@ -303,7 +329,7 @@ function Board({gameState, setGameState}) {
303
329
return
304
330
}
305
331
paintShadow ( context , cursor_x , cursor_y , currentColor ( gameState ) )
306
- } , [ gameState , cursor_x , cursor_y , context , ctrlKeyDown , canvasRef , auth , board , counting , countingGroup , forbidden_x , forbidden_y , lastMove ] )
332
+ } , [ gameState , context , cursor_x , cursor_y , ctrlKeyDown , canvasRef , auth , board , counting , forbidden_x , forbidden_y , lastMove , isCursorInBounds , getCountingGroup ] )
307
333
308
334
useEffect ( ( ) => {
309
335
if ( id === gameId && queueStatus === "up_to_date" ) {
@@ -333,7 +359,7 @@ function Board({gameState, setGameState}) {
333
359
334
360
return (
335
361
< div className = "grid h-full" >
336
- < canvas className = "place-self-center" ref = { canvasRef }
362
+ < canvas className = { twJoin ( "place-self-center" ) } ref = { canvasRef }
337
363
onMouseLeave = { ( ) => {
338
364
setCursor_x ( - 1 )
339
365
setCursor_y ( - 1 )
@@ -354,20 +380,6 @@ function getRadius(radius) {
354
380
return diameter / 2
355
381
}
356
382
357
- function getCountingGroup ( board , cursor_x , cursor_y ) {
358
- if ( cursor_x < 0 ||
359
- cursor_x >= board . length ||
360
- cursor_y < 0 ||
361
- cursor_y >= board . length ) {
362
- return undefined
363
- }
364
- let { has, hasStone} = board [ cursor_y ] [ cursor_x ]
365
- if ( ! hasStone ) {
366
- return undefined
367
- }
368
- return has
369
- }
370
-
371
383
function MuteIcon ( ) {
372
384
let toggleMuted = useMuteStore ( ( state ) => state . toggleMuted )
373
385
let muted = useMuteStore ( state => state . muted )
0 commit comments