Validate battle save unit roster
This commit is contained in:
@@ -71,6 +71,7 @@ type BattleSaveValidationOptions = {
|
||||
expectedBattleId?: string;
|
||||
mapWidth?: number;
|
||||
mapHeight?: number;
|
||||
validUnitIds?: ReadonlySet<string>;
|
||||
};
|
||||
|
||||
const unitDirections = new Set<UnitDirection>(['south', 'east', 'north', 'west']);
|
||||
@@ -118,7 +119,7 @@ export function isValidBattleSaveState(state: unknown, options: BattleSaveValida
|
||||
!isStringArray(state.actedUnitIds) ||
|
||||
!isBattleLog(state.battleLog) ||
|
||||
!isAttackIntentArray(state.attackIntents) ||
|
||||
!state.units.every((unit) => isSavedBattleUnitState(unit, options)) ||
|
||||
!areSavedBattleUnitsValid(state.units, options) ||
|
||||
!isBondArray(state.bonds)
|
||||
) {
|
||||
return false;
|
||||
@@ -185,7 +186,29 @@ function isAttackIntentArray(value: unknown) {
|
||||
);
|
||||
}
|
||||
|
||||
function isSavedBattleUnitState(value: unknown, options: BattleSaveValidationOptions) {
|
||||
function areSavedBattleUnitsValid(units: unknown[], options: BattleSaveValidationOptions) {
|
||||
const seenUnitIds = new Set<string>();
|
||||
|
||||
for (const unit of units) {
|
||||
if (!isSavedBattleUnitState(unit, options) || seenUnitIds.has(unit.id)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
seenUnitIds.add(unit.id);
|
||||
}
|
||||
|
||||
if (!options.validUnitIds) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (seenUnitIds.size !== options.validUnitIds.size) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return [...seenUnitIds].every((unitId) => options.validUnitIds?.has(unitId));
|
||||
}
|
||||
|
||||
function isSavedBattleUnitState(value: unknown, options: BattleSaveValidationOptions): value is SavedBattleUnitState {
|
||||
if (!isRecord(value) || typeof value.id !== 'string' || value.id.length === 0 || !isRecord(value.equipment)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user