diff --git a/public/trivabble.js b/public/trivabble.js index 64589e8..d333b69 100644 --- a/public/trivabble.js +++ b/public/trivabble.js @@ -187,6 +187,7 @@ let boardLangSelect; let boardLabelSelect; const downloadedDictionaries = {}; + const boardDef = {nbRows: 0, nbColumns: 0, rackLength: 0}; const playerLetters = []; let currentPlayer = ""; @@ -239,15 +240,13 @@ } function setRack(rack) { - const rackLength = getBoardDimensions(getSetting("BoardLabel"))[2]; - for (let i = 0; i < rackLength; i++) { + for (let i = 0; i < boardDef.rackLength; i++) { setTileParent(playerLetters[i], rack[i] || ""); } } function getFreeRackSpaceIndex() { - const rackLength = getBoardDimensions(getSetting("BoardLabel"))[2]; - for (let i = 0; i < rackLength; i++) { + for (let i = 0; i < boardDef.rackLength; i++) { if (!playerLetters[i].getElementsByClassName("tile")[0]) { return i; } @@ -456,8 +455,7 @@ } } } else if (tileInitDest.getElementsByClassName("tile")[0]) { - const rackLength = getBoardDimensions(getSetting("BoardLabel"))[2]; - for (let i = 0; i < rackLength; i++) { + for (let i = 0; i < boardDef.rackLength; i++) { if (!playerLetters[i].getElementsByClassName("tile")[0]) { playerLetters[i].appendChild(movingTile); break; @@ -762,17 +760,8 @@ setTileParent(boardCells[index].getElementsByClassName("tile-placeholder")[0], letter, highlight); } - function getBoardDimensions(boardLabel) { - return (typeof boardLabel === "undefined") ? [] : boardLabel - .match(/[0-9]*/gu) - .filter(function (x) {return (x.length !== 0);}) - .map(function (x) {return parseInt(x);}); - } - function setBoard(board) { - const [nbRows, nbColumns] = getBoardDimensions(getSetting("BoardLabel")); - - for (let i = 0; i < nbRows * nbColumns; i++) { + for (let i = 0; i < boardDef.nbRows * boardDef.nbColumns; i++) { setCell(i, board[i]); } } @@ -795,7 +784,12 @@ case "boardLabel": document.getElementById("board-label").value = value; setSetting("BoardLabel", value); - initBoard(value); + [boardDef.nbRows, boardDef.nbColumns, boardDef.rackLength] = + (typeof value === "undefined") ? [0, 0, 0] : value + .match(/[0-9]*/gu) + .filter(function (x) {return (x.length !== 0);}) + .map(function (x) {return parseInt(x);}); + initBoard(); break; } } @@ -1238,8 +1232,13 @@ } if (data.boardLabel) { - set("boardLabel", data.boardLabel); - (boardLabelSelect || {}).value = data.boardLabel; + if (Object.keys(BoardList).indexOf(data.boardLabel) === -1) { + myAlert("Can't find board '" + data.boardLabel + "'. Change board or start a new game."); + (boardLabelSelect || {}).value = ""; + } else { + set("boardLabel", data.boardLabel); + (boardLabelSelect || {}).value = data.boardLabel; + } } if (data.letterValues) { @@ -1531,6 +1530,7 @@ playerName: getSetting("PlayerName"), boardLang: getSetting("BoardLang"), boardLabel: getSetting("BoardLabel"), + bagFactor: BoardList[getSetting("BoardLabel")].factor, version: VERSION, cmds: cmds }; @@ -1680,7 +1680,7 @@ myConfirm( format(_("Are you sure you want to change board to '{0}'? This will put all the tiles back in the bag and start another game."), _(lang)), function () { - sendCmds([{cmd: "changeBoard", lang: code, label: getSetting("BoardLabel")}]); + sendCmds([{cmd: "changeBoard", lang: code, label: getSetting("BoardLabel"), factor: BoardList[getSetting("BoardLabel")].factor}]); }, function () { boardLangSelect.value = getSetting("BoardLang"); @@ -1694,7 +1694,7 @@ myConfirm( format(_("Are you sure you want to change board to {0}? This will put all the tiles back in the bag and start another game."), label), function () { - sendCmds([{cmd: "changeBoard", lang: getSetting("BoardLang"), label: label}]); + sendCmds([{cmd: "changeBoard", lang: getSetting("BoardLang"), label: label, factor: BoardList[label].factor}]); }, function () { boardLabelSelect.value = getSetting("BoardLabel"); @@ -1764,13 +1764,11 @@ } function getLetterFromBoard(row, col) { - const [nbRows, nbColumns] = getBoardDimensions(getSetting("BoardLabel")); - - if ((row < 0) || (row >= nbRows) || (col < 0) || (col >= nbColumns)) { + if ((row < 0) || (row >= boardDef.nbRows) || (col < 0) || (col >= boardDef.nbColumns)) { return ""; } try { - const index = (row * nbColumns) + col; + const index = (row * boardDef.nbColumns) + col; const tilePlaceholder = boardCells[index].getElementsByClassName("tile-placeholder")[0]; const tile = tilePlaceholder.getElementsByClassName("tile")[0]; return tile.firstChild.textContent; @@ -1816,14 +1814,12 @@ } function searchNewWords() { - const nbColumns = getBoardDimensions(getSetting("BoardLabel"))[1]; - const words = []; for (const i of Object.keys(currentTilePlayed)) { /* Get board position */ - const row = Math.floor(i / nbColumns); - const col = i % nbColumns; + const row = Math.floor(i / boardDef.nbColumns); + const col = i % boardDef.nbColumns; /* Look for word in column */ const newWordInCol = searchWordInLine(row, col, 1, 0); @@ -1849,7 +1845,6 @@ } function modifyTileClass(word, value, action) { - const [nbRows, nbColumns] = getBoardDimensions(getSetting("BoardLabel")); /* action: true for add, false for remove */ @@ -1857,7 +1852,7 @@ /* only for new words */ for (let l = 0; l < word.letters.length; l++) { - const index = ((word.row + (l * word.incRow)) * nbColumns) + word.col + (l * word.incCol); + const index = ((word.row + (l * word.incRow)) * boardDef.nbColumns) + word.col + (l * word.incCol); const tilePlaceholder = boardCells[index].getElementsByClassName("tile-placeholder")[0]; const tile = tilePlaceholder.getElementsByClassName("tile")[0]; @@ -1867,7 +1862,7 @@ } else { /* tiles on board */ - for (let index = 0; index < nbRows * nbColumns; index++) { + for (let index = 0; index < boardDef.nbRows * boardDef.nbColumns; index++) { const tilePlaceholder = boardCells[index].getElementsByClassName("tile-placeholder")[0]; const tile = tilePlaceholder.getElementsByClassName("tile")[0]; if (typeof tile !== "undefined") { @@ -1948,7 +1943,6 @@ } function scoreWords() { - const [nbColumns, rackLength] = getBoardDimensions(getSetting("BoardLabel")).slice(1); let totalScore = 0; const newWords = searchNewWords(); @@ -1958,7 +1952,7 @@ const word = newWords[k]; for (let l = 0; l < word.letters.length; l++) { - const index = ((word.row + (l * word.incRow)) * nbColumns) + word.col + (l * word.incCol); + const index = ((word.row + (l * word.incRow)) * boardDef.nbColumns) + word.col + (l * word.incCol); /* Letter score */ const tilePlaceholder = boardCells[index].getElementsByClassName("tile-placeholder")[0]; @@ -1997,7 +1991,7 @@ } /* Check for trivabble (the premium score) */ - if (Object.keys(currentTilePlayed).length === rackLength) { + if (Object.keys(currentTilePlayed).length === boardDef.rackLength) { totalScore += getSetting("PREMIUM_SEVEN_TILES"); } @@ -2031,7 +2025,6 @@ return; } - const nbColumns = getBoardDimensions(getSetting("BoardLabel"))[1]; const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; const col = cell.cellIndex - 1; const row = getRowIndex(cell.parentNode) - 1; @@ -2041,7 +2034,7 @@ msg: _("Look at:") + " " + letters[row] + (cell.cellIndex + 1), specialMsg: { type: "highlightCell", - cell: (row * nbColumns) + col + cell: (row * boardDef.nbColumns) + col } }]); } @@ -2539,35 +2532,26 @@ startGame(getSetting("GameNumber")); } - function initBoard(boardLabel) { - + function initBoard() { const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; - - if (Object.keys(BoardList).indexOf(boardLabel) === -1) { - myAlert("Can't find board '" + boardLabel + "'. Change board or start a new game."); - return; - } - - const boardDef = BoardList[boardLabel]; - - const [nbRows, nbColumns, rackLength] = getBoardDimensions(boardLabel); + const def = BoardList[getSetting("BoardLabel")]; let cell; let row; board.innerHTML = "