+ var hiddenboard = chess_from(current_display_line.start_fen, current_display_line.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.pv.length - 1) {
+ var first_move = current_display_line.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.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'][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;