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

feat(server): Add option to exclude rooms from public listing #653

Merged
merged 2 commits into from
Apr 27, 2020
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
4 changes: 3 additions & 1 deletion docs/documentation/api/Lobby.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@ Options are:

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

Accepts two parameters:
Accepts three parameters:

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

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

`unlisted` (optional): if set to `true`, the room will be excluded from the public list of room instances.

Returns `roomID`, which is the ID of the newly created game instance.

#### Joining a game
Expand Down
6 changes: 5 additions & 1 deletion src/server/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ describe('.createApiServer', () => {
'1': 'red',
},
},
unlisted: true,
});
});

Expand Down Expand Up @@ -142,6 +143,7 @@ describe('.createApiServer', () => {
'1': 'red',
}),
}),
unlisted: true,
})
);
});
Expand Down Expand Up @@ -1041,7 +1043,7 @@ describe('.createApiServer', () => {
beforeEach(() => {
delete process.env.API_SECRET;
db = new AsyncStorage({
fetch: async () => {
fetch: async gameID => {
return {
metadata: {
players: {
Expand All @@ -1054,6 +1056,7 @@ describe('.createApiServer', () => {
credentials: 'SECRET2',
},
},
unlisted: gameID === 'bar-4',
},
};
},
Expand All @@ -1063,6 +1066,7 @@ describe('.createApiServer', () => {
'foo-1': { gameName: 'foo' },
'bar-2': { gameName: 'bar' },
'bar-3': { gameName: 'bar' },
'bar-4': { gameName: 'bar' },
};
const keys = Object.keys(metadata);
if (opts && opts.gameName) {
Expand Down
38 changes: 24 additions & 14 deletions src/server/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import { InitializeGame } from '../core/initialize';
import * as StorageAPI from './db/base';
import { Server, Game } from '../types';

const createGameMetadata = ({ gameName }): Server.GameMetadata => ({
const createGameMetadata = ({ gameName, unlisted }): Server.GameMetadata => ({
gameName,
unlisted,
players: {},
setupData: {},
});
Expand All @@ -31,15 +32,17 @@ const createGameMetadata = ({ gameName }): Server.GameMetadata => ({
* @param {object} setupData - User-defined object that's available
* during game setup.
* @param {object } lobbyConfig - Configuration options for the lobby.
* @param {boolean} unlisted - Whether the game should be excluded from public listing.
*/
export const CreateGame = async (
db: StorageAPI.Sync | StorageAPI.Async,
game: Game,
numPlayers: number,
setupData: object,
lobbyConfig: Server.LobbyConfig
lobbyConfig: Server.LobbyConfig,
unlisted: boolean
) => {
const gameMetadata = createGameMetadata({ gameName: game.name });
const gameMetadata = createGameMetadata({ gameName: game.name, unlisted });

const state = InitializeGame({
game,
Expand Down Expand Up @@ -106,6 +109,8 @@ export const addApiToServer = ({
const gameName = ctx.params.name;
// User-data to pass to the game setup function.
const setupData = ctx.request.body.setupData;
// Whether the game should be excluded from public listing.
const unlisted = ctx.request.body.unlisted;
// The number of players for this game instance.
let numPlayers = parseInt(ctx.request.body.numPlayers);
if (!numPlayers) {
Expand All @@ -118,7 +123,8 @@ export const addApiToServer = ({
game,
numPlayers,
setupData,
lobbyConfig
lobbyConfig,
unlisted
);

ctx.body = {
Expand All @@ -134,15 +140,17 @@ export const addApiToServer = ({
const { metadata } = await (db as StorageAPI.Async).fetch(gameID, {
metadata: true,
});
rooms.push({
gameID,
players: Object.values(metadata.players).map((player: any) => {
// strip away credentials
const { credentials, ...strippedInfo } = player;
return strippedInfo;
}),
setupData: metadata.setupData,
});
if (!metadata.unlisted) {
rooms.push({
gameID,
players: Object.values(metadata.players).map((player: any) => {
// strip away credentials
const { credentials, ...strippedInfo } = player;
return strippedInfo;
}),
setupData: metadata.setupData,
});
}
}
ctx.body = {
rooms: rooms,
Expand Down Expand Up @@ -243,6 +251,7 @@ export const addApiToServer = ({
const gameID = ctx.params.id;
const playerID = ctx.request.body.playerID;
const credentials = ctx.request.body.credentials;
const unlisted = ctx.request.body.unlisted;
const { metadata } = await (db as StorageAPI.Async).fetch(gameID, {
metadata: true,
});
Expand Down Expand Up @@ -280,7 +289,8 @@ export const addApiToServer = ({
game,
numPlayers,
setupData,
lobbyConfig
lobbyConfig,
unlisted
);
metadata.nextRoomID = nextRoomID;

Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ export namespace Server {
setupData: any;
gameover?: any;
nextRoomID?: string;
unlisted?: boolean;
}

export interface LobbyConfig {
Expand Down