Harden malformed campaign save recovery

This commit is contained in:
2026-07-05 09:04:06 +09:00
parent d92d5f9061
commit 56b01a5c94
2 changed files with 89 additions and 13 deletions

View File

@@ -786,25 +786,58 @@ function normalizeCampaignState(state: Partial<CampaignState>): CampaignState {
...cloneCampaignState(state as CampaignState)
};
normalized.version = 1;
normalized.updatedAt = normalized.updatedAt || new Date().toISOString();
normalized.updatedAt = typeof normalized.updatedAt === 'string' && normalized.updatedAt ? normalized.updatedAt : new Date().toISOString();
normalized.activeSaveSlot = normalizeSlot(normalized.activeSaveSlot);
normalized.gold = normalized.gold ?? 0;
normalized.roster = (normalized.roster ?? []).map(cloneUnit);
normalized.bonds = (normalized.bonds ?? []).map(cloneBondSnapshot);
normalized.completedCampDialogues = [...new Set(normalized.completedCampDialogues ?? [])];
normalized.completedCampVisits = [...new Set(normalized.completedCampVisits ?? [])];
normalized.inventory = { ...(normalized.inventory ?? {}) };
normalized.selectedSortieUnitIds = [...new Set(normalized.selectedSortieUnitIds ?? [])];
normalized.sortieFormationAssignments = normalizeSortieFormationAssignments(normalized.sortieFormationAssignments);
normalized.sortieItemAssignments = normalizeSortieItemAssignments(normalized.sortieItemAssignments);
normalized.gold = normalizeNonNegativeInteger(normalized.gold);
normalized.roster = arrayOrEmpty<UnitData>(normalized.roster).map(cloneUnit);
normalized.bonds = arrayOrEmpty<CampBondSnapshot>(normalized.bonds).map(cloneBondSnapshot);
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));
normalized.reserveTrainingFocus = normalizeReserveTrainingFocusId(normalized.reserveTrainingFocus);
normalized.battleHistory = normalized.battleHistory ?? {};
if (normalized.firstBattleReport) {
normalized.battleHistory = recordOrEmpty<CampaignBattleSettlement>(normalized.battleHistory);
if (isPlainObject(normalized.firstBattleReport)) {
normalized.firstBattleReport = cloneReport(normalized.firstBattleReport);
} else {
normalized.firstBattleReport = undefined;
}
return normalized;
}
function arrayOrEmpty<T>(value: unknown): T[] {
return Array.isArray(value) ? value as T[] : [];
}
function isPlainObject(value: unknown): value is Record<string, unknown> {
return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
}
function recordOrEmpty<T = unknown>(value: unknown): Record<string, T> {
return isPlainObject(value) ? value as Record<string, T> : {};
}
function uniqueStrings(value: unknown): string[] {
return [...new Set(arrayOrEmpty<unknown>(value).filter((entry): entry is string => typeof entry === 'string' && entry.length > 0))];
}
function normalizeNonNegativeInteger(value: unknown) {
const numeric = Math.floor(Number(value));
return Number.isFinite(numeric) && numeric > 0 ? numeric : 0;
}
function normalizeInventory(value: unknown) {
return Object.entries(recordOrEmpty(value)).reduce<Record<string, number>>((inventory, [itemId, amount]) => {
const normalizedAmount = normalizeNonNegativeInteger(amount);
if (itemId && normalizedAmount > 0) {
inventory[itemId] = normalizedAmount;
}
return inventory;
}, {});
}
function normalizeSortieItemAssignments(assignments?: CampaignSortieItemAssignments) {
return Object.entries(assignments ?? {}).reduce<CampaignSortieItemAssignments>((next, [unitId, stocks]) => {
const unitStocks = Object.entries(stocks ?? {}).reduce<Record<string, number>>((nextStocks, [itemId, amount]) => {