feat : add move classification icons

This commit is contained in:
GuillaumeSD
2024-03-07 00:28:32 +01:00
parent c93983fa1f
commit 99a90def9c
23 changed files with 170 additions and 122 deletions

View File

@@ -4,15 +4,15 @@ import {
getStandardDeviation,
getWeightedMean,
} from "@/lib/helpers";
import { Accuracy, MoveEval } from "@/types/eval";
import { Accuracy, PositionEval } from "@/types/eval";
import { getPositionWinPercentage } from "./winPercentage";
export const computeAccuracy = (moves: MoveEval[]): Accuracy => {
const movesWinPercentage = moves.map(getPositionWinPercentage);
export const computeAccuracy = (positions: PositionEval[]): Accuracy => {
const positionsWinPercentage = positions.map(getPositionWinPercentage);
const weights = getAccuracyWeights(movesWinPercentage);
const weights = getAccuracyWeights(positionsWinPercentage);
const movesAccuracy = getMovesAccuracy(movesWinPercentage);
const movesAccuracy = getMovesAccuracy(positionsWinPercentage);
const whiteAccuracy = getPlayerAccuracy(movesAccuracy, weights, "white");
const blackAccuracy = getPlayerAccuracy(movesAccuracy, weights, "black");

View File

@@ -1,13 +1,13 @@
import { MoveEval } from "@/types/eval";
import { PositionEval } from "@/types/eval";
import { getPositionWinPercentage } from "./winPercentage";
import { MoveClassification } from "@/types/enums";
import { openings } from "@/data/openings";
export const getMovesClassification = (
rawMoves: MoveEval[],
rawMoves: PositionEval[],
uciMoves: string[],
fens: string[]
): MoveEval[] => {
): PositionEval[] => {
const positionsWinPercentage = rawMoves.map(getPositionWinPercentage);
let currentOpening: string | undefined = undefined;

View File

@@ -1,10 +1,10 @@
import { LineEval, MoveEval } from "@/types/eval";
import { LineEval, PositionEval } from "@/types/eval";
export const parseEvaluationResults = (
results: string[],
whiteToPlay: boolean
): MoveEval => {
const parsedResults: MoveEval = {
): PositionEval => {
const parsedResults: PositionEval = {
lines: [],
};
const tempResults: Record<string, LineEval> = {};

View File

@@ -1,13 +1,13 @@
import { ceilsNumber } from "@/lib/helpers";
import { MoveEval } from "@/types/eval";
import { PositionEval } from "@/types/eval";
export const getPositionWinPercentage = (move: MoveEval): number => {
if (move.lines[0].cp !== undefined) {
return getWinPercentageFromCp(move.lines[0].cp);
export const getPositionWinPercentage = (position: PositionEval): number => {
if (position.lines[0].cp !== undefined) {
return getWinPercentageFromCp(position.lines[0].cp);
}
if (move.lines[0].mate !== undefined) {
return getWinPercentageFromMate(move.lines[0].mate);
if (position.lines[0].mate !== undefined) {
return getWinPercentageFromMate(position.lines[0].mate);
}
throw new Error("No cp or mate in move");

View File

@@ -3,7 +3,7 @@ import {
EvaluateGameParams,
EvaluatePositionWithUpdateParams,
GameEval,
MoveEval,
PositionEval,
} from "@/types/eval";
import { parseEvaluationResults } from "./helpers/parseResults";
import { computeAccuracy } from "./helpers/accuracy";
@@ -108,11 +108,11 @@ export abstract class UciEngine {
await this.sendCommands(["ucinewgame", "isready"], "readyok");
this.worker.postMessage("position startpos");
const moves: MoveEval[] = [];
const positions: PositionEval[] = [];
for (const fen of fens) {
const whoIsCheckmated = getWhoIsCheckmated(fen);
if (whoIsCheckmated) {
moves.push({
positions.push({
lines: [
{
pv: [],
@@ -125,19 +125,19 @@ export abstract class UciEngine {
continue;
}
const result = await this.evaluatePosition(fen, depth);
moves.push(result);
positions.push(result);
}
const movesWithClassification = getMovesClassification(
moves,
const positionsWithClassification = getMovesClassification(
positions,
uciMoves,
fens
);
const accuracy = computeAccuracy(moves);
const accuracy = computeAccuracy(positions);
this.ready = true;
return {
moves: movesWithClassification,
positions: positionsWithClassification,
accuracy,
settings: {
engine: this.engineName,
@@ -148,7 +148,10 @@ export abstract class UciEngine {
};
}
private async evaluatePosition(fen: string, depth = 16): Promise<MoveEval> {
private async evaluatePosition(
fen: string,
depth = 16
): Promise<PositionEval> {
console.log(`Evaluating position: ${fen}`);
const lichessEval = await getLichessEval(fen, this.multiPv);