feat : add move classification
This commit is contained in:
@@ -60,5 +60,26 @@ export const useChessActions = (chessAtom: PrimitiveAtom<Chess>) => {
|
||||
setGame(newGame);
|
||||
}, [copyGame, setGame]);
|
||||
|
||||
return { setPgn, reset, makeMove, undoMove };
|
||||
const goToMove = useCallback(
|
||||
(moveIdx: number, game: Chess) => {
|
||||
if (moveIdx < 0) return;
|
||||
|
||||
const newGame = new Chess();
|
||||
newGame.loadPgn(game.pgn());
|
||||
|
||||
const movesNb = game.history().length;
|
||||
if (moveIdx > movesNb) return;
|
||||
|
||||
let lastMove: Move | null = null;
|
||||
for (let i = movesNb; i > moveIdx; i--) {
|
||||
lastMove = newGame.undo();
|
||||
}
|
||||
|
||||
setGame(newGame);
|
||||
playSoundFromMove(lastMove);
|
||||
},
|
||||
[setGame]
|
||||
);
|
||||
|
||||
return { setPgn, reset, makeMove, undoMove, goToMove };
|
||||
};
|
||||
|
||||
18
src/hooks/usePlayerNames.ts
Normal file
18
src/hooks/usePlayerNames.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { Chess } from "chess.js";
|
||||
import { PrimitiveAtom, useAtomValue } from "jotai";
|
||||
import { useGameDatabase } from "./useGameDatabase";
|
||||
|
||||
export const usePlayersNames = (gameAtom: PrimitiveAtom<Chess>) => {
|
||||
const game = useAtomValue(gameAtom);
|
||||
const { gameFromUrl } = useGameDatabase();
|
||||
|
||||
const whiteName =
|
||||
gameFromUrl?.white?.name || game.header()["White"] || "White";
|
||||
const blackName =
|
||||
gameFromUrl?.black?.name || game.header()["Black"] || "Black";
|
||||
|
||||
return {
|
||||
whiteName,
|
||||
blackName,
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user