}
}
- setConf("POLLING_DELAY", 2000);
- setConf("ENABLE_WEBSOCKETS", true);
- setConf("ENABLE_EVENT_SOURCE", true);
- setConf("MAX_WEBSOCKET_ERRORS", 1);
- setConf("APP_PATH", "");
- setConf("ENABLE_SPELL_CHECKER", false);
- setConf("DOUBLE_TAP_DURATIONS", [650, 1100, 1800, 3000, 5000]);
+ setConf("POLLING_DELAY", 2000);
+ setConf("ENABLE_WEBSOCKETS", true);
+ setConf("ENABLE_EVENT_SOURCE", true);
+ setConf("MAX_WEBSOCKET_ERRORS", 1);
+ setConf("APP_PATH", "");
+ setConf("ENABLE_MSG_SOUND", true);
+ setConf("ENABLE_TILE_SOUND", true);
+ setConf("ENABLE_SPELL_CHECKER", false);
+ setConf("DOUBLE_TAP_DURATIONS", [650, 1100, 1800, 3000, 5000]);
setConf("FLASH_LIGHT_DURATIONS", [800, 1600, 3200]);
- setConf("FLASH_LIGHT_COLOR", "#E3E");
- setConf("ENABLE_TIMER", true);
+ setConf("FLASH_LIGHT_COLOR", "#E3E");
++ setConf("ENABLE_TIMER", true);
+
+ function isSetting(key) {
+ return Object.prototype.hasOwnProperty.call(Conf, key) ||
+ Object.prototype.hasOwnProperty.call(localStorage, key);
+ }
+
+ function getSetting(key) {
+ let type;
+ let value;
+
+ /* get default value from configuration */
+ if (Object.prototype.hasOwnProperty.call(Conf, key)) {
+ value = Conf[key];
+ type = typeof value;
+ }
+
+ /* try to retrieve value from localstorage */
+ if (Object.prototype.hasOwnProperty.call(localStorage, key)) {
+ value = localStorage.getItem(key);
+
+ /* get type from localStorage if no default is set */
+ if (typeof type === "undefined") {
+ if (Object.prototype.hasOwnProperty.call(localStorage, key + "_type")) {
+ type = localStorage.getItem(key + "_type");
+ } else {
+ type = "string";
+ }
+ }
+
+ /* cast from string to type */
+ if (type === "boolean") {
+ value = (value === "true");
+ } else if (type === "number") {
+ value = Number(value);
+ } else if (type === "object") {
+ value = JSON.parse(value);
+ } else if (type === "string") {
+ value = String(value);
+ } else {
+ console.error("Unsupported type");
+ }
+ }
+
+ return value;
+ }
+
+ function setSetting(key, value) {
+ let type;
+
+ /* try to retrieve type from configuration */
+ if (Object.prototype.hasOwnProperty.call(Conf, key)) {
+ type = typeof Conf[key];
+ }
+
+ /* try to retrieve type from localstorage */
+ if (typeof type === "undefined") {
+ if (Object.prototype.hasOwnProperty.call(localStorage, key)) {
+ type = localStorage.getItem(key + "_type");
+ }
+ }
+
+ /* if not set type is defined from value */
+ if (typeof type === "undefined") {
+ type = typeof value;
+ }
+
+ /* storage value in localstorage */
+ if (type === typeof value) {
+ if (type === "object") {
+ value = JSON.stringify(value);
+ }
+ if ((type === "boolean") ||
+ (type === "number") ||
+ (type === "object") ||
+ (type === "string")) {
+ localStorage.setItem(key, value);
+
+ /* store type into localstorage if no default value in configuration */
+ if (!Object.prototype.hasOwnProperty.call(Conf, key)) {
+ localStorage.setItem(key + "_type", type);
+ }
+ } else {
+ console.error("Unsupported type");
+ }
+ } else {
+ console.error("incoherent type");
+ }
+ }
+
+ function migrateSetting(key, type) {
+ if (Object.prototype.hasOwnProperty.call(localStorage, key)) {
+ localStorage.setItem(key + "_type", type);
+ }
+ }
+ migrateSetting("doubleTapDuration", "number");
+ migrateSetting("flashLightDuration", "number");
+ migrateSetting("spellCheckerEnabled", "boolean");
+ migrateSetting("trivabbleGameNumber", "number");
+ migrateSetting("trivabbleBoardLang", "string");
+ migrateSetting("trivabbleGameNumber", "number");
+ migrateSetting("trivabbleLang", "string");
+ migrateSetting("trivabblePlayerName", "string");
const _ = (window.libD && libD.l10n) ? libD.l10n() : function (s) {
return s;
}
}
- localStorage.timerEnable = Conf.ENABLE_TIMER;
+ function timerString(delta) {
+ const d = Math.floor(delta / (60 * 60 * 24));
+ const h = Math.floor((delta % (60 * 60 * 24)) / (60 * 60));
+ const m = Math.floor((delta % (60 * 60)) / 60);
+ const s = Math.floor(delta % 60);
+ return ((d > 0) ? format(_("{0}d "), d) : "") +
+ ((h > 0) ? format(_("{0}h "), h) : "") +
+ format(_("{0}' {1}''"), m, s);
+ }
+
+ function timerDate() {
+ return Math.floor(Date.now() / 1000);
+ }
+
+ function initTimer() {
+ if (!Object.prototype.hasOwnProperty.call(localStorage, "timerTurnDate")) {
+ localStorage.timerTurnDate = timerDate();
+ }
+ if (!Object.prototype.hasOwnProperty.call(localStorage, "timerGlobalDate")) {
+ localStorage.timerGlobalDate = timerDate();
+ }
+ if (!Object.prototype.hasOwnProperty.call(localStorage, "timerEnable")) {
++ localStorage.timerEnable = getSetting("ENABLE_TIMER");
+ }
+ document.getElementById("enable-timer").onclick = function () {
+ setTimerState(document.getElementById("enable-timer").checked);
+ };
+ setTimerState(localStorage.timerEnable);
+ }
+
+ function setTimerState(state) {
+ if (state) {
+ document.getElementById("timer").style.display = "block";
+ document.getElementById("enable-timer").checked = true;
+ localStorage.timer = setInterval(function () {
+ document.getElementById("timer-turn").textContent = timerString(timerDate() - Number(localStorage.timerTurnDate));
+ document.getElementById("timer-global").textContent = timerString(timerDate() - Number(localStorage.timerGlobalDate));
+ }, 1000);
+ } else {
+ document.getElementById("timer").style.display = "none";
+ document.getElementById("enable-timer").checked = false;
+ if (Object.prototype.hasOwnProperty.call(localStorage, "timer")) {
+ clearInterval(localStorage.timer);
+ delete localStorage.timer;
+ }
+ localStorage.timerGlobalDate = localStorage.timerTurnDate = timerDate();
+ }
+ }
+
function repromptName(f) {
- if (localStorage.trivabblePlayerName && localStorage.trivabblePlayerName.trim()) {
+ if (getSetting("trivabblePlayerName") && getSetting("trivabblePlayerName").trim()) {
f();
} else {
myPrompt(