2 var hiddenboard = null;
4 var arrow_targets = [];
5 var occupied_by_arrows = [];
6 var refutation_lines = [];
10 var sort_refutation_lines_by_score = 0;
11 var highlight_from = undefined;
12 var highlight_to = undefined;
13 var unique = Math.random();
16 var display_lines = [];
17 var current_display_line = null;
18 var current_display_move = null;
20 var request_update = function(board) {
22 url: "http://analysis.sesse.net/analysis.pl?ims=" + ims + "&unique=" + unique
23 //url: "http://analysis.sesse.net:5000/analysis.pl?ims=" + ims + "&unique=" + unique
24 }).done(function(data, textstatus, xhr) {
25 ims = xhr.getResponseHeader('X-Remoteglot-Last-Modified');
26 var num_viewers = xhr.getResponseHeader('X-Remoteglot-Num-Viewers');
27 update_board(board, data, num_viewers);
29 // Wait ten seconds, then try again.
30 setTimeout(function() { request_update(board); }, 10000);
34 var clear_arrows = function() {
35 for (var i = 0; i < arrows.length; ++i) {
36 if (arrows[i].connection1) {
37 jsPlumb.detach(arrows[i].connection1);
39 if (arrows[i].connection2) {
40 jsPlumb.detach(arrows[i].connection2);
45 for (var i = 0; i < arrow_targets.length; ++i) {
46 document.body.removeChild(arrow_targets[i]);
50 occupied_by_arrows = [];
51 for (var y = 0; y < 8; ++y) {
52 occupied_by_arrows.push([false, false, false, false, false, false, false, false]);
56 var redraw_arrows = function() {
57 for (var i = 0; i < arrows.length; ++i) {
58 position_arrow(arrows[i]);
62 var sign = function(x) {
72 // See if drawing this arrow on the board would cause unduly amount of confusion.
73 var interfering_arrow = function(from, to) {
74 var from_col = from.charCodeAt(0) - "a1".charCodeAt(0);
75 var from_row = from.charCodeAt(1) - "a1".charCodeAt(1);
76 var to_col = to.charCodeAt(0) - "a1".charCodeAt(0);
77 var to_row = to.charCodeAt(1) - "a1".charCodeAt(1);
79 occupied_by_arrows[from_row][from_col] = true;
81 // Knight move: Just check that we haven't been at the destination before.
82 if ((Math.abs(to_col - from_col) == 2 && Math.abs(to_row - from_row) == 1) ||
83 (Math.abs(to_col - from_col) == 1 && Math.abs(to_row - from_row) == 2)) {
84 return occupied_by_arrows[to_row][to_col];
87 // Sliding piece: Check if anything except the from-square is seen before.
88 var dx = sign(to_col - from_col);
89 var dy = sign(to_row - from_row);
95 if (occupied_by_arrows[y][x]) {
98 occupied_by_arrows[y][x] = true;
99 } while (x != to_col || y != to_row);
104 var add_target = function() {
105 var elem = document.createElement("div");
106 $(elem).addClass("window");
107 elem.id = "target" + arrow_targets.length;
108 document.body.appendChild(elem);
109 arrow_targets.push(elem);
113 var position_arrow = function(arrow) {
114 var zoom_factor = $("#board").width() / 400.0;
115 var line_width = arrow.line_width * zoom_factor;
116 var arrow_size = arrow.arrow_size * zoom_factor;
118 var square_width = $(".square-a8").width();
119 var from_y = (7 - arrow.from_row + 0.5)*square_width;
120 var to_y = (7 - arrow.to_row + 0.5)*square_width;
121 var from_x = (arrow.from_col + 0.5)*square_width;
122 var to_x = (arrow.to_col + 0.5)*square_width;
124 var dx = to_x - from_x;
125 var dy = to_y - from_y;
126 var len = Math.sqrt(dx * dx + dy * dy);
129 var pos = $(".square-a8").position();
130 $("#" + arrow.s1).css({ top: pos.top + from_y + (0.5 * arrow_size) * dy, left: pos.left + from_x + (0.5 * arrow_size) * dx });
131 $("#" + arrow.d1).css({ top: pos.top + to_y - (0.5 * arrow_size) * dy, left: pos.left + to_x - (0.5 * arrow_size) * dx });
132 $("#" + arrow.s1v).css({ top: pos.top + from_y - 0 * dy, left: pos.left + from_x - 0 * dx });
133 $("#" + arrow.d1v).css({ top: pos.top + to_y + 0 * dy, left: pos.left + to_x + 0 * dx });
135 if (arrow.connection1) {
136 jsPlumb.detach(arrow.connection1);
138 if (arrow.connection2) {
139 jsPlumb.detach(arrow.connection2);
141 if (current_display_line !== null) {
142 delete arrow.connection1;
143 delete arrow.connection2;
146 arrow.connection1 = jsPlumb.connect({
149 connector:["Straight"],
152 endpointClass:"c1Endpoint",
155 lineWidth:line_width,
156 strokeStyle:arrow.fg_color,
162 arrow.connection2 = jsPlumb.connect({
165 connector:["Straight"],
168 endpointClass:"c1Endpoint",
172 strokeStyle:arrow.fg_color,
183 lineWidth:line_width,
191 var create_arrow = function(from_square, to_square, fg_color, line_width, arrow_size) {
192 var from_col = from_square.charCodeAt(0) - "a1".charCodeAt(0);
193 var from_row = from_square.charCodeAt(1) - "a1".charCodeAt(1);
194 var to_col = to_square.charCodeAt(0) - "a1".charCodeAt(0);
195 var to_row = to_square.charCodeAt(1) - "a1".charCodeAt(1);
207 line_width: line_width,
208 arrow_size: arrow_size,
212 position_arrow(arrow);
216 var compare_by_sort_key = function(refutation_lines, toplay, a, b) {
217 var ska = refutation_lines[a].sort_key;
218 var skb = refutation_lines[b].sort_key;
219 if (ska < skb) return -1;
220 if (ska > skb) return 1;
224 var compare_by_score = function(refutation_lines, toplay, a, b) {
225 var sa = parseInt(refutation_lines[b].score_sort_key);
226 var sb = parseInt(refutation_lines[a].score_sort_key);
234 // Fake multi-PV using the refutation lines. Find all “relevant” moves,
235 // sorted by quality, descending.
236 var find_nonstupid_moves = function(data, margin) {
237 // First of all, if there are any moves that are more than 0.5 ahead of
238 // the primary move, the refutation lines are probably bunk, so just
240 var best_score = undefined;
241 var pv_score = undefined;
242 for (var move in data.refutation_lines) {
243 var score = parseInt(data.refutation_lines[move].score_sort_key);
244 if (move == data.pv_uci[0]) {
247 if (best_score === undefined || score > best_score) {
250 if (!(data.refutation_lines[move].depth >= 8)) {
255 if (best_score - pv_score > 50) {
259 // Now find all moves that are within “margin” of the best score.
260 // The PV move will always be first.
262 for (var move in data.refutation_lines) {
263 var score = parseInt(data.refutation_lines[move].score_sort_key);
264 if (move != data.pv_uci[0] && best_score - score <= margin) {
268 moves = moves.sort(function(a, b) { return compare_by_score(data.refutation_lines, data.position.toplay, a, b) });
269 moves.unshift(data.pv_uci[0]);
274 var thousands = function(x) {
275 return String(x).split('').reverse().join('').replace(/(\d{3}\B)/g, '$1,').split('').reverse().join('');
278 var print_pv = function(fen, uci_pv, pretty_pv, move_num, toplay, limit) {
288 var move = "<a class=\"move\" href=\"javascript:show_line(" + (display_lines.length - 1) + ", " + 0 + ");\">" + pretty_pv[0] + "</a>";
289 pv = move_num + '. … ' + move;
294 for ( ; i < pretty_pv.length; ++i) {
295 var move = "<a class=\"move\" href=\"javascript:show_line(" + (display_lines.length - 1) + ", " + i + ");\">" + pretty_pv[i] + "</a>";
304 pv += move_num + '. ' + move;
315 var update_highlight = function() {
316 $("#board").find('.square-55d63').removeClass('nonuglyhighlight');
317 if (current_display_line === null && highlight_from !== undefined && highlight_to !== undefined) {
318 $("#board").find('.square-' + highlight_from).addClass('nonuglyhighlight');
319 $("#board").find('.square-' + highlight_to).addClass('nonuglyhighlight');
323 var update_refutation_lines = function(board) {
324 if (display_lines.length > 1) {
325 display_lines = [ display_lines[0] ];
328 var tbl = $("#refutationlines");
332 for (var move in refutation_lines) {
335 var compare = sort_refutation_lines_by_score ? compare_by_score : compare_by_sort_key;
336 moves = moves.sort(function(a, b) { return compare(refutation_lines, toplay, a, b) });
337 for (var i = 0; i < moves.length; ++i) {
338 var line = refutation_lines[moves[i]];
340 var tr = document.createElement("tr");
342 var move_td = document.createElement("td");
343 tr.appendChild(move_td);
344 $(move_td).addClass("move");
345 if (line.pv_uci.length == 0) {
346 $(move_td).text(line.pretty_move);
348 var move = "<a class=\"move\" href=\"javascript:show_line(" + display_lines.length + ", " + 0 + ");\">" + line.pretty_move + "</a>";
349 $(move_td).html(move);
352 var score_td = document.createElement("td");
353 tr.appendChild(score_td);
354 $(score_td).addClass("score");
355 $(score_td).text(line.pretty_score);
357 var depth_td = document.createElement("td");
358 tr.appendChild(depth_td);
359 $(depth_td).addClass("depth");
360 $(depth_td).text("d" + line.depth);
362 var pv_td = document.createElement("td");
363 tr.appendChild(pv_td);
364 $(pv_td).addClass("pv");
365 $(pv_td).html(print_pv(fen, line.pv_uci, line.pv_pretty, move_num, toplay, 10));
370 // Make one of the links clickable and the other nonclickable.
371 if (sort_refutation_lines_by_score) {
372 $("#sortbyscore0").html("<a href=\"javascript:resort_refutation_lines(0)\">Move</a>");
373 $("#sortbyscore1").html("<strong>Score</strong>");
375 $("#sortbyscore0").html("<strong>Move</strong>");
376 $("#sortbyscore1").html("<a href=\"javascript:resort_refutation_lines(1)\">Score</a>");
380 var update_board = function(board, data, num_viewers) {
384 var headline = 'Analysis';
385 if (data.position.last_move !== 'none') {
386 headline += ' after ' + data.position.move_num + '. ';
387 if (data.position.toplay == 'W') {
390 headline += data.position.last_move;
393 $("#headline").text(headline);
395 if (num_viewers === null) {
396 $("#numviewers").text("");
397 } else if (num_viewers == 1) {
398 $("#numviewers").text("You are the only current viewer");
400 $("#numviewers").text(num_viewers + " current viewers");
404 if (data.score !== null) {
405 $("#score").text(data.score);
409 if (data.nodes && data.nps && data.depth) {
410 var stats = thousands(data.nodes) + ' nodes, ' + thousands(data.nps) + ' nodes/sec, depth ' + data.depth + ' ply';
412 stats += ' (' + data.seldepth + ' selective)';
414 if (data.tbhits && data.tbhits > 0) {
415 if (data.tbhits == 1) {
416 stats += ', one Nalimov hit';
418 stats += ', ' + data.tbhits + ' Nalimov hits';
423 $("#searchstats").text(stats);
426 // Update the board itself.
427 fen = data.position.fen;
428 update_displayed_line();
430 if (data.position.last_move_uci) {
431 highlight_from = data.position.last_move_uci.substr(0, 2);
432 highlight_to = data.position.last_move_uci.substr(2, 4);
434 highlight_from = highlight_to = undefined;
439 $("#pv").html(print_pv(data.position.fen, data.pv_uci, data.pv_pretty, data.position.move_num, data.position.toplay));
441 // Update the PV arrow.
443 if (data.pv_uci.length >= 1) {
444 // draw a continuation arrow as long as it's the same piece
445 for (var i = 0; i < data.pv_uci.length; i += 2) {
446 var from = data.pv_uci[i].substr(0, 2);
447 var to = data.pv_uci[i].substr(2,4);
448 if ((i >= 2 && from != data.pv_uci[i - 2].substr(2, 4)) ||
449 interfering_arrow(from, to)) {
452 create_arrow(from, to, '#f66', 6, 20);
455 var alt_moves = find_nonstupid_moves(data, 30);
456 for (var i = 1; i < alt_moves.length && i < 3; ++i) {
457 create_arrow(alt_moves[i].substr(0, 2),
458 alt_moves[i].substr(2, 4), '#f66', 1, 10);
462 // See if all semi-reasonable moves have only one possible response.
463 if (data.pv_uci.length >= 2) {
464 var nonstupid_moves = find_nonstupid_moves(data, 300);
465 var response = data.pv_uci[1];
466 for (var i = 0; i < nonstupid_moves.length; ++i) {
467 if (nonstupid_moves[i] == data.pv_uci[0]) {
468 // ignore the PV move for refutation lines.
471 if (!data.refutation_lines ||
472 !data.refutation_lines[nonstupid_moves[i]] ||
473 !data.refutation_lines[nonstupid_moves[i]].pv_uci ||
474 data.refutation_lines[nonstupid_moves[i]].pv_uci.length < 1) {
475 // Incomplete PV, abort.
476 response = undefined;
479 var this_response = data.refutation_lines[nonstupid_moves[i]].pv_uci[1];
480 if (response !== this_response) {
481 // Different response depending on lines, abort.
482 response = undefined;
487 if (nonstupid_moves.length > 0 && response !== undefined) {
488 create_arrow(response.substr(0, 2),
489 response.substr(2, 4), '#66f', 6, 20);
493 // Update the refutation lines.
494 fen = data.position.fen;
495 move_num = data.position.move_num;
496 toplay = data.position.toplay;
497 refutation_lines = data.refutation_lines;
498 update_refutation_lines(board);
501 setTimeout(function() { request_update(board); }, 100);
504 var resort_refutation_lines = function(sort_by_score) {
505 sort_refutation_lines_by_score = sort_by_score;
506 update_refutation_lines(board);
509 var show_line = function(line_num, move_num) {
510 if (line_num == -1) {
511 current_display_line = null;
512 current_display_move = null;
514 current_display_line = display_lines[line_num];
515 current_display_move = move_num;
517 update_displayed_line();
522 var prev_move = function() {
523 --current_display_move;
524 update_displayed_line();
527 var next_move = function() {
528 ++current_display_move;
529 update_displayed_line();
532 var update_displayed_line = function() {
533 if (current_display_line === null) {
534 $("#linenav").hide();
535 $("#linemsg").show();
540 $("#linenav").show();
541 $("#linemsg").hide();
543 if (current_display_move == 0) {
544 $("#prevmove").html("Previous");
546 $("#prevmove").html("<a href=\"javascript:prev_move();\">Previous</a></span>");
548 if (current_display_move == current_display_line.uci_pv.length - 1) {
549 $("#nextmove").html("Next");
551 $("#nextmove").html("<a href=\"javascript:next_move();\">Next</a></span>");
554 hiddenboard.position(current_display_line.start_fen, false);
555 for (var i = 0; i <= current_display_move; ++i) {
556 var move = current_display_line.uci_pv[i];
557 move = move.substr(0, 2) + "-" + move.substr(2, 4);
558 hiddenboard.move(move, false);
560 board.position(hiddenboard.position());
563 var init = function() {
565 board = new ChessBoard('board', 'start');
566 hiddenboard = new ChessBoard('hiddenboard', 'start');
568 request_update(board);
569 $(window).resize(function() {
575 $(document).ready(init);