diff --git a/features/lobbies.feature b/features/lobbies.feature index ec9f7f1..69c65c5 100644 --- a/features/lobbies.feature +++ b/features/lobbies.feature @@ -20,7 +20,12 @@ Feature: Lobby Discovery Given "green" creates a network for game "f666036d-d9e1-4d70-b0c3-4a68b24a9884" And "blue" is connected and ready for game "f666036d-d9e1-4d70-b0c3-4a68b24a9884" - When "blue" creates a lobby + When "blue" creates a lobby with these settings: + """ + { + "public": true + } + """ And "blue" receives the network event "lobby" with the argument "prb67ouj837u" When "green" requests all lobbies @@ -28,6 +33,31 @@ Feature: Lobby Discovery | code | playerCount | | prb67ouj837u | 1 | + Scenario: Only list public lobbies + Given "green" creates a network for game "f666036d-d9e1-4d70-b0c3-4a68b24a9884" + And "blue" is connected and ready for game "f666036d-d9e1-4d70-b0c3-4a68b24a9884" + And "yellow" is connected and ready for game "f666036d-d9e1-4d70-b0c3-4a68b24a9884" + + When "blue" creates a lobby with these settings: + """ + { + "public": true + } + """ + And "blue" receives the network event "lobby" with the argument "dhgp75mn2bll" + And "yellow" creates a lobby with these settings: + """ + { + "public": false + } + """ + And "yellow" receives the network event "lobby" with the argument "1qva9vyurwbbl" + + When "green" requests all lobbies + Then "green" should have received only these lobbies + | code | playerCount | + | dhgp75mn2bll | 1 | + diff --git a/internal/signaling/peer.go b/internal/signaling/peer.go index d9635a9..bcb2eb5 100644 --- a/internal/signaling/peer.go +++ b/internal/signaling/peer.go @@ -336,7 +336,7 @@ func (p *Peer) HandleCreatePacket(ctx context.Context, packet CreatePacket) erro p.Lobby = util.GenerateLobbyCode(ctx) } - err := p.store.CreateLobby(ctx, p.Game, p.Lobby, p.ID) + err := p.store.CreateLobby(ctx, p.Game, p.Lobby, p.ID, packet.Public) if err != nil { if err == stores.ErrLobbyExists { continue diff --git a/internal/signaling/stores/postgres.go b/internal/signaling/stores/postgres.go index 46e898f..dfdd6a0 100644 --- a/internal/signaling/stores/postgres.go +++ b/internal/signaling/stores/postgres.go @@ -140,7 +140,7 @@ func (s *PostgresStore) Publish(ctx context.Context, topic string, data []byte) return nil } -func (s *PostgresStore) CreateLobby(ctx context.Context, game, lobbyCode, peerID string) error { +func (s *PostgresStore) CreateLobby(ctx context.Context, game, lobbyCode, peerID string, public bool) error { if len(lobbyCode) > 20 { logger := logging.GetLogger(ctx) logger.Warn("lobby code too long", zap.String("lobbyCode", lobbyCode)) @@ -154,9 +154,9 @@ func (s *PostgresStore) CreateLobby(ctx context.Context, game, lobbyCode, peerID now := util.Now(ctx) res, err := s.DB.Exec(ctx, ` INSERT INTO lobbies (code, game, public, created_at, updated_at) - VALUES ($1, $2, true, $3, $3) + VALUES ($1, $2, $3, $4, $4) ON CONFLICT DO NOTHING - `, lobbyCode, game, now) + `, lobbyCode, game, public, now) if err != nil { return err } diff --git a/internal/signaling/stores/shared.go b/internal/signaling/stores/shared.go index f02442f..517390e 100644 --- a/internal/signaling/stores/shared.go +++ b/internal/signaling/stores/shared.go @@ -16,7 +16,7 @@ var ErrInvalidPeerID = errors.New("invalid peer id") type SubscriptionCallback func(context.Context, []byte) type Store interface { - CreateLobby(ctx context.Context, game, lobby, id string) error + CreateLobby(ctx context.Context, game, lobby, id string, public bool) error JoinLobby(ctx context.Context, game, lobby, id string) ([]string, error) IsPeerInLobby(ctx context.Context, game, lobby, id string) (bool, error) LeaveLobby(ctx context.Context, game, lobby, id string) ([]string, error)