Skip to content

Commit cf96955

Browse files
c-wdelucis
andauthored
Add option to exclude games from public listing (#653)
Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>
1 parent 6aff09c commit cf96955

4 files changed

Lines changed: 33 additions & 16 deletions

File tree

docs/documentation/api/Lobby.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,14 @@ Options are:
5252

5353
Creates a new authenticated room for a game named `name`.
5454

55-
Accepts two parameters:
55+
Accepts three parameters:
5656

5757
`numPlayers` (required): the number of players.
5858

5959
`setupData` (optional): custom object that is passed to the game `setup` function.
6060

61+
`unlisted` (optional): if set to `true`, the room will be excluded from the public list of room instances.
62+
6163
Returns `roomID`, which is the ID of the newly created game instance.
6264

6365
#### Joining a game

src/server/api.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ describe('.createApiServer', () => {
9696
'1': 'red',
9797
},
9898
},
99+
unlisted: true,
99100
});
100101
});
101102

@@ -142,6 +143,7 @@ describe('.createApiServer', () => {
142143
'1': 'red',
143144
}),
144145
}),
146+
unlisted: true,
145147
})
146148
);
147149
});
@@ -1041,7 +1043,7 @@ describe('.createApiServer', () => {
10411043
beforeEach(() => {
10421044
delete process.env.API_SECRET;
10431045
db = new AsyncStorage({
1044-
fetch: async () => {
1046+
fetch: async gameID => {
10451047
return {
10461048
metadata: {
10471049
players: {
@@ -1054,6 +1056,7 @@ describe('.createApiServer', () => {
10541056
credentials: 'SECRET2',
10551057
},
10561058
},
1059+
unlisted: gameID === 'bar-4',
10571060
},
10581061
};
10591062
},
@@ -1063,6 +1066,7 @@ describe('.createApiServer', () => {
10631066
'foo-1': { gameName: 'foo' },
10641067
'bar-2': { gameName: 'bar' },
10651068
'bar-3': { gameName: 'bar' },
1069+
'bar-4': { gameName: 'bar' },
10661070
};
10671071
const keys = Object.keys(metadata);
10681072
if (opts && opts.gameName) {

src/server/api.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ import { InitializeGame } from '../core/initialize';
1616
import * as StorageAPI from './db/base';
1717
import { Server, Game } from '../types';
1818

19-
const createGameMetadata = ({ gameName }): Server.GameMetadata => ({
19+
const createGameMetadata = ({ gameName, unlisted }): Server.GameMetadata => ({
2020
gameName,
21+
unlisted,
2122
players: {},
2223
setupData: {},
2324
});
@@ -31,15 +32,17 @@ const createGameMetadata = ({ gameName }): Server.GameMetadata => ({
3132
* @param {object} setupData - User-defined object that's available
3233
* during game setup.
3334
* @param {object } lobbyConfig - Configuration options for the lobby.
35+
* @param {boolean} unlisted - Whether the game should be excluded from public listing.
3436
*/
3537
export const CreateGame = async (
3638
db: StorageAPI.Sync | StorageAPI.Async,
3739
game: Game,
3840
numPlayers: number,
3941
setupData: object,
40-
lobbyConfig: Server.LobbyConfig
42+
lobbyConfig: Server.LobbyConfig,
43+
unlisted: boolean
4144
) => {
42-
const gameMetadata = createGameMetadata({ gameName: game.name });
45+
const gameMetadata = createGameMetadata({ gameName: game.name, unlisted });
4346

4447
const state = InitializeGame({
4548
game,
@@ -106,6 +109,8 @@ export const addApiToServer = ({
106109
const gameName = ctx.params.name;
107110
// User-data to pass to the game setup function.
108111
const setupData = ctx.request.body.setupData;
112+
// Whether the game should be excluded from public listing.
113+
const unlisted = ctx.request.body.unlisted;
109114
// The number of players for this game instance.
110115
let numPlayers = parseInt(ctx.request.body.numPlayers);
111116
if (!numPlayers) {
@@ -118,7 +123,8 @@ export const addApiToServer = ({
118123
game,
119124
numPlayers,
120125
setupData,
121-
lobbyConfig
126+
lobbyConfig,
127+
unlisted
122128
);
123129

124130
ctx.body = {
@@ -134,15 +140,17 @@ export const addApiToServer = ({
134140
const { metadata } = await (db as StorageAPI.Async).fetch(gameID, {
135141
metadata: true,
136142
});
137-
rooms.push({
138-
gameID,
139-
players: Object.values(metadata.players).map((player: any) => {
140-
// strip away credentials
141-
const { credentials, ...strippedInfo } = player;
142-
return strippedInfo;
143-
}),
144-
setupData: metadata.setupData,
145-
});
143+
if (!metadata.unlisted) {
144+
rooms.push({
145+
gameID,
146+
players: Object.values(metadata.players).map((player: any) => {
147+
// strip away credentials
148+
const { credentials, ...strippedInfo } = player;
149+
return strippedInfo;
150+
}),
151+
setupData: metadata.setupData,
152+
});
153+
}
146154
}
147155
ctx.body = {
148156
rooms: rooms,
@@ -243,6 +251,7 @@ export const addApiToServer = ({
243251
const gameID = ctx.params.id;
244252
const playerID = ctx.request.body.playerID;
245253
const credentials = ctx.request.body.credentials;
254+
const unlisted = ctx.request.body.unlisted;
246255
const { metadata } = await (db as StorageAPI.Async).fetch(gameID, {
247256
metadata: true,
248257
});
@@ -280,7 +289,8 @@ export const addApiToServer = ({
280289
game,
281290
numPlayers,
282291
setupData,
283-
lobbyConfig
292+
lobbyConfig,
293+
unlisted
284294
);
285295
metadata.nextRoomID = nextRoomID;
286296

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ export namespace Server {
278278
setupData: any;
279279
gameover?: any;
280280
nextRoomID?: string;
281+
unlisted?: boolean;
281282
}
282283

283284
export interface LobbyConfig {

0 commit comments

Comments
 (0)