From 6fa502432efd9d4bfa856eea58f18d29f5a5ec94 Mon Sep 17 00:00:00 2001 From: Wickedness Date: Sun, 5 Jul 2026 09:50:31 +0900 Subject: [PATCH] Filter stale sortie save ids --- .../verify-campaign-save-normalization.mjs | 37 ++++++++++++++++++- src/game/state/campaignState.ts | 25 ++++++++++--- 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/scripts/verify-campaign-save-normalization.mjs b/scripts/verify-campaign-save-normalization.mjs index 1265ecb..11ef7bd 100644 --- a/scripts/verify-campaign-save-normalization.mjs +++ b/scripts/verify-campaign-save-normalization.mjs @@ -118,6 +118,41 @@ try { `Expected malformed inventory and sortie assignments to normalize: ${JSON.stringify(malformed)}` ); + storage.clear(); + storage.set( + campaignStorageKey, + JSON.stringify({ + version: 1, + updatedAt: '2026-07-03T12:00:00.000Z', + step: 'third-camp', + activeSaveSlot: 1, + roster: [{ id: 'liu-bei', name: 'Liu Bei', faction: 'ally' }], + selectedSortieUnitIds: ['liu-bei', 'ghost-unit', 'guan-yu', 'liu-bei'], + sortieFormationAssignments: { + 'liu-bei': 'front', + 'ghost-unit': 'flank', + 'guan-yu': 'support', + broken: 'center' + }, + sortieItemAssignments: { + 'liu-bei': { bean: '2' }, + 'ghost-unit': { bean: '1' }, + 'guan-yu': { wine: '1' } + } + }) + ); + const staleSortie = loadCampaignState(); + assert( + staleSortie.selectedSortieUnitIds.length === 1 && + staleSortie.selectedSortieUnitIds[0] === 'liu-bei' && + staleSortie.sortieFormationAssignments['liu-bei'] === 'front' && + staleSortie.sortieFormationAssignments['ghost-unit'] === undefined && + staleSortie.sortieItemAssignments['liu-bei']?.bean === 2 && + staleSortie.sortieItemAssignments['ghost-unit'] === undefined && + staleSortie.sortieItemAssignments['guan-yu'] === undefined, + `Expected stale sortie ids to be filtered against the recovered roster: ${JSON.stringify(staleSortie)}` + ); + storage.clear(); storage.set( campaignStorageKey, @@ -269,7 +304,7 @@ try { const fallback = loadCampaignState(); assert(fallback.step === 'second-camp' && fallback.activeSaveSlot === 2, `Expected valid slotted save fallback: ${JSON.stringify(fallback)}`); - console.log('Verified campaign save normalization, malformed-field/bond/sortie-item/latest-history/detail recovery, unknown-step/report/history recovery, unsupported-version rejection, and slotted fallback.'); + console.log('Verified campaign save normalization, malformed-field/bond/sortie-roster/latest-history/detail recovery, unknown-step/report/history recovery, unsupported-version rejection, and slotted fallback.'); } finally { await server.close(); } diff --git a/src/game/state/campaignState.ts b/src/game/state/campaignState.ts index cb76aee..388f9ee 100644 --- a/src/game/state/campaignState.ts +++ b/src/game/state/campaignState.ts @@ -811,9 +811,12 @@ function normalizeCampaignState(state: Partial): CampaignState { normalized.completedCampDialogues = uniqueStrings(normalized.completedCampDialogues); normalized.completedCampVisits = uniqueStrings(normalized.completedCampVisits); normalized.inventory = normalizeInventory(normalized.inventory); - normalized.selectedSortieUnitIds = uniqueStrings(normalized.selectedSortieUnitIds); - normalized.sortieFormationAssignments = normalizeSortieFormationAssignments(recordOrEmpty(normalized.sortieFormationAssignments)); - normalized.sortieItemAssignments = normalizeSortieItemAssignments(recordOrEmpty(normalized.sortieItemAssignments)); + const rosterUnitIds = normalizedRosterUnitIds(normalized.roster); + const sortieUnitFilter = rosterUnitIds.size > 0 ? rosterUnitIds : undefined; + normalized.selectedSortieUnitIds = uniqueStrings(normalized.selectedSortieUnitIds) + .filter((unitId) => !sortieUnitFilter || sortieUnitFilter.has(unitId)); + normalized.sortieFormationAssignments = normalizeSortieFormationAssignments(recordOrEmpty(normalized.sortieFormationAssignments), sortieUnitFilter); + normalized.sortieItemAssignments = normalizeSortieItemAssignments(recordOrEmpty(normalized.sortieItemAssignments), sortieUnitFilter); normalized.reserveTrainingFocus = normalizeReserveTrainingFocusId(normalized.reserveTrainingFocus); normalized.battleHistory = normalizeBattleHistory(normalized.battleHistory); normalized.latestBattleId = normalizeLatestBattleId(normalized.latestBattleId, normalized.battleHistory); @@ -837,6 +840,14 @@ function uniqueStrings(value: unknown): string[] { return [...new Set(arrayOrEmpty(value).filter((entry): entry is string => typeof entry === 'string' && entry.length > 0))]; } +function normalizedRosterUnitIds(roster: UnitData[]) { + return new Set( + roster + .map((unit) => unit.id) + .filter((unitId): unitId is string => typeof unitId === 'string' && unitId.length > 0) + ); +} + function normalizeNonNegativeInteger(value: unknown) { const numeric = Math.floor(Number(value)); return Number.isFinite(numeric) && numeric > 0 ? numeric : 0; @@ -1001,8 +1012,12 @@ function normalizeReserveTrainingSnapshot(value: unknown): CampaignReserveTraini }; } -function normalizeSortieItemAssignments(assignments?: Record) { +function normalizeSortieItemAssignments(assignments?: Record, allowedUnitIds?: Set) { return Object.entries(assignments ?? {}).reduce((next, [unitId, stocks]) => { + if (!unitId || (allowedUnitIds && !allowedUnitIds.has(unitId))) { + return next; + } + const unitStocks = Object.entries(recordOrEmpty(stocks)).reduce>((nextStocks, [itemId, amount]) => { const normalizedAmount = normalizeNonNegativeInteger(amount); if (itemId && normalizedAmount > 0) { @@ -1011,7 +1026,7 @@ function normalizeSortieItemAssignments(assignments?: Record) { return nextStocks; }, {}); - if (unitId && Object.keys(unitStocks).length > 0) { + if (Object.keys(unitStocks).length > 0) { next[unitId] = unitStocks; } return next;