fix : play board optimization
This commit is contained in:
@@ -1,62 +0,0 @@
|
||||
import {
|
||||
boardAtom,
|
||||
currentPositionAtom,
|
||||
engineDepthAtom,
|
||||
engineMultiPvAtom,
|
||||
gameAtom,
|
||||
gameEvalAtom,
|
||||
} from "@/sections/analysis/states";
|
||||
import { CurrentPosition, PositionEval } from "@/types/eval";
|
||||
import { useAtom, useAtomValue } from "jotai";
|
||||
import { useEffect } from "react";
|
||||
import { useEngine } from "./useEngine";
|
||||
import { EngineName } from "@/types/enums";
|
||||
|
||||
export const useCurrentPosition = (engineName?: EngineName) => {
|
||||
const [currentPosition, setCurrentPosition] = useAtom(currentPositionAtom);
|
||||
const engine = useEngine(engineName);
|
||||
const gameEval = useAtomValue(gameEvalAtom);
|
||||
const game = useAtomValue(gameAtom);
|
||||
const board = useAtomValue(boardAtom);
|
||||
const depth = useAtomValue(engineDepthAtom);
|
||||
const multiPv = useAtomValue(engineMultiPvAtom);
|
||||
|
||||
useEffect(() => {
|
||||
const position: CurrentPosition = {
|
||||
lastMove: board.history({ verbose: true }).at(-1),
|
||||
};
|
||||
|
||||
if (gameEval) {
|
||||
const boardHistory = board.history();
|
||||
const gameHistory = game.history();
|
||||
|
||||
if (
|
||||
boardHistory.length <= gameHistory.length &&
|
||||
gameHistory.slice(0, boardHistory.length).join() === boardHistory.join()
|
||||
) {
|
||||
const evalIndex = board.history().length;
|
||||
|
||||
position.eval = gameEval.positions[evalIndex];
|
||||
position.lastEval =
|
||||
evalIndex > 0 ? gameEval.positions[evalIndex - 1] : undefined;
|
||||
}
|
||||
}
|
||||
|
||||
if (!position.eval && engine?.isReady()) {
|
||||
const setPartialEval = (positionEval: PositionEval) => {
|
||||
setCurrentPosition({ ...position, eval: positionEval });
|
||||
};
|
||||
|
||||
engine.evaluatePositionWithUpdate({
|
||||
fen: board.fen(),
|
||||
depth,
|
||||
multiPv,
|
||||
setPartialEval,
|
||||
});
|
||||
}
|
||||
|
||||
setCurrentPosition(position);
|
||||
}, [gameEval, board, game, engine, depth, multiPv, setCurrentPosition]);
|
||||
|
||||
return currentPosition;
|
||||
};
|
||||
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;
|
||||
};
|
||||
Reference in New Issue
Block a user