3 var arrow_targets = [];
4 var occupied_by_arrows = [];
5 var refutation_lines = [];
9 var sort_refutation_lines_by_score = 0;
10 var highlight_from = undefined;
11 var highlight_to = undefined;
12 var unique = Math.random();
14 var request_update = function(board) {
16 url: "http://analysis.sesse.net/analysis.pl?ims=" + ims + "&unique=" + unique
17 //url: "http://analysis.sesse.net:5000/analysis.pl?ims=" + ims + "&unique=" + unique
18 }).done(function(data, textstatus, xhr) {
19 ims = xhr.getResponseHeader('X-Remoteglot-Last-Modified');
20 var num_viewers = xhr.getResponseHeader('X-Remoteglot-Num-Viewers');
21 update_board(board, data, num_viewers);
23 // Wait ten seconds, then try again.
24 setTimeout(function() { request_update(board); }, 10000);
28 var clear_arrows = function() {
29 for (var i = 0; i < arrows.length; ++i) {
30 jsPlumb.detach(arrows[i].connection1);
31 jsPlumb.detach(arrows[i].connection2);
35 for (var i = 0; i < arrow_targets.length; ++i) {
36 document.body.removeChild(arrow_targets[i]);
40 occupied_by_arrows = [];
41 for (var y = 0; y < 8; ++y) {
42 occupied_by_arrows.push([false, false, false, false, false, false, false, false]);
46 var redraw_arrows = function() {
47 for (var i = 0; i < arrows.length; ++i) {
48 position_arrow(arrows[i]);
52 var sign = function(x) {
62 // See if drawing this arrow on the board would cause unduly amount of confusion.
63 var interfering_arrow = function(from, to) {
64 var from_col = from.charCodeAt(0) - "a1".charCodeAt(0);
65 var from_row = from.charCodeAt(1) - "a1".charCodeAt(1);
66 var to_col = to.charCodeAt(0) - "a1".charCodeAt(0);
67 var to_row = to.charCodeAt(1) - "a1".charCodeAt(1);
69 occupied_by_arrows[from_row][from_col] = true;
71 // Knight move: Just check that we haven't been at the destination before.
72 if ((Math.abs(to_col - from_col) == 2 && Math.abs(to_row - from_row) == 1) ||
73 (Math.abs(to_col - from_col) == 1 && Math.abs(to_row - from_row) == 2)) {
74 return occupied_by_arrows[to_row][to_col];
77 // Sliding piece: Check if anything except the from-square is seen before.
78 var dx = sign(to_col - from_col);
79 var dy = sign(to_row - from_row);
85 if (occupied_by_arrows[y][x]) {
88 occupied_by_arrows[y][x] = true;
89 } while (x != to_col || y != to_row);
94 var add_target = function() {
95 var elem = document.createElement("div");
96 $(elem).addClass("window");
97 elem.id = "target" + arrow_targets.length;
98 document.body.appendChild(elem);
99 arrow_targets.push(elem);
103 var position_arrow = function(arrow) {
104 var zoom_factor = $("#board").width() / 400.0;
105 var line_width = arrow.line_width * zoom_factor;
106 var arrow_size = arrow.arrow_size * zoom_factor;
108 var square_width = $(".square-a8").width();
109 var from_y = (7 - arrow.from_row + 0.5)*square_width;
110 var to_y = (7 - arrow.to_row + 0.5)*square_width;
111 var from_x = (arrow.from_col + 0.5)*square_width;
112 var to_x = (arrow.to_col + 0.5)*square_width;
114 var dx = to_x - from_x;
115 var dy = to_y - from_y;
116 var len = Math.sqrt(dx * dx + dy * dy);
119 var pos = $(".square-a8").position();
120 $("#" + arrow.s1).css({ top: pos.top + from_y + (0.5 * arrow_size) * dy, left: pos.left + from_x + (0.5 * arrow_size) * dx });
121 $("#" + arrow.d1).css({ top: pos.top + to_y - (0.5 * arrow_size) * dy, left: pos.left + to_x - (0.5 * arrow_size) * dx });
122 $("#" + arrow.s1v).css({ top: pos.top + from_y - 0 * dy, left: pos.left + from_x - 0 * dx });
123 $("#" + arrow.d1v).css({ top: pos.top + to_y + 0 * dy, left: pos.left + to_x + 0 * dx });
125 if (arrow.connection1) {
126 jsPlumb.detach(arrow.connection1);
128 if (arrow.connection2) {
129 jsPlumb.detach(arrow.connection2);
131 arrow.connection1 = jsPlumb.connect({
134 connector:["Straight"],
137 endpointClass:"c1Endpoint",
140 lineWidth:line_width,
141 strokeStyle:arrow.fg_color,
147 arrow.connection2 = jsPlumb.connect({
150 connector:["Straight"],
153 endpointClass:"c1Endpoint",
157 strokeStyle:arrow.fg_color,
168 lineWidth:line_width,
176 var create_arrow = function(from_square, to_square, fg_color, line_width, arrow_size) {
177 var from_col = from_square.charCodeAt(0) - "a1".charCodeAt(0);
178 var from_row = from_square.charCodeAt(1) - "a1".charCodeAt(1);
179 var to_col = to_square.charCodeAt(0) - "a1".charCodeAt(0);
180 var to_row = to_square.charCodeAt(1) - "a1".charCodeAt(1);
192 line_width: line_width,
193 arrow_size: arrow_size,
197 position_arrow(arrow);
201 var compare_by_sort_key = function(refutation_lines, toplay, a, b) {
202 var ska = refutation_lines[a].sort_key;
203 var skb = refutation_lines[b].sort_key;
204 if (ska < skb) return -1;
205 if (ska > skb) return 1;
209 var compare_by_score = function(refutation_lines, toplay, a, b) {
210 var sa = parseInt(refutation_lines[b].score_sort_key);
211 var sb = parseInt(refutation_lines[a].score_sort_key);
219 // Fake multi-PV using the refutation lines. Find all “relevant” moves,
220 // sorted by quality, descending.
221 var find_nonstupid_moves = function(data, margin) {
222 // First of all, if there are any moves that are more than 0.5 ahead of
223 // the primary move, the refutation lines are probably bunk, so just
225 var best_score = undefined;
226 var pv_score = undefined;
227 for (var move in data.refutation_lines) {
228 var score = parseInt(data.refutation_lines[move].score_sort_key);
229 if (move == data.pv_uci[0]) {
232 if (best_score === undefined || score > best_score) {
235 if (!(data.refutation_lines[move].depth >= 8)) {
240 if (best_score - pv_score > 50) {
244 // Now find all moves that are within “margin” of the best score.
245 // The PV move will always be first.
247 for (var move in data.refutation_lines) {
248 var score = parseInt(data.refutation_lines[move].score_sort_key);
249 if (move != data.pv_uci[0] && best_score - score <= margin) {
253 moves = moves.sort(function(a, b) { return compare_by_score(data.refutation_lines, a, b) });
254 moves.unshift(data.pv_uci[0]);
259 var thousands = function(x) {
260 return String(x).split('').reverse().join('').replace(/(\d{3}\B)/g, '$1,').split('').reverse().join('');
263 var print_pv = function(pretty_pv, move_num, toplay, limit) {
267 pv = move_num + '. … ' + pretty_pv[0];
272 for ( ; i < pretty_pv.length; ++i) {
280 pv += move_num + '. ' + pretty_pv[i];
284 pv += ' ' + pretty_pv[i];
291 var update_highlight = function() {
292 $("#board").find('.square-55d63').removeClass('nonuglyhighlight');
293 if (highlight_from !== undefined && highlight_to !== undefined) {
294 $("#board").find('.square-' + highlight_from).addClass('nonuglyhighlight');
295 $("#board").find('.square-' + highlight_to).addClass('nonuglyhighlight');
299 var update_refutation_lines = function(board) {
300 var tbl = $("#refutationlines");
304 for (var move in refutation_lines) {
307 var compare = sort_refutation_lines_by_score ? compare_by_score : compare_by_sort_key;
308 moves = moves.sort(function(a, b) { return compare(refutation_lines, toplay, a, b) });
309 for (var i = 0; i < moves.length; ++i) {
310 var line = refutation_lines[moves[i]];
312 var tr = document.createElement("tr");
314 var move_td = document.createElement("td");
315 tr.appendChild(move_td);
316 $(move_td).addClass("move");
317 $(move_td).text(line.pretty_move);
319 var score_td = document.createElement("td");
320 tr.appendChild(score_td);
321 $(score_td).addClass("score");
322 $(score_td).text(line.pretty_score);
324 var depth_td = document.createElement("td");
325 tr.appendChild(depth_td);
326 $(depth_td).addClass("depth");
327 $(depth_td).text("d" + line.depth);
329 var pv_td = document.createElement("td");
330 tr.appendChild(pv_td);
331 $(pv_td).addClass("pv");
332 $(pv_td).text(print_pv(line.pv_pretty, move_num, toplay, 10));
337 // Make one of the links clickable and the other nonclickable.
338 if (sort_refutation_lines_by_score) {
339 $("#sortbyscore0").html("<a href=\"javascript:resort_refutation_lines(0)\">Move</a>");
340 $("#sortbyscore1").html("<strong>Score</strong>");
342 $("#sortbyscore0").html("<strong>Move</strong>");
343 $("#sortbyscore1").html("<a href=\"javascript:resort_refutation_lines(1)\">Score</a>");
347 var update_board = function(board, data, num_viewers) {
349 var headline = 'Analysis';
350 if (data.position.last_move !== 'none') {
351 headline += ' after ' + data.position.move_num + '. ';
352 if (data.position.toplay == 'W') {
355 headline += data.position.last_move;
358 $("#headline").text(headline);
360 if (num_viewers === null) {
361 $("#numviewers").text("");
362 } else if (num_viewers == 1) {
363 $("#numviewers").text("You are the only current viewer");
365 $("#numviewers").text(num_viewers + " current viewers");
369 if (data.score !== null) {
370 $("#score").text(data.score);
374 if (data.nodes && data.nps && data.depth) {
375 var stats = thousands(data.nodes) + ' nodes, ' + thousands(data.nps) + ' nodes/sec, depth ' + data.depth + ' ply';
377 stats += ' (' + data.seldepth + ' selective)';
379 if (data.tbhits && data.tbhits > 0) {
380 if (data.tbhits == 1) {
381 stats += ', one Nalimov hit';
383 stats += ', ' + data.tbhits + ' Nalimov hits';
388 $("#searchstats").text(stats);
391 // Update the board itself.
392 board.position(data.position.fen);
394 if (data.position.last_move_uci) {
395 highlight_from = data.position.last_move_uci.substr(0, 2);
396 highlight_to = data.position.last_move_uci.substr(2, 4);
398 highlight_from = highlight_to = undefined;
403 var pv = print_pv(data.pv_pretty, data.position.move_num, data.position.toplay);
406 // Update the PV arrow.
408 if (data.pv_uci.length >= 1) {
409 // draw a continuation arrow as long as it's the same piece
410 for (var i = 0; i < data.pv_uci.length; i += 2) {
411 var from = data.pv_uci[i].substr(0, 2);
412 var to = data.pv_uci[i].substr(2,4);
413 if ((i >= 2 && from != data.pv_uci[i - 2].substr(2, 4)) ||
414 interfering_arrow(from, to)) {
417 create_arrow(from, to, '#f66', 6, 20);
420 var alt_moves = find_nonstupid_moves(data, 30);
421 for (var i = 1; i < alt_moves.length && i < 3; ++i) {
422 create_arrow(alt_moves[i].substr(0, 2),
423 alt_moves[i].substr(2, 4), '#f66', 1, 10);
427 // See if all semi-reasonable moves have only one possible response.
428 if (data.pv_uci.length >= 2) {
429 var nonstupid_moves = find_nonstupid_moves(data, 300);
430 var response = data.pv_uci[1];
431 for (var i = 0; i < nonstupid_moves.length; ++i) {
432 if (nonstupid_moves[i] == data.pv_uci[0]) {
433 // ignore the PV move for refutation lines.
436 if (!data.refutation_lines ||
437 !data.refutation_lines[nonstupid_moves[i]] ||
438 !data.refutation_lines[nonstupid_moves[i]].pv_uci ||
439 data.refutation_lines[nonstupid_moves[i]].pv_uci.length < 1) {
440 // Incomplete PV, abort.
441 response = undefined;
444 var this_response = data.refutation_lines[nonstupid_moves[i]].pv_uci[1];
445 if (response !== this_response) {
446 // Different response depending on lines, abort.
447 response = undefined;
452 if (nonstupid_moves.length > 0 && response !== undefined) {
453 create_arrow(response.substr(0, 2),
454 response.substr(2, 4), '#66f', 6, 20);
458 // Update the refutation lines.
459 move_num = data.position.move_num;
460 toplay = data.position.toplay;
461 refutation_lines = data.refutation_lines;
462 update_refutation_lines(board);
465 setTimeout(function() { request_update(board); }, 100);
468 var resort_refutation_lines = function(sort_by_score) {
469 sort_refutation_lines_by_score = sort_by_score;
470 update_refutation_lines(board);
473 var init = function() {
475 board = new ChessBoard('board', 'start');
477 request_update(board);
478 $(window).resize(function() {
484 $(document).ready(init);