feat : eval bar value on win percentage
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import { EvaluateGameParams, LineEval } from "@/types/eval";
|
import { EvaluateGameParams, PositionEval } from "@/types/eval";
|
||||||
import { Game } from "@/types/game";
|
import { Game } from "@/types/game";
|
||||||
import { Chess } from "chess.js";
|
import { Chess } from "chess.js";
|
||||||
|
import { getPositionWinPercentage } from "./engine/helpers/winPercentage";
|
||||||
|
|
||||||
export const getEvaluateGameParams = (game: Chess): EvaluateGameParams => {
|
export const getEvaluateGameParams = (game: Chess): EvaluateGameParams => {
|
||||||
const history = game.history({ verbose: true });
|
const history = game.history({ verbose: true });
|
||||||
@@ -89,31 +90,26 @@ export const moveLineUciToSan = (
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const getEvaluationBarValue = (
|
export const getEvaluationBarValue = (
|
||||||
bestLine: LineEval,
|
position: PositionEval
|
||||||
whiteToPlay: boolean
|
|
||||||
): { whiteBarPercentage: number; label: string } => {
|
): { whiteBarPercentage: number; label: string } => {
|
||||||
if (bestLine.mate) {
|
const whiteBarPercentage = getPositionWinPercentage(position);
|
||||||
return {
|
const bestLine = position.lines[0];
|
||||||
whiteBarPercentage: whiteToPlay && bestLine.mate > 0 ? 100 : 0,
|
|
||||||
label: `M${Math.abs(bestLine.mate)}`,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!bestLine.cp) {
|
if (bestLine.mate) {
|
||||||
return { whiteBarPercentage: 50, label: "0.0" };
|
return { label: `M${Math.abs(bestLine.mate)}`, whiteBarPercentage };
|
||||||
}
|
}
|
||||||
|
|
||||||
const cp = bestLine.cp;
|
const cp = bestLine.cp;
|
||||||
const whiteBarPercentage = Math.min(50 + cp / 20, 98);
|
if (!cp) return { whiteBarPercentage, label: "0.0" };
|
||||||
|
|
||||||
const pEval = Math.abs(cp) / 100;
|
const pEval = Math.abs(cp) / 100;
|
||||||
const label = pEval.toFixed(1);
|
let label = pEval.toFixed(1);
|
||||||
|
|
||||||
if (label.toString().length > 3) {
|
if (label.toString().length > 3) {
|
||||||
return { whiteBarPercentage, label: pEval.toFixed(0) };
|
label = pEval.toFixed(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
return { whiteBarPercentage, label: pEval.toFixed(1) };
|
return { whiteBarPercentage, label };
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getWhoIsCheckmated = (fen: string): "w" | "b" | null => {
|
export const getWhoIsCheckmated = (fen: string): "w" | "b" | null => {
|
||||||
|
|||||||
@@ -4,32 +4,32 @@ import { MoveClassification } from "@/types/enums";
|
|||||||
import { openings } from "@/data/openings";
|
import { openings } from "@/data/openings";
|
||||||
|
|
||||||
export const getMovesClassification = (
|
export const getMovesClassification = (
|
||||||
rawMoves: PositionEval[],
|
rawPositions: PositionEval[],
|
||||||
uciMoves: string[],
|
uciMoves: string[],
|
||||||
fens: string[]
|
fens: string[]
|
||||||
): PositionEval[] => {
|
): PositionEval[] => {
|
||||||
const positionsWinPercentage = rawMoves.map(getPositionWinPercentage);
|
const positionsWinPercentage = rawPositions.map(getPositionWinPercentage);
|
||||||
let currentOpening: string | undefined = undefined;
|
let currentOpening: string | undefined = undefined;
|
||||||
|
|
||||||
const moves = rawMoves.map((rawMove, index) => {
|
const positions = rawPositions.map((rawPosition, index) => {
|
||||||
if (index === 0) return rawMove;
|
if (index === 0) return rawPosition;
|
||||||
|
|
||||||
const currentFen = fens[index].split(" ")[0];
|
const currentFen = fens[index].split(" ")[0];
|
||||||
const opening = openings.find((opening) => opening.fen === currentFen);
|
const opening = openings.find((opening) => opening.fen === currentFen);
|
||||||
if (opening) {
|
if (opening) {
|
||||||
currentOpening = opening.name;
|
currentOpening = opening.name;
|
||||||
return {
|
return {
|
||||||
...rawMove,
|
...rawPosition,
|
||||||
opening: opening.name,
|
opening: opening.name,
|
||||||
moveClassification: MoveClassification.Book,
|
moveClassification: MoveClassification.Book,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const uciMove = uciMoves[index - 1];
|
const uciMove = uciMoves[index - 1];
|
||||||
const bestMove = rawMoves[index - 1].bestMove;
|
const bestMove = rawPositions[index - 1].bestMove;
|
||||||
if (uciMove === bestMove) {
|
if (uciMove === bestMove) {
|
||||||
return {
|
return {
|
||||||
...rawMove,
|
...rawPosition,
|
||||||
opening: currentOpening,
|
opening: currentOpening,
|
||||||
moveClassification: MoveClassification.Best,
|
moveClassification: MoveClassification.Best,
|
||||||
};
|
};
|
||||||
@@ -46,13 +46,13 @@ export const getMovesClassification = (
|
|||||||
);
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...rawMove,
|
...rawPosition,
|
||||||
opening: currentOpening,
|
opening: currentOpening,
|
||||||
moveClassification,
|
moveClassification,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
return moves;
|
return positions;
|
||||||
};
|
};
|
||||||
|
|
||||||
const getMoveClassification = (
|
const getMoveClassification = (
|
||||||
|
|||||||
@@ -25,9 +25,9 @@ export default function EvaluationBar({ height }: Props) {
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const bestLine = position?.eval?.lines[0];
|
const bestLine = position?.eval?.lines[0];
|
||||||
if (!bestLine || bestLine.depth < 6) return;
|
if (!position.eval || !bestLine || bestLine.depth < 6) return;
|
||||||
|
|
||||||
const evalBar = getEvaluationBarValue(bestLine, isWhiteToPlay);
|
const evalBar = getEvaluationBarValue(position.eval);
|
||||||
setEvalBar(evalBar);
|
setEvalBar(evalBar);
|
||||||
}, [position, isWhiteToPlay]);
|
}, [position, isWhiteToPlay]);
|
||||||
|
|
||||||
|
|||||||
@@ -40,8 +40,10 @@ const SquareRenderer = forwardRef<HTMLDivElement, CustomSquareProps>(
|
|||||||
height={40}
|
height={40}
|
||||||
style={{
|
style={{
|
||||||
position: "absolute",
|
position: "absolute",
|
||||||
top: -15,
|
top: "max(-15px, -1.8vw)",
|
||||||
right: -15,
|
right: "max(-15px, -1.8vw)",
|
||||||
|
maxWidth: "3.6vw",
|
||||||
|
maxHeight: "3.6vw",
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|||||||
Reference in New Issue
Block a user