Skip to content

Commit 34c746f

Browse files
committed
fix(server): send GamePaused message on pause
1 parent fe17ca4 commit 34c746f

File tree

10 files changed

+14
-52
lines changed

10 files changed

+14
-52
lines changed

plugin/src/test/sc/plugin2021/GameTest.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ class GameTest: WordSpec({
4545
// hashing it to avoid cloning, since we get the original mutable object
4646
finalState = data.hashCode()
4747
}
48-
49-
override fun onPaused(nextPlayer: Player) {
50-
}
5148
})
5249

5350
"finish without issues".config(invocationTimeout = Constants.GAME_TIMEOUT.milliseconds) {

sdk/src/server-api/sc/api/plugins/host/IGameListener.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,4 @@ import sc.shared.PlayerScore
77
interface IGameListener {
88
fun onGameOver(results: Map<Player, PlayerScore>)
99
fun onStateChanged(data: IGameState, observersOnly: Boolean)
10-
fun onPaused(nextPlayer: Player)
1110
}

sdk/src/server-api/sc/networking/clients/AbstractLobbyClientListener.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package sc.networking.clients
22

33
import sc.api.plugins.IGameState
4-
import sc.framework.plugins.Player
54
import sc.protocol.room.RoomMessage
65
import sc.protocol.responses.GamePreparedResponse
76
import sc.protocol.responses.ErrorPacket
@@ -10,7 +9,6 @@ import sc.shared.GameResult
109
abstract class AbstractLobbyClientListener: ILobbyClientListener {
1110
override fun onNewState(roomId: String, state: IGameState) {}
1211
override fun onGameOver(roomId: String, data: GameResult) {}
13-
override fun onGamePaused(roomId: String, nextPlayer: Player) {}
1412
override fun onRoomMessage(roomId: String, data: RoomMessage) {}
1513

1614
override fun onError(error: ErrorPacket) {}

sdk/src/server-api/sc/networking/clients/ILobbyClientListener.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package sc.networking.clients
22

33
import sc.api.plugins.IGameState
4-
import sc.framework.plugins.Player
54
import sc.protocol.room.RoomMessage
65
import sc.protocol.responses.GamePreparedResponse
76
import sc.protocol.responses.ErrorPacket
@@ -12,7 +11,6 @@ import sc.shared.GameResult
1211
interface ILobbyClientListener {
1312
fun onNewState(roomId: String, state: IGameState)
1413
fun onGameOver(roomId: String, data: GameResult)
15-
fun onGamePaused(roomId: String, nextPlayer: Player)
1614
fun onRoomMessage(roomId: String, data: RoomMessage)
1715
fun onError(error: ErrorPacket)
1816

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ protected final void onObject(ProtocolPacket message) {
5353
onNewState(roomId, ((MementoMessage) data).getState());
5454
} else if (data instanceof GameResult) {
5555
onGameOver(roomId, (GameResult) data);
56-
} else if (data instanceof GamePaused) {
57-
onGamePaused(roomId, ((GamePaused) data).getNextPlayer());
5856
} else if (data instanceof ErrorMessage) {
5957
ErrorMessage error = (ErrorMessage) data;
6058
logger.warn("{} in room {}", error.getLogMessage(), roomId);
@@ -88,12 +86,6 @@ protected final void onObject(ProtocolPacket message) {
8886
}
8987
}
9088

91-
private void onGamePaused(String roomId, Player nextPlayer) {
92-
for (ILobbyClientListener listener : this.listeners) {
93-
listener.onGamePaused(roomId, nextPlayer);
94-
}
95-
}
96-
9789
private void onGameOver(String roomId, GameResult data) {
9890
logger.info("Received game result: {}", data);
9991
for (IHistoryListener listener : this.historyListeners) {

sdk/src/server-api/sc/protocol/room/GamePaused.kt

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,9 @@ package sc.protocol.room
33
import com.thoughtworks.xstream.annotations.XStreamAlias
44
import com.thoughtworks.xstream.annotations.XStreamAsAttribute
55

6-
import sc.framework.plugins.Player
7-
8-
/**
9-
* Indicates to observers that the game has been paused.
10-
*
11-
* @param nextPlayer the next Player to move after unpausing.
12-
*/
6+
/** Indicates to observers that the game has been (un)paused. */
137
@XStreamAlias(value = "paused")
148
data class GamePaused(
159
@XStreamAsAttribute
16-
val nextPlayer: Player
10+
val paused: Boolean
1711
): ObservableRoomMessage

server/src/sc/server/Lobby.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import org.slf4j.LoggerFactory
44
import sc.api.plugins.IMove
55
import sc.api.plugins.exceptions.GameRoomException
66
import sc.api.plugins.exceptions.RescuableClientException
7+
import sc.protocol.ProtocolPacket
78
import sc.protocol.requests.*
89
import sc.protocol.responses.PlayerScoreResponse
910
import sc.protocol.responses.TestModeResponse
@@ -27,6 +28,11 @@ class Lobby: GameRoomManager(), Closeable, IClientRequestListener {
2728
clientManager.start()
2829
}
2930

31+
private fun notifyObservers(packet: ProtocolPacket) =
32+
clientManager.clients
33+
.filter { it.isAdministrator }
34+
.forEach { it.send(packet) }
35+
3036
/** Handle requests or moves of clients.
3137
* @throws RescuableClientException if something goes wrong.
3238
* Usually results in termination of the connection to the offending client. */
@@ -47,13 +53,8 @@ class Lobby: GameRoomManager(), Closeable, IClientRequestListener {
4753
if(!this.findRoom(packet.roomId).join(source))
4854
throw GameRoomException("Room ${packet.roomId} is already full!")
4955
is JoinGameRequest -> {
50-
val gameRoomMessage = this.joinOrCreateGame(source, packet.gameType)
51-
// null is returned if join was unsuccessful
52-
if (gameRoomMessage != null) {
53-
clientManager.clients
54-
.filter { it.isAdministrator }
55-
.forEach { it.send(gameRoomMessage) }
56-
}
56+
joinOrCreateGame(source, packet.gameType)
57+
?.let { notifyObservers(it) }
5758
}
5859
is AuthenticateRequest -> source.authenticate(packet.password)
5960
is AdminLobbyRequest -> {

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

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,8 @@ public void addObserver(Client source) {
359359

360360
/**
361361
* Pause or un-pause a game.
362-
*
363362
* @param pause true if game is to be paused
363+
* @return a RoomPacket with a GamePaused message or null if unsuccessful
364364
*/
365365
public synchronized void pause(boolean pause) {
366366
if (isOver()) {
@@ -379,10 +379,11 @@ public synchronized void pause(boolean pause) {
379379
// pause game after current turn has finished
380380
pausableGame.setPaused(pause);
381381

382-
// continue execution
383382
if (!pause && status == GameStatus.ACTIVE) {
383+
// continue execution
384384
pausableGame.afterPause();
385385
}
386+
observerBroadcast(new GamePaused(pause));
386387
}
387388

388389
/**
@@ -421,16 +422,6 @@ private void destroy() {
421422
this.gameRoomManager.remove(this);
422423
}
423424

424-
/**
425-
* Broadcast to all observers that the game is paused.
426-
*
427-
* @param nextPlayer Player who comes next after the pause
428-
*/
429-
@Override
430-
public void onPaused(Player nextPlayer) {
431-
observerBroadcast(new GamePaused(nextPlayer));
432-
}
433-
434425
/** Return true if GameStatus is OVER. */
435426
public boolean isOver() {
436427
return getStatus() == GameStatus.OVER;

server/test/sc/server/client/TestLobbyClientListener.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,6 @@ public void onGameOver(String roomId, GameResult data) {
9292
gameOverReceived = true;
9393
}
9494

95-
@Override
96-
public void onGamePaused(String roomId, Player nextPlayer) {
97-
this.roomId = roomId;
98-
this.player = nextPlayer;
99-
gamePausedReceived = true;
100-
}
101-
10295
@Override
10396
public void onGameObserved(String roomId) {
10497
this.roomId = roomId;

server/test/sc/server/network/LobbyRequestTest.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,7 @@ class LobbyRequestTest: WordSpec({
162162

163163
val controller = admin.control(prepared.roomId)
164164
controller.pause()
165-
// TODO pausing is currently not acknowledged
166-
// roomListener.waitForMessage(GamePaused::class)
165+
roomListener.waitForMessage(GamePaused::class)
167166
withClue("appropriate result for aborted game") {
168167
players[1].sendMessageToRoom(prepared.roomId, TestMove(0))
169168
val result = roomListener.waitForMessage(GameResult::class)

0 commit comments

Comments
 (0)