Normalize malformed battle history saves
This commit is contained in:
@@ -811,7 +811,7 @@ function normalizeCampaignState(state: Partial<CampaignState>): CampaignState {
|
||||
normalized.sortieFormationAssignments = normalizeSortieFormationAssignments(recordOrEmpty(normalized.sortieFormationAssignments));
|
||||
normalized.sortieItemAssignments = normalizeSortieItemAssignments(recordOrEmpty(normalized.sortieItemAssignments));
|
||||
normalized.reserveTrainingFocus = normalizeReserveTrainingFocusId(normalized.reserveTrainingFocus);
|
||||
normalized.battleHistory = recordOrEmpty<CampaignBattleSettlement>(normalized.battleHistory);
|
||||
normalized.battleHistory = normalizeBattleHistory(normalized.battleHistory);
|
||||
normalized.firstBattleReport = normalizeFirstBattleReport(normalized.firstBattleReport);
|
||||
return normalized;
|
||||
}
|
||||
@@ -847,6 +847,53 @@ function normalizeInventory(value: unknown) {
|
||||
}, {});
|
||||
}
|
||||
|
||||
function normalizeBattleHistory(value: unknown) {
|
||||
return Object.entries(recordOrEmpty<unknown>(value)).reduce<Record<string, CampaignBattleSettlement>>((history, [key, settlement]) => {
|
||||
if (!isCampaignBattleSettlement(settlement)) {
|
||||
return history;
|
||||
}
|
||||
|
||||
history[key] = {
|
||||
battleId: settlement.battleId,
|
||||
battleTitle: settlement.battleTitle,
|
||||
outcome: settlement.outcome,
|
||||
rewardGold: normalizeNonNegativeInteger(settlement.rewardGold),
|
||||
itemRewards: uniqueStrings(settlement.itemRewards),
|
||||
campaignRewards: cloneCampaignRewardSnapshot(settlement.campaignRewards),
|
||||
objectives: settlement.objectives.map((objective) => ({ ...objective })),
|
||||
units: settlement.units.map((unit) => ({
|
||||
...unit,
|
||||
equipment: recordOrEmpty(unit.equipment) as UnitData['equipment']
|
||||
})),
|
||||
bonds: settlement.bonds.map((bond) => ({ ...bond })),
|
||||
reserveTraining: settlement.reserveTraining?.map((entry) => ({ ...entry })),
|
||||
completedAt: settlement.completedAt
|
||||
};
|
||||
return history;
|
||||
}, {});
|
||||
}
|
||||
|
||||
function isCampaignBattleSettlement(value: unknown): value is CampaignBattleSettlement {
|
||||
if (!isPlainObject(value)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const settlement = value as Partial<CampaignBattleSettlement>;
|
||||
return (
|
||||
typeof settlement.battleId === 'string' &&
|
||||
settlement.battleId.length > 0 &&
|
||||
typeof settlement.battleTitle === 'string' &&
|
||||
settlement.battleTitle.length > 0 &&
|
||||
(settlement.outcome === 'victory' || settlement.outcome === 'defeat') &&
|
||||
Array.isArray(settlement.itemRewards) &&
|
||||
Array.isArray(settlement.objectives) &&
|
||||
Array.isArray(settlement.units) &&
|
||||
Array.isArray(settlement.bonds) &&
|
||||
typeof settlement.completedAt === 'string' &&
|
||||
settlement.completedAt.length > 0
|
||||
);
|
||||
}
|
||||
|
||||
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]) => {
|
||||
@@ -1104,13 +1151,26 @@ function cloneCampaignRewardSnapshot(rewards?: CampaignRewardSnapshot): Campaign
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const rewardRecord = recordOrEmpty(rewards);
|
||||
return {
|
||||
supplies: [...(rewards.supplies ?? [])],
|
||||
equipment: [...(rewards.equipment ?? [])],
|
||||
reputation: [...(rewards.reputation ?? [])],
|
||||
recruits: (rewards.recruits ?? []).map((recruit) => ({ ...recruit })),
|
||||
unlocks: (rewards.unlocks ?? []).map((unlock) => ({ ...unlock })),
|
||||
note: rewards.note
|
||||
supplies: uniqueStrings(rewardRecord.supplies),
|
||||
equipment: uniqueStrings(rewardRecord.equipment),
|
||||
reputation: uniqueStrings(rewardRecord.reputation),
|
||||
recruits: arrayOrEmpty<unknown>(rewardRecord.recruits)
|
||||
.filter(isPlainObject)
|
||||
.map((recruit) => ({
|
||||
unitId: typeof recruit.unitId === 'string' ? recruit.unitId : '',
|
||||
name: typeof recruit.name === 'string' ? recruit.name : ''
|
||||
}))
|
||||
.filter((recruit) => recruit.unitId && recruit.name),
|
||||
unlocks: arrayOrEmpty<unknown>(rewardRecord.unlocks)
|
||||
.filter(isPlainObject)
|
||||
.map((unlock) => ({
|
||||
battleId: typeof unlock.battleId === 'string' ? unlock.battleId : '',
|
||||
title: typeof unlock.title === 'string' ? unlock.title : ''
|
||||
}))
|
||||
.filter((unlock) => unlock.battleId && unlock.title),
|
||||
note: typeof rewardRecord.note === 'string' ? rewardRecord.note : undefined
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user