Harden sortie formation assignment recovery
This commit is contained in:
@@ -5,6 +5,9 @@ export type SortieFormationAssignments = Partial<Record<string, SortieFormationR
|
||||
|
||||
export const sortieFormationRoles: SortieFormationRole[] = ['front', 'flank', 'support', 'reserve'];
|
||||
|
||||
const maxSortieFormationAssignmentUnits = 96;
|
||||
const maxSortieFormationUnitIdLength = 96;
|
||||
|
||||
export type SortieDeploymentPlanEntry = {
|
||||
unitId: string;
|
||||
role: SortieFormationRole;
|
||||
@@ -38,11 +41,30 @@ export function normalizeSortieFormationAssignments(
|
||||
assignments?: Record<string, unknown>,
|
||||
allowedUnitIds?: Set<string>
|
||||
): SortieFormationAssignments {
|
||||
return Object.fromEntries(
|
||||
Object.entries(assignments ?? {}).filter(([unitId, role]) => {
|
||||
return (!allowedUnitIds || allowedUnitIds.has(unitId)) && isSortieFormationRole(role);
|
||||
})
|
||||
) as SortieFormationAssignments;
|
||||
return Object.entries(assignments ?? {}).reduce<SortieFormationAssignments>((next, [unitId, role]) => {
|
||||
const normalizedUnitId = normalizeSortieFormationUnitId(unitId);
|
||||
if (
|
||||
!normalizedUnitId ||
|
||||
next[normalizedUnitId] ||
|
||||
(allowedUnitIds && !allowedUnitIds.has(normalizedUnitId)) ||
|
||||
(!allowedUnitIds && Object.keys(next).length >= maxSortieFormationAssignmentUnits) ||
|
||||
!isSortieFormationRole(role)
|
||||
) {
|
||||
return next;
|
||||
}
|
||||
|
||||
next[normalizedUnitId] = role;
|
||||
return next;
|
||||
}, {});
|
||||
}
|
||||
|
||||
function normalizeSortieFormationUnitId(value: unknown) {
|
||||
if (typeof value !== 'string') {
|
||||
return '';
|
||||
}
|
||||
|
||||
const text = value.trim();
|
||||
return text.length > 0 && text.length <= maxSortieFormationUnitIdLength ? text : '';
|
||||
}
|
||||
|
||||
export function createSortieDeploymentPlan(
|
||||
|
||||
Reference in New Issue
Block a user