From 4ea8a3e472ad74b63bd243c54922f5e1bb025630 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sun, 13 Nov 2016 19:10:39 +0100 Subject: [PATCH] Make the frontend more resistant to bogus PVs. --- www/js/remoteglot.js | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/www/js/remoteglot.js b/www/js/remoteglot.js index d145717..5674cf1 100644 --- a/www/js/remoteglot.js +++ b/www/js/remoteglot.js @@ -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. -- 2.39.2