Skip to content

Commit 08d3e3a

Browse files
committed
time: show countdown
1 parent b0617d9 commit 08d3e3a

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

src/main/client/src/feature/game/Game.jsx

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ export function Game() {
7979
function Board({gameState, setGameState}) {
8080
let [cursor_x, setCursor_x] = useState(-1)
8181
let [cursor_y, setCursor_y] = useState(-1)
82+
let [countdown, setCountdown] = useState(9)
83+
let countdownRef = useRef()
84+
countdownRef.current = countdown
8285
let [ctrlKeyDown, setCtrlKeyDown] = useState(false)
8386
let zoom = useViewStateStore(state => state.zoom)
8487
let {gameId} = useParams()
@@ -100,6 +103,29 @@ function Board({gameState, setGameState}) {
100103
let dragging = useLayoutStore(state => state.dragging)
101104
let muted = useMuteStore(state => state.muted)
102105
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+
103129
let playClickSound = useCallback(() => {
104130
if (muted) {
105131
return
@@ -164,8 +190,6 @@ function Board({gameState, setGameState}) {
164190
return has
165191
}, [gameState, counting, board, isCursorInBounds])
166192

167-
let showMoveNumbers = ctrlKeyDown && (isKibitz(gameState, auth) || gameHasEnded(gameState))
168-
169193
let context = useMemo(() => {
170194
let dim = board.length
171195
if (!dim) {
@@ -224,6 +248,12 @@ function Board({gameState, setGameState}) {
224248
}
225249
}, [board, canvasRef, zoom])
226250

251+
useEffect(() => {
252+
if (!showMoveNumbers) {
253+
paintLastMove(context, lastMove, countdown)
254+
}
255+
}, [showMoveNumbers, context, lastMove, countdown])
256+
227257
let onMouseMove = useCallback((e) => {
228258
if (dragging) {
229259
return
@@ -285,12 +315,13 @@ function Board({gameState, setGameState}) {
285315
if (!isSelfPlay(gameState)) { // can't add early in self play; myColor is 0
286316
setGameState(addMove(gameState, {...move, color: myColor})) // early add move
287317
}
318+
resetCountdown()
288319
playClickSound()
289320
stompClient.publish({
290321
destination: "/app/game/move",
291322
body: JSON.stringify(move),
292323
})
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])
294325

295326
useEffect(() => {
296327
if (!board.length) {
@@ -312,7 +343,7 @@ function Board({gameState, setGameState}) {
312343
if (showMoveNumbers) {
313344
paintMoveNumbers(context, board)
314345
} else {
315-
paintLastMove(context, lastMove)
346+
paintLastMove(context, lastMove, countdownRef.current)
316347
}
317348
if (currentPlayer(gameState) !== auth.name) {
318349
return
@@ -352,9 +383,10 @@ function Board({gameState, setGameState}) {
352383
let sub = stompClient.subscribe("/topic/move/" + gameId, (message) => {
353384
let move = JSON.parse(message.body)
354385
setGameState(addMove(gameState, move))
386+
resetCountdown()
355387
})
356388
return sub.unsubscribe
357-
}, [gameState, setGameState, stompClient, gameId])
389+
}, [gameState, setGameState, stompClient, gameId, resetCountdown])
358390

359391
if (!board.length) {
360392
return <div>Loading...</div>

src/main/client/src/feature/game/paint.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,16 +174,26 @@ function paintMoveNumber({cursorXref, cursorYref, stoneRadius, canvasRef, grid},
174174
ctx.fillText(historyEntry.n + 1, x, y)
175175
}
176176

177-
export function paintLastMove({canvasRef, grid, stoneRadius}, lastMove) {
177+
export function paintLastMove(context, lastMove, countdown) {
178178
if (!lastMove) {
179179
return
180180
}
181+
let {canvasRef, grid, stoneRadius} = context
181182
let {x: grid_x, y: grid_y, color} = lastMove
182183
let style = color === BLACK ?
183184
"rgba(255,255,255)" :
184185
"rgba(0,0,0)"
186+
paintStone(context, grid_x, grid_y, color)
185187
let [x, y] = grid[grid_y][grid_x]
186188
let ctx = canvasRef.current.getContext("2d")
189+
if (countdown && countdown < 10 && countdown > 0) {
190+
ctx.font = "bold " + Math.trunc(stoneRadius * 1.125) + "px sans-serif"
191+
ctx.textBaseline = "middle"
192+
ctx.textAlign = "center"
193+
ctx.fillStyle = style
194+
ctx.fillText(countdown, x, y)
195+
return
196+
}
187197
let length = stoneRadius * 0.875
188198
ctx.fillStyle = style
189199
ctx.beginPath()

0 commit comments

Comments
 (0)