forked from online-go/gtp2ogs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
622 lines (564 loc) · 26.1 KB
/
Copy pathbot.js
File metadata and controls
622 lines (564 loc) · 26.1 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
// vim: tw=120 softtabstop=4 shiftwidth=4
let child_process = require('child_process');
let console = require('./console').console;
let config = require('./config');
/*********/
/** Bot **/
/*********/
class Bot {
constructor(conn, game, cmd) {{{
this.conn = conn;
this.game = game;
this.commands_sent = 0;
this.command_callbacks = [];
this.command_error_callbacks = [];
this.firstmove = true;
this.ignore = false; // Ignore output from bot ?
// Set to true when the bot process has died and needs to be restarted before it can be used again.
this.dead = false;
// Set to true when there is a command failure or a bot failure and the game fail counter should be incremented.
// After a few failures we stop retrying and resign the game.
this.failed = false;
try {
this.proc = child_process.spawn(cmd[0], cmd.slice(1));
} catch (e) {
this.log("Failed to start the bot: ", e);
this.ignore = true;
this.dead = true;
this.failed = true;
return;
}
if (config.DEBUG) this.log("Starting ", cmd.join(' '));
this.proc.stderr.on('data', (data) => {
if (this.ignore) return;
this.error("stderr: " + data);
});
let stdout_buffer = "";
this.proc.stdout.on('data', (data) => {
if (this.ignore) return;
stdout_buffer += data.toString();
if (config.json) {
try {
stdout_buffer = JSON.parse(stdout_buffer);
} catch (e) {
// Partial result received, wait until we can parse the result
return;
}
}
if (!stdout_buffer || stdout_buffer[stdout_buffer.length-1] !== '\n') {
//this.log("Partial result received, buffering until the output ends with a newline");
return;
}
if (config.DEBUG) {
this.log("<<<", stdout_buffer.trim());
}
let lines = stdout_buffer.split("\n");
stdout_buffer = "";
for (let i=0; i < lines.length; ++i) {
let line = lines[i];
if (line.trim() === "") {
continue;
}
if (line[0] === '=') {
while (lines[i].trim() !== "") {
++i;
}
let cb = this.command_callbacks.shift();
this.command_error_callbacks.shift();
if (cb) cb(line.substr(1).trim());
}
else if (line.trim()[0] === '?') {
this.log(line);
while (lines[i].trim() !== "") {
++i;
this.log(lines[i]);
}
this.failed = true;
this.command_callbacks.shift();
let eb = this.command_error_callbacks.shift();
if (eb) eb(line.substr(1).trim());
}
else {
this.log("Unexpected output: ", line);
this.failed = true;
this.command_callbacks.shift();
let eb = this.command_error_callbacks.shift();
if (eb) eb();
//throw new Error("Unexpected output: " + line);
}
}
});
this.proc.on('exit', (code) => {
if (config.DEBUG) {
this.log('Bot exited');
}
this.command_callbacks.shift();
this.dead = true;
let eb = this.command_error_callbacks.shift();
if (eb) eb(code);
});
this.proc.stdin.on('error', (code) => {
if (config.DEBUG) {
this.log('Bot stdin write error');
}
this.command_callbacks.shift();
this.dead = true;
this.failed = true;
let eb = this.command_error_callbacks.shift();
if (eb) eb(code);
});
}}}
pid() {
if (this.proc) {
return this.proc.pid;
} else {
return -1;
}
}
log() { /* {{{ */
let arr = ["[" + this.pid() + "]"];
for (let i=0; i < arguments.length; ++i) {
arr.push(arguments[i]);
}
console.log.apply(null, arr);
} /* }}} */
error() { /* {{{ */
let arr = ["[" + this.pid() + "]"];
for (let i=0; i < arguments.length; ++i) {
arr.push(arguments[i]);
}
console.error.apply(null, arr);
} /* }}} */
verbose() { /* {{{ */
let arr = ["[" + this.pid() + "]"];
for (let i=0; i < arguments.length; ++i) {
arr.push(arguments[i]);
}
console.verbose.apply(null, arr);
} /* }}} */
loadClock(state) {
//
// References:
// http://www.lysator.liu.se/~gunnar/gtp/gtp2-spec-draft2/gtp2-spec.html#sec:time-handling
// http://www.weddslist.com/kgs/how/kgsGtp.html
//
// GTP v2 only supports Canadian byoyomi, no timer (see spec above), and absolute (period time zero).
//
// kgs-time_settings adds support for Japanese byoyomi.
//
// TODO: Use known_commands to check for kgs-time_settings support automatically.
//
// The kgsGtp interface (http://www.weddslist.com/kgs/how/kgsGtp.html) converts byoyomi to absolute time
// for bots that don't support kgs-time_settings by using main_time plus periods * period_time. But then the bot
// would view that as the total time left for entire rest of game...
//
// Japanese byoyomi with one period left could be viewed as a special case of Canadian byoyomi where the number of stones is always = 1
//
if (config.NOCLOCK) return;
let black_offset = 0;
let white_offset = 0;
//let now = state.clock.now ? state.clock.now : (Date.now() - this.conn.clock_drift);
let now = Date.now() - this.conn.clock_drift;
if (state.clock.current_player === state.clock.black_player_id) {
black_offset = ((this.firstmove===true ? config.startupbuffer : 0) + now - state.clock.last_move) / 1000;
} else {
white_offset = ((this.firstmove===true ? config.startupbuffer : 0) + now - state.clock.last_move) / 1000;
}
if (state.time_control.system === 'byoyomi') {
// GTP spec says time_left should have 0 for stones until main_time has run out.
//
// If the bot connects in the middle of a byoyomi period, it won't know how much time it has left before the period expires.
// When restarting the bot mid-match during testing, it sometimes lost on timeout because of this. To work around it, we can
// reduce the byoyomi period size by the offset. Not strictly accurate but GTP protocol provides nothing better. Once bot moves
// again, the next state setup should have this corrected. This problem would happen if a bot were to crash and re-start during
// a period. This is only an issue if it is our turn, and our main time left is 0.
//
if (config.KGSTIME) {
let black_timeleft = 0;
let white_timeleft = 0;
if (state.clock.black_time.thinking_time > 0) {
black_timeleft = Math.max( Math.floor(state.clock.black_time.thinking_time - black_offset), 0);
} else {
black_timeleft = Math.max( Math.floor(state.time_control.period_time - black_offset), 0);
}
if (state.clock.white_time.thinking_time > 0) {
white_timeleft = Math.max( Math.floor(state.clock.white_time.thinking_time - white_offset), 0);
} else {
white_timeleft = Math.max( Math.floor(state.time_control.period_time - white_offset), 0);
}
// Restarting the bot can make a time left so small the bot makes a rushed terrible move. If we have less than half a period
// to think and extra periods left, lets go ahead and use the period up.
//
if (state.clock.black_time.thinking_time === 0 && state.clock.black_time.periods > 1 && black_timeleft < state.time_control.period_time / 2) {
black_timeleft = Math.max( Math.floor(state.time_control.period_time - black_offset) + state.time_control.period_time, 0 );
state.clock.black_time.periods--;
}
if (state.clock.white_time.thinking_time === 0 && state.clock.white_time.periods > 1 && white_timeleft < state.time_control.period_time / 2) {
white_timeleft = Math.max( Math.floor(state.time_control.period_time - white_offset) + state.time_control.period_time, 0 );
state.clock.white_time.periods--;
}
this.command("kgs-time_settings byoyomi " + state.time_control.main_time + " "
+ Math.floor(state.time_control.period_time -
(state.clock.current_player === state.clock.black_player_id ? black_offset : white_offset)
)
+ " " + state.time_control.periods);
// Turns out in Japanese byoyomi mode, for Leela and pacci, they expect time left in the current byoyomi period on time_left
//
this.command("time_left black " + black_timeleft + " " + (state.clock.black_time.thinking_time > 0 ? "0" : state.clock.black_time.periods));
this.command("time_left white " + white_timeleft + " " + (state.clock.white_time.thinking_time > 0 ? "0" : state.clock.white_time.periods));
} else {
// OGS enforces the number of periods is always 1 or greater. Let's pretend the final period is a Canadian Byoyomi of 1 stone.
// This lets the bot know it can use the full period per move, not try to fit the rest of the game into the time left.
//
let black_timeleft = Math.max( Math.floor(state.clock.black_time.thinking_time
- black_offset + (state.clock.black_time.periods - 1) * state.time_control.period_time), 0);
let white_timeleft = Math.max( Math.floor(state.clock.white_time.thinking_time
- white_offset + (state.clock.white_time.periods - 1) * state.time_control.period_time), 0);
this.command("time_settings " + (state.time_control.main_time + (state.time_control.periods - 1) * state.time_control.period_time) + " "
+ Math.floor(state.time_control.period_time -
(state.clock.current_player === state.clock.black_player_id
? (black_timeleft > 0 ? 0 : black_offset) : (white_timeleft > 0 ? 0 : white_offset)
)
)
+ " 1");
// Since we're faking byoyomi using Canadian, time_left actually does mean the time left to play our 1 stone.
//
this.command("time_left black " + (black_timeleft > 0 ? black_timeleft + " 0"
: Math.floor(state.time_control.period_time - black_offset) + " 1") );
this.command("time_left white " + (white_timeleft > 0 ? white_timeleft + " 0"
: Math.floor(state.time_control.period_time - white_offset) + " 1") );
}
} else if (state.time_control.system === 'canadian') {
// Canadian Byoyomi is the only time controls GTP v2 officially supports.
//
let black_timeleft = Math.max( Math.floor(state.clock.black_time.thinking_time - black_offset), 0);
let white_timeleft = Math.max( Math.floor(state.clock.white_time.thinking_time - white_offset), 0);
if (config.KGSTIME) {
this.command("kgs-time_settings canadian " + state.time_control.main_time + " "
+ state.time_control.period_time + " " + state.time_control.stones_per_period);
} else {
this.command("time_settings " + state.time_control.main_time + " "
+ state.time_control.period_time + " " + state.time_control.stones_per_period);
}
this.command("time_left black " + (black_timeleft > 0 ? black_timeleft + " 0"
: Math.floor(state.clock.black_time.block_time - black_offset) + " " + state.clock.black_time.moves_left));
this.command("time_left white " + (white_timeleft > 0 ? white_timeleft + " 0"
: Math.floor(state.clock.white_time.block_time - white_offset) + " " + state.clock.white_time.moves_left));
} else if (state.time_control.system === 'fischer') {
// Not supported by kgs-time_settings and I assume most bots. A better way than absolute is to handle this with
// a fake Canadian byoyomi. This should let the bot know a good approximation of how to handle
// the time remaining.
//
let black_timeleft = Math.max( Math.floor(state.clock.black_time.thinking_time - black_offset), 0);
let white_timeleft = Math.max( Math.floor(state.clock.white_time.thinking_time - white_offset), 0);
if (config.KGSTIME) {
this.command("kgs-time_settings canadian " + (state.time_control.initial_time - state.time_control.time_increment)
+ " " + state.time_control.time_increment + " 1");
} else {
this.command("time_settings " + (state.time_control.initial_time - state.time_control.time_increment)
+ " " + state.time_control.time_increment + " 1");
}
// Always tell the bot we are in main time ('0') so it doesn't try to think all of timeleft per move. But
// subtract the increment time above to avoid timeouts.
//
this.command("time_left black " + black_timeleft + " 0");
this.command("time_left white " + white_timeleft + " 0");
} else if (state.time_control.system === 'simple') {
// Simple could also be viewed as a Canadian byomoyi that starts immediately with # of stones = 1
//
this.command("time_settings 0 " + state.time_control.per_move + " 1");
if (state.clock.black_time)
{
let black_timeleft = Math.max( Math.floor((state.clock.black_time - now)/1000 - black_offset), 0);
this.command("time_left black " + black_timeleft + " 1");
this.command("time_left white 1 1");
} else {
let white_timeleft = Math.max( Math.floor((state.clock.white_time - now)/1000 - white_offset), 0);
this.command("time_left black 1 1");
this.command("time_left white " + white_timeleft + " 1");
}
} else if (state.time_control.system === 'absolute') {
let black_timeleft = Math.max( Math.floor(state.clock.black_time.thinking_time - black_offset), 0);
let white_timeleft = Math.max( Math.floor(state.clock.white_time.thinking_time - white_offset), 0);
if (config.KGSTIME) {
this.command("kgs-time_settings absolute " + state.time_control.total_time);
} else {
this.command("time_settings " + state.time_control.total_time + " 0 0");
}
this.command("time_left black " + black_timeleft + " 0");
this.command("time_left white " + white_timeleft + " 0");
}
// OGS doesn't actually send 'none' time control type
//
/* else if (state.time_control.system === 'none') {
if (config.KGSTIME) {
this.command("kgs-time_settings none");
} else {
// GTP v2 says byoyomi time > 0 and stones = 0 means no time limits
//
this.command("time_settings 0 1 0");
}
} */
}
loadState(state, cb, eb) { /* {{{ */
if (this.dead) {
if (config.DEBUG) { this.log("Attempting to load dead bot") }
this.failed = true;
if (eb) { eb() }
return false;
}
this.command("boardsize " + state.width, () => {}, eb);
this.command("clear_board", () => {}, eb);
this.command("komi " + state.komi, () => {}, eb);
//this.log(state);
//this.loadClock(state);
let have_initial_state = false;
if (state.initial_state) {
let black = decodeMoves(state.initial_state.black, state.width);
let white = decodeMoves(state.initial_state.white, state.width);
have_initial_state = (black.length || white.length);
for (let i=0; i < black.length; ++i)
this.command("play black " + move2gtpvertex(black[i], state.width), () => {}, eb);
for (let i=0; i < white.length; ++i)
this.command("play white " + move2gtpvertex(white[i], state.width), () => {}, eb);
}
// Replay moves made
let color = state.initial_player;
let doing_handicap = (!have_initial_state && state.free_handicap_placement && state.handicap > 1);
let handicap_moves = [];
let moves = decodeMoves(state.moves, state.width);
for (let i=0; i < moves.length; ++i) {
let move = moves[i];
let c = color
// Use set_free_handicap for handicap stones, play otherwise.
if (doing_handicap && handicap_moves.length < state.handicap) {
handicap_moves.push(move);
if (handicap_moves.length === state.handicap)
this.sendHandicapMoves(handicap_moves, state.width);
else continue; // don't switch color.
} else {
this.command("play " + c + ' ' + move2gtpvertex(move, state.width))
}
color = color === 'black' ? 'white' : 'black';
}
if (config.SHOWBOARD) {
this.command("showboard", cb, eb);
}
return true;
} /* }}} */
command(str, cb, eb, final_command) { /* {{{ */
if (this.dead) {
if (config.DEBUG) { this.log("Attempting to send a command to dead bot:", str) }
this.failed = true;
if (eb) { eb() }
return;
}
this.command_callbacks.push(cb);
this.command_error_callbacks.push(eb);
if (config.DEBUG) {
this.log(">>>", str);
}
try {
if (config.json) {
if (!this.json_initialized) {
this.proc.stdin.write(`{"gtp_commands": [`);
this.json_initialized = true;
} else {
this.proc.stdin.write(",");
}
this.proc.stdin.write(JSON.stringify(str));
if (final_command) {
this.proc.stdin.write("]}");
this.proc.stdin.end()
}
} else {
this.proc.stdin.write(str + "\r\n");
}
} catch (e) {
// I think this does not normally happen, the exception will usually be raised in the async write handler
// and delivered through an 'error' event.
//
this.log("Failed to send command: ", str);
this.log(e);
this.dead = true;
this.failed = true;
// Already calling the callback!
this.command_error_callbacks.shift();
if (eb) eb(e);
}
} /* }}} */
// For commands like genmove, place_free_handicap ... :
// Send @cmd to engine and call @cb with returned moves.
// TODO: We may want to have a timeout here, in case bot crashes. Set it before this.command, clear it in the callback?
//
getMoves(cmd, state, cb, eb) { /* {{{ */
// Do this here so we only do it once, plus if there is a long delay between clock message and move message, we'll
// subtract that missing time from what we tell the bot.
//
this.loadClock(state);
// Only relevent with persistent bots. Leave the setting on until we actually have requested a move.
// Must be after loadClock() since loadClock() checks this.firstmove!
//
this.firstmove = false;
this.command(cmd, (line) => {
line = typeof(line) === "string" ? line.toLowerCase() : null;
let parts = line.split(/ +/);
let moves = [];
for (let i=0; i < parts.length; i++) {
let move = parts[i];
let resign = move === 'resign';
let pass = move === 'pass';
let x=-1, y=-1;
if (!resign && !pass) {
if (move && move[0]) {
x = gtpchar2num(move[0]);
y = state.width - parseInt(move.substr(1))
} else {
this.log(cmd + " failed, resigning");
resign = true;
}
}
moves.push({'x': x, 'y': y, 'text': move, 'resign': resign, 'pass': pass});
}
cb(moves);
},
eb,
true /* final command */
)
} /* }}} */
kill() { /* {{{ */
this.log("Stopping bot");
this.ignore = true; // Prevent race conditions / inconsistencies. Could be in the middle of genmove ...
this.dead = true;
this.command("quit");
if (this.proc) {
this.proc.kill();
setTimeout(() => {
// To be 100% sure.
if (config.DEBUG) this.log("Killing process directly with a signal");
this.proc.kill(9);
}, 5000);
}
} /* }}} */
sendMove(move, width, color){
if (config.DEBUG) this.log("Calling sendMove with", move2gtpvertex(move, width));
this.command("play " + color + " " + move2gtpvertex(move, width));
}
sendHandicapMoves(moves, width) { /* {{{ */
let cmd = "set_free_handicap";
for (let i = 0; i < moves.length; i++)
cmd += " " + move2gtpvertex(moves[i], width);
this.command(cmd);
} /* }}} */
// Called on game over, in case you need something special.
//
gameOver() {
}
}
function decodeMoves(move_obj, board_size) { /* {{{ */
let ret = [];
let width = board_size;
let height = board_size;
/*
if (DEBUG) {
console.log("Decoding ", move_obj);
}
*/
let decodeSingleMoveArray = (arr) => {
let obj = {
x : arr[0],
y : arr[1],
timedelta : arr.length > 2 ? arr[2] : -1,
color : arr.length > 3 ? arr[3] : 0,
}
let extra = arr.length > 4 ? arr[4] : {};
for (let k in extra) {
obj[k] = extra[k];
}
return obj;
}
if (move_obj instanceof Array) {
if (move_obj.length && typeof(move_obj[0]) === 'number') {
ret.push(decodeSingleMoveArray(move_obj));
}
else {
for (let i=0; i < move_obj.length; ++i) {
let mv = move_obj[i];
if (mv instanceof Array) {
ret.push(decodeSingleMoveArray(mv));
}
else {
throw new Error("Unrecognized move format: ", mv);
}
}
}
}
else if (typeof(move_obj) === "string") {
if (/[a-zA-Z][0-9]/.test(move_obj)) {
/* coordinate form, used from human input. */
let move_string = move_obj;
let moves = move_string.split(/([a-zA-Z][0-9]+|[.][.])/);
for (let i=0; i < moves.length; ++i) {
if (i%2) { /* even are the 'splits', which should always be blank unless there is an error */
let x = pretty_char2num(moves[i][0]);
let y = height-parseInt(moves[i].substring(1));
if ((width && x >= width) || x < 0) x = y= -1;
if ((height && y >= height) || y < 0) x = y = -1;
ret.push({"x": x, "y": y, "edited": false, "color": 0});
} else {
if (moves[i] !== "") {
throw "Unparsed move input: " + moves[i];
}
}
}
} else {
/* Pure letter encoded form, used for all records */
let move_string = move_obj;
for (let i=0; i < move_string.length-1; i += 2) {
let edited = false;
let color = 0;
if (move_string[i+0] === '!') {
edited = true;
color = parseInt(move_string[i+1]);
i += 2;
}
let x = char2num(move_string[i]);
let y = char2num(move_string[i+1]);
if (width && x >= width) x = y= -1;
if (height && y >= height) x = y = -1;
ret.push({"x": x, "y": y, "edited": edited, "color": color});
}
}
}
else {
throw new Error("Invalid move format: ", move_obj);
}
return ret;
} /* }}} */
function char2num(ch) { /* {{{ */
if (ch === ".") return -1;
return "abcdefghijklmnopqrstuvwxyz".indexOf(ch);
} /* }}} */
function pretty_char2num(ch) { /* {{{ */
if (ch === ".") return -1;
return "abcdefghjklmnopqrstuvwxyz".indexOf(ch.toLowerCase());
} /* }}} */
function move2gtpvertex(move, board_size) { /* {{{ */
if (move.x < 0) {
return "pass";
}
return num2gtpchar(move['x']) + (board_size-move['y'])
} /* }}} */
function gtpchar2num(ch) { /* {{{ */
if (ch === "." || !ch)
return -1;
return "abcdefghjklmnopqrstuvwxyz".indexOf(ch.toLowerCase());
} /* }}} */
function num2gtpchar(num) { /* {{{ */
if (num === -1)
return ".";
return "abcdefghjklmnopqrstuvwxyz"[num];
} /* }}} */
exports.Bot = Bot;
exports.decodeMoves = decodeMoves;
exports.move2gtpvertex = move2gtpvertex;