Skip to content

Commit b2c2db8

Browse files
committed
feat(network): send playerCount in RoomWasJoinedEvent
1 parent ddcb6bd commit b2c2db8

File tree

4 files changed

+23
-27
lines changed

4 files changed

+23
-27
lines changed

sdk/src/server-api/sc/protocol/responses/RoomWasJoinedEvent.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import com.thoughtworks.xstream.annotations.XStreamAlias
44
import com.thoughtworks.xstream.annotations.XStreamAsAttribute
55
import sc.protocol.ResponsePacket
66

7-
/** Sent to all administrative clients when a player joined a game via a JoinRoomRequest.
8-
* @param existing whether the joined room has existed beforehand or was newly created. */
7+
/** Sent to all administrative clients after a player joined a GameRoom via a JoinRoomRequest.
8+
* @param playerCount the number of players in the room after the join. */
99
@XStreamAlias(value = "joinedGameRoom")
1010
data class RoomWasJoinedEvent(
1111
@XStreamAsAttribute
1212
val roomId: String,
1313
@XStreamAsAttribute
14-
val existing: Boolean
14+
val playerCount: Int
1515
): ResponsePacket

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

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.io.FileWriter;
3131
import java.io.IOException;
3232
import java.util.*;
33+
import java.util.stream.Collectors;
3334

3435
/**
3536
* A wrapper for an actual <code>GameInstance</code>. GameInstances are provided
@@ -48,7 +49,7 @@ public class GameRoom implements IGameListener {
4849
private boolean pauseRequested = false;
4950
private ObservingClient replayObserver;
5051

51-
public final IGameInstance game;
52+
public final IGameInstance game; // TODO make inaccessible
5253
public final List<ObserverRole> observers = new ArrayList<>();
5354

5455
public enum GameStatus {
@@ -461,23 +462,19 @@ private Player resolvePlayer(Client client) throws GameRoomException {
461462
}
462463

463464
/** Get {@link PlayerRole Players} that occupy a slot. */
464-
private Collection<PlayerRole> getPlayers() {
465-
ArrayList<PlayerRole> clients = new ArrayList<>();
466-
for (PlayerSlot slot : this.playerSlots) {
467-
if (!slot.isEmpty()) {
468-
clients.add(slot.getRole());
469-
}
470-
}
471-
return clients;
465+
public Collection<PlayerRole> getPlayers() {
466+
return playerSlots.stream()
467+
.filter(p -> !p.isEmpty())
468+
.map(PlayerSlot::getRole)
469+
.collect(Collectors.toList());
472470
}
473471

474472
/** Get Server {@link IClient Clients} of all {@link PlayerRole Players}. */
475473
public Collection<IClient> getClients() {
476-
ArrayList<IClient> clients = new ArrayList<>();
477-
for (PlayerRole slot : getPlayers()) {
478-
clients.add(slot.getClient());
479-
}
480-
return clients;
474+
return playerSlots.stream()
475+
.filter(p -> !p.isEmpty())
476+
.map(PlayerSlot::getClient)
477+
.collect(Collectors.toList());
481478
}
482479

483480
/** Add a Server {@link Client Client} in the role of an Observer. */

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public synchronized RoomWasJoinedEvent createAndJoinGame(Client client, String g
128128
throws RescuableClientException {
129129
GameRoom room = createGame(gameType);
130130
if (room.join(client)) {
131-
return new RoomWasJoinedEvent(room.getId(), false);
131+
return roomJoined(room);
132132
}
133133
return null;
134134
}
@@ -144,13 +144,16 @@ public synchronized RoomWasJoinedEvent joinOrCreateGame(Client client, String ga
144144
throws RescuableClientException {
145145
for (GameRoom gameRoom : getGames()) {
146146
if (gameRoom.join(client)) {
147-
return new RoomWasJoinedEvent(gameRoom.getId(), true);
147+
return roomJoined(gameRoom);
148148
}
149149
}
150-
151150
return createAndJoinGame(client, gameType);
152151
}
153152

153+
private RoomWasJoinedEvent roomJoined(GameRoom room) {
154+
return new RoomWasJoinedEvent(room.getId(), room.getPlayers().size());
155+
}
156+
154157
/** Create an unmodifiable Collection of the {@link GameRoom GameRooms}. */
155158
public synchronized Collection<GameRoom> getGames() {
156159
return Collections.unmodifiableCollection(this.rooms.values());

server/test/sc/server/gaming/GameRoomTest.kt

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,14 @@ class GameRoomTest: WordSpec({
2222
// TODO Replay observing
2323
// Configuration.set(Configuration.SAVE_REPLAY, "true")
2424
"create a game when a player joins" {
25-
manager.joinOrCreateGame(client, TestPlugin.TEST_PLUGIN_UUID).existing shouldBe false
25+
manager.joinOrCreateGame(client, TestPlugin.TEST_PLUGIN_UUID).playerCount shouldBe 1
2626
manager.games shouldHaveSize 1
27-
manager.games.single().game.players shouldHaveSize 1
2827
}
28+
val room = manager.games.single()
2929
"add a second player to the existing game" {
30-
manager.joinOrCreateGame(client, TestPlugin.TEST_PLUGIN_UUID).existing shouldBe true
31-
manager.games.single().game.players shouldHaveSize 2
30+
manager.joinOrCreateGame(client, TestPlugin.TEST_PLUGIN_UUID).playerCount shouldBe 2
3231
}
3332
"return correct scores on game over" {
34-
val room = manager.games.single()
3533
val playersScores = room.game.players.associateWith { PlayerScore(ScoreCause.REGULAR, "Game terminated", 0) }
3634
room.onGameOver(playersScores)
3735
room.result.isRegular shouldBe true
@@ -64,11 +62,9 @@ class GameRoomTest: WordSpec({
6462
}
6563
room.clients shouldHaveSize 1
6664
}
67-
room.game.players shouldHaveSize 0
6865
"accept a second client and create Players" {
6966
ReservationManager.redeemReservationCode(client, reservations[1])
7067
room.clients shouldHaveSize 2
71-
room.game.players shouldHaveSize 2
7268
}
7369
"reject a third client" {
7470
room.join(client) shouldBe false

0 commit comments

Comments
 (0)