package routes import ( "dk/internal/actions" "dk/internal/components" "dk/internal/models/towns" "dk/internal/models/users" "fmt" sushi "git.sharkk.net/Sharkk/Sushi" "git.sharkk.net/Sharkk/Sushi/auth" ) // Map acts as a representation of owned/unowned maps in the town stores. type Map struct { ID int Name string Cost int Owned bool X int Y int TP int } func RegisterTownRoutes(app *sushi.App) { group := app.Group("/town") group.Use(auth.RequireAuth("/login")) group.Use(requireTown()) group.Get("/", showTown) group.Get("/inn", showInn) group.Post("/inn", rest) group.Get("/shop", showShop) group.Get("/shop/buy/:id", buyItem) group.Get("/maps", showMaps) group.Get("/maps/buy/:id", buyMap) } // requireTown middleware ensures the user is in a town func requireTown() sushi.Middleware { return func(ctx sushi.Ctx, next func()) { user := ctx.GetCurrentUser() if user == nil { ctx.SendError(401, "Not authenticated") return } userModel := user.(*users.User) if userModel.Currently != "In Town" { ctx.SendError(403, "You must be in town") return } town, err := towns.ByCoords(userModel.X, userModel.Y) if err != nil || town == nil || town.ID == 0 { ctx.SendError(403, fmt.Sprintf("Invalid town location (%d, %d)", userModel.X, userModel.Y)) return } ctx.SetUserValue("town", town) next() } } func showTown(ctx sushi.Ctx) { 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 sushi.Ctx) { town := ctx.UserValue("town").(*towns.Town) sess := ctx.GetCurrentSession() value, exists := sess.GetFlash("rested") rested := false if exists && value == true { rested = true } components.RenderPage(ctx, town.Name+" Inn", "town/inn.html", map[string]any{ "town": town, "rested": rested, }) } func rest(ctx sushi.Ctx) { sess := ctx.GetCurrentSession() town := ctx.UserValue("town").(*towns.Town) user := ctx.GetCurrentUser().(*users.User) err := actions.RestAtInn(user, town) if err != nil { sess.SetFlash("error", err.Error()) ctx.Redirect("/town/inn") return } sess.SetFlash("rested", true) ctx.Redirect("/town/inn") } func showShop(ctx sushi.Ctx) { sess := ctx.GetCurrentSession() var errorHTML string errorMsg := sess.GetFlashMessage("error") if errorMsg != "" { errorHTML = `
` + errorMsg + "
" } town := ctx.UserValue("town").(*towns.Town) itemList, err := town.GetShopItems() if err != nil { fmt.Printf("Town shop list error: %s\n", err.Error()) } components.RenderPage(ctx, town.Name+" Shop", "town/shop.html", map[string]any{ "town": town, "itemlist": itemList, "error_message": errorHTML, }) } func buyItem(ctx sushi.Ctx) { sess := ctx.GetCurrentSession() id := ctx.Param("id").Int() town := ctx.UserValue("town").(*towns.Town) user := ctx.GetCurrentUser().(*users.User) err := actions.BuyShopItem(user, town, id) if err != nil { sess.SetFlash("error", err.Error()) ctx.Redirect("/town/shop") return } ctx.Redirect("/town/shop") } func showMaps(ctx sushi.Ctx) { user := ctx.UserValue("user").(*users.User) town := ctx.UserValue("town").(*towns.Town) towns, _ := towns.All() var mapList []*Map for _, town := range towns { mapList = append(mapList, &Map{ ID: town.ID, Name: town.Name, Cost: town.MapCost, Owned: user.HasTownMap(town.ID), X: town.X, Y: town.Y, TP: town.TPCost, }) } components.RenderPage(ctx, town.Name+" Maps", "town/maps.html", map[string]any{ "town": town, "maplist": mapList, }) } func buyMap(ctx sushi.Ctx) { sess := ctx.GetCurrentSession() id := ctx.Param("id").Int() user := ctx.GetCurrentUser().(*users.User) err := actions.BuyTownMap(user, id) if err != nil { sess.SetFlash("error", err.Error()) ctx.Redirect("/town/maps") return } ctx.Redirect("/town/maps") }