feat : set depth & multipv
This commit is contained in:
@@ -1,32 +1,44 @@
|
||||
import { boardAtom, gameAtom, gameEvalAtom } from "@/sections/analysis/states";
|
||||
import { MoveEval } from "@/types/eval";
|
||||
import { Move } from "chess.js";
|
||||
import { useAtomValue } from "jotai";
|
||||
import { useMemo } from "react";
|
||||
|
||||
export type CurrentMove = Partial<Move> & {
|
||||
eval?: MoveEval;
|
||||
lastEval?: MoveEval;
|
||||
};
|
||||
|
||||
export const useCurrentMove = () => {
|
||||
const gameEval = useAtomValue(gameEvalAtom);
|
||||
const game = useAtomValue(gameAtom);
|
||||
const board = useAtomValue(boardAtom);
|
||||
|
||||
const currentEvalMove = useMemo(() => {
|
||||
if (!gameEval) return undefined;
|
||||
const currentMove: CurrentMove = useMemo(() => {
|
||||
const move = {
|
||||
...board.history({ verbose: true }).at(-1),
|
||||
};
|
||||
|
||||
if (!gameEval) return move;
|
||||
|
||||
const boardHistory = board.history();
|
||||
const gameHistory = game.history();
|
||||
|
||||
if (
|
||||
boardHistory.length >= gameHistory.length ||
|
||||
gameHistory.slice(0, boardHistory.length).join() !== boardHistory.join()
|
||||
)
|
||||
return;
|
||||
boardHistory.length <= gameHistory.length &&
|
||||
gameHistory.slice(0, boardHistory.length).join() === boardHistory.join()
|
||||
) {
|
||||
const evalIndex = board.history().length;
|
||||
|
||||
const evalIndex = board.history().length;
|
||||
return {
|
||||
...move,
|
||||
eval: gameEval.moves[evalIndex],
|
||||
lastEval: evalIndex > 0 ? gameEval.moves[evalIndex - 1] : undefined,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
...board.history({ verbose: true }).at(-1),
|
||||
eval: gameEval.moves[evalIndex],
|
||||
lastEval: evalIndex > 0 ? gameEval.moves[evalIndex - 1] : undefined,
|
||||
};
|
||||
return move;
|
||||
}, [gameEval, board, game]);
|
||||
|
||||
return currentEvalMove;
|
||||
return currentMove;
|
||||
};
|
||||
|
||||
@@ -53,6 +53,54 @@ export const useGameDatabase = (shouldFetchGames?: boolean) => {
|
||||
loadGames();
|
||||
}, [loadGames]);
|
||||
|
||||
const addGame = useCallback(
|
||||
async (game: Chess) => {
|
||||
if (!db) throw new Error("Database not initialized");
|
||||
|
||||
const gameToAdd = formatGameToDatabase(game);
|
||||
const gameId = await db.add("games", gameToAdd as Game);
|
||||
|
||||
loadGames();
|
||||
|
||||
return gameId;
|
||||
},
|
||||
[db, loadGames]
|
||||
);
|
||||
|
||||
const setGameEval = useCallback(
|
||||
async (gameId: number, evaluation: GameEval) => {
|
||||
if (!db) throw new Error("Database not initialized");
|
||||
|
||||
const game = await db.get("games", gameId);
|
||||
if (!game) throw new Error("Game not found");
|
||||
|
||||
await db.put("games", { ...game, eval: evaluation });
|
||||
|
||||
loadGames();
|
||||
},
|
||||
[db, loadGames]
|
||||
);
|
||||
|
||||
const getGame = useCallback(
|
||||
async (gameId: number) => {
|
||||
if (!db) return undefined;
|
||||
|
||||
return db.get("games", gameId);
|
||||
},
|
||||
[db]
|
||||
);
|
||||
|
||||
const deleteGame = useCallback(
|
||||
async (gameId: number) => {
|
||||
if (!db) throw new Error("Database not initialized");
|
||||
|
||||
await db.delete("games", gameId);
|
||||
|
||||
loadGames();
|
||||
},
|
||||
[db, loadGames]
|
||||
);
|
||||
|
||||
const router = useRouter();
|
||||
const { gameId } = router.query;
|
||||
|
||||
@@ -62,43 +110,7 @@ export const useGameDatabase = (shouldFetchGames?: boolean) => {
|
||||
setGameFromUrl(game);
|
||||
});
|
||||
}
|
||||
}, [gameId, games]);
|
||||
|
||||
const addGame = async (game: Chess) => {
|
||||
if (!db) throw new Error("Database not initialized");
|
||||
|
||||
const gameToAdd = formatGameToDatabase(game);
|
||||
const gameId = await db.add("games", gameToAdd as Game);
|
||||
|
||||
loadGames();
|
||||
|
||||
return gameId;
|
||||
};
|
||||
|
||||
const setGameEval = async (gameId: number, evaluation: GameEval) => {
|
||||
if (!db) throw new Error("Database not initialized");
|
||||
|
||||
const game = await db.get("games", gameId);
|
||||
if (!game) throw new Error("Game not found");
|
||||
|
||||
await db.put("games", { ...game, eval: evaluation });
|
||||
|
||||
loadGames();
|
||||
};
|
||||
|
||||
const getGame = async (gameId: number) => {
|
||||
if (!db) return undefined;
|
||||
|
||||
return db.get("games", gameId);
|
||||
};
|
||||
|
||||
const deleteGame = async (gameId: number) => {
|
||||
if (!db) throw new Error("Database not initialized");
|
||||
|
||||
await db.delete("games", gameId);
|
||||
|
||||
loadGames();
|
||||
};
|
||||
}, [gameId, setGameFromUrl, getGame]);
|
||||
|
||||
const isReady = db !== null;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user