Merge branch 'upstream' into dev-submaps

This commit is contained in:
Mészáros Gergely 2022-04-13 17:29:41 +02:00
commit f99edae5db
4 changed files with 44 additions and 39 deletions

View file

@ -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
.shift()
.split(separator)
.map(x => x.toLowerCase());
const data = rows.filter(a => a.trim() !== "").map(r => r.split(separator)); 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};
})(); })();

View file

@ -459,7 +459,7 @@ async function parseLoadedData(data) {
invalidReligions.forEach(r => { invalidReligions.forEach(r => {
const invalidCells = cells.i.filter(i => cells.religion[i] === r); const invalidCells = cells.i.filter(i => cells.religion[i] === r);
invalidCells.forEach(i => (cells.religion[i] = 0)); invalidCells.forEach(i => (cells.religion[i] = 0));
ERROR && console.error("Data Integrity Check. Invalid religion", c, "is assigned to cells", invalidCells); ERROR && console.error("Data Integrity Check. Invalid religion", r, "is assigned to cells", invalidCells);
}); });
const invalidFeatures = [...new Set(cells.f)].filter(f => f && !pack.features[f]); const invalidFeatures = [...new Set(cells.f)].filter(f => f && !pack.features[f]);

View file

@ -74,7 +74,7 @@ window.Routes = (function () {
const ports = allPorts.filter(b => b.port === f); // all ports on the same feature const ports = allPorts.filter(b => b.port === f); // all ports on the same feature
if (!ports.length) return; if (!ports.length) return;
if (features[f].border) addOverseaRoute(f, ports[0]); if (features[f]?.border) addOverseaRoute(f, ports[0]);
// get inner-map routes // get inner-map routes
for (let s = 0; s < ports.length; s++) { for (let s = 0; s < ports.length; s++) {

View file

@ -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();
} }
} }