From 1bec723d4f8dbfbbc22b474f0212ea5a5b2c033c Mon Sep 17 00:00:00 2001 From: Azgaar Date: Sat, 9 Apr 2022 20:18:25 +0500 Subject: [PATCH] style uploadCulturesData, fix removed issue --- modules/io/formats.js | 23 ++++++++------ modules/ui/cultures-editor.js | 56 +++++++++++++++++------------------ 2 files changed, 42 insertions(+), 37 deletions(-) diff --git a/modules/io/formats.js b/modules/io/formats.js index 329d85db..439e124e 100644 --- a/modules/io/formats.js +++ b/modules/io/formats.js @@ -1,19 +1,24 @@ -"use strict" +"use strict"; window.Formats = (function () { - async function csvParser (file, separator=",") { + async function csvParser(file, separator = ",") { const txt = await file.text(); const rows = txt.split("\n"); - const headers = rows.shift().split(separator).map(x => x.toLowerCase()); - const data = rows.filter(a => a.trim()!=="").map(r=>r.split(separator)); + const headers = rows + .shift() + .split(separator) + .map(x => x.toLowerCase()); + const data = rows.filter(a => a.trim() !== "").map(r => r.split(separator)); + return { headers, data, - iterator: function* (sortf){ - const dataset = sortf? this.data.sort(sortf):this.data; - for (const d of dataset) - yield Object.fromEntries(d.map((a, i) => [this.headers[i], a])); - }}; + iterator: function* (sortf) { + const dataset = sortf ? this.data.sort(sortf) : this.data; + for (const d of dataset) yield Object.fromEntries(d.map((a, i) => [this.headers[i], a])); + } + }; } + return {csvParser}; })(); diff --git a/modules/ui/cultures-editor.js b/modules/ui/cultures-editor.js index 65d5fd38..484346cc 100644 --- a/modules/ui/cultures-editor.js +++ b/modules/ui/cultures-editor.js @@ -39,7 +39,6 @@ function editCultures() { document.getElementById("culturesAdd").addEventListener("click", enterAddCulturesMode); document.getElementById("culturesExport").addEventListener("click", downloadCulturesData); document.getElementById("culturesImport").addEventListener("click", () => document.getElementById("culturesCSVToLoad").click()); - document.getElementById("culturesCSVToLoad").addEventListener("change", uploadCulturesData); function refreshCulturesEditor() { @@ -886,55 +885,56 @@ function editCultures() { exitCulturesManualAssignment("close"); exitAddCultureMode(); } + async function uploadCulturesData() { const csv = await Formats.csvParser(this.files[0]); this.value = ""; + const cultures = pack.cultures; const shapes = Object.keys(COA.shields.types) .map(type => Object.keys(COA.shields[type])) .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])) { let current; if (+c.id < cultures.length) { current = cultures[c.id]; + current.removed = false; 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 { - current = { - i: cultures.length, - center: ra(populated), - area: 0, - cells: 0, - origin: 0, - rural: 0, - urban: 0, - } - cultures.push(current) + current = {i: cultures.length, center: ra(populated), area: 0, cells: 0, origin: 0, rural: 0, urban: 0}; + cultures.push(current); } + 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.expansionism = +c.expansionism; 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); - current.base = nbi==-1? 0: nbi; + if (cultureTypes.includes(c.type)) current.type = c.type; + 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); - 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; + + drawCultures(); refreshCulturesEditor(); } }