76 lines
1.6 KiB
Go

package routes
import (
"dk/internal/auth"
"dk/internal/components"
"dk/internal/helpers"
"dk/internal/middleware"
"dk/internal/models/fights"
"dk/internal/models/monsters"
"dk/internal/models/spells"
"dk/internal/models/users"
"dk/internal/router"
"math/rand"
)
func RegisterFightRoutes(r *router.Router) {
group := r.Group("/fight")
group.Use(auth.RequireAuth())
group.Use(middleware.RequireFighting())
group.Get("/", showFight)
}
func showFight(ctx router.Ctx, _ []string) {
user := ctx.UserValue("user").(*users.User)
fight, err := fights.Find(user.FightID)
if err != nil {
ctx.SetContentType("text/plain")
ctx.SetBodyString("Fight not found")
return
}
monster, err := monsters.Find(fight.MonsterID)
if err != nil {
ctx.SetContentType("text/plain")
ctx.SetBodyString("Monster not found for fight")
return
}
// If turn 0, determine first strike and advance to turn 1
if fight.Turn == 0 {
// 50% chance user goes first
fight.FirstStrike = rand.Float32() < 0.5
fight.Turn = 1
fight.Save()
}
hpPct := helpers.ClampPct(float64(user.HP), float64(user.MaxHP), 0, 100)
hpColor := ""
if hpPct < 35 {
hpColor = "danger"
} else if hpPct < 75 {
hpColor = "warning"
}
spellMap := helpers.NewOrderedMap[int, *spells.Spell]()
if user.Spells != "" {
for _, id := range user.GetSpellIDs() {
if spell, err := spells.Find(id); err == nil {
spellMap.Set(id, spell)
}
}
}
components.RenderPage(ctx, "Fighting", "fight/fight.html", map[string]any{
"fight": fight,
"user": user,
"monster": monster,
"mon_hppct": hpPct,
"mon_hpcol": hpColor,
"spells": spellMap.ToSlice(),
})
}