Recover malformed roster equipment
This commit is contained in:
@@ -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) ||
|
||||
|
||||
Reference in New Issue
Block a user