From 5ac99d180de7a2348fc44b0bbc9647c8794f39ba Mon Sep 17 00:00:00 2001 From: Azgaar Date: Tue, 22 Oct 2024 14:45:25 +0200 Subject: [PATCH] chore: parse DEBUG setting as an object --- index.html | 6 +++--- main.js | 4 ++-- modules/io/cloud.js | 6 +++--- modules/io/load.js | 2 +- modules/renderers/draw-state-labels.js | 14 +++++++++----- modules/submap.js | 3 +-- utils/stringUtils.js | 8 ++++++++ versioning.js | 2 +- 8 files changed, 28 insertions(+), 17 deletions(-) diff --git a/index.html b/index.html index d14c9381..8f2705fb 100644 --- a/index.html +++ b/index.html @@ -8073,7 +8073,7 @@ - + @@ -8120,7 +8120,7 @@ - + @@ -8131,7 +8131,7 @@ - + diff --git a/main.js b/main.js index 7beab61f..c4279296 100644 --- a/main.js +++ b/main.js @@ -4,7 +4,7 @@ // set debug options const PRODUCTION = location.hostname && location.hostname !== "localhost" && location.hostname !== "127.0.0.1"; -const DEBUG = localStorage.getItem("debug"); +const DEBUG = JSON.safeParse(localStorage.getItem("debug")) || {}; const INFO = true; const TIME = true; const WARN = true; @@ -922,7 +922,7 @@ function calculateTemperatures() { const [, y] = grid.points[rowCellId]; const rowLatitude = mapCoordinates.latN - (y / graphHeight) * mapCoordinates.latT; // [90; -90] const tempSeaLevel = calculateSeaLevelTemp(rowLatitude); - DEBUG && console.info(`${rn(rowLatitude)}° sea temperature: ${rn(tempSeaLevel)}°C`); + DEBUG.temperature && console.info(`${rn(rowLatitude)}° sea temperature: ${rn(tempSeaLevel)}°C`); for (let cellId = rowCellId; cellId < rowCellId + grid.cellsX; cellId++) { const tempAltitudeDrop = getAltitudeTemperatureDrop(cells.h[cellId]); diff --git a/modules/io/cloud.js b/modules/io/cloud.js index b25b6a90..17ca92db 100644 --- a/modules/io/cloud.js +++ b/modules/io/cloud.js @@ -60,7 +60,7 @@ window.Cloud = (function () { async save(fileName, contents) { const resp = await this.call("filesUpload", {path: "/" + fileName, contents}); - DEBUG && console.info("Dropbox response:", resp); + DEBUG.cloud && console.info("Dropbox response:", resp); return true; }, @@ -104,7 +104,7 @@ window.Cloud = (function () { // Callback function for auth window async setDropBoxToken(token) { - DEBUG && console.info("Access token:", token); + DEBUG.cloud && console.info("Access token:", token); setToken(this.name, token); await this.connect(token); this.authWindow.close(); @@ -131,7 +131,7 @@ window.Cloud = (function () { allow_download: true }; const resp = await this.call("sharingCreateSharedLinkWithSettings", {path, settings}); - DEBUG && console.info("Dropbox link object:", resp.result); + DEBUG.cloud && console.info("Dropbox link object:", resp.result); return resp.result.url; } }; diff --git a/modules/io/load.js b/modules/io/load.js index cd00b28c..05819ce7 100644 --- a/modules/io/load.js +++ b/modules/io/load.js @@ -13,7 +13,7 @@ async function quickLoad() { async function loadFromDropbox() { const mapPath = byId("loadFromDropboxSelect")?.value; - DEBUG && console.info("Loading map from Dropbox:", mapPath); + console.info("Loading map from Dropbox:", mapPath); const blob = await Cloud.providers.dropbox.load(mapPath); uploadMap(blob); } diff --git a/modules/renderers/draw-state-labels.js b/modules/renderers/draw-state-labels.js index ab49437d..d30d185c 100644 --- a/modules/renderers/draw-state-labels.js +++ b/modules/renderers/draw-state-labels.js @@ -47,8 +47,10 @@ function drawStateLabels(list) { const pathPoints = [[ray1.x, ray1.y], state.pole, [ray2.x, ray2.y]]; if (ray1.x > ray2.x) pathPoints.reverse(); - DEBUG && drawPoint(state.pole, {color: "black", radius: 1}); - DEBUG && drawPath(pathPoints, {color: "black", width: 0.2}); + if (DEBUG.stateLabels) { + drawPoint(state.pole, {color: "black", radius: 1}); + drawPath(pathPoints, {color: "black", width: 0.2}); + } labelPaths.push([state.i, pathPoints]); } @@ -163,9 +165,11 @@ function drawStateLabels(list) { const offset1 = [x + -dy * offset, y + dx * offset]; const offset2 = [x + dy * offset, y + -dx * offset]; - DEBUG && drawPoint([x, y], {color: isInsideState(x, y) ? "blue" : "red", radius: 0.8}); - DEBUG && drawPoint(offset1, {color: isInsideState(...offset1) ? "blue" : "red", radius: 0.4}); - DEBUG && drawPoint(offset2, {color: isInsideState(...offset2) ? "blue" : "red", radius: 0.4}); + if (DEBUG.stateLabels) { + drawPoint([x, y], {color: isInsideState(x, y) ? "blue" : "red", radius: 0.8}); + drawPoint(offset1, {color: isInsideState(...offset1) ? "blue" : "red", radius: 0.4}); + drawPoint(offset2, {color: isInsideState(...offset2) ? "blue" : "red", radius: 0.4}); + } const inState = isInsideState(x, y) && isInsideState(...offset1) && isInsideState(...offset2); if (!inState) break; diff --git a/modules/submap.js b/modules/submap.js index 2fb4582f..3c783fb0 100644 --- a/modules/submap.js +++ b/modules/submap.js @@ -31,7 +31,6 @@ window.Submap = (function () { seed = parentMap.seed; Math.random = aleaPRNG(seed); INFO && console.group("SubMap with seed: " + seed); - DEBUG && console.info("Using Options:", options); applyGraphSize(); grid = generateGrid(); @@ -373,7 +372,7 @@ window.Submap = (function () { b.removed = true; return; } - DEBUG && console.info(`Moving ${b.name} from ${cityCell} to ${newCell} near ${neighbor}.`); + [b.x, b.y] = b.port ? getCloseToEdgePoint(newCell, neighbor) : cells.p[newCell]; if (b.port) b.port = cells.f[neighbor]; // copy feature number b.cell = newCell; diff --git a/utils/stringUtils.js b/utils/stringUtils.js index 6325d278..1027ee8f 100644 --- a/utils/stringUtils.js +++ b/utils/stringUtils.js @@ -56,3 +56,11 @@ JSON.isValid = str => { } return true; }; + +JSON.safeParse = str => { + try { + return JSON.parse(str); + } catch (e) { + return null; + } +}; diff --git a/versioning.js b/versioning.js index acb67e2b..076de37c 100644 --- a/versioning.js +++ b/versioning.js @@ -12,7 +12,7 @@ * * Example: 1.102.2 -> Major version 1, Minor version 102, Patch version 2 */ -const VERSION = "1.105.18"; +const VERSION = "1.105.19"; if (parseMapVersion(VERSION) !== VERSION) alert("versioning.js: Invalid format or parsing function"); {