From 5d01811f985a433049cde53c0bbc04a8cf07537e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Jakse?= Date: Tue, 5 Aug 2025 16:20:52 +0200 Subject: [PATCH] #22: Changing one's player name breaks spell checking and word count --- l10n/po/fr/trivabble.po | 6 ++++ l10n/pot/trivabble.pot | 6 ++++ public/trivabble.js | 41 ++++++++++++++++++++---- server/trivabble-server.js | 64 +++++++++++++++++++++++++++++++++++--- 4 files changed, 107 insertions(+), 10 deletions(-) diff --git a/l10n/po/fr/trivabble.po b/l10n/po/fr/trivabble.po index 2e2227f..80e150c 100644 --- a/l10n/po/fr/trivabble.po +++ b/l10n/po/fr/trivabble.po @@ -301,6 +301,12 @@ msgstr "Impossible de trouver le plateau de type '{0}'. Changez de type de plate msgid "Caution, this feature is experimental; any feedback will be appreciated" msgstr "Attention, cette fonctionnalité est expérimentale ; tout commentaire sera grandement apprécié" +msgid "Sorry, this server doesn't support player renaming." +msgstr "Désolé, ce serveur ne prend pas en charge le changement de nom." + +msgid "Could not change your name. Please check that it's not already used by someone else!" +msgstr "Impossible de changer votre nom. Veuillez vérifier que personne n’utilise déjà ce nom !" + msgid "joker" msgstr "joker" diff --git a/l10n/pot/trivabble.pot b/l10n/pot/trivabble.pot index 7198382..892cf05 100644 --- a/l10n/pot/trivabble.pot +++ b/l10n/pot/trivabble.pot @@ -307,6 +307,12 @@ msgstr "" msgid "Caution, this feature is experimental; any feedback will be appreciated" msgstr "" +msgid "Sorry, this server doesn't support player renaming." +msgstr "" + +msgid "Could not change your name. Please check that it's not already used by someone else!" +msgstr "" + msgid "joker" msgstr "" diff --git a/public/trivabble.js b/public/trivabble.js index 6dc4c4b..12d2b06 100644 --- a/public/trivabble.js +++ b/public/trivabble.js @@ -26,7 +26,7 @@ (function () { "use strict"; - const VERSION = 202005070130; + const VERSION = 202508050000; const Conf = window.TrivabbleConf || {}; @@ -224,6 +224,7 @@ let currentMessageId = 1; const waitingMsgs = []; + const msgCmdById = {}; let lastPlayer = null; @@ -1127,6 +1128,20 @@ blink(countCell); nbTilesInRacks += player.rackCount; } + + if (Object.prototype.hasOwnProperty.call(player, "newName")) { + const newName = player.newName; + tablePlayers[newName] = tablePlayers[playerName]; + delete tablePlayers[playerName]; + playerNameCell = tablePlayers[playerName].querySelector("td"); + playerNameCell.textContent = newName; + if (playerName == getSetting("PlayerName")) { + setSetting("PlayerName", newName); + name.textContent = newName; + } else { + blink(playerNameCell); + } + } } refreshCurrentPlayer(); @@ -1203,10 +1218,14 @@ return; } + let cmdName = null; + if (data.id) { const pos = waitingMsgs.indexOf(data.id); if (pos !== -1) { waitingMsgs.splice(pos, 1); + cmdName = msgCmdById[data.id]; + delete msgCmdById[data.id]; } } @@ -1219,7 +1238,11 @@ } if (data.error) { - fatalError(new Error("Error from the server: " + data.error + " - " + data.reason)); + if (cmdName === "changePlayerName") { + alert(_("Could not change your name. Please check that it's not already used by someone else!"); + } else { + fatalError(new Error("Error from the server: " + data.error + " - " + data.reason)); + } } if (data.stopping) { @@ -1600,6 +1623,7 @@ for (const cmd of cmds) { waitingMsgs.push(currentMessageId); cmd.id = currentMessageId++; + msgCmdById[cmd.id] = cmd.cmd; } } @@ -1693,15 +1717,20 @@ } function changeName() { + if (serverVersion < 202508050000) { + alert(_("Sorry, this server doesn't support player renaming.")); + return; + } + myPrompt( _("To change your name, enter a new one. You can keep using your current name by cancelling. Please note that if you change your name and you have games in progress, you will not be able to keep playing them anymore unless you get back to your current name."), function (newName) { - if (newName && newName.trim()) { - setSetting("PlayerName", newName.trim()); - name.textContent = getSetting("PlayerName"); + const n = newName && newName.trim(); + if (n) { + sendCmds([{cmd: "changePlayerName", playerName: n}]); } }, - getSetting("PlayerNamer") + getSetting("PlayerName") ); } diff --git a/server/trivabble-server.js b/server/trivabble-server.js index 9fbb0be..69b41de 100644 --- a/server/trivabble-server.js +++ b/server/trivabble-server.js @@ -40,7 +40,7 @@ const DEFAULT_BAG_FACTOR = 1; const CHECK_FIRST_WORD = 1; // 1: check, 0: not set, -1: no check const FREEZE_PLAYED_TILES = -1; // 1: freeze, 0: not set, -1: no freeze -const VERSION = 202005070100; +const VERSION = 202508050000; function envTrue(name) { return (process.env[name] || "").toLowerCase() === "true"; @@ -245,6 +245,8 @@ function newBoard(label) { return res; } +// if you add something there that stores the player name, please make sure to +// update changePlayerName // eslint-disable-next-line max-params Game.prototype.init = function (lang, label, factor, checkFirstWord, freezePlayedTiles) { this.label = label || DEFAULT_BOARD_LABEL; @@ -505,6 +507,51 @@ Game.prototype.reset = function (player) { this.playerJoined(); }; +Game.prototype.changePlayerName = function (playerName, newName) { + if (!newName || !newName.trim()) { + return ""; + } + + const n = newName.trim(); + if (n === playerName) { + return ""; + } + + const currentPlayerID = "#" + playerName; + const newPlayerID = "#" + n; + if ( + !Object.prototype.hasOwnProperty.call(this.racks, currentPlayerID) || + Object.prototype.hasOwnProperty.call(this.racks, newPlayerID) + ) { + return ""; + } + + if (this.currentPlayer === playerName) { + this.currentPlayer = n; + } + + this.racks[newPlayerID] = this.racks[currentPlayerID]; + delete this.racks[currentPlayerID]; + + this.scores[newPlayerID] = this.scores[currentPlayerID]; + delete this.scores[currentPlayerID]; + + for (const [key, player] of Object.entries(this.tileOwner)) { + if (player === playerName) { + this.tileOwner[key] = n; + } + } + + this.pendingEvents.push({ + players: [{ + player: playerName, + newName: n + }] + }); + this.commit(); + return n; +}; + function newGameId() { let number; @@ -539,10 +586,10 @@ function countTiles(rack) { function joinGame(gameNumber, message) { if (!gameNumber) { - gameNumber = ((message) && (parseInt(message.gameNumber) > 0)) ? message.gameNumber : newGameId(); + gameNumber = (message && (parseInt(message.gameNumber) > 0)) + ? message.gameNumber + : newGameId(); } - message.gameNumber = gameNumber; - let game; if (games[gameNumber]) { @@ -601,6 +648,15 @@ function handleCommand(cmdNumber, message, response) { break; } + case "changePlayerName": { + const newName = game.changePlayerName(playerName, cmd.playerName); + if (newName) { + message.playerName = newName; + } + reply(message, response, cmdNumber, {error: newName ? 0 : 1}); + break; + } + case "hello": { game.playerJoined(playerName); reply(message, response, cmdNumber, {error: 0, boardLang: game.lang, boardLabel: game.label, bagFactor: game.factor, version: VERSION}); -- 2.30.2