Filter stale sortie save ids
This commit is contained in:
@@ -811,9 +811,12 @@ function normalizeCampaignState(state: Partial<CampaignState>): CampaignState {
|
||||
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));
|
||||
const rosterUnitIds = normalizedRosterUnitIds(normalized.roster);
|
||||
const sortieUnitFilter = rosterUnitIds.size > 0 ? rosterUnitIds : undefined;
|
||||
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.battleHistory = normalizeBattleHistory(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))];
|
||||
}
|
||||
|
||||
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) {
|
||||
const numeric = Math.floor(Number(value));
|
||||
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]) => {
|
||||
if (!unitId || (allowedUnitIds && !allowedUnitIds.has(unitId))) {
|
||||
return next;
|
||||
}
|
||||
|
||||
const unitStocks = Object.entries(recordOrEmpty(stocks)).reduce<Record<string, number>>((nextStocks, [itemId, amount]) => {
|
||||
const normalizedAmount = normalizeNonNegativeInteger(amount);
|
||||
if (itemId && normalizedAmount > 0) {
|
||||
@@ -1011,7 +1026,7 @@ function normalizeSortieItemAssignments(assignments?: Record<string, unknown>) {
|
||||
return nextStocks;
|
||||
}, {});
|
||||
|
||||
if (unitId && Object.keys(unitStocks).length > 0) {
|
||||
if (Object.keys(unitStocks).length > 0) {
|
||||
next[unitId] = unitStocks;
|
||||
}
|
||||
return next;
|
||||
|
||||
Reference in New Issue
Block a user