Skip to content

Commit

Permalink
Prevent crash when a player runs out of time
Browse files Browse the repository at this point in the history
  • Loading branch information
wjgr2004 committed Dec 27, 2024
1 parent d439bd8 commit 4caf5de
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
29 changes: 11 additions & 18 deletions src/components/boards/BoardGame.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ function BoardGame() {
const headers = useStore(store, (s) => s.headers);
const setFen = useStore(store, (s) => s.setFen);
const setHeaders = useStore(store, (s) => s.setHeaders);
const setResult = useStore(store, (s) => s.setResult);
const appendMove = useStore(store, (s) => s.appendMove);

const [, setTabs] = useAtom(tabsAtom);
Expand Down Expand Up @@ -452,17 +453,10 @@ function BoardGame() {

useEffect(() => {
if (gameState === "playing" && whiteTime !== null && whiteTime <= 0) {
if (intervalId) {
clearInterval(intervalId);
}
setIntervalId(null);
setGameState("gameOver");
setHeaders({
...headers,
result: "0-1",
});
setResult("0-1");
}
}, [gameState, whiteTime, setGameState, setHeaders, headers]);
}, [gameState, whiteTime, setGameState, setResult]);

useEffect(() => {
if (gameState !== "playing") {
Expand All @@ -476,18 +470,17 @@ function BoardGame() {
useEffect(() => {
if (gameState === "playing" && blackTime !== null && blackTime <= 0) {
setGameState("gameOver");
setHeaders({
...headers,
result: "1-0",
});
setResult("1-0");
}
}, [gameState, blackTime, setGameState, setHeaders, headers]);
}, [gameState, blackTime, setGameState, setResult]);

function decrementTime() {
if (pos?.turn === "white" && whiteTime !== null) {
setWhiteTime((prev) => prev! - 100);
} else if (pos?.turn === "black" && blackTime !== null) {
setBlackTime((prev) => prev! - 100);
if (gameState === "playing") {
if (pos?.turn === "white" && whiteTime !== null) {
setWhiteTime((prev) => prev! - 100);
} else if (pos?.turn === "black" && blackTime !== null) {
setBlackTime((prev) => prev! - 100);
}
}
}

Expand Down
10 changes: 9 additions & 1 deletion src/state/store.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { BestMoves, Score } from "@/bindings";
import type { BestMoves, Outcome, Score } from "@/bindings";
import { ANNOTATION_INFO, type Annotation } from "@/utils/annotation";
import { getPGN } from "@/utils/chess";
import { parseSanOrUci, positionFromFen } from "@/utils/chessops";
Expand Down Expand Up @@ -72,6 +72,7 @@ export interface TreeStoreState {
setAnnotation: (payload: Annotation) => void;
setComment: (payload: string) => void;
setHeaders: (payload: GameHeaders) => void;
setResult: (payload: Outcome) => void;
setShapes: (shapes: DrawShape[]) => void;
setScore: (score: Score) => void;

Expand Down Expand Up @@ -440,6 +441,13 @@ export const createTreeStore = (id?: string, initialTree?: TreeState) => {
}
}),
),
setResult: (result) =>
set(
produce((state) => {
state.dirty = true;
state.headers.result = result;
}),
),
setShapes: (shapes) =>
set(
produce((state) => {
Expand Down

0 comments on commit 4caf5de

Please sign in to comment.