Skip to content

Commit

Permalink
feat: added on game end logic
Browse files Browse the repository at this point in the history
  • Loading branch information
djpiper28 committed Aug 31, 2024
1 parent 78409a9 commit c91a4e7
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
20 changes: 12 additions & 8 deletions backend/network/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,19 +298,23 @@ func (c *WsConnection) listenAndHandle() error {
return err
}

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

var wg sync.WaitGroup
for pid, hand := range res.Hands {
wg.Add(1)
go func(pid uuid.UUID, hand []*gameLogic.WhiteCard) {
defer wg.Done()
msg := RpcOnWhiteCardPlayPhase{YourHand: hand,
BlackCard: res.NewBlackCard,
CardCzarId: res.NewCzarId,
WinnerId: res.WinnerId}

var msg RpcMessage
if res.GameEnded {
msg = RpcOnGameEnd{
WinnerId: res.WinnerId,
}
} else {
msg = RpcOnWhiteCardPlayPhase{YourHand: hand,
BlackCard: res.NewBlackCard,
CardCzarId: res.NewCzarId,
WinnerId: res.WinnerId}
}
encodedMsg, err := EncodeRpcMessage(msg)
if err != nil {
logger.Logger.Error("Cannot encode message to send to player")
Expand Down
12 changes: 12 additions & 0 deletions backend/network/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ const (
MsgCzarSelectCard
// Tx when the game state returns to white cards being played
MsgOnWhiteCardPlayPhase

// Tx when the game ends and goes back to the lobby
MsgOnGameEnd
)

type RpcMessageBody struct {
Expand Down Expand Up @@ -275,3 +278,12 @@ type RpcOnWhiteCardPlayPhase struct {
func (msg RpcOnWhiteCardPlayPhase) Type() RpcMessageType {
return MsgOnWhiteCardPlayPhase
}

type RpcOnGameEnd struct {
// Optional winner ID for game ending normally
WinnerId uuid.UUID `json:"winnerId,omitEmpty"`
}

func (msg RpcOnGameEnd) Type() RpcMessageType {
return MsgOnGameEnd
}
31 changes: 31 additions & 0 deletions cahfrontend/src/gameState/gameState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
BlackCard,
GameStateCzarJudgingCards,
GameStateInfo,
GameStateInLobby,
GameStateWhiteCardsBeingSelected,
Player,
WhiteCard,
Expand All @@ -13,6 +14,7 @@ import {
MsgNewOwner,
MsgOnCardPlayed,
MsgOnCzarJudgingPhase,
MsgOnGameEnd,
MsgOnJoin,
MsgOnPlayerCreate,
MsgOnPlayerDisconnect,
Expand All @@ -32,6 +34,7 @@ import {
RpcNewOwnerMsg,
RpcOnCardPlayedMsg,
RpcOnCzarJudgingPhaseMsg,
RpcOnGameEnd,
RpcOnJoinMsg,
RpcOnPlayerCreateMsg,
RpcOnPlayerDisconnectMsg,
Expand Down Expand Up @@ -373,6 +376,31 @@ class GameState {
this.onPlayerListChange?.(this.players);
}

private handleOnGameEnd(msg: RpcOnGameEnd) {
if (msg.winnerId) {
this.players = this.players.map((player) => {
if (player.id === msg.winnerId) {
return {
...player,
points: player.points + 1,
};
}

return player;
});
this.onPlayerListChange?.(this.players);
}

this.roundState.totalPlays = 0;
this.roundState.yourPlays = [];
this.roundState.yourHand = [];
this.roundState.currentCardCzarId = "not-in-round";
this.onRoundStateChange?.(structuredClone(this.roundState));

this.lobbyState.gameState = GameStateInLobby;
this.onLobbyStateChange?.(structuredClone(this.lobbyState));
}

/**
* Handles an RPC message from the server. When testing call the private method and ignore the "error".
*/
Expand Down Expand Up @@ -431,6 +459,9 @@ class GameState {
return this.handleOnWhiteCardPlayPhase(
rpcMessage.data as RpcOnWhiteCardPlayPhase,
);
case MsgOnGameEnd:
console.log("Handling on game end message");
return this.handleOnGameEnd(rpcMessage.data as RpcOnGameEnd);
default:
throw new Error(
`Cannot handle RPC message as type is not valid ${rpcMessage.type}`,
Expand Down

0 comments on commit c91a4e7

Please sign in to comment.