Skip to content

Commit

Permalink
Get specific instance of a room by its ID (#405)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonharrison authored and nicolodavis committed May 24, 2019
1 parent 4964e3f commit 3e50dca
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
12 changes: 12 additions & 0 deletions docs/api/Lobby.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ Returns an array of `rooms`. Each instance has fields:

`players`: the list of seats and players that have joined the game, if any.

#### Getting specific instance of a room by its ID

##### GET `/games/{name}/{id}`

Returns a room instance given its roomID.

Returns a room instance. Each instance has fields:

`roomID`: the ID of the game instance.

`players`: the list of seats and players that have joined the game, if any.

#### Client Authentication

All actions for an authenticated game require an additional payload field `credentials`, which must be the given secret associated with the player.
16 changes: 16 additions & 0 deletions src/server/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,22 @@ export const addApiToServer = ({ app, db, games, lobbyConfig }) => {
};
});

router.get('/games/:name/:id', async ctx => {
const gameName = ctx.params.name;
const gameID = ctx.params.id;
const room = await db.get(`${gameName}:${GameMetadataKey(gameID)}`);
if (!room) {
ctx.throw(404, 'Room ' + gameID + ' not found');
}
const strippedRoom = {
roomID: gameID,
players: Object.values(room.players).map(player => {
return { id: player.id, name: player.name };
}),
};
ctx.body = strippedRoom;
});

router.post('/games/:name/:id/join', koaBody(), async ctx => {
const gameName = ctx.params.name;
const gameID = ctx.params.id;
Expand Down
69 changes: 69 additions & 0 deletions src/server/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,75 @@ describe('.createApiServer', () => {
});
});
});

describe('requesting room', () => {
let db;
beforeEach(() => {
delete process.env.API_SECRET;
db = {
get: async () => {
return {
players: {
'0': {
id: 0,
credentials: 'SECRET1',
},
'1': {
id: 1,
credentials: 'SECRET2',
},
},
};
},
set: async () => {},
list: async () => {
return [
'bar:bar-0',
'bar:bar-0:metadata',
'foo:foo-0',
'foo:foo-0:metadata',
'bar:bar-1',
'bar:bar-1:metadata',
];
},
};
});

describe('when given room ID', async () => {
let response;
let room;
beforeEach(async () => {
let games = [Game({ name: 'foo' }), Game({ name: 'bar' })];
let app = createApiServer({ db, games });
response = await request(app.callback()).get('/games/bar/bar-0');
room = JSON.parse(response.text);
});

test('returns game ids', async () => {
expect(room.roomID).toEqual('bar-0');
});

test('returns player names', async () => {
expect(room.players).toEqual([{ id: 0 }, { id: 1 }]);
});
});

describe('when given a non-existent room ID', async () => {
let response;
beforeEach(async () => {
db.get = async () => {
return null;
};
let games = [Game({ name: 'foo' })];
let app = createApiServer({ db, games });
response = await request(app.callback()).get('/games/bar/doesnotexist');
});

test('throws error 404', async () => {
expect(response.status).toEqual(404);
});
});
});
});

describe('.addApiToServer', () => {
Expand Down

0 comments on commit 3e50dca

Please sign in to comment.