fix : show current opening
This commit is contained in:
@@ -13,7 +13,7 @@ export const getMovesClassification = (
|
|||||||
fens: string[]
|
fens: string[]
|
||||||
): PositionEval[] => {
|
): PositionEval[] => {
|
||||||
const positionsWinPercentage = rawPositions.map(getPositionWinPercentage);
|
const positionsWinPercentage = rawPositions.map(getPositionWinPercentage);
|
||||||
let currentOpening: string = "Unknown opening";
|
let currentOpening: string | undefined = undefined;
|
||||||
|
|
||||||
const positions = rawPositions.map((rawPosition, index) => {
|
const positions = rawPositions.map((rawPosition, index) => {
|
||||||
if (index === 0) return rawPosition;
|
if (index === 0) return rawPosition;
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import { useEngine } from "../../../hooks/useEngine";
|
|||||||
import { EngineName } from "@/types/enums";
|
import { EngineName } from "@/types/enums";
|
||||||
import { getEvaluateGameParams } from "@/lib/chess";
|
import { getEvaluateGameParams } from "@/lib/chess";
|
||||||
import { getMovesClassification } from "@/lib/engine/helpers/moveClassification";
|
import { getMovesClassification } from "@/lib/engine/helpers/moveClassification";
|
||||||
|
import { openings } from "@/data/openings";
|
||||||
|
|
||||||
export const useCurrentPosition = (engineName?: EngineName) => {
|
export const useCurrentPosition = (engineName?: EngineName) => {
|
||||||
const [currentPosition, setCurrentPosition] = useAtom(currentPositionAtom);
|
const [currentPosition, setCurrentPosition] = useAtom(currentPositionAtom);
|
||||||
@@ -26,16 +27,17 @@ export const useCurrentPosition = (engineName?: EngineName) => {
|
|||||||
const [savedEvals, setSavedEvals] = useAtom(savedEvalsAtom);
|
const [savedEvals, setSavedEvals] = useAtom(savedEvalsAtom);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
const boardHistory = board.history({ verbose: true });
|
||||||
const position: CurrentPosition = {
|
const position: CurrentPosition = {
|
||||||
lastMove: board.history({ verbose: true }).at(-1),
|
lastMove: boardHistory.at(-1),
|
||||||
};
|
};
|
||||||
|
|
||||||
const boardHistory = board.history();
|
|
||||||
const gameHistory = game.history();
|
const gameHistory = game.history();
|
||||||
|
|
||||||
if (
|
if (
|
||||||
boardHistory.length <= gameHistory.length &&
|
boardHistory.length <= gameHistory.length &&
|
||||||
gameHistory.slice(0, boardHistory.length).join() === boardHistory.join()
|
gameHistory.slice(0, boardHistory.length).join() ===
|
||||||
|
boardHistory.map((m) => m.san).join()
|
||||||
) {
|
) {
|
||||||
position.currentMoveIdx = boardHistory.length;
|
position.currentMoveIdx = boardHistory.length;
|
||||||
|
|
||||||
@@ -59,6 +61,17 @@ export const useCurrentPosition = (engineName?: EngineName) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!position.eval?.opening) {
|
||||||
|
for (const move of boardHistory.toReversed()) {
|
||||||
|
const moveFen = move.after.split(" ")[0];
|
||||||
|
const opening = openings.find((opening) => opening.fen === moveFen);
|
||||||
|
if (opening) {
|
||||||
|
position.opening = opening.name;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
setCurrentPosition(position);
|
setCurrentPosition(position);
|
||||||
|
|
||||||
if (
|
if (
|
||||||
|
|||||||
@@ -1,24 +1,14 @@
|
|||||||
import { useState } from "react";
|
import { useAtomValue } from "jotai";
|
||||||
import { useCurrentPosition } from "../../hooks/useCurrentPosition";
|
|
||||||
import { Grid2 as Grid, Skeleton, Typography } from "@mui/material";
|
import { Grid2 as Grid, Skeleton, Typography } from "@mui/material";
|
||||||
|
import { currentPositionAtom } from "../../states";
|
||||||
|
|
||||||
export default function Opening() {
|
export default function Opening() {
|
||||||
const position = useCurrentPosition();
|
const position = useAtomValue(currentPositionAtom);
|
||||||
const [lastOpening, setLastOpening] = useState("");
|
|
||||||
|
|
||||||
const lastMove = position?.lastMove;
|
const lastMove = position?.lastMove;
|
||||||
if (!lastMove && lastOpening) {
|
|
||||||
setLastOpening("");
|
|
||||||
}
|
|
||||||
if (!lastMove) return null;
|
if (!lastMove) return null;
|
||||||
|
|
||||||
const opening =
|
const opening = position?.eval?.opening || position.opening;
|
||||||
position?.eval?.opening && !position?.eval?.opening.includes("Unknown")
|
|
||||||
? position.eval.opening
|
|
||||||
: lastOpening;
|
|
||||||
if (opening && opening !== lastOpening) {
|
|
||||||
setLastOpening(opening);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!opening) {
|
if (!opening) {
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -52,6 +52,7 @@ export interface CurrentPosition {
|
|||||||
eval?: PositionEval;
|
eval?: PositionEval;
|
||||||
lastEval?: PositionEval;
|
lastEval?: PositionEval;
|
||||||
currentMoveIdx?: number;
|
currentMoveIdx?: number;
|
||||||
|
opening?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface EvaluateGameParams {
|
export interface EvaluateGameParams {
|
||||||
|
|||||||
Reference in New Issue
Block a user