Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set public setting when creating lobby. #66

Merged
merged 2 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion features/lobbies.feature
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,44 @@ 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
Then "green" should have received only these lobbies
| 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 | public |
| dhgp75mn2bll | 1 | true |




Expand Down
8 changes: 6 additions & 2 deletions features/support/steps/network.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { After, DataTable, Given, Then, When } from '@cucumber/cucumber'
import assert from 'assert'
import { World } from '../world'

After(async function (this: World) {
Expand Down Expand Up @@ -178,7 +177,12 @@ Then('{string} should have received only these lobbies', function (this: World,
delete lobby[key]
}
})
assert.notStrictEqual(lobby, row)
koenbollen marked this conversation as resolved.
Show resolved Hide resolved
const want = row as any
Object.keys(row).forEach(key => {
if (`${lobby[key] as string}` !== `${want[key] as string}`) {
throw new Error(`expected ${key} to be ${want[key] as string} but got ${lobby[key] as string}`)
}
})
})
if (player.lastReceivedLobbies.length !== expectedLobbies.hashes().length) {
throw new Error(`expected ${expectedLobbies.hashes().length} lobbies but got ${player.lastReceivedLobbies.length}`)
Expand Down
2 changes: 1 addition & 1 deletion internal/signaling/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions internal/signaling/stores/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
}
Expand Down Expand Up @@ -279,7 +279,7 @@ func (s *PostgresStore) ListLobbies(ctx context.Context, game, filter string) ([

var lobbies []Lobby
rows, err := s.DB.Query(ctx, `
SELECT code, peers, meta
SELECT code, peers, public, meta
FROM lobbies
WHERE game = $1
AND public = true
Expand All @@ -294,7 +294,7 @@ func (s *PostgresStore) ListLobbies(ctx context.Context, game, filter string) ([
for rows.Next() {
var lobby Lobby
var peers []string
err = rows.Scan(&lobby.Code, &peers, &lobby.CustomData)
err = rows.Scan(&lobby.Code, &peers, &lobby.Public, &lobby.CustomData)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/signaling/stores/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down