board language setting is now per game
authorLaurent Mazet <mazet@softndesign.org>
Mon, 4 May 2020 21:27:36 +0000 (23:27 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Mon, 4 May 2020 21:27:36 +0000 (23:27 +0200)
public/index.html
public/trivabble.js
server/trivabble-server.js

index 769a81c1dcc4a109007537c55d57faf087d93f19..df566e8126018ca2b3531423fe690f4e73e2a4e3 100644 (file)
@@ -19,8 +19,7 @@
             <button data-l10n="text-content" id="join-game">Join adversaries</button>
             <p id="p-name"> <span data-l10n="text-content">You are:</span> <span id="name" data-l10n="text-content">(name to give)</span><br /><button class="minibutton" id="change-name" data-l10n="text-content">Change</button></p>
             <button id="clear-game" data-l10n="text-content">Put back all the tiles&#10;in the bag</button>
-            <p><span data-l10n="text-content">Board language: </span><span id="board-lang" data-l10n="text-content">(pending)</span></p>
-            <p>
+            <p id="p-name"><span data-l10n="text-content">Board language:</span> <span id="board-lang" data-l10n="text-content">(pending)</span><br />
                 <button data-l10n="text-content" id="change-de-board">German</button>
                 <button data-l10n="text-content" id="change-en-board">English</button>
                 <button data-l10n="text-content" id="change-fr-board">French</button>
index 36ff3448c7c00ef6ade37518923465a184153c62..34a93305942c64f3f86bf54ca8704f372f81657d 100644 (file)
@@ -29,6 +29,8 @@
 
     const VERSION = 202005011900;
 
+    const langName = {"de": "German", "en": "English", "es": "Spanish", "fr": "French"};
+
     const Conf = window.TrivabbleConf || {};
 
     function setConf(parameterName, defaultValue) {
                 document.getElementById("number").textContent = localStorage.trivabbleGameNumber = value;
                 break;
             case "boardLang":
-                document.getElementById("board-lang").textContent = localStorage.trivabbleBoardLang = value;
+                document.getElementById("board-lang").textContent =
+                    langName[localStorage.trivabbleBoardLang = value];
                 break;
         }
     }
 
     function changeBoardLang(lang) {
         myConfirm(
-            _("Are you sure you want to change board to '" + lang + "'? This will put all the tiles back in the bag an start another game."),
+            _("Are you sure you want to change board to '" + langName[lang] + "'? This will put all the tiles back in the bag an start another game."),
             function () {
                 sendCmds([{cmd: "changeBoard", lang: lang}]);
             }
             document.getElementById("board-lang").textContent = localStorage.trivabbleBoardLang;
         }
 
-        startGame(localStorage.trivabbleGameNumber);
+        startGame(localStorage.trivabbleGameNumber, localStorage.trivabbleBoardLang);
     }
 
     function initLang() {
index 080659e837f6da959c135b5857c90590da36488a..66539747aa8f0a38f6fde196bfbd66decf77d83c 100644 (file)
@@ -64,13 +64,10 @@ const boardPieces = {
     "es": require ("./es.js"),
     "fr": require ("./fr.js")
 };
-var boardLang = "fr";
+const defaultLang = "fr";
 
 /* eslint-enable quote-props */
 
-var bag = boardPieces[boardLang].bag;
-var values = boardPieces[boardLang].values;
-
 const games = {};
 
 let saveTimeout = null;
@@ -105,7 +102,6 @@ function shuffleInPlace(a) {
 }
 
 function Game() {
-    this.letterValues = values;
     this.init();
     this.listeningPlayers = [];
     this.pendingEvents = [];
@@ -214,9 +210,11 @@ function newBoard() {
     return res;
 }
 
-Game.prototype.init = function () {
+Game.prototype.init = function (lang) {
     this.board = newBoard();
-    this.bag = bag.slice();
+    this.lang = lang || defaultLang;
+    this.bag = boardPieces[this.lang].bag.slice();
+    this.values = boardPieces[this.lang].values;
     this.racks = {};
     this.scores = {};
     this.lastUpdated = new Date();
@@ -228,6 +226,7 @@ Game.prototype.init = function () {
 Game.prototype.toJSON = function () {
     return {
         board:            this.board,
+        lang:             this.lang,
         bag:              this.bag,
         racks:            this.racks,
         scores:           this.scores,
@@ -239,7 +238,9 @@ Game.prototype.toJSON = function () {
 Game.fromJSON = function (obj) {
     const game = new Game();
     game.board            = obj.board  || newBoard();
-    game.bag              = obj.bag    || bag.slice();
+    game.lang             = obj.lang   || defaultLang;
+    game.bag              = boardPieces[game.lang].bag.slice();
+    game.values           = boardPieces[game.lang].values;
     game.racks            = obj.racks  || {};
     game.scores           = obj.scores || {};
     game.lastUpdated      = obj.lastUpdated ? new Date(obj.lastUpdated) : new Date();
@@ -396,7 +397,7 @@ Game.prototype.bagPushLetter = function (letter, player) {
 };
 
 Game.prototype.reset = function (player) {
-    this.init();
+    this.init(this.lang);
     this.pendingEvents.push({
         player: player,
         action: "reset",
@@ -473,12 +474,12 @@ function handleCommand(cmdNumber, message, response) {
                 error: 0,
                 gameNumber: gameNumber,
                 playerName: playerName,
-                boardLang: boardLang,
+                boardLang: game.lang,
                 currentPlayer: game.currentPlayer,
                 rack: game.getPlayerRack(playerName),
                 board: game.board,
                 remainingLetters: game.bag.length,
-                letterValues: values,
+                letterValues: game.values,
                 version: VERSION
             });
             break;
@@ -486,7 +487,7 @@ function handleCommand(cmdNumber, message, response) {
 
         case "hello": {
             game.playerJoined(playerName);
-            reply(message, response, cmdNumber, {error: 0, version: VERSION});
+            reply(message, response, cmdNumber, {error: 0, boardLang: game.lang, version: VERSION});
             break;
         }
 
@@ -664,11 +665,9 @@ function handleCommand(cmdNumber, message, response) {
         }
 
         case "changeBoard": {
-            boardLang = cmd.lang;
-            bag = boardPieces[boardLang].bag;
-            values = boardPieces[boardLang].values;
+            game.lang = cmd.lang || defaultLang;
             game.reset();
-            reply(message, response, cmdNumber, {error: 0});
+            reply(message, response, cmdNumber, {error: 0, boardLang: game.lang});
             break;
         }
 
@@ -704,8 +703,8 @@ function handleCommands(message, responseAndType) {
                 playerName:       message.playerName,
                 currentPlayer:    game.currentPlayer,
                 gameNumber:       gameNumber,
-                boardLang:        boardLang,
-                letterValues:     game.letterValues,
+                boardLang:        game.lang,
+                letterValues:     game.values,
                 rack:             game.getPlayerRack(message.playerName),
                 board:            game.board,
                 remainingLetters: game.bag.length,