forked from tensorflow/minigo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
98 lines (98 loc) · 3.95 KB
/
Copy pathapp.js
File metadata and controls
98 lines (98 loc) · 3.95 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
define(["require", "exports", "./position", "./gtp_socket", "./base", "./util"], function (require, exports, position_1, gtp_socket_1, base_1, util) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class App {
constructor() {
this.gtp = new gtp_socket_1.Socket();
this.engineBusy = false;
this.positionMap = new Map();
this.gameOver = false;
this.gtp.onData('mg-update', (j) => {
let position = this.positionMap.get(j.id);
if (position === undefined) {
return;
}
position.update(j);
this.onPositionUpdate(position, j);
});
this.gtp.onData('mg-position', (j) => {
let position;
let def = j;
if (def.move == null) {
let p = this.positionMap.get(def.id);
if (p == null) {
p = new position_1.Position(def);
this.rootPosition = p;
}
position = this.rootPosition;
}
else {
if (def.parentId === undefined) {
throw new Error('child node must have a valid parentId');
}
let parent = this.positionMap.get(def.parentId);
if (parent == null) {
throw new Error(`couldn't find parent ${def.parentId}`);
}
let child = parent.getChild(util.parseMove(def.move));
if (child != null) {
position = child;
}
else {
position = new position_1.Position(def);
parent.addChild(position);
}
}
position.update(j);
this.onNewPosition(position);
this.positionMap.set(position.id, position);
});
}
onGameOver() {
this.gameOver = true;
}
connect() {
let uri = `http://${document.domain}:${location.port}/minigui`;
let params = new URLSearchParams(window.location.search);
let p = params.get("gtp_debug");
let debug = (p != null) && (p == "" || p == "1" || p.toLowerCase() == "true");
return fetch('config').then((response) => {
return response.json();
}).then((cfg) => {
base_1.setBoardSize(cfg.boardSize);
let stones = new Array(base_1.N * base_1.N);
stones.fill(base_1.Color.Empty);
this.rootPosition = new position_1.Position({
id: 'dummy-root',
moveNum: 0,
toPlay: 'b',
});
this.activePosition = this.rootPosition;
if (cfg.players.length != 1) {
throw new Error(`expected 1 player, got ${cfg.players}`);
}
return this.gtp.connect(uri, cfg.players[0], debug);
});
}
newGame() {
this.gameOver = false;
this.positionMap.clear();
this.rootPosition.children = [];
this.activePosition = this.rootPosition;
let containerElem = document.querySelector('.minigui');
if (containerElem != null) {
let dataset = containerElem.dataset;
for (let key in dataset) {
if (key.startsWith('gtp')) {
let cmd = key.substr(3).toLowerCase();
let args = dataset[key];
this.gtp.send(`${cmd} ${args}`);
}
}
}
return this.gtp.send('clear_board');
}
}
exports.App = App;
});
//# sourceMappingURL=app.js.map