diff --git a/backend/api/websocket.go b/backend/api/websocket.go index 55ceee1..c18c2de 100644 --- a/backend/api/websocket.go +++ b/backend/api/websocket.go @@ -95,6 +95,36 @@ func onPlayerJoin(client *socketio.Socket, room *types.Room, player *types.Playe game.OnRoomUpdate(room) }) + client.On("SetCardDeck", func(datas ...any) { + setCardDeckRequest := types.C2S_SetCardDeck{} + unpackData(datas, &setCardDeckRequest) + + if room.GameState != types.StateLobby { + client.Emit("Status", types.S2C_Status{ + IsError: true, + StatusCode: "game_already_running", + Message: "You can't change the card deck while the game is running", + }) + return + } + if !player.HasPermissionBit(types.PermissionHost) { + client.Emit("Status", types.S2C_Status{ + IsError: true, + StatusCode: "insufficient_permission", + Message: "You can't change the card deck unless you are host", + }) + return + } + if !game.SetCardDeck(room, setCardDeckRequest.CardDeckId) { + client.Emit("Status", types.S2C_Status{ + IsError: true, + StatusCode: "invalid_card_deck", + Message: "No card deck exists with this ID", + }) + return + } + }) + client.On("UpdatePlayer", func(datas ...any) { player.Mutex.Lock() defer player.Mutex.Unlock() diff --git a/backend/game/game.go b/backend/game/game.go index 9792ea0..2ee1613 100644 --- a/backend/game/game.go +++ b/backend/game/game.go @@ -123,6 +123,24 @@ func UpdateGameState(room *types.Room, newState types.GameState) { OnRoomUpdate(room) } +func SetCardDeck(room *types.Room, id int) bool { + if id < 0 || id > 1 { + return false + } + room.CardDeckId = id + OnRoomUpdate(room) + return true +} + +func CreateCardDeckObj(room *types.Room) { + switch room.CardDeckId { + case 0: + room.CardDeck = &decks.Classic{} + case 1: + room.CardDeck = &decks.HexV1{} + } +} + func BroadcastInRoom(room *types.Room, topic string, data interface{}) { for _, player := range room.Players { if !player.Connection.IsConnected || player.Connection.Socket == nil { @@ -179,7 +197,7 @@ func StartGame(room *types.Room) { if room.GameState != types.StateLobby { return } - room.CardDeck = &decks.Classic{} + CreateCardDeckObj(room) room.CardDeck.Init(room) UpdateGameState(room, types.StateRunning) UpdateAllPlayers(room) diff --git a/backend/types/websocket_packets.go b/backend/types/websocket_packets.go index 77b649f..16f917e 100644 --- a/backend/types/websocket_packets.go +++ b/backend/types/websocket_packets.go @@ -47,6 +47,9 @@ type S2C_PlayedCardUpdate struct { Card Card } +type C2S_SetCardDeck struct { + CardDeckId int +} type C2S_UpdatePlayer struct { PlayerId bson.ObjectID Username *string