Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent crash when a player runs out of time #481

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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