From 6000f82ac9a59232656395a85dc3e829c03dcce0 Mon Sep 17 00:00:00 2001 From: Azgaar Date: Mon, 26 Aug 2024 03:48:58 +0200 Subject: [PATCH] fix the isOutdated function for versions past 1.99 --- modules/io/load.js | 58 ++++++++++++++++++++++++-------------- modules/zones-generator.js | 33 +++++++++++----------- versioning.js | 16 +++++++---- 3 files changed, 64 insertions(+), 43 deletions(-) diff --git a/modules/io/load.js b/modules/io/load.js index 81786374..19a155c1 100644 --- a/modules/io/load.js +++ b/modules/io/load.js @@ -104,8 +104,6 @@ function showUploadErrorMessage(error, URL, random) { function uploadMap(file, callback) { uploadMap.timeStart = performance.now(); - const OLDEST_SUPPORTED_VERSION = 0.7; - const currentVersion = parseFloat(version); const fileReader = new FileReader(); fileReader.onloadend = async function (fileLoadedEvent) { @@ -114,14 +112,14 @@ function uploadMap(file, callback) { const result = fileLoadedEvent.target.result; const [mapData, mapVersion] = await parseLoadedResult(result); - const isInvalid = !mapData || isNaN(mapVersion) || mapData.length < 26 || !mapData[5]; - const isUpdated = mapVersion === currentVersion; - const isAncient = mapVersion < OLDEST_SUPPORTED_VERSION; - const isNewer = mapVersion > currentVersion; - const isOutdated = mapVersion < currentVersion; + const isInvalid = !mapData || !isValidVersion(mapVersion) || mapData.length < 26 || !mapData[5]; + const isUpdated = compareVersions(mapVersion, version).isEqual; + const isAncient = compareVersions(mapVersion, "0.7.00").isOlder; + const isNewer = compareVersions(mapVersion, version).isNewer; + const isOutdated = compareVersions(mapVersion, version).isOlder; if (isInvalid) return showUploadMessage("invalid", mapData, mapVersion); - if (isUpdated) return parseLoadedData(mapData); + if (isUpdated) return parseLoadedData("updated", mapData, mapVersion); if (isAncient) return showUploadMessage("ancient", mapData, mapVersion); if (isNewer) return showUploadMessage("newer", mapData, mapVersion); if (isOutdated) return showUploadMessage("outdated", mapData, mapVersion); @@ -130,6 +128,23 @@ function uploadMap(file, callback) { fileReader.readAsArrayBuffer(file); } +function isValidVersion(versionString) { + if (!versionString) return false; + const [major, minor, patch] = versionString.split("."); + return !isNaN(major) && !isNaN(minor) && !isNaN(patch); +} + +function compareVersions(version1, version2) { + const [major1, minor1, patch1] = version1.split("."); + const [major2, minor2, patch2] = version2.split("."); + + const isEqual = major1 === major2 && minor1 === minor2 && patch1 === patch2; + const isNewer = major1 > major2 || (major1 === major2 && (minor1 > minor2 || (minor1 === minor2 && patch1 > patch2))); + const isOlder = major1 < major2 || (major1 === major2 && (minor1 < minor2 || (minor1 === minor2 && patch1 < patch2))); + + return {isEqual, isNewer, isOlder}; +} + async function uncompress(compressedData) { try { const uncompressedStream = new Blob([compressedData]).stream().pipeThrough(new DecompressionStream("gzip")); @@ -154,7 +169,7 @@ async function parseLoadedResult(result) { const decoded = isDelimited ? resultAsString : decodeURIComponent(atob(resultAsString)); const mapData = decoded.split("\r\n"); - const mapVersion = parseFloat(mapData[0].split("|")[0] || mapData[0]); + const mapVersion = mapData[0].split("|")[0] || mapData[0]; return [mapData, mapVersion]; } catch (error) { // map file can be compressed with gzip @@ -167,21 +182,21 @@ async function parseLoadedResult(result) { } function showUploadMessage(type, mapData, mapVersion) { - const archive = link("https://github.com/Azgaar/Fantasy-Map-Generator/wiki/Changelog", "archived version"); - let message, title, canBeLoaded; + let message, title; if (type === "invalid") { - message = `The file does not look like a valid save file.
Please check the data format`; + message = "The file does not look like a valid save file.
Please check the data format"; title = "Invalid file"; - canBeLoaded = false; + } else if (type === "updated") { + parseLoadedData(mapData, mapVersion); + return; } else if (type === "ancient") { + const archive = link("https://github.com/Azgaar/Fantasy-Map-Generator/wiki/Changelog", "archived version"); message = `The map version you are trying to load (${mapVersion}) is too old and cannot be updated to the current version.
Please keep using an ${archive}`; title = "Ancient file"; - canBeLoaded = false; } else if (type === "newer") { message = `The map version you are trying to load (${mapVersion}) is newer than the current version.
Please load the file in the appropriate version`; title = "Newer file"; - canBeLoaded = false; } else if (type === "outdated") { INFO && console.info(`Loading map. Auto-update from ${mapVersion} to ${version}`); parseLoadedData(mapData, mapVersion); @@ -189,13 +204,14 @@ function showUploadMessage(type, mapData, mapVersion) { } alertMessage.innerHTML = message; - const buttons = { - OK: function () { - $(this).dialog("close"); - if (canBeLoaded) parseLoadedData(mapData, mapVersion); + $("#alert").dialog({ + title, + buttons: { + OK: function () { + $(this).dialog("close"); + } } - }; - $("#alert").dialog({title, buttons}); + }); } async function parseLoadedData(data, mapVersion) { diff --git a/modules/zones-generator.js b/modules/zones-generator.js index 6454e895..94914b09 100644 --- a/modules/zones-generator.js +++ b/modules/zones-generator.js @@ -2,17 +2,17 @@ window.Zones = (function () { const config = { - invasion: {modifier: 1.8, generate: addInvasion}, // invasion of enemy lands - rebels: {modifier: 1.6, generate: addRebels}, // rebels along a state border - proselytism: {modifier: 1.6, generate: addProselytism}, // proselitism of organized religion - crusade: {modifier: 1.6, generate: addCrusade}, // crusade on heresy lands - disease: {modifier: 1.8, generate: addDisease}, // disease starting in a random city - disaster: {modifier: 1.4, generate: addDisaster}, // disaster starting in a random city - eruption: {modifier: 1.4, generate: addEruption}, // volcanic eruption aroung volcano - avalanche: {modifier: 1.0, generate: addAvalanche}, // avalanche impacting highland road - fault: {modifier: 1.4, generate: addFault}, // fault line in elevated areas - flood: {modifier: 1.4, generate: addFlood}, // flood on river banks - tsunami: {modifier: 1.2, generate: addTsunami} // tsunami starting near coast + invasion: {quantity: 1.8, generate: addInvasion}, // invasion of enemy lands + rebels: {quantity: 1.6, generate: addRebels}, // rebels along a state border + proselytism: {quantity: 1.6, generate: addProselytism}, // proselitism of organized religion + crusade: {quantity: 1.6, generate: addCrusade}, // crusade on heresy lands + disease: {quantity: 1.8, generate: addDisease}, // disease starting in a random city + disaster: {quantity: 1.2, generate: addDisaster}, // disaster starting in a random city + eruption: {quantity: 1.4, generate: addEruption}, // volcanic eruption aroung volcano + avalanche: {quantity: 1.0, generate: addAvalanche}, // avalanche impacting highland road + fault: {quantity: 1.4, generate: addFault}, // fault line in elevated areas + flood: {quantity: 1.4, generate: addFlood}, // flood on river banks + tsunami: {quantity: 1.2, generate: addTsunami} // tsunami starting near coast }; const generate = function (globalModifier = 1) { @@ -22,12 +22,13 @@ window.Zones = (function () { pack.zones = []; Object.values(config).forEach(type => { - const count = rn(Math.random() * type.modifier * globalModifier); + const count = rn(Math.random() * type.quantity * globalModifier); for (let i = 0; i < count; i++) { type.generate(usedCells); } }); + console.table(pack.zones); drawZones(); function drawZones() { @@ -59,11 +60,11 @@ window.Zones = (function () { if (!atWar.length) return; const invader = ra(atWar); - const target = invader.diplomacy.findIndex(d => d === "Enemy"); + const target = invader.diplomacy.find(d => d === "Enemy"); const cells = pack.cells; const cell = ra( - cells.i.filter(i => cells.state[i] === target && cells.c[i].some(c => cells.state[c] === invader.i)) + cells.i.filter(i => cells.state[i] === target.i && cells.c[i].some(c => cells.state[c] === invader.i)) ); if (!cell) return; @@ -78,7 +79,7 @@ window.Zones = (function () { cells.c[q].forEach(e => { if (usedCells[e]) return; - if (cells.state[e] !== target) return; + if (cells.state[e] !== target.i) return; usedCells[e] = 1; queue.push(e); }); @@ -426,7 +427,7 @@ window.Zones = (function () { }); } - const name = getAdjective(burgs[cells.burg[cell]].name) + " Flood"; + const name = getAdjective(pack.burgs[cells.burg[cell]].name) + " Flood"; pack.zones.push({name, type: "Disaster", cells: cellsArray, color: "url(#hatch13)"}); } diff --git a/versioning.js b/versioning.js index 91aa8060..0c74a022 100644 --- a/versioning.js +++ b/versioning.js @@ -8,11 +8,8 @@ const version = "1.100.00"; // generator version, update each time const loadingScreenVersion = document.getElementById("versionText"); if (loadingScreenVersion) loadingScreenVersion.innerText = `v${version}`; - const versionNumber = parseFloat(version); - const storedVersion = localStorage.getItem("version") ? parseFloat(localStorage.getItem("version")) : 0; - - const isOutdated = storedVersion !== versionNumber; - if (isOutdated) clearCache(); + const storedVersion = localStorage.getItem("version"); + if (isOutdated(storedVersion)) await clearCache(); const showUpdate = storedVersion < versionNumber; if (showUpdate) setTimeout(showUpdateWindow, 6000); @@ -73,8 +70,15 @@ const version = "1.100.00"; // generator version, update each time }); } + function isOutdated(storedVersion) { + if (!storedVersion) return true; + const [major, minor, _patch] = version.split("."); + const [storedMajor, storedMinor, _storedPatch] = storedVersion.split("."); + return storedMajor !== major || storedMinor !== minor; // ignore patch version + } + async function clearCache() { const cacheNames = await caches.keys(); - Promise.all(cacheNames.map(cacheName => caches.delete(cacheName))); + return Promise.all(cacheNames.map(cacheName => caches.delete(cacheName))); } }