Skip to content

Instantly share code, notes, and snippets.

@andyjessop
Last active January 29, 2020 19:28
Show Gist options
  • Save andyjessop/d78dd74a46a11480253b2085927c24d6 to your computer and use it in GitHub Desktop.
Save andyjessop/d78dd74a46a11480253b2085927c24d6 to your computer and use it in GitHub Desktop.
Engine API proposal
const { from, raw, san, to } = await engine.getBestMove(
fen,
{
engine: Engines.Komodo,
personality: Personalities.Aggressive,
strength: 5,
}
);
export namespace Engine {
export interface API {
createEvaluation: (fen: string, options: JCE.Engine.BestMoveOptions) => Evaluation;
getBestMove: (fen: string, engineOptions: EngineOptions) => Promise<BestMove.Extended>;
}
export namespace BestMove {
export interface Model {
fen: string;
from: string;
san: string;
to: string;
}
export interface Extended extends Model {
raw: JCE.Engine.Response;
}
}
export interface EngineOptions {
engine: Engines;
personality: Personalities;
strength: number;
}
export const enum Engines {
Komodo = 'komodo',
Stockfish = 'Stockfish',
}
export interface Evaluation extends EventEmitter {
destroy: () => void;
getLates: () => EvaluationLines;
start: () => void;
}
export interface EvaluationLines {
lines: BestMove.Model[][];
mateIn: JCE.Engine.BaseResponse['mateIn'];
raw: JCE.Engine.Stream;
score: number;
}
export type DestroyEvaluation = () => void;
export const enum Personalities {
Aggressive = 'agressive',
Defensive = 'defensive',
}
}
import { createEvaluation } from 'engine';
const evaluation = createEvaluation(fen, engineOptions);
evaluation.on(Evaluation.Update, handleEvaluation);
evaluation.start();
// ...some time later
evaluation.getLatest(); // { lines, mateIn, raw, score }
evaluation.off(Evaluation.Update, handleEvaluation); // to unsubsribe
evaluation.destroy(); // stops the engine and unsubscribes everything.
function handleEvaluation(data: EvaluationLines) {
const { lines, mateIn, raw, score } = data;
// lines =>
// [
// [{ from: 'e2', to: 'e4', fen: '...', san: 'e4' }],
// [{ from: 'd2', to: 'd4', fen: '...', san: 'd4' }]
// ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment