Reject overlapping live battle save units

This commit is contained in:
2026-07-05 13:33:25 +09:00
parent ad7e80b58f
commit 4931cb2cf7
2 changed files with 14 additions and 0 deletions

View File

@@ -59,6 +59,7 @@ try {
['invalid unit exp threshold', { units: patchUnit(0, { exp: 100 }) }], ['invalid unit exp threshold', { units: patchUnit(0, { exp: 100 }) }],
['invalid max-level unit exp threshold', { units: patchUnit(0, { level: 99, exp: 101 }) }], ['invalid max-level unit exp threshold', { units: patchUnit(0, { level: 99, exp: 101 }) }],
['invalid unit hp', { units: patchUnit(0, { hp: 99, maxHp: 30 }) }], ['invalid unit hp', { units: patchUnit(0, { hp: 99, maxHp: 30 }) }],
['duplicate live unit tile', { units: patchUnit(1, { x: validState.units[0].x, y: validState.units[0].y }) }],
['invalid unit x', { units: patchUnit(0, { x: 12 }) }], ['invalid unit x', { units: patchUnit(0, { x: 12 }) }],
['invalid unit direction', { units: patchUnit(0, { direction: 'down' }) }], ['invalid unit direction', { units: patchUnit(0, { direction: 'down' }) }],
['missing equipment slot', { units: patchUnit(0, { equipment: { weapon: createEquipmentSet().weapon, armor: createEquipmentSet().armor } }) }], ['missing equipment slot', { units: patchUnit(0, { equipment: { weapon: createEquipmentSet().weapon, armor: createEquipmentSet().armor } }) }],
@@ -116,6 +117,11 @@ try {
'Expected save missing a current battle unit to be rejected.' 'Expected save missing a current battle unit to be rejected.'
); );
assert(
isValidBattleSaveState({ ...validState, units: patchUnit(1, { hp: 0, x: validState.units[0].x, y: validState.units[0].y }) }, options),
'Expected defeated units to tolerate overlapping saved coordinates.'
);
console.log('Verified battle save normalization and corrupted battle save rejection.'); console.log('Verified battle save normalization and corrupted battle save rejection.');
} finally { } finally {
await server.close(); await server.close();

View File

@@ -219,6 +219,7 @@ function isAttackIntentArray(value: unknown, options: BattleSaveValidationOption
function areSavedBattleUnitsValid(units: unknown[], options: BattleSaveValidationOptions) { function areSavedBattleUnitsValid(units: unknown[], options: BattleSaveValidationOptions) {
const seenUnitIds = new Set<string>(); const seenUnitIds = new Set<string>();
const seenLiveTileKeys = new Set<string>();
for (const unit of units) { for (const unit of units) {
if (!isSavedBattleUnitState(unit, options) || seenUnitIds.has(unit.id)) { if (!isSavedBattleUnitState(unit, options) || seenUnitIds.has(unit.id)) {
@@ -226,6 +227,13 @@ function areSavedBattleUnitsValid(units: unknown[], options: BattleSaveValidatio
} }
seenUnitIds.add(unit.id); seenUnitIds.add(unit.id);
if (unit.hp > 0) {
const tileKey = `${unit.x}:${unit.y}`;
if (seenLiveTileKeys.has(tileKey)) {
return false;
}
seenLiveTileKeys.add(tileKey);
}
} }
if (!options.validUnitIds) { if (!options.validUnitIds) {