From 80ce9ba7caa00b21540547b5ee95f07330901979 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 19 Feb 2026 14:27:20 +0000 Subject: [PATCH] Refactor: update existing regiment note in-place; remove all regiment notes in one pass Co-authored-by: Azgaar <26469650+Azgaar@users.noreply.github.com> --- public/modules/military-generator.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/public/modules/military-generator.js b/public/modules/military-generator.js index 8d11f546..b16d84cb 100644 --- a/public/modules/military-generator.js +++ b/public/modules/military-generator.js @@ -246,12 +246,13 @@ window.Military = (function () { const expected = 3 * populationRate; // expected regiment size const mergeable = (n0, n1) => (!n0.s && !n1.s) || n0.u === n1.u; // check if regiments can be merged + // remove all existing regiment notes before regenerating + for (let i = notes.length - 1; i >= 0; i--) { + if (notes[i].id.startsWith("regiment")) notes.splice(i, 1); + } + // get regiments for each state valid.forEach(s => { - // remove outdated regiment notes before regenerating - for (let i = notes.length - 1; i >= 0; i--) { - if (notes[i].id.startsWith(`regiment${s.i}-`)) notes.splice(i, 1); - } s.military = createRegiments(s.temp.platoons, s); delete s.temp; // do not store temp data }); @@ -385,9 +386,13 @@ window.Military = (function () { const conflict = campaign ? ` during the ${campaign.name}` : ""; const legend = `Regiment was formed in ${year} ${options.era}${conflict}. ${station}${troops}`; const id = `regiment${s.i}-${r.i}`; - const existingIndex = notes.findIndex(n => n.id === id); - if (existingIndex !== -1) notes.splice(existingIndex, 1); - notes.push({id, name: r.name, legend}); + const existing = notes.find(n => n.id === id); + if (existing) { + existing.name = r.name; + existing.legend = legend; + } else { + notes.push({id, name: r.name, legend}); + } }; return {