Skip to content

Commit

Permalink
Add appeal and hand
Browse files Browse the repository at this point in the history
  • Loading branch information
klydra committed Feb 14, 2023
1 parent 0f22bf6 commit ed29044
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 4 deletions.
117 changes: 116 additions & 1 deletion server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,22 @@ func main() {
return c.JSON(http.StatusOK, Session{Code: game.Id})
})

e.Router.POST("/session/hand", 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)
}

var cards []string
err = json.Unmarshal([]byte(user.GetString("hand")), &cards)
if err != nil {
return apis.NewApiError(500, "Couldn't get player hand.", err)
}

return c.JSON(http.StatusOK, Hand{Cards: cards})
})

e.Router.POST("/game/draw", func(c echo.Context) error {
// Retrieve target user
user, err := app.Dao().FindRecordById("players", c.Request().Header.Get("token"))
Expand Down Expand Up @@ -1025,7 +1041,106 @@ func main() {
return apis.NewBadRequestError("User has already called.", err)
}

players[i].Drawing = true
players[i].Called = true
break
}
}

// 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/appeal", 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 called status
for i := 0; i < len(players); i++ {
if players[i].Name == user.GetString("player") {
if players[i].Called {
return apis.NewBadRequestError("Player has already called.", err)
}

// Draw 2 cards if possible
draw := 2
for len(stack) > 1 && draw > 0 {
if rules.Ordered {
cards = append(cards, stack[0])
stack = append(stack, stack[1:]...)
} else {
index := rand.Intn(len(stack) - 2)
cards = append(cards, stack[index])
stack = append(stack[:index], stack[index+1:]...)
}
draw--
}

break
}
}
Expand Down
17 changes: 14 additions & 3 deletions src/api/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
const API_HOST = "http://localhost:8090/api";

async function _get(path: string, headers?: HeadersInit) {
return (
await fetch(API_HOST + path, { method: "GET", headers: headers })
).json();
}

async function _post(path: string, headers?: HeadersInit) {
return (
await fetch(API_HOST + path, { method: "POST", headers: headers })
Expand All @@ -13,10 +19,11 @@ const API_SESSION_LEAVE = "/session/leave";
const API_SESSION_START = "/session/start";
const API_SESSION_RULES = "/session/rules";
const API_SESSION_ONGOING = "/session/ongoing";
const API_SESSION_HAND = "/session/hand";
const API_GAME_DRAW = "/game/draw";
const API_GAME_PLAY = "/game/play";
const API_GAME_WISH = "/game/wish";
const API_GAME_HOLD = "/game/hold";
const API_GAME_WISH = "/game/wish";
const API_GAME_CALL = "/game/call";
const API_GAME_APPEAL = "/game/appeal";
const API_GAME_SWITCH = "/game/switch";
Expand Down Expand Up @@ -57,6 +64,10 @@ export async function sessionOngoing() {
return _post(API_SESSION_ONGOING, { ...credentials() });
}

export async function sessionHand() {
return _get(API_SESSION_HAND, { ...credentials() });
}

export async function gameDraw() {
return _post(API_GAME_DRAW, { ...credentials() });
}
Expand All @@ -73,8 +84,8 @@ export async function gameWish(card: string, color: string) {
return _post(API_GAME_HOLD, { ...credentials(), card, color });
}

export async function gameCall(player: string) {
return _post(API_GAME_CALL, { ...credentials(), player });
export async function gameCall() {
return _post(API_GAME_CALL, { ...credentials() });
}

export async function gameAppeal(player: string) {
Expand Down

0 comments on commit ed29044

Please sign in to comment.