refactor: cleanup codebase — remove hydration guards, extract constants, fix bugs
Build and Publish Mana Loop Docker Image / build-and-publish (push) Successful in 1m20s

This commit is contained in:
2026-05-26 11:20:36 +02:00
parent 5c64bb00fa
commit b402b8f56e
23 changed files with 579 additions and 979 deletions
+28 -68
View File
@@ -3,6 +3,7 @@
import type { EquipmentInstance, AppliedEnchantment, EnchantmentDesign, ApplicationProgress } from './types';
import { calculateApplicationTime, calculateApplicationManaPerHour } from './crafting-utils';
import { HOURS_PER_TICK } from './constants';
import { hasSpecial, SPECIAL_EFFECTS } from './effects/special-effects';
import type { ComputedEffects } from './effects/upgrade-effects.types';
import type { AttunementState } from './types';
@@ -11,32 +12,16 @@ import { ENCHANTMENT_EFFECTS } from './data/enchantment-effects';
// ─── Application Validation ─────────────────────────────────────────────────
// Check if enchantment application can start
export function canApplyEnchantment(
instance: EquipmentInstance | undefined,
design: EnchantmentDesign | undefined,
currentAction: string
): { canApply: boolean; reason?: string } {
if (!instance) {
return { canApply: false, reason: 'Equipment instance not found' };
}
if (!design) {
return { canApply: false, reason: 'Enchantment design not found' };
}
if (currentAction !== 'meditate') {
return { canApply: false, reason: 'Must be in meditate state' };
}
if (!instance.tags?.includes('Ready for Enchantment')) {
return { canApply: false, reason: 'Equipment must be prepared for enchanting' };
}
if (instance.usedCapacity + design.totalCapacityUsed > instance.totalCapacity) {
return { canApply: false, reason: 'Not enough capacity on equipment' };
}
if (!instance) return { canApply: false, reason: 'Equipment instance not found' };
if (!design) return { canApply: false, reason: 'Enchantment design not found' };
if (currentAction !== 'meditate') return { canApply: false, reason: 'Must be in meditate state' };
if (!instance.tags?.includes('Ready for Enchantment')) return { canApply: false, reason: 'Equipment must be prepared for enchanting' };
if (instance.usedCapacity + design.totalCapacityUsed > instance.totalCapacity) return { canApply: false, reason: 'Not enough capacity on equipment' };
return { canApply: true };
}
@@ -51,21 +36,18 @@ export interface ApplicationCosts {
export function calculateApplicationCosts(design: EnchantmentDesign): ApplicationCosts {
const time = calculateApplicationTime(design);
const manaPerHour = calculateApplicationManaPerHour(design);
const manaPerTick = manaPerHour * 0.04; // HOURS_PER_TICK
const manaPerTick = manaPerHour * HOURS_PER_TICK;
return { time, manaPerHour, manaPerTick };
}
// ─── Application Progress ───────────────────────────────────────────────────
// Initialize application progress
export function initializeApplicationProgress(
equipmentInstanceId: string,
designId: string,
design: EnchantmentDesign
): ApplicationProgress {
const costs = calculateApplicationCosts(design);
return {
equipmentInstanceId,
designId,
@@ -77,7 +59,13 @@ export function initializeApplicationProgress(
};
}
// Calculate application progress after a tick
// Free enchant chance per special effect
const FREE_ENCHANT_CHANCES: Record<string, number> = {
[SPECIAL_EFFECTS.ENCHANT_PRESERVATION]: 0.25,
[SPECIAL_EFFECTS.THRIFTY_ENCHANTER]: 0.10,
[SPECIAL_EFFECTS.OPTIMIZED_ENCHANTING]: 0.25,
};
export interface ApplicationTickResult {
progress: number;
manaSpent: number;
@@ -93,20 +81,14 @@ export function calculateApplicationTick(
manaPerTick: number,
computedEffects: ComputedEffects
): ApplicationTickResult {
let progress = currentProgress + 0.04;
let progress = currentProgress + HOURS_PER_TICK;
let manaSpent = currentManaSpent + manaPerTick;
let manaConsumed = manaPerTick;
let triggeredFreeEnchant = false;
let freeEnchantChance = 0;
if (hasSpecial(computedEffects, SPECIAL_EFFECTS.ENCHANT_PRESERVATION)) {
freeEnchantChance += 0.25;
}
if (hasSpecial(computedEffects, SPECIAL_EFFECTS.THRIFTY_ENCHANTER)) {
freeEnchantChance += 0.10;
}
if (hasSpecial(computedEffects, SPECIAL_EFFECTS.OPTIMIZED_ENCHANTING)) {
freeEnchantChance += 0.25;
for (const [special, chance] of Object.entries(FREE_ENCHANT_CHANCES)) {
if (hasSpecial(computedEffects, special)) freeEnchantChance += chance;
}
if (freeEnchantChance > 0 && Math.random() < freeEnchantChance) {
@@ -116,47 +98,32 @@ export function calculateApplicationTick(
triggeredFreeEnchant = true;
}
return {
progress,
manaSpent,
manaConsumed,
isComplete: progress >= required,
triggeredFreeEnchant,
};
return { progress, manaSpent, manaConsumed, isComplete: progress >= required, triggeredFreeEnchant };
}
// ─── Enchantment Application ────────────────────────────────────────────────
// Apply enchantments to equipment instance
const PURE_ESSENCE_STACK_BONUS = 1.25;
const PURE_ESSENCE_COST_CAP = 100;
export function applyEnchantments(
instance: EquipmentInstance,
design: EnchantmentDesign,
computedEffects: ComputedEffects
): {
updatedInstance: EquipmentInstance;
xpGained: number;
logMessage: string;
} {
): { updatedInstance: EquipmentInstance; xpGained: number; logMessage: string } {
const isPureEssenceActive = hasSpecial(computedEffects, SPECIAL_EFFECTS.PURE_ESSENCE);
const newEnchantments: AppliedEnchantment[] = design.effects.map(eff => {
let stacks = eff.stacks;
let actualCost = eff.capacityCost;
const effectDef = ENCHANTMENT_EFFECTS[eff.effectId];
if (isPureEssenceActive && effectDef && effectDef.baseCapacityCost < 100) {
stacks = Math.ceil(stacks * 1.25);
}
const bonusStacks = isPureEssenceActive && effectDef && effectDef.baseCapacityCost < PURE_ESSENCE_COST_CAP;
return {
effectId: eff.effectId,
stacks,
actualCost,
stacks: bonusStacks ? Math.ceil(eff.stacks * PURE_ESSENCE_STACK_BONUS) : eff.stacks,
actualCost: eff.capacityCost,
};
});
const xpGained = calculateEnchantingXP(design.totalCapacityUsed);
const updatedInstance: EquipmentInstance = {
...instance,
enchantments: [...instance.enchantments, ...newEnchantments],
@@ -176,15 +143,12 @@ export function updateEnchanterAttunement(
attunements: Record<string, AttunementState>,
xpGained: number
): Record<string, AttunementState> {
if (!attunements?.enchanter?.active || xpGained <= 0) {
return attunements;
}
if (!attunements?.enchanter?.active || xpGained <= 0) return attunements;
const enchanterState = attunements.enchanter;
let newXP = enchanterState.experience + xpGained;
let newLevel = enchanterState.level;
while (newLevel < MAX_ATTUNEMENT_LEVEL) {
const xpNeeded = getAttunementXPForLevel(newLevel + 1);
if (newXP >= xpNeeded) {
@@ -197,11 +161,7 @@ export function updateEnchanterAttunement(
return {
...attunements,
enchanter: {
...enchanterState,
level: newLevel,
experience: newXP,
},
enchanter: { ...enchanterState, level: newLevel, experience: newXP },
};
}
@@ -222,7 +182,7 @@ export function resumeApplication() {
// ─── Progress Calculations ──────────────────────────────────────────────────
export function getApplicationManaCostForTick(manaPerHour: number): number {
return manaPerHour * 0.04;
return manaPerHour * HOURS_PER_TICK;
}
export function getApplicationRemainingTime(currentProgress: number, required: number): number {