Show message after importing biomes

This commit is contained in:
Wuzzy 2023-10-25 10:19:21 +02:00
parent 9713603cd5
commit ec7a07da11
2 changed files with 45 additions and 2 deletions

View File

@ -145,11 +145,13 @@ A Voronoi diagram is supposed to be here but for some reason it cannot be displa
<h2 class="configHeader"><span class="collapser" id="importHeaderLink"></span> Import</h2> <h2 class="configHeader"><span class="collapser" id="importHeaderLink"></span> Import</h2>
<div class="configFrame" id="importContainer"> <div class="configFrame" id="importContainer">
<form id="importForm"> <form id="importForm">
<p><b>WARNING</b>: Importing will replace all the biomes!</p>
<label for="inputImport">Put JSON text here:</label> <label for="inputImport">Put JSON text here:</label>
<br> <br>
<textarea id="inputImport" type="text"></textarea> <textarea id="inputImport" type="text"></textarea>
<br> <br>
<button id="inputImportSubmit" type="button">Import</button> <button id="inputImportSubmit" type="button">Import</button>
<div id="importResultOuter" hidden><br><div id="importResultMessage"></div>
</form> </form>
</div> </div>
</div> </div>

View File

@ -1709,9 +1709,14 @@ inputExportClear.onclick = function() {
} }
/* Import */ /* Import */
// TODO: Make import cleaner, error checking, messages
inputImportSubmit.onclick = function() { inputImportSubmit.onclick = function() {
let importMessage = function(message) {
importResultOuter.hidden = false;
importResultMessage.innerText = message;
}
let importStr = inputImport.value; let importStr = inputImport.value;
// Do the JSON parse
let reviver = function(key, value) { let reviver = function(key, value) {
if (key === "name" && ((typeof value) === "string")) { if (key === "name" && ((typeof value) === "string")) {
return value; return value;
@ -1725,15 +1730,45 @@ inputImportSubmit.onclick = function() {
return value; return value;
} }
} }
let parsedJSON = JSON.parse(importStr, reviver);
let parsedJSON;
try {
parsedJSON = JSON.parse(importStr, reviver);
} catch {
importMessage("Import failed. Not a valid JSON object.");
}
if (typeof parsedJSON !== "object") { if (typeof parsedJSON !== "object") {
importMessage("Import failed. Not a valid JSON object.");
return; return;
} }
// Populate the temporary newPoints that MAY
// set the biomePoints if successful
let newPoints = []; let newPoints = [];
lastBiomeID = 0; lastBiomeID = 0;
let fieldsToCheck = [
{ fieldName: "name", type: "string" },
{ fieldName: "heat_point", type: "number" },
{ fieldName: "humidity_point", type: "number" },
{ fieldName: "y_min", type: "number" },
{ fieldName: "y_max", type: "number" },
]
for (let p=0; p<parsedJSON.length; p++) { for (let p=0; p<parsedJSON.length; p++) {
let parsedPoint = parsedJSON[p]; let parsedPoint = parsedJSON[p];
// Type checking
for (let f=0; f<fieldsToCheck.length; f++) {
let field = fieldsToCheck[f].fieldName;
let wantType = fieldsToCheck[f].type;
let gotType = typeof parsedPoint[field];
if (gotType === "undefined") {
importMessage(`Import failed. attribute "${field}" of biome #${p} is undefined.`)
return;
} else if (gotType !== wantType) {
importMessage(`Import failed. attribute "${field}" of biome #${p} is of type "${gotType}" but "${wantType}" expected.`)
return;
}
}
let newPoint = { let newPoint = {
id: lastBiomeID, id: lastBiomeID,
name: parsedPoint.name, name: parsedPoint.name,
@ -1746,10 +1781,16 @@ inputImportSubmit.onclick = function() {
lastBiomeID++; lastBiomeID++;
newPoints.push(newPoint); newPoints.push(newPoint);
} }
// Replace the biomes
biomePoints = newPoints; biomePoints = newPoints;
repopulateBiomeSelector(); repopulateBiomeSelector();
updateWidgetStates(); updateWidgetStates();
draw(true); draw(true);
if (biomePoints.length === 1) {
importMessage("Import successful. 1 biome imported.");
} else {
importMessage(`Import successful. ${biomePoints.length} biomes imported.`);
}
} }
/* Events for collapsing/extending config section with the arrow thingie */ /* Events for collapsing/extending config section with the arrow thingie */