3 var arrow_targets = [];
4 var occupied_by_arrows = [];
6 var request_update = function(board, first) {
8 //url: "http://analysis.sesse.net/analysis.pl?first=" + first
9 url: "http://analysis.sesse.net:5000/analysis.pl?first=" + first
10 }).done(function(data) {
11 update_board(board, data);
15 var clear_arrows = function() {
16 for (var i = 0; i < arrows.length; ++i) {
17 jsPlumb.detach(arrows[i].connection1);
18 jsPlumb.detach(arrows[i].connection2);
22 for (var i = 0; i < arrow_targets.length; ++i) {
23 document.body.removeChild(arrow_targets[i]);
27 occupied_by_arrows = [];
28 for (var y = 0; y < 8; ++y) {
29 occupied_by_arrows.push([false, false, false, false, false, false, false, false]);
33 var redraw_arrows = function() {
34 for (var i = 0; i < arrows.length; ++i) {
35 position_arrow(arrows[i]);
39 var sign = function(x) {
49 // See if drawing this arrow on the board would cause unduly amount of confusion.
50 var interfering_arrow = function(from, to) {
51 var from_col = from.charCodeAt(0) - "a1".charCodeAt(0);
52 var from_row = from.charCodeAt(1) - "a1".charCodeAt(1);
53 var to_col = to.charCodeAt(0) - "a1".charCodeAt(0);
54 var to_row = to.charCodeAt(1) - "a1".charCodeAt(1);
56 occupied_by_arrows[from_row][from_col] = true;
58 // Knight move: Just check that we haven't been at the destination before.
59 if ((Math.abs(to_col - from_col) == 2 && Math.abs(to_row - from_row) == 1) ||
60 (Math.abs(to_col - from_col) == 1 && Math.abs(to_row - from_row) == 2)) {
61 return occupied_by_arrows[to_row][to_col];
64 // Sliding piece: Check if anything except the from-square is seen before.
65 var dx = sign(to_col - from_col);
66 var dy = sign(to_row - from_row);
72 if (occupied_by_arrows[y][x]) {
75 occupied_by_arrows[y][x] = true;
76 } while (x != to_col || y != to_row);
81 var add_target = function() {
82 var elem = document.createElement("div");
83 $(elem).addClass("window");
84 elem.id = "target" + arrow_targets.length;
85 document.body.appendChild(elem);
86 arrow_targets.push(elem);
90 var position_arrow = function(arrow) {
91 var zoom_factor = $("#board").width() / 400.0;
92 var line_width = arrow.line_width * zoom_factor;
93 var arrow_size = arrow.arrow_size * zoom_factor;
95 var square_width = Math.floor(($("#board").width() - 1) / 8);
96 var from_y = (7 - arrow.from_row + 0.5)*square_width + 1;
97 var to_y = (7 - arrow.to_row + 0.5)*square_width + 1;
98 var from_x = (arrow.from_col + 0.5)*square_width + 1;
99 var to_x = (arrow.to_col + 0.5)*square_width + 1;
101 var dx = to_x - from_x;
102 var dy = to_y - from_y;
103 var len = Math.sqrt(dx * dx + dy * dy);
106 var pos = $("#board").position();
107 $("#" + arrow.s1).css({ top: pos.top + from_y + (0.5 * arrow_size) * dy, left: pos.left + from_x + (0.5 * arrow_size) * dx });
108 $("#" + arrow.d1).css({ top: pos.top + to_y - (0.5 * arrow_size) * dy, left: pos.left + to_x - (0.5 * arrow_size) * dx });
109 $("#" + arrow.s1v).css({ top: pos.top + from_y - 0 * dy, left: pos.left + from_x - 0 * dx });
110 $("#" + arrow.d1v).css({ top: pos.top + to_y + 0 * dy, left: pos.left + to_x + 0 * dx });
112 if (arrow.connection1) {
113 jsPlumb.detach(arrow.connection1);
115 if (arrow.connection2) {
116 jsPlumb.detach(arrow.connection2);
118 arrow.connection1 = jsPlumb.connect({
121 connector:["Straight"],
124 endpointClass:"c1Endpoint",
127 lineWidth:line_width,
128 strokeStyle:arrow.fg_color,
134 arrow.connection2 = jsPlumb.connect({
137 connector:["Straight"],
140 endpointClass:"c1Endpoint",
144 strokeStyle:arrow.fg_color,
155 lineWidth:line_width,
163 var create_arrow = function(from_square, to_square, fg_color, line_width, arrow_size) {
164 var from_col = from_square.charCodeAt(0) - "a1".charCodeAt(0);
165 var from_row = from_square.charCodeAt(1) - "a1".charCodeAt(1);
166 var to_col = to_square.charCodeAt(0) - "a1".charCodeAt(0);
167 var to_row = to_square.charCodeAt(1) - "a1".charCodeAt(1);
179 line_width: line_width,
180 arrow_size: arrow_size,
184 position_arrow(arrow);
188 // Fake multi-PV using the refutation lines. Find all “relevant” moves,
189 // sorted by quality, descending.
190 var find_nonstupid_moves = function(data, margin) {
191 // First of all, if there are any moves that are more than 0.5 ahead of
192 // the primary move, the refutation lines are probably bunk, so just
194 var best_score = undefined;
195 var pv_score = undefined;
196 for (var move in data.refutation_lines) {
197 var score = data.refutation_lines[move].score_sort_key;
198 if (move == data.pv_uci[0]) {
201 if (best_score === undefined || score > best_score) {
204 if (!(data.refutation_lines[move].depth >= 8)) {
209 if (best_score - pv_score > 50) {
213 // Now find all moves that are within “margin” of the best score.
214 // The PV move will always be first.
216 for (var move in data.refutation_lines) {
217 var score = data.refutation_lines[move].score_sort_key;
218 if (move != data.pv_uci[0] && best_score - score <= margin) {
222 moves = moves.sort(function(a, b) { return data.refutation_lines[b].score_sort_key - data.refutation_lines[a].score_sort_key; });
223 moves.unshift(data.pv_uci[0]);
228 var thousands = function(x) {
229 return String(x).split('').reverse().join('').replace(/(\d{3}\B)/g, '$1,').split('').reverse().join('');
232 var print_pv = function(pretty_pv, move_num, toplay, limit) {
236 pv = move_num + '. … ' + pretty_pv[0];
241 for ( ; i < pretty_pv.length; ++i) {
249 pv += move_num + '. ' + pretty_pv[i];
253 pv += ' ' + pretty_pv[i];
260 var compare_by_sort_key = function(data, a, b) {
261 var ska = data.refutation_lines[a].sort_key;
262 var skb = data.refutation_lines[b].sort_key;
263 if (ska < skb) return -1;
264 if (ska > skb) return 1;
268 var update_board = function(board, data) {
270 var headline = 'Analysis';
271 if (data.position.last_move !== 'none') {
272 headline += ' after ' + data.position.move_num + '. ';
273 if (data.position.toplay == 'W') {
276 headline += data.position.last_move;
279 $("#headline").text(headline);
282 if (data.score !== null) {
283 $("#score").text(data.score);
287 if (data.nodes && data.nps && data.depth) {
288 var stats = thousands(data.nodes) + ' nodes, ' + thousands(data.nps) + ' nodes/sec, depth ' + data.depth + ' ply';
290 stats += ' (' + data.seldepth + ' selective)';
292 if (data.tbhits && data.tbhits > 0) {
293 if (data.tbhits == 1) {
294 stats += ', one Nalimov hit';
296 stats += ', ' + data.tbhits + ' Nalimov hits';
301 $("#searchstats").text(stats);
304 // Update the board itself.
305 board.position(data.position.fen);
307 $("#board").find('.square-55d63').removeClass('nonuglyhighlight');
308 if (data.position.last_move_uci) {
309 var from = data.position.last_move_uci.substr(0, 2);
310 var to = data.position.last_move_uci.substr(2, 4);
311 $("#board").find('.square-' + from).addClass('nonuglyhighlight');
312 $("#board").find('.square-' + to).addClass('nonuglyhighlight');
316 var pv = print_pv(data.pv_pretty, data.position.move_num, data.position.toplay);
319 // Update the PV arrow.
321 if (data.pv_uci.length >= 1) {
322 // draw a continuation arrow as long as it's the same piece
323 for (var i = 0; i < data.pv_uci.length; i += 2) {
324 var from = data.pv_uci[i].substr(0, 2);
325 var to = data.pv_uci[i].substr(2,4);
326 if ((i >= 2 && from != data.pv_uci[i - 2].substr(2, 4)) ||
327 interfering_arrow(from, to)) {
330 create_arrow(from, to, '#f66', 6, 20);
333 var alt_moves = find_nonstupid_moves(data, 30);
334 for (var i = 1; i < alt_moves.length && i < 3; ++i) {
335 create_arrow(alt_moves[i].substr(0, 2),
336 alt_moves[i].substr(2, 4), '#f66', 1, 10);
340 // See if all semi-reasonable moves have only one possible response.
341 if (data.pv_uci.length >= 2) {
342 var nonstupid_moves = find_nonstupid_moves(data, 300);
343 var response = data.pv_uci[1];
344 for (var i = 0; i < nonstupid_moves.length; ++i) {
345 if (nonstupid_moves[i] == data.pv_uci[0]) {
346 // ignore the PV move for refutation lines.
349 if (!data.refutation_lines ||
350 !data.refutation_lines[nonstupid_moves[i]] ||
351 !data.refutation_lines[nonstupid_moves[i]].pv_uci ||
352 data.refutation_lines[nonstupid_moves[i]].pv_uci.length < 1) {
353 // Incomplete PV, abort.
354 response = undefined;
357 var this_response = data.refutation_lines[nonstupid_moves[i]].pv_uci[1];
358 if (response !== this_response) {
359 // Different response depending on lines, abort.
360 response = undefined;
365 if (nonstupid_moves.length > 0 && response !== undefined) {
366 create_arrow(response.substr(0, 2),
367 response.substr(2, 4), '#66f', 6, 20);
371 // Show the refutation lines.
372 var tbl = $("#refutationlines");
376 for (var move in data.refutation_lines) {
379 moves = moves.sort(function(a, b) { return compare_by_sort_key(data, a, b) });
380 for (var i = 0; i < moves.length; ++i) {
381 var line = data.refutation_lines[moves[i]];
383 var tr = document.createElement("tr");
385 var move_td = document.createElement("td");
386 tr.appendChild(move_td);
387 $(move_td).addClass("move");
388 $(move_td).text(line.pretty_move);
390 var score_td = document.createElement("td");
391 tr.appendChild(score_td);
392 $(score_td).addClass("score");
393 $(score_td).text(line.pretty_score);
395 var depth_td = document.createElement("td");
396 tr.appendChild(depth_td);
397 $(depth_td).addClass("depth");
398 $(depth_td).text("d" + line.depth);
400 var pv_td = document.createElement("td");
401 tr.appendChild(pv_td);
402 $(pv_td).addClass("pv");
403 $(pv_td).text(print_pv(line.pv_pretty, data.position.move_num, data.position.toplay, 10));
409 setTimeout(function() { request_update(board, 0); }, 100);
412 var init = function() {
414 board = new ChessBoard('board', 'start');
416 request_update(board, 1);
417 $(window).resize(function() {
422 $(document).ready(init);