Validate battle save equipment state
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import type { BattleBond, UnitData } from '../data/scenario';
|
||||
import type { CampaignStep } from './campaignState';
|
||||
import type { UnitDirection } from '../data/unitAssets';
|
||||
import { equipmentSlots, type EquipmentSlot } from '../data/battleItems';
|
||||
|
||||
export type BattleSaveFaction = 'ally' | 'enemy';
|
||||
export type BattleSaveRosterTab = 'ally' | 'enemy';
|
||||
@@ -74,6 +75,8 @@ type BattleSaveValidationOptions = {
|
||||
validUnitIds?: ReadonlySet<string>;
|
||||
validUsableIds?: ReadonlySet<string>;
|
||||
validItemIds?: ReadonlySet<string>;
|
||||
validEquipmentItemIds?: ReadonlySet<string>;
|
||||
validEquipmentSlotItems?: Partial<Record<EquipmentSlot, ReadonlySet<string>>>;
|
||||
};
|
||||
|
||||
const unitDirections = new Set<UnitDirection>(['south', 'east', 'north', 'west']);
|
||||
@@ -237,7 +240,7 @@ function areSavedBattleUnitsValid(units: unknown[], options: BattleSaveValidatio
|
||||
}
|
||||
|
||||
function isSavedBattleUnitState(value: unknown, options: BattleSaveValidationOptions): value is SavedBattleUnitState {
|
||||
if (!isRecord(value) || typeof value.id !== 'string' || value.id.length === 0 || !isRecord(value.equipment)) {
|
||||
if (!isRecord(value) || typeof value.id !== 'string' || value.id.length === 0 || !isEquipmentSet(value.equipment, options)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -266,6 +269,30 @@ function isSavedBattleUnitState(value: unknown, options: BattleSaveValidationOpt
|
||||
return value.direction === undefined || unitDirections.has(value.direction as UnitDirection);
|
||||
}
|
||||
|
||||
function isEquipmentSet(value: unknown, options: BattleSaveValidationOptions): value is UnitData['equipment'] {
|
||||
if (!isRecord(value) || Object.keys(value).length !== equipmentSlots.length) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return equipmentSlots.every((slot) => isEquipmentStateForSlot(value[slot], slot, options));
|
||||
}
|
||||
|
||||
function isEquipmentStateForSlot(value: unknown, slot: EquipmentSlot, options: BattleSaveValidationOptions) {
|
||||
if (!isRecord(value)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const { itemId, level, exp } = value;
|
||||
return (
|
||||
typeof itemId === 'string' &&
|
||||
itemId.length > 0 &&
|
||||
(!options.validEquipmentItemIds || options.validEquipmentItemIds.has(itemId)) &&
|
||||
(!options.validEquipmentSlotItems?.[slot] || options.validEquipmentSlotItems[slot].has(itemId)) &&
|
||||
isPositiveInteger(level) &&
|
||||
isNonNegativeFiniteNumber(exp)
|
||||
);
|
||||
}
|
||||
|
||||
function isBondArray(value: unknown, options: BattleSaveValidationOptions) {
|
||||
return (
|
||||
Array.isArray(value) &&
|
||||
|
||||
Reference in New Issue
Block a user