Clarify state normalization helpers

This commit is contained in:
Jan Bader
2026-06-17 22:33:05 +02:00
parent 9093004146
commit c16adb87b6
+26 -22
View File
@@ -124,32 +124,26 @@ function normalizeState(candidate) {
combatStyle: ["melee", "ranged", "skill"].includes(candidate?.combatStyle) ? candidate.combatStyle : "melee", combatStyle: ["melee", "ranged", "skill"].includes(candidate?.combatStyle) ? candidate.combatStyle : "melee",
recommendationCount: Math.min(20, Math.max(3, Number(candidate?.recommendationCount || 8))), recommendationCount: Math.min(20, Math.max(3, Number(candidate?.recommendationCount || 8))),
newItem: normalizeNewItem(candidate?.newItem), newItem: normalizeNewItem(candidate?.newItem),
pieces: fallback.pieces.map((piece, index) => enforceFixedPieceFields({ pieces: fallback.pieces.map((piece, index) => normalizePiece(piece, candidate?.pieces?.[index], index)),
...piece,
...(candidate?.pieces?.[index] || {}),
baseDamage: Number(candidate?.pieces?.[index]?.baseDamage ?? (candidate?.pieces?.[index]?.baseType === "damage" ? candidate?.pieces?.[index]?.baseValue : piece.baseDamage) ?? 0),
baseHealth: Number(candidate?.pieces?.[index]?.baseHealth ?? (candidate?.pieces?.[index]?.baseType === "health" ? candidate?.pieces?.[index]?.baseValue : piece.baseHealth) ?? 0),
noEffects: piece.noEffects || Boolean(candidate?.pieces?.[index]?.noEffects),
effects: piece.noEffects ? [] : [0, 1].map((effectIndex) => ({
type: candidate?.pieces?.[index]?.effects?.[effectIndex]?.type || "",
value: clampEffectValue(
candidate?.pieces?.[index]?.effects?.[effectIndex]?.type || "",
Number(candidate?.pieces?.[index]?.effects?.[effectIndex]?.value || 1),
),
})),
}, index)),
}; };
} }
function normalizePiece(fallbackPiece, candidatePiece, index) {
const baseDamage = Number(candidatePiece?.baseDamage ?? (candidatePiece?.baseType === "damage" ? candidatePiece?.baseValue : fallbackPiece.baseDamage) ?? 0);
const baseHealth = Number(candidatePiece?.baseHealth ?? (candidatePiece?.baseType === "health" ? candidatePiece?.baseValue : fallbackPiece.baseHealth) ?? 0);
const noEffects = fallbackPiece.noEffects || Boolean(candidatePiece?.noEffects);
return enforceFixedPieceFields({
...fallbackPiece,
...(candidatePiece || {}),
baseDamage,
baseHealth,
noEffects,
effects: noEffects ? [] : normalizeEffects(candidatePiece?.effects),
}, index);
}
function normalizeNewItem(candidate) { function normalizeNewItem(candidate) {
const fallback = createDefaultNewItem(); const fallback = createDefaultNewItem();
const effects = [0, 1].map((effectIndex) => {
const type = candidate?.effects?.[effectIndex]?.type || "";
return {
type,
value: clampEffectValue(type, Number(candidate?.effects?.[effectIndex]?.value || 1)),
};
});
return { return {
...fallback, ...fallback,
...candidate, ...candidate,
@@ -157,10 +151,20 @@ function normalizeNewItem(candidate) {
targetPieceId: candidate?.targetPieceId || null, targetPieceId: candidate?.targetPieceId || null,
baseDamage: Math.max(0, Number(candidate?.baseDamage || 0)), baseDamage: Math.max(0, Number(candidate?.baseDamage || 0)),
baseHealth: Math.max(0, Number(candidate?.baseHealth || 0)), baseHealth: Math.max(0, Number(candidate?.baseHealth || 0)),
effects, effects: normalizeEffects(candidate?.effects),
}; };
} }
function normalizeEffects(candidateEffects) {
return [0, 1].map((effectIndex) => {
const type = candidateEffects?.[effectIndex]?.type || "";
return {
type,
value: clampEffectValue(type, Number(candidateEffects?.[effectIndex]?.value || 1)),
};
});
}
function normalizeProfile(profile) { function normalizeProfile(profile) {
if (PROFILES[profile]) return profile; if (PROFILES[profile]) return profile;
if (["damage", "skill", "balanced"].includes(profile)) return "lifesteal"; if (["damage", "skill", "balanced"].includes(profile)) return "lifesteal";