Recover stale latest battle ids

This commit is contained in:
2026-07-05 09:45:56 +09:00
parent 5c7cfed12e
commit 49b2b2300f
2 changed files with 16 additions and 1 deletions

View File

@@ -163,6 +163,7 @@ try {
version: 1, version: 1,
updatedAt: '2026-07-04T01:00:00.000Z', updatedAt: '2026-07-04T01:00:00.000Z',
step: 'fifth-camp', step: 'fifth-camp',
latestBattleId: 'missing-history-entry',
battleHistory: { battleHistory: {
'wrong-history-key': { 'wrong-history-key': {
battleId: 'first-battle-zhuo-commandery', battleId: 'first-battle-zhuo-commandery',
@@ -230,6 +231,10 @@ try {
malformedHistory.battleHistory['first-battle-zhuo-commandery']?.campaignRewards?.recruits.length === 1, malformedHistory.battleHistory['first-battle-zhuo-commandery']?.campaignRewards?.recruits.length === 1,
`Expected nested battle history rewards to normalize: ${JSON.stringify(malformedHistory)}` `Expected nested battle history rewards to normalize: ${JSON.stringify(malformedHistory)}`
); );
assert(
malformedHistory.latestBattleId === 'first-battle-zhuo-commandery',
`Expected stale latestBattleId to recover to the latest valid settlement: ${JSON.stringify(malformedHistory)}`
);
assert( assert(
malformedHistory.battleHistory['first-battle-zhuo-commandery']?.objectives.length === 1 && malformedHistory.battleHistory['first-battle-zhuo-commandery']?.objectives.length === 1 &&
malformedHistory.battleHistory['first-battle-zhuo-commandery']?.objectives[0].rewardGold === 80 && malformedHistory.battleHistory['first-battle-zhuo-commandery']?.objectives[0].rewardGold === 80 &&
@@ -264,7 +269,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/history-key/detail recovery, unknown-step/report/history recovery, unsupported-version rejection, and slotted 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.');
} finally { } finally {
await server.close(); await server.close();
} }

View File

@@ -816,6 +816,7 @@ function normalizeCampaignState(state: Partial<CampaignState>): CampaignState {
normalized.sortieItemAssignments = normalizeSortieItemAssignments(recordOrEmpty(normalized.sortieItemAssignments)); normalized.sortieItemAssignments = normalizeSortieItemAssignments(recordOrEmpty(normalized.sortieItemAssignments));
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.firstBattleReport = normalizeFirstBattleReport(normalized.firstBattleReport); normalized.firstBattleReport = normalizeFirstBattleReport(normalized.firstBattleReport);
return normalized; return normalized;
} }
@@ -863,6 +864,15 @@ function normalizeBattleHistory(value: unknown) {
}, {}); }, {});
} }
function normalizeLatestBattleId(value: unknown, battleHistory: Record<string, CampaignBattleSettlement>) {
if (typeof value === 'string' && value in battleHistory) {
return value;
}
return Object.values(battleHistory)
.sort((a, b) => String(b.completedAt).localeCompare(String(a.completedAt)))[0]?.battleId;
}
function normalizeCampaignBattleSettlement(value: unknown): CampaignBattleSettlement | undefined { function normalizeCampaignBattleSettlement(value: unknown): CampaignBattleSettlement | undefined {
if (!isPlainObject(value)) { if (!isPlainObject(value)) {
return undefined; return undefined;