diff --git a/server/main.go b/server/main.go index 8a83893..1a9e288 100644 --- a/server/main.go +++ b/server/main.go @@ -796,6 +796,99 @@ func main() { return c.JSON(http.StatusOK, Hand{Cards: cards}) }) + e.Router.POST("/game/hold", func(c echo.Context) error { + // Retrieve target user + user, err := app.Dao().FindRecordById("players", c.Request().Header.Get("token")) + if err != nil { + return apis.NewBadRequestError("Couldn't find user.", err) + } + + game, err := app.Dao().FindRecordById("games", user.GetString("game")) + if err != nil { + return apis.NewBadRequestError("User not participating in game.", err) + } + + var cards []string + err = json.Unmarshal([]byte(user.GetString("cards")), &cards) + if err != nil { + return apis.NewApiError(500, "Couldn't get player cards.", err) + } + + var players []Player + err = json.Unmarshal([]byte(game.GetString("players")), &players) + if err != nil { + return apis.NewApiError(500, "Couldn't get game players.", err) + } + + var rules Rules + err = json.Unmarshal([]byte(game.GetString("rules")), &rules) + if err != nil { + return apis.NewBadRequestError("Couldn't get game rules.", err) + } + + var stack []string + err = json.Unmarshal([]byte(game.GetString("stack")), &stack) + if err != nil { + return apis.NewApiError(500, "Couldn't get game card stack.", err) + } + + // Checking if player has turn + if game.GetString("live") != user.GetString("name") { + return apis.NewBadRequestError("It's not the players' turn.", nil) + } + + // Update drawing status + for i := 0; i < len(players); i++ { + if players[i].Name == user.GetString("name") { + if !players[i].Drawing { + return apis.NewBadRequestError("User is not currently drawing.", err) + } + + players[i].Drawing = false + break + } + } + + // Shifting turn to next player + next := nextPlayer(user.GetString("name"), players, rules) + if next < 0 { + return apis.NewApiError(500, "Couldn't evaluate next player.", err) + } + game.Set("live", players[next]) + + // Update state + playersUpdate, err := json.Marshal(players) + if err != nil { + return apis.NewApiError(500, "Couldn't get game players update.", err) + } + + stackUpdate, err := json.Marshal(stack) + if err != nil { + return apis.NewApiError(500, "Couldn't get game stack update.", err) + } + + cardsUpdate, err := json.Marshal(cards) + if err != nil { + return apis.NewApiError(500, "Couldn't get user cards update.", err) + } + + game.Set("players", playersUpdate) + game.Set("stack", stackUpdate) + user.Set("cards", cardsUpdate) + + err = app.Dao().SaveRecord(game) + if err != nil { + return apis.NewApiError(500, "Couldn't create player record.", err) + } + + err = app.Dao().SaveRecord(user) + if err != nil { + return apis.NewApiError(500, "Couldn't create player record.", err) + } + + return c.JSON(http.StatusOK, Hand{Cards: cards}) + }) + e.Router.POST("/game/wish", func(c echo.Context) error { // Retrieve target user user, err := app.Dao().FindRecordById("players", c.Request().Header.Get("token"))