leave current game before joining a new game
authorLaurent Mazet <mazet@softndesign.org>
Mon, 3 Feb 2025 06:22:18 +0000 (07:22 +0100)
committerRaphaël Jakse <raph@jakse.fr>
Fri, 14 Feb 2025 07:13:19 +0000 (08:13 +0100)
public/trivabble.js

index 804e7f32f77a4c915f93bf9acca63a49600c2b3e..1ba9d615674d501ca38ff11f156aa9eef257db76 100644 (file)
         sendXHR(cmds);
     }
 
+    function sendBackTiles() {
+        const cmds = [];
+
+        for (let i = 0; i < playerLetters.length; i++) {
+            if (playerLetters[i].getElementsByClassName("tile")[0]) {
+                cmds.push({
+                    cmd: "moveLetter",
+                    from: "rack",
+                    indexFrom: i,
+                    to: "bag",
+                    indexTo: -1
+                });
+            }
+        }
+
+        if (cmds.length) {
+            sendCmds(cmds);
+        }
+        setRack("");
+    }
+
     function joinGame() {
         checkGameInProgress(
             function () {
                         if (isNaN(n)) {
                             myAlert(_("It seems your did not give a correct number, or you clicked on “Cancel”. As a result, the current game continues, if any. To join a game, click on “Join a game” again."));
                         } else {
+                            sendBackTiles();
+                            sendCmds([{cmd: "leaveGame"}]);
                             setSetting("GameNumber", n);
                             location.reload();
                         }
         );
     }
 
+    function leaveGame() {
+        myConfirm(
+            _("You are about to leave this game. Your letters will be put back in bag. Are you sure?"),
+            function () {
+                sendBackTiles();
+                sendCmds([{cmd: "leaveGame"}]);
+                setSetting("GameNumber", 0);
+                location.reload();
+            }
+        );
+    }
+
     const specialTypesText = {
         doubleLetter: "Double\nLetter",
         doubleWord:   "Double\nWord",
         );
     }
 
-    function sendBackTiles() {
-        const cmds = [];
-
-        for (let i = 0; i < playerLetters.length; i++) {
-            if (playerLetters[i].getElementsByClassName("tile")[0]) {
-                cmds.push({
-                    cmd: "moveLetter",
-                    from: "rack",
-                    indexFrom: i,
-                    to: "bag",
-                    indexTo: -1
-                });
-            }
-        }
-
-        if (cmds.length) {
-            sendCmds(cmds);
-        }
-        setRack("");
-    }
-
-    function leaveGame() {
-        myConfirm(
-            _("You are about to leave this game. Your letters will be put back in bag. Are you sure?"),
-            function () {
-                sendBackTiles();
-                sendCmds([{cmd: "leaveGame"}]);
-                setSetting("GameNumber", 0);
-                location.reload();
-            }
-        );
-    }
-
     function clearRack() {
         myConfirm(
             _("Are you sure you want to put all your tiles back in the bag?"),