diff options
Diffstat (limited to 'sw-server/src/main/kotlin/org/luxons')
-rw-r--r-- | sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameController.kt | 46 |
1 files changed, 26 insertions, 20 deletions
diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameController.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameController.kt index 8b9bbd19..40a9407d 100644 --- a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameController.kt +++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameController.kt @@ -37,15 +37,17 @@ class GameController( val game = player.game logger.info("Game {}: player {} is ready for the next turn", game.id, player) - val players = player.lobby.getPlayers() + synchronized(player.lobby) { + val players = player.lobby.getPlayers() - sendPlayerReady(game.id, player) + sendPlayerReady(game.id, player) - val allReady = players.all { it.isReady } - if (allReady) { - logger.info("Game {}: all players ready, sending turn info", game.id) - players.forEach { it.isReady = false } - sendTurnInfo(players, game, false) + val allReady = players.all { it.isReady } + if (allReady) { + logger.info("Game {}: all players ready, sending turn info", game.id) + players.forEach { it.isReady = false } + sendTurnInfo(players, game, false) + } } } @@ -59,20 +61,22 @@ class GameController( fun prepareMove(action: PrepareMoveAction, principal: Principal) { val player = principal.player val game = player.game - val preparedCardBack = game.prepareMove(player.index, action.move) - val preparedCard = PreparedCard(player.username, preparedCardBack) - logger.info("Game {}: player {} prepared move {}", game.id, principal.name, action.move) - sendPreparedCard(game.id, preparedCard) + synchronized(game) { + val preparedCardBack = game.prepareMove(player.index, action.move) + val preparedCard = PreparedCard(player.username, preparedCardBack) + logger.info("Game {}: player {} prepared move {}", game.id, principal.name, action.move) + sendPreparedCard(game.id, preparedCard) - if (game.allPlayersPreparedTheirMove()) { - logger.info("Game {}: all players have prepared their move, executing turn...", game.id) - game.playTurn() - sendTurnInfo(player.lobby.getPlayers(), game, true) - if (game.endOfGameReached()) { - player.lobby.setEndOfGame() + if (game.allPlayersPreparedTheirMove()) { + logger.info("Game {}: all players have prepared their move, executing turn...", game.id) + game.playTurn() + sendTurnInfo(player.lobby.getPlayers(), game, true) + if (game.endOfGameReached()) { + player.lobby.setEndOfGame() + } + } else { + template.convertAndSendToUser(player.username, "/queue/game/preparedMove", action.move) } - } else { - template.convertAndSendToUser(player.username, "/queue/game/preparedMove", action.move) } } @@ -80,7 +84,9 @@ class GameController( fun unprepareMove(principal: Principal) { val player = principal.player val game = player.game - game.unprepareMove(player.index) + synchronized(game) { + game.unprepareMove(player.index) + } val preparedCard = PreparedCard(player.username, null) logger.info("Game {}: player {} unprepared his move", game.id, principal.name) sendPreparedCard(game.id, preparedCard) |