Skip to content

Commit

Permalink
Jump to next puzzle immediately (#355)
Browse files Browse the repository at this point in the history
  • Loading branch information
JayceFFT authored Sep 6, 2024
1 parent cc4f78c commit 2125233
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/components/puzzles/PuzzleBoard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { Completion, Puzzle } from "@/utils/puzzles";
import { getNodeAtPath, treeIteratorMainLine } from "@/utils/treeReducer";
import { Box } from "@mantine/core";
import { useElementSize, useForceUpdate } from "@mantine/hooks";
import { useLocalStorage, useSessionStorage } from "@mantine/hooks";
import {
Chess,
type Move,
Expand Down Expand Up @@ -41,6 +42,11 @@ function PuzzleBoard({
const makeMove = useStore(store, (s) => s.makeMove);
const makeMoves = useStore(store, (s) => s.makeMoves);
const reset = useForceUpdate();
const [jumpToNextPuzzleImmediately, setJumpToNextPuzzleImmediately] =
useLocalStorage<boolean>({
key: "puzzle-jump-immediately",
defaultValue: true,
});

const currentNode = getNodeAtPath(root, position);

Expand Down Expand Up @@ -77,17 +83,20 @@ function PuzzleBoard({

function checkMove(move: Move) {
if (!pos) return;
if (!puzzle) return;

const newPos = pos.clone();
const uci = makeUci(move);
newPos.play(move);
if (puzzle && (puzzle.moves[currentMove] === uci || newPos.isCheckmate())) {

if (puzzle.moves[currentMove] === uci) {
if (currentMove === puzzle.moves.length - 1) {
if (puzzle.completion !== "incorrect") {
changeCompletion("correct");
}
setEnded(false);

if (db) {
if (db && newPos.isCheckmate() && jumpToNextPuzzleImmediately) {
generatePuzzle(db);
}
}
Expand Down
18 changes: 18 additions & 0 deletions src/components/puzzles/Puzzles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
ScrollArea,
Select,
Stack,
Switch,
Text,
Tooltip,
} from "@mantine/core";
Expand All @@ -40,6 +41,7 @@ import { Chess, parseUci } from "chessops";
import { parseFen } from "chessops/fen";
import { useAtom, useSetAtom } from "jotai";
import { useContext, useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import { useStore } from "zustand";
import GameNotation from "../boards/GameNotation";
import ChallengeHistory from "../common/ChallengeHistory";
Expand All @@ -49,6 +51,7 @@ import AddPuzzle from "./AddPuzzle";
import PuzzleBoard from "./PuzzleBoard";

function Puzzles({ id }: { id: string }) {
const { t } = useTranslation();
const store = useContext(TreeStateContext)!;
const setFen = useStore(store, (s) => s.setFen);
const goToStart = useStore(store, (s) => s.goToStart);
Expand All @@ -74,6 +77,12 @@ function Puzzles({ id }: { id: string }) {
defaultValue: [1000, 1500],
});

const [jumpToNextPuzzleImmediately, setJumpToNextPuzzleImmediately] =
useLocalStorage<boolean>({
key: "puzzle-jump-immediately",
defaultValue: true,
});

const wonPuzzles = puzzles.filter(
(puzzle) => puzzle.completion === "correct",
);
Expand Down Expand Up @@ -249,6 +258,15 @@ function Puzzles({ id }: { id: string }) {
</Text>
)}
<Group>
<Switch
defaultChecked
onChange={(event) =>
setJumpToNextPuzzleImmediately(event.currentTarget.checked)
}
checked={jumpToNextPuzzleImmediately}
label={t("Puzzle.JumpToNextPuzzleImmediately")}
/>

<Tooltip label="New Puzzle">
<ActionIcon
disabled={!selectedDb}
Expand Down
2 changes: 2 additions & 0 deletions src/translation/en_US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,5 +356,7 @@ export const en_US = {
"PgnInput.ExtraMarkups": "Extra Markups",
"PgnInput.MaxDepth": "Max Depth",
"PgnInput.TotalMoves": "Total Moves",

"Puzzle.JumpToNextPuzzleImmediately": "Jump to next puzzle immediately",
},
};

0 comments on commit 2125233

Please sign in to comment.