Harden campaign save timestamp recovery
This commit is contained in:
@@ -831,7 +831,7 @@ function normalizeCampaignState(state: Partial<CampaignState>): CampaignState {
|
||||
...cloneCampaignState(state as CampaignState)
|
||||
};
|
||||
normalized.version = 1;
|
||||
normalized.updatedAt = typeof normalized.updatedAt === 'string' && normalized.updatedAt ? normalized.updatedAt : new Date().toISOString();
|
||||
normalized.updatedAt = normalizeCampaignTimestamp(normalized.updatedAt) ?? new Date().toISOString();
|
||||
normalized.step = normalizeCampaignStep(normalized.step);
|
||||
normalized.activeSaveSlot = normalizeSlot(normalized.activeSaveSlot);
|
||||
normalized.gold = normalizeNonNegativeInteger(normalized.gold);
|
||||
@@ -899,6 +899,23 @@ function normalizeNonNegativeInteger(value: unknown) {
|
||||
return Number.isFinite(numeric) && numeric > 0 ? numeric : 0;
|
||||
}
|
||||
|
||||
function normalizeCampaignTimestamp(value: unknown) {
|
||||
if (typeof value !== 'string' || value.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return campaignTimestampMs(value) === undefined ? undefined : value;
|
||||
}
|
||||
|
||||
function campaignTimestampMs(value: unknown) {
|
||||
if (typeof value !== 'string' || value.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const timestamp = Date.parse(value);
|
||||
return Number.isFinite(timestamp) ? timestamp : undefined;
|
||||
}
|
||||
|
||||
function normalizeInventory(value: unknown) {
|
||||
return Object.entries(recordOrEmpty(value)).reduce<Record<string, number>>((inventory, [itemId, amount]) => {
|
||||
const normalizedItemId = itemId.replace(/\s+/g, ' ').trim();
|
||||
@@ -930,7 +947,7 @@ function normalizeLatestBattleId(value: unknown, battleHistory: Record<string, C
|
||||
}
|
||||
|
||||
return Object.values(battleHistory)
|
||||
.sort((a, b) => String(b.completedAt).localeCompare(String(a.completedAt)))[0]?.battleId;
|
||||
.sort((a, b) => (campaignTimestampMs(b.completedAt) ?? 0) - (campaignTimestampMs(a.completedAt) ?? 0))[0]?.battleId;
|
||||
}
|
||||
|
||||
function filterFirstBattleReportRosterReferences(report: FirstBattleReport, rosterUnitIds: Set<string>): FirstBattleReport {
|
||||
@@ -951,14 +968,14 @@ function normalizeCampaignBattleSettlement(value: unknown): CampaignBattleSettle
|
||||
|
||||
const settlement = value as Partial<CampaignBattleSettlement>;
|
||||
const battleId = typeof settlement.battleId === 'string' ? settlement.battleId : '';
|
||||
const completedAt = normalizeCampaignTimestamp(settlement.completedAt);
|
||||
if (
|
||||
battleId.length === 0 ||
|
||||
!(battleId in battleScenarios) ||
|
||||
typeof settlement.battleTitle !== 'string' ||
|
||||
settlement.battleTitle.length === 0 ||
|
||||
(settlement.outcome !== 'victory' && settlement.outcome !== 'defeat') ||
|
||||
typeof settlement.completedAt !== 'string' ||
|
||||
settlement.completedAt.length === 0
|
||||
!completedAt
|
||||
) {
|
||||
return undefined;
|
||||
}
|
||||
@@ -982,7 +999,7 @@ function normalizeCampaignBattleSettlement(value: unknown): CampaignBattleSettle
|
||||
reserveTraining: arrayOrEmpty<unknown>(settlement.reserveTraining)
|
||||
.map(normalizeReserveTrainingSnapshot)
|
||||
.filter((entry): entry is CampaignReserveTrainingSnapshot => Boolean(entry)),
|
||||
completedAt: settlement.completedAt
|
||||
completedAt
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1154,7 +1171,7 @@ function normalizeFirstBattleReport(report: unknown): FirstBattleReport | undefi
|
||||
),
|
||||
completedCampDialogues: uniqueStrings(report.completedCampDialogues),
|
||||
completedCampVisits: uniqueStrings(report.completedCampVisits),
|
||||
createdAt: typeof report.createdAt === 'string' && report.createdAt.length > 0 ? report.createdAt : new Date().toISOString()
|
||||
createdAt: normalizeCampaignTimestamp(report.createdAt) ?? new Date().toISOString()
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1276,6 +1293,11 @@ function reserveTrainingFocusDefinition(focusId?: string) {
|
||||
}
|
||||
|
||||
function readStoredCampaignState(slot?: number) {
|
||||
const parsed = readStoredCampaignStateObject(slot);
|
||||
return parsed ? normalizeCampaignState(parsed) : undefined;
|
||||
}
|
||||
|
||||
function readStoredCampaignStateObject(slot?: number) {
|
||||
const raw = tryStorage()?.getItem(slot ? campaignSaveSlotKey(slot) : campaignStorageKey);
|
||||
if (!raw) {
|
||||
return undefined;
|
||||
@@ -1289,7 +1311,7 @@ function readStoredCampaignState(slot?: number) {
|
||||
if (parsed.version !== undefined && parsed.version !== 1) {
|
||||
return undefined;
|
||||
}
|
||||
return normalizeCampaignState(parsed);
|
||||
return parsed;
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
@@ -1320,10 +1342,18 @@ function normalizeSlot(slot: number | undefined) {
|
||||
}
|
||||
|
||||
function readLatestSlottedCampaignState() {
|
||||
const latest = listCampaignSaveSlots()
|
||||
.filter((slot) => slot.exists && slot.updatedAt)
|
||||
.sort((a, b) => String(b.updatedAt).localeCompare(String(a.updatedAt)))[0];
|
||||
return latest ? readStoredCampaignState(latest.slot) : undefined;
|
||||
let latestSlot: number | undefined;
|
||||
let latestUpdatedAt = Number.NEGATIVE_INFINITY;
|
||||
|
||||
for (let slot = 1; slot <= campaignSaveSlotCount; slot += 1) {
|
||||
const timestamp = campaignTimestampMs(readStoredCampaignStateObject(slot)?.updatedAt);
|
||||
if (timestamp !== undefined && timestamp > latestUpdatedAt) {
|
||||
latestSlot = slot;
|
||||
latestUpdatedAt = timestamp;
|
||||
}
|
||||
}
|
||||
|
||||
return latestSlot ? readStoredCampaignState(latestSlot) : undefined;
|
||||
}
|
||||
|
||||
function advanceBond(bond: CampBondSnapshot, amount: number) {
|
||||
|
||||
Reference in New Issue
Block a user