Recover malformed roster equipment

This commit is contained in:
2026-07-05 10:07:06 +09:00
parent 4be59e5c80
commit 7cd7f4b8c9
2 changed files with 91 additions and 10 deletions

View File

@@ -180,6 +180,54 @@ try {
`Expected stale bonds to be filtered against the recovered roster while valid bonds survive: ${JSON.stringify(staleBonds)}`
);
storage.clear();
storage.set(
campaignStorageKey,
JSON.stringify({
version: 1,
updatedAt: '2026-07-03T14:00:00.000Z',
step: 'third-camp',
activeSaveSlot: 1,
roster: [
{
id: 'liu-bei',
name: 'Liu Bei',
faction: 'ally',
className: 'Lord',
classKey: 'lost-class',
level: '6',
exp: '18',
hp: '42',
maxHp: '45',
attack: '22',
move: '5',
stats: { might: '19', intelligence: '17', leadership: '21', agility: '13', luck: '12' },
equipment: {
weapon: { itemId: 'short-bow', level: '3', exp: '14' },
armor: { itemId: 'short-bow', level: '2', exp: '9' },
accessory: { itemId: 'grain-pouch', level: '0', exp: '7' }
},
x: '2',
y: '3'
},
{ id: 'ghost-unit', name: 'Ghost', faction: 'neutral' }
]
})
);
const malformedRoster = loadCampaignState();
assert(
malformedRoster.roster.length === 1 &&
malformedRoster.roster[0].id === 'liu-bei' &&
malformedRoster.roster[0].classKey === 'infantry' &&
malformedRoster.roster[0].level === 6 &&
malformedRoster.roster[0].equipment.weapon.itemId === 'short-bow' &&
malformedRoster.roster[0].equipment.weapon.level === 3 &&
malformedRoster.roster[0].equipment.armor.itemId === 'cloth-armor' &&
malformedRoster.roster[0].equipment.accessory.itemId === 'grain-pouch' &&
malformedRoster.roster[0].equipment.accessory.level === 1,
`Expected malformed roster units and equipment to be normalized safely: ${JSON.stringify(malformedRoster)}`
);
storage.clear();
storage.set(
campaignStorageKey,
@@ -391,7 +439,7 @@ try {
const fallback = loadCampaignState();
assert(fallback.step === 'second-camp' && fallback.activeSaveSlot === 2, `Expected valid slotted save fallback: ${JSON.stringify(fallback)}`);
console.log('Verified campaign save normalization, malformed-field/bond-roster/sortie-roster/latest-history/detail recovery, unknown-step/report/history recovery, unsupported-version rejection, and slotted fallback.');
console.log('Verified campaign save normalization, malformed-field/roster-equipment/bond-roster/sortie-roster/latest-history/detail recovery, unknown-step/report/history recovery, unsupported-version rejection, and slotted fallback.');
} finally {
await server.close();
}

View File

@@ -1,4 +1,5 @@
import { equipmentExpToNext, equipmentSlots } from '../data/battleItems';
import { equipmentExpToNext, equipmentSlots, itemCatalog, type EquipmentSlot } from '../data/battleItems';
import { unitClasses } from '../data/battleRules';
import { battleScenarios, type BattleScenarioId } from '../data/battles';
import { campaignRecruitUnits, type BattleBond, type UnitData } from '../data/scenario';
import { normalizeSortieFormationAssignments, type SortieFormationAssignments } from '../data/sortieDeployment';
@@ -247,6 +248,12 @@ export type CampaignReserveTrainingFocusDefinition = {
export const defaultCampaignReserveTrainingFocusId: CampaignReserveTrainingFocusId = 'balanced';
const defaultEquipmentBySlot: Record<EquipmentSlot, string> = {
weapon: 'training-sword',
armor: 'cloth-armor',
accessory: 'grain-pouch'
};
const campaignInventoryCaps: Record<string, number> = {
['\uCF69']: 120,
['\uC0C1\uCC98\uC57D']: 80,
@@ -803,8 +810,8 @@ function normalizeCampaignState(state: Partial<CampaignState>): CampaignState {
normalized.activeSaveSlot = normalizeSlot(normalized.activeSaveSlot);
normalized.gold = normalizeNonNegativeInteger(normalized.gold);
normalized.roster = arrayOrEmpty<unknown>(normalized.roster)
.filter(isPlainObject)
.map((unit) => cloneUnit(unit as UnitData));
.map(normalizeUnitDataSnapshot)
.filter((unit): unit is UnitData => Boolean(unit));
normalized.bonds = arrayOrEmpty<unknown>(normalized.bonds)
.map(normalizeCampBondSnapshot)
.filter((bond): bond is CampBondSnapshot => Boolean(bond));
@@ -1079,7 +1086,7 @@ function normalizeFirstBattleReport(report: unknown): FirstBattleReport | undefi
.map(normalizeBattleObjectiveSnapshot)
.filter((objective): objective is BattleObjectiveSnapshot => Boolean(objective)),
units: arrayOrEmpty<unknown>(report.units)
.map(normalizeBattleReportUnitSnapshot)
.map(normalizeUnitDataSnapshot)
.filter((unit): unit is UnitData => Boolean(unit)),
bonds: arrayOrEmpty<unknown>(report.bonds)
.map(normalizeCampBondSnapshot)
@@ -1095,7 +1102,7 @@ function normalizeFirstBattleReport(report: unknown): FirstBattleReport | undefi
};
}
function normalizeBattleReportUnitSnapshot(value: unknown): UnitData | undefined {
function normalizeUnitDataSnapshot(value: unknown): UnitData | undefined {
if (
!isPlainObject(value) ||
typeof value.id !== 'string' ||
@@ -1115,11 +1122,11 @@ function normalizeBattleReportUnitSnapshot(value: unknown): UnitData | undefined
name: value.name,
faction: value.faction,
className: typeof value.className === 'string' ? value.className : '',
classKey: typeof value.classKey === 'string' ? value.classKey as UnitData['classKey'] : 'infantry',
level: normalizeNonNegativeInteger(value.level),
classKey: normalizeUnitClassKey(value.classKey),
level: Math.max(1, normalizeNonNegativeInteger(value.level)),
exp: normalizeNonNegativeInteger(value.exp),
hp,
maxHp,
maxHp: Math.max(1, maxHp),
attack: normalizeNonNegativeInteger(value.attack),
move: normalizeNonNegativeInteger(value.move),
stats: {
@@ -1129,12 +1136,38 @@ function normalizeBattleReportUnitSnapshot(value: unknown): UnitData | undefined
agility: normalizeNonNegativeInteger(stats.agility),
luck: normalizeNonNegativeInteger(stats.luck)
},
equipment: recordOrEmpty(value.equipment) as UnitData['equipment'],
equipment: normalizeEquipmentSet(value.equipment),
x: normalizeNonNegativeInteger(value.x),
y: normalizeNonNegativeInteger(value.y)
};
}
function normalizeUnitClassKey(value: unknown): UnitData['classKey'] {
return typeof value === 'string' && value in unitClasses ? value as UnitData['classKey'] : 'infantry';
}
function normalizeEquipmentSet(value: unknown): UnitData['equipment'] {
const equipmentRecord = recordOrEmpty(value);
return equipmentSlots.reduce<UnitData['equipment']>((equipment, slot) => {
const state = recordOrEmpty(equipmentRecord[slot]);
const itemId =
typeof state.itemId === 'string' && itemCatalog[state.itemId]?.slot === slot
? state.itemId
: defaultEquipmentBySlot[slot];
equipment[slot] = {
itemId,
level: normalizeEquipmentLevel(state.level),
exp: normalizeNonNegativeInteger(state.exp)
};
return equipment;
}, {} as UnitData['equipment']);
}
function normalizeEquipmentLevel(value: unknown) {
const level = normalizeNonNegativeInteger(value);
return Math.min(9, Math.max(1, level));
}
function normalizeCampMvpSnapshot(value: unknown): CampMvpSnapshot | undefined {
if (
!isPlainObject(value) ||