Make the frontend more resistant to bogus PVs.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 13 Nov 2016 18:10:39 +0000 (19:10 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 13 Nov 2016 18:10:39 +0000 (19:10 +0100)
www/js/remoteglot.js

index d145717..5674cf1 100644 (file)
@@ -277,13 +277,29 @@ var request_update = function() {
                        location.reload(true);
                }
 
-               possibly_play_sound(current_analysis_data, new_data);
-               current_analysis_data = new_data;
-               update_board();
-               update_num_viewers(num_viewers);
+               // Verify that the PV makes sense.
+               var valid = true;
+               var hiddenboard = new Chess(new_data['position']['fen']);
+               for (var i = 0; i < new_data['pv'].length; ++i) {
+                       if (hiddenboard.move(data['pv'][i]) === null) {
+                               valid = false;
+                               break;
+                       }
+               }
+
+               var timeout = 100;
+               if (valid) {
+                       possibly_play_sound(current_analysis_data, new_data);
+                       current_analysis_data = new_data;
+                       update_board();
+                       update_num_viewers(num_viewers);
+               } else {
+                       console.log("Received invalid update, waiting five seconds and trying again.");
+                       timeout = 5000;
+               }
 
                // Next update.
-               current_analysis_request_timer = setTimeout(function() { request_update(); }, 100);
+               current_analysis_request_timer = setTimeout(function() { request_update(); }, timeout);
        }).fail(function(jqXHR, textStatus, errorThrown) {
                if (textStatus === "abort") {
                        // Aborted because we are switching backends. Abandon and don't retry,
@@ -1071,7 +1087,7 @@ var update_board = function() {
        if (data['position'] && data['position']['last_move_uci']) {
                highlight_from = data['position']['last_move_uci'].substr(0, 2);
                highlight_to = data['position']['last_move_uci'].substr(2, 2);
-       } else if (current_display_line_is_history && current_display_move >= 0) {
+       } else if (current_display_line_is_history && current_display_line && current_display_move >= 0) {
                // We don't have historic analysis for this position, but we
                // can reconstruct what the last move was by just replaying
                // from the start.