Skip to content

Commit f6e70fd

Browse files
lehaSVV2009nicolodavis
authored andcommitted
Add custom renderer parameter to lobby + clean up code (#353)
1 parent 97b3ea7 commit f6e70fd

6 files changed

Lines changed: 235 additions & 173 deletions

File tree

src/lobby/connection.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class _LobbyConnectionImpl {
2424
this.gameInstances.length = 0;
2525
const resp = await fetch(this._baseUrl());
2626
if (resp.status !== 200) {
27-
throw 'HTTP status ' + resp.status;
27+
throw new Error('HTTP status ' + resp.status);
2828
}
2929
const json = await resp.json();
3030
for (let gameName of json) {
@@ -49,7 +49,7 @@ class _LobbyConnectionImpl {
4949

5050
_getGameComponents(gameName) {
5151
for (let comp of this.gameComponents) {
52-
if (comp.game.name == gameName) return comp;
52+
if (comp.game.name === gameName) return comp;
5353
}
5454
}
5555

@@ -63,11 +63,11 @@ class _LobbyConnectionImpl {
6363
try {
6464
let inst = this._findPlayer(this.playerName);
6565
if (inst) {
66-
throw 'player has already joined ' + inst.gameID;
66+
throw new Error('player has already joined ' + inst.gameID);
6767
}
6868
inst = this._getGameInstance(gameID);
6969
if (!inst) {
70-
throw 'game instance ' + gameID + ' not found';
70+
throw new Error('game instance ' + gameID + ' not found');
7171
}
7272
const resp = await fetch(
7373
this._baseUrl() + '/' + gameName + '/' + gameID + '/join',
@@ -80,7 +80,7 @@ class _LobbyConnectionImpl {
8080
headers: { 'Content-Type': 'application/json' },
8181
}
8282
);
83-
if (resp.status !== 200) throw 'HTTP status ' + resp.status;
83+
if (resp.status !== 200) throw new Error('HTTP status ' + resp.status);
8484
const json = await resp.json();
8585
inst.players[Number.parseInt(playerID)].name = this.playerName;
8686
this.playerCredentials = json.playerCredentials;
@@ -92,7 +92,7 @@ class _LobbyConnectionImpl {
9292
async leave(gameName, gameID) {
9393
try {
9494
let inst = this._getGameInstance(gameID);
95-
if (!inst) throw 'game instance not found';
95+
if (!inst) throw new Error('game instance not found');
9696
for (let player of inst.players) {
9797
if (player.name === this.playerName) {
9898
const resp = await fetch(
@@ -106,13 +106,15 @@ class _LobbyConnectionImpl {
106106
headers: { 'Content-Type': 'application/json' },
107107
}
108108
);
109-
if (resp.status !== 200) throw 'HTTP status ' + resp.status;
109+
if (resp.status !== 200) {
110+
throw new Error('HTTP status ' + resp.status);
111+
}
110112
delete player.name;
111113
delete this.playerCredentials;
112114
return;
113115
}
114116
}
115-
throw 'player not found in room';
117+
throw new Error('player not found in room');
116118
} catch (error) {
117119
throw new Error('failed to leave room ' + gameID + ' (' + error + ')');
118120
}
@@ -130,20 +132,20 @@ class _LobbyConnectionImpl {
130132
async create(gameName, numPlayers) {
131133
try {
132134
const comp = this._getGameComponents(gameName);
133-
if (!comp) throw 'game not found';
135+
if (!comp) throw new Error('game not found');
134136
if (
135137
numPlayers < comp.game.minPlayers ||
136138
numPlayers > comp.game.maxPlayers
137139
)
138-
throw 'invalid number of players ' + numPlayers;
140+
throw new Error('invalid number of players ' + numPlayers);
139141
const resp = await fetch(this._baseUrl() + '/' + gameName + '/create', {
140142
method: 'POST',
141143
body: JSON.stringify({
142144
numPlayers: numPlayers,
143145
}),
144146
headers: { 'Content-Type': 'application/json' },
145147
});
146-
if (resp.status !== 200) throw 'HTTP status ' + resp.status;
148+
if (resp.status !== 200) throw new Error('HTTP status ' + resp.status);
147149
} catch (error) {
148150
throw new Error(
149151
'failed to create room for ' + gameName + ' (' + error + ')'

src/lobby/create-room-form.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,23 @@ class LobbyCreateRoomForm extends React.Component {
2020
numPlayers: 2,
2121
};
2222

23-
_createGameNameOption(game, idx) {
23+
_createGameNameOption = (game, idx) => {
2424
return (
2525
<option key={'name-option-' + idx} value={idx}>
2626
{game.game.name}
2727
</option>
2828
);
29-
}
29+
};
3030

31-
_createNumPlayersOption(idx) {
31+
_createNumPlayersOption = idx => {
3232
return (
3333
<option key={'num-option-' + idx} value={idx}>
3434
{idx}
3535
</option>
3636
);
37-
}
37+
};
3838

39-
_createNumPlayersRange(game) {
39+
_createNumPlayersRange = game => {
4040
if (!game.minPlayers) {
4141
game.minPlayers = 1;
4242
}
@@ -45,7 +45,7 @@ class LobbyCreateRoomForm extends React.Component {
4545
}
4646
console.assert(game.maxPlayers >= game.minPlayers);
4747
return [...new Array(game.maxPlayers + 1).keys()].slice(game.minPlayers);
48-
}
48+
};
4949

5050
render() {
5151
return (
@@ -59,39 +59,39 @@ class LobbyCreateRoomForm extends React.Component {
5959
<span>Players:</span>
6060
<select
6161
value={this.state.numPlayers}
62-
onChange={evt => this.onChangeNumPlayers(evt)}
62+
onChange={this.onChangeNumPlayers}
6363
>
6464
{this._createNumPlayersRange(
6565
this.props.games[this.state.selectedGame].game
6666
).map(this._createNumPlayersOption)}
6767
</select>
6868
<span className="buttons">
69-
<button onClick={() => this.onClickCreate()}>Create</button>
69+
<button onClick={this.onClickCreate}>Create</button>
7070
</span>
7171
</div>
7272
);
7373
}
7474

75-
onChangeNumPlayers(event) {
75+
onChangeNumPlayers = event => {
7676
this.setState({
7777
numPlayers: Number.parseInt(event.target.value),
7878
});
79-
}
79+
};
8080

81-
onChangeSelectedGame(event) {
81+
onChangeSelectedGame = event => {
8282
let idx = Number.parseInt(event.target.value);
8383
this.setState({
8484
selectedGame: idx,
8585
numPlayers: this._createNumPlayersRange(this.props.games[idx].game)[0],
8686
});
87-
}
87+
};
8888

89-
onClickCreate() {
89+
onClickCreate = () => {
9090
this.props.createGame(
9191
this.props.games[this.state.selectedGame].game.name,
9292
this.state.numPlayers
9393
);
94-
}
94+
};
9595
}
9696

9797
export default LobbyCreateRoomForm;

src/lobby/login-form.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ class LobbyLoginForm extends React.Component {
3030
<input
3131
type="text"
3232
value={this.state.playerName}
33-
onChange={evt => this.onChangePlayerName(evt)}
34-
onKeyPress={evt => this.onKeyPress(evt)}
33+
onChange={this.onChangePlayerName}
34+
onKeyPress={this.onKeyPress}
3535
/>
3636
<span className="buttons">
37-
<button className="buttons" onClick={() => this.onClickEnter()}>
37+
<button className="buttons" onClick={this.onClickEnter}>
3838
Enter
3939
</button>
4040
</span>
@@ -47,24 +47,24 @@ class LobbyLoginForm extends React.Component {
4747
);
4848
}
4949

50-
onClickEnter() {
50+
onClickEnter = () => {
5151
if (this.state.playerName === '') return;
5252
this.props.onEnter(this.state.playerName);
53-
}
53+
};
5454

55-
onKeyPress(event) {
55+
onKeyPress = event => {
5656
if (event.key === 'Enter') {
5757
this.onClickEnter();
5858
}
59-
}
59+
};
6060

61-
onChangePlayerName(event) {
61+
onChangePlayerName = event => {
6262
const name = event.target.value.trim();
6363
this.setState({
6464
playerName: name,
6565
nameErrorMsg: name.length > 0 ? '' : 'empty player name',
6666
});
67-
}
67+
};
6868
}
6969

7070
export default LobbyLoginForm;

0 commit comments

Comments
 (0)