feat : init game eval
This commit is contained in:
@@ -1,14 +1,16 @@
|
||||
import { GameEval, MoveEval } from "@/types/eval";
|
||||
|
||||
export class Stockfish {
|
||||
private worker: Worker;
|
||||
private ready: boolean = false;
|
||||
|
||||
constructor() {
|
||||
this.worker = new Worker(
|
||||
this.isWasmSupported() ? "./stockfish.wasm.js" : "./stockfish.js"
|
||||
this.isWasmSupported()
|
||||
? "engines/stockfish.wasm.js"
|
||||
: "engines/stockfish.js"
|
||||
);
|
||||
|
||||
this.sendCommands(["uci"], "uciok");
|
||||
this.sendCommands(["setoption name MultiPV value 2", "isready"], "readyok");
|
||||
|
||||
console.log("Stockfish created");
|
||||
}
|
||||
|
||||
@@ -21,6 +23,25 @@ export class Stockfish {
|
||||
);
|
||||
}
|
||||
|
||||
public async init(): Promise<void> {
|
||||
await this.sendCommands(["uci"], "uciok");
|
||||
await this.sendCommands(
|
||||
["setoption name MultiPV value 2", "isready"],
|
||||
"readyok"
|
||||
);
|
||||
this.ready = true;
|
||||
}
|
||||
|
||||
public shutdown(): void {
|
||||
this.ready = false;
|
||||
this.worker.postMessage("quit");
|
||||
this.worker.terminate();
|
||||
}
|
||||
|
||||
public isReady(): boolean {
|
||||
return this.ready;
|
||||
}
|
||||
|
||||
private async sendCommands(
|
||||
commands: string[],
|
||||
finalMessage: string
|
||||
@@ -42,15 +63,20 @@ export class Stockfish {
|
||||
}
|
||||
|
||||
public async evaluateGame(fens: string[], depth = 16): Promise<GameEval> {
|
||||
this.ready = false;
|
||||
console.log("Evaluating game");
|
||||
await this.sendCommands(["ucinewgame", "isready"], "readyok");
|
||||
this.worker.postMessage("position startpos");
|
||||
|
||||
const moves: MoveEval[] = [];
|
||||
for (const fen of fens) {
|
||||
console.log(`Evaluating position: ${fen}`);
|
||||
const result = await this.evaluatePosition(fen, depth);
|
||||
moves.push(result);
|
||||
}
|
||||
|
||||
this.ready = true;
|
||||
console.log("Game evaluated");
|
||||
return { moves };
|
||||
}
|
||||
|
||||
File diff suppressed because one or more lines are too long
Binary file not shown.
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user