fix the isOutdated function for versions past 1.99

This commit is contained in:
Azgaar 2024-08-26 03:48:58 +02:00
parent 8dd4d486f0
commit 6000f82ac9
3 changed files with 64 additions and 43 deletions

View file

@ -104,8 +104,6 @@ function showUploadErrorMessage(error, URL, random) {
function uploadMap(file, callback) { function uploadMap(file, callback) {
uploadMap.timeStart = performance.now(); uploadMap.timeStart = performance.now();
const OLDEST_SUPPORTED_VERSION = 0.7;
const currentVersion = parseFloat(version);
const fileReader = new FileReader(); const fileReader = new FileReader();
fileReader.onloadend = async function (fileLoadedEvent) { fileReader.onloadend = async function (fileLoadedEvent) {
@ -114,14 +112,14 @@ function uploadMap(file, callback) {
const result = fileLoadedEvent.target.result; const result = fileLoadedEvent.target.result;
const [mapData, mapVersion] = await parseLoadedResult(result); const [mapData, mapVersion] = await parseLoadedResult(result);
const isInvalid = !mapData || isNaN(mapVersion) || mapData.length < 26 || !mapData[5]; const isInvalid = !mapData || !isValidVersion(mapVersion) || mapData.length < 26 || !mapData[5];
const isUpdated = mapVersion === currentVersion; const isUpdated = compareVersions(mapVersion, version).isEqual;
const isAncient = mapVersion < OLDEST_SUPPORTED_VERSION; const isAncient = compareVersions(mapVersion, "0.7.00").isOlder;
const isNewer = mapVersion > currentVersion; const isNewer = compareVersions(mapVersion, version).isNewer;
const isOutdated = mapVersion < currentVersion; const isOutdated = compareVersions(mapVersion, version).isOlder;
if (isInvalid) return showUploadMessage("invalid", mapData, mapVersion); 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 (isAncient) return showUploadMessage("ancient", mapData, mapVersion);
if (isNewer) return showUploadMessage("newer", mapData, mapVersion); if (isNewer) return showUploadMessage("newer", mapData, mapVersion);
if (isOutdated) return showUploadMessage("outdated", mapData, mapVersion); if (isOutdated) return showUploadMessage("outdated", mapData, mapVersion);
@ -130,6 +128,23 @@ function uploadMap(file, callback) {
fileReader.readAsArrayBuffer(file); 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) { async function uncompress(compressedData) {
try { try {
const uncompressedStream = new Blob([compressedData]).stream().pipeThrough(new DecompressionStream("gzip")); 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 decoded = isDelimited ? resultAsString : decodeURIComponent(atob(resultAsString));
const mapData = decoded.split("\r\n"); 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]; return [mapData, mapVersion];
} catch (error) { } catch (error) {
// map file can be compressed with gzip // map file can be compressed with gzip
@ -167,21 +182,21 @@ async function parseLoadedResult(result) {
} }
function showUploadMessage(type, mapData, mapVersion) { function showUploadMessage(type, mapData, mapVersion) {
const archive = link("https://github.com/Azgaar/Fantasy-Map-Generator/wiki/Changelog", "archived version"); let message, title;
let message, title, canBeLoaded;
if (type === "invalid") { if (type === "invalid") {
message = `The file does not look like a valid save file.<br>Please check the data format`; message = "The file does not look like a valid save file.<br>Please check the data format";
title = "Invalid file"; title = "Invalid file";
canBeLoaded = false; } else if (type === "updated") {
parseLoadedData(mapData, mapVersion);
return;
} else if (type === "ancient") { } 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.<br>Please keep using an ${archive}`; message = `The map version you are trying to load (${mapVersion}) is too old and cannot be updated to the current version.<br>Please keep using an ${archive}`;
title = "Ancient file"; title = "Ancient file";
canBeLoaded = false;
} else if (type === "newer") { } else if (type === "newer") {
message = `The map version you are trying to load (${mapVersion}) is newer than the current version.<br>Please load the file in the appropriate version`; message = `The map version you are trying to load (${mapVersion}) is newer than the current version.<br>Please load the file in the appropriate version`;
title = "Newer file"; title = "Newer file";
canBeLoaded = false;
} else if (type === "outdated") { } else if (type === "outdated") {
INFO && console.info(`Loading map. Auto-update from ${mapVersion} to ${version}`); INFO && console.info(`Loading map. Auto-update from ${mapVersion} to ${version}`);
parseLoadedData(mapData, mapVersion); parseLoadedData(mapData, mapVersion);
@ -189,13 +204,14 @@ function showUploadMessage(type, mapData, mapVersion) {
} }
alertMessage.innerHTML = message; alertMessage.innerHTML = message;
const buttons = { $("#alert").dialog({
OK: function () { title,
$(this).dialog("close"); buttons: {
if (canBeLoaded) parseLoadedData(mapData, mapVersion); OK: function () {
$(this).dialog("close");
}
} }
}; });
$("#alert").dialog({title, buttons});
} }
async function parseLoadedData(data, mapVersion) { async function parseLoadedData(data, mapVersion) {

View file

@ -2,17 +2,17 @@
window.Zones = (function () { window.Zones = (function () {
const config = { const config = {
invasion: {modifier: 1.8, generate: addInvasion}, // invasion of enemy lands invasion: {quantity: 1.8, generate: addInvasion}, // invasion of enemy lands
rebels: {modifier: 1.6, generate: addRebels}, // rebels along a state border rebels: {quantity: 1.6, generate: addRebels}, // rebels along a state border
proselytism: {modifier: 1.6, generate: addProselytism}, // proselitism of organized religion proselytism: {quantity: 1.6, generate: addProselytism}, // proselitism of organized religion
crusade: {modifier: 1.6, generate: addCrusade}, // crusade on heresy lands crusade: {quantity: 1.6, generate: addCrusade}, // crusade on heresy lands
disease: {modifier: 1.8, generate: addDisease}, // disease starting in a random city disease: {quantity: 1.8, generate: addDisease}, // disease starting in a random city
disaster: {modifier: 1.4, generate: addDisaster}, // disaster starting in a random city disaster: {quantity: 1.2, generate: addDisaster}, // disaster starting in a random city
eruption: {modifier: 1.4, generate: addEruption}, // volcanic eruption aroung volcano eruption: {quantity: 1.4, generate: addEruption}, // volcanic eruption aroung volcano
avalanche: {modifier: 1.0, generate: addAvalanche}, // avalanche impacting highland road avalanche: {quantity: 1.0, generate: addAvalanche}, // avalanche impacting highland road
fault: {modifier: 1.4, generate: addFault}, // fault line in elevated areas fault: {quantity: 1.4, generate: addFault}, // fault line in elevated areas
flood: {modifier: 1.4, generate: addFlood}, // flood on river banks flood: {quantity: 1.4, generate: addFlood}, // flood on river banks
tsunami: {modifier: 1.2, generate: addTsunami} // tsunami starting near coast tsunami: {quantity: 1.2, generate: addTsunami} // tsunami starting near coast
}; };
const generate = function (globalModifier = 1) { const generate = function (globalModifier = 1) {
@ -22,12 +22,13 @@ window.Zones = (function () {
pack.zones = []; pack.zones = [];
Object.values(config).forEach(type => { 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++) { for (let i = 0; i < count; i++) {
type.generate(usedCells); type.generate(usedCells);
} }
}); });
console.table(pack.zones);
drawZones(); drawZones();
function drawZones() { function drawZones() {
@ -59,11 +60,11 @@ window.Zones = (function () {
if (!atWar.length) return; if (!atWar.length) return;
const invader = ra(atWar); 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 cells = pack.cells;
const cell = ra( 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; if (!cell) return;
@ -78,7 +79,7 @@ window.Zones = (function () {
cells.c[q].forEach(e => { cells.c[q].forEach(e => {
if (usedCells[e]) return; if (usedCells[e]) return;
if (cells.state[e] !== target) return; if (cells.state[e] !== target.i) return;
usedCells[e] = 1; usedCells[e] = 1;
queue.push(e); 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)"}); pack.zones.push({name, type: "Disaster", cells: cellsArray, color: "url(#hatch13)"});
} }

View file

@ -8,11 +8,8 @@ const version = "1.100.00"; // generator version, update each time
const loadingScreenVersion = document.getElementById("versionText"); const loadingScreenVersion = document.getElementById("versionText");
if (loadingScreenVersion) loadingScreenVersion.innerText = `v${version}`; if (loadingScreenVersion) loadingScreenVersion.innerText = `v${version}`;
const versionNumber = parseFloat(version); const storedVersion = localStorage.getItem("version");
const storedVersion = localStorage.getItem("version") ? parseFloat(localStorage.getItem("version")) : 0; if (isOutdated(storedVersion)) await clearCache();
const isOutdated = storedVersion !== versionNumber;
if (isOutdated) clearCache();
const showUpdate = storedVersion < versionNumber; const showUpdate = storedVersion < versionNumber;
if (showUpdate) setTimeout(showUpdateWindow, 6000); 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() { async function clearCache() {
const cacheNames = await caches.keys(); const cacheNames = await caches.keys();
Promise.all(cacheNames.map(cacheName => caches.delete(cacheName))); return Promise.all(cacheNames.map(cacheName => caches.delete(cacheName)));
} }
} }