From 9a5ed65f04d087e21ad63963b93cfd4bcc1f1f1c Mon Sep 17 00:00:00 2001 From: Sky Johnson Date: Thu, 14 Aug 2025 17:37:21 -0500 Subject: [PATCH] implement teleporting to towns via maps --- internal/components/asides.go | 4 ++-- internal/routes/index.go | 41 +++++++++++++++++++++++++++++++++++ main.go | 9 ++++++-- templates/leftside.html | 8 +++---- 4 files changed, 54 insertions(+), 8 deletions(-) diff --git a/internal/components/asides.go b/internal/components/asides.go index 4a988e0..7be4517 100644 --- a/internal/components/asides.go +++ b/internal/components/asides.go @@ -22,10 +22,10 @@ func LeftAside(ctx router.Ctx) map[string]any { // Build owned town maps list if user.Towns != "" { - townMap := helpers.NewOrderedMap[int, string]() + townMap := helpers.NewOrderedMap[int, *towns.Town]() for _, id := range user.GetTownIDs() { if town, err := towns.Find(id); err == nil { - townMap.Set(id, town.Name) + townMap.Set(id, town) } } data["_towns"] = townMap.ToSlice() diff --git a/internal/routes/index.go b/internal/routes/index.go index a7b9382..4411d0a 100644 --- a/internal/routes/index.go +++ b/internal/routes/index.go @@ -3,8 +3,11 @@ package routes import ( "dk/internal/actions" "dk/internal/components" + "dk/internal/models/towns" "dk/internal/models/users" "dk/internal/router" + "dk/internal/session" + "slices" "strconv" ) @@ -60,3 +63,41 @@ func Explore(ctx router.Ctx, _ []string) { } components.RenderPage(ctx, "", "explore.html", nil) } + +func Teleport(ctx router.Ctx, params []string) { + sess := ctx.UserValue("session").(*session.Session) + + id, err := strconv.Atoi(params[0]) + if err != nil { + sess.SetFlash("error", "Error teleporting; "+err.Error()) + ctx.Redirect("/", 302) + return + } + + town, err := towns.Find(id) + if err != nil { + sess.SetFlash("error", "Failed to teleport. Unknown town.") + ctx.Redirect("/", 302) + return + } + + user := ctx.UserValue("user").(*users.User) + if !slices.Contains(user.GetTownIDs(), id) { + sess.SetFlash("error", "You don't have a map to "+town.Name+".") + ctx.Redirect("/", 302) + return + } + + if user.TP < town.TPCost { + sess.SetFlash("error", "You don't have enough TP to teleport to "+town.Name+".") + ctx.Redirect("/", 302) + return + } + + user.TP -= town.TPCost + user.X, user.Y = town.X, town.Y + user.Currently = "In Town" + user.Save() + + ctx.Redirect("/town", 302) +} diff --git a/main.go b/main.go index ed29c4c..1bb81ab 100644 --- a/main.go +++ b/main.go @@ -175,8 +175,13 @@ func start(port string) error { r.Use(csrf.Middleware()) r.Get("/", routes.Index) - r.WithMiddleware(auth.RequireAuth()).Get("/explore", routes.Explore) - r.WithMiddleware(auth.RequireAuth()).Post("/move", routes.Move) + + actions := r.Group("") + actions.Use(auth.RequireAuth()) + actions.Get("/explore", routes.Explore) + actions.Post("/move", routes.Move) + actions.Get("/teleport/:to", routes.Teleport) + routes.RegisterAuthRoutes(r) routes.RegisterTownRoutes(r) diff --git a/templates/leftside.html b/templates/leftside.html index b177973..110542a 100644 --- a/templates/leftside.html +++ b/templates/leftside.html @@ -27,11 +27,11 @@
Towns
{if #_towns > 0} Teleport to: - {for id,name in _towns} - {if town != nil and name == town.Name} - {name} (here) + {for t in _towns} + {if town != nil and t.Name == town.Name} + {t.Name} (here) {else} - {name} + {t.Name} {/if} {/for} {else}