]> git.sesse.net Git - remoteglot-book/blobdiff - www/js/book.js
Allow switching computer moves with j/k.
[remoteglot-book] / www / js / book.js
index 30a5677d95b2aca3efa64627c0080631a1e0cb98..aacd98030c516bef45fa7bd0d380884e16f90674 100644 (file)
@@ -121,7 +121,7 @@ var find_last_move_score = function() {
                        ";includetransp=0"
        }).done(function(data, textstatus, xhr) {
                var moves = data['moves'];
-               var root_move = sort_moves_by_common(moves, data);
+               var root_move = sort_move_by_frequency(moves, data);
                var your_move, your_index;
 
                for (var i = 0; i < moves.length; ++i) {
@@ -131,7 +131,6 @@ var find_last_move_score = function() {
                                your_index = i;
                        }
                }
-               moves.sort(function(a, b) { return b['num'] - a['num'] });
 
                if (your_move) {
                        $("#yourfraction").text(format_fraction(your_move['fraction']));
@@ -159,15 +158,18 @@ var find_last_move_score = function() {
        });
 }
 
+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) {
-               var candidate_moves = [];
+               candidate_moves = [];
 
                var moves = data['moves'];
-               var root_move = sort_moves_by_common(moves, data);
+               var root_move = sort_move_by_frequency(moves, data);
 
                var practice_minimum_move_fraction;
                if (move_override > 20) {
@@ -186,22 +188,40 @@ var find_computer_move = function() {
                }
 
                // Pick one at random.
-               var chosen_index = Math.floor(Math.random() * candidate_moves.length);
-               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");
-               }
-               make_move(chosen['move']);
+               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_moves_by_common = function(moves, data)
+var sort_move_by_frequency = function(moves, data)
 {
        var total_num = find_total_games(moves);
        var root_move;
@@ -473,6 +493,7 @@ var set_practice = function(value) {
                $("#stats").hide();
                $("#practiceoutput").show();
                document.getElementById("includetransp").checked = false;
+               set_includetransp(false);
        } else {
                $("#stats").show();
                $("#practiceoutput").hide();
@@ -734,6 +755,10 @@ var init = function() {
                        next_move();
                } else if (event.which == 37) {
                        prev_move();
+               } else if (event.which == 74) {  // j
+                       if (practice_mode) next_variant();
+               } else if (event.which == 75) {  // k
+                       if (practice_mode) prev_variant();
                }
        });