+ var hiddenboard = chess_from(current_display_line.start_fen, current_display_line.pretty_pv, current_display_move);
+ set_board_position(hiddenboard.fen());
+ if (display_fen !== hiddenboard.fen() && !current_display_line_is_history) {
+ // Fire off a hash request, since we're now off the main position
+ // and it just changed.
+ explore_hash(hiddenboard.fen());
+ }
+ display_fen = hiddenboard.fen();
+ update_imbalance(hiddenboard.fen());
+}
+
+var set_board_position = function(new_fen) {
+ board_is_animating = true;
+ var old_fen = board.fen();
+ board.position(new_fen);
+ if (board.fen() === old_fen) {
+ board_is_animating = false;
+ }
+}
+
+/**
+ * @param {boolean} param_enable_sound
+ */
+var set_sound = function(param_enable_sound) {
+ enable_sound = param_enable_sound;
+ if (enable_sound) {
+ $("#soundon").html("<strong>On</strong>");
+ $("#soundoff").html("<a href=\"javascript:set_sound(false)\">Off</a>");
+
+ // Seemingly at least Firefox prefers MP3 over Opus; tell it otherwise,
+ // and also preload the file since the user has selected audio.
+ var ding = document.getElementById('ding');
+ if (ding && ding.canPlayType && ding.canPlayType('audio/ogg; codecs="opus"') === 'probably') {
+ ding.src = 'ding.opus';
+ ding.load();
+ }
+ } else {
+ $("#soundon").html("<a href=\"javascript:set_sound(true)\">On</a>");
+ $("#soundoff").html("<strong>Off</strong>");
+ }
+ if (supports_html5_storage()) {
+ localStorage['enable_sound'] = enable_sound ? 1 : 0;
+ }
+}
+window['set_sound'] = set_sound;
+
+/** Send off a hash probe request to the backend.
+ * @param {string} fen
+ */
+var explore_hash = function(fen) {
+ // If we already have a backend response going, abort it.
+ if (current_hash_xhr) {
+ current_hash_xhr.abort();
+ }
+ if (current_hash_display_timer) {
+ clearTimeout(current_hash_display_timer);
+ current_hash_display_timer = null;
+ }
+ $("#refutationlines").empty();
+ current_hash_xhr = $.ajax({
+ url: backend_hash_url + "?fen=" + fen
+ }).done(function(data, textstatus, xhr) {
+ show_explore_hash_results(data, fen);
+ });
+}
+
+/** Process the JSON response from a hash probe request.
+ * @param {!Object} data
+ * @param {string} fen
+ */
+var show_explore_hash_results = function(data, fen) {
+ if (board_is_animating) {
+ // Updating while the animation is still going causes
+ // the animation to jerk. This is pretty crude, but it will do.
+ current_hash_display_timer = setTimeout(function() { show_explore_hash_results(data, fen); }, 100);
+ return;
+ }
+ current_hash_display_timer = null;
+ hash_refutation_lines = data['lines'];
+ update_board();
+}
+
+// almost all of this stuff comes from the chessboard.js example page
+var onDragStart = function(source, piece, position, orientation) {
+ var pseudogame = new Chess(display_fen);
+ if (pseudogame.game_over() === true ||
+ (pseudogame.turn() === 'w' && piece.search(/^b/) !== -1) ||
+ (pseudogame.turn() === 'b' && piece.search(/^w/) !== -1)) {
+ return false;
+ }
+
+ recommended_move = get_best_move(pseudogame, source, null, pseudogame.turn() === 'b');
+ if (recommended_move) {
+ var squareEl = $('#board .square-' + recommended_move.to);
+ squareEl.addClass('highlight1-32417');
+ }
+ return true;
+}
+
+var mousedownSquare = function(e) {
+ reverse_dragging_from = null;
+ var square = $(this).attr('data-square');
+
+ var pseudogame = new Chess(display_fen);
+ if (pseudogame.game_over() === true) {
+ return;
+ }
+
+ // If the square is empty, or has a piece of the side not to move,
+ // we handle it. If not, normal piece dragging will take it.
+ var position = board.position();
+ if (!position.hasOwnProperty(square) ||
+ (pseudogame.turn() === 'w' && position[square].search(/^b/) !== -1) ||
+ (pseudogame.turn() === 'b' && position[square].search(/^w/) !== -1)) {
+ reverse_dragging_from = square;
+ recommended_move = get_best_move(pseudogame, null, square, pseudogame.turn() === 'b');
+ if (recommended_move) {
+ var squareEl = $('#board .square-' + recommended_move.from);
+ squareEl.addClass('highlight1-32417');
+ squareEl = $('#board .square-' + recommended_move.to);
+ squareEl.addClass('highlight1-32417');
+ }
+ }
+}
+
+var mouseupSquare = function(e) {
+ if (reverse_dragging_from === null) {
+ return;
+ }
+ var source = $(this).attr('data-square');
+ var target = reverse_dragging_from;
+ reverse_dragging_from = null;
+ if (onDrop(source, target) !== 'snapback') {
+ onSnapEnd(source, target);
+ }
+ $("#board").find('.square-55d63').removeClass('highlight1-32417');
+}
+
+var get_best_move = function(game, source, target, invert) {
+ var moves = game.moves({ verbose: true });
+ if (source !== null) {
+ moves = moves.filter(function(move) { return move.from == source; });
+ }
+ if (target !== null) {
+ moves = moves.filter(function(move) { return move.to == target; });
+ }
+ if (moves.length == 0) {
+ return null;
+ }
+ if (moves.length == 1) {
+ return moves[0];
+ }
+
+ // More than one move. Use the display lines (if we have them)
+ // to disambiguate; otherwise, we have no information.
+ var move_hash = {};
+ for (var i = 0; i < moves.length; ++i) {
+ move_hash[moves[i].san] = moves[i];
+ }
+
+ // See if we're already exploring some line.
+ if (current_display_line &&
+ current_display_move < current_display_line.pretty_pv.length - 1) {
+ var first_move = current_display_line.pretty_pv[current_display_move + 1];
+ if (move_hash[first_move]) {
+ return move_hash[first_move];
+ }
+ }
+
+ // History and PV take priority over the display lines.
+ for (var i = 0; i < 2; ++i) {
+ var line = display_lines[i];
+ var first_move = line.pretty_pv[line.start_display_move_num];
+ if (move_hash[first_move]) {
+ return move_hash[first_move];
+ }
+ }
+
+ var best_move = null;
+ var best_move_score = null;
+
+ for (var move in refutation_lines) {
+ var line = refutation_lines[move];
+ if (!line['score']) {
+ continue;
+ }
+ var first_move = line['pv_pretty'][0];
+ if (move_hash[first_move]) {
+ var score = compute_score_sort_key(line['score'], line['depth'], invert);
+ if (best_move_score === null || score > best_move_score) {
+ best_move = move_hash[first_move];
+ best_move_score = score;
+ }
+ }
+ }
+ return best_move;
+}
+
+var onDrop = function(source, target) {
+ if (source === target) {
+ if (recommended_move === null) {
+ return 'snapback';
+ } else {
+ // Accept the move. It will be changed in onSnapEnd.
+ return;
+ }
+ } else {
+ // Suggestion not asked for.
+ recommended_move = null;
+ }
+
+ // see if the move is legal
+ var pseudogame = new Chess(display_fen);
+ var move = pseudogame.move({
+ from: source,
+ to: target,
+ promotion: 'q' // NOTE: always promote to a queen for example simplicity
+ });
+
+ // illegal move
+ if (move === null) return 'snapback';
+}
+
+var onSnapEnd = function(source, target) {
+ if (source === target && recommended_move !== null) {
+ source = recommended_move.from;
+ target = recommended_move.to;
+ }
+ recommended_move = null;
+ var pseudogame = new Chess(display_fen);
+ var move = pseudogame.move({
+ from: source,
+ to: target,
+ promotion: 'q' // NOTE: always promote to a queen for example simplicity
+ });
+
+ if (current_display_line &&
+ current_display_move < current_display_line.pretty_pv.length - 1 &&
+ current_display_line.pretty_pv[current_display_move + 1] === move.san) {
+ next_move();
+ return;
+ }
+
+ // Walk down the displayed lines until we find one that starts with
+ // this move, then select that. Note that this gives us a good priority
+ // order (history first, then PV, then multi-PV lines).
+ for (var i = 0; i < display_lines.length; ++i) {
+ var line = display_lines[i];
+ if (line.pretty_pv[line.start_display_move_num] === move.san) {
+ show_line(i, 0);
+ return;
+ }
+ }
+
+ // Shouldn't really be here if we have hash probes, but there's really
+ // nothing we can do.
+}
+// End of dragging-related code.
+
+var fmt_cp = function(v) {
+ if (v === 0) {
+ return "0.00";
+ } else if (v > 0) {
+ return "+" + (v / 100).toFixed(2);
+ } else {
+ v = -v;
+ return "-" + (v / 100).toFixed(2);
+ }
+}
+
+var format_short_score = function(score) {
+ if (!score) {
+ return "???";
+ }
+ if (score[0] === 'm') {
+ if (score[2]) { // Is a bound.
+ return score[2] + "\u00a0M " + score[1];
+ } else {
+ return "M " + score[1];
+ }
+ } else if (score[0] === 'd') {
+ return "TB draw";
+ } else if (score[0] === 'cp') {
+ if (score[2]) { // Is a bound.
+ return score[2] + "\u00a0" + fmt_cp(score[1]);
+ } else {
+ return fmt_cp(score[1]);
+ }
+ }
+ return null;
+}
+
+var format_long_score = function(score) {
+ if (!score) {
+ return "???";
+ }
+ if (score[0] === 'm') {
+ if (score[1] > 0) {
+ return "White mates in " + score[1];
+ } else {
+ return "Black mates in " + (-score[1]);
+ }
+ } else if (score[0] === 'd') {
+ return "Theoretical draw";
+ } else if (score[0] === 'cp') {
+ return "Score: " + format_short_score(score);
+ }
+ return null;
+}
+
+var compute_plot_score = function(score) {
+ if (score[0] === 'm') {
+ if (score[1] > 0) {
+ return 500;
+ } else {
+ return -500;
+ }
+ } else if (score[0] === 'd') {
+ return 0;
+ } else if (score[0] === 'cp') {
+ if (score[1] > 500) {
+ return 500;
+ } else if (score[1] < -500) {
+ return -500;
+ } else {
+ return score[1];
+ }