Skip to content

Commit 3e50dca

Browse files
jasonharrisonnicolodavis
authored andcommitted
Get specific instance of a room by its ID (#405)
1 parent 4964e3f commit 3e50dca

3 files changed

Lines changed: 97 additions & 0 deletions

File tree

docs/api/Lobby.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,18 @@ Returns an array of `rooms`. Each instance has fields:
9898

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

101+
#### Getting specific instance of a room by its ID
102+
103+
##### GET `/games/{name}/{id}`
104+
105+
Returns a room instance given its roomID.
106+
107+
Returns a room instance. Each instance has fields:
108+
109+
`roomID`: the ID of the game instance.
110+
111+
`players`: the list of seats and players that have joined the game, if any.
112+
101113
#### Client Authentication
102114

103115
All actions for an authenticated game require an additional payload field `credentials`, which must be the given secret associated with the player.

src/server/api.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,22 @@ export const addApiToServer = ({ app, db, games, lobbyConfig }) => {
130130
};
131131
});
132132

133+
router.get('/games/:name/:id', async ctx => {
134+
const gameName = ctx.params.name;
135+
const gameID = ctx.params.id;
136+
const room = await db.get(`${gameName}:${GameMetadataKey(gameID)}`);
137+
if (!room) {
138+
ctx.throw(404, 'Room ' + gameID + ' not found');
139+
}
140+
const strippedRoom = {
141+
roomID: gameID,
142+
players: Object.values(room.players).map(player => {
143+
return { id: player.id, name: player.name };
144+
}),
145+
};
146+
ctx.body = strippedRoom;
147+
});
148+
133149
router.post('/games/:name/:id/join', koaBody(), async ctx => {
134150
const gameName = ctx.params.name;
135151
const gameID = ctx.params.id;

src/server/api.test.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,75 @@ describe('.createApiServer', () => {
492492
});
493493
});
494494
});
495+
496+
describe('requesting room', () => {
497+
let db;
498+
beforeEach(() => {
499+
delete process.env.API_SECRET;
500+
db = {
501+
get: async () => {
502+
return {
503+
players: {
504+
'0': {
505+
id: 0,
506+
credentials: 'SECRET1',
507+
},
508+
'1': {
509+
id: 1,
510+
credentials: 'SECRET2',
511+
},
512+
},
513+
};
514+
},
515+
set: async () => {},
516+
list: async () => {
517+
return [
518+
'bar:bar-0',
519+
'bar:bar-0:metadata',
520+
'foo:foo-0',
521+
'foo:foo-0:metadata',
522+
'bar:bar-1',
523+
'bar:bar-1:metadata',
524+
];
525+
},
526+
};
527+
});
528+
529+
describe('when given room ID', async () => {
530+
let response;
531+
let room;
532+
beforeEach(async () => {
533+
let games = [Game({ name: 'foo' }), Game({ name: 'bar' })];
534+
let app = createApiServer({ db, games });
535+
response = await request(app.callback()).get('/games/bar/bar-0');
536+
room = JSON.parse(response.text);
537+
});
538+
539+
test('returns game ids', async () => {
540+
expect(room.roomID).toEqual('bar-0');
541+
});
542+
543+
test('returns player names', async () => {
544+
expect(room.players).toEqual([{ id: 0 }, { id: 1 }]);
545+
});
546+
});
547+
548+
describe('when given a non-existent room ID', async () => {
549+
let response;
550+
beforeEach(async () => {
551+
db.get = async () => {
552+
return null;
553+
};
554+
let games = [Game({ name: 'foo' })];
555+
let app = createApiServer({ db, games });
556+
response = await request(app.callback()).get('/games/bar/doesnotexist');
557+
});
558+
559+
test('throws error 404', async () => {
560+
expect(response.status).toEqual(404);
561+
});
562+
});
563+
});
495564
});
496565

497566
describe('.addApiToServer', () => {

0 commit comments

Comments
 (0)