96 lines
2.4 KiB
Go
96 lines
2.4 KiB
Go
package routes
|
|
|
|
import (
|
|
"dk/internal/auth"
|
|
"dk/internal/helpers"
|
|
"dk/internal/items"
|
|
"dk/internal/middleware"
|
|
"dk/internal/router"
|
|
"dk/internal/template/components"
|
|
"dk/internal/towns"
|
|
"dk/internal/users"
|
|
)
|
|
|
|
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.Get("/shop", showShop)
|
|
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) {
|
|
var errorHTML string
|
|
if flash := auth.GetFlashMessage(ctx); flash != nil {
|
|
errorHTML = `<div style="color: red; margin-bottom: 1rem;">` + flash.Message + "</div>"
|
|
}
|
|
|
|
town := ctx.UserValue("town").(*towns.Town)
|
|
|
|
components.RenderPage(ctx, town.Name+" Inn", "town/inn.html", map[string]any{
|
|
"town": town,
|
|
"rested": false,
|
|
"error_message": errorHTML,
|
|
})
|
|
}
|
|
|
|
func rest(ctx router.Ctx, _ []string) {
|
|
town := ctx.UserValue("town").(*towns.Town)
|
|
user := ctx.UserValue("user").(*users.User)
|
|
|
|
if user.Gold < town.InnCost {
|
|
auth.SetFlashMessage(ctx, "error", "You can't afford to stay here tonight.")
|
|
ctx.Redirect("/town/inn", 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,
|
|
})
|
|
}
|
|
|
|
func showShop(ctx router.Ctx, _ []string) {
|
|
var errorHTML string
|
|
if flash := auth.GetFlashMessage(ctx); flash != nil {
|
|
errorHTML = `<div style="color: red; margin-bottom: 1rem;">` + flash.Message + "</div>"
|
|
}
|
|
|
|
town := ctx.UserValue("town").(*towns.Town)
|
|
|
|
var itemlist []*items.Item
|
|
if town.ShopList != "" {
|
|
itemMap := helpers.NewOrderedMap[int, *items.Item]()
|
|
for _, id := range town.GetShopItems() {
|
|
if item, err := items.Find(id); err == nil {
|
|
itemMap.Set(id, item)
|
|
}
|
|
}
|
|
itemlist = itemMap.ToSlice()
|
|
}
|
|
|
|
components.RenderPage(ctx, town.Name+" Shop", "town/shop.html", map[string]any{
|
|
"town": town,
|
|
"itemlist": itemlist,
|
|
"error_message": errorHTML,
|
|
})
|
|
}
|