use setting functions

This commit is contained in:
Laurent Mazet 2020-09-10 08:04:40 +02:00
parent 9f499f11b3
commit ae9c2a7da3
1 changed files with 77 additions and 78 deletions

View File

@ -131,11 +131,11 @@
(type === "number") ||
(type === "object") ||
(type === "string")) {
localStore.setItem(key, value);
localStorage.setItem(key, value);
/* store type into localstorage if no default value in configuration */
if (Object.prototype.hasOwnProperty.call(Conf, key)) {
localStore.setItem(key + "_type", type);
if (!Object.prototype.hasOwnProperty.call(Conf, key)) {
localStorage.setItem(key + "_type", type);
}
} else {
console.error("Unsupported type");
@ -263,11 +263,11 @@
return;
}
if (!force && localStorage.spellCheckerEnabled !== "true") {
if (!force && getSetting("spellCheckerEnabled") !== "true") {
myConfirm(
_("Spell checking requires Trivabble to download a dictionary. Do you confirm?"),
function () {
localStorage.spellCheckerEnabled = "true";
setSetting("spellCheckerEnabled", "true");
getDictionary(code, callback, true);
}
);
@ -301,7 +301,7 @@
}
function spellCheckerSettingChecked() {
localStorage.spellCheckerEnabled = document.getElementById("enable-spell-checker").checked.toString();
setSetting("spellCheckerEnabled", document.getElementById("enable-spell-checker").checked);
initSpellChecker();
}
@ -729,14 +729,16 @@
function set(key, value) {
switch (key) {
case "playerName":
name.textContent = localStorage.trivabblePlayerName = value;
name.textContent = value;
setSetting("trivabblePlayerName", value);
break;
case "gameNumber":
document.getElementById("number").textContent = localStorage.trivabbleGameNumber = value;
document.getElementById("number").textContent = value;
setSetting("trivabbleGameNumber", value);
break;
case "boardLang":
localStorage.trivabbleBoardLang = value;
document.getElementById("board-lang").value = value;
setSetting("trivabbleBoardLang", value);
break;
}
}
@ -752,7 +754,7 @@
myAlert(
format(
_("You are about to leave the current game. To recover it, please note its number: {0}"),
localStorage.trivabbleGameNumber
getSetting("trivabbleGameNumber")
),
f
);
@ -832,7 +834,7 @@
chatMessages.scrollTop = chatMessages.scrollHeight;
if (sender && sender !== localStorage.trivabblePlayerName) {
if (sender && sender !== getSetting("trivabblePlayerName")) {
if (msgSound.checked) {
audioChat.play();
}
@ -872,7 +874,7 @@
case "changeBoardLang": {
const newLang = boardLangSelect.querySelector("[value=" + msg.specialMsg.newBoardLang + "]").textContent;
infoMessage(
(msg.sender === localStorage.trivabblePlayerName)
(msg.sender === getSetting("trivabblePlayerName"))
? format(_("You changed the language of the board to {0}"), newLang)
: format(_("{0} changed the language of the board to {1}"), msg.sender, newLang)
);
@ -1059,13 +1061,13 @@
}
if (data.to === "board") {
setCell(data.indexTo, data.letter, Object.prototype.hasOwnProperty.call(data, "player") && data.player !== localStorage.trivabblePlayerName);
setCell(data.indexTo, data.letter, Object.prototype.hasOwnProperty.call(data, "player") && data.player !== getSetting("trivabblePlayerName"));
} else if (data.to === "rack") {
setRackCell(data.indexTo, data.letter);
}
break;
case "setCell":
setCell(data.indexTo, data.letter, Object.prototype.hasOwnProperty.call(data, "player") && data.player !== localStorage.trivabblePlayerName);
setCell(data.indexTo, data.letter, Object.prototype.hasOwnProperty.call(data, "player") && data.player !== getSetting("trivabblePlayerName"));
if ((data.letter !== "") && (currentTilePlayed[data.indexTo] === "-")) {
currentTilePlayed[data.indexTo] = data.letter;
}
@ -1127,7 +1129,7 @@
}
if (data.gameNumber) {
set("gameNumber", data.gameNumber);
set("gameNumber", Number(data.gameNumber));
}
if (data.availableBoardLangs) {
@ -1195,7 +1197,7 @@
startConnection();
retryPollingTimeout = 0;
},
delay || Conf.POLLING_DELAY
delay || getSetting("POLLING_DELAY")
);
} else {
retriedImmediately = true;
@ -1272,7 +1274,7 @@
} else {
webSocketErrors++;
if (webSocketErrors > Conf.MAX_WEBSOCKET_ERRORS) {
if (webSocketErrors > getSetting("MAX_WEBSOCKET_ERRORS")) {
blacklistWebsockets = true;
}
}
@ -1299,7 +1301,7 @@
}
function pollServerWithEventSource() {
if (canConnect() && Conf.ENABLE_EVENT_SOURCE && window.EventSource) {
if (canConnect() && getSetting("ENABLE_EVENT_SOURCE") && window.EventSource) {
closeConnections();
pollingServer = true;
@ -1312,7 +1314,7 @@
}
function pollServerWithWebSocket() {
if (canConnect() && Conf.ENABLE_WEBSOCKETS && !blacklistWebsockets && window.WebSocket) {
if (canConnect() && getSetting("ENABLE_WEBSOCKETS") && !blacklistWebsockets && window.WebSocket) {
closeConnections();
pollingServer = true;
@ -1414,7 +1416,7 @@
xhrRequest(cmdsWithContext(cmds), function (xhr) {
if (xhr.readyState === 4) {
if (xhr.status === 0 || xhr.status >= 300) {
setTimeout(sendCmds.bind(null, cmds), Conf.POLLING_DELAY);
setTimeout(sendCmds.bind(null, cmds), getSetting("POLLING_DELAY"));
return;
}
@ -1431,9 +1433,9 @@
function cmdsWithContext(cmds) {
return {
gameNumber: localStorage.trivabbleGameNumber || "",
playerName: localStorage.trivabblePlayerName,
boardLang: localStorage.trivabbleBoardLang,
gameNumber: getSetting("trivabbleGameNumber") || "",
playerName: getSetting("trivabblePlayerName"),
boardLang: getSetting("trivabbleBoardLang"),
version: VERSION,
cmds: cmds
};
@ -1454,7 +1456,7 @@
if (!pollingReady) {
startConnection();
setTimeout(sendCmds, Conf.POLLING_DELAY, cmds);
setTimeout(sendCmds, getSetting("POLLING_DELAY"), cmds);
return;
}
@ -1465,13 +1467,13 @@
checkGameInProgress(
function () {
myPrompt(
_("To join a game, please give the number which is displayed on your adversary(ies)' screen.\nIf you do not know it, ask them.\n\nWarning: your adversary must not take your number, (s)he must keep his/her own. If you whish to recover your current game, please not the following number: {0}.").replace("{0}", localStorage.trivabbleGameNumber),
_("To join a game, please give the number which is displayed on your adversary(ies)' screen.\nIf you do not know it, ask them.\n\nWarning: your adversary must not take your number, (s)he must keep his/her own. If you whish to recover your current game, please not the following number: {0}.").replace("{0}", getSetting("trivabbleGameNumber")),
function (n) {
n = parseInt(n);
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 {
localStorage.trivabbleGameNumber = n;
setSetting("trivabbleGameNumber", n);
location.reload();
}
}
@ -1500,17 +1502,17 @@
_("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()) {
localStorage.trivabblePlayerName = newName.trim();
name.textContent = localStorage.trivabblePlayerName;
setSetting("trivabblePlayerName", newName.trim());
name.textContent = getSetting("trivabblePlayerName");
}
},
localStorage.trivabblePlayerName
getSetting("trivabblePlayerNamer")
);
}
function startGame(number) {
if (number) {
localStorage.trivabbleGameNumber = number;
setSetting("trivabbleGameNumber", number);
}
startConnection();
}
@ -1580,7 +1582,7 @@
sendCmds([{cmd: "changeBoard", lang: code}]);
},
function () {
boardLangSelect.value = localStorage.trivabbleBoardLang;
boardLangSelect.value = getSetting("trivabbleBoardLang");
}
);
}
@ -1764,11 +1766,11 @@
}
function checkSpellingClicked() {
if (localStorage.spellCheckerEnabled === "false") {
if (getSetting("spellCheckerEnabled") === "false") {
return;
}
getDictionary(localStorage.trivabbleBoardLang, checkSpelling);
getDictionary(getSetting("trivabbleBoardLang"), checkSpelling);
}
function checkSpelling(dictionary) {
@ -1857,7 +1859,7 @@
return;
}
if ((laserTouch.last !== cell) || (Date.now() - laserTouch.date > localStorage.doubleTapDuration)) {
if ((laserTouch.last !== cell) || (Date.now() - laserTouch.date > getSetting("doubleTapDuration"))) {
laserTouch.last = cell;
laserTouch.date = Date.now();
return;
@ -1871,7 +1873,7 @@
placeholder.classList.add("laser-highlighting");
placeholder.classList.add("laser-highlight");
const sixthFlashLightDuration = Number(localStorage.flashLightDuration) / 6;
const sixthFlashLightDuration = getSetting("flashLightDuration") / 6;
function on() {
placeholder.classList.add("laser-highlight");
@ -1955,34 +1957,34 @@
msgSound = document.getElementById("msg-sound");
tilesSound.onclick = function () {
localStorage.trivabbleTileSound = tilesSound.checked;
setSetting("trivabbleTileSound", tilesSound.checked);
};
msgSound.onclick = function () {
localStorage.trivabbleMsgSound = msgSound.checked;
setSetting("trivabbleMsgSound", msgSound.checked);
};
if (Object.prototype.hasOwnProperty.call(localStorage, "trivabbleMsgSound")) {
msgSound.checked = localStorage.trivabbleMsgSound === "true";
if (isSetting("trivabbleMsgSound")) {
msgSound.checked = getSetting("trivabbleMsgSound");
} else {
localStorage.trivabbleMsgSound = msgSound.checked;
setSetting("trivabbleMsgSound", msgSound.checked);
}
if (Object.prototype.hasOwnProperty.call(localStorage, "trivabbleTileSound")) {
tilesSound.checked = localStorage.trivabbleTileSound === "true";
if (isSetting("trivabbleTileSound")) {
tilesSound.checked = getSetting("trivabbleTileSound");
} else {
localStorage.trivabbleTilesSound = tilesSound.checked;
setSetting("trivabbleTilesSound", tilesSound.checked);
}
}
function initSpellChecker() {
if (Conf.ENABLE_SPELL_CHECKER) {
if (getSetting("ENABLE_SPELL_CHECKER")) {
document.getElementById("enable-spell-checker-outer").style.display = "";
}
document.getElementById("enable-spell-checker").onclick = spellCheckerSettingChecked;
if (!Conf.ENABLE_SPELL_CHECKER || localStorage.spellCheckerEnabled === "false") {
if (!getSetting("ENABLE_SPELL_CHECKER") || !getSetting("spellCheckerEnabled")) {
document.getElementById("check-spelling").style.display = "none";
document.getElementById("enable-spell-checker").checked = false;
document.getElementById("info-spell-checking").style.display = "none";
@ -2014,46 +2016,43 @@
document.body.addEventListener("dblclick", laserDblClick);
document.body.addEventListener("touchstart", laserTouch);
if (!Object.prototype.hasOwnProperty.call(localStorage, "doubleTapDuration")) {
localStorage.doubleTapDuration = Conf.DOUBLE_TAP_DURATIONS[Math.floor(Conf.DOUBLE_TAP_DURATIONS.length / 2)];
if (!isSetting("doubleTapDuration")) {
setSetting("doubleTapDuration", getSetting("DOUBLE_TAP_DURATIONS")[Math.floor(getSetting("DOUBLE_TAP_DURATIONS").length / 2)]);
}
const doubleTapDuration = document.getElementById("double-tap-duration");
if (doubleTapDuration) {
doubleTapDuration.max = Conf.DOUBLE_TAP_DURATIONS.length - 1;
doubleTapDuration.value = translateDuration(localStorage.doubleTapDuration, Conf.DOUBLE_TAP_DURATIONS);
doubleTapDuration.max = getSetting("DOUBLE_TAP_DURATIONS").length - 1;
doubleTapDuration.value = translateDuration(getSetting("doubleTapDuration"), getSetting("DOUBLE_TAP_DURATIONS"));
doubleTapDuration.onchange = function () {
localStorage.doubleTapDuration = translateDuration(
setSetting("doubleTapDuration", translateDuration(
document.getElementById("double-tap-duration").value,
Conf.DOUBLE_TAP_DURATIONS
);
getSetting("DOUBLE_TAP_DURATIONS")
));
};
}
if (!Object.prototype.hasOwnProperty.call(localStorage, "flashLightDuration")) {
localStorage.flashLightDuration = Conf.FLASH_LIGHT_DURATIONS[Math.floor(Conf.FLASH_LIGHT_DURATIONS.length / 2)];
if (!isSetting("flashLightDuration")) {
setSetting("flashLightDuration", getSetting("FLASH_LIGHT_DURATIONS")[Math.floor(getSetting("FLASH_LIGHT_DURATIONS").length / 2)]);
}
const flashLightDuration = document.getElementById("flash-light-duration");
if (flashLightDuration) {
flashLightDuration.max = Conf.FLASH_LIGHT_DURATIONS.length - 1;
flashLightDuration.value = translateDuration(localStorage.flashLightDuration, Conf.FLASH_LIGHT_DURATIONS);
flashLightDuration.max = getSetting("FLASH_LIGHT_DURATIONS").length - 1;
flashLightDuration.value = translateDuration(getSetting("flashLightDuration"), getSetting("FLASH_LIGHT_DURATIONS"));
flashLightDuration.onchange = function () {
localStorage.flashLightDuration = translateDuration(
setSetting("flashLightDuration", translateDuration(
document.getElementById("flash-light-duration").value,
Conf.FLASH_LIGHT_DURATIONS
);
getSetting("FLASH_LIGHT_DURATIONS")
));
};
}
if (!Object.prototype.hasOwnProperty.call(localStorage, "flashLightColor")) {
localStorage.flashLightColor = Conf.FLASH_LIGHT_COLOR;
}
const flashLightColor = document.getElementById("flash-light-color");
if (flashLightColor) {
flashLightColor.value = localStorage.flashLightColor;
flashLightColor.value = getSetting("FLASH_LIGHT_COLOR");
flashLightColor.onchange = function () {
localStorage.flashLightColor = document.getElementById("flash-light-color").value;
setSetting("FLASH_LIGHT_COLOR", document.getElementById("flash-light-color").value);
const style = document.createElement("style");
style.textContent = ".laser-highlight {background-color: " + localStorage.flashLightColor + ";}";
style.textContent = ".laser-highlight {background-color: " + getSetting("FLASH_LIGHT_COLOR") + ";}";
document.body.appendChild(style);
};
flashLightColor.onchange();
@ -2061,14 +2060,14 @@
}
function repromptName(f) {
if (localStorage.trivabblePlayerName && localStorage.trivabblePlayerName.trim()) {
if (getSetting("trivabblePlayerName") && getSetting("trivabblePlayerName").trim()) {
f();
} else {
myPrompt(
_("It seems your did not give your name. You need to do it for the game to run properly."),
function (name) {
if (name && name.trim()) {
localStorage.trivabblePlayerName = name.trim();
setSetting("trivabblePlayerName", name.trim());
}
repromptName(f);
@ -2117,11 +2116,11 @@
);
});
boardLangSelect.value = localStorage.trivabbleBoardLang;
boardLangSelect.value = getSetting("trivabbleBoardLang");
}
function langSelectionChange(e) {
localStorage.trivabbleLang = e.target.value;
setSetting("trivabbleLang", e.target.value);
location.reload();
}
@ -2160,12 +2159,12 @@
}
function initGame() {
if (!localStorage.trivabblePlayerName) {
if (!getSetting("trivabblePlayerName")) {
myPrompt(
_("Hello! To begin, enter your name. Your adversaries will see this name when you play with them."),
function (name) {
if (name && name.trim()) {
localStorage.trivabblePlayerName = name;
setSetting("trivabblePlayerName", name);
}
repromptName(initGame);
}
@ -2174,7 +2173,7 @@
return;
}
name.textContent = localStorage.trivabblePlayerName;
name.textContent = getSetting("trivabblePlayerName");
const letters = "ABCDEFGHIJKLMNO";
@ -2249,19 +2248,19 @@
board.lastChild.appendChild(board.rows[0].cells[0].cloneNode(false));
if (localStorage.trivabbleGameNumber) {
document.getElementById("number").textContent = localStorage.trivabbleGameNumber;
if (getSetting("trivabbleGameNumber")) {
document.getElementById("number").textContent = getSetting("trivabbleGameNumber");
}
if (localStorage.trivabbleBoardLang) {
document.getElementById("board-lang").value = localStorage.trivabbleBoardLang;
if (getSetting("trivabbleBoardLang")) {
document.getElementById("board-lang").value = getSetting("trivabbleBoardLang");
}
startGame(localStorage.trivabbleGameNumber);
startGame(getSetting("trivabbleGameNumber"));
}
function initLang() {
const lang = libD.lang = localStorage.trivabbleLang || libD.lang;
const lang = libD.lang = getSetting("trivabbleLang") || libD.lang;
const langSel = document.getElementById("select-lang");
langSel.value = lang;