From 4df07783905965eaa44f8792f032b7e760b85079 Mon Sep 17 00:00:00 2001 From: GuillaumeSD Date: Sat, 17 Feb 2024 22:45:52 +0100 Subject: [PATCH] fix : accuracy --- src/lib/engine/stockfish.ts | 50 +++++++++-------------------- src/sections/index/reviewResult.tsx | 6 ++-- 2 files changed, 19 insertions(+), 37 deletions(-) diff --git a/src/lib/engine/stockfish.ts b/src/lib/engine/stockfish.ts index 07ec622..3562b44 100644 --- a/src/lib/engine/stockfish.ts +++ b/src/lib/engine/stockfish.ts @@ -70,27 +70,22 @@ export class Stockfish { await this.sendCommands(["ucinewgame", "isready"], "readyok"); this.worker.postMessage("position startpos"); - let whiteCpVsBestMove = 0; - let blackCpVsBestMove = 0; + let whiteCpSum = 0; + let whiteCpMax = 0; + let blackCpSum = 0; + let blackCpMax = 0; const moves: MoveEval[] = []; for (const fen of fens) { console.log(`Evaluating position: ${fen}`); const result = await this.evaluatePosition(fen, depth); - const bestLine = result.lines[0]; - const beforeMoveBestLine: LineEval = moves.at(-1)?.lines[0] ?? { - pv: [], - cp: 0, - }; - const wasWhiteMove = fen.split(" ")[1] === "b"; - const cpVsBestMove = this.calculateCpVsBestMove( - bestLine, - beforeMoveBestLine - ); - if (wasWhiteMove) { - whiteCpVsBestMove += cpVsBestMove; + const bestLineEval = result.lines[0].cp ?? 0; + if (this.isWhiteToMove(fen)) { + whiteCpMax += bestLineEval; + blackCpSum += bestLineEval; } else { - blackCpVsBestMove += cpVsBestMove; + blackCpMax += bestLineEval; + whiteCpSum += bestLineEval; } moves.push(result); @@ -99,30 +94,17 @@ export class Stockfish { this.ready = true; console.log("Game evaluated"); console.log(moves); - const whiteAccuracy = this.calculateAccuracy( - whiteCpVsBestMove, - moves.length - ); - const blackAccuracy = this.calculateAccuracy( - blackCpVsBestMove, - moves.length - ); + const whiteAccuracy = this.calculateAccuracy(whiteCpSum, whiteCpMax); + const blackAccuracy = this.calculateAccuracy(blackCpSum, blackCpMax); return { moves, whiteAccuracy, blackAccuracy }; } - private calculateAccuracy(cpVsBestMove: number, movesNb: number): number { - return 100 - (cpVsBestMove / movesNb) * 100; + private calculateAccuracy(sum: number, max: number): number { + return (sum / max) * 100; } - private calculateCpVsBestMove( - bestLine: LineEval, - beforeMoveBestLine: LineEval - ): number { - if (bestLine.cp === undefined || beforeMoveBestLine.cp === undefined) { - return 0; - } - - return bestLine.cp - beforeMoveBestLine.cp; + private isWhiteToMove(fen: string): boolean { + return fen.split(" ")[1] === "w"; } public async evaluatePosition(fen: string, depth = 16): Promise { diff --git a/src/sections/index/reviewResult.tsx b/src/sections/index/reviewResult.tsx index 85beb69..e1933be 100644 --- a/src/sections/index/reviewResult.tsx +++ b/src/sections/index/reviewResult.tsx @@ -16,8 +16,8 @@ export default function ReviewResult() { Accuracies
- {gameEval.whiteAccuracy}% - {gameEval.blackAccuracy}% + {gameEval.whiteAccuracy.toFixed(1)}% + {gameEval.blackAccuracy.toFixed(1)}%
@@ -36,7 +36,7 @@ export default function ReviewResult() { {moveEval?.lines.map((line) => (
- {line.cp ? line.cp / 100 : `Mate in ${line.mate}`} + {line.cp ? line.cp / 100 : `Mate in ${Math.abs(line.mate ?? 0)}`} {line.pv.slice(0, 7).join(", ")}