61 lines
1.4 KiB
Go

package routes
import (
"dk/internal/auth"
"dk/internal/middleware"
"dk/internal/router"
"dk/internal/template/components"
"dk/internal/towns"
"dk/internal/users"
"fmt"
)
func RegisterTownRoutes(r *router.Router) {
group := r.Group("/town")
group.Use(middleware.RequireAuth())
group.Use(middleware.RequireTown())
group.Get("/", showTown)
group.Get("/inn", showInn)
group.WithMiddleware(middleware.CSRF(auth.Manager)).Post("/inn", rest)
}
func showTown(ctx router.Ctx, _ []string) {
town := ctx.UserValue("town").(*towns.Town)
components.RenderPage(ctx, town.Name, "town/town.html", map[string]any{
"town": town,
"newscontent": components.GenerateTownNews(),
"whosonline": components.GenerateTownWhosOnline(),
})
}
func showInn(ctx router.Ctx, _ []string) {
town := ctx.UserValue("town").(*towns.Town)
components.RenderPage(ctx, town.Name+" Inn", "town/inn.html", map[string]any{
"town": town,
"rested": false,
})
}
func rest(ctx router.Ctx, _ []string) {
town := ctx.UserValue("town").(*towns.Town)
user := ctx.UserValue("user").(*users.User)
if user.Gold < town.InnCost {
fmt.Println("Cant afford")
ctx.Redirect("/town", 303)
return
}
user.Set("Gold", user.Gold-town.InnCost)
user.Set("HP", user.MaxHP)
user.Set("MP", user.MaxMP)
user.Set("TP", user.MaxTP)
user.Save()
components.RenderPage(ctx, town.Name+" Inn", "town/inn.html", map[string]any{
"town": town,
"rested": true,
})
}