Skip to content

Commit

Permalink
Merge branch 'master' into server-trailing-slash
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolodavis committed May 19, 2019
2 parents ac6f24a + f289379 commit 1e0a2da
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 56 deletions.
2 changes: 1 addition & 1 deletion docs/api/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Accepts two parameters, all required:

Returns all instances of the game named `name`.

Returns an array of `gameInstances`. Each instance has fields:
Returns an array of `rooms`. Each instance has fields:

`gameID`: the ID of the game instance.

Expand Down
14 changes: 7 additions & 7 deletions src/lobby/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class _LobbyConnectionImpl {
this.playerName = playerName || 'Visitor';
this.playerCredentials = playerCredentials;
this.server = server;
this.gameInstances = [];
this.rooms = [];
}

_baseUrl() {
Expand All @@ -21,7 +21,7 @@ class _LobbyConnectionImpl {

async refresh() {
try {
this.gameInstances.length = 0;
this.rooms.length = 0;
const resp = await fetch(this._baseUrl());
if (resp.status !== 200) {
throw new Error('HTTP status ' + resp.status);
Expand All @@ -31,18 +31,18 @@ class _LobbyConnectionImpl {
if (!this._getGameComponents(gameName)) continue;
const gameResp = await fetch(this._baseUrl() + '/' + gameName);
const gameJson = await gameResp.json();
for (let inst of gameJson.gameInstances) {
for (let inst of gameJson.rooms) {
inst.gameName = gameName;
}
this.gameInstances = this.gameInstances.concat(gameJson.gameInstances);
this.rooms = this.rooms.concat(gameJson.rooms);
}
} catch (error) {
throw new Error('failed to retrieve list of games (' + error + ')');
}
}

_getGameInstance(gameID) {
for (let inst of this.gameInstances) {
for (let inst of this.rooms) {
if (inst['gameID'] === gameID) return inst;
}
}
Expand All @@ -54,7 +54,7 @@ class _LobbyConnectionImpl {
}

_findPlayer(playerName) {
for (let inst of this.gameInstances) {
for (let inst of this.rooms) {
if (inst.players.some(player => player.name === playerName)) return inst;
}
}
Expand Down Expand Up @@ -125,7 +125,7 @@ class _LobbyConnectionImpl {
if (inst) {
await this.leave(inst.gameName, inst.gameID);
}
this.gameInstances = [];
this.rooms = [];
this.playerName = 'Visitor';
}

Expand Down
32 changes: 16 additions & 16 deletions src/lobby/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ import { LobbyConnection } from './connection.js';

describe('lobby', () => {
let lobby;
let gameInstance1, gameInstance2;
let room1, room2;
let jsonResult = [];
let nextStatus = 200;

beforeEach(async () => {
gameInstance1 = { gameID: 'gameID_1', players: [{ id: '0' }] };
gameInstance2 = { gameID: 'gameID_2', players: [{ id: '1' }] };
room1 = { gameID: 'gameID_1', players: [{ id: '0' }] };
room2 = { gameID: 'gameID_2', players: [{ id: '1' }] };
// result of connection requests
jsonResult = [
() => ['game1', 'game2'],
() => {
return { gameInstances: [gameInstance1] };
return { rooms: [room1] };
},
() => {
return { gameInstances: [gameInstance2] };
return { rooms: [room2] };
},
];
let nextResult = jsonResult.shift.bind(jsonResult);
Expand Down Expand Up @@ -60,7 +60,7 @@ describe('lobby', () => {
describe('get list of rooms', () => {
test('when the server requests succeed', async () => {
expect(fetch).toHaveBeenCalledTimes(3);
expect(lobby.gameInstances).toEqual([gameInstance1, gameInstance2]);
expect(lobby.rooms).toEqual([room1, room2]);
});
test('when the server request fails', async () => {
nextStatus = 404;
Expand All @@ -69,7 +69,7 @@ describe('lobby', () => {
} catch (error) {
expect(error).toBeInstanceOf(Error);
}
expect(lobby.gameInstances).toEqual([]);
expect(lobby.rooms).toEqual([]);
});
});

Expand All @@ -83,7 +83,7 @@ describe('lobby', () => {
test('when the room exists', async () => {
await lobby.join('game1', 'gameID_1', '0');
expect(fetch).toHaveBeenCalledTimes(4);
expect(lobby.gameInstances[0].players[0]).toEqual({
expect(lobby.rooms[0].players[0]).toEqual({
id: '0',
name: 'Bob',
});
Expand All @@ -95,10 +95,10 @@ describe('lobby', () => {
} catch (error) {
expect(error).toBeInstanceOf(Error);
}
expect(lobby.gameInstances).toEqual([gameInstance1, gameInstance2]);
expect(lobby.rooms).toEqual([room1, room2]);
});
test('when the seat is not available', async () => {
gameInstance1.players[0].name = 'Bob';
room1.players[0].name = 'Bob';
try {
await lobby.join('game1', 'gameID_3', '0');
} catch (error) {
Expand All @@ -114,7 +114,7 @@ describe('lobby', () => {
}
});
test('when the player has already joined another game', async () => {
gameInstance2.players[0].name = 'Bob';
room2.players[0].name = 'Bob';
try {
await lobby.join('game1', 'gameID_1', '0');
} catch (error) {
Expand All @@ -138,7 +138,7 @@ describe('lobby', () => {
test('when the room exists', async () => {
await lobby.leave('game1', 'gameID_1');
expect(fetch).toHaveBeenCalledTimes(5);
expect(lobby.gameInstances).toEqual([gameInstance1, gameInstance2]);
expect(lobby.rooms).toEqual([room1, room2]);
});
test('when the room does not exist', async () => {
try {
Expand All @@ -147,7 +147,7 @@ describe('lobby', () => {
expect(error).toBeInstanceOf(Error);
}
expect(fetch).toHaveBeenCalledTimes(4);
expect(lobby.gameInstances).toEqual([gameInstance1, gameInstance2]);
expect(lobby.rooms).toEqual([room1, room2]);
});
test('when the player is not in the room', async () => {
await lobby.leave('game1', 'gameID_1');
Expand All @@ -172,7 +172,7 @@ describe('lobby', () => {
beforeEach(async () => {});
test('when the player leaves the lobby', async () => {
await lobby.disconnect();
expect(lobby.gameInstances).toEqual([]);
expect(lobby.rooms).toEqual([]);
});
test('when the player had joined a room', async () => {
// result of request 'join'
Expand All @@ -185,7 +185,7 @@ describe('lobby', () => {
return {};
});
await lobby.disconnect();
expect(lobby.gameInstances).toEqual([]);
expect(lobby.rooms).toEqual([]);
});
});

Expand Down Expand Up @@ -232,7 +232,7 @@ describe('lobby', () => {
});
test('get list of rooms for supported games', async () => {
expect(fetch).toHaveBeenCalledTimes(2);
expect(lobby.gameInstances).toEqual([gameInstance1]);
expect(lobby.rooms).toEqual([room1]);
});
});
});
12 changes: 6 additions & 6 deletions src/lobby/react.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,13 @@ class Lobby extends React.Component {
return this.state.phase !== phase ? 'hidden' : 'phase';
};

renderRooms = (gameInstances, playerName) => {
return gameInstances.map(gameInstance => {
const { gameID, gameName, players } = gameInstance;
renderRooms = (rooms, playerName) => {
return rooms.map(room => {
const { gameID, gameName, players } = room;
return (
<LobbyRoomInstance
key={'instance-' + gameID}
gameInstance={{ gameID, gameName, players: Object.values(players) }}
room={{ gameID, gameName, players: Object.values(players) }}
playerName={playerName}
onClickJoin={this._joinRoom}
onClickLeave={this._leaveRoom}
Expand All @@ -233,7 +233,7 @@ class Lobby extends React.Component {
return renderer({
errorMsg,
gameComponents,
gameInstances: this.connection.gameInstances,
rooms: this.connection.rooms,
phase,
playerName,
runningGame,
Expand Down Expand Up @@ -272,7 +272,7 @@ class Lobby extends React.Component {
<div id="instances">
<table>
<tbody>
{this.renderRooms(this.connection.gameInstances, playerName)}
{this.renderRooms(this.connection.rooms, playerName)}
</tbody>
</table>
<span className="error-msg">
Expand Down
12 changes: 6 additions & 6 deletions src/lobby/react.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ describe('lobby', () => {

describe('exiting lobby', () => {
beforeEach(async () => {
lobby.instance().connection.gameInstances = [
lobby.instance().connection.rooms = [
{
gameID: 'gameID1',
players: {
Expand Down Expand Up @@ -198,7 +198,7 @@ describe('lobby', () => {

describe('creating a room', () => {
beforeEach(async () => {
lobby.instance().connection.gameInstances = [
lobby.instance().connection.rooms = [
{
gameID: 'gameID1',
players: { '0': { id: 0 } },
Expand Down Expand Up @@ -281,7 +281,7 @@ describe('lobby', () => {

describe('joining a room', () => {
beforeEach(async () => {
lobby.instance().connection.gameInstances = [
lobby.instance().connection.rooms = [
{
gameID: 'gameID1',
players: { '0': { id: 0 } },
Expand Down Expand Up @@ -336,7 +336,7 @@ describe('lobby', () => {

describe('leaving a room', () => {
beforeEach(async () => {
lobby.instance().connection.gameInstances = [
lobby.instance().connection.rooms = [
{
gameID: 'gameID1',
players: {
Expand Down Expand Up @@ -383,7 +383,7 @@ describe('lobby', () => {

describe('starting a game', () => {
beforeEach(async () => {
lobby.instance().connection.gameInstances = [
lobby.instance().connection.rooms = [
{
gameID: 'gameID1',
players: {
Expand Down Expand Up @@ -475,7 +475,7 @@ describe('lobby', () => {

describe('exiting during game', () => {
beforeEach(async () => {
lobby.instance().connection.gameInstances = [
lobby.instance().connection.rooms = [
{
gameID: 'gameID1',
players: {
Expand Down
20 changes: 10 additions & 10 deletions src/lobby/room-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import PropTypes from 'prop-types';

class LobbyRoomInstance extends React.Component {
static propTypes = {
gameInstance: PropTypes.shape({
room: PropTypes.shape({
gameName: PropTypes.string.isRequired,
gameID: PropTypes.string.isRequired,
players: PropTypes.array.isRequired,
Expand Down Expand Up @@ -85,20 +85,20 @@ class LobbyRoomInstance extends React.Component {
};

render() {
const inst = this.props.gameInstance;
const room = this.props.room;
let status = 'OPEN';
if (!inst.players.find(player => !player.name)) {
if (!room.players.find(player => !player.name)) {
status = 'RUNNING';
}
return (
<tr key={'line-' + inst.gameID}>
<td key={'cell-name-' + inst.gameID}>{inst.gameName}</td>
<td key={'cell-status-' + inst.gameID}>{status}</td>
<td key={'cell-seats-' + inst.gameID}>
{inst.players.map(this._createSeat).join(', ')}
<tr key={'line-' + room.gameID}>
<td key={'cell-name-' + room.gameID}>{room.gameName}</td>
<td key={'cell-status-' + room.gameID}>{status}</td>
<td key={'cell-seats-' + room.gameID}>
{room.players.map(this._createSeat).join(', ')}
</td>
<td key={'cell-buttons-' + inst.gameID}>
{this._createInstanceButtons(inst)}
<td key={'cell-buttons-' + room.gameID}>
{this._createInstanceButtons(room)}
</td>
</tr>
);
Expand Down
6 changes: 3 additions & 3 deletions src/server/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ export const addApiToServer = ({ app, db, games }) => {
router.get('/games/:name', async ctx => {
const gameName = ctx.params.name;
const gameList = await db.list();
let gameInstances = [];
let rooms = [];
for (let key of [...gameList]) {
if (isGameMetadataKey(key, gameName)) {
const gameID = key.slice(
gameName.length + 1,
key.lastIndexOf(':metadata')
);
const metadata = await db.get(key);
gameInstances.push({
rooms.push({
gameID: gameID,
players: Object.values(metadata.players).map(player => {
// strip away credentials
Expand All @@ -109,7 +109,7 @@ export const addApiToServer = ({ app, db, games }) => {
}
}
ctx.body = {
gameInstances: gameInstances,
rooms: rooms,
};
});

Expand Down
14 changes: 7 additions & 7 deletions src/server/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,26 +469,26 @@ describe('.createApiServer', () => {
});
describe('when given 2 games', async () => {
let response;
let instances;
let rooms;
beforeEach(async () => {
let games = [Game({ name: 'foo' }), Game({ name: 'bar' })];
let app = createApiServer({ db, games });
response = await request(app.callback()).get('/games/bar');
instances = JSON.parse(response.text).gameInstances;
rooms = JSON.parse(response.text).rooms;
});

test('returns instances of the selected game', async () => {
expect(instances).toHaveLength(2);
expect(rooms).toHaveLength(2);
});

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

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

0 comments on commit 1e0a2da

Please sign in to comment.