feat : add evaluation bar

This commit is contained in:
GuillaumeSD
2024-02-25 00:35:02 +01:00
parent 1f748f99ca
commit 892004f0d0
7 changed files with 158 additions and 26 deletions

View File

@@ -1,3 +1,4 @@
import { LineEval } from "@/types/eval";
import { Game } from "@/types/game";
import { Chess } from "chess.js";
@@ -77,3 +78,30 @@ export const moveLineUciToSan = (
}
};
};
export const getEvaluationBarValue = (
bestLine: LineEval,
whiteToPlay: boolean
): { whiteBarPercentage: number; label: string } => {
if (bestLine.mate) {
return {
whiteBarPercentage: whiteToPlay ? 100 : 0,
label: `M${bestLine.mate}`,
};
}
if (!bestLine.cp) {
return { whiteBarPercentage: 50, label: "0.0" };
}
const cp = bestLine.cp;
const whiteBarPercentage = Math.min(50 + cp / 20, 98);
const label = (cp / 100).toFixed(1);
if (label.toString().length > 3) {
return { whiteBarPercentage, label: (cp / 100).toFixed(0) };
}
return { whiteBarPercentage, label: (cp / 100).toFixed(1) };
};