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)}`
|
`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.clear();
|
||||||
storage.set(
|
storage.set(
|
||||||
campaignStorageKey,
|
campaignStorageKey,
|
||||||
@@ -269,7 +304,7 @@ try {
|
|||||||
const fallback = loadCampaignState();
|
const fallback = loadCampaignState();
|
||||||
assert(fallback.step === 'second-camp' && fallback.activeSaveSlot === 2, `Expected valid slotted save fallback: ${JSON.stringify(fallback)}`);
|
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 {
|
} finally {
|
||||||
await server.close();
|
await server.close();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -811,9 +811,12 @@ function normalizeCampaignState(state: Partial<CampaignState>): CampaignState {
|
|||||||
normalized.completedCampDialogues = uniqueStrings(normalized.completedCampDialogues);
|
normalized.completedCampDialogues = uniqueStrings(normalized.completedCampDialogues);
|
||||||
normalized.completedCampVisits = uniqueStrings(normalized.completedCampVisits);
|
normalized.completedCampVisits = uniqueStrings(normalized.completedCampVisits);
|
||||||
normalized.inventory = normalizeInventory(normalized.inventory);
|
normalized.inventory = normalizeInventory(normalized.inventory);
|
||||||
normalized.selectedSortieUnitIds = uniqueStrings(normalized.selectedSortieUnitIds);
|
const rosterUnitIds = normalizedRosterUnitIds(normalized.roster);
|
||||||
normalized.sortieFormationAssignments = normalizeSortieFormationAssignments(recordOrEmpty(normalized.sortieFormationAssignments));
|
const sortieUnitFilter = rosterUnitIds.size > 0 ? rosterUnitIds : undefined;
|
||||||
normalized.sortieItemAssignments = normalizeSortieItemAssignments(recordOrEmpty(normalized.sortieItemAssignments));
|
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.reserveTrainingFocus = normalizeReserveTrainingFocusId(normalized.reserveTrainingFocus);
|
||||||
normalized.battleHistory = normalizeBattleHistory(normalized.battleHistory);
|
normalized.battleHistory = normalizeBattleHistory(normalized.battleHistory);
|
||||||
normalized.latestBattleId = normalizeLatestBattleId(normalized.latestBattleId, 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))];
|
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) {
|
function normalizeNonNegativeInteger(value: unknown) {
|
||||||
const numeric = Math.floor(Number(value));
|
const numeric = Math.floor(Number(value));
|
||||||
return Number.isFinite(numeric) && numeric > 0 ? numeric : 0;
|
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]) => {
|
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 unitStocks = Object.entries(recordOrEmpty(stocks)).reduce<Record<string, number>>((nextStocks, [itemId, amount]) => {
|
||||||
const normalizedAmount = normalizeNonNegativeInteger(amount);
|
const normalizedAmount = normalizeNonNegativeInteger(amount);
|
||||||
if (itemId && normalizedAmount > 0) {
|
if (itemId && normalizedAmount > 0) {
|
||||||
@@ -1011,7 +1026,7 @@ function normalizeSortieItemAssignments(assignments?: Record<string, unknown>) {
|
|||||||
return nextStocks;
|
return nextStocks;
|
||||||
}, {});
|
}, {});
|
||||||
|
|
||||||
if (unitId && Object.keys(unitStocks).length > 0) {
|
if (Object.keys(unitStocks).length > 0) {
|
||||||
next[unitId] = unitStocks;
|
next[unitId] = unitStocks;
|
||||||
}
|
}
|
||||||
return next;
|
return next;
|
||||||
|
|||||||
Reference in New Issue
Block a user