forked from wonderingabout/gtp2ogs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
573 lines (516 loc) · 24.3 KB
/
bot.js
File metadata and controls
573 lines (516 loc) · 24.3 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
// vim: tw=120 softtabstop=4 shiftwidth=4
const child_process = require('child_process');
const split2 = require('split2');
const { decodeMoves } = require("./gtp/decodeMoves");
const { gtpchar2num } = require("./gtp/gtpchar2num");
const { move2gtpvertex } = require("./gtp/move2gtpvertex");
const { console } = require('./console');
const config = require('./config');
const { Pv } = require('./pv');
const gtpCommandEndRegex = new RegExp("(\\r?\\n){2}$");
const gtpCommandSplitRegex = new RegExp("(\\r?\\n){2}");
/*********/
/** 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;
if (config.ogspv) this.pv = new Pv(config.ogspv, game);
if (config.DEBUG) this.log("Starting ", cmd.join(' '));
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;
}
this.proc.stderr.pipe(split2()).on('data', (data) => {
if (this.ignore) return;
const errline = data.toString().trim();
if (errline === "") return;
this.error(`stderr: ${errline}`);
if (config.ogspv) this.pv.postPvToChat(errline);
if (config.aichat) {
const chat_match = /(DISCUSSION|MALKOVICH):(.*)/.exec(errline)
if (chat_match) {
this.game.sendChat(chat_match[2], this.game.state.moves.length + 1, chat_match[1].toLowerCase())
}
}
});
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 || !(gtpCommandEndRegex.test(stdout_buffer))) {
//this.log("Partial result received, buffering until the output ends with a newline");
return;
}
if (config.DEBUG) {
this.log("<<<", stdout_buffer.trim());
}
const lines = stdout_buffer.split(gtpCommandSplitRegex);
stdout_buffer = "";
for (let i = 0; i < lines.length; ++i) {
const line = lines[i];
if (line.trim() === "") {
continue;
}
if (line[0] === '=') {
while (lines[i].trim() !== "") {
++i;
}
const 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();
const 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();
const 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;
const 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;
const eb = this.command_error_callbacks.shift();
if (eb) eb(code);
});
}
pid() {
if (this.proc) {
return this.proc.pid;
} else {
return -1;
}
}
log() {
const arr = [`[${this.pid()}]`];
for (let i = 0; i < arguments.length; ++i) {
arr.push(arguments[i]);
}
console.log.apply(null, arr);
}
error() {
const arr = [`[${this.pid()}]`];
for (let i = 0; i < arguments.length; ++i) {
arr.push(arguments[i]);
}
console.error.apply(null, arr);
}
verbose() {
const 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.
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;
// clock_drift compensates for difference between server and client time, and latency.
let now = Date.now() - this.conn.clock_drift;
let black_offset = 0;
let white_offset = 0;
// offset indicates how long we've had since last move. Ogs only communicates how much
// time the player had when last move was made.
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') {
let black_time = state.clock.black_time.thinking_time;
let white_time = state.clock.white_time.thinking_time;
let black_periods = state.clock.black_time.periods;
let white_periods = state.clock.white_time.periods;
let black_timeleft = 0;
let white_timeleft = 0;
if (this.kgstime) {
if (black_time > 0) {
black_timeleft = black_time - black_offset;
} else {
black_timeleft = state.time_control.period_time - black_offset;
}
if (white_time > 0) {
white_timeleft = white_time - white_offset;
} else {
white_timeleft = state.time_control.period_time - white_offset;
}
// If we're so slow that time left - offset is negative, we need to roll over to period time.
while (black_timeleft < 0 && black_periods > 1) {
black_timeleft += state.clock.black_time.period_time;
if (black_time > 0) {
black_time = 0;
} else {
black_periods--;
}
}
while (white_timeleft < 0 && white_periods > 1) {
white_timeleft += state.clock.white_time.period_time;
if (white_time > 0) {
white_time = 0;
} else {
white_periods--;
}
}
this.command(`kgs-time_settings byoyomi ${state.time_control.main_time} ${state.time_control.period_time} ${state.time_control.periods}`);
this.command(`time_left black ${Math.floor(Math.max(black_timeleft, 0))} ${black_time > 0 ? "0" : black_periods}`);
this.command(`time_left white ${Math.floor(Math.max(white_timeleft, 0))} ${white_time > 0 ? "0" : white_periods}`);
} else {
/* Gtp does not support Japanese Byoyomi. We fake it as Canadian Byoyomi.
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.
*/
// add all periods to the main time.
// If we're already in overtime, exclude the current period.
if (black_time > 0) {
black_timeleft = black_time - black_offset + state.clock.black_time.period_time * black_periods;
} else {
black_timeleft = state.time_control.period_time - black_offset + state.clock.black_time.period_time * (black_periods - 1);
}
if (white_time > 0) {
white_timeleft = white_time - white_offset + state.clock.black_time.period_time * black_periods;
} else {
white_timeleft = state.time_control.period_time - white_offset + state.clock.black_time.period_time * (black_periods - 1);
}
this.command(`time_settings ${state.time_control.main_time + (state.time_control.periods - 1) * state.time_control.period_time} ${state.time_control.period_time} 1`);
// If we're in the last period, tell the bot. Otherwise pretend we're in main time.
if (black_timeleft <= state.clock.black_time.period_time) {
this.command(`time_left black ${Math.floor(Math.max(black_timeleft, 0))} 1`);
} else {
this.command(`time_left black ${Math.floor(black_timeleft - state.clock.black_time.period_time)} 0`);
}
if (white_timeleft <= state.clock.white_time.period_time) {
this.command(`time_left white ${Math.floor(Math.max(white_timeleft, 0))} 1`);
} else {
this.command(`time_left white ${Math.floor(white_timeleft - state.clock.white_time.period_time)} 0`);
}
}
} else if (state.time_control.system === 'canadian') {
/* Canadian Byoyomi is the only time controls GTP v2 officially supports.
*/
let black_timeleft = state.clock.black_time.thinking_time - black_offset;
let white_timeleft = state.clock.white_time.thinking_time - white_offset;
let black_stones = 0;
let white_stones = 0;
if (black_timeleft <= 0) {
black_stones = state.clock.black_time.moves_left;
black_timeleft += state.clock.black_time.block_time;
}
if (white_timeleft <= 0) {
white_stones = state.clock.white_time.moves_left;
white_timeleft += state.clock.white_time.block_time;
}
if (this.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 ${Math.floor(Math.max(black_timeleft, 0))} ${black_stones}`);
this.command(`time_left white ${Math.floor(Math.max(white_timeleft, 0))} ${white_stones}`);
} else if (state.time_control.system === 'fischer') {
if (this.katafischer) {
let black_timeleft = state.clock.black_time.thinking_time - black_offset;
let white_timeleft = state.clock.white_time.thinking_time - white_offset;
this.command(`kata-time_settings fischer-capped ${state.time_control.initial_time} ${state.time_control.time_increment} ${state.time_control.max_time} -1`);
this.command(`time_left black ${Math.floor(Math.max(black_timeleft, 0))} 0`);
this.command(`time_left white ${Math.floor(Math.max(white_timeleft, 0))} 0`);
} else {
/* 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 = state.clock.black_time.thinking_time - black_offset - state.time_control.time_increment;
let white_timeleft = state.clock.white_time.thinking_time - white_offset - state.time_control.time_increment;
let black_periods = 0;
let white_periods = 0;
if (this.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`);
}
if (black_timeleft <= 0) {
black_periods = 1;
black_timeleft += state.time_control.time_increment;
}
if (white_timeleft <= 0) {
white_periods = 1;
white_timeleft += state.time_control.time_increment;
}
/* 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 ${Math.floor(Math.max(black_timeleft, 0))} ${black_periods}`);
this.command(`time_left white ${Math.floor(Math.max(white_timeleft, 0))} ${white_periods}`);
}
} else if (state.time_control.system === 'simple') {
/* Simple could also be viewed as a Canadian Byoyomi that starts
immediately with # of stones = 1
*/
// for some reason ogs sends a timestamp (equal to state.clock.last_move) instead of our time.
// Luckely we can use state.time_control.per_move since simple time is always in overtime.
let black_timeleft = state.time_control.per_move - black_offset;
let white_timeleft = state.time_control.per_move - white_offset;
this.command(`time_settings 0 ${state.time_control.per_move} 1`);
this.command(`time_left black ${Math.floor(Math.max(black_timeleft, 0))} 1`);
this.command(`time_left white ${Math.floor(Math.max(white_timeleft, 0))} 1`);
} else if (state.time_control.system === 'absolute') {
let black_timeleft = state.clock.black_time.thinking_time - black_offset;
let white_timeleft = state.clock.white_time.thinking_time - white_offset;
this.command(`time_settings ${state.time_control.total_time} 0 0`);
this.command(`time_left black ${Math.floor(Math.max(black_timeleft, 0))} 0`);
this.command(`time_left white ${Math.floor(Math.max(white_timeleft, 0))} 0`);
}
/* OGS doesn't actually send 'none' time control type
else if (state.time_control.system === 'none') {
if (this.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;
}
this.command("list_commands", (commands) => {
this.kgstime = commands.includes("kgs-time_settings");
this.katatime = commands.includes("kata-list_time_settings");
if (this.katatime) {
this.command("kata-list_time_settings", (kataTimeSettings) => {
this.katafischer = kataTimeSettings.includes("fischer-capped")
this.loadState2(state, cb, eb);
}, eb);
} else {
this.katafischer = false;
this.loadState2(state, cb, eb);
}
}, eb);
}
loadState2(state, cb, eb) {
if (state.width === state.height) {
this.command(`boardsize ${state.width}`, () => { }, eb);
} else {
this.command(`boardsize ${state.width} ${state.height}`, () => { }, 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) {
const black = decodeMoves(state.initial_state.black, state.width, state.height);
const white = decodeMoves(state.initial_state.white, state.width, state.height);
have_initial_state = (black.length || white.length);
for (let i = 0; i < black.length; ++i)
this.command(`play black ${move2gtpvertex(black[i], state.width, state.height)}`, () => { }, eb);
for (let i = 0; i < white.length; ++i)
this.command(`play white ${move2gtpvertex(white[i], state.width, state.height)}`, () => { }, eb);
}
// Replay moves made
let color = state.initial_player;
const doing_handicap = (!have_initial_state && state.free_handicap_placement && state.handicap > 1);
const handicap_moves = [];
const moves = decodeMoves(state.moves, state.width, state.height);
for (let i = 0; i < moves.length; ++i) {
const move = moves[i];
// 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, state.height);
else continue; // don't switch color.
} else {
this.command(`play ${color} ${move2gtpvertex(move, state.width, state.height)}`)
}
color = (color === 'black' ? 'white' : 'black');
}
if (config.showboard) {
this.command("showboard", cb, eb);
} else {
cb();
}
}
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;
const parts = line.split(/ +/);
const moves = [];
for (let i = 0; i < parts.length; i++) {
const move = parts[i];
let resign = move === 'resign';
const pass = move === 'pass';
let x = -1,
y = -1;
if (!resign && !pass) {
if (move && move[0]) {
x = gtpchar2num(move[0]);
y = state.height - 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 ...
// "quit" needs to be sent before we toggle this.dead since command() checks the status of this.dead
this.command("quit");
this.dead = true;
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, height, color) {
if (config.DEBUG) this.log("Calling sendMove with", move2gtpvertex(move, width, height));
this.command(`play ${color} ${move2gtpvertex(move, width, height)}`);
}
sendHandicapMoves(moves, width, height) {
let cmd = "set_free_handicap";
for (let i = 0; i < moves.length; i++)
cmd += ` ${move2gtpvertex(moves[i], width, height)}`;
this.command(cmd);
}
// Called on game over, in case you need something special.
//
gameOver() {
}
}
exports.Bot = Bot;