From f6ad4a22d5b68f756c0a507f32fc106dc3f334a7 Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Mon, 3 Feb 2025 07:22:18 +0100 Subject: [PATCH] leave current game before joining a new game --- public/trivabble.js | 68 +++++++++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/public/trivabble.js b/public/trivabble.js index 804e7f3..1ba9d61 100644 --- a/public/trivabble.js +++ b/public/trivabble.js @@ -1567,6 +1567,27 @@ 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 () { @@ -1577,6 +1598,8 @@ 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(); } @@ -1586,6 +1609,18 @@ ); } + 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", @@ -1711,39 +1746,6 @@ ); } - 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?"), -- 2.30.2