package player // MaxLevel is the maximum player level. const MaxLevel = 50 // RequiredExp returns the experience needed to reach the next level. // Formula: level^2 * 100 func RequiredExp(level int) int { return level * level * 100 } // CalcStatsForLevel computes combat stats for a given level. func CalcStatsForLevel(level int) (maxHP, maxMP, attackPower float64) { maxHP = 100 + float64(level-1)*10 maxMP = 50 + float64(level-1)*5 attackPower = 10 + float64(level-1)*2 return } // LevelUpResult holds the result of applying experience gain. type LevelUpResult struct { OldLevel int `json:"oldLevel"` NewLevel int `json:"newLevel"` Experience int `json:"experience"` NextExp int `json:"nextExp"` MaxHP float64 `json:"maxHp"` MaxMP float64 `json:"maxMp"` AttackPower float64 `json:"attackPower"` LeveledUp bool `json:"leveledUp"` } // ApplyExperience adds exp to current level/exp and applies level ups. // Returns the result including new stats if leveled up. func ApplyExperience(currentLevel, currentExp, gainedExp int) LevelUpResult { level := currentLevel exp := currentExp + gainedExp // Process level ups for level < MaxLevel { required := RequiredExp(level) if exp < required { break } exp -= required level++ } // Cap at max level if level >= MaxLevel { level = MaxLevel exp = 0 // No more exp needed at max level } maxHP, maxMP, attackPower := CalcStatsForLevel(level) nextExp := 0 if level < MaxLevel { nextExp = RequiredExp(level) } return LevelUpResult{ OldLevel: currentLevel, NewLevel: level, Experience: exp, NextExp: nextExp, MaxHP: maxHP, MaxMP: maxMP, AttackPower: attackPower, LeveledUp: level > currentLevel, } }