forked from rooklift/nibbler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
51_winrate.js
275 lines (202 loc) · 6.17 KB
/
51_winrate.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
"use strict";
function NewGrapher() {
let grapher = Object.create(null);
grapher.last_drawn_board = null;
grapher.last_draw_time = -10000;
grapher.last_position_marker_x = null;
grapher.draws = 0;
grapher.skips = 0;
grapher.clear_graph = function() {
let boundingrect = graph.getBoundingClientRect();
let width = boundingrect.right - boundingrect.left;
let height = boundingrect.bottom - boundingrect.top;
// This clears the canvas...
graph.width = width;
graph.height = height;
};
grapher.imaginary_length = function(real_length) {
// What length we'll pretend the eval list is for the sake of aesthetics.
// Maybe we should make this the max of [current variation line length] and [main line length]?
if (real_length < 48) {
return 48;
} else {
return real_length;
}
};
grapher.draw = function(node, force) {
if (config.graph_height <= 0) {
return;
} else if (force || performance.now() - this.last_draw_time > 1000) {
this.draw_everything(node);
this.draws++;
} else {
this.clear_position_line();
this.draw_position_line(node.future_eval_history().length, node);
this.skips++;
}
};
grapher.draw_everything = function(node) {
this.clear_graph();
let width = graph.width; // After the above.
let height = graph.height;
this.draw_50_percent_line(width, height);
let eval_list = node.future_eval_history();
let imaginary_length = this.imaginary_length(eval_list.length);
// We make lists of contiguous edges that can be drawn at once...
let runs = this.make_runs(eval_list, width, height, imaginary_length);
// Draw our normal runs...
graphctx.strokeStyle = "white";
graphctx.lineWidth = 2;
graphctx.setLineDash([]);
for (let run of runs.normal_runs) {
graphctx.beginPath();
graphctx.moveTo(run[0].x1, run[0].y1);
for (let edge of run) {
graphctx.lineTo(edge.x2, edge.y2);
}
graphctx.stroke();
}
// Draw our dashed runs...
graphctx.strokeStyle = "#999999";
graphctx.lineWidth = 2;
graphctx.setLineDash([2, 2]);
for (let run of runs.dashed_runs) {
graphctx.beginPath();
graphctx.moveTo(run[0].x1, run[0].y1);
for (let edge of run) {
graphctx.lineTo(edge.x2, edge.y2);
}
graphctx.stroke();
}
// Finish...
this.draw_position_line(eval_list.length, node);
this.last_drawn_board = node.get_board();
this.last_draw_time = performance.now();
};
grapher.make_runs = function(eval_list, width, height, imaginary_length) {
// Returns an object with 2 arrays (normal_runs and dashed_runs).
// Each of those is an array of arrays of contiguous edges that can be drawn at once.
let all_edges = [];
let last_x = null;
let last_y = null;
let last_n = null;
// This loop creates all edges that we are going to draw, and marks each
// edge as dashed or not...
for (let n = 0; n < eval_list.length; n++) {
let e = eval_list[n];
if (e !== null) {
let x = width * n / imaginary_length;
let y = (1 - e) * height;
if (y < 1) y = 1;
if (y > height - 2) y = height - 2;
if (last_x !== null) {
all_edges.push({
x1: last_x,
y1: last_y,
x2: x,
y2: y,
dashed: n - last_n !== 1,
});
}
last_x = x;
last_y = y;
last_n = n;
}
}
// Now we make runs of contiguous edges that share a style...
let normal_runs = [];
let dashed_runs = [];
let run = [];
let current_meta_list = normal_runs; // Will point at normal_runs or dashed_runs.
for (let edge of all_edges) {
if ((edge.dashed && current_meta_list !== dashed_runs) || (!edge.dashed && current_meta_list !== normal_runs)) {
if (run.length > 0) {
current_meta_list.push(run);
}
current_meta_list = edge.dashed ? dashed_runs : normal_runs;
run = [];
}
run.push(edge);
}
if (run.length > 0) {
current_meta_list.push(run);
}
return {normal_runs, dashed_runs};
};
grapher.draw_50_percent_line = function(width, height) {
graphctx.strokeStyle = "#666666";
graphctx.lineWidth = 1;
graphctx.setLineDash([2, 2]);
graphctx.beginPath();
graphctx.moveTo(0, height / 2 - 0.5);
graphctx.lineTo(width, height / 2 - 0.5);
graphctx.stroke();
};
grapher.draw_position_line = function(eval_list_length, node) {
this.last_position_marker_x = null;
if (eval_list_length < 2) {
return;
}
let width = graph.width;
let height = graph.height;
let imaginary_length = this.imaginary_length(eval_list_length);
let depth = node.depth();
let x = Math.floor(width * depth / imaginary_length) + 0.5;
graphctx.strokeStyle = node.is_main_line() ? "#6cccee" : "#ffff00";
graphctx.lineWidth = 1;
graphctx.setLineDash([2, 2]);
graphctx.beginPath();
graphctx.moveTo(x, height / 2 - 3);
graphctx.lineTo(x, 0);
graphctx.stroke();
graphctx.beginPath();
graphctx.moveTo(x, height / 2 + 2);
graphctx.lineTo(x, height);
graphctx.stroke();
this.last_position_marker_x = x;
};
grapher.clear_position_line = function() {
// This leaves some ugly artifacts on the canvas.
let x = this.last_position_marker_x;
if (typeof x !== "number") {
return;
}
let width = graph.width;
let height = graph.height;
graphctx.strokeStyle = "#080808"; // Match the CSS background
graphctx.lineWidth = 1;
graphctx.setLineDash([2, 2]);
graphctx.beginPath();
graphctx.moveTo(x, height / 2 - 3);
graphctx.lineTo(x, 0);
graphctx.stroke();
graphctx.beginPath();
graphctx.moveTo(x, height / 2 + 2);
graphctx.lineTo(x, height);
graphctx.stroke();
};
grapher.node_from_click = function(node, event) {
if (!event || config.graph_height <= 0) {
return null;
}
let mousex = event.offsetX;
if (typeof mousex !== "number") {
return null;
}
let width = graph.width;
if (typeof width !== "number" || width < 1) {
return null;
}
let node_list = node.future_node_history();
if (node_list.length === 0) {
return null;
}
// OK, everything is valid...
let imaginary_length = this.imaginary_length(node_list.length);
let click_depth = Math.round(imaginary_length * mousex / width);
if (click_depth < 0) click_depth = 0;
if (click_depth >= node_list.length) click_depth = node_list.length - 1;
return node_list[click_depth];
};
return grapher;
}