fix lots of fight/level up components

This commit is contained in:
Sky Johnson 2025-08-28 17:25:24 -05:00
parent 73d946e011
commit 4288619520
5 changed files with 58 additions and 23 deletions

Binary file not shown.

View File

@ -9,6 +9,7 @@ import (
"dk/internal/models/towns"
"dk/internal/models/users"
"fmt"
"maps"
"math"
"math/rand"
)
@ -33,18 +34,18 @@ func (r *FightResult) EndFightWithVictory(monster *monsters.Monster, user *users
r.FightUpdates["reward_gold"] = rewardGold
r.FightUpdates["reward_exp"] = rewardExp
newLevel, newStr, newDex, newExp := user.CalculateLevelUp(rewardExp)
levelUpResults := user.GrantExp(rewardExp)
r.UserUpdates = map[string]any{
"fight_id": 0,
"currently": "Exploring",
"gold": user.Gold + rewardGold,
"exp": newExp,
"level": newLevel,
"strength": newStr,
"dexterity": newDex,
if r.UserUpdates == nil {
r.UserUpdates = make(map[string]any)
}
r.UserUpdates["fight_id"] = 0
r.UserUpdates["currently"] = "Exploring"
r.UserUpdates["gold"] = user.Gold + rewardGold
maps.Copy(r.UserUpdates, levelUpResults)
r.Ended = true
r.Victory = true
r.Won = true
@ -167,6 +168,7 @@ func HandleSpell(fight *fights.Fight, user *users.User, spellID int) *FightResul
monster, err := monsters.Find(fight.MonsterID)
if err == nil {
result.EndFightWithVictory(monster, user)
result.UserUpdates["mp"] = user.MP - spell.MP
result.ActionText += " " + fmt.Sprintf("%s has been defeated!", monster.Name)
}
}
@ -182,6 +184,7 @@ func HandleSpell(fight *fights.Fight, user *users.User, spellID int) *FightResul
monster, err := monsters.Find(fight.MonsterID)
if err == nil {
result.EndFightWithVictory(monster, user)
result.UserUpdates["mp"] = user.MP - spell.MP
result.ActionText += " " + fmt.Sprintf("%s has been defeated!", monster.Name)
}
}

View File

@ -46,6 +46,16 @@ func RightAside(ctx sushi.Ctx) map[string]any {
user := ctx.GetCurrentUser().(*users.User)
data["_class"] = user.Class()
if user.Level == 1 {
data["_exp_display"] = user.Exp
data["_exp_needed"] = helpers.ExpAtLevel(2)
} else {
currentLevelExp := helpers.ExpAtLevel(user.Level)
nextLevelExp := helpers.ExpAtLevel(user.Level + 1)
data["_exp_display"] = user.Exp - currentLevelExp
data["_exp_needed"] = nextLevelExp - currentLevelExp
}
data["_expprog"] = fmt.Sprintf("%.1f", user.ExpProgress())
hpPct := helpers.ClampPct(float64(user.HP), float64(user.MaxHP), 0, 100)

View File

@ -295,17 +295,22 @@ func (u *User) ExpNeededForNextLevel() int {
func (u *User) GrantExp(expAmount int) map[string]any {
oldLevel := u.Level
newLevel, newStr, newDex, newExp := u.CalculateLevelUp(expAmount)
results := u.CalculateLevelUp(expAmount)
updates := map[string]any{
"exp": newExp,
"exp": results["exp"],
}
newLevel := results["level"].(int)
// Only include level/stats if they actually changed
if newLevel > oldLevel {
updates["level"] = newLevel
updates["strength"] = newStr
updates["dexterity"] = newDex
updates["strength"] = results["strength"]
updates["dexterity"] = results["dexterity"]
updates["max_hp"] = results["max_hp"]
updates["max_mp"] = results["max_mp"]
updates["max_tp"] = results["max_tp"]
// Learn new spells for each level gained
for level := oldLevel + 1; level <= newLevel; level++ {
@ -319,10 +324,15 @@ func (u *User) GrantExp(expAmount int) map[string]any {
return updates
}
func (u *User) CalculateLevelUp(expGain int) (newLevel, newStr, newDex, newExp int) {
func (u *User) CalculateLevelUp(expGain int) map[string]any {
class := u.Class()
level := u.Level
str := u.Strength
dex := u.Dexterity
maxHP := u.MaxHP
maxMP := u.MaxMP
maxTP := u.MaxTP
totalExp := u.Exp + expGain
for {
@ -332,24 +342,36 @@ func (u *User) CalculateLevelUp(expGain int) (newLevel, newStr, newDex, newExp i
}
level++
str++
dex++
totalExp -= expNeeded
str += class.RateSTR
dex += class.RateDEX
maxHP += class.RateHP
maxMP += class.RateMP
maxTP += 2
}
return level, str, dex, totalExp
return map[string]any{
"level": level,
"strength": str,
"dexterity": dex,
"max_hp": maxHP,
"max_mp": maxMP,
"max_tp": maxTP,
"exp": totalExp,
}
}
func (u *User) ExpProgress() float64 {
nextLevelExp := helpers.ExpAtLevel(u.Level + 1)
if u.Level == 1 {
return float64(u.Exp) / float64(u.ExpNeededForNextLevel()) * 100
return float64(u.Exp) / float64(nextLevelExp) * 100
}
currentLevelExp := helpers.ExpAtLevel(u.Level)
nextLevelExp := u.ExpNeededForNextLevel()
progressExp := u.Exp
expIntoLevel := u.Exp - currentLevelExp
expNeededForLevel := nextLevelExp - currentLevelExp
return float64(progressExp) / float64(nextLevelExp-currentLevelExp) * 100
return float64(expIntoLevel) / float64(expNeededForLevel) * 100
}
func (u *User) Class() *classes.Class {

View File

@ -2,7 +2,7 @@
<ul class="unstyled">
<li><b>{user.Username}</b></li>
<li>Level {user.Level} {_class.Name}</li>
<li>Exp: {user.Exp} ({_expprog}%)</li>
<li>Exp: {_exp_display} ({_expprog}%)</li>
<li>Gold: {user.Gold}</li>
<li>HP: {user.HP}/{user.MaxHP}</li>
<li>MP: {user.MP}/{user.MaxMP}</li>