implement teleporting to towns via maps

This commit is contained in:
Sky Johnson 2025-08-14 17:37:21 -05:00
parent 53567c9603
commit 9a5ed65f04
4 changed files with 54 additions and 8 deletions

View File

@ -22,10 +22,10 @@ func LeftAside(ctx router.Ctx) map[string]any {
// Build owned town maps list // Build owned town maps list
if user.Towns != "" { if user.Towns != "" {
townMap := helpers.NewOrderedMap[int, string]() townMap := helpers.NewOrderedMap[int, *towns.Town]()
for _, id := range user.GetTownIDs() { for _, id := range user.GetTownIDs() {
if town, err := towns.Find(id); err == nil { if town, err := towns.Find(id); err == nil {
townMap.Set(id, town.Name) townMap.Set(id, town)
} }
} }
data["_towns"] = townMap.ToSlice() data["_towns"] = townMap.ToSlice()

View File

@ -3,8 +3,11 @@ package routes
import ( import (
"dk/internal/actions" "dk/internal/actions"
"dk/internal/components" "dk/internal/components"
"dk/internal/models/towns"
"dk/internal/models/users" "dk/internal/models/users"
"dk/internal/router" "dk/internal/router"
"dk/internal/session"
"slices"
"strconv" "strconv"
) )
@ -60,3 +63,41 @@ func Explore(ctx router.Ctx, _ []string) {
} }
components.RenderPage(ctx, "", "explore.html", nil) 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)
}

View File

@ -175,8 +175,13 @@ func start(port string) error {
r.Use(csrf.Middleware()) r.Use(csrf.Middleware())
r.Get("/", routes.Index) 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.RegisterAuthRoutes(r)
routes.RegisterTownRoutes(r) routes.RegisterTownRoutes(r)

View File

@ -27,11 +27,11 @@
<div class="title"><img src="/assets/images/button_towns.gif" alt="Towns" title="Towns"></div> <div class="title"><img src="/assets/images/button_towns.gif" alt="Towns" title="Towns"></div>
{if #_towns > 0} {if #_towns > 0}
<i>Teleport to:</i> <i>Teleport to:</i>
{for id,name in _towns} {for t in _towns}
{if town != nil and name == town.Name} {if town != nil and t.Name == town.Name}
<span>{name} <i>(here)</i></span> <span>{t.Name} <i>(here)</i></span>
{else} {else}
<a href="#">{name}</a> <a href="/teleport/{t.ID}">{t.Name}</a>
{/if} {/if}
{/for} {/for}
{else} {else}