diff --git a/app.js b/app.js index 2673603..f6da13c 100644 --- a/app.js +++ b/app.js @@ -124,32 +124,26 @@ function normalizeState(candidate) { combatStyle: ["melee", "ranged", "skill"].includes(candidate?.combatStyle) ? candidate.combatStyle : "melee", recommendationCount: Math.min(20, Math.max(3, Number(candidate?.recommendationCount || 8))), newItem: normalizeNewItem(candidate?.newItem), - pieces: fallback.pieces.map((piece, index) => enforceFixedPieceFields({ - ...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)), + pieces: fallback.pieces.map((piece, index) => normalizePiece(piece, candidate?.pieces?.[index], 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) { 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 { ...fallback, ...candidate, @@ -157,10 +151,20 @@ function normalizeNewItem(candidate) { targetPieceId: candidate?.targetPieceId || null, baseDamage: Math.max(0, Number(candidate?.baseDamage || 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) { if (PROFILES[profile]) return profile; if (["damage", "skill", "balanced"].includes(profile)) return "lifesteal";