Skip to content

Commit

Permalink
Adding hold route
Browse files Browse the repository at this point in the history
  • Loading branch information
klydra committed Feb 14, 2023
1 parent d4d6485 commit c526d90
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down

0 comments on commit c526d90

Please sign in to comment.