Validate enemy usable save keys
This commit is contained in:
@@ -11349,7 +11349,8 @@ export class BattleScene extends Phaser.Scene {
|
||||
expectedBattleId: battleScenario.id,
|
||||
mapWidth: battleMap.width,
|
||||
mapHeight: battleMap.height,
|
||||
validUnitIds: new Set(battleUnits.map((unit) => unit.id))
|
||||
validUnitIds: new Set(battleUnits.map((unit) => unit.id)),
|
||||
validUsableIds: new Set(Object.keys(usableCatalog))
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -72,6 +72,7 @@ type BattleSaveValidationOptions = {
|
||||
mapWidth?: number;
|
||||
mapHeight?: number;
|
||||
validUnitIds?: ReadonlySet<string>;
|
||||
validUsableIds?: ReadonlySet<string>;
|
||||
};
|
||||
|
||||
const unitDirections = new Set<UnitDirection>(['south', 'east', 'north', 'west']);
|
||||
@@ -130,7 +131,7 @@ export function isValidBattleSaveState(state: unknown, options: BattleSaveValida
|
||||
!isOptionalBuffArray(state.battleBuffs, options) ||
|
||||
!isOptionalStatusArray(state.battleStatuses, options) ||
|
||||
!isOptionalStatsRecord(state.battleStats, options) ||
|
||||
!isOptionalStringArray(state.enemyUsableUseKeys) ||
|
||||
!isOptionalEnemyUsableUseKeyArray(state.enemyUsableUseKeys, state.battleId, options) ||
|
||||
!isOptionalStringArray(state.triggeredBattleEvents)
|
||||
) {
|
||||
return false;
|
||||
@@ -339,6 +340,28 @@ function isOptionalStatsRecord(value: unknown, options: BattleSaveValidationOpti
|
||||
);
|
||||
}
|
||||
|
||||
function isOptionalEnemyUsableUseKeyArray(value: unknown, battleId: string, options: BattleSaveValidationOptions) {
|
||||
return value === undefined || (Array.isArray(value) && value.every((entry) => isEnemyUsableUseKey(entry, battleId, options)));
|
||||
}
|
||||
|
||||
function isEnemyUsableUseKey(value: unknown, battleId: string, options: BattleSaveValidationOptions) {
|
||||
if (typeof value !== 'string') {
|
||||
return false;
|
||||
}
|
||||
|
||||
const [entryBattleId, unitId, usableId, extra] = value.split(':');
|
||||
return (
|
||||
extra === undefined &&
|
||||
entryBattleId === battleId &&
|
||||
typeof unitId === 'string' &&
|
||||
unitId.length > 0 &&
|
||||
typeof usableId === 'string' &&
|
||||
usableId.length > 0 &&
|
||||
isKnownUnitId(unitId, options) &&
|
||||
(!options.validUsableIds || options.validUsableIds.has(usableId))
|
||||
);
|
||||
}
|
||||
|
||||
function isKnownUnitId(unitId: string, options: BattleSaveValidationOptions) {
|
||||
return !options.validUnitIds || options.validUnitIds.has(unitId);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user