Skip to content

Commit

Permalink
route: Gib!
Browse files Browse the repository at this point in the history
  • Loading branch information
Muirrum committed Oct 16, 2023
1 parent 3420e5e commit 1c02001
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
74 changes: 73 additions & 1 deletion internal/handlers/hkgi.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,5 +346,77 @@ func Craft(c *fiber.Ctx) error {
}
}

return nil
return c.JSON(&fiber.Map{"ok": true})
}

func Gib(c *fiber.Ctx) error {
val := &XValidator{
validator: validate,
}

var gib models.GibItem
if err := c.BodyParser(gib); err != nil {
return err
}

if errs := val.Validate(gib); len(errs) > 0 && errs[0].Error {
errMsgs := make([]string, 0)

for _, err := range errs {
errMsgs = append(errMsgs, fmt.Sprintf(
"[%s]: '%v' | Needs to implement '%s'",
err.FailedField,
err.Value,
err.Tag,
))
}

return &fiber.Error{
Code: fiber.ErrBadRequest.Code,
Message: strings.Join(errMsgs, " and "),
}
}

db := database.DB
_, err := db.Query("SELECT FROM stead WHERE username=$1", gib.To)
if err != nil {
return &fiber.Error{
Code: fiber.StatusNotFound,
Message: "who dat?",
}
}

needs := map[string]interface{}{
gib.Item: gib.Amount,
}

err = game.TakeItem(c.Locals("username").(string), needs)

if err != nil {
log.Println(err)
return &fiber.Error{
Code: fiber.StatusBadRequest,
Message: "you can't afford that!",
}
}

err = game.GiveItem(gib.To, needs)

if err != nil {
log.Println(err)
game.GiveItem(c.Locals("username").(string), needs)
return &fiber.Error{
Code: fiber.StatusInternalServerError,
Message: "couldn't gib, have refund",
}
}

state.GlobalState.ActivityPush("gib", map[string]interface{}{
"to": gib.To,
"from": c.Locals("username").(string),
"item": gib.Item,
"amount": gib.Amount,
})

return c.JSON(&fiber.Map{"ok": true})
}
6 changes: 6 additions & 0 deletions internal/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ type UseItem struct {
Item string `json:"item" validate:"required"`
}

type GibItem struct {
To string `json:"username" validate:"required"`
Item string `json:"item" validate:"required"`
Amount int `json:"amount" validate:"required"`
}

type Craft struct {
PlantId int `json:"plant_id" validate:"required"`
RecipeIndex int `json:"recipe_index" validate:"required"`
Expand Down
2 changes: 2 additions & 0 deletions internal/routers/hkgi.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,6 @@ func SetupHkgiRoutes(hkgi fiber.Router) {

hkgi.Post("/craft", func(c *fiber.Ctx) error { return nil })

hkgi.Post("/gib", handlers.Gib)

}

0 comments on commit 1c02001

Please sign in to comment.