Skip to content

Commit 2c0f229

Browse files
committed
fix(sdk): handle responses cleanly in LobbyClient and don't track rooms
1 parent c69feaa commit 2c0f229

File tree

2 files changed

+16
-30
lines changed

2 files changed

+16
-30
lines changed

sdk/src/server-api/sc/networking/clients/LobbyClient.java

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717

1818
import java.io.IOException;
1919
import java.net.Socket;
20-
import java.util.ArrayList;
21-
import java.util.Collections;
22-
import java.util.List;
20+
import java.util.*;
2321

2422
/**
2523
* This class is used to handle all communication with a server.
@@ -31,7 +29,6 @@
3129
*/
3230
public final class LobbyClient extends XStreamClient implements IPollsHistory {
3331
private static final Logger logger = LoggerFactory.getLogger(LobbyClient.class);
34-
private final List<String> rooms = new ArrayList<>();
3532
private final AsyncResultManager asyncManager = new AsyncResultManager();
3633
private final List<ILobbyClientListener> listeners = new ArrayList<>();
3734
private final List<IHistoryListener> historyListeners = new ArrayList<>();
@@ -52,10 +49,6 @@ private static INetworkInterface createTcpNetwork(String host, int port) throws
5249
return new TcpNetwork(new Socket(host, port));
5350
}
5451

55-
public List<String> getRooms() {
56-
return Collections.unmodifiableList(this.rooms);
57-
}
58-
5952
@Override
6053
protected final void onObject(ProtocolMessage o) {
6154
if (o == null) {
@@ -83,26 +76,21 @@ protected final void onObject(ProtocolMessage o) {
8376
onRoomMessage(roomId, data);
8477
}
8578
} else if (o instanceof GamePreparedResponse) {
86-
GamePreparedResponse preparation = (GamePreparedResponse) o;
87-
onGamePrepared(preparation);
79+
onGamePrepared((GamePreparedResponse) o);
8880
} else if (o instanceof JoinedRoomResponse) {
89-
String roomId = ((JoinedRoomResponse) o).getRoomId();
90-
this.rooms.add(roomId);
91-
onGameJoined(roomId);
81+
onGameJoined(((JoinedRoomResponse) o).getRoomId());
82+
} else if (o instanceof RoomWasJoinedEvent) {
83+
onGameJoined(((RoomWasJoinedEvent) o).getRoomId());
9284
} else if (o instanceof LeftGameEvent) {
93-
String roomId = ((LeftGameEvent) o).getRoomId();
94-
this.rooms.remove(roomId);
95-
onGameLeft(roomId);
96-
} else if (o instanceof ProtocolErrorMessage) {
97-
ProtocolErrorMessage response = (ProtocolErrorMessage) o;
98-
99-
onError(response.getMessage(), response);
85+
onGameLeft(((LeftGameEvent) o).getRoomId());
10086
} else if (o instanceof ObservationResponse) {
101-
String roomId = ((ObservationResponse) o).getRoomId();
102-
onGameObserved(roomId);
103-
} else if (o instanceof TestModeResponse) { // for handling testing
87+
onGameObserved(((ObservationResponse) o).getRoomId());
88+
} else if (o instanceof TestModeResponse) {
10489
boolean testMode = (((TestModeResponse) o).getTestMode());
10590
logger.info("TestMode was set to {} ", testMode);
91+
} else if (o instanceof ProtocolErrorMessage) {
92+
ProtocolErrorMessage response = (ProtocolErrorMessage) o;
93+
onError(response.getMessage(), response);
10694
} else {
10795
onCustomObject(o);
10896
}
@@ -259,8 +247,8 @@ protected void request(ProtocolMessage request, Class<? extends ProtocolMessage>
259247
protected RequestResult blockingRequest(ProtocolMessage request,
260248
Class<? extends ProtocolMessage> response) throws InterruptedException {
261249
// TODO return a proper future here
262-
// This is really old async code, so the variable needs to be final but still manipulatable - IDEA suggested to
263-
// use an array and we'll stay with that until we reimplement it properly.
250+
// This is really old async code, so the variable needs to be final but still mutable
251+
// IDEA suggested to use an array and we'll stay with that until we reimplement it properly.
264252
final RequestResult[] requestResult = {null};
265253
final Object beacon = new Object();
266254
synchronized(beacon) {

server/test/sc/server/network/LobbyTest.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,11 @@ class LobbyTest: RealServerTest() {
3737
player1.joinRoomRequest(TestPlugin.TEST_PLUGIN_UUID)
3838
player2.joinRoomRequest(TestPlugin.TEST_PLUGIN_UUID)
3939

40-
TestHelper.assertEqualsWithTimeout(1, { player1.rooms.size })
41-
TestHelper.assertEqualsWithTimeout(1, { player2.rooms.size })
40+
// TODO Listen for RoomJoinedResponse directly instead
41+
//TestHelper.assertEqualsWithTimeout(1, { player1.rooms.size })
42+
//TestHelper.assertEqualsWithTimeout(1, { player2.rooms.size })
4243
TestHelper.assertEqualsWithTimeout(1, { this@LobbyTest.gameMgr.games.size })
4344

44-
Assert.assertEquals(1, this.gameMgr.games.size)
45-
Assert.assertEquals(player1.rooms[0], player2.rooms[0])
46-
4745
val theRoom = this@LobbyTest.gameMgr.games.iterator().next()
4846

4947
Assert.assertEquals(false, theRoom.isOver)

0 commit comments

Comments
 (0)