3 var arrow_targets = [];
4 var occupied_by_arrows = [];
6 var highlight_from = undefined;
7 var highlight_to = undefined;
8 var unique = Math.random();
10 var request_update = function(board, first) {
12 url: "http://analysis.sesse.net/analysis.pl?ims=" + ims + "&unique=" + unique
13 //url: "http://analysis.sesse.net:5000/analysis.pl?ims=" + ims + "&unique=" + unique
14 }).done(function(data, textstatus, xhr) {
15 ims = xhr.getResponseHeader('X-Remoteglot-Last-Modified');
16 var num_viewers = xhr.getResponseHeader('X-Remoteglot-Num-Viewers');
17 update_board(board, data, num_viewers);
21 var clear_arrows = function() {
22 for (var i = 0; i < arrows.length; ++i) {
23 jsPlumb.detach(arrows[i].connection1);
24 jsPlumb.detach(arrows[i].connection2);
28 for (var i = 0; i < arrow_targets.length; ++i) {
29 document.body.removeChild(arrow_targets[i]);
33 occupied_by_arrows = [];
34 for (var y = 0; y < 8; ++y) {
35 occupied_by_arrows.push([false, false, false, false, false, false, false, false]);
39 var redraw_arrows = function() {
40 for (var i = 0; i < arrows.length; ++i) {
41 position_arrow(arrows[i]);
45 var sign = function(x) {
55 // See if drawing this arrow on the board would cause unduly amount of confusion.
56 var interfering_arrow = function(from, to) {
57 var from_col = from.charCodeAt(0) - "a1".charCodeAt(0);
58 var from_row = from.charCodeAt(1) - "a1".charCodeAt(1);
59 var to_col = to.charCodeAt(0) - "a1".charCodeAt(0);
60 var to_row = to.charCodeAt(1) - "a1".charCodeAt(1);
62 occupied_by_arrows[from_row][from_col] = true;
64 // Knight move: Just check that we haven't been at the destination before.
65 if ((Math.abs(to_col - from_col) == 2 && Math.abs(to_row - from_row) == 1) ||
66 (Math.abs(to_col - from_col) == 1 && Math.abs(to_row - from_row) == 2)) {
67 return occupied_by_arrows[to_row][to_col];
70 // Sliding piece: Check if anything except the from-square is seen before.
71 var dx = sign(to_col - from_col);
72 var dy = sign(to_row - from_row);
78 if (occupied_by_arrows[y][x]) {
81 occupied_by_arrows[y][x] = true;
82 } while (x != to_col || y != to_row);
87 var add_target = function() {
88 var elem = document.createElement("div");
89 $(elem).addClass("window");
90 elem.id = "target" + arrow_targets.length;
91 document.body.appendChild(elem);
92 arrow_targets.push(elem);
96 var position_arrow = function(arrow) {
97 var zoom_factor = $("#board").width() / 400.0;
98 var line_width = arrow.line_width * zoom_factor;
99 var arrow_size = arrow.arrow_size * zoom_factor;
101 var square_width = $(".square-a8").width();
102 var from_y = (7 - arrow.from_row + 0.5)*square_width;
103 var to_y = (7 - arrow.to_row + 0.5)*square_width;
104 var from_x = (arrow.from_col + 0.5)*square_width;
105 var to_x = (arrow.to_col + 0.5)*square_width;
107 var dx = to_x - from_x;
108 var dy = to_y - from_y;
109 var len = Math.sqrt(dx * dx + dy * dy);
112 var pos = $(".square-a8").position();
113 $("#" + arrow.s1).css({ top: pos.top + from_y + (0.5 * arrow_size) * dy, left: pos.left + from_x + (0.5 * arrow_size) * dx });
114 $("#" + arrow.d1).css({ top: pos.top + to_y - (0.5 * arrow_size) * dy, left: pos.left + to_x - (0.5 * arrow_size) * dx });
115 $("#" + arrow.s1v).css({ top: pos.top + from_y - 0 * dy, left: pos.left + from_x - 0 * dx });
116 $("#" + arrow.d1v).css({ top: pos.top + to_y + 0 * dy, left: pos.left + to_x + 0 * dx });
118 if (arrow.connection1) {
119 jsPlumb.detach(arrow.connection1);
121 if (arrow.connection2) {
122 jsPlumb.detach(arrow.connection2);
124 arrow.connection1 = jsPlumb.connect({
127 connector:["Straight"],
130 endpointClass:"c1Endpoint",
133 lineWidth:line_width,
134 strokeStyle:arrow.fg_color,
140 arrow.connection2 = jsPlumb.connect({
143 connector:["Straight"],
146 endpointClass:"c1Endpoint",
150 strokeStyle:arrow.fg_color,
161 lineWidth:line_width,
169 var create_arrow = function(from_square, to_square, fg_color, line_width, arrow_size) {
170 var from_col = from_square.charCodeAt(0) - "a1".charCodeAt(0);
171 var from_row = from_square.charCodeAt(1) - "a1".charCodeAt(1);
172 var to_col = to_square.charCodeAt(0) - "a1".charCodeAt(0);
173 var to_row = to_square.charCodeAt(1) - "a1".charCodeAt(1);
185 line_width: line_width,
186 arrow_size: arrow_size,
190 position_arrow(arrow);
194 // Fake multi-PV using the refutation lines. Find all “relevant” moves,
195 // sorted by quality, descending.
196 var find_nonstupid_moves = function(data, margin) {
197 // First of all, if there are any moves that are more than 0.5 ahead of
198 // the primary move, the refutation lines are probably bunk, so just
200 var best_score = undefined;
201 var pv_score = undefined;
202 for (var move in data.refutation_lines) {
203 var score = parseInt(data.refutation_lines[move].score_sort_key);
204 if (move == data.pv_uci[0]) {
207 if (best_score === undefined || score > best_score) {
210 if (!(data.refutation_lines[move].depth >= 8)) {
215 if (best_score - pv_score > 50) {
219 // Now find all moves that are within “margin” of the best score.
220 // The PV move will always be first.
222 for (var move in data.refutation_lines) {
223 var score = parseInt(data.refutation_lines[move].score_sort_key);
224 if (move != data.pv_uci[0] && best_score - score <= margin) {
228 moves = moves.sort(function(a, b) { return parseInt(data.refutation_lines[b].score_sort_key) - parseInt(data.refutation_lines[a].score_sort_key); });
229 moves.unshift(data.pv_uci[0]);
234 var thousands = function(x) {
235 return String(x).split('').reverse().join('').replace(/(\d{3}\B)/g, '$1,').split('').reverse().join('');
238 var print_pv = function(pretty_pv, move_num, toplay, limit) {
242 pv = move_num + '. … ' + pretty_pv[0];
247 for ( ; i < pretty_pv.length; ++i) {
255 pv += move_num + '. ' + pretty_pv[i];
259 pv += ' ' + pretty_pv[i];
266 var compare_by_sort_key = function(data, a, b) {
267 var ska = data.refutation_lines[a].sort_key;
268 var skb = data.refutation_lines[b].sort_key;
269 if (ska < skb) return -1;
270 if (ska > skb) return 1;
274 var update_highlight = function() {
275 $("#board").find('.square-55d63').removeClass('nonuglyhighlight');
276 if (highlight_from !== undefined && highlight_to !== undefined) {
277 $("#board").find('.square-' + highlight_from).addClass('nonuglyhighlight');
278 $("#board").find('.square-' + highlight_to).addClass('nonuglyhighlight');
282 var update_board = function(board, data, num_viewers) {
284 var headline = 'Analysis';
285 if (data.position.last_move !== 'none') {
286 headline += ' after ' + data.position.move_num + '. ';
287 if (data.position.toplay == 'W') {
290 headline += data.position.last_move;
293 $("#headline").text(headline);
295 if (num_viewers === null) {
296 $("#numviewers").text("");
297 } else if (num_viewers == 1) {
298 $("#numviewers").text("You are the only current viewer");
300 $("#numviewers").text(num_viewers + " current viewers");
304 if (data.score !== null) {
305 $("#score").text(data.score);
309 if (data.nodes && data.nps && data.depth) {
310 var stats = thousands(data.nodes) + ' nodes, ' + thousands(data.nps) + ' nodes/sec, depth ' + data.depth + ' ply';
312 stats += ' (' + data.seldepth + ' selective)';
314 if (data.tbhits && data.tbhits > 0) {
315 if (data.tbhits == 1) {
316 stats += ', one Nalimov hit';
318 stats += ', ' + data.tbhits + ' Nalimov hits';
323 $("#searchstats").text(stats);
326 // Update the board itself.
327 board.position(data.position.fen);
329 if (data.position.last_move_uci) {
330 highlight_from = data.position.last_move_uci.substr(0, 2);
331 highlight_to = data.position.last_move_uci.substr(2, 4);
333 highlight_from = highlight_to = undefined;
338 var pv = print_pv(data.pv_pretty, data.position.move_num, data.position.toplay);
341 // Update the PV arrow.
343 if (data.pv_uci.length >= 1) {
344 // draw a continuation arrow as long as it's the same piece
345 for (var i = 0; i < data.pv_uci.length; i += 2) {
346 var from = data.pv_uci[i].substr(0, 2);
347 var to = data.pv_uci[i].substr(2,4);
348 if ((i >= 2 && from != data.pv_uci[i - 2].substr(2, 4)) ||
349 interfering_arrow(from, to)) {
352 create_arrow(from, to, '#f66', 6, 20);
355 var alt_moves = find_nonstupid_moves(data, 30);
356 for (var i = 1; i < alt_moves.length && i < 3; ++i) {
357 create_arrow(alt_moves[i].substr(0, 2),
358 alt_moves[i].substr(2, 4), '#f66', 1, 10);
362 // See if all semi-reasonable moves have only one possible response.
363 if (data.pv_uci.length >= 2) {
364 var nonstupid_moves = find_nonstupid_moves(data, 300);
365 var response = data.pv_uci[1];
366 for (var i = 0; i < nonstupid_moves.length; ++i) {
367 if (nonstupid_moves[i] == data.pv_uci[0]) {
368 // ignore the PV move for refutation lines.
371 if (!data.refutation_lines ||
372 !data.refutation_lines[nonstupid_moves[i]] ||
373 !data.refutation_lines[nonstupid_moves[i]].pv_uci ||
374 data.refutation_lines[nonstupid_moves[i]].pv_uci.length < 1) {
375 // Incomplete PV, abort.
376 response = undefined;
379 var this_response = data.refutation_lines[nonstupid_moves[i]].pv_uci[1];
380 if (response !== this_response) {
381 // Different response depending on lines, abort.
382 response = undefined;
387 if (nonstupid_moves.length > 0 && response !== undefined) {
388 create_arrow(response.substr(0, 2),
389 response.substr(2, 4), '#66f', 6, 20);
393 // Show the refutation lines.
394 var tbl = $("#refutationlines");
398 for (var move in data.refutation_lines) {
401 moves = moves.sort(function(a, b) { return compare_by_sort_key(data, a, b) });
402 for (var i = 0; i < moves.length; ++i) {
403 var line = data.refutation_lines[moves[i]];
405 var tr = document.createElement("tr");
407 var move_td = document.createElement("td");
408 tr.appendChild(move_td);
409 $(move_td).addClass("move");
410 $(move_td).text(line.pretty_move);
412 var score_td = document.createElement("td");
413 tr.appendChild(score_td);
414 $(score_td).addClass("score");
415 $(score_td).text(line.pretty_score);
417 var depth_td = document.createElement("td");
418 tr.appendChild(depth_td);
419 $(depth_td).addClass("depth");
420 $(depth_td).text("d" + line.depth);
422 var pv_td = document.createElement("td");
423 tr.appendChild(pv_td);
424 $(pv_td).addClass("pv");
425 $(pv_td).text(print_pv(line.pv_pretty, data.position.move_num, data.position.toplay, 10));
431 setTimeout(function() { request_update(board, 0); }, 100);
434 var init = function() {
436 board = new ChessBoard('board', 'start');
438 request_update(board, 1);
439 $(window).resize(function() {
445 $(document).ready(init);