From 78aff48d30f7943a720c2d0714d5fe304685c29b Mon Sep 17 00:00:00 2001 From: Joffrey Bion Date: Sat, 28 Mar 2020 23:00:18 +0100 Subject: Improve state updates for turn info --- .../luxons/sevenwonders/server/api/Converters.kt | 2 +- .../server/controllers/GameController.kt | 40 ++++++++++++---------- 2 files changed, 22 insertions(+), 20 deletions(-) (limited to 'sw-server') diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/api/Converters.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/api/Converters.kt index 5fb73b23..a2390a7d 100644 --- a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/api/Converters.kt +++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/api/Converters.kt @@ -13,4 +13,4 @@ fun Lobby.toDTO(currentPlayer: Player): LobbyDTO { } fun Player.toDTO(currentUser: String) = - PlayerDTO(username, displayName, index, isGameOwner, username === currentUser) + PlayerDTO(username, displayName, index, isGameOwner, username === currentUser, isReady) 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 e1fae39b..0db7a21c 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 @@ -2,7 +2,8 @@ package org.luxons.sevenwonders.server.controllers import org.hildan.livedoc.core.annotations.Api import org.luxons.sevenwonders.engine.Game -import org.luxons.sevenwonders.model.GameState +import org.luxons.sevenwonders.model.Action +import org.luxons.sevenwonders.model.PlayerTurnInfo import org.luxons.sevenwonders.model.api.actions.PrepareMoveAction import org.luxons.sevenwonders.model.cards.PreparedCard import org.luxons.sevenwonders.server.api.toDTO @@ -40,8 +41,7 @@ class GameController @Autowired constructor( val game = player.game logger.info("Game {}: player {} is ready for the next turn", game.id, player) - val lobby = player.lobby - val players = lobby.getPlayers() + val players = player.lobby.getPlayers() sendPlayerReady(game.id, player) @@ -49,20 +49,10 @@ class GameController @Autowired constructor( if (allReady) { logger.info("Game {}: all players ready, sending turn info", game.id) players.forEach { it.isReady = false } - sendTurnInfo(players, game) + sendTurnInfo(players, game, false) } } - private fun sendTurnInfo(players: List, game: Game) { - for (turnInfo in game.getCurrentTurnInfo()) { - val player = players[turnInfo.playerIndex] - template.convertAndSendToUser(player.username, "/queue/game/turn", turnInfo) - } - } - - private fun sendPlayerReady(gameId: Long, player: Player) = - template.convertAndSend("/topic/game/$gameId/playerReady", "\"${player.username}\"") - /** * Prepares the player's next move. When all players have prepared their moves, all moves are executed. * @@ -82,16 +72,28 @@ class GameController @Autowired constructor( if (game.allPlayersPreparedTheirMove()) { logger.info("Game {}: all players have prepared their move, executing turn...", game.id) - val table = game.playTurn() - sendPlayedMoves(game.id, table) + game.playTurn() + sendTurnInfo(player.lobby.getPlayers(), game, true) } } - private fun sendPlayedMoves(gameId: Long, gameState: GameState) = - template.convertAndSend("/topic/game/$gameId/tableUpdates", gameState) + private fun sendPlayerReady(gameId: Long, player: Player) = + template.convertAndSend("/topic/game/$gameId/playerReady", "\"${player.username}\"") private fun sendPreparedCard(gameId: Long, preparedCard: PreparedCard) = - template.convertAndSend("/topic/game/$gameId/prepared", preparedCard) + template.convertAndSend("/topic/game/$gameId/prepared", preparedCard) + + private fun sendTurnInfo(players: List, game: Game, hideHands: Boolean) { + val turns = game.getCurrentTurnInfo() + val turnsToSend = if (hideHands) turns.hideHandsAndWaitForReadiness() else turns + for (turnInfo in turnsToSend) { + val player = players[turnInfo.playerIndex] + template.convertAndSendToUser(player.username, "/queue/game/turn", turnInfo) + } + } + + private fun Collection.hideHandsAndWaitForReadiness() = + map { it.copy(action = Action.SAY_READY, hand = null) } companion object { private val logger = LoggerFactory.getLogger(GameController::class.java) -- cgit