From 71245b16551ec575b6f3efefa1f749207749aa62 Mon Sep 17 00:00:00 2001 From: Sky Johnson Date: Wed, 13 Aug 2025 18:19:54 -0500 Subject: [PATCH] first pass on move/explore logic --- internal/actions/move.go | 61 +++++++++++++++++++++++++++++++++++++++ internal/routes/index.go | 37 ++++++++++++++++++++++++ internal/server/server.go | 2 ++ internal/towns/towns.go | 14 +++++++++ templates/leftside.html | 11 +++---- 5 files changed, 120 insertions(+), 5 deletions(-) create mode 100644 internal/actions/move.go diff --git a/internal/actions/move.go b/internal/actions/move.go new file mode 100644 index 0000000..3918135 --- /dev/null +++ b/internal/actions/move.go @@ -0,0 +1,61 @@ +package actions + +import ( + "dk/internal/control" + "dk/internal/towns" + "dk/internal/users" + "fmt" +) + +type Direction int + +const ( + North Direction = iota + East + South + West +) + +func (d Direction) String() string { + switch d { + case North: + return "North" + case East: + return "East" + case South: + return "South" + case West: + return "West" + default: + return "Unknown" + } +} + +func Move(user *users.User, dir Direction) (string, int, int, error) { + control, err := control.Get() + if err != nil { + panic("Move: CONTROL ROW SHOULD EXIST") + } + + newX, newY := user.X, user.Y + switch dir { + case North: + newY++ + case East: + newX++ + case South: + newY-- + case West: + newX-- + } + + if !control.IsWithinWorldBounds(newX, newY) { + return user.Currently, user.X, user.Y, fmt.Errorf("You've hit the edge of the world.") + } + + if towns.ExistsAt(newX, newY) { + return "In Town", newX, newY, nil + } + + return "Exploring", newX, newY, nil +} diff --git a/internal/routes/index.go b/internal/routes/index.go index dfd99e3..f7e3863 100644 --- a/internal/routes/index.go +++ b/internal/routes/index.go @@ -1,9 +1,11 @@ package routes import ( + "dk/internal/actions" "dk/internal/middleware" "dk/internal/router" "dk/internal/template/components" + "strconv" ) func Index(ctx router.Ctx, _ []string) { @@ -11,8 +13,43 @@ func Index(ctx router.Ctx, _ []string) { if user != nil { if user.Currently == "In Town" { ctx.Redirect("/town", 303) + return } } components.RenderPage(ctx, "", "intro.html", nil) } + +func Move(ctx router.Ctx, _ []string) { + user := middleware.GetCurrentUser(ctx) + dir, err := strconv.Atoi(string(ctx.PostArgs().Peek("direction"))) + if err != nil { + ctx.SetContentType("text/plain") + ctx.SetBodyString("move form parsing error") + return + } + + currently, newX, newY, err := actions.Move(user, actions.Direction(dir)) + if err != nil { + ctx.SetContentType("text/plain") + ctx.SetBodyString("move error: " + err.Error()) + return + } + + user.Set("Currently", currently) + user.Set("X", newX) + user.Set("Y", newY) + user.Save() + + if currently == "In Town" { + ctx.Redirect("/town", 303) + return + } + + ctx.Redirect("/explore", 303) +} + +func Explore(ctx router.Ctx, _ []string) { + ctx.SetContentType("text/plain") + ctx.SetBodyString("Exploring") +} diff --git a/internal/server/server.go b/internal/server/server.go index 2c3eb92..ec5f17e 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -40,6 +40,8 @@ func Start(port string) error { r.Use(middleware.CSRF(auth.Manager)) r.Get("/", routes.Index) + r.Get("/explore", routes.Explore) + r.Post("/move", routes.Move) routes.RegisterAuthRoutes(r) routes.RegisterTownRoutes(r) diff --git a/internal/towns/towns.go b/internal/towns/towns.go index a693686..97d76cb 100644 --- a/internal/towns/towns.go +++ b/internal/towns/towns.go @@ -196,6 +196,20 @@ func ByCoords(x, y int) (*Town, error) { return town, nil } +// ExistsAt checks for a town at the given coordinates, returning true/false +func ExistsAt(x, y int) bool { + var exists bool + + query := `SELECT COUNT(*) > 0 FROM towns WHERE x = ? AND y = ? LIMIT 1` + + err := database.Query(query, func(stmt *sqlite.Stmt) error { + exists = stmt.ColumnInt(0) > 0 + return nil + }, x, y) + + return err == nil && exists +} + // Retrieves towns within a certain distance from a point func ByDistance(fromX, fromY, maxDistance int) ([]*Town, error) { var towns []*Town diff --git a/templates/leftside.html b/templates/leftside.html index b1569d7..756b8d6 100644 --- a/templates/leftside.html +++ b/templates/leftside.html @@ -9,16 +9,17 @@ {/if}
{user.X}{if user.X < 0}W{else}E{/if}, {user.Y}{if user.Y < 0}N{else}S{/if}
- + View Map
- + {csrf} +
- - + +
- +