Skip to content

Commit

Permalink
feat: started implementing the czar selection
Browse files Browse the repository at this point in the history
  • Loading branch information
djpiper28 committed Aug 30, 2024
1 parent 9f0fb0f commit b6fe32b
Show file tree
Hide file tree
Showing 4 changed files with 4,634 additions and 3,394 deletions.
16 changes: 16 additions & 0 deletions backend/network/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,22 @@ func (c *WsConnection) listenAndHandle() error {
}
return nil
},
CzarSelectCardHandler: func(msg RpcCzarSelectCardMsg) error {
handler = "Czar Selects A Card"

res, err := gameRepo.Repo.CzarSelectsCard(c.GameId, c.PlayerId, msg.Cards)
if err != nil {
return err
}

if res.GameEnded {
// TODO: end the game lol
}

// TODO: send some output

return err
},
})

microSeconds := time.Since(startTime).Microseconds()
Expand Down
20 changes: 20 additions & 0 deletions backend/network/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ const (
MsgOnCardPlayed
// Tx when the judging phase starts
MsgOnCzarJudgingPhase

// Rx when a czar selects a card
MsgCzarSelectCard
)

type RpcMessageBody struct {
Expand All @@ -71,6 +74,7 @@ type RpcCommandHandlers struct {
PingHandler func() error
StartGameHandler func() error
PlayCardsHandler func(msg RpcPlayCardsMsg) error
CzarSelectCardHandler func(msg RpcCzarSelectCardMsg) error
}

func DecodeAs[T RpcMessage](data []byte) (T, error) {
Expand Down Expand Up @@ -115,6 +119,13 @@ func DecodeRpcMessage(data []byte, handlers RpcCommandHandlers) error {
}

return handlers.PlayCardsHandler(command)
case MsgCzarSelectCard:
command, err := DecodeAs[RpcCzarSelectCardMsg](data)
if err != nil {
return err
}

return handlers.CzarSelectCardHandler(command)
default:
logger.Logger.Error("Unknown command", "type", cmd.Type)
return errors.New("Unknown command")
Expand Down Expand Up @@ -242,3 +253,12 @@ type RpcOnCzarJudgingPhaseMsg struct {
func (msg RpcOnCzarJudgingPhaseMsg) Type() RpcMessageType {
return MsgOnCzarJudgingPhase
}

type RpcCzarSelectCardMsg struct {
// Unsorted array of card IDs, i.e: {1, 3, 2}
Cards []int `json:"cards"`
}

func (msg RpcCzarSelectCardMsg) Type() RpcMessageType {
return MsgCzarSelectCard
}
Loading

0 comments on commit b6fe32b

Please sign in to comment.