fix : analyze game stuck on 1% progress

This commit is contained in:
GuillaumeSD
2025-05-16 13:24:17 +02:00
parent c35c1bf0a3
commit 653f3f1de4
5 changed files with 20 additions and 18 deletions

View File

@@ -10,7 +10,7 @@ import {
Square, Square,
} from "react-chessboard/dist/chessboard/types"; } from "react-chessboard/dist/chessboard/types";
import { useChessActions } from "@/hooks/useChessActions"; import { useChessActions } from "@/hooks/useChessActions";
import { useCallback, useMemo, useRef, useState } from "react"; import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { Color, MoveClassification } from "@/types/enums"; import { Color, MoveClassification } from "@/types/enums";
import { Chess } from "chess.js"; import { Chess } from "chess.js";
import { getSquareRenderer } from "./squareRenderer"; import { getSquareRenderer } from "./squareRenderer";
@@ -64,11 +64,10 @@ export default function Board({
const boardHue = useAtomValue(boardHueAtom); const boardHue = useAtomValue(boardHueAtom);
const gameFen = game.fen(); const gameFen = game.fen();
const [previousFen, setPreviousFen] = useState(gameFen);
if (gameFen !== previousFen) { useEffect(() => {
setPreviousFen(gameFen);
setClickedSquares([]); setClickedSquares([]);
} }, [gameFen, setClickedSquares]);
const isPiecePlayable = useCallback( const isPiecePlayable = useCallback(
({ piece }: { piece: string }): boolean => { ({ piece }: { piece: string }): boolean => {

View File

@@ -84,8 +84,6 @@ export class UciEngine {
private async setMultiPv(multiPv: number) { private async setMultiPv(multiPv: number) {
if (multiPv === this.multiPv) return; if (multiPv === this.multiPv) return;
this.throwErrorIfNotReady();
if (multiPv < 2 || multiPv > 6) { if (multiPv < 2 || multiPv > 6) {
throw new Error(`Invalid MultiPV value : ${multiPv}`); throw new Error(`Invalid MultiPV value : ${multiPv}`);
} }
@@ -101,8 +99,6 @@ export class UciEngine {
private async setElo(elo: number) { private async setElo(elo: number) {
if (elo === this.elo) return; if (elo === this.elo) return;
this.throwErrorIfNotReady();
if (elo < 1320 || elo > 3190) { if (elo < 1320 || elo > 3190) {
throw new Error(`Invalid Elo value : ${elo}`); throw new Error(`Invalid Elo value : ${elo}`);
} }
@@ -142,12 +138,12 @@ export class UciEngine {
private terminateWorker(worker: EngineWorker) { private terminateWorker(worker: EngineWorker) {
console.log(`Terminating worker from ${this.enginePath}`); console.log(`Terminating worker from ${this.enginePath}`);
worker.uci("quit");
worker.terminate?.();
worker.isReady = false; worker.isReady = false;
worker.uci("quit");
worker.terminate();
} }
public async stopSearch(): Promise<void> { public async stopAllCurrentJobs(): Promise<void> {
this.workerQueue = []; this.workerQueue = [];
await this.sendCommandsToEachWorker(["stop", "isready"], "readyok"); await this.sendCommandsToEachWorker(["stop", "isready"], "readyok");
@@ -255,10 +251,10 @@ export class UciEngine {
workersNb = 1, workersNb = 1,
}: EvaluateGameParams): Promise<GameEval> { }: EvaluateGameParams): Promise<GameEval> {
this.throwErrorIfNotReady(); this.throwErrorIfNotReady();
setEvaluationProgress?.(1);
await this.setMultiPv(multiPv);
this.isReady = false; this.isReady = false;
setEvaluationProgress?.(1);
await this.setMultiPv(multiPv);
await this.sendCommandsToEachWorker(["ucinewgame", "isready"], "readyok"); await this.sendCommandsToEachWorker(["ucinewgame", "isready"], "readyok");
this.setWorkersNb(workersNb); this.setWorkersNb(workersNb);
@@ -308,7 +304,9 @@ export class UciEngine {
updateEval(i, result); updateEval(i, result);
}) })
); );
await this.setWorkersNb(1); await this.setWorkersNb(1);
this.isReady = true;
const positionsWithClassification = getMovesClassification( const positionsWithClassification = getMovesClassification(
positions, positions,
@@ -322,7 +320,6 @@ export class UciEngine {
playersRatings?.black playersRatings?.black
); );
this.isReady = true;
return { return {
positions: positionsWithClassification, positions: positionsWithClassification,
estimatedElo, estimatedElo,
@@ -369,7 +366,7 @@ export class UciEngine {
const lichessEvalPromise = getLichessEval(fen, multiPv); const lichessEvalPromise = getLichessEval(fen, multiPv);
await this.stopSearch(); await this.stopAllCurrentJobs();
await this.setMultiPv(multiPv); await this.setMultiPv(multiPv);
const onNewMessage = (messages: string[]) => { const onNewMessage = (messages: string[]) => {
@@ -404,6 +401,8 @@ export class UciEngine {
depth = 16 depth = 16
): Promise<string | undefined> { ): Promise<string | undefined> {
this.throwErrorIfNotReady(); this.throwErrorIfNotReady();
await this.stopAllCurrentJobs();
await this.setElo(elo); await this.setElo(elo);
console.log(`Evaluating position: ${fen}`); console.log(`Evaluating position: ${fen}`);

View File

@@ -165,7 +165,7 @@ export const useCurrentPosition = (engine: UciEngine | null) => {
return () => { return () => {
if (engine?.getIsReady()) { if (engine?.getIsReady()) {
engine?.stopSearch(); engine?.stopAllCurrentJobs();
} }
}; };
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps

View File

@@ -101,6 +101,10 @@ export default function AnalyzeButton() {
black.rating, black.rating,
]); ]);
useEffect(() => {
setEvaluationProgress(0);
}, [engine, setEvaluationProgress]);
// Automatically analyze when a new game is loaded and ready to analyze // Automatically analyze when a new game is loaded and ready to analyze
useEffect(() => { useEffect(() => {
if (!gameEval && readyToAnalyse) { if (!gameEval && readyToAnalyse) {

View File

@@ -46,7 +46,7 @@ export default function BoardContainer() {
playEngineMove(); playEngineMove();
return () => { return () => {
engine?.stopSearch(); engine?.stopAllCurrentJobs();
}; };
}, [gameFen, isGameInProgress]); // eslint-disable-line react-hooks/exhaustive-deps }, [gameFen, isGameInProgress]); // eslint-disable-line react-hooks/exhaustive-deps