fix : improve engine workers management
This commit is contained in:
Binary file not shown.
|
Before Width: | Height: | Size: 96 KiB After Width: | Height: | Size: 126 KiB |
@@ -12,19 +12,18 @@ const LinearProgressBar = (
|
||||
if (props.value === 0) return null;
|
||||
|
||||
return (
|
||||
<Grid container alignItems="center" justifyContent="center" size={12}>
|
||||
<Typography variant="caption" align="center">
|
||||
{props.label}
|
||||
</Typography>
|
||||
<Grid
|
||||
container
|
||||
width="90%"
|
||||
alignItems="center"
|
||||
justifyContent="center"
|
||||
wrap="nowrap"
|
||||
width="90%"
|
||||
columnGap={2}
|
||||
size={12}
|
||||
>
|
||||
<Typography variant="caption" align="center">
|
||||
{props.label}
|
||||
</Typography>
|
||||
<Grid sx={{ width: "100%" }}>
|
||||
<LinearProgress
|
||||
variant="determinate"
|
||||
@@ -34,9 +33,7 @@ const LinearProgressBar = (
|
||||
height: "5px",
|
||||
[`&.${linearProgressClasses.colorPrimary}`]: {
|
||||
backgroundColor:
|
||||
theme.palette.grey[
|
||||
theme.palette.mode === "light" ? 200 : 700
|
||||
],
|
||||
theme.palette.grey[theme.palette.mode === "light" ? 200 : 700],
|
||||
},
|
||||
[`& .${linearProgressClasses.bar}`]: {
|
||||
borderRadius: 5,
|
||||
@@ -51,7 +48,6 @@ const LinearProgressBar = (
|
||||
)}%`}</Typography>
|
||||
</Grid>
|
||||
</Grid>
|
||||
</Grid>
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
@@ -7,10 +7,7 @@ import { UciEngine } from "@/lib/engine/uciEngine";
|
||||
import { EngineName } from "@/types/enums";
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
export const useEngine = (
|
||||
engineName: EngineName | undefined,
|
||||
workersNb?: number
|
||||
) => {
|
||||
export const useEngine = (engineName: EngineName | undefined) => {
|
||||
const [engine, setEngine] = useState<UciEngine | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
@@ -20,35 +17,32 @@ export const useEngine = (
|
||||
return;
|
||||
}
|
||||
|
||||
pickEngine(engineName, workersNb).then((newEngine) => {
|
||||
pickEngine(engineName).then((newEngine) => {
|
||||
setEngine((prev) => {
|
||||
prev?.shutdown();
|
||||
return newEngine;
|
||||
});
|
||||
});
|
||||
}, [engineName, workersNb]);
|
||||
}, [engineName]);
|
||||
|
||||
return engine;
|
||||
};
|
||||
|
||||
const pickEngine = (
|
||||
engine: EngineName,
|
||||
workersNb?: number
|
||||
): Promise<UciEngine> => {
|
||||
const pickEngine = (engine: EngineName): Promise<UciEngine> => {
|
||||
switch (engine) {
|
||||
case EngineName.Stockfish17:
|
||||
return Stockfish17.create(false, workersNb);
|
||||
return Stockfish17.create(false);
|
||||
case EngineName.Stockfish17Lite:
|
||||
return Stockfish17.create(true, workersNb);
|
||||
return Stockfish17.create(true);
|
||||
case EngineName.Stockfish16_1:
|
||||
return Stockfish16_1.create(false, workersNb);
|
||||
return Stockfish16_1.create(false);
|
||||
case EngineName.Stockfish16_1Lite:
|
||||
return Stockfish16_1.create(true, workersNb);
|
||||
return Stockfish16_1.create(true);
|
||||
case EngineName.Stockfish16:
|
||||
return Stockfish16.create(false, workersNb);
|
||||
return Stockfish16.create(false);
|
||||
case EngineName.Stockfish16NNUE:
|
||||
return Stockfish16.create(true, workersNb);
|
||||
return Stockfish16.create(true);
|
||||
case EngineName.Stockfish11:
|
||||
return Stockfish11.create(workersNb);
|
||||
return Stockfish11.create();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
import { EngineName } from "@/types/enums";
|
||||
import { UciEngine } from "./uciEngine";
|
||||
import { getEngineWorkers } from "./worker";
|
||||
|
||||
export class Stockfish11 {
|
||||
public static async create(workersNb?: number): Promise<UciEngine> {
|
||||
const workers = getEngineWorkers("engines/stockfish-11.js", workersNb);
|
||||
public static async create(): Promise<UciEngine> {
|
||||
const enginePath = "engines/stockfish-11.js";
|
||||
|
||||
return UciEngine.create(EngineName.Stockfish11, workers);
|
||||
return UciEngine.create(EngineName.Stockfish11, enginePath);
|
||||
}
|
||||
|
||||
public static isSupported() {
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
import { EngineName } from "@/types/enums";
|
||||
import { UciEngine } from "./uciEngine";
|
||||
import { isMultiThreadSupported, isWasmSupported } from "./shared";
|
||||
import { getEngineWorkers } from "./worker";
|
||||
import { sendCommandsToWorker } from "./worker";
|
||||
import { EngineWorker } from "@/types/engine";
|
||||
|
||||
export class Stockfish16 {
|
||||
public static async create(
|
||||
nnue?: boolean,
|
||||
workersNb?: number
|
||||
): Promise<UciEngine> {
|
||||
public static async create(nnue?: boolean): Promise<UciEngine> {
|
||||
if (!Stockfish16.isSupported()) {
|
||||
throw new Error("Stockfish 16 is not supported");
|
||||
}
|
||||
@@ -19,10 +17,9 @@ export class Stockfish16 {
|
||||
? "engines/stockfish-16/stockfish-nnue-16.js"
|
||||
: "engines/stockfish-16/stockfish-nnue-16-single.js";
|
||||
|
||||
const customEngineInit = async (
|
||||
sendCommands: UciEngine["sendCommands"]
|
||||
) => {
|
||||
await sendCommands(
|
||||
const customEngineInit = async (worker: EngineWorker) => {
|
||||
await sendCommandsToWorker(
|
||||
worker,
|
||||
[`setoption name Use NNUE value ${!!nnue}`, "isready"],
|
||||
"readyok"
|
||||
);
|
||||
@@ -32,9 +29,7 @@ export class Stockfish16 {
|
||||
? EngineName.Stockfish16NNUE
|
||||
: EngineName.Stockfish16;
|
||||
|
||||
const workers = getEngineWorkers(enginePath, workersNb);
|
||||
|
||||
return UciEngine.create(engineName, workers, customEngineInit);
|
||||
return UciEngine.create(engineName, enginePath, customEngineInit);
|
||||
}
|
||||
|
||||
public static isSupported() {
|
||||
|
||||
@@ -1,13 +1,9 @@
|
||||
import { EngineName } from "@/types/enums";
|
||||
import { UciEngine } from "./uciEngine";
|
||||
import { isMultiThreadSupported, isWasmSupported } from "./shared";
|
||||
import { getEngineWorkers } from "./worker";
|
||||
|
||||
export class Stockfish16_1 {
|
||||
public static async create(
|
||||
lite?: boolean,
|
||||
workersNb?: number
|
||||
): Promise<UciEngine> {
|
||||
public static async create(lite?: boolean): Promise<UciEngine> {
|
||||
if (!Stockfish16_1.isSupported()) {
|
||||
throw new Error("Stockfish 16.1 is not supported");
|
||||
}
|
||||
@@ -23,9 +19,7 @@ export class Stockfish16_1 {
|
||||
? EngineName.Stockfish16_1Lite
|
||||
: EngineName.Stockfish16_1;
|
||||
|
||||
const workers = getEngineWorkers(enginePath, workersNb);
|
||||
|
||||
return UciEngine.create(engineName, workers);
|
||||
return UciEngine.create(engineName, enginePath);
|
||||
}
|
||||
|
||||
public static isSupported() {
|
||||
|
||||
@@ -1,13 +1,9 @@
|
||||
import { EngineName } from "@/types/enums";
|
||||
import { UciEngine } from "./uciEngine";
|
||||
import { isMultiThreadSupported, isWasmSupported } from "./shared";
|
||||
import { getEngineWorkers } from "./worker";
|
||||
|
||||
export class Stockfish17 {
|
||||
public static async create(
|
||||
lite?: boolean,
|
||||
workersNb?: number
|
||||
): Promise<UciEngine> {
|
||||
public static async create(lite?: boolean): Promise<UciEngine> {
|
||||
if (!Stockfish17.isSupported()) {
|
||||
throw new Error("Stockfish 17 is not supported");
|
||||
}
|
||||
@@ -23,9 +19,7 @@ export class Stockfish17 {
|
||||
? EngineName.Stockfish17Lite
|
||||
: EngineName.Stockfish17;
|
||||
|
||||
const workers = getEngineWorkers(enginePath, workersNb);
|
||||
|
||||
return UciEngine.create(engineName, workers);
|
||||
return UciEngine.create(engineName, enginePath);
|
||||
}
|
||||
|
||||
public static isSupported() {
|
||||
|
||||
@@ -15,38 +15,41 @@ import { getLichessEval } from "../lichess";
|
||||
import { getMovesClassification } from "./helpers/moveClassification";
|
||||
import { computeEstimatedElo } from "./helpers/estimateElo";
|
||||
import { EngineWorker, WorkerJob } from "@/types/engine";
|
||||
import { getEngineWorker, sendCommandsToWorker } from "./worker";
|
||||
|
||||
export class UciEngine {
|
||||
private workers: EngineWorker[];
|
||||
public readonly name: EngineName;
|
||||
private workers: EngineWorker[] = [];
|
||||
private workerQueue: WorkerJob[] = [];
|
||||
private isReady = false;
|
||||
private engineName: EngineName;
|
||||
private enginePath: string;
|
||||
private customEngineInit?:
|
||||
| ((worker: EngineWorker) => Promise<void>)
|
||||
| undefined = undefined;
|
||||
private multiPv = 3;
|
||||
private elo: number | undefined = undefined;
|
||||
|
||||
private constructor(engineName: EngineName, workers: EngineWorker[]) {
|
||||
this.engineName = engineName;
|
||||
this.workers = workers;
|
||||
private constructor(
|
||||
engineName: EngineName,
|
||||
enginePath: string,
|
||||
customEngineInit: UciEngine["customEngineInit"]
|
||||
) {
|
||||
this.name = engineName;
|
||||
this.enginePath = enginePath;
|
||||
this.customEngineInit = customEngineInit;
|
||||
}
|
||||
|
||||
public static async create(
|
||||
engineName: EngineName,
|
||||
workers: EngineWorker[],
|
||||
customEngineInit?: (
|
||||
sendCommands: UciEngine["sendCommands"]
|
||||
) => Promise<void>
|
||||
enginePath: string,
|
||||
customEngineInit?: UciEngine["customEngineInit"]
|
||||
): Promise<UciEngine> {
|
||||
const engine = new UciEngine(engineName, workers);
|
||||
const engine = new UciEngine(engineName, enginePath, customEngineInit);
|
||||
|
||||
await engine.broadcastCommands(["uci"], "uciok");
|
||||
await engine.setMultiPv(engine.multiPv, true);
|
||||
await customEngineInit?.(engine.sendCommands.bind(engine));
|
||||
for (const worker of workers) {
|
||||
worker.isReady = true;
|
||||
}
|
||||
await engine.addNewWorker();
|
||||
engine.isReady = true;
|
||||
|
||||
console.log(`${engineName} initialized with ${workers.length} workers`);
|
||||
console.log(`${engineName} initialized`);
|
||||
return engine;
|
||||
}
|
||||
|
||||
@@ -61,31 +64,34 @@ export class UciEngine {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
private releaseWorker(worker: EngineWorker) {
|
||||
worker.isReady = true;
|
||||
private async releaseWorker(worker: EngineWorker) {
|
||||
const nextJob = this.workerQueue.shift();
|
||||
if (!nextJob) {
|
||||
worker.isReady = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (nextJob) {
|
||||
this.sendCommands(
|
||||
const res = await sendCommandsToWorker(
|
||||
worker,
|
||||
nextJob.commands,
|
||||
nextJob.finalMessage,
|
||||
nextJob.onNewMessage
|
||||
).then(nextJob.resolve);
|
||||
}
|
||||
);
|
||||
|
||||
this.releaseWorker(worker);
|
||||
nextJob.resolve(res);
|
||||
}
|
||||
|
||||
private async setMultiPv(multiPv: number, initCase = false) {
|
||||
if (!initCase) {
|
||||
private async setMultiPv(multiPv: number) {
|
||||
if (multiPv === this.multiPv) return;
|
||||
|
||||
this.throwErrorIfNotReady();
|
||||
}
|
||||
|
||||
if (multiPv < 2 || multiPv > 6) {
|
||||
throw new Error(`Invalid MultiPV value : ${multiPv}`);
|
||||
}
|
||||
|
||||
await this.broadcastCommands(
|
||||
await this.sendCommandsToEachWorker(
|
||||
[`setoption name MultiPV value ${multiPv}`, "isready"],
|
||||
"readyok"
|
||||
);
|
||||
@@ -93,23 +99,21 @@ export class UciEngine {
|
||||
this.multiPv = multiPv;
|
||||
}
|
||||
|
||||
private async setElo(elo: number, initCase = false) {
|
||||
if (!initCase) {
|
||||
private async setElo(elo: number) {
|
||||
if (elo === this.elo) return;
|
||||
|
||||
this.throwErrorIfNotReady();
|
||||
}
|
||||
|
||||
if (elo < 1320 || elo > 3190) {
|
||||
throw new Error(`Invalid Elo value : ${elo}`);
|
||||
}
|
||||
|
||||
await this.broadcastCommands(
|
||||
await this.sendCommandsToEachWorker(
|
||||
["setoption name UCI_LimitStrength value true", "isready"],
|
||||
"readyok"
|
||||
);
|
||||
|
||||
await this.broadcastCommands(
|
||||
await this.sendCommandsToEachWorker(
|
||||
[`setoption name UCI_Elo value ${elo}`, "isready"],
|
||||
"readyok"
|
||||
);
|
||||
@@ -117,9 +121,13 @@ export class UciEngine {
|
||||
this.elo = elo;
|
||||
}
|
||||
|
||||
public getIsReady(): boolean {
|
||||
return this.isReady;
|
||||
}
|
||||
|
||||
private throwErrorIfNotReady() {
|
||||
if (!this.isReady) {
|
||||
throw new Error(`${this.engineName} is not ready`);
|
||||
throw new Error(`${this.name} is not ready`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,21 +136,21 @@ export class UciEngine {
|
||||
this.workerQueue = [];
|
||||
|
||||
for (const worker of this.workers) {
|
||||
this.terminateWorker(worker);
|
||||
}
|
||||
|
||||
console.log(`${this.name} shutdown`);
|
||||
}
|
||||
|
||||
private terminateWorker(worker: EngineWorker) {
|
||||
worker.uci("quit");
|
||||
worker.terminate?.();
|
||||
worker.isReady = false;
|
||||
}
|
||||
|
||||
console.log(`${this.engineName} shutdown`);
|
||||
}
|
||||
|
||||
public getIsReady(): boolean {
|
||||
return this.isReady;
|
||||
}
|
||||
|
||||
public async stopSearch(): Promise<void> {
|
||||
this.workerQueue = [];
|
||||
await this.broadcastCommands(["stop", "isready"], "readyok");
|
||||
await this.sendCommandsToEachWorker(["stop", "isready"], "readyok");
|
||||
|
||||
for (const worker of this.workers) {
|
||||
this.releaseWorker(worker);
|
||||
@@ -167,46 +175,74 @@ export class UciEngine {
|
||||
});
|
||||
}
|
||||
|
||||
return this.sendCommandsToWorker(
|
||||
const res = await sendCommandsToWorker(
|
||||
worker,
|
||||
commands,
|
||||
finalMessage,
|
||||
onNewMessage
|
||||
);
|
||||
}
|
||||
|
||||
private async sendCommandsToWorker(
|
||||
worker: EngineWorker,
|
||||
commands: string[],
|
||||
finalMessage: string,
|
||||
onNewMessage?: (messages: string[]) => void
|
||||
): Promise<string[]> {
|
||||
return new Promise((resolve) => {
|
||||
const messages: string[] = [];
|
||||
worker.listen = (data) => {
|
||||
messages.push(data);
|
||||
onNewMessage?.(messages);
|
||||
|
||||
if (data.startsWith(finalMessage)) {
|
||||
this.releaseWorker(worker);
|
||||
resolve(messages);
|
||||
}
|
||||
};
|
||||
for (const command of commands) {
|
||||
worker.uci(command);
|
||||
}
|
||||
});
|
||||
return res;
|
||||
}
|
||||
|
||||
private async broadcastCommands(
|
||||
private async sendCommandsToEachWorker(
|
||||
commands: string[],
|
||||
finalMessage: string,
|
||||
onNewMessage?: (messages: string[]) => void
|
||||
): Promise<void> {
|
||||
await Promise.all(
|
||||
this.workers.map((worker) =>
|
||||
this.sendCommandsToWorker(worker, commands, finalMessage, onNewMessage)
|
||||
)
|
||||
this.workers.map(async (worker) => {
|
||||
await sendCommandsToWorker(
|
||||
worker,
|
||||
commands,
|
||||
finalMessage,
|
||||
onNewMessage
|
||||
);
|
||||
this.releaseWorker(worker);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
private async addNewWorker() {
|
||||
const worker = getEngineWorker(this.enginePath);
|
||||
|
||||
await sendCommandsToWorker(worker, ["uci"], "uciok");
|
||||
await sendCommandsToWorker(
|
||||
worker,
|
||||
[`setoption name MultiPV value ${this.multiPv}`, "isready"],
|
||||
"readyok"
|
||||
);
|
||||
await this.customEngineInit?.(worker);
|
||||
await sendCommandsToWorker(worker, ["ucinewgame", "isready"], "readyok");
|
||||
|
||||
this.workers.push(worker);
|
||||
this.releaseWorker(worker);
|
||||
}
|
||||
|
||||
private async setWorkersNb(workersNb: number) {
|
||||
if (workersNb === this.workers.length) return;
|
||||
|
||||
if (workersNb < 1) {
|
||||
throw new Error(
|
||||
`Number of workers must be greater than 0, got ${workersNb} instead`
|
||||
);
|
||||
}
|
||||
|
||||
if (workersNb < this.workers.length) {
|
||||
const workersToRemove = this.workers.slice(workersNb);
|
||||
this.workers = this.workers.slice(0, workersNb);
|
||||
|
||||
for (const worker of workersToRemove) {
|
||||
this.terminateWorker(worker);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const workersNbToCreate = workersNb - this.workers.length;
|
||||
|
||||
await Promise.all(
|
||||
new Array(workersNbToCreate).fill(0).map(() => this.addNewWorker())
|
||||
);
|
||||
}
|
||||
|
||||
@@ -217,13 +253,15 @@ export class UciEngine {
|
||||
multiPv = this.multiPv,
|
||||
setEvaluationProgress,
|
||||
playersRatings,
|
||||
workersNb = 1,
|
||||
}: EvaluateGameParams): Promise<GameEval> {
|
||||
this.throwErrorIfNotReady();
|
||||
setEvaluationProgress?.(1);
|
||||
await this.setMultiPv(multiPv);
|
||||
this.isReady = false;
|
||||
|
||||
await this.broadcastCommands(["ucinewgame", "isready"], "readyok");
|
||||
await this.sendCommandsToEachWorker(["ucinewgame", "isready"], "readyok");
|
||||
this.setWorkersNb(workersNb);
|
||||
|
||||
const positions: PositionEval[] = new Array(fens.length);
|
||||
let completed = 0;
|
||||
@@ -267,10 +305,11 @@ export class UciEngine {
|
||||
return;
|
||||
}
|
||||
|
||||
const result = await this.evaluatePosition(fen, depth);
|
||||
const result = await this.evaluatePosition(fen, depth, workersNb);
|
||||
updateEval(i, result);
|
||||
})
|
||||
);
|
||||
await this.setWorkersNb(1);
|
||||
|
||||
const positionsWithClassification = getMovesClassification(
|
||||
positions,
|
||||
@@ -290,7 +329,7 @@ export class UciEngine {
|
||||
estimatedElo,
|
||||
accuracy,
|
||||
settings: {
|
||||
engine: this.engineName,
|
||||
engine: this.name,
|
||||
date: new Date().toISOString(),
|
||||
depth,
|
||||
multiPv,
|
||||
@@ -300,9 +339,10 @@ export class UciEngine {
|
||||
|
||||
private async evaluatePosition(
|
||||
fen: string,
|
||||
depth = 16
|
||||
depth = 16,
|
||||
workersNb: number
|
||||
): Promise<PositionEval> {
|
||||
if (this.workers.length < 2) {
|
||||
if (workersNb < 2) {
|
||||
const lichessEval = await getLichessEval(fen, this.multiPv);
|
||||
if (
|
||||
lichessEval.lines.length >= this.multiPv &&
|
||||
|
||||
@@ -1,30 +1,8 @@
|
||||
import { EngineWorker } from "@/types/engine";
|
||||
|
||||
export const getEngineWorkers = (
|
||||
enginePath: string,
|
||||
workersInputNb?: number
|
||||
): EngineWorker[] => {
|
||||
if (workersInputNb !== undefined && workersInputNb < 1) {
|
||||
throw new Error(
|
||||
`Number of workers must be greater than 0, got ${workersInputNb} instead`
|
||||
);
|
||||
}
|
||||
export const getEngineWorker = (enginePath: string): EngineWorker => {
|
||||
console.log(`Creating worker from ${enginePath}`);
|
||||
|
||||
const engineWorkers: EngineWorker[] = [];
|
||||
|
||||
const maxWorkersNb = Math.max(
|
||||
1,
|
||||
navigator.hardwareConcurrency - 4,
|
||||
Math.ceil((navigator.hardwareConcurrency * 2) / 3)
|
||||
);
|
||||
const deviceMemory =
|
||||
"deviceMemory" in navigator && typeof navigator.deviceMemory === "number"
|
||||
? navigator.deviceMemory
|
||||
: 4;
|
||||
const workersNb = workersInputNb ?? Math.min(maxWorkersNb, deviceMemory, 10);
|
||||
console.log(`Starting ${workersNb} workers from ${enginePath}`);
|
||||
|
||||
for (let i = 0; i < workersNb; i++) {
|
||||
const worker = new window.Worker(enginePath);
|
||||
|
||||
const engineWorker: EngineWorker = {
|
||||
@@ -38,8 +16,44 @@ export const getEngineWorkers = (
|
||||
engineWorker.listen(event.data);
|
||||
};
|
||||
|
||||
engineWorkers.push(engineWorker);
|
||||
}
|
||||
|
||||
return engineWorkers;
|
||||
return engineWorker;
|
||||
};
|
||||
|
||||
export const sendCommandsToWorker = (
|
||||
worker: EngineWorker,
|
||||
commands: string[],
|
||||
finalMessage: string,
|
||||
onNewMessage?: (messages: string[]) => void
|
||||
): Promise<string[]> => {
|
||||
return new Promise((resolve) => {
|
||||
const messages: string[] = [];
|
||||
|
||||
worker.listen = (data) => {
|
||||
messages.push(data);
|
||||
onNewMessage?.(messages);
|
||||
|
||||
if (data.startsWith(finalMessage)) {
|
||||
resolve(messages);
|
||||
}
|
||||
};
|
||||
|
||||
for (const command of commands) {
|
||||
worker.uci(command);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
export const getRecommendedWorkersNb = (): number => {
|
||||
const maxWorkersNbFromThreads = Math.max(
|
||||
1,
|
||||
navigator.hardwareConcurrency - 4,
|
||||
Math.ceil((navigator.hardwareConcurrency * 2) / 3)
|
||||
);
|
||||
|
||||
const maxWorkersNbFromMemory =
|
||||
"deviceMemory" in navigator && typeof navigator.deviceMemory === "number"
|
||||
? navigator.deviceMemory
|
||||
: 4;
|
||||
|
||||
return Math.min(maxWorkersNbFromThreads, maxWorkersNbFromMemory, 10);
|
||||
};
|
||||
|
||||
@@ -10,15 +10,13 @@ import {
|
||||
import { CurrentPosition, PositionEval } from "@/types/eval";
|
||||
import { useAtom, useAtomValue } from "jotai";
|
||||
import { useEffect } from "react";
|
||||
import { useEngine } from "../../../hooks/useEngine";
|
||||
import { EngineName } from "@/types/enums";
|
||||
import { getEvaluateGameParams } from "@/lib/chess";
|
||||
import { getMovesClassification } from "@/lib/engine/helpers/moveClassification";
|
||||
import { openings } from "@/data/openings";
|
||||
import { UciEngine } from "@/lib/engine/uciEngine";
|
||||
|
||||
export const useCurrentPosition = (engineName?: EngineName) => {
|
||||
export const useCurrentPosition = (engine: UciEngine | null) => {
|
||||
const [currentPosition, setCurrentPosition] = useAtom(currentPositionAtom);
|
||||
const engine = useEngine(engineName, 1);
|
||||
const gameEval = useAtomValue(gameEvalAtom);
|
||||
const game = useAtomValue(gameAtom);
|
||||
const board = useAtomValue(boardAtom);
|
||||
@@ -77,7 +75,7 @@ export const useCurrentPosition = (engineName?: EngineName) => {
|
||||
if (
|
||||
!position.eval &&
|
||||
engine?.getIsReady() &&
|
||||
engineName &&
|
||||
engine.name &&
|
||||
!board.isCheckmate() &&
|
||||
!board.isStalemate()
|
||||
) {
|
||||
@@ -85,12 +83,13 @@ export const useCurrentPosition = (engineName?: EngineName) => {
|
||||
fen: string,
|
||||
setPartialEval?: (positionEval: PositionEval) => void
|
||||
) => {
|
||||
if (!engine?.getIsReady() || !engineName)
|
||||
if (!engine.getIsReady()) {
|
||||
throw new Error("Engine not ready");
|
||||
}
|
||||
const savedEval = savedEvals[fen];
|
||||
if (
|
||||
savedEval &&
|
||||
savedEval.engine === engineName &&
|
||||
savedEval.engine === engine.name &&
|
||||
(savedEval.lines?.length ?? 0) >= multiPv &&
|
||||
(savedEval.lines[0].depth ?? 0) >= depth
|
||||
) {
|
||||
@@ -111,7 +110,7 @@ export const useCurrentPosition = (engineName?: EngineName) => {
|
||||
|
||||
setSavedEvals((prev) => ({
|
||||
...prev,
|
||||
[fen]: { ...rawPositionEval, engine: engineName },
|
||||
[fen]: { ...rawPositionEval, engine: engine.name },
|
||||
}));
|
||||
|
||||
return rawPositionEval;
|
||||
@@ -165,7 +164,9 @@ export const useCurrentPosition = (engineName?: EngineName) => {
|
||||
}
|
||||
|
||||
return () => {
|
||||
if (engine?.getIsReady()) {
|
||||
engine?.stopSearch();
|
||||
}
|
||||
};
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [gameEval, board, game, engine, depth, multiPv]);
|
||||
|
||||
@@ -7,13 +7,12 @@ import {
|
||||
import { useAtomValue } from "jotai";
|
||||
import {
|
||||
boardAtom,
|
||||
currentPositionAtom,
|
||||
engineMultiPvAtom,
|
||||
engineNameAtom,
|
||||
gameAtom,
|
||||
gameEvalAtom,
|
||||
} from "../../states";
|
||||
import LineEvaluation from "./lineEvaluation";
|
||||
import { useCurrentPosition } from "../../hooks/useCurrentPosition";
|
||||
import { LineEval } from "@/types/eval";
|
||||
import PlayersMetric from "./playersMetric";
|
||||
import MoveInfo from "./moveInfo";
|
||||
@@ -21,8 +20,7 @@ import Opening from "./opening";
|
||||
|
||||
export default function AnalysisTab(props: GridProps) {
|
||||
const linesNumber = useAtomValue(engineMultiPvAtom);
|
||||
const engineName = useAtomValue(engineNameAtom);
|
||||
const position = useCurrentPosition(engineName);
|
||||
const position = useAtomValue(currentPositionAtom);
|
||||
const game = useAtomValue(gameAtom);
|
||||
const board = useAtomValue(boardAtom);
|
||||
const gameEval = useAtomValue(gameEvalAtom);
|
||||
|
||||
@@ -18,10 +18,13 @@ import { SavedEvals } from "@/types/eval";
|
||||
import { useEffect, useCallback } from "react";
|
||||
import { usePlayersData } from "@/hooks/usePlayersData";
|
||||
import { Typography } from "@mui/material";
|
||||
import { useCurrentPosition } from "../hooks/useCurrentPosition";
|
||||
import { getRecommendedWorkersNb } from "@/lib/engine/worker";
|
||||
|
||||
export default function AnalyzeButton() {
|
||||
const engineName = useAtomValue(engineNameAtom);
|
||||
const engine = useEngine(engineName);
|
||||
useCurrentPosition(engine);
|
||||
const [evaluationProgress, setEvaluationProgress] = useAtom(
|
||||
evaluationProgressAtom
|
||||
);
|
||||
@@ -55,6 +58,7 @@ export default function AnalyzeButton() {
|
||||
white: white?.rating,
|
||||
black: black?.rating,
|
||||
},
|
||||
workersNb: getRecommendedWorkersNb(),
|
||||
});
|
||||
|
||||
setEval(newGameEval);
|
||||
|
||||
@@ -19,7 +19,7 @@ import { usePlayersData } from "@/hooks/usePlayersData";
|
||||
export default function BoardContainer() {
|
||||
const screenSize = useScreenSize();
|
||||
const engineName = useAtomValue(enginePlayNameAtom);
|
||||
const engine = useEngine(engineName, 1);
|
||||
const engine = useEngine(engineName);
|
||||
const game = useAtomValue(gameAtom);
|
||||
const { white, black } = usePlayersData(gameAtom);
|
||||
const playerColor = useAtomValue(playerColorAtom);
|
||||
|
||||
@@ -62,6 +62,7 @@ export interface EvaluateGameParams {
|
||||
multiPv?: number;
|
||||
setEvaluationProgress?: (value: number) => void;
|
||||
playersRatings?: { white?: number; black?: number };
|
||||
workersNb?: number;
|
||||
}
|
||||
|
||||
export interface SavedEval {
|
||||
|
||||
Reference in New Issue
Block a user