fix : play board optimization
This commit is contained in:
24
src/hooks/useGameData.ts
Normal file
24
src/hooks/useGameData.ts
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
import { Chess, Move } from "chess.js";
|
||||||
|
import { PrimitiveAtom, useAtom, useAtomValue } from "jotai";
|
||||||
|
import { useEffect } from "react";
|
||||||
|
|
||||||
|
export interface GameData {
|
||||||
|
history: Move[];
|
||||||
|
lastMove: Move | undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const useGameData = (
|
||||||
|
gameAtom: PrimitiveAtom<Chess>,
|
||||||
|
gameDataAtom: PrimitiveAtom<GameData>
|
||||||
|
) => {
|
||||||
|
const game = useAtomValue(gameAtom);
|
||||||
|
const [gameData, setGameData] = useAtom(gameDataAtom);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const history = game.history({ verbose: true });
|
||||||
|
const lastMove = history.at(-1);
|
||||||
|
setGameData({ history, lastMove });
|
||||||
|
}, [game]); // eslint-disable-line react-hooks/exhaustive-deps
|
||||||
|
|
||||||
|
return gameData;
|
||||||
|
};
|
||||||
@@ -9,7 +9,7 @@ import {
|
|||||||
import { CurrentPosition, PositionEval } from "@/types/eval";
|
import { CurrentPosition, PositionEval } from "@/types/eval";
|
||||||
import { useAtom, useAtomValue } from "jotai";
|
import { useAtom, useAtomValue } from "jotai";
|
||||||
import { useEffect } from "react";
|
import { useEffect } from "react";
|
||||||
import { useEngine } from "./useEngine";
|
import { useEngine } from "../../../hooks/useEngine";
|
||||||
import { EngineName } from "@/types/enums";
|
import { EngineName } from "@/types/enums";
|
||||||
|
|
||||||
export const useCurrentPosition = (engineName?: EngineName) => {
|
export const useCurrentPosition = (engineName?: EngineName) => {
|
||||||
@@ -3,7 +3,7 @@ import { Grid, List, Typography } from "@mui/material";
|
|||||||
import { useAtomValue } from "jotai";
|
import { useAtomValue } from "jotai";
|
||||||
import { boardAtom, engineMultiPvAtom, gameAtom } from "../states";
|
import { boardAtom, engineMultiPvAtom, gameAtom } from "../states";
|
||||||
import LineEvaluation from "./lineEvaluation";
|
import LineEvaluation from "./lineEvaluation";
|
||||||
import { useCurrentPosition } from "@/hooks/useCurrentPosition";
|
import { useCurrentPosition } from "../hooks/useCurrentPosition";
|
||||||
import { LineEval } from "@/types/eval";
|
import { LineEval } from "@/types/eval";
|
||||||
import { EngineName } from "@/types/enums";
|
import { EngineName } from "@/types/enums";
|
||||||
import EngineSettingsButton from "@/sections/engineSettings/engineSettingsButton";
|
import EngineSettingsButton from "@/sections/engineSettings/engineSettingsButton";
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { useCurrentPosition } from "@/hooks/useCurrentPosition";
|
import { useCurrentPosition } from "../hooks/useCurrentPosition";
|
||||||
import { Grid, Typography } from "@mui/material";
|
import { Grid, Typography } from "@mui/material";
|
||||||
|
|
||||||
export default function Opening() {
|
export default function Opening() {
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import {
|
|||||||
playableSquaresAtom,
|
playableSquaresAtom,
|
||||||
playerColorAtom,
|
playerColorAtom,
|
||||||
isGameInProgressAtom,
|
isGameInProgressAtom,
|
||||||
|
gameDataAtom,
|
||||||
} from "../states";
|
} from "../states";
|
||||||
import { Square } from "react-chessboard/dist/chessboard/types";
|
import { Square } from "react-chessboard/dist/chessboard/types";
|
||||||
import { useChessActions } from "@/hooks/useChessActions";
|
import { useChessActions } from "@/hooks/useChessActions";
|
||||||
@@ -18,10 +19,12 @@ import { Color, EngineName } from "@/types/enums";
|
|||||||
import SquareRenderer from "./squareRenderer";
|
import SquareRenderer from "./squareRenderer";
|
||||||
import { useEngine } from "@/hooks/useEngine";
|
import { useEngine } from "@/hooks/useEngine";
|
||||||
import { uciMoveParams } from "@/lib/chess";
|
import { uciMoveParams } from "@/lib/chess";
|
||||||
|
import { useGameData } from "@/hooks/useGameData";
|
||||||
|
|
||||||
export default function Board() {
|
export default function Board() {
|
||||||
const boardRef = useRef<HTMLDivElement>(null);
|
const boardRef = useRef<HTMLDivElement>(null);
|
||||||
const { boardSize } = useScreenSize();
|
const { boardSize } = useScreenSize();
|
||||||
|
const engine = useEngine(EngineName.Stockfish16);
|
||||||
const game = useAtomValue(gameAtom);
|
const game = useAtomValue(gameAtom);
|
||||||
const playerColor = useAtomValue(playerColorAtom);
|
const playerColor = useAtomValue(playerColorAtom);
|
||||||
const { makeMove: makeGameMove } = useChessActions(gameAtom);
|
const { makeMove: makeGameMove } = useChessActions(gameAtom);
|
||||||
@@ -29,7 +32,7 @@ export default function Board() {
|
|||||||
const setPlayableSquares = useSetAtom(playableSquaresAtom);
|
const setPlayableSquares = useSetAtom(playableSquaresAtom);
|
||||||
const engineSkillLevel = useAtomValue(engineSkillLevelAtom);
|
const engineSkillLevel = useAtomValue(engineSkillLevelAtom);
|
||||||
const isGameInProgress = useAtomValue(isGameInProgressAtom);
|
const isGameInProgress = useAtomValue(isGameInProgressAtom);
|
||||||
const engine = useEngine(EngineName.Stockfish16);
|
useGameData(gameAtom, gameDataAtom);
|
||||||
|
|
||||||
const gameFen = game.fen();
|
const gameFen = game.fen();
|
||||||
const isGameFinished = game.isGameOver();
|
const isGameFinished = game.isGameOver();
|
||||||
@@ -129,7 +132,7 @@ export default function Board() {
|
|||||||
xs={12}
|
xs={12}
|
||||||
>
|
>
|
||||||
<Chessboard
|
<Chessboard
|
||||||
id="AnalysisBoard"
|
id="PlayBoard"
|
||||||
position={gameFen}
|
position={gameFen}
|
||||||
onPieceDrop={onPieceDrop}
|
onPieceDrop={onPieceDrop}
|
||||||
boardOrientation={playerColor === Color.White ? "white" : "black"}
|
boardOrientation={playerColor === Color.White ? "white" : "black"}
|
||||||
|
|||||||
@@ -1,4 +1,8 @@
|
|||||||
import { clickedSquaresAtom, gameAtom, playableSquaresAtom } from "../states";
|
import {
|
||||||
|
clickedSquaresAtom,
|
||||||
|
gameDataAtom,
|
||||||
|
playableSquaresAtom,
|
||||||
|
} from "../states";
|
||||||
import { useAtomValue } from "jotai";
|
import { useAtomValue } from "jotai";
|
||||||
import { CSSProperties, forwardRef } from "react";
|
import { CSSProperties, forwardRef } from "react";
|
||||||
import { CustomSquareProps } from "react-chessboard/dist/chessboard/types";
|
import { CustomSquareProps } from "react-chessboard/dist/chessboard/types";
|
||||||
@@ -6,13 +10,12 @@ import { CustomSquareProps } from "react-chessboard/dist/chessboard/types";
|
|||||||
const SquareRenderer = forwardRef<HTMLDivElement, CustomSquareProps>(
|
const SquareRenderer = forwardRef<HTMLDivElement, CustomSquareProps>(
|
||||||
(props, ref) => {
|
(props, ref) => {
|
||||||
const { children, square, style } = props;
|
const { children, square, style } = props;
|
||||||
const game = useAtomValue(gameAtom);
|
|
||||||
const clickedSquares = useAtomValue(clickedSquaresAtom);
|
const clickedSquares = useAtomValue(clickedSquaresAtom);
|
||||||
const playableSquares = useAtomValue(playableSquaresAtom);
|
const playableSquares = useAtomValue(playableSquaresAtom);
|
||||||
|
const gameData = useAtomValue(gameDataAtom);
|
||||||
|
|
||||||
const lastMove = game.history({ verbose: true }).at(-1);
|
const fromSquare = gameData.lastMove?.from;
|
||||||
const fromSquare = lastMove?.from;
|
const toSquare = gameData.lastMove?.to;
|
||||||
const toSquare = lastMove?.to;
|
|
||||||
|
|
||||||
const highlightSquareStyle: CSSProperties | undefined =
|
const highlightSquareStyle: CSSProperties | undefined =
|
||||||
clickedSquares.includes(square)
|
clickedSquares.includes(square)
|
||||||
|
|||||||
@@ -1,8 +1,13 @@
|
|||||||
|
import { GameData } from "@/hooks/useGameData";
|
||||||
import { Color } from "@/types/enums";
|
import { Color } from "@/types/enums";
|
||||||
import { Chess } from "chess.js";
|
import { Chess } from "chess.js";
|
||||||
import { atom } from "jotai";
|
import { atom } from "jotai";
|
||||||
|
|
||||||
export const gameAtom = atom(new Chess());
|
export const gameAtom = atom(new Chess());
|
||||||
|
export const gameDataAtom = atom<GameData>({
|
||||||
|
history: [],
|
||||||
|
lastMove: undefined,
|
||||||
|
});
|
||||||
export const playerColorAtom = atom<Color>(Color.White);
|
export const playerColorAtom = atom<Color>(Color.White);
|
||||||
export const engineSkillLevelAtom = atom<number>(1);
|
export const engineSkillLevelAtom = atom<number>(1);
|
||||||
export const isGameInProgressAtom = atom(false);
|
export const isGameInProgressAtom = atom(false);
|
||||||
|
|||||||
Reference in New Issue
Block a user