mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2025-12-17 09:41:24 +01:00
style uploadCulturesData, fix removed issue
This commit is contained in:
parent
d18bb87ddf
commit
1bec723d4f
2 changed files with 42 additions and 37 deletions
|
|
@ -1,19 +1,24 @@
|
||||||
"use strict"
|
"use strict";
|
||||||
|
|
||||||
window.Formats = (function () {
|
window.Formats = (function () {
|
||||||
async function csvParser (file, separator=",") {
|
async function csvParser(file, separator = ",") {
|
||||||
const txt = await file.text();
|
const txt = await file.text();
|
||||||
const rows = txt.split("\n");
|
const rows = txt.split("\n");
|
||||||
const headers = rows.shift().split(separator).map(x => x.toLowerCase());
|
const headers = rows
|
||||||
const data = rows.filter(a => a.trim()!=="").map(r=>r.split(separator));
|
.shift()
|
||||||
|
.split(separator)
|
||||||
|
.map(x => x.toLowerCase());
|
||||||
|
const data = rows.filter(a => a.trim() !== "").map(r => r.split(separator));
|
||||||
|
|
||||||
return {
|
return {
|
||||||
headers,
|
headers,
|
||||||
data,
|
data,
|
||||||
iterator: function* (sortf){
|
iterator: function* (sortf) {
|
||||||
const dataset = sortf? this.data.sort(sortf):this.data;
|
const dataset = sortf ? this.data.sort(sortf) : this.data;
|
||||||
for (const d of dataset)
|
for (const d of dataset) yield Object.fromEntries(d.map((a, i) => [this.headers[i], a]));
|
||||||
yield Object.fromEntries(d.map((a, i) => [this.headers[i], a]));
|
|
||||||
}};
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
return {csvParser};
|
return {csvParser};
|
||||||
})();
|
})();
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,6 @@ function editCultures() {
|
||||||
document.getElementById("culturesAdd").addEventListener("click", enterAddCulturesMode);
|
document.getElementById("culturesAdd").addEventListener("click", enterAddCulturesMode);
|
||||||
document.getElementById("culturesExport").addEventListener("click", downloadCulturesData);
|
document.getElementById("culturesExport").addEventListener("click", downloadCulturesData);
|
||||||
document.getElementById("culturesImport").addEventListener("click", () => document.getElementById("culturesCSVToLoad").click());
|
document.getElementById("culturesImport").addEventListener("click", () => document.getElementById("culturesCSVToLoad").click());
|
||||||
|
|
||||||
document.getElementById("culturesCSVToLoad").addEventListener("change", uploadCulturesData);
|
document.getElementById("culturesCSVToLoad").addEventListener("change", uploadCulturesData);
|
||||||
|
|
||||||
function refreshCulturesEditor() {
|
function refreshCulturesEditor() {
|
||||||
|
|
@ -886,55 +885,56 @@ function editCultures() {
|
||||||
exitCulturesManualAssignment("close");
|
exitCulturesManualAssignment("close");
|
||||||
exitAddCultureMode();
|
exitAddCultureMode();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function uploadCulturesData() {
|
async function uploadCulturesData() {
|
||||||
const csv = await Formats.csvParser(this.files[0]);
|
const csv = await Formats.csvParser(this.files[0]);
|
||||||
this.value = "";
|
this.value = "";
|
||||||
|
|
||||||
const cultures = pack.cultures;
|
const cultures = pack.cultures;
|
||||||
const shapes = Object.keys(COA.shields.types)
|
const shapes = Object.keys(COA.shields.types)
|
||||||
.map(type => Object.keys(COA.shields[type]))
|
.map(type => Object.keys(COA.shields[type]))
|
||||||
.flat();
|
.flat();
|
||||||
const populated = pack.cells.pop.map((c, i) => c? i: null).filter(c => c);
|
|
||||||
cultures.forEach((item) => {if (item.i) item.removed = true});
|
const populated = pack.cells.pop.map((c, i) => (c ? i : null)).filter(c => c);
|
||||||
|
|
||||||
for (const c of csv.iterator((a, b) => +a[0] > +b[0])) {
|
for (const c of csv.iterator((a, b) => +a[0] > +b[0])) {
|
||||||
let current;
|
let current;
|
||||||
if (+c.id < cultures.length) {
|
if (+c.id < cultures.length) {
|
||||||
current = cultures[c.id];
|
current = cultures[c.id];
|
||||||
|
current.removed = false;
|
||||||
const ratio = current.urban / (current.rural + current.urban);
|
const ratio = current.urban / (current.rural + current.urban);
|
||||||
applyPopulationChange(current.rural, current.urban, c.population*(1 - ratio), c.population*ratio, +c.id);
|
applyPopulationChange(current.rural, current.urban, c.population * (1 - ratio), c.population * ratio, +c.id);
|
||||||
} else {
|
} else {
|
||||||
current = {
|
current = {i: cultures.length, center: ra(populated), area: 0, cells: 0, origin: 0, rural: 0, urban: 0};
|
||||||
i: cultures.length,
|
cultures.push(current);
|
||||||
center: ra(populated),
|
|
||||||
area: 0,
|
|
||||||
cells: 0,
|
|
||||||
origin: 0,
|
|
||||||
rural: 0,
|
|
||||||
urban: 0,
|
|
||||||
}
|
|
||||||
cultures.push(current)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
current.name = c.culture;
|
current.name = c.culture;
|
||||||
current.code = abbreviate(current.name, cultures.map(c => c.code));
|
current.code = abbreviate(
|
||||||
|
current.name,
|
||||||
|
cultures.map(c => c.code)
|
||||||
|
);
|
||||||
|
|
||||||
current.color = c.color;
|
current.color = c.color;
|
||||||
current.expansionism = +c.expansionism;
|
current.expansionism = +c.expansionism;
|
||||||
current.origin = +c.origin;
|
current.origin = +c.origin;
|
||||||
if (cultureTypes.includes(c.type))
|
|
||||||
current.type = c.type;
|
|
||||||
else
|
|
||||||
current.type = "Generic";
|
|
||||||
current.removed = false;
|
|
||||||
const shieldShape = c["emblems shape"].toLowerCase();
|
|
||||||
if (shapes.includes(shieldShape))
|
|
||||||
current.shield = shieldShape
|
|
||||||
else
|
|
||||||
current.shield = "heater";
|
|
||||||
|
|
||||||
const nbi = nameBases.findIndex(n => n.name==c.namesbase);
|
if (cultureTypes.includes(c.type)) current.type = c.type;
|
||||||
current.base = nbi==-1? 0: nbi;
|
else current.type = "Generic";
|
||||||
|
|
||||||
|
const shieldShape = c["emblems shape"].toLowerCase();
|
||||||
|
if (shapes.includes(shieldShape)) current.shield = shieldShape;
|
||||||
|
else current.shield = "heater";
|
||||||
|
|
||||||
|
const nameBaseIndex = nameBases.findIndex(n => n.name == c.namesbase);
|
||||||
|
current.base = nameBaseIndex === -1 ? 0 : nameBaseIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
const validId = cultures.filter(c => !c.removed).map(c => c.i);
|
const validId = cultures.filter(c => !c.removed).map(c => c.i);
|
||||||
cultures.forEach(item => item.origin = validId.includes(item.origin)? item.origin:0);
|
cultures.forEach(item => (item.origin = validId.includes(item.origin) ? item.origin : 0));
|
||||||
cultures[0].origin = null;
|
cultures[0].origin = null;
|
||||||
|
|
||||||
|
drawCultures();
|
||||||
refreshCulturesEditor();
|
refreshCulturesEditor();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue