board language setting is now per game

This commit is contained in:
Laurent Mazet 2020-05-04 23:27:36 +02:00
parent b9dd9bd054
commit c4f3c433a1
3 changed files with 24 additions and 23 deletions

View 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>

View 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) {
@ -540,7 +542,8 @@
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;
}
}
@ -1336,7 +1339,7 @@
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}]);
}
@ -1645,7 +1648,7 @@
document.getElementById("board-lang").textContent = localStorage.trivabbleBoardLang;
}
startGame(localStorage.trivabbleGameNumber);
startGame(localStorage.trivabbleGameNumber, localStorage.trivabbleBoardLang);
}
function initLang() {

View 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,