feat : add engines choices

This commit is contained in:
GuillaumeSD
2024-04-14 04:07:18 +02:00
parent 56b267762e
commit 1546709452
20 changed files with 172 additions and 37 deletions

View File

@@ -0,0 +1,8 @@
import { EngineName } from "@/types/enums";
import { UciEngine } from "./uciEngine";
export class Stockfish11 extends UciEngine {
constructor() {
super(EngineName.Stockfish11, "engines/stockfish-11.js");
}
}

View File

@@ -2,17 +2,29 @@ import { EngineName } from "@/types/enums";
import { UciEngine } from "./uciEngine";
export class Stockfish16 extends UciEngine {
constructor() {
const isWasmSupported = Stockfish16.isWasmSupported();
constructor(nnue?: boolean) {
if (!Stockfish16.isSupported()) {
throw new Error("Stockfish 16 is not supported");
}
const enginePath = isWasmSupported
? "engines/stockfish-wasm/stockfish-nnue-16.js"
: "engines/stockfish.js";
const isMultiThreadSupported = Stockfish16.isMultiThreadSupported();
if (!isMultiThreadSupported) console.log("Single thread mode");
super(EngineName.Stockfish16, enginePath);
const enginePath = isMultiThreadSupported
? "engines/stockfish-16-wasm/stockfish-nnue-16.js"
: "engines/stockfish-16-wasm/stockfish-nnue-16-single.js";
const customEngineInit = async () => {
await this.sendCommands(
[`setoption name Use NNUE value ${!!nnue}`, "isready"],
"readyok"
);
};
super(EngineName.Stockfish16, enginePath, customEngineInit);
}
public static isWasmSupported() {
public static isSupported() {
return (
typeof WebAssembly === "object" &&
WebAssembly.validate(
@@ -20,4 +32,8 @@ export class Stockfish16 extends UciEngine {
)
);
}
public static isMultiThreadSupported() {
return SharedArrayBuffer !== undefined;
}
}

View File

@@ -20,10 +20,16 @@ export abstract class UciEngine {
private engineName: EngineName;
private multiPv = 3;
private skillLevel: number | undefined = undefined;
private customEngineInit?: () => Promise<void>;
constructor(engineName: EngineName, enginePath: string) {
constructor(
engineName: EngineName,
enginePath: string,
customEngineInit?: () => Promise<void>
) {
this.engineName = engineName;
this.worker = new Worker(enginePath);
this.customEngineInit = customEngineInit;
console.log(`${engineName} created`);
}
@@ -31,6 +37,7 @@ export abstract class UciEngine {
public async init(): Promise<void> {
await this.sendCommands(["uci"], "uciok");
await this.setMultiPv(this.multiPv, true);
await this.customEngineInit?.();
this.ready = true;
console.log(`${this.engineName} initialized`);
}
@@ -94,7 +101,7 @@ export abstract class UciEngine {
await this.sendCommands(["stop", "isready"], "readyok");
}
private async sendCommands(
protected async sendCommands(
commands: string[],
finalMessage: string,
onNewMessage?: (messages: string[]) => void