Skip to content

Commit 4d79218

Browse files
committed
fix timeoutless game
1 parent 6135b1a commit 4d79218

File tree

5 files changed

+35
-25
lines changed

5 files changed

+35
-25
lines changed

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ export function Game() {
8282
function Board({gameState, setGameState}) {
8383
let [cursor_x, setCursor_x] = useState(-1)
8484
let [cursor_y, setCursor_y] = useState(-1)
85-
let timeout = useTimeoutStore(state => state.timeout)
85+
let timeRemaining = useTimeoutStore(state => state.timeRemaining)
8686
let timesetting = gameState.timesetting
87-
let setTimeout = useTimeoutStore(state => state.setTimeout)
88-
let timeoutRef = useRef()
89-
timeoutRef.current = timeout
87+
let setTimeRemaining = useTimeoutStore(state => state.setTimeRemaining)
88+
let timeRemainingRef = useRef()
89+
timeRemainingRef.current = timeRemaining
9090
let gameStateRef = useRef()
9191
gameStateRef.current = gameState
9292
let [ctrlKeyDown, setCtrlKeyDown] = useState(false)
@@ -118,10 +118,13 @@ function Board({gameState, setGameState}) {
118118
if (intervalIdRef.current) {
119119
clearInterval(intervalIdRef.current)
120120
}
121-
setTimeout(timesetting)
121+
setTimeRemaining(timesetting)
122+
if (!timesetting) {
123+
return
124+
}
122125
intervalIdRef.current = setInterval(() => {
123-
let t = timeoutRef.current - 1
124-
setTimeout(t)
126+
let t = timeRemainingRef.current - 1
127+
setTimeRemaining(t)
125128
if (t <= 0) {
126129
clearInterval(intervalIdRef.current)
127130
window.setTimeout(() => {
@@ -133,7 +136,7 @@ function Board({gameState, setGameState}) {
133136
}
134137
}, 1000)
135138

136-
}, [setTimeout, timesetting, stompClient])
139+
}, [setTimeRemaining, timesetting, stompClient])
137140

138141
useEffect(() => {
139142
resetCountdown()
@@ -265,9 +268,9 @@ function Board({gameState, setGameState}) {
265268

266269
useEffect(() => {
267270
if (!showMoveNumbers && !counting && !end) {
268-
paintLastMove(context, lastMove, timeout)
271+
paintLastMove(context, lastMove, timeRemaining)
269272
}
270-
}, [showMoveNumbers, context, lastMove, timeout, counting, end])
273+
}, [showMoveNumbers, context, lastMove, timeRemaining, counting, end])
271274

272275
let onMouseMove = useCallback((e) => {
273276
if (dragging) {
@@ -363,7 +366,7 @@ function Board({gameState, setGameState}) {
363366
if (showMoveNumbers) {
364367
paintMoveNumbers(context, board)
365368
} else if (!counting && !end) {
366-
paintLastMove(context, lastMove, timeoutRef.current)
369+
paintLastMove(context, lastMove, timeRemainingRef.current)
367370
} else if (lastMove && !lastMove.action) {
368371
paintNumber(context, lastMove.x, lastMove.y, lastMove.n + 1, lastMove.color)
369372
}

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,14 @@ function WhoIsWho({gameState}) {
8989
let {black, white} = gameState
9090
let counting = isCounting(gameState)
9191
let end = gameHasEnded(gameState)
92-
let timeout = useTimeoutStore(state => state.timeout)
92+
let timeRemaining = useTimeoutStore(state => state.timeRemaining)
9393
let timesetting = gameState.timesetting
9494
let color = currentColor(gameState)
9595
let navigate = useNavigate()
9696
let onExit = useCallback(() => {
9797
navigate(base + "/lobby")
9898
}, [navigate])
99+
let timeClassname = timeRemaining > 0 && timeRemaining <= 5 ? "font-bold text-red-500" : "text-white"
99100
return (
100101
<div className="flex-none grid grid-cols-[max-content_max-content_max-content_auto_max-content] w-full gap-x-4">
101102
<div className="flex"><BabyStone color="white" className="pr-1" />{white}</div>
@@ -110,11 +111,17 @@ function WhoIsWho({gameState}) {
110111
<IoMdExit />
111112
</IconContext.Provider>
112113
</button>
113-
<div>{!counting && !end && color === WHITE ? timeout : timesetting}</div>
114-
<div />
115-
<div>{!counting && !end && color === BLACK ? timeout : timesetting}</div>
116-
<div />
117-
<div />
114+
{timesetting ? <>
115+
<div className={color === WHITE ? timeClassname : ""}>
116+
{!counting && !end && color === WHITE ? timeRemaining : timesetting}
117+
</div>
118+
<div />
119+
<div className={color === BLACK ? timeClassname : ""}>
120+
{!counting && !end && color === BLACK ? timeRemaining : timesetting}
121+
</div>
122+
<div />
123+
<div />
124+
</> : ""}
118125
</div>
119126
)
120127
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ export function paintNumber(context, grid_x, grid_y, n, color) {
199199
ctx.fillText(n, x, y)
200200
}
201201

202-
export function paintLastMove(context, lastMove, countdown) {
202+
export function paintLastMove(context, lastMove, timeRemaining) {
203203
if (!lastMove) {
204204
return
205205
}
@@ -211,8 +211,8 @@ export function paintLastMove(context, lastMove, countdown) {
211211
paintStone(context, grid_x, grid_y, color)
212212
let [x, y] = grid[grid_y][grid_x]
213213
let ctx = canvasRef.current.getContext("2d")
214-
if (countdown && countdown < 10 && countdown > 0) {
215-
paintNumber(context, grid_x, grid_y, countdown, color)
214+
if (timeRemaining > 0 && timeRemaining < 10) {
215+
paintNumber(context, grid_x, grid_y, timeRemaining, color)
216216
return
217217
}
218218
let length = stoneRadius * 0.875

src/main/client/src/store.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ export const useMuteStore = create(
4747
)
4848

4949
export const useTimeoutStore = create(set => ({
50-
timeout: 10,
51-
setTimeout: timeout => set(produce(draft => {
52-
draft.timeout = timeout
50+
timeRemaining: 10,
51+
setTimeRemaining: timeRemaining => set(produce(draft => {
52+
draft.timeRemaining = timeRemaining
5353
}), true),
5454
}))

src/main/java/com/bernd/GameController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.bernd.game.Board;
44
import com.bernd.game.MoveList;
55
import com.bernd.model.AcceptRequest;
6-
import com.bernd.model.ActiveGame;
76
import com.bernd.model.Chat;
87
import com.bernd.model.ChatMessage;
98
import com.bernd.model.Game;
@@ -93,7 +92,8 @@ public void action(Move move, Principal p) {
9392
}
9493
int color = getColorFromGameState(game);
9594
Chat chat = chats.get(game.id());
96-
if (System.currentTimeMillis() > game.updated() + game.timesetting() * 1000L) {
95+
if (game.timesetting() != 0
96+
&& System.currentTimeMillis() > game.updated() + game.timesetting() * 1000L) {
9797
games.put(game.withTimeoutState());
9898
String text = color == Board.W ? "B+Time" : "W+Time";
9999
ChatMessage message = new ChatMessage(chat.counter().getAndIncrement(), text, null, "status", null);

0 commit comments

Comments
 (0)