-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.js
92 lines (75 loc) · 2.06 KB
/
engine.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
var ucci = require('./ucci').ucci,
util = require('util');
function UCIEngine(path, options, uci) {
var self = this;
this.engine = new ucci(path, options || {
usemillisec: false,
batch: true,
debug: true,
ponder: false,}, uci);
this.serial = 0;
this.queue = [];
this.current = null;
this.evaldone = null;
// hook up the events, print out on screen
this.engine.on('engineError', function(err) {
util.log(err);
});
this.engine.on('engineState', function(newstate) {
util.log(newstate);
});
this.engine.on('commandSent', function(command) {
util.log(command);
});
this.engine.on('ucciok', function(data) {
util.log(data);
// print engine id
util.log('engine id: \n' + util.inspect(self.engine.id, false, null));
// print engine defaults
util.log('engine defaults: \n' + util.inspect(self.engine.defaults, false, null));
// trigger the readyok event.
self.engine.isready();
});
this.engine.on('bye', function(data) {
util.log(data);
});
this.engine.on('id', function(data) {
util.log(data);
});
this.engine.on('option', function(data) {
util.log(data);
});
this.engine.on('readyok', function(data) {
if (self.queue.length > 0) {
self.current = self.queue.shift();
self.engine.position(self.current.p);
self.engine.go('', 'depth 6');
}
});
this.engine.on('bestmove', function(data) {
if (self.current && self.evaldone) {
self.current.m = data;
self.evaldone(self.current);
}
if (self.queue.length > 0) {
self.engine.isready();
}
});
};
UCIEngine.prototype.launch = function() {
this.engine.loadEngine();
this.engine.boot();
};
UCIEngine.prototype.quit = function() {
this.engine.quit();
};
UCIEngine.prototype.evaluate = function(pos, token) {
this.serial = this.serial + 1;
this.queue.push({'p': pos, 's': this.serial, 't': token });
this.engine.isready();
return this.serial;
};
UCIEngine.prototype.evaluate_done = function(callback) {
this.evaldone = callback;
};
exports.UCIEngine = UCIEngine;