Skip to content

Commit f289379

Browse files
jasonharrisonnicolodavis
authored andcommitted
Rename gameInstances to rooms (#402)
1 parent 4957968 commit f289379

8 files changed

Lines changed: 56 additions & 56 deletions

File tree

docs/api/API.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Accepts two parameters, all required:
5656

5757
Returns all instances of the game named `name`.
5858

59-
Returns an array of `gameInstances`. Each instance has fields:
59+
Returns an array of `rooms`. Each instance has fields:
6060

6161
`gameID`: the ID of the game instance.
6262

src/lobby/connection.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class _LobbyConnectionImpl {
1212
this.playerName = playerName || 'Visitor';
1313
this.playerCredentials = playerCredentials;
1414
this.server = server;
15-
this.gameInstances = [];
15+
this.rooms = [];
1616
}
1717

1818
_baseUrl() {
@@ -21,7 +21,7 @@ class _LobbyConnectionImpl {
2121

2222
async refresh() {
2323
try {
24-
this.gameInstances.length = 0;
24+
this.rooms.length = 0;
2525
const resp = await fetch(this._baseUrl());
2626
if (resp.status !== 200) {
2727
throw new Error('HTTP status ' + resp.status);
@@ -31,18 +31,18 @@ class _LobbyConnectionImpl {
3131
if (!this._getGameComponents(gameName)) continue;
3232
const gameResp = await fetch(this._baseUrl() + '/' + gameName);
3333
const gameJson = await gameResp.json();
34-
for (let inst of gameJson.gameInstances) {
34+
for (let inst of gameJson.rooms) {
3535
inst.gameName = gameName;
3636
}
37-
this.gameInstances = this.gameInstances.concat(gameJson.gameInstances);
37+
this.rooms = this.rooms.concat(gameJson.rooms);
3838
}
3939
} catch (error) {
4040
throw new Error('failed to retrieve list of games (' + error + ')');
4141
}
4242
}
4343

4444
_getGameInstance(gameID) {
45-
for (let inst of this.gameInstances) {
45+
for (let inst of this.rooms) {
4646
if (inst['gameID'] === gameID) return inst;
4747
}
4848
}
@@ -54,7 +54,7 @@ class _LobbyConnectionImpl {
5454
}
5555

5656
_findPlayer(playerName) {
57-
for (let inst of this.gameInstances) {
57+
for (let inst of this.rooms) {
5858
if (inst.players.some(player => player.name === playerName)) return inst;
5959
}
6060
}
@@ -125,7 +125,7 @@ class _LobbyConnectionImpl {
125125
if (inst) {
126126
await this.leave(inst.gameName, inst.gameID);
127127
}
128-
this.gameInstances = [];
128+
this.rooms = [];
129129
this.playerName = 'Visitor';
130130
}
131131

src/lobby/connection.test.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ import { LobbyConnection } from './connection.js';
1010

1111
describe('lobby', () => {
1212
let lobby;
13-
let gameInstance1, gameInstance2;
13+
let room1, room2;
1414
let jsonResult = [];
1515
let nextStatus = 200;
1616

1717
beforeEach(async () => {
18-
gameInstance1 = { gameID: 'gameID_1', players: [{ id: '0' }] };
19-
gameInstance2 = { gameID: 'gameID_2', players: [{ id: '1' }] };
18+
room1 = { gameID: 'gameID_1', players: [{ id: '0' }] };
19+
room2 = { gameID: 'gameID_2', players: [{ id: '1' }] };
2020
// result of connection requests
2121
jsonResult = [
2222
() => ['game1', 'game2'],
2323
() => {
24-
return { gameInstances: [gameInstance1] };
24+
return { rooms: [room1] };
2525
},
2626
() => {
27-
return { gameInstances: [gameInstance2] };
27+
return { rooms: [room2] };
2828
},
2929
];
3030
let nextResult = jsonResult.shift.bind(jsonResult);
@@ -60,7 +60,7 @@ describe('lobby', () => {
6060
describe('get list of rooms', () => {
6161
test('when the server requests succeed', async () => {
6262
expect(fetch).toHaveBeenCalledTimes(3);
63-
expect(lobby.gameInstances).toEqual([gameInstance1, gameInstance2]);
63+
expect(lobby.rooms).toEqual([room1, room2]);
6464
});
6565
test('when the server request fails', async () => {
6666
nextStatus = 404;
@@ -69,7 +69,7 @@ describe('lobby', () => {
6969
} catch (error) {
7070
expect(error).toBeInstanceOf(Error);
7171
}
72-
expect(lobby.gameInstances).toEqual([]);
72+
expect(lobby.rooms).toEqual([]);
7373
});
7474
});
7575

@@ -83,7 +83,7 @@ describe('lobby', () => {
8383
test('when the room exists', async () => {
8484
await lobby.join('game1', 'gameID_1', '0');
8585
expect(fetch).toHaveBeenCalledTimes(4);
86-
expect(lobby.gameInstances[0].players[0]).toEqual({
86+
expect(lobby.rooms[0].players[0]).toEqual({
8787
id: '0',
8888
name: 'Bob',
8989
});
@@ -95,10 +95,10 @@ describe('lobby', () => {
9595
} catch (error) {
9696
expect(error).toBeInstanceOf(Error);
9797
}
98-
expect(lobby.gameInstances).toEqual([gameInstance1, gameInstance2]);
98+
expect(lobby.rooms).toEqual([room1, room2]);
9999
});
100100
test('when the seat is not available', async () => {
101-
gameInstance1.players[0].name = 'Bob';
101+
room1.players[0].name = 'Bob';
102102
try {
103103
await lobby.join('game1', 'gameID_3', '0');
104104
} catch (error) {
@@ -114,7 +114,7 @@ describe('lobby', () => {
114114
}
115115
});
116116
test('when the player has already joined another game', async () => {
117-
gameInstance2.players[0].name = 'Bob';
117+
room2.players[0].name = 'Bob';
118118
try {
119119
await lobby.join('game1', 'gameID_1', '0');
120120
} catch (error) {
@@ -138,7 +138,7 @@ describe('lobby', () => {
138138
test('when the room exists', async () => {
139139
await lobby.leave('game1', 'gameID_1');
140140
expect(fetch).toHaveBeenCalledTimes(5);
141-
expect(lobby.gameInstances).toEqual([gameInstance1, gameInstance2]);
141+
expect(lobby.rooms).toEqual([room1, room2]);
142142
});
143143
test('when the room does not exist', async () => {
144144
try {
@@ -147,7 +147,7 @@ describe('lobby', () => {
147147
expect(error).toBeInstanceOf(Error);
148148
}
149149
expect(fetch).toHaveBeenCalledTimes(4);
150-
expect(lobby.gameInstances).toEqual([gameInstance1, gameInstance2]);
150+
expect(lobby.rooms).toEqual([room1, room2]);
151151
});
152152
test('when the player is not in the room', async () => {
153153
await lobby.leave('game1', 'gameID_1');
@@ -172,7 +172,7 @@ describe('lobby', () => {
172172
beforeEach(async () => {});
173173
test('when the player leaves the lobby', async () => {
174174
await lobby.disconnect();
175-
expect(lobby.gameInstances).toEqual([]);
175+
expect(lobby.rooms).toEqual([]);
176176
});
177177
test('when the player had joined a room', async () => {
178178
// result of request 'join'
@@ -185,7 +185,7 @@ describe('lobby', () => {
185185
return {};
186186
});
187187
await lobby.disconnect();
188-
expect(lobby.gameInstances).toEqual([]);
188+
expect(lobby.rooms).toEqual([]);
189189
});
190190
});
191191

@@ -232,7 +232,7 @@ describe('lobby', () => {
232232
});
233233
test('get list of rooms for supported games', async () => {
234234
expect(fetch).toHaveBeenCalledTimes(2);
235-
expect(lobby.gameInstances).toEqual([gameInstance1]);
235+
expect(lobby.rooms).toEqual([room1]);
236236
});
237237
});
238238
});

src/lobby/react.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,13 @@ class Lobby extends React.Component {
209209
return this.state.phase !== phase ? 'hidden' : 'phase';
210210
};
211211

212-
renderRooms = (gameInstances, playerName) => {
213-
return gameInstances.map(gameInstance => {
214-
const { gameID, gameName, players } = gameInstance;
212+
renderRooms = (rooms, playerName) => {
213+
return rooms.map(room => {
214+
const { gameID, gameName, players } = room;
215215
return (
216216
<LobbyRoomInstance
217217
key={'instance-' + gameID}
218-
gameInstance={{ gameID, gameName, players: Object.values(players) }}
218+
room={{ gameID, gameName, players: Object.values(players) }}
219219
playerName={playerName}
220220
onClickJoin={this._joinRoom}
221221
onClickLeave={this._leaveRoom}
@@ -233,7 +233,7 @@ class Lobby extends React.Component {
233233
return renderer({
234234
errorMsg,
235235
gameComponents,
236-
gameInstances: this.connection.gameInstances,
236+
rooms: this.connection.rooms,
237237
phase,
238238
playerName,
239239
runningGame,
@@ -272,7 +272,7 @@ class Lobby extends React.Component {
272272
<div id="instances">
273273
<table>
274274
<tbody>
275-
{this.renderRooms(this.connection.gameInstances, playerName)}
275+
{this.renderRooms(this.connection.rooms, playerName)}
276276
</tbody>
277277
</table>
278278
<span className="error-msg">

src/lobby/react.test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ describe('lobby', () => {
129129

130130
describe('exiting lobby', () => {
131131
beforeEach(async () => {
132-
lobby.instance().connection.gameInstances = [
132+
lobby.instance().connection.rooms = [
133133
{
134134
gameID: 'gameID1',
135135
players: {
@@ -198,7 +198,7 @@ describe('lobby', () => {
198198

199199
describe('creating a room', () => {
200200
beforeEach(async () => {
201-
lobby.instance().connection.gameInstances = [
201+
lobby.instance().connection.rooms = [
202202
{
203203
gameID: 'gameID1',
204204
players: { '0': { id: 0 } },
@@ -281,7 +281,7 @@ describe('lobby', () => {
281281

282282
describe('joining a room', () => {
283283
beforeEach(async () => {
284-
lobby.instance().connection.gameInstances = [
284+
lobby.instance().connection.rooms = [
285285
{
286286
gameID: 'gameID1',
287287
players: { '0': { id: 0 } },
@@ -336,7 +336,7 @@ describe('lobby', () => {
336336

337337
describe('leaving a room', () => {
338338
beforeEach(async () => {
339-
lobby.instance().connection.gameInstances = [
339+
lobby.instance().connection.rooms = [
340340
{
341341
gameID: 'gameID1',
342342
players: {
@@ -383,7 +383,7 @@ describe('lobby', () => {
383383

384384
describe('starting a game', () => {
385385
beforeEach(async () => {
386-
lobby.instance().connection.gameInstances = [
386+
lobby.instance().connection.rooms = [
387387
{
388388
gameID: 'gameID1',
389389
players: {
@@ -475,7 +475,7 @@ describe('lobby', () => {
475475

476476
describe('exiting during game', () => {
477477
beforeEach(async () => {
478-
lobby.instance().connection.gameInstances = [
478+
lobby.instance().connection.rooms = [
479479
{
480480
gameID: 'gameID1',
481481
players: {

src/lobby/room-instance.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import PropTypes from 'prop-types';
1111

1212
class LobbyRoomInstance extends React.Component {
1313
static propTypes = {
14-
gameInstance: PropTypes.shape({
14+
room: PropTypes.shape({
1515
gameName: PropTypes.string.isRequired,
1616
gameID: PropTypes.string.isRequired,
1717
players: PropTypes.array.isRequired,
@@ -85,20 +85,20 @@ class LobbyRoomInstance extends React.Component {
8585
};
8686

8787
render() {
88-
const inst = this.props.gameInstance;
88+
const room = this.props.room;
8989
let status = 'OPEN';
90-
if (!inst.players.find(player => !player.name)) {
90+
if (!room.players.find(player => !player.name)) {
9191
status = 'RUNNING';
9292
}
9393
return (
94-
<tr key={'line-' + inst.gameID}>
95-
<td key={'cell-name-' + inst.gameID}>{inst.gameName}</td>
96-
<td key={'cell-status-' + inst.gameID}>{status}</td>
97-
<td key={'cell-seats-' + inst.gameID}>
98-
{inst.players.map(this._createSeat).join(', ')}
94+
<tr key={'line-' + room.gameID}>
95+
<td key={'cell-name-' + room.gameID}>{room.gameName}</td>
96+
<td key={'cell-status-' + room.gameID}>{status}</td>
97+
<td key={'cell-seats-' + room.gameID}>
98+
{room.players.map(this._createSeat).join(', ')}
9999
</td>
100-
<td key={'cell-buttons-' + inst.gameID}>
101-
{this._createInstanceButtons(inst)}
100+
<td key={'cell-buttons-' + room.gameID}>
101+
{this._createInstanceButtons(room)}
102102
</td>
103103
</tr>
104104
);

src/server/api.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,15 @@ export const addApiToServer = ({ app, db, games }) => {
9191
router.get('/games/:name', async ctx => {
9292
const gameName = ctx.params.name;
9393
const gameList = await db.list();
94-
let gameInstances = [];
94+
let rooms = [];
9595
for (let key of [...gameList]) {
9696
if (isGameMetadataKey(key, gameName)) {
9797
const gameID = key.slice(
9898
gameName.length + 1,
9999
key.lastIndexOf(':metadata')
100100
);
101101
const metadata = await db.get(key);
102-
gameInstances.push({
102+
rooms.push({
103103
gameID: gameID,
104104
players: Object.values(metadata.players).map(player => {
105105
// strip away credentials
@@ -109,7 +109,7 @@ export const addApiToServer = ({ app, db, games }) => {
109109
}
110110
}
111111
ctx.body = {
112-
gameInstances: gameInstances,
112+
rooms: rooms,
113113
};
114114
});
115115

src/server/api.test.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -469,26 +469,26 @@ describe('.createApiServer', () => {
469469
});
470470
describe('when given 2 games', async () => {
471471
let response;
472-
let instances;
472+
let rooms;
473473
beforeEach(async () => {
474474
let games = [Game({ name: 'foo' }), Game({ name: 'bar' })];
475475
let app = createApiServer({ db, games });
476476
response = await request(app.callback()).get('/games/bar');
477-
instances = JSON.parse(response.text).gameInstances;
477+
rooms = JSON.parse(response.text).rooms;
478478
});
479479

480480
test('returns instances of the selected game', async () => {
481-
expect(instances).toHaveLength(2);
481+
expect(rooms).toHaveLength(2);
482482
});
483483

484484
test('returns game ids', async () => {
485-
expect(instances[0].gameID).toEqual('bar-0');
486-
expect(instances[1].gameID).toEqual('bar-1');
485+
expect(rooms[0].gameID).toEqual('bar-0');
486+
expect(rooms[1].gameID).toEqual('bar-1');
487487
});
488488

489489
test('returns player names', async () => {
490-
expect(instances[0].players).toEqual([{ id: 0 }, { id: 1 }]);
491-
expect(instances[1].players).toEqual([{ id: 0 }, { id: 1 }]);
490+
expect(rooms[0].players).toEqual([{ id: 0 }, { id: 1 }]);
491+
expect(rooms[1].players).toEqual([{ id: 0 }, { id: 1 }]);
492492
});
493493
});
494494
});

0 commit comments

Comments
 (0)