forked from rooklift/nibbler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
20_utils.js
371 lines (280 loc) · 7.21 KB
/
20_utils.js
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
"use strict";
// At some point I tried caching the results of XY() and S()
// but for XY(), object lookups were slower than calculating,
// and for S(), it just isn't called enough to matter.
function XY(s) { // e.g. "b7" --> [1, 1]
if (typeof s !== "string" || s.length !== 2) {
return [-1, -1];
}
s = s.toLowerCase();
let x = s.charCodeAt(0) - 97;
let y = 8 - parseInt(s[1], 10);
if (x < 0 || x > 7 || y < 0 || y > 7 || Number.isNaN(y)) {
return [-1, -1];
}
return [x, y];
}
function S(x, y) { // e.g. (1, 1) --> "b7"
if (typeof x !== "number" || typeof y !== "number" || x < 0 || x > 7 || y < 0 || y > 7) {
return "??";
}
let xs = String.fromCharCode(x + 97);
let ys = String.fromCharCode((8 - y) + 48);
return xs + ys;
}
function InfoVal(s, key) {
// Given some string like "info depth 8 seldepth 22 time 469 nodes 3918 score cp 46 hashfull 13 nps 8353 tbhits 0 multipv 1 pv d2d4 g8f6"
// pull the value for the key out, e.g. in this example, key "nps" returns "8353" (as a string).
//
// Since Lc0's info strings often have the value ending in ")", we strip that out.
if (typeof s !== "string" || typeof key !== "string") {
return "";
}
let tokens = s.split(" ").filter(s => s !== "");
for (let i = 0; i < tokens.length - 1; i++) {
if (tokens[i] === key) {
if (tokens[i + 1].endsWith(")")) {
return tokens[i + 1].slice(0, -1);
} else {
return tokens[i + 1];
}
}
}
return "";
}
function InfoPV(s) {
// Pull the PV out, assuming it's at the end of the string.
if (typeof s !== "string") {
return [];
}
let tokens = s.split(" ").filter(s => s !== "");
for (let i = 0; i < tokens.length - 1; i++) {
if (tokens[i] === "pv") {
return tokens.slice(i + 1);
}
}
return [];
}
function InfoWDL(s) {
// Pull the WDL out as a string.
if (typeof s !== "string") {
return "??";
}
let tokens = s.split(" ").filter(s => s !== "");
for (let i = 0; i < tokens.length - 1; i++) {
if (tokens[i] === "wdl") {
return tokens.slice(i + 1, i + 4).join(" ");
}
}
return "??";
}
function CompareArrays(a, b) {
if (Array.isArray(a) === false || Array.isArray(b) === false) {
return false;
}
if (a.length !== b.length) {
return false;
}
for (let n = 0; n < a.length; n++) {
if (a[n] !== b[n]) {
return false;
}
}
return true;
}
function ArrayStartsWith(a, b) { // where b is itself an array
if (Array.isArray(a) === false || Array.isArray(b) === false) {
return false;
}
if (a.length < b.length) {
return false;
}
for (let n = 0; n < b.length; n++) {
if (a[n] !== b[n]) {
return false;
}
}
return true;
}
function OppositeColour(s) {
if (s === "w" || s === "W") return "b";
if (s === "b" || s === "B") return "w";
return "";
}
function ReplaceAll(s, search, replace) { // Fairly slow.
return s.split(search).join(replace);
}
function SafeString(s) {
if (typeof s !== "string") {
return undefined;
}
s = ReplaceAll(s, "&", "&"); // This needs to be first of course.
s = ReplaceAll(s, "<", "<");
s = ReplaceAll(s, ">", ">");
s = ReplaceAll(s, "'", "'");
s = ReplaceAll(s, "\"", """);
return s;
}
function Log(s) {
if (typeof config.logfile !== "string" || config.logfile === "") {
return;
}
// Log.logfilename - name of currently open log file (undefined if none)
// Log.stream - actual write stream
if (Log.logfilename !== config.logfile) {
if (Log.logfilename) {
console.log(`Closing ${Log.logfilename}`);
Log.stream.end();
}
console.log(`Opening ${config.logfile}`);
Log.logfilename = config.logfile;
Log.stream = fs.createWriteStream(config.logfile, {flags: "a"});
}
Log.stream.write(s + "\n");
}
function New2DArray(width, height) {
let ret = [];
for (let x = 0; x < width; x++) {
ret.push([]);
for (let y = 0; y < height; y++) {
ret[x].push(null);
}
}
return ret;
}
function CanvasCoords(x, y) {
// Given the x, y coordinates on the board (a8 is 0, 0)
// return an object with the canvas coordinates for
// the square, and also the centre.
//
// x1,y1--------
// | |
// | cx,cy |
// | |
// --------x2,y2
let css = config.square_size;
let x1 = x * css;
let y1 = y * css;
let x2 = x1 + css;
let y2 = y1 + css;
if (config.flip) {
[x1, x2] = [(css * 8) - x2, (css * 8) - x1];
[y1, y2] = [(css * 8) - y2, (css * 8) - y1];
}
let cx = x1 + css / 2;
let cy = y1 + css / 2;
return {x1, y1, x2, y2, cx, cy};
}
function EventPathString(event, prefix) {
// Given an event with event.path like ["foo", "bar", "searchmove_e2e4", "whatever"]
// return the string "e2e4", assuming the prefix matches. Else return null.
if (!event || typeof prefix !== "string") {
return null;
}
let path = event.path || (event.composedPath && event.composedPath());
if (path) {
for (let item of path) {
if (typeof item.id === "string") {
if (item.id.startsWith(prefix)) {
return item.id.slice(prefix.length);
}
}
}
}
return null;
}
function EventPathN(event, prefix) {
// As above, but returning a number, or null.
let s = EventPathString(event, prefix);
if (typeof s !== "string") {
return null;
}
let n = parseInt(s, 10);
if (Number.isNaN(n)) {
return null;
}
return n;
}
function SwapElements(obj1, obj2) {
// https://stackoverflow.com/questions/10716986/swap-2-html-elements-and-preserve-event-listeners-on-them
var temp = document.createElement("div");
obj1.parentNode.insertBefore(temp, obj1);
obj2.parentNode.insertBefore(obj1, obj2);
temp.parentNode.insertBefore(obj2, temp);
temp.parentNode.removeChild(temp);
}
function NString(n) {
if (typeof n !== "number") {
return "?";
}
if (n < 1000) {
return n.toString();
}
if (n < 100000) {
return (n / 1000).toFixed(1) + "k";
}
if (n < 1000000) {
return (n / 1000).toFixed(0) + "k";
}
if (n < 100000000) {
return (n / 1000000).toFixed(1) + "M";
}
return (n / 1000000).toFixed(0) + "M";
}
function DateString(dt) {
let y = dt.getFullYear();
let m = dt.getMonth() + 1;
let d = dt.getDate();
let parts = [
y.toString(),
(m > 9 ? "" : "0") + m.toString(),
(d > 9 ? "" : "0") + d.toString(),
];
return parts.join(".");
}
function QfromPawns(pawns) {
// Note carefully: the arg is pawns not centipawns.
if (typeof (pawns) !== "number") {
return 0.5;
}
let winrate = 1 / (1 + Math.pow(10, -pawns / 4));
return winrate * 2 - 1;
}
function Sign(n) {
if (n < 0) return -1;
if (n > 0) return 1;
return 0;
}
function DurationString(ms) {
let hours = Math.floor(ms / 3600000);
ms -= hours * 3600000;
let minutes = Math.floor(ms / 60000);
ms -= minutes * 60000;
let seconds = Math.floor(ms / 1000);
if (hours > 0) {
return `${hours}h ${minutes}m`;
}
if (minutes > 0) {
return `${minutes}m ${seconds}s`;
}
return `${seconds}s`;
}
function NumbersBetween(a, b) {
// Given integers a and b, return a list of integers between the two, inclusive.
let add = a < b ? 1 : -1;
let ret = [];
for (let x = a; x !== b; x += add) {
ret.push(x);
}
ret.push(b);
return ret;
}
function RandInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
function RandChoice(arr) {
if (Array.isArray(arr) === false || arr.length === 0) {
return undefined;
}
return arr[RandInt(0, arr.length)];
}