Harden battle save growth validation
This commit is contained in:
@@ -1,7 +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';
|
||||
import { equipmentExpToNext, equipmentSlots, type EquipmentSlot } from '../data/battleItems';
|
||||
|
||||
export type BattleSaveFaction = 'ally' | 'enemy';
|
||||
export type BattleSaveRosterTab = 'ally' | 'enemy';
|
||||
@@ -239,8 +239,7 @@ function isSavedBattleUnitState(value: unknown, options: BattleSaveValidationOpt
|
||||
}
|
||||
|
||||
if (
|
||||
!isPositiveInteger(value.level) ||
|
||||
!isNonNegativeFiniteNumber(value.exp) ||
|
||||
!isValidCharacterProgress(value.level, value.exp) ||
|
||||
!isNonNegativeFiniteNumber(value.hp) ||
|
||||
!isPositiveInteger(value.maxHp) ||
|
||||
Number(value.hp) > Number(value.maxHp) ||
|
||||
@@ -282,11 +281,28 @@ function isEquipmentStateForSlot(value: unknown, slot: EquipmentSlot, options: B
|
||||
itemId.length > 0 &&
|
||||
(!options.validEquipmentItemIds || options.validEquipmentItemIds.has(itemId)) &&
|
||||
(!options.validEquipmentSlotItems?.[slot] || options.validEquipmentSlotItems[slot].has(itemId)) &&
|
||||
isPositiveInteger(level) &&
|
||||
isNonNegativeFiniteNumber(exp)
|
||||
isValidEquipmentProgress(level, exp)
|
||||
);
|
||||
}
|
||||
|
||||
function isValidCharacterProgress(level: unknown, exp: unknown) {
|
||||
return (
|
||||
isPositiveInteger(level) &&
|
||||
Number.isInteger(exp) &&
|
||||
Number(exp) >= 0 &&
|
||||
(Number(level) >= 99 ? Number(exp) <= 100 : Number(exp) < 100)
|
||||
);
|
||||
}
|
||||
|
||||
function isValidEquipmentProgress(level: unknown, exp: unknown) {
|
||||
if (!Number.isInteger(level) || Number(level) < 1 || Number(level) > 9 || !Number.isInteger(exp) || Number(exp) < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const next = equipmentExpToNext(Number(level));
|
||||
return Number(level) >= 9 ? Number(exp) <= next : Number(exp) < next;
|
||||
}
|
||||
|
||||
function isBondArray(value: unknown, options: BattleSaveValidationOptions) {
|
||||
return (
|
||||
Array.isArray(value) &&
|
||||
|
||||
Reference in New Issue
Block a user