feat : add move classification icons
This commit is contained in:
@@ -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");
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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> = {};
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { LineEval, MoveEval } from "@/types/eval";
|
||||
import { LineEval, PositionEval } from "@/types/eval";
|
||||
import { sortLines } from "./engine/helpers/parseResults";
|
||||
import {
|
||||
LichessError,
|
||||
@@ -9,7 +9,7 @@ import {
|
||||
export const getLichessEval = async (
|
||||
fen: string,
|
||||
multiPv = 1
|
||||
): Promise<MoveEval> => {
|
||||
): Promise<PositionEval> => {
|
||||
try {
|
||||
const res = await fetch(
|
||||
`https://lichess.org/api/cloud-eval?fen=${fen}&multiPv=${multiPv}`
|
||||
|
||||
Reference in New Issue
Block a user