Select Git revision
Forked from
SOFT Core / SOFT 260 / Boost Board Game
Source project has a limited visibility.
bei.js 2.24 KiB
import Communicator from './communicator.js';
import GAME_LIBRARY from '../gameLibrary.js';
export default class Engine extends Communicator {
constructor(name, author, version) {
super();
this.name = name;
this.author = author;
this.version = version;
this.game = undefined;
this.position = undefined;
this.taboo = undefined;
this.thinking = false;
}
_move(move) {
this._sendMessage('move', move);
this.thinking = false;
}
onConnect() {
this._sendMessage('protocol', '1.0.0');
if (this.name !== undefined) {
this._sendMessage('id', 'name', this.name);
}
if (this.author !== undefined) {
this._sendMessage('id', 'author', this.author);
}
if (this.version !== undefined) {
this._sendMessage('id', 'version', this.version);
}
this._sendMessage('beiok');
}
onNewGame(gameIdentifier) {
this.game = GAME_LIBRARY.get(gameIdentifier);
this.position = undefined;
this.taboo = undefined;
this._sendMessage(this.game !== undefined ? 'known' : 'unknown');
}
onSetStrength(strength) {}
onSetLine(position, taboo) {
this.position = position;
this.taboo = taboo;
}
onPonder(color) {}
onGo() {
this.thinking = true;
}
onStop() {
if (this.thinking) {
this._move(this.position.getMoveTo(this.position.children[0]));
}
}
onReceiveMessage(type, ...values) {
switch (type) {
case 'bei':
this.onConnect(...values);
break;
case 'isready':
this._sendMessage('readyok');
break;
case 'newgame':
this.onNewGame(values[0]);
break;
case 'setoption':
switch (values[0]) {
case 'strength':
this.onSetStrength(Number(values[1]));
break;
default:
}
break;
case 'setline':
this.onSetLine(
this.game.deserializePosition(values[0]),
new Set(values.map(
(serialization) => this.game.deserializePosition(serialization).signature,
)),
);
break;
case 'ponder':
this.onPonder(Number(values[0]));
break;
case 'go':
this.onGo();
break;
case 'stop':
this.onStop();
break;
case 'quit':
this.deactivate();
break;
default:
}
}
}