forked from tensorflow/minigo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.ts
More file actions
223 lines (196 loc) · 6.61 KB
/
Copy pathapp.ts
File metadata and controls
223 lines (196 loc) · 6.61 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
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import {Annotation, Position} from './position'
import {Socket} from './gtp_socket'
import {Color, Move, N, Nullable, movesEqual, setBoardSize, stonesEqual, toKgs} from './base'
import {Board} from './board'
import * as util from './util'
interface PositionJson {
id: string;
parentId?: string;
moveNum: number;
toPlay: string;
stones: string;
move?: string;
gameOver: boolean;
comment?: string;
caps?: number[];
}
interface PositionUpdateJson {
id: string;
n?: number;
q?: number;
pv?: string;
variations?: string[][];
search?: string[];
childN?: number[];
childQ?: number[];
}
// App base class used by the different Minigui UI implementations.
abstract class App {
// WebSocket connection to the Miniui backend server.
protected gtp = new Socket();
// True if Minigo is processing a genmove command.
protected engineBusy = false;
// The root of the game tree, an empty board.
protected rootPosition: Position;
// The currently active position.
protected activePosition: Position;
protected positionMap = new Map<string, Position>();
protected abstract onGameOver(): void;
protected abstract onPositionUpdate(position: Position, update: Position.Update): void;
protected abstract onNewPosition(position: Position): void;
constructor() {
this.gtp.onData('mg-update', (j: PositionUpdateJson) => {
let position = this.positionMap.get(j.id);
if (position === undefined) {
// Just after refreshing the page, the backend will still be sending
// position updates if pondering is enabled. Ignore updates for any
// positions we don't know about.
return;
}
let update = this.newPositionUpdate(j);
position.update(update);
this.onPositionUpdate(position, update);
});
this.gtp.onData('mg-position', (j: PositionJson | PositionUpdateJson) => {
let position = this.newPosition(j as PositionJson);
let update = this.newPositionUpdate(j);
position.update(update);
this.onNewPosition(position);
if (position.gameOver) {
this.onGameOver();
}
});
}
private newPosition(j: PositionJson): Position {
let position = this.positionMap.get(j.id);
if (position !== undefined) {
return position;
}
for (let prop of ['stones', 'toPlay', 'gameOver']) {
if (!j.hasOwnProperty(prop)) {
throw new Error(`missing required property: ${prop}`);
}
}
let def = j as PositionJson;
let stones: Color[] = [];
const stoneMap: {[index: string]: Color} = {
'.': Color.Empty,
'X': Color.Black,
'O': Color.White,
};
for (let i = 0; i < def.stones.length; ++i) {
stones.push(stoneMap[def.stones[i]]);
}
let toPlay = util.parseColor(def.toPlay);
let gameOver = def.gameOver;
if (def.parentId === undefined) {
// No parent, this must be the root.
if (def.move != null) {
throw new Error('move mustn\'t be set for root position');
}
position = this.rootPosition;
position.id = def.id;
} else {
// The position has a parent, which must exist in the positionMap.
let parent = this.positionMap.get(def.parentId);
if (parent === undefined) {
throw new Error(
`Can't find parent with id ${def.parentId} for position ${def.id}`);
}
if (def.move == null) {
throw new Error('new positions must specify move');
}
let move = util.parseMove(def.move);
position = parent.addChild(def.id, move, stones, gameOver);
}
if (j.comment) {
position.comment = j.comment;
}
if (j.caps !== undefined) {
position.captures[0] = j.caps[0];
position.captures[1] = j.caps[1];
}
if (position.toPlay != toPlay) {
throw new Error(`expected ${position.toPlay}, got ${toPlay}`);
}
this.positionMap.set(position.id, position);
return position;
}
private newPositionUpdate(j: PositionUpdateJson): Position.Update {
let update: Position.Update = {}
if (j.n != null) { update.n = j.n; }
if (j.q != null) { update.q = j.q; }
if (j.variations != null) {
update.variations = {};
for (let key in j.variations) {
if (key == null) {
continue;
}
update.variations[key] = util.parseMoves(j.variations[key]);
}
}
if (j.childN != null) {
update.childN = j.childN;
}
if (j.childQ != null) {
update.childQ = [];
for (let q of j.childQ) {
update.childQ.push(q / 1000);
}
}
return update;
}
protected 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 this.gtp.connect(uri, debug).then((size: number) => {
// setBoardSize sets the global variable N to the board size for the game
// (as provided by the backend engine). The code uses N from hereon in.
setBoardSize(size);
let stones = new Array<Color>(N * N);
stones.fill(Color.Empty);
this.rootPosition = new Position(
'dummy-root', null, stones, null, Color.Black, false, true);
this.activePosition = this.rootPosition;
});
}
protected newGame() {
this.positionMap.clear();
this.rootPosition.children = [];
this.activePosition = this.rootPosition;
this.gtp.send('clear_board');
this.gtp.send('info');
// Iterate over the data-* attributes attached to the main minigui container
// element, looking for data-gtp-* attributes. Send any matching ones as GTP
// commands to the backend.
let containerElem = document.querySelector('.minigui') as HTMLElement;
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}`);
}
}
}
}
}
export {
App,
}