Skip to content

Commit adb251d

Browse files
adngdbdelucis
andauthored
refactor(server): Use the term "match" in the API and Lobby (#704)
Closes #703 * GameMetadata -> MatchMetadata * rooms -> matches * API exposes `matchID`, make Lobby use that instead of `gameID` or `roomID`. * CreateGame -> CreateMatch * gameID, roomID -> matchID * Document Lobby API endpoints. * nextRoomID -> nextMatchID * Update error messages. * gameList -> matchList * Use "match" wherever it makes sense in lobby code. * Replace "room" with "match" in documentation. * Rename 'game' to 'match' in comments and test names. Co-authored-by: Chris Swithinbank <swithinbank@gmail.com> * doesGameRequireAuthentication -> doesMatchRequireAuthentication * Rename gameID to matchID in database interface functions. Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>
1 parent f32dc76 commit adb251d

18 files changed

Lines changed: 444 additions & 363 deletions

docs/documentation/api/Client.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ The `Board` component will receive the following as `props`:
113113

114114
11. `playerID`: The player ID associated with the client.
115115

116-
12. `gameMetadata`: An object containing the players that have joined
117-
the game from a [room](/api/Lobby.md).
116+
12. `matchMetadata`: An object containing the players that have joined
117+
the game from a [match](/api/Lobby.md).
118118

119119
Example:
120120

docs/documentation/api/Lobby.md

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ import { Lobby } from 'boardgame.io/react';
2121

2222
### Server-side API
2323

24-
The [Server](/api/Server) hosts the Lobby REST API that can be used to create and join rooms. It is particularly useful when you want to
24+
The [Server](/api/Server) hosts the Lobby REST API that can be used to create and join matches. It is particularly useful when you want to
2525
authenticate clients to prove that they have the right to send
2626
actions on behalf of a player.
2727

28-
Authenticated games are created with server-side tokens for each player. You can create a room with the `create` API call, and join a player to a room with the `join` API call.
28+
Authenticated games are created with server-side tokens for each player. You can create a match with the `create` API call, and join a player to a match with the `join` API call.
2929

3030
A game that is authenticated will not accept moves from a client on behalf of a player without the appropriate credential token.
3131

32-
Use the `create` API call to create a room that requires credential tokens. When you call the `join` API, you can retrieve the credential token for a particular player.
32+
Use the `create` API call to create a match that requires credential tokens. When you call the `join` API, you can retrieve the credential token for a particular player.
3333

3434
#### Configuration
3535

@@ -45,27 +45,27 @@ Options are:
4545
- `apiPort`: If specified, it runs the Lobby API in a separate Koa server on this port. Otherwise, it shares the same Koa server runnning on the default boardgame.io `port`.
4646
- `apiCallback`: Called when the Koa server is ready. Only applicable if `apiPort` is specified.
4747

48-
#### Creating a room
48+
#### Creating a match
4949

5050
##### POST `/games/{name}/create`
5151

52-
Creates a new authenticated room for a game named `name`.
52+
Creates a new authenticated match for a game named `name`.
5353

5454
Accepts three parameters:
5555

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

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

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

62-
Returns `roomID`, which is the ID of the newly created game instance.
62+
Returns `matchID`, which is the ID of the newly created game instance.
6363

6464
#### Joining a game
6565

6666
##### POST `/games/{name}/{id}/join`
6767

68-
Allows a player to join a particular room instance `id` of a game named `name`.
68+
Allows a player to join a particular match instance `id` of a game named `name`.
6969

7070
Accepts three JSON body parameters:
7171

@@ -81,7 +81,7 @@ Returns `playerCredentials` which is the token this player will require to authe
8181

8282
##### POST `/games/{name}/{id}/update`
8383

84-
Rename and/or update additional information of a user in the room instance `id` of a game named `name` previously joined by the player.
84+
Rename and/or update additional information of a user in the match instance `id` of a game named `name` previously joined by the player.
8585

8686
Accepts four parameters, requires at least one of the two optional parameters:
8787

@@ -93,41 +93,41 @@ Accepts four parameters, requires at least one of the two optional parameters:
9393

9494
`data` (optional): additional information associated to the player.
9595

96-
#### Leaving a room
96+
#### Leaving a match
9797

9898
##### POST `/games/{name}/{id}/leave`
9999

100-
Leave the room instance `id` of a game named `name` previously joined by the player.
100+
Leave the match instance `id` of a game named `name` previously joined by the player.
101101

102102
Accepts two parameters, all required:
103103

104104
`playerID`: the ID used by the player in the game (0, 1...).
105105

106106
`credentials`: the authentication token of the player.
107107

108-
#### Listing all room instances of a given game
108+
#### Listing all match instances of a given game
109109

110110
##### GET `/games/{name}`
111111

112-
Returns all room instances of the game named `name`.
112+
Returns all match instances of the game named `name`.
113113

114-
Returns an array of `rooms`. Each instance has fields:
114+
Returns an array of `matches`. Each instance has fields:
115115

116-
`roomID`: the ID of the room instance.
116+
`matchID`: the ID of the match instance.
117117

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

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

122-
#### Getting specific instance of a room by its ID
122+
#### Getting specific instance of a match by its ID
123123

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

126-
Returns a room instance given its roomID.
126+
Returns a match instance given its matchID.
127127

128-
Returns a room instance. Each instance has fields:
128+
Returns a match instance. Each instance has fields:
129129

130-
`roomID`: the ID of the room instance.
130+
`matchID`: the ID of the match instance.
131131

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

@@ -143,14 +143,14 @@ All actions for an authenticated game require an additional payload field `crede
143143

144144
`{name}` (required): the name of the game being played again.
145145

146-
`{id}` (required): the ID of the previous finished room.
146+
`{id}` (required): the ID of the previous finished match.
147147

148-
Given a previous room, generates a room ID where users should go if they want to play again. Creates this new room if it didn't exist before.
148+
Given a previous match, generates a match ID where users should go if they want to play again. Creates this new match if it didn't exist before.
149149

150150
Accepts these parameters:
151151

152152
`playerID` (required): the player ID of the player on the previous game.
153153

154154
`credentials` (required): player's credentials.
155155

156-
Returns `nextRoomID`, which is the ID of the newly created room that the user should go to play again.
156+
Returns `nextMatchID`, which is the ID of the newly created match that the user should go to play again.

src/lobby/connection.js

Lines changed: 24 additions & 24 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.rooms = [];
15+
this.matches = [];
1616
}
1717

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

2222
async refresh() {
2323
try {
24-
this.rooms.length = 0;
24+
this.matches.length = 0;
2525
const resp = await fetch(this._baseUrl());
2626
if (resp.status !== 200) {
2727
throw new Error('HTTP status ' + resp.status);
@@ -31,19 +31,19 @@ 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.rooms) {
34+
for (let inst of gameJson.matches) {
3535
inst.gameName = gameName;
3636
}
37-
this.rooms = this.rooms.concat(gameJson.rooms);
37+
this.matches = this.matches.concat(gameJson.matches);
3838
}
3939
} catch (error) {
40-
throw new Error('failed to retrieve list of games (' + error + ')');
40+
throw new Error('failed to retrieve list of matches (' + error + ')');
4141
}
4242
}
4343

44-
_getGameInstance(gameID) {
45-
for (let inst of this.rooms) {
46-
if (inst['gameID'] === gameID) return inst;
44+
_getMatchInstance(matchID) {
45+
for (let inst of this.matches) {
46+
if (inst['matchID'] === matchID) return inst;
4747
}
4848
}
4949

@@ -54,23 +54,23 @@ class _LobbyConnectionImpl {
5454
}
5555

5656
_findPlayer(playerName) {
57-
for (let inst of this.rooms) {
57+
for (let inst of this.matches) {
5858
if (inst.players.some(player => player.name === playerName)) return inst;
5959
}
6060
}
6161

62-
async join(gameName, gameID, playerID) {
62+
async join(gameName, matchID, playerID) {
6363
try {
6464
let inst = this._findPlayer(this.playerName);
6565
if (inst) {
66-
throw new Error('player has already joined ' + inst.gameID);
66+
throw new Error('player has already joined ' + inst.matchID);
6767
}
68-
inst = this._getGameInstance(gameID);
68+
inst = this._getMatchInstance(matchID);
6969
if (!inst) {
70-
throw new Error('game instance ' + gameID + ' not found');
70+
throw new Error('game instance ' + matchID + ' not found');
7171
}
7272
const resp = await fetch(
73-
this._baseUrl() + '/' + gameName + '/' + gameID + '/join',
73+
this._baseUrl() + '/' + gameName + '/' + matchID + '/join',
7474
{
7575
method: 'POST',
7676
body: JSON.stringify({
@@ -85,18 +85,18 @@ class _LobbyConnectionImpl {
8585
inst.players[Number.parseInt(playerID)].name = this.playerName;
8686
this.playerCredentials = json.playerCredentials;
8787
} catch (error) {
88-
throw new Error('failed to join room ' + gameID + ' (' + error + ')');
88+
throw new Error('failed to join match ' + matchID + ' (' + error + ')');
8989
}
9090
}
9191

92-
async leave(gameName, gameID) {
92+
async leave(gameName, matchID) {
9393
try {
94-
let inst = this._getGameInstance(gameID);
95-
if (!inst) throw new Error('game instance not found');
94+
let inst = this._getMatchInstance(matchID);
95+
if (!inst) throw new Error('match instance not found');
9696
for (let player of inst.players) {
9797
if (player.name === this.playerName) {
9898
const resp = await fetch(
99-
this._baseUrl() + '/' + gameName + '/' + gameID + '/leave',
99+
this._baseUrl() + '/' + gameName + '/' + matchID + '/leave',
100100
{
101101
method: 'POST',
102102
body: JSON.stringify({
@@ -114,18 +114,18 @@ class _LobbyConnectionImpl {
114114
return;
115115
}
116116
}
117-
throw new Error('player not found in room');
117+
throw new Error('player not found in match');
118118
} catch (error) {
119-
throw new Error('failed to leave room ' + gameID + ' (' + error + ')');
119+
throw new Error('failed to leave match ' + matchID + ' (' + error + ')');
120120
}
121121
}
122122

123123
async disconnect() {
124124
let inst = this._findPlayer(this.playerName);
125125
if (inst) {
126-
await this.leave(inst.gameName, inst.gameID);
126+
await this.leave(inst.gameName, inst.matchID);
127127
}
128-
this.rooms = [];
128+
this.matches = [];
129129
this.playerName = 'Visitor';
130130
}
131131

@@ -148,7 +148,7 @@ class _LobbyConnectionImpl {
148148
if (resp.status !== 200) throw new Error('HTTP status ' + resp.status);
149149
} catch (error) {
150150
throw new Error(
151-
'failed to create room for ' + gameName + ' (' + error + ')'
151+
'failed to create match for ' + gameName + ' (' + error + ')'
152152
);
153153
}
154154
}

0 commit comments

Comments
 (0)