Add a way to ask the client to reload
authorRaphaël Jakse <raphael.jakse@univ-grenoble-alpes.fr>
Sun, 5 Apr 2020 08:25:52 +0000 (10:25 +0200)
committerRaphaël Jakse <raphael.jakse@univ-grenoble-alpes.fr>
Sun, 5 Apr 2020 08:25:52 +0000 (10:25 +0200)
l10n/po/fr/trivabble.po
l10n/pot/trivabble.pot
trivabble.js

index 9f7c06e40f3a2c4993373f7fee6c0f17a6bb1439..d37e360111a925af065ebc96f86c18af62a492a1 100644 (file)
@@ -109,5 +109,8 @@ msgstr "Remettre toutes les pièces du\nchevalet dans le sac"
 msgid  "Interface language:"\r
 msgstr "Langue de l'interface :"\r
 \r
+msgid "Sorry to disturb you, but we need to reload the game so you can continue playing. You will not lose anything except the messages in the chat. Are you ready?"\r
+msgstr "Désolé pour le dérangement, mais nous avons besoin de recharger le jeu pour que vous poussiez continuer à jouer. Vous ne perdrez rien sauf les messages dans le chat. Êtes-vous prêt·e ?"\r
+\r
 msgid "To continue playing, click here"\r
 msgstr "Pour continuer à jouer, cliquez ici"\r
index 337b9c29e4d5c453ed8558262a710663fe3f329b..a5cda81225d482829f994d4aebc2d9b912fe8a97 100644 (file)
@@ -115,5 +115,8 @@ msgstr ""
 msgid  "Interface language:"
 msgstr ""
 
+msgid "Sorry to disturb you, but we need to reload the game so you can continue playing. You will not lose anything except the messages in the chat. Are you ready?"
+msgstr ""
+
 msgid "To continue playing, click here"
 msgstr ""
index 0b7663498ceefc6539791db70170eae15ed8c91b..7960f78e9fa45d6388298edea9f16b7fc909ca5e 100644 (file)
@@ -27,6 +27,7 @@
 (function () {
     "use strict";
 
+    var VERSION = 202004041733;
     var POLLING_DELAY = 2000;
 
     var _ = (window.libD && libD.l10n) ? libD.l10n() : function (s) {return s;};
@@ -50,6 +51,7 @@
     var lastDate = 0;
 
     var blockMove = 0;
+    var needsRestart = false;
     var gameInProgress = false;
 
     function mouseDown(ele, fun, stop) {
     var pollingReady = false;
     var retriedImmediately = false;
 
-    function fatalError(e) {
+    function forceReload(msg) {
+        needsRestart = true;
         myConfirm(
-            _("Sorry, a problem just happened. The page must be reloaded. If the problem is not too serious, you should be able to keep playing normally. Otherwise, contact the person who is able to fix the problem. Click on “Yes” to reload the page."),
+            msg,
             function () {
                 location.reload();
             }, function () {
         throw e;
     }
 
+    function fatalError(e) {
+        if (!needsRestart) {
+            // No need to show the user that a problem just happened if a message asking to restart has already been shown.
+            forceReload(_("Sorry, a problem just happened. The page must be reloaded. If the problem is not too serious, you should be able to keep playing normally. Otherwise, contact the person who is able to fix the problem. Click on “Yes” to reload the page."));
+        }
+        throw e;
+    }
+
     function jsonError(json, e) {
         console.error("An error ocurred while parsing this JSON message:", json);
         fatalError(e);
             return;
         }
 
+        if (data.pleaseRestart) {
+            forceReload(_("Sorry to disturb you, but we need to reload the game so you can continue playing. You will not lose anything except the messages in the chat. Are you ready?"));
+        }
+
         if (data.msg) {
             chatMessages.appendChild(
                 document.createElement("div")
     }
 
     function retryPolling() {
+        if (needsRestart) {
+            return;
+        }
+
         pollingServer = false;
         pollingReady = false;
 
     }
 
     function send(data) {
+        if (needsRestart) {
+            return;
+        }
+
         if (!pollingReady && ((data.cmds && data.cmds.length) && data.cmds[0].cmd !== "joinGame")) {
             sendCmds();
             setTimeout(send.bind(null, data), POLLING_DELAY);
 
             pollingServer = true;
             thisRequestIsPolling = true;
+            data.version = VERSION;
         }
 
         if (!thisRequestIsPolling) {
                 parseAndHandleReceivedData(e.data);
             };
 
-            es.onerror = retryPolling;
+            es.onerror = function () {
+                if (needsRestart) {
+                    es.close();
+                } else {
+                    retryPolling();
+                }
+            };
+
             return;
         }