mirror of
https://github.com/HexCardGames/HexDeck.git
synced 2025-09-05 03:08:39 +02:00
feat(backend): implement changing card deck
This commit is contained in:
@ -95,6 +95,36 @@ func onPlayerJoin(client *socketio.Socket, room *types.Room, player *types.Playe
|
|||||||
game.OnRoomUpdate(room)
|
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) {
|
client.On("UpdatePlayer", func(datas ...any) {
|
||||||
player.Mutex.Lock()
|
player.Mutex.Lock()
|
||||||
defer player.Mutex.Unlock()
|
defer player.Mutex.Unlock()
|
||||||
|
@ -123,6 +123,24 @@ func UpdateGameState(room *types.Room, newState types.GameState) {
|
|||||||
OnRoomUpdate(room)
|
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{}) {
|
func BroadcastInRoom(room *types.Room, topic string, data interface{}) {
|
||||||
for _, player := range room.Players {
|
for _, player := range room.Players {
|
||||||
if !player.Connection.IsConnected || player.Connection.Socket == nil {
|
if !player.Connection.IsConnected || player.Connection.Socket == nil {
|
||||||
@ -179,7 +197,7 @@ func StartGame(room *types.Room) {
|
|||||||
if room.GameState != types.StateLobby {
|
if room.GameState != types.StateLobby {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
room.CardDeck = &decks.Classic{}
|
CreateCardDeckObj(room)
|
||||||
room.CardDeck.Init(room)
|
room.CardDeck.Init(room)
|
||||||
UpdateGameState(room, types.StateRunning)
|
UpdateGameState(room, types.StateRunning)
|
||||||
UpdateAllPlayers(room)
|
UpdateAllPlayers(room)
|
||||||
|
@ -47,6 +47,9 @@ type S2C_PlayedCardUpdate struct {
|
|||||||
Card Card
|
Card Card
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type C2S_SetCardDeck struct {
|
||||||
|
CardDeckId int
|
||||||
|
}
|
||||||
type C2S_UpdatePlayer struct {
|
type C2S_UpdatePlayer struct {
|
||||||
PlayerId bson.ObjectID
|
PlayerId bson.ObjectID
|
||||||
Username *string
|
Username *string
|
||||||
|
Reference in New Issue
Block a user