-
-
Notifications
You must be signed in to change notification settings - Fork 412
Expand file tree
/
Copy pathenginesyncer.js
More file actions
415 lines (338 loc) · 10.8 KB
/
Copy pathenginesyncer.js
File metadata and controls
415 lines (338 loc) · 10.8 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
import * as remote from '@electron/remote'
import EventEmitter from 'events'
import {existsSync} from 'fs'
import {dirname, resolve} from 'path'
import argvsplit from 'argv-split'
import {v4 as uuid} from 'uuid'
import {fromDimensions as newBoard} from '@sabaki/go-board'
import {Controller, ControllerStateTracker, Command} from '@sabaki/gtp'
import {parseCompressedVertices} from '@sabaki/sgf'
import i18n from '../i18n.js'
import {getBoard, getRootProperty} from './gametree.js'
import {noop, equals} from './helper.js'
const t = i18n.context('EngineSyncer')
const setting = remote.require('./setting')
const alpha = 'ABCDEFGHJKLMNOPQRSTUVWXYZ'
const quitTimeout = setting.get('gtp.engine_quit_timeout')
function parseVertex(coord, size) {
if (coord == null || coord === 'resign') return null
if (coord === 'pass') return [-1, -1]
let x = alpha.indexOf(coord[0].toUpperCase())
let y = size - +coord.slice(1)
return [x, y]
}
function parseAnalysis(line, board) {
return line
.split(/\s*info\s+/)
.slice(1)
.map(x => x.trim())
.map(x => {
let matchPV = x.match(/(pass|[A-Za-z]\d+)(\s+(pass|[A-Za-z]\d+))*$/)
if (matchPV == null) return null
let passIndex = matchPV[0].indexOf('pass')
if (passIndex < 0) passIndex = Infinity
return [
x
.slice(0, matchPV.index)
.trim()
.split(/\s+/)
.slice(0, -1),
matchPV[0]
.slice(0, passIndex)
.split(/\s+/)
.filter(x => x.length >= 2)
]
})
.filter(x => x != null)
.map(([tokens, pv]) => {
let keys = tokens.filter((_, i) => i % 2 === 0)
let values = tokens.filter((_, i) => i % 2 === 1)
keys.push('pv')
values.push(pv)
return keys.reduce((acc, x, i) => ((acc[x] = values[i]), acc), {})
})
.filter(({move}) => move.match(/^[A-Za-z]\d+$/))
.map(({move, visits, winrate, scoreLead, pv}) => ({
vertex: board.parseVertex(move),
visits: +visits,
winrate: winrate.includes('.') ? +winrate * 100 : +winrate / 100,
scoreLead: scoreLead != null ? +scoreLead : null,
moves: pv.map(x => board.parseVertex(x))
}))
}
export default class EngineSyncer extends EventEmitter {
constructor(engine) {
super()
let {path, args, commands} = engine
this._busy = false
this._suspended = true
this._analysis = null
this.id = uuid()
this.engine = engine
this.commands = []
this.treePosition = null
let absolutePath = resolve(path)
let executePath = existsSync(absolutePath) ? absolutePath : path
this.controller = new Controller(executePath, [...argvsplit(args)], {
cwd: dirname(absolutePath)
})
this.stateTracker = new ControllerStateTracker(this.controller)
this.controller.on('started', () => {
this.treePosition = null
this.analysis = null
Promise.all([
this.controller.sendCommand({name: 'name'}),
this.controller.sendCommand({name: 'version'}),
this.controller.sendCommand({name: 'protocol_version'}),
this.controller.sendCommand({name: 'list_commands'}).then(response => {
this.commands = response.content.split('\n')
}),
...(commands != null && commands.trim() !== ''
? commands
.split(';')
.filter(x => x.trim() !== '')
.map(command =>
this.controller.sendCommand(Command.fromString(command))
)
: [])
]).catch(noop)
})
this.controller.on('stopped', () => {
this.treePosition = null
this.analysis = null
})
this.controller.on(
'command-sent',
async ({command, subscribe, getResponse}) => {
if (
command.name.match(/^(lz-)?(genmove_)?analyze$/) != null ||
command.args.length > 0
) {
// Handle analysis commands
let sign = command.args[0].toUpperCase() === 'W' ? -1 : 1
let boardsize = this.stateTracker.state.boardsize || [19, 19]
let board = newBoard(...boardsize)
subscribe(({line}) => {
// Parse analysis info
if (line.startsWith('info ')) {
let variations = parseAnalysis(line, board)
this.analysis = {
sign,
variations,
winrate: Math.max(...variations.map(({winrate}) => winrate)),
scoreLead: Math.max(
...variations.map(({scoreLead}) => scoreLead)
)
}
} else if (line.startsWith('play ')) {
sign = -sign
this.analysis = null
this.treePosition = null
}
})
} else if (this.treePosition != null) {
// Invalidate treePosition
let prevHistory = JSON.parse(
JSON.stringify(this.stateTracker.state.history)
)
await getResponse()
if (!equals(prevHistory, this.stateTracker.state.history)) {
this.treePosition = null
this.analysis = null
}
}
}
)
// Sync properties
for (let eventName of [
'started',
'stopped',
'command-sent',
'response-received'
]) {
this.controller.on(eventName, () => {
this.busy = this.controller.busy
this.suspended = this.controller.process == null
})
}
}
get state() {
return this.stateTracker.state
}
get busy() {
return this._busy
}
set busy(value) {
if (value !== this._busy) {
this._busy = value
this.emit('busy-changed')
}
}
get suspended() {
return this._suspended
}
set suspended(value) {
if (value !== this._suspended) {
this._suspended = value
this.emit('suspended-changed')
}
}
get analysis() {
return this._analysis
}
set analysis(value) {
if (value !== this._analysis) {
this._analysis = value
this.emit('analysis-update')
}
}
start() {
this.controller.start()
}
async stop() {
await this.controller.stop(quitTimeout)
}
async sendAbort() {
if (this.controller.busy) {
await this.controller.sendCommand({name: 'protocol_version'})
}
}
async queueCommand(...args) {
this.sendAbort()
return await this.stateTracker.queueCommand(...args)
}
async sync(tree, id) {
this.sendAbort()
let board = getBoard(tree, id)
if (!board.isValid()) {
throw new Error(t('GTP engines don’t support invalid board positions.'))
} else if (Math.max(board.width, board.height) > alpha.length) {
throw new Error(
t(
p =>
`GTP engines only support board sizes that don’t exceed ${p.length}.`,
{
length: alpha.length
}
)
)
}
let komi = +getRootProperty(tree, 'KM', 0)
let boardsize = [board.width, board.height]
// Replay
let nodeBoard = getBoard(tree, id)
let engineBoard = newBoard(board.width, board.height)
let history = []
let boardSynced = true
let nodes = [...tree.listNodesVertically(id, -1, {})].reverse()
for (let node of nodes) {
let placedHandicapStones = false
if (
node.data.AB &&
node.data.AB.length >= 2 &&
engineBoard.isEmpty() &&
(await this.stateTracker.knowsCommand('set_free_handicap'))
) {
// Place handicap stones
let vertices = []
.concat(...node.data.AB.map(parseCompressedVertices))
.sort()
let coords = vertices
.map(v => board.stringifyVertex(v))
.filter(x => x != null)
.filter((x, i, arr) => i === 0 || x !== arr[i - 1])
if (coords.length > 0) {
history.push({name: 'set_free_handicap', args: coords})
for (let vertex of vertices) {
if (engineBoard.get(vertex) !== 0) continue
engineBoard = engineBoard.makeMove(1, vertex)
}
placedHandicapStones = true
}
}
for (let prop of ['B', 'W', 'AB', 'AW']) {
if (node.data[prop] == null || (placedHandicapStones && prop === 'AB'))
continue
let color = prop.slice(-1)
let sign = color === 'B' ? 1 : -1
let vertices = [].concat(
...node.data[prop].map(parseCompressedVertices)
)
for (let vertex of vertices) {
if (engineBoard.has(vertex) && engineBoard.get(vertex) !== 0) continue
let coord = !engineBoard.has(vertex)
? 'pass'
: board.stringifyVertex(vertex)
history.push({name: 'play', args: [color, coord]})
engineBoard = engineBoard.makeMove(sign, vertex)
}
}
}
if (!equals(engineBoard.signMap, nodeBoard.signMap)) {
boardSynced = false
}
// Incremental rearrangement
if (!boardSynced) {
history = [...this.state.history]
engineBoard = newBoard(board.width, board.height)
for (let command of this.state.history) {
if (command.name === 'play') {
let [color, coord] = command.args
let sign = color.toUpperCase() === 'B' ? 1 : -1
engineBoard = engineBoard.makeMove(
sign,
parseVertex(coord, boardsize)
)
} else if (command.name === 'set_free_handicap') {
for (let coord of command.args) {
engineBoard = engineBoard.makeMove(1, parseVertex(coord, boardsize))
}
}
}
let diff = engineBoard.diff(board).filter(v => board.get(v) !== 0)
for (let vertex of diff) {
let sign = board.get(vertex)
let color = sign > 0 ? 'B' : 'W'
let coord = board.stringifyVertex(vertex)
history.push({name: 'play', args: [color, coord]})
engineBoard = engineBoard.makeMove(sign, vertex)
}
if (equals(engineBoard.signMap, board.signMap)) {
boardSynced = true
}
}
// Complete rearrangement
if (!boardSynced) {
history = []
engineBoard = newBoard(board.width, board.height)
for (let x = 0; x < board.width; x++) {
for (let y = 0; y < board.height; y++) {
let vertex = [x, y]
let sign = board.get(vertex)
let color = sign > 0 ? 'B' : 'W'
if (sign === 0) continue
history.push({
name: 'play',
args: [color, board.stringifyVertex(vertex)]
})
engineBoard = engineBoard.makeMove(sign, vertex)
}
}
if (equals(engineBoard.signMap, board.signMap)) {
boardSynced = true
}
}
if (!boardSynced) {
throw new Error(
t('Current board arrangement can’t be recreated on the GTP engine.')
)
}
try {
await this.stateTracker.sync({komi, boardsize, history})
} catch (err) {
throw new Error(t('GTP engine can’t be synced to current state.'))
}
this.treePosition = id
this.analysis = null
}
}