113 lines
2.5 KiB
Go
113 lines
2.5 KiB
Go
package routes
|
|
|
|
import (
|
|
"dk/internal/actions"
|
|
"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"
|
|
"strconv"
|
|
)
|
|
|
|
func RegisterFightRoutes(r *router.Router) {
|
|
group := r.Group("/fight")
|
|
group.Use(auth.RequireAuth())
|
|
group.Use(middleware.RequireFighting())
|
|
|
|
group.Get("/", showFight)
|
|
group.Post("/", handleFightAction)
|
|
}
|
|
|
|
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()
|
|
}
|
|
|
|
monHpPct := helpers.ClampPct(float64(fight.MonsterHP), float64(fight.MonsterMaxHP), 0, 100)
|
|
|
|
monHpColor := ""
|
|
if monHpPct < 35 {
|
|
monHpColor = "danger"
|
|
} else if monHpPct < 75 {
|
|
monHpColor = "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": monHpPct,
|
|
"mon_hpcol": monHpColor,
|
|
"spells": spellMap.ToSlice(),
|
|
})
|
|
}
|
|
|
|
func handleFightAction(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
|
|
}
|
|
|
|
action := string(ctx.FormValue("action"))
|
|
|
|
switch action {
|
|
case "attack":
|
|
actions.HandleAttack(fight, user)
|
|
case "spell":
|
|
spellIDStr := string(ctx.FormValue("spell_id"))
|
|
if spellID, err := strconv.Atoi(spellIDStr); err == nil {
|
|
actions.HandleSpell(fight, user, spellID)
|
|
}
|
|
case "run":
|
|
actions.HandleRun(fight, user)
|
|
default:
|
|
fight.AddAction("Invalid action!")
|
|
}
|
|
|
|
fight.IncrementTurn()
|
|
fight.Save()
|
|
user.Save()
|
|
|
|
// Redirect back to fight page
|
|
ctx.Redirect("/fight", 302)
|
|
}
|