112 lines
2.2 KiB
Go

package routes
import (
"dk/internal/actions"
"dk/internal/components"
"dk/internal/models/towns"
"dk/internal/models/users"
"slices"
"strconv"
sushi "git.sharkk.net/Sharkk/Sushi"
)
func Index(ctx sushi.Ctx) {
if !ctx.IsAuthenticated() {
components.RenderPage(ctx, "", "intro.html", nil)
return
}
user := ctx.GetCurrentUser().(*users.User)
switch user.Currently {
case "In Town":
ctx.Redirect("/town")
case "Exploring":
ctx.Redirect("/explore")
case "Fighting":
ctx.Redirect("/fight")
default:
ctx.Redirect("/explore")
}
}
func Move(ctx sushi.Ctx) {
sess := ctx.GetCurrentSession()
user := ctx.GetCurrentUser().(*users.User)
if user.Currently == "Fighting" {
sess.SetFlash("error", "You can't just run from a fight!")
ctx.Redirect("/fight")
return
}
dir, err := strconv.Atoi(string(ctx.PostArgs().Peek("direction")))
if err != nil {
ctx.SendError(400, "move form parsing error")
return
}
currently, newX, newY, err := actions.Move(user, actions.Direction(dir))
if err != nil {
ctx.SendError(400, "move error: "+err.Error())
return
}
user.Currently = currently
user.SetPosition(newX, newY)
user.Save()
switch currently {
case "In Town":
ctx.Redirect("/town")
case "Fighting":
ctx.Redirect("/fight")
default:
ctx.Redirect("/explore")
}
}
func Explore(ctx sushi.Ctx) {
user := ctx.GetCurrentUser().(*users.User)
if user.Currently != "Exploring" {
ctx.Redirect("/")
return
}
components.RenderPage(ctx, "", "explore.html", nil)
}
func Teleport(ctx sushi.Ctx) {
sess := ctx.GetCurrentSession()
id := ctx.Param("id").Int()
town, err := towns.Find(id)
if err != nil {
sess.SetFlash("error", "Failed to teleport. Unknown town.")
ctx.Redirect("/")
return
}
user := ctx.GetCurrentUser().(*users.User)
if !slices.Contains(user.GetTownIDs(), id) {
sess.SetFlash("error", "You don't have a map to "+town.Name+".")
ctx.Redirect("/")
return
}
if user.TP < town.TPCost {
sess.SetFlash("error", "You don't have enough TP to teleport to "+town.Name+".")
ctx.Redirect("/")
return
}
user.TP -= town.TPCost
user.SetPosition(town.X, town.Y)
user.Currently = "In Town"
user.Save()
sess.SetFlash("success", "You teleported to "+town.Name+" successfully!")
ctx.Redirect("/town")
}