Skip to content

Commit 653f3f1

Browse files
committed
fix : analyze game stuck on 1% progress
1 parent c35c1bf commit 653f3f1

5 files changed

Lines changed: 20 additions & 18 deletions

File tree

src/components/board/index.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
Square,
1111
} from "react-chessboard/dist/chessboard/types";
1212
import { useChessActions } from "@/hooks/useChessActions";
13-
import { useCallback, useMemo, useRef, useState } from "react";
13+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
1414
import { Color, MoveClassification } from "@/types/enums";
1515
import { Chess } from "chess.js";
1616
import { getSquareRenderer } from "./squareRenderer";
@@ -64,11 +64,10 @@ export default function Board({
6464
const boardHue = useAtomValue(boardHueAtom);
6565

6666
const gameFen = game.fen();
67-
const [previousFen, setPreviousFen] = useState(gameFen);
68-
if (gameFen !== previousFen) {
69-
setPreviousFen(gameFen);
67+
68+
useEffect(() => {
7069
setClickedSquares([]);
71-
}
70+
}, [gameFen, setClickedSquares]);
7271

7372
const isPiecePlayable = useCallback(
7473
({ piece }: { piece: string }): boolean => {

src/lib/engine/uciEngine.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ export class UciEngine {
8484
private async setMultiPv(multiPv: number) {
8585
if (multiPv === this.multiPv) return;
8686

87-
this.throwErrorIfNotReady();
88-
8987
if (multiPv < 2 || multiPv > 6) {
9088
throw new Error(`Invalid MultiPV value : ${multiPv}`);
9189
}
@@ -101,8 +99,6 @@ export class UciEngine {
10199
private async setElo(elo: number) {
102100
if (elo === this.elo) return;
103101

104-
this.throwErrorIfNotReady();
105-
106102
if (elo < 1320 || elo > 3190) {
107103
throw new Error(`Invalid Elo value : ${elo}`);
108104
}
@@ -142,12 +138,12 @@ export class UciEngine {
142138

143139
private terminateWorker(worker: EngineWorker) {
144140
console.log(`Terminating worker from ${this.enginePath}`);
145-
worker.uci("quit");
146-
worker.terminate?.();
147141
worker.isReady = false;
142+
worker.uci("quit");
143+
worker.terminate();
148144
}
149145

150-
public async stopSearch(): Promise<void> {
146+
public async stopAllCurrentJobs(): Promise<void> {
151147
this.workerQueue = [];
152148
await this.sendCommandsToEachWorker(["stop", "isready"], "readyok");
153149

@@ -255,10 +251,10 @@ export class UciEngine {
255251
workersNb = 1,
256252
}: EvaluateGameParams): Promise<GameEval> {
257253
this.throwErrorIfNotReady();
258-
setEvaluationProgress?.(1);
259-
await this.setMultiPv(multiPv);
260254
this.isReady = false;
255+
setEvaluationProgress?.(1);
261256

257+
await this.setMultiPv(multiPv);
262258
await this.sendCommandsToEachWorker(["ucinewgame", "isready"], "readyok");
263259
this.setWorkersNb(workersNb);
264260

@@ -308,7 +304,9 @@ export class UciEngine {
308304
updateEval(i, result);
309305
})
310306
);
307+
311308
await this.setWorkersNb(1);
309+
this.isReady = true;
312310

313311
const positionsWithClassification = getMovesClassification(
314312
positions,
@@ -322,7 +320,6 @@ export class UciEngine {
322320
playersRatings?.black
323321
);
324322

325-
this.isReady = true;
326323
return {
327324
positions: positionsWithClassification,
328325
estimatedElo,
@@ -369,7 +366,7 @@ export class UciEngine {
369366

370367
const lichessEvalPromise = getLichessEval(fen, multiPv);
371368

372-
await this.stopSearch();
369+
await this.stopAllCurrentJobs();
373370
await this.setMultiPv(multiPv);
374371

375372
const onNewMessage = (messages: string[]) => {
@@ -404,6 +401,8 @@ export class UciEngine {
404401
depth = 16
405402
): Promise<string | undefined> {
406403
this.throwErrorIfNotReady();
404+
405+
await this.stopAllCurrentJobs();
407406
await this.setElo(elo);
408407

409408
console.log(`Evaluating position: ${fen}`);

src/sections/analysis/hooks/useCurrentPosition.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ export const useCurrentPosition = (engine: UciEngine | null) => {
165165

166166
return () => {
167167
if (engine?.getIsReady()) {
168-
engine?.stopSearch();
168+
engine?.stopAllCurrentJobs();
169169
}
170170
};
171171
// eslint-disable-next-line react-hooks/exhaustive-deps

src/sections/analysis/panelHeader/analyzeButton.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ export default function AnalyzeButton() {
101101
black.rating,
102102
]);
103103

104+
useEffect(() => {
105+
setEvaluationProgress(0);
106+
}, [engine, setEvaluationProgress]);
107+
104108
// Automatically analyze when a new game is loaded and ready to analyze
105109
useEffect(() => {
106110
if (!gameEval && readyToAnalyse) {

src/sections/play/board.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export default function BoardContainer() {
4646
playEngineMove();
4747

4848
return () => {
49-
engine?.stopSearch();
49+
engine?.stopAllCurrentJobs();
5050
};
5151
}, [gameFen, isGameInProgress]); // eslint-disable-line react-hooks/exhaustive-deps
5252

0 commit comments

Comments
 (0)