]> git.sesse.net Git - remoteglot/blobdiff - www/js/remoteglot.js
Add a primitive user-selectable delay, for matching up with TV. Requested by Sigurd...
[remoteglot] / www / js / remoteglot.js
index 3a95204e20bc7692a38d2fb3fdc36ed5b7e61eab..d98c90210d7c3e216a0f4d7dad813bc45f96ffb7 100644 (file)
@@ -127,6 +127,9 @@ var unique = null;
 /** @type {boolean} @private */
 var enable_sound = false;
 
+/** @type {!number} @private */
+var delay_ms = 0;
+
 /**
  * Our best estimate of how many milliseconds we need to add to 
  * new Date() to get the true UTC time. Calibrated against the
@@ -257,51 +260,19 @@ var request_update = function() {
        current_analysis_xhr = $.ajax({
                url: backend_url + "?ims=" + ims + "&unique=" + unique
        }).done(function(data, textstatus, xhr) {
-               sync_server_clock(xhr.getResponseHeader('Date'));
-               ims = xhr.getResponseHeader('X-RGLM');
-               var num_viewers = xhr.getResponseHeader('X-RGNV');
-               var new_data;
-               if (Array.isArray(data)) {
-                       new_data = JSON.parse(JSON.stringify(current_analysis_data));
-                       JSON_delta.patch(new_data, data);
+               if (delay_ms === 0) {
+                       process_update_response(data, textstatus, xhr);
                } else {
-                       new_data = data;
-               }
-
-               var minimum_version = xhr.getResponseHeader('X-RGMV');
-               if (minimum_version && minimum_version > SCRIPT_VERSION) {
-                       // Upgrade to latest version with a force-reload.
-                       location.reload(true);
-               }
-
-               // Verify that the PV makes sense.
-               var valid = true;
-               if (new_data['pv']) {
-                       var hiddenboard = new Chess(new_data['position']['fen']);
-                       for (var i = 0; i < new_data['pv'].length; ++i) {
-                               if (hiddenboard.move(new_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.");
-                       setTimeout(function() { location.reload(true); }, 5000);
+                       setTimeout(function() { process_update_response(data, textstatus, xhr); }, delay_ms);
                }
 
                // Next update.
                if (!backend_url.match(/history/)) {
+                       var timeout = 100;
                        current_analysis_request_timer = setTimeout(function() { request_update(); }, timeout);
                }
        }).fail(function(jqXHR, textStatus, errorThrown) {
+               document.body.style.opacity = null;
                if (textStatus === "abort") {
                        // Aborted because we are switching backends. Abandon and don't retry,
                        // because another one is already started for us.
@@ -312,6 +283,47 @@ var request_update = function() {
        });
 }
 
+var process_update_response = function(data, textstatus, xhr) {
+       sync_server_clock(xhr.getResponseHeader('Date'));
+       ims = xhr.getResponseHeader('X-RGLM');
+       var num_viewers = xhr.getResponseHeader('X-RGNV');
+       var new_data;
+       if (Array.isArray(data)) {
+               new_data = JSON.parse(JSON.stringify(current_analysis_data));
+               JSON_delta.patch(new_data, data);
+       } else {
+               new_data = data;
+       }
+
+       var minimum_version = xhr.getResponseHeader('X-RGMV');
+       if (minimum_version && minimum_version > SCRIPT_VERSION) {
+               // Upgrade to latest version with a force-reload.
+               location.reload(true);
+       }
+
+       // Verify that the PV makes sense.
+       var valid = true;
+       if (new_data['pv']) {
+               var hiddenboard = new Chess(new_data['position']['fen']);
+               for (var i = 0; i < new_data['pv'].length; ++i) {
+                       if (hiddenboard.move(new_data['pv'][i]) === null) {
+                               valid = false;
+                               break;
+                       }
+               }
+       }
+
+       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.");
+               setTimeout(function() { location.reload(true); }, 5000);
+       }
+}
+
 var possibly_play_sound = function(old_data, new_data) {
        if (!enable_sound) {
                return;
@@ -1028,6 +1040,8 @@ var patch_move = function(move) {
 /** Update all the HTML on the page, based on current global state.
  */
 var update_board = function() {
+       document.body.style.opacity = null;
+
        var data = displayed_analysis_data || current_analysis_data;
        var current_data = current_analysis_data;  // Convenience alias.
 
@@ -2291,6 +2305,7 @@ var switch_backend = function(game) {
 window['switch_backend'] = switch_backend;
 
 window['flip'] = function() { board.flip(); redraw_arrows(); };
+window['set_delay_ms'] = function(ms) { delay_ms = ms; console.log('Delay is now ' + ms + ' ms.'); };
 
 // Mostly from Wikipedia's chess set as of October 2022, but some pieces are from
 // the 2013 version, as I like those better (and it matches the 2014 PNGs; nobody