Skip to content

Commit 340a73f

Browse files
committed
fix: only pass IMove as player action
1 parent b5a49c9 commit 340a73f

File tree

7 files changed

+34
-39
lines changed

7 files changed

+34
-39
lines changed

plugin/src/server/sc/plugin2021/Game.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sc.plugin2021
22

33
import org.slf4j.LoggerFactory
4+
import sc.api.plugins.IMove
45
import sc.api.plugins.exceptions.TooManyPlayersException
56
import sc.framework.plugins.AbstractGame
67
import sc.framework.plugins.ActionTimeout
@@ -9,7 +10,6 @@ import sc.plugin2021.util.Constants
910
import sc.plugin2021.util.GameRuleLogic
1011
import sc.plugin2021.util.MoveMistake
1112
import sc.plugin2021.util.WinReason
12-
import sc.protocol.room.RoomMessage
1313
import sc.shared.InvalidMoveException
1414
import sc.shared.PlayerScore
1515
import sc.shared.ScoreCause
@@ -126,13 +126,13 @@ class Game(override val currentState: GameState = GameState()): AbstractGame<Pla
126126
ActionTimeout(true, Constants.HARD_TIMEOUT, Constants.SOFT_TIMEOUT)
127127

128128
@Throws(InvalidMoveException::class)
129-
override fun onRoundBasedAction(fromPlayer: Player, data: RoomMessage) {
130-
if (data !is Move)
129+
override fun onRoundBasedAction(fromPlayer: Player, move: IMove) {
130+
if (move !is Move)
131131
throw InvalidMoveException(MoveMistake.INVALID_FORMAT)
132132

133133
logger.debug("Current State: $currentState")
134-
logger.debug("Performing Move $data")
135-
GameRuleLogic.performMove(currentState, data)
134+
logger.debug("Performing Move $move")
135+
GameRuleLogic.performMove(currentState, move)
136136
GameRuleLogic.removeInvalidColors(currentState)
137137
next(if (isGameOver) null else currentState.currentPlayer)
138138
logger.debug("Current Board:\n${currentState.board}")

sdk/src/server-api/sc/api/plugins/IGameInstance.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package sc.api.plugins
33
import sc.api.plugins.exceptions.GameLogicException
44
import sc.api.plugins.host.IGameListener
55
import sc.framework.plugins.Player
6-
import sc.protocol.room.RoomMessage
76
import sc.shared.InvalidMoveException
87
import sc.shared.PlayerScore
98
import sc.shared.ScoreCause
@@ -15,16 +14,15 @@ interface IGameInstance {
1514
fun onPlayerLeft(player: Player, cause: ScoreCause? = null)
1615

1716
/**
18-
* Called by the Server once an action was received.
17+
* Called by the Server upon receiving a Move.
1918
*
2019
* @param fromPlayer The player who invoked this action.
21-
* @param data ProtocolMessage with the action
2220
*
2321
* @throws GameLogicException if any invalid action is done
2422
* @throws InvalidMoveException if the received move violates the rules
2523
*/
2624
@Throws(GameLogicException::class, InvalidMoveException::class)
27-
fun onAction(fromPlayer: Player, data: RoomMessage)
25+
fun onAction(fromPlayer: Player, move: IMove)
2826
fun addGameListener(listener: IGameListener)
2927
fun removeGameListener(listener: IGameListener)
3028

sdk/src/server-api/sc/framework/plugins/AbstractGame.kt

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package sc.framework.plugins
33
import org.slf4j.LoggerFactory
44
import sc.api.plugins.IGameInstance
55
import sc.api.plugins.IGameState
6+
import sc.api.plugins.IMove
67
import sc.api.plugins.exceptions.GameLogicException
78
import sc.api.plugins.exceptions.NotYourTurnException
89
import sc.api.plugins.host.IGameListener
9-
import sc.protocol.room.RoomMessage
1010
import sc.shared.*
1111

1212
abstract class AbstractGame<P : Player>(override val pluginUUID: String) : IGameInstance {
@@ -31,18 +31,15 @@ abstract class AbstractGame<P : Player>(override val pluginUUID: String) : IGame
3131
}
3232

3333
/**
34-
* Called by the Server once an action was received.
35-
*
36-
* @param fromPlayer The player who invoked this action.
37-
* @param data The plugin-specific data.
34+
* Called by the Server upon receiving a Move.
3835
*
3936
* @throws GameLogicException if no move has been requested from the given [Player]
4037
* @throws InvalidMoveException when the given Move is not possible
4138
*/
4239
@Throws(GameLogicException::class, InvalidMoveException::class)
43-
override fun onAction(fromPlayer: Player, data: RoomMessage) {
40+
override fun onAction(fromPlayer: Player, move: IMove) {
4441
if (fromPlayer != activePlayer)
45-
throw NotYourTurnException(activePlayer, fromPlayer, data)
42+
throw NotYourTurnException(activePlayer, fromPlayer, move)
4643
moveRequestTimeout?.let { timer ->
4744
timer.stop()
4845
logger.info("Time needed for move: " + timer.timeDiff)
@@ -51,14 +48,14 @@ abstract class AbstractGame<P : Player>(override val pluginUUID: String) : IGame
5148
fromPlayer.softTimeout = true
5249
onPlayerLeft(fromPlayer, ScoreCause.SOFT_TIMEOUT)
5350
} else {
54-
onRoundBasedAction(fromPlayer, data)
51+
onRoundBasedAction(fromPlayer, move)
5552
}
56-
} ?: throw GameLogicException("We didn't request a move from you yet.")
53+
} ?: throw GameLogicException("Move from $fromPlayer has not been requested.")
5754
}
5855

5956
/** Called by [onAction] to execute a move of a Player. */
6057
@Throws(InvalidMoveException::class)
61-
abstract fun onRoundBasedAction(fromPlayer: Player, data: RoomMessage)
58+
abstract fun onRoundBasedAction(fromPlayer: Player, move: IMove)
6259

6360
/**
6461
* Returns a WinCondition if the Game is over.

server/src/sc/server/Lobby.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sc.server
22

33
import org.slf4j.LoggerFactory
4+
import sc.api.plugins.IMove
45
import sc.api.plugins.exceptions.GameRoomException
56
import sc.api.plugins.exceptions.RescuableClientException
67
import sc.protocol.requests.*
@@ -35,7 +36,10 @@ class Lobby: GameRoomManager(), Closeable, IClientRequestListener {
3536
is RoomPacket -> {
3637
// i.e. new move
3738
val room = this.findRoom(packet.roomId)
38-
room.onEvent(source, packet.data)
39+
val move = packet.data
40+
if(move !is IMove)
41+
throw GameRoomException("Received non-move packet: $packet")
42+
room.onEvent(source, move)
3943
}
4044
is JoinPreparedRoomRequest ->
4145
ReservationManager.redeemReservationCode(source, packet.reservationCode)

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.slf4j.LoggerFactory;
66
import sc.api.plugins.IGameInstance;
77
import sc.api.plugins.IGameState;
8+
import sc.api.plugins.IMove;
89
import sc.api.plugins.exceptions.GameException;
910
import sc.api.plugins.exceptions.GameLogicException;
1011
import sc.api.plugins.exceptions.GameRoomException;
@@ -310,27 +311,27 @@ public synchronized List<String> reserveSlots(SlotDescriptor[] descriptors) {
310311
* Execute received action.
311312
*
312313
* @param source Client which caused the event
313-
* @param data ProtocolMessage containing the action
314+
* @param move ProtocolMessage containing the action
314315
*/
315-
public synchronized void onEvent(Client source, RoomMessage data) throws GameRoomException {
316+
public synchronized void onEvent(Client source, IMove move) throws GameRoomException {
316317
if (isOver())
317-
throw new GameException("Game is already over, but got " + data);
318+
throw new GameException("Game is already over, but got " + move);
318319

319320
Player player = resolvePlayer(source);
320321
try {
321-
game.onAction(player, data);
322+
game.onAction(player, move);
322323
} catch (InvalidMoveException e) {
323324
final String error = String.format("Ungueltiger Zug von '%s'.\n%s", player.getDisplayName(), e);
324325
logger.error(error);
325326
player.setViolationReason(e.getMessage());
326-
ErrorMessage errorMessage = new ErrorMessage(data, error);
327+
ErrorMessage errorMessage = new ErrorMessage(move, error);
327328
player.notifyListeners(errorMessage);
328329
observerBroadcast(errorMessage);
329330
history.add(errorMessage);
330331
game.onPlayerLeft(player, ScoreCause.RULE_VIOLATION);
331332
throw new GameLogicException(e.toString(), e);
332333
} catch (GameLogicException e) {
333-
player.notifyListeners(new ErrorMessage(data, e.getMessage()));
334+
player.notifyListeners(new ErrorMessage(move, e.getMessage()));
334335
throw e;
335336
}
336337
}

server/test/sc/server/plugins/TestGame.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package sc.server.plugins
22

3+
import sc.api.plugins.IMove
34
import sc.api.plugins.exceptions.TooManyPlayersException
45
import sc.framework.plugins.AbstractGame
56
import sc.framework.plugins.ActionTimeout
67
import sc.framework.plugins.Player
7-
import sc.protocol.room.RoomMessage
88
import sc.server.helpers.TestTeam
99
import sc.shared.*
1010

@@ -15,11 +15,13 @@ data class TestGame(
1515
override val playerScores: List<PlayerScore> = emptyList()
1616
override val winners: List<Player> = emptyList()
1717

18-
override fun onRoundBasedAction(fromPlayer: Player, data: RoomMessage) {
19-
if (data is TestMove) {
20-
data.perform(currentState)
21-
next(if (currentState.currentPlayer === TestTeam.RED) currentState.red else currentState.blue)
22-
} else throw InvalidMoveException(TestMoveMistake.INVALID_FORMAT)
18+
override fun onRoundBasedAction(fromPlayer: Player, move: IMove) {
19+
if (move !is TestMove)
20+
throw InvalidMoveException(object: IMoveMistake {
21+
override val message = "TestGame only processes TestMove"
22+
})
23+
move.perform(currentState)
24+
next(if (currentState.currentPlayer === TestTeam.RED) currentState.red else currentState.blue)
2325
}
2426

2527
override fun checkWinCondition(): WinCondition? {

server/test/sc/server/plugins/TestMoveMistake.kt

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)