3 var arrow_targets = [];
4 var occupied_by_arrows = [];
5 var highlight_from = undefined;
6 var highlight_to = undefined;
8 var request_update = function(board, first) {
10 //url: "http://analysis.sesse.net/analysis.pl?first=" + first
11 url: "http://analysis.sesse.net:5000/analysis.pl?first=" + first
12 }).done(function(data) {
13 update_board(board, data);
17 var clear_arrows = function() {
18 for (var i = 0; i < arrows.length; ++i) {
19 jsPlumb.detach(arrows[i].connection1);
20 jsPlumb.detach(arrows[i].connection2);
24 for (var i = 0; i < arrow_targets.length; ++i) {
25 document.body.removeChild(arrow_targets[i]);
29 occupied_by_arrows = [];
30 for (var y = 0; y < 8; ++y) {
31 occupied_by_arrows.push([false, false, false, false, false, false, false, false]);
35 var redraw_arrows = function() {
36 for (var i = 0; i < arrows.length; ++i) {
37 position_arrow(arrows[i]);
41 var sign = function(x) {
51 // See if drawing this arrow on the board would cause unduly amount of confusion.
52 var interfering_arrow = function(from, to) {
53 var from_col = from.charCodeAt(0) - "a1".charCodeAt(0);
54 var from_row = from.charCodeAt(1) - "a1".charCodeAt(1);
55 var to_col = to.charCodeAt(0) - "a1".charCodeAt(0);
56 var to_row = to.charCodeAt(1) - "a1".charCodeAt(1);
58 occupied_by_arrows[from_row][from_col] = true;
60 // Knight move: Just check that we haven't been at the destination before.
61 if ((Math.abs(to_col - from_col) == 2 && Math.abs(to_row - from_row) == 1) ||
62 (Math.abs(to_col - from_col) == 1 && Math.abs(to_row - from_row) == 2)) {
63 return occupied_by_arrows[to_row][to_col];
66 // Sliding piece: Check if anything except the from-square is seen before.
67 var dx = sign(to_col - from_col);
68 var dy = sign(to_row - from_row);
74 if (occupied_by_arrows[y][x]) {
77 occupied_by_arrows[y][x] = true;
78 } while (x != to_col || y != to_row);
83 var add_target = function() {
84 var elem = document.createElement("div");
85 $(elem).addClass("window");
86 elem.id = "target" + arrow_targets.length;
87 document.body.appendChild(elem);
88 arrow_targets.push(elem);
92 var position_arrow = function(arrow) {
93 var zoom_factor = $("#board").width() / 400.0;
94 var line_width = arrow.line_width * zoom_factor;
95 var arrow_size = arrow.arrow_size * zoom_factor;
97 var square_width = Math.floor(($("#board").width() - 1) / 8);
98 var from_y = (7 - arrow.from_row + 0.5)*square_width + 1;
99 var to_y = (7 - arrow.to_row + 0.5)*square_width + 1;
100 var from_x = (arrow.from_col + 0.5)*square_width + 1;
101 var to_x = (arrow.to_col + 0.5)*square_width + 1;
103 var dx = to_x - from_x;
104 var dy = to_y - from_y;
105 var len = Math.sqrt(dx * dx + dy * dy);
108 var pos = $("#board").position();
109 $("#" + arrow.s1).css({ top: pos.top + from_y + (0.5 * arrow_size) * dy, left: pos.left + from_x + (0.5 * arrow_size) * dx });
110 $("#" + arrow.d1).css({ top: pos.top + to_y - (0.5 * arrow_size) * dy, left: pos.left + to_x - (0.5 * arrow_size) * dx });
111 $("#" + arrow.s1v).css({ top: pos.top + from_y - 0 * dy, left: pos.left + from_x - 0 * dx });
112 $("#" + arrow.d1v).css({ top: pos.top + to_y + 0 * dy, left: pos.left + to_x + 0 * dx });
114 if (arrow.connection1) {
115 jsPlumb.detach(arrow.connection1);
117 if (arrow.connection2) {
118 jsPlumb.detach(arrow.connection2);
120 arrow.connection1 = jsPlumb.connect({
123 connector:["Straight"],
126 endpointClass:"c1Endpoint",
129 lineWidth:line_width,
130 strokeStyle:arrow.fg_color,
136 arrow.connection2 = jsPlumb.connect({
139 connector:["Straight"],
142 endpointClass:"c1Endpoint",
146 strokeStyle:arrow.fg_color,
157 lineWidth:line_width,
165 var create_arrow = function(from_square, to_square, fg_color, line_width, arrow_size) {
166 var from_col = from_square.charCodeAt(0) - "a1".charCodeAt(0);
167 var from_row = from_square.charCodeAt(1) - "a1".charCodeAt(1);
168 var to_col = to_square.charCodeAt(0) - "a1".charCodeAt(0);
169 var to_row = to_square.charCodeAt(1) - "a1".charCodeAt(1);
181 line_width: line_width,
182 arrow_size: arrow_size,
186 position_arrow(arrow);
190 // Fake multi-PV using the refutation lines. Find all “relevant” moves,
191 // sorted by quality, descending.
192 var find_nonstupid_moves = function(data, margin) {
193 // First of all, if there are any moves that are more than 0.5 ahead of
194 // the primary move, the refutation lines are probably bunk, so just
196 var best_score = undefined;
197 var pv_score = undefined;
198 for (var move in data.refutation_lines) {
199 var score = data.refutation_lines[move].score_sort_key;
200 if (move == data.pv_uci[0]) {
203 if (best_score === undefined || score > best_score) {
206 if (!(data.refutation_lines[move].depth >= 8)) {
211 if (best_score - pv_score > 50) {
215 // Now find all moves that are within “margin” of the best score.
216 // The PV move will always be first.
218 for (var move in data.refutation_lines) {
219 var score = data.refutation_lines[move].score_sort_key;
220 if (move != data.pv_uci[0] && best_score - score <= margin) {
224 moves = moves.sort(function(a, b) { return data.refutation_lines[b].score_sort_key - data.refutation_lines[a].score_sort_key; });
225 moves.unshift(data.pv_uci[0]);
230 var thousands = function(x) {
231 return String(x).split('').reverse().join('').replace(/(\d{3}\B)/g, '$1,').split('').reverse().join('');
234 var print_pv = function(pretty_pv, move_num, toplay, limit) {
238 pv = move_num + '. … ' + pretty_pv[0];
243 for ( ; i < pretty_pv.length; ++i) {
251 pv += move_num + '. ' + pretty_pv[i];
255 pv += ' ' + pretty_pv[i];
262 var compare_by_sort_key = function(data, a, b) {
263 var ska = data.refutation_lines[a].sort_key;
264 var skb = data.refutation_lines[b].sort_key;
265 if (ska < skb) return -1;
266 if (ska > skb) return 1;
270 var update_highlight = function() {
271 $("#board").find('.square-55d63').removeClass('nonuglyhighlight');
272 if (highlight_from !== undefined && highlight_to !== undefined) {
273 $("#board").find('.square-' + highlight_from).addClass('nonuglyhighlight');
274 $("#board").find('.square-' + highlight_to).addClass('nonuglyhighlight');
278 var update_board = function(board, data) {
280 var headline = 'Analysis';
281 if (data.position.last_move !== 'none') {
282 headline += ' after ' + data.position.move_num + '. ';
283 if (data.position.toplay == 'W') {
286 headline += data.position.last_move;
289 $("#headline").text(headline);
292 if (data.score !== null) {
293 $("#score").text(data.score);
297 if (data.nodes && data.nps && data.depth) {
298 var stats = thousands(data.nodes) + ' nodes, ' + thousands(data.nps) + ' nodes/sec, depth ' + data.depth + ' ply';
300 stats += ' (' + data.seldepth + ' selective)';
302 if (data.tbhits && data.tbhits > 0) {
303 if (data.tbhits == 1) {
304 stats += ', one Nalimov hit';
306 stats += ', ' + data.tbhits + ' Nalimov hits';
311 $("#searchstats").text(stats);
314 // Update the board itself.
315 board.position(data.position.fen);
317 if (data.position.last_move_uci) {
318 highlight_from = data.position.last_move_uci.substr(0, 2);
319 highlight_to = data.position.last_move_uci.substr(2, 4);
321 highlight_from = highlight_to = undefined;
326 var pv = print_pv(data.pv_pretty, data.position.move_num, data.position.toplay);
329 // Update the PV arrow.
331 if (data.pv_uci.length >= 1) {
332 // draw a continuation arrow as long as it's the same piece
333 for (var i = 0; i < data.pv_uci.length; i += 2) {
334 var from = data.pv_uci[i].substr(0, 2);
335 var to = data.pv_uci[i].substr(2,4);
336 if ((i >= 2 && from != data.pv_uci[i - 2].substr(2, 4)) ||
337 interfering_arrow(from, to)) {
340 create_arrow(from, to, '#f66', 6, 20);
343 var alt_moves = find_nonstupid_moves(data, 30);
344 for (var i = 1; i < alt_moves.length && i < 3; ++i) {
345 create_arrow(alt_moves[i].substr(0, 2),
346 alt_moves[i].substr(2, 4), '#f66', 1, 10);
350 // See if all semi-reasonable moves have only one possible response.
351 if (data.pv_uci.length >= 2) {
352 var nonstupid_moves = find_nonstupid_moves(data, 300);
353 var response = data.pv_uci[1];
354 for (var i = 0; i < nonstupid_moves.length; ++i) {
355 if (nonstupid_moves[i] == data.pv_uci[0]) {
356 // ignore the PV move for refutation lines.
359 if (!data.refutation_lines ||
360 !data.refutation_lines[nonstupid_moves[i]] ||
361 !data.refutation_lines[nonstupid_moves[i]].pv_uci ||
362 data.refutation_lines[nonstupid_moves[i]].pv_uci.length < 1) {
363 // Incomplete PV, abort.
364 response = undefined;
367 var this_response = data.refutation_lines[nonstupid_moves[i]].pv_uci[1];
368 if (response !== this_response) {
369 // Different response depending on lines, abort.
370 response = undefined;
375 if (nonstupid_moves.length > 0 && response !== undefined) {
376 create_arrow(response.substr(0, 2),
377 response.substr(2, 4), '#66f', 6, 20);
381 // Show the refutation lines.
382 var tbl = $("#refutationlines");
386 for (var move in data.refutation_lines) {
389 moves = moves.sort(function(a, b) { return compare_by_sort_key(data, a, b) });
390 for (var i = 0; i < moves.length; ++i) {
391 var line = data.refutation_lines[moves[i]];
393 var tr = document.createElement("tr");
395 var move_td = document.createElement("td");
396 tr.appendChild(move_td);
397 $(move_td).addClass("move");
398 $(move_td).text(line.pretty_move);
400 var score_td = document.createElement("td");
401 tr.appendChild(score_td);
402 $(score_td).addClass("score");
403 $(score_td).text(line.pretty_score);
405 var depth_td = document.createElement("td");
406 tr.appendChild(depth_td);
407 $(depth_td).addClass("depth");
408 $(depth_td).text("d" + line.depth);
410 var pv_td = document.createElement("td");
411 tr.appendChild(pv_td);
412 $(pv_td).addClass("pv");
413 $(pv_td).text(print_pv(line.pv_pretty, data.position.move_num, data.position.toplay, 10));
419 setTimeout(function() { request_update(board, 0); }, 100);
422 var init = function() {
424 board = new ChessBoard('board', 'start');
426 request_update(board, 1);
427 $(window).resize(function() {
433 $(document).ready(init);