+var find_last_move_score = function() {
+ var history = game.history({ verbose: true });
+ var side_to_move = (move_override % 2 == 0) ? 'W' : 'B';
+ var move_num = (side_to_move === practice_side) ? (move_override - 2) : (move_override - 1);
+ if (move_num < 0) {
+ $("#yourmove").text("(none)");
+ $("#yourfraction").text("N/A");
+ $("#yourrank").text("N/A");
+ $("#yourawin").text("??.?%");
+ $("#yourawindiff").text("+?.?%");
+ return;
+ }
+
+ var chosen_move = history[move_num].san;
+ $("#yourmove").text(chosen_move);
+ $.ajax({
+ url: "/opening-stats.pl?fen=" + encodeURIComponent(fen_before_move(move_num)) +
+ ";includetransp=0"
+ }).done(function(data, textstatus, xhr) {
+ var moves = data['moves'];
+ var root_move = sort_move_by_frequency(moves, data);
+ var your_move, your_index;
+
+ for (var i = 0; i < moves.length; ++i) {
+ var move = moves[i];
+ if (move['move'] === chosen_move) {
+ your_move = move;
+ your_index = i;
+ }
+ }
+
+ if (your_move) {
+ $("#yourfraction").text(format_fraction(your_move['fraction']));
+ $("#yourawin").text(format_fraction(your_move['corrected_win_ratio']));
+ $("#yourrank").text(format_ordinal(your_index + 1) + ", based on " + root_move['num'] + " games");
+ var diff = your_move['corrected_win_ratio'] - root_move['corrected_win_ratio'];
+ $("#yourawindiff").css("color", "black");
+ if (diff === 0) {
+ $("#yourawindiff").text("0.0%");
+ } else if (diff > 0) {
+ $("#yourawindiff").text("+" + format_fraction(diff));
+ $("#yourawindiff").css("color", "green");
+ } else {
+ $("#yourawindiff").text(format_fraction(diff));
+ if (diff < -0.02) {
+ $("#yourawindiff").css("color", "red");
+ }
+ }
+ } else {
+ $("#yourfraction").text("??.?%");
+ $("#yourrank").text("?th");
+ $("#yourawin").text("??.?%");
+ $("#yourawindiff").text("+?.?%");
+ }
+ });
+}
+
+var candidate_moves = [];
+var chosen_index = null;
+
+var find_computer_move = function() {
+ var fen = current_display_fen();
+ $.ajax({
+ url: "/opening-stats.pl?fen=" + encodeURIComponent(fen) + ";includetransp=0"
+ }).done(function(data, textstatus, xhr) {
+ candidate_moves = [];
+
+ var moves = data['moves'];
+ var root_move = sort_move_by_frequency(moves, data);
+
+ var practice_minimum_move_fraction;
+ if (move_override > 20) {
+ practice_minimum_move_fraction = practice_minimum_move_fraction_move5;
+ } else {
+ practice_minimum_move_fraction = practice_minimum_move_fraction_start +
+ ((move_override-1)/10.0) * (practice_minimum_move_fraction_move5 - practice_minimum_move_fraction_start);
+ }
+ console.log(practice_minimum_move_fraction);
+
+ for (var i = 0; i < Math.min(moves.length, practice_top_moves_limit); ++i) {
+ var move = moves[i];
+ if (i == 0 || move['fraction'] >= practice_minimum_move_fraction) {
+ candidate_moves.push(move);
+ }
+ }
+
+ // Pick one at random.
+ choose_move(Math.floor(Math.random() * candidate_moves.length));
+ });
+}
+
+var choose_move = function(idx) {
+ chosen_index = idx;
+ var chosen = candidate_moves[chosen_index];
+ $("#compmove").text(chosen['move']);
+ $("#compfraction").text((100.0 * chosen['fraction']).toFixed(1) + "%");
+ if (candidate_moves.length == 1) {
+ $("#comprank").text("only candidate move");
+ } else {
+ $("#comprank").text(format_ordinal(chosen_index + 1) + " out of " + candidate_moves.length + " candidate moves, j/k to switch");
+ }
+ make_move(chosen['move']);
+}
+
+var prev_variant = function() {
+ if (chosen_index !== null) {
+ --move_override;
+ choose_move((chosen_index + candidate_moves.length - 1) % candidate_moves.length);
+ }
+}
+
+var next_variant = function() {
+ if (chosen_index !== null) {
+ --move_override;
+ choose_move((chosen_index + 1) % candidate_moves.length);
+ }
+}
+
+// Add deried data and then sort moves to get the most common ones (in-place).
+// Remove the root mode and return it. Currently usable for practice mode only!
+var sort_move_by_frequency = function(moves, data)
+{
+ var total_num = find_total_games(moves);
+ var root_move;
+ for (var i = 0; i < moves.length; ++i) {
+ var move = moves[i];
+ calc_move_derived_data(move, total_num, data, (practice_side === 'W'));
+ if (!move['move']) {
+ root_move = (moves.splice(i, 1))[0];
+ --i;
+ }
+ }
+ moves.sort(function(a, b) { return b['num'] - a['num'] });
+ return root_move;
+}
+