Filter stale sortie save ids
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
@@ -811,9 +811,12 @@ function normalizeCampaignState(state: Partial<CampaignState>): 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<unknown>(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<string, unknown>) {
|
||||
function normalizeSortieItemAssignments(assignments?: Record<string, unknown>, allowedUnitIds?: Set<string>) {
|
||||
return Object.entries(assignments ?? {}).reduce<CampaignSortieItemAssignments>((next, [unitId, stocks]) => {
|
||||
if (!unitId || (allowedUnitIds && !allowedUnitIds.has(unitId))) {
|
||||
return next;
|
||||
}
|
||||
|
||||
const unitStocks = Object.entries(recordOrEmpty(stocks)).reduce<Record<string, number>>((nextStocks, [itemId, amount]) => {
|
||||
const normalizedAmount = normalizeNonNegativeInteger(amount);
|
||||
if (itemId && normalizedAmount > 0) {
|
||||
@@ -1011,7 +1026,7 @@ function normalizeSortieItemAssignments(assignments?: Record<string, unknown>) {
|
||||
return nextStocks;
|
||||
}, {});
|
||||
|
||||
if (unitId && Object.keys(unitStocks).length > 0) {
|
||||
if (Object.keys(unitStocks).length > 0) {
|
||||
next[unitId] = unitStocks;
|
||||
}
|
||||
return next;
|
||||
|
||||
Reference in New Issue
Block a user