Skip to content

Commit 0c3fe0d

Browse files
committed
fix(server): simplify GameRoom join code
1 parent 14ed8a6 commit 0c3fe0d

File tree

3 files changed

+18
-19
lines changed

3 files changed

+18
-19
lines changed

server/src/sc/server/gaming/GameRoom.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -213,23 +213,17 @@ public String getId() {
213213
* Join a client into this room.
214214
* Starts the game if full.
215215
*
216-
* @return true if successfully joined
216+
* @return true if successfully joined,
217+
* false if there was no free slot
217218
*/
218-
public synchronized boolean join(Client client) throws GameRoomException {
219-
for (PlayerSlot slot : this.playerSlots) {
220-
// find PlayerSlot that it not in use for the new Client
221-
if (slot.isEmpty() && !slot.isReserved()) {
222-
fillSlot(slot, client);
223-
return true;
224-
}
225-
}
226-
227-
if (this.playerSlots.size() < getMaximumPlayerCount()) {
228-
fillSlot(openSlot(), client);
229-
return true;
230-
}
231-
232-
return false;
219+
public synchronized boolean join(Client client) {
220+
PlayerSlot slot = playerSlots.stream()
221+
.filter(PlayerSlot::isFree).findFirst()
222+
.orElseGet(() -> playerSlots.size() < getMaximumPlayerCount() ? openSlot() : null);
223+
if(slot == null)
224+
return false;
225+
fillSlot(slot, client);
226+
return true;
233227
}
234228

235229
/**
@@ -238,7 +232,7 @@ public synchronized boolean join(Client client) throws GameRoomException {
238232
* @param openSlot PlayerSlot to fill
239233
* @param client Client to fill PlayerSlot
240234
*/
241-
synchronized void fillSlot(PlayerSlot openSlot, Client client) throws GameRoomException {
235+
synchronized void fillSlot(PlayerSlot openSlot, Client client) {
242236
openSlot.setClient(client); // sets role of Slot as PlayerRole
243237
client.send(new JoinedRoomResponse(getId()));
244238
startIfReady();
@@ -250,7 +244,7 @@ private boolean isReady() {
250244
}
251245

252246
/** Starts game if ready and not over. */
253-
private void startIfReady() throws GameRoomException {
247+
private void startIfReady() {
254248
logger.debug("startIfReady called");
255249
if (isOver()) {
256250
logger.warn("Game already over: {}", game);

server/src/sc/server/gaming/GameRoomManager.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,15 @@ public synchronized RoomWasJoinedEvent createAndJoinGame(Client client, String g
142142
public synchronized RoomWasJoinedEvent joinOrCreateGame(Client client, String gameType)
143143
throws RescuableClientException {
144144
for (GameRoom gameRoom : getGames()) {
145+
// TODO gameType isn't checked
145146
if (gameRoom.join(client)) {
146147
return roomJoined(gameRoom);
147148
}
148149
}
149150
return createAndJoinGame(client, gameType);
150151
}
151152

152-
private RoomWasJoinedEvent roomJoined(GameRoom room) {
153+
protected RoomWasJoinedEvent roomJoined(GameRoom room) {
153154
return new RoomWasJoinedEvent(room.getId(), room.getClients().size());
154155
}
155156

server/src/sc/server/gaming/PlayerSlot.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public GameRoom getRoom() {
2626
return this.room;
2727
}
2828

29+
public boolean isFree() {
30+
return isEmpty() && !isReserved();
31+
}
32+
2933
public boolean isEmpty() {
3034
return this.client == null;
3135
}

0 commit comments

Comments
 (0)