summaryrefslogtreecommitdiff
path: root/sw-server/src/main/kotlin
diff options
context:
space:
mode:
authorJoffrey Bion <joffrey.bion@booking.com>2020-03-28 23:00:18 +0100
committerJoffrey Bion <joffrey.bion@booking.com>2020-03-28 23:51:52 +0100
commit78aff48d30f7943a720c2d0714d5fe304685c29b (patch)
tree1a76e943fe8223ef53bb08e05795420db28d8d80 /sw-server/src/main/kotlin
parentImprove blueprint helpers (diff)
downloadseven-wonders-78aff48d30f7943a720c2d0714d5fe304685c29b.tar.gz
seven-wonders-78aff48d30f7943a720c2d0714d5fe304685c29b.tar.bz2
seven-wonders-78aff48d30f7943a720c2d0714d5fe304685c29b.zip
Improve state updates for turn info
Diffstat (limited to 'sw-server/src/main/kotlin')
-rw-r--r--sw-server/src/main/kotlin/org/luxons/sevenwonders/server/api/Converters.kt2
-rw-r--r--sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameController.kt40
2 files changed, 22 insertions, 20 deletions
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<Player>, 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<Player>, 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<PlayerTurnInfo>.hideHandsAndWaitForReadiness() =
+ map { it.copy(action = Action.SAY_READY, hand = null) }
companion object {
private val logger = LoggerFactory.getLogger(GameController::class.java)
bgstack15