diff options
| author | tyropro <[email protected]> | 2026-05-11 11:00:10 +0100 |
|---|---|---|
| committer | tyropro <[email protected]> | 2026-05-11 11:01:33 +0100 |
| commit | 5a4e848bd00b4ad5796921d7907f179b3b5590cf (patch) | |
| tree | 35ddb68dde6b48d9a2bd485ab3c6cb4c36cde644 | |
| parent | b03856d83a149815b945c5b80629d7a359afce9a (diff) | |
refactor: changed create lobby to always include 1 player (the host) and to match the lobby creation on the client webui
| -rw-r--r-- | internal/api/game.go | 4 | ||||
| -rw-r--r-- | internal/api/lobbies.go | 67 | ||||
| -rw-r--r-- | internal/api/players.go | 36 |
3 files changed, 80 insertions, 27 deletions
diff --git a/internal/api/game.go b/internal/api/game.go index da15474..e1ffb2c 100644 --- a/internal/api/game.go +++ b/internal/api/game.go @@ -56,7 +56,7 @@ func (l *Lobby) CreateGame() { for player_uuid, next_player_uuid := range l.PlayerOrder {
// make the loop cyclic instead of terminate at the last player
if next_player_uuid == nil {
- next_player_uuid = l.FirstPlayer
+ next_player_uuid = &l.FirstPlayer
}
gamestate_player_order[player_uuid] = *next_player_uuid
@@ -102,7 +102,7 @@ func (l *Lobby) CreateGame() { gamestate.Players = gamestate_players
- gamestate.CurrentPlayerUUID = *l.FirstPlayer
+ gamestate.CurrentPlayerUUID = l.FirstPlayer
gamestate.Rooms = l.DefaultRooms()
diff --git a/internal/api/lobbies.go b/internal/api/lobbies.go index 3776ffb..70435aa 100644 --- a/internal/api/lobbies.go +++ b/internal/api/lobbies.go @@ -2,7 +2,9 @@ package api import ( "cluedo-backend/internal/status" + "fmt" "log" + "math/rand/v2" "net/http" "sync" @@ -26,7 +28,13 @@ type Lobby struct { Players map[string]Player `json:"players"` PlayerOrder map[string]*string `json:"player_order"` - FirstPlayer *string `json:"first_player"` + FirstPlayer string `json:"first_player"` + HostUUID string `json:"host_uuid"` + + PINEnabled bool `json:"pin_enabled"` + PIN *string `json:"pin"` + + RequestOnJoinEnabled bool `json:"request_on_join_enabled"` Options LobbyOptions `json:"options"` GameState *GameState `json:"game_state"` @@ -47,14 +55,22 @@ func NewLobbyManager() *LobbyManager { } } -func NewLobby(lobby_uuid string) Lobby { +func NewLobby(lobby_uuid string, host_uuid string, pin *string, request_on_join_enabled bool) Lobby { return Lobby{ - UUID: lobby_uuid, + UUID: lobby_uuid, + Players: make(map[string]Player), PlayerOrder: make(map[string]*string), - FirstPlayer: nil, - Options: DefaultLobbyOptions(), - GameState: nil, + FirstPlayer: host_uuid, + HostUUID: host_uuid, + + PINEnabled: pin != nil, + PIN: pin, + + RequestOnJoinEnabled: request_on_join_enabled, + + Options: DefaultLobbyOptions(), + GameState: nil, } } @@ -201,6 +217,14 @@ func get_lobby(lobby_manager *LobbyManager) gin.HandlerFunc { } } +type CreateLobbyRequest struct { + HostPlayerName string `json:"host_player_name"` + + LobbyPlayerName string `json:"lobby_player_name"` + PINEnabled bool `json:"pin_enabled"` + RequestOnJoinEnabled bool `json:"request_on_join_enabled"` +} + type CreateLobbyResponse struct { UUID string `json:"uuid"` } @@ -210,12 +234,33 @@ func create_lobby(lobby_manager *LobbyManager) gin.HandlerFunc { lobby_manager.mu.Lock() defer lobby_manager.mu.Unlock() + var req_body CreateLobbyRequest + + err := c.ShouldBindJSON(req_body) + if err != nil { + c.IndentedJSON(status.GenerateErrorResponse(status.BodyMarshallError)) + return + } + lobby_uuid := uuid.New().String() + host_player_uuid := uuid.New().String() + + var pin *string = nil + + if req_body.PINEnabled { + pin = generatePIN() + } - new_lobby := NewLobby(lobby_uuid) + new_lobby := NewLobby(lobby_uuid, host_player_uuid, pin, req_body.RequestOnJoinEnabled) lobby_manager.Lobbies[lobby_uuid] = new_lobby + new_player := NewPlayer(host_player_uuid, req_body.HostPlayerName) + + new_lobby.Players[host_player_uuid] = new_player + + new_lobby.PlayerOrder[host_player_uuid] = nil + response := CreateLobbyResponse{ UUID: lobby_uuid, } @@ -223,3 +268,11 @@ func create_lobby(lobby_manager *LobbyManager) gin.HandlerFunc { c.IndentedJSON(http.StatusCreated, response) } } + +func generatePIN() *string { + random_number := rand.IntN(10000) + + random_number_string := fmt.Sprintf("%04d", random_number) + + return &random_number_string +} diff --git a/internal/api/players.go b/internal/api/players.go index 5b3ddcd..aeb805a 100644 --- a/internal/api/players.go +++ b/internal/api/players.go @@ -108,24 +108,24 @@ func create_player(lobby_manager *LobbyManager) gin.HandlerFunc { lobby.Players[new_player_uuid] = new_player // edit player order to add newly created player to last - if lobby.FirstPlayer == nil { - // if no one is added, set the new player to point to the terminator - lobby.PlayerOrder[new_player_uuid] = nil - lobby.FirstPlayer = &new_player_uuid - } else { - // find the last player - for player_uuid, next_player_uuid := range lobby.PlayerOrder { - // if player is last, - // - set player to new player - // - set new player to terminator - if next_player_uuid == nil { - lobby.PlayerOrder[player_uuid] = &new_player_uuid - lobby.PlayerOrder[new_player_uuid] = nil - - break - } + // if lobby.FirstPlayer == nil { + // // if no one is added, set the new player to point to the terminator + // lobby.PlayerOrder[new_player_uuid] = nil + // lobby.FirstPlayer = new_player_uuid + // } else { + // find the last player + for player_uuid, next_player_uuid := range lobby.PlayerOrder { + // if player is last, + // - set player to new player + // - set new player to terminator + if next_player_uuid == nil { + lobby.PlayerOrder[player_uuid] = &new_player_uuid + lobby.PlayerOrder[new_player_uuid] = nil + + break } } + // } lobby_manager.Lobbies[lobby_uuid] = lobby @@ -159,8 +159,8 @@ func delete_player(lobby_manager *LobbyManager) gin.HandlerFunc { next_player_referenced := player_order[player_uuid] - if *lobby.FirstPlayer == player_uuid { - lobby.FirstPlayer = next_player_referenced + if lobby.FirstPlayer == player_uuid { + lobby.FirstPlayer = *next_player_referenced } else { for current_player_uuid, next_player_uuid := range player_order { if next_player_uuid == &player_uuid { |
