-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyze.js
190 lines (145 loc) · 4.57 KB
/
analyze.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//process.env['PATH'] = process.env['PATH'] + ':' + process.env['LAMBDA_TASK_ROOT'];
var _ = require('lodash');
var spawn = require('child_process').spawn;
var logs = '';
function runStockfish(gamePositions, cb, depth) {
if (gamePositions.length > 240) {
throw "Too many game positions - max number is 240 (120 move pairs)";
}
// Some heuristics to decide if user inputted depth can be executed before AWS timeout
if (gamePositions.length <= 80) {
// Depth between 10 and 18, default 17
depth = (Number.isInteger(depth) && depth > 9 && depth < 19) ? depth : 17;
} else if (gamePositions.length <= 120) {
// Depth between 10 and 17, default 16
depth = (Number.isInteger(depth) && depth > 9 && depth < 18) ? depth : 16;
} else {
// Depth between 10 and 15, default 15
depth = (Number.isInteger(depth) && depth > 9 && depth < 15) ? depth : 15;
}
var stockfish = spawn('./Stockfish2/src/stockfish');
var lastStartTime;
var currentEval = '?';
var currentlyAnalysing = null;
console.log("Stockfish depth is " + depth);
var analysingFuns = _.map(gamePositions, function(position) {
//console.log(position.fen);
// First of all decorate with selected ply depth
position.ply = depth;
return function() {
// Set state vars
lastStartTime = Date.now();
currentEval = '?';
//console.log("Setting position: " + position.fen);
var movetime = Math.floor(Math.random()*1500) + 2500;
currentlyAnalysing = position;
stockfish.stdin.write('ucinewgame\n');
stockfish.stdin.write('position fen ' + position.fen + '\n');
stockfish.stdin.write('go movetime ' + movetime + '\n');
//stockfish.stdin.write('go depth ' + depth + '\n');
}
});
function analysisDone() {
console.log("Analysis over");
stockfish.stdin.end();
setTimeout(function() {
cb(gamePositions);
}, 0)
}
function nextPos() {
//console.log("Next pos");
if (analysingFuns.length === 0) {
return analysisDone();
}
var f = analysingFuns.pop();
f();
}
stockfish.stdout.on('data', function(data) {
var msg = data.toString('utf8');
//console.log(msg);
var nparts = msg.split('\n');
_.each(nparts, function(part) {
if (part.trim() === '') return;
//console.log(part);
var parts = part.split(" ");
// check for score info
var scoreIndex = parts.indexOf('score');
if (scoreIndex !== -1) {
//console.log("Changing eval: " + currentEval);
currentEval = parts[scoreIndex+2];
}
// check for bestmove info
var bestMoveIndex = parts.indexOf('bestmove');
if (bestMoveIndex !== -1) {
var duration = Date.now() - lastStartTime;
//console.log("Eval is " + currentEval + ", bestmove is " + parts[bestMoveIndex+1] + ", took " + duration + " ms");
currentlyAnalysing.eval = currentEval;
currentlyAnalysing.bestmove = parts[bestMoveIndex+1];
nextPos();
}
});
/*
console.log("------");
console.log(msg);
console.log("------");
*/
});
//setTimeout(nextPos, 600);
nextPos();
}
function runStockfishOnFen(fen, cb, depth) {
// Depth between 16 and 24, default is 22
depth = (Number.isInteger(depth) && depth > 15 && depth < 25) ? depth : 22;
var stockfish = spawn('./Stockfish2/src/stockfish');
var lastStartTime;
var currentEval = '?';
// This should be abstracted so that pgn and fen can use one listening function.
stockfish.stdout.on('data', function(data) {
var msg = data.toString('utf8');
//console.log(msg);
var nparts = msg.split('\n');
_.each(nparts, function(part) {
if (part.trim() === '') return;
//console.log(part);
var parts = part.split(" ");
// check for score info
var scoreIndex = parts.indexOf('score');
if (scoreIndex !== -1) {
//console.log("Changing eval: " + currentEval);
currentEval = parts[scoreIndex+2];
}
// check for bestmove info
var bestMoveIndex = parts.indexOf('bestmove');
if (bestMoveIndex !== -1) {
analysisDone(currentEval, parts[bestMoveIndex+1]);
}
});
/*
console.log("------");
console.log(msg);
console.log("------");
*/
});
function analysisDone(evaluation, bestmove) {
console.log("Fen analysis over");
stockfish.stdin.end();
setTimeout(function() {
cb({
fen: fen,
eval: evaluation,
bestmove: bestmove,
depth: depth
});
}, 0)
}
function launch() {
stockfish.stdin.write('ucinewgame\n');
stockfish.stdin.write('position fen ' + fen + '\n');
stockfish.stdin.write('go depth ' + depth + '\n');
}
setTimeout(launch, 0);
}
//runStockfish();
exports.handler = runStockfish; // For direct AWS test call
exports.pgnHandler = runStockfish;
exports.fenHandler = runStockfishOnFen;