From b71f2a8810c74d64039fbee2bff46bcef134ce26 Mon Sep 17 00:00:00 2001 From: Cara Salter Date: Fri, 15 Mar 2024 09:32:38 +1100 Subject: [PATCH] Set default log level to debug, fix getStead Previously, gameticks were filling up the log unnecessarily. These were moved to trace, and the default log level was set at debug. TODO: Make log level configurable by /etc/hkgi/config Signed-Off-By: Cara Salter --- internal/game/game.go | 4 ++-- internal/handlers/hkgi.go | 15 ++++++++++----- internal/router.go | 2 ++ 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/internal/game/game.go b/internal/game/game.go index 4f164f0..d111a5f 100644 --- a/internal/game/game.go +++ b/internal/game/game.go @@ -147,7 +147,7 @@ func RunTick() error { db := database.DB for { - log.Info("Running game tick...") + log.Trace("Running game tick...") var users []models.Stead err := db.Select(&users, "SELECT * FROM stead") if err != nil { @@ -219,7 +219,7 @@ func RunTick() error { } state.GlobalState.ActivityPrune() - log.Info("Done") + log.Trace("Done") time.Sleep(500 * time.Millisecond) } diff --git a/internal/handlers/hkgi.go b/internal/handlers/hkgi.go index d0194c7..abed83c 100644 --- a/internal/handlers/hkgi.go +++ b/internal/handlers/hkgi.go @@ -10,6 +10,7 @@ import ( "time" "github.com/gofiber/fiber/v2" + fiberLog "github.com/gofiber/fiber/v2/log" "github.com/hackagotchi/hkgi/database" "github.com/hackagotchi/hkgi/internal/game" "github.com/hackagotchi/hkgi/internal/models" @@ -45,7 +46,9 @@ func GetStead(c *fiber.Ctx) error { var inventory json.RawMessage var ephemeral_statuses []uint8 - err := db.QueryRowx("SELECT inventory, (CASE WHEN ephemeral_statuses IS NULL THEN '{\"\"}' ELSE ephemeral_statuses END) as ephemeral_statuses FROM stead WHERE username=$1", user).Scan(&inventory, &ephemeral_statuses) + fiberLog.Debug("Fetching stead") + + err := db.QueryRowx("SELECT inventory, (CASE WHEN ephemeral_statuses IS NULL THEN '{}' ELSE ephemeral_statuses END) as ephemeral_statuses FROM stead WHERE username=$1", user).Scan(&inventory, &ephemeral_statuses) if err != nil { return err @@ -149,7 +152,7 @@ func UseItem(c *fiber.Ctx) error { } var item models.UseItem - if err := c.BodyParser(item); err != nil { + if err := c.BodyParser(&item); err != nil { return err } if errs := val.Validate(item); len(errs) > 0 && errs[0].Error { @@ -171,15 +174,17 @@ func UseItem(c *fiber.Ctx) error { } manifest := state.GlobalState.Manifest + items := manifest["items"].(map[string]interface{}) - if manifest[item.Item] == nil { + if items[item.Item] == nil { return &fiber.Error{ Code: fiber.ErrBadRequest.Code, Message: "no such item found", } } - if !manifest[item.Item].(map[string]interface{})["usable"].(bool) { + usable, ok := items[item.Item].(map[string]interface{})["usable"] + if !ok || !usable.(bool) { return &fiber.Error{ Code: fiber.ErrBadRequest.Code, Message: "that's not an item you can use!", @@ -252,7 +257,7 @@ func Craft(c *fiber.Ctx) error { } var cReq models.Craft - if err := c.BodyParser(cReq); err != nil { + if err := c.BodyParser(&cReq); err != nil { return err } diff --git a/internal/router.go b/internal/router.go index dda4155..c39418c 100644 --- a/internal/router.go +++ b/internal/router.go @@ -2,6 +2,7 @@ package internal import ( "github.com/gofiber/fiber/v2" + "github.com/gofiber/fiber/v2/log" "github.com/gofiber/fiber/v2/middleware/logger" "github.com/hackagotchi/hkgi/internal/handlers" "github.com/hackagotchi/hkgi/internal/routers" @@ -9,6 +10,7 @@ import ( func SetupRoutes(app *fiber.App) { app.Use(logger.New()) + log.SetLevel(log.LevelDebug) app.Post("/signup", handlers.Signup) hkgi := app.Group("/hkgi", logger.New()) routers.SetupHkgiRoutes(hkgi)