Repository navigation
Add game-wide player counts #406
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| Feature: Player Counts | ||
|
|
||
| Background: | ||
| Given the "signaling" backend is running | ||
| And the "testproxy" backend is running | ||
| And "green" is connected as "1u8fw4aph5ypt" and ready for game "f666036d-d9e1-4d70-b0c3-4a68b24a9884" | ||
|
|
||
| Scenario: Count yourself before joining a lobby | ||
| Then "green" should see 1 players online and 0 in a lobby | ||
|
|
||
| Scenario: Count private lobby membership and leaving | ||
| When "green" creates a lobby | ||
| Then "green" should see 1 players online and 1 in a lobby | ||
| And "green" leaves the lobby | ||
| And "green" should see 1 players online and 0 in a lobby | ||
|
|
||
| Scenario: Count connected sessions only for this game | ||
| Given these peers exist: | ||
| | peer | game | disconnected | | ||
| | peer0 | f666036d-d9e1-4d70-b0c3-4a68b24a9884 | false | | ||
| | peer1 | f666036d-d9e1-4d70-b0c3-4a68b24a9884 | true | | ||
| | peer2 | 4307bd86-e1df-41b8-b9df-e22afcf084bd | false | | ||
| And these lobbies exist: | ||
| | code | game | playerCount | public | | ||
| | private | f666036d-d9e1-4d70-b0c3-4a68b24a9884 | 2 | false | | ||
| | public | f666036d-d9e1-4d70-b0c3-4a68b24a9884 | 2 | true | | ||
| | other | 4307bd86-e1df-41b8-b9df-e22afcf084bd | 3 | true | | ||
| Then "green" should see 2 players online and 1 in a lobby | ||
|
|
||
| Scenario: Stop counting a player who closes their network | ||
| Given "blue" is connected as "h5yzwyizlwao" and ready for game "f666036d-d9e1-4d70-b0c3-4a68b24a9884" | ||
| And "green,blue" are joined in a lobby | ||
| Then "green" should see 2 players online and 2 in a lobby | ||
| When "blue" disconnects | ||
| Then "green" receives the network event "disconnected" | ||
| And "green" should see 1 players online and 1 in a lobby | ||
|
|
||
| Scenario: Filter lobby membership while keeping online counts game-wide | ||
| Given these peers exist: | ||
| | peer | game | disconnected | | ||
| | peer0 | f666036d-d9e1-4d70-b0c3-4a68b24a9884 | false | | ||
| | peer1 | f666036d-d9e1-4d70-b0c3-4a68b24a9884 | true | | ||
| | peer2 | f666036d-d9e1-4d70-b0c3-4a68b24a9884 | false | | ||
| | peer3 | 4307bd86-e1df-41b8-b9df-e22afcf084bd | false | | ||
| And these lobbies exist: | ||
| | code | game | playerCount | public | custom_data | | ||
| | private | f666036d-d9e1-4d70-b0c3-4a68b24a9884 | 2 | false | {"kind":"ranked"} | | ||
| | ranked | f666036d-d9e1-4d70-b0c3-4a68b24a9884 | 2 | true | {"kind":"ranked"} | | ||
| | casual | f666036d-d9e1-4d70-b0c3-4a68b24a9884 | 3 | true | {"kind":"casual"} | | ||
| | other | 4307bd86-e1df-41b8-b9df-e22afcf084bd | 4 | true | {"kind":"other"} | | ||
| Then "green" should see 3 players online and 2 in a lobby | ||
| And "green" should see 3 players online and 2 in a lobby matching '{}' | ||
| And "green" should see 3 players online and 1 in a lobby matching '{"kind":"ranked"}' | ||
| And "green" should see 3 players online and 2 in a lobby matching '{"kind":"casual"}' | ||
| And "green" should see 3 players online and 0 in a lobby matching '{"kind":"other"}' | ||
| And "green" should see 3 players online and 1 in a lobby matching '{"code":"private"}' | ||
| And "green" should see 3 players online and 1 in a lobby matching '{"kind":{"$in":["ranked"]},"playerCount":{"$gte":2}}' | ||
|
|
||
| Scenario: Reject invalid lobby filters | ||
| Then "green" should reject player counts matching '{"kind":{"$invalid":"ranked"}}' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -324,6 +324,34 @@ func (s *PostgresStore) GetLobby(ctx context.Context, game, lobbyCode string) (L | |
| return lobby, nil | ||
| } | ||
|
|
||
| func (s *PostgresStore) GetPlayerCounts(ctx context.Context, game, country, region, filter string) (PlayerCounts, error) { | ||
| var counts PlayerCounts | ||
| if filter == "" { | ||
| filter = "{}" | ||
| } | ||
| where, values, err := s.filterConverter.Convert([]byte(filter), 4) | ||
| if err != nil { | ||
| return counts, fmt.Errorf("invalid filter: %w", err) | ||
| } | ||
| err = s.DB.QueryRow(ctx, ` | ||
| WITH game_lobbies AS ( | ||
| SELECT code, peers, custom_data, | ||
| COALESCE(ARRAY_LENGTH(peers, 1), 0) AS "playerCount", | ||
| created_at AS "createdAt", updated_at AS "updatedAt", | ||
| lobby_latency_estimate(peers, $2, $3) AS latency | ||
| FROM lobbies | ||
| WHERE game = $1 | ||
| ) | ||
| SELECT COUNT(*), COUNT(*) FILTER (WHERE EXISTS ( | ||
| SELECT 1 FROM game_lobbies | ||
| WHERE peers.peer = ANY(game_lobbies.peers) AND (`+where+`) | ||
| )) | ||
| FROM peers | ||
| WHERE game = $1 AND disconnected = FALSE | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks like a pretty heavy query for a player count. Doesn't the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not MATERIALIZED so it will just be inlined instead of running that query first and then using the result for the outer query. |
||
| `, append([]any{game, country, region}, values...)...).Scan(&counts.Online, &counts.InLobby) | ||
| return counts, err | ||
| } | ||
|
|
||
| func (s *PostgresStore) ListLobbies(ctx context.Context, game string, country, region string, filter, sort string, limit int) ([]Lobby, error) { | ||
| // TODO: Remove this. | ||
| if filter == "" { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| BEGIN; | ||
|
|
||
| DROP INDEX "peers_connected_game"; | ||
|
|
||
| COMMIT; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| BEGIN; | ||
|
|
||
| CREATE INDEX "peers_connected_game" ON "peers" ("game") INCLUDE ("peer") | ||
| WHERE "disconnected" = FALSE; | ||
|
|
||
| COMMIT; |
Uh oh!
There was an error while loading. Please reload this page.