diff options
author | Joffrey Bion <joffrey.bion@booking.com> | 2020-03-29 18:02:47 +0200 |
---|---|---|
committer | Joffrey Bion <joffrey.bion@booking.com> | 2020-03-29 18:07:31 +0200 |
commit | 57a6e5135ee554079c6064bd770d51545800969d (patch) | |
tree | 9c688d1fc261cf6740c63a604695f9301220830f /sw-server/src/main | |
parent | Ensure error saga starts first (diff) | |
download | seven-wonders-57a6e5135ee554079c6064bd770d51545800969d.tar.gz seven-wonders-57a6e5135ee554079c6064bd770d51545800969d.tar.bz2 seven-wonders-57a6e5135ee554079c6064bd770d51545800969d.zip |
Fix duplicated inconsistent state
Diffstat (limited to 'sw-server/src/main')
6 files changed, 29 insertions, 49 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 a2390a7d..7c3ccfbb 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 @@ -5,12 +5,15 @@ import org.luxons.sevenwonders.model.api.PlayerDTO import org.luxons.sevenwonders.server.lobby.Lobby import org.luxons.sevenwonders.server.lobby.Player -fun Lobby.toDTO(currentPlayer: Player): LobbyDTO { - val players = getPlayers().map { it.toDTO(currentPlayer.username) } - val joinability = joinability(currentPlayer.displayName) - val startability = startability(currentPlayer.username) - return LobbyDTO(id, name, owner.username, players, state, joinability, startability) -} +fun Lobby.toDTO(): LobbyDTO = LobbyDTO( + id = id, + name = name, + owner = owner.username, + players = getPlayers().map { it.toDTO() }, + state = state, + hasEnoughPlayers = hasEnoughPlayers(), + maxPlayersReached = maxPlayersReached() +) + +fun Player.toDTO() = PlayerDTO(username, displayName, index, isGameOwner, isReady) -fun Player.toDTO(currentUser: String) = - PlayerDTO(username, displayName, index, isGameOwner, username === currentUser, isReady) diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameBrowserController.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameBrowserController.kt index 0bd1de79..3de0de97 100644 --- a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameBrowserController.kt +++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameBrowserController.kt @@ -42,7 +42,7 @@ class GameBrowserController @Autowired constructor( fun listGames(principal: Principal): Collection<LobbyDTO> { logger.info("Player '{}' subscribed to /topic/games", principal.name) val player = playerRepository.find(principal.name) - return lobbyRepository.list().map { it.toDTO(player) } + return lobbyRepository.list().map { it.toDTO() } } /** @@ -64,7 +64,7 @@ class GameBrowserController @Autowired constructor( logger.info("Game '{}' ({}) created by {} ({})", lobby.name, lobby.id, player.displayName, player.username) // notify everyone that a new game exists - val lobbyDto = lobby.toDTO(player) + val lobbyDto = lobby.toDTO() template.convertAndSend("/topic/games", listOf(lobbyDto)) return lobbyDto } @@ -87,8 +87,8 @@ class GameBrowserController @Autowired constructor( lobby.addPlayer(player) logger.info("Player '{}' ({}) joined game {}", player.displayName, player.username, lobby.name) - val lobbyDTO = lobby.toDTO(player) - lobbyController.sendLobbyUpdateToPlayers(lobby, player) + val lobbyDTO = lobby.toDTO() + lobbyController.sendLobbyUpdateToPlayers(lobby) return lobbyDTO } 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 0db7a21c..803cca78 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 @@ -66,7 +66,7 @@ class GameController @Autowired constructor( val player = principal.player val game = player.game val preparedCardBack = game.prepareMove(player.index, action.move) - val preparedCard = PreparedCard(player.toDTO(principal.name), preparedCardBack) + val preparedCard = PreparedCard(player.toDTO(), preparedCardBack) logger.info("Game {}: player {} prepared move {}", game.id, principal.name, action.move) sendPreparedCard(game.id, preparedCard) diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/HomeController.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/HomeController.kt index a0d53f8b..c2619f8c 100644 --- a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/HomeController.kt +++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/HomeController.kt @@ -1,9 +1,9 @@ package org.luxons.sevenwonders.server.controllers import org.hildan.livedoc.core.annotations.Api +import org.luxons.sevenwonders.model.api.ConnectedPlayer import org.luxons.sevenwonders.model.api.PlayerDTO import org.luxons.sevenwonders.model.api.actions.ChooseNameAction -import org.luxons.sevenwonders.server.api.toDTO import org.luxons.sevenwonders.server.repositories.PlayerRepository import org.slf4j.LoggerFactory import org.springframework.beans.factory.annotation.Autowired @@ -32,12 +32,12 @@ class HomeController @Autowired constructor( */ @MessageMapping("/chooseName") @SendToUser("/queue/nameChoice") - fun chooseName(@Validated action: ChooseNameAction, principal: Principal): PlayerDTO { + fun chooseName(@Validated action: ChooseNameAction, principal: Principal): ConnectedPlayer { val username = principal.name val player = playerRepository.createOrUpdate(username, action.playerName) logger.info("Player '{}' chose the name '{}'", username, player.displayName) - return player.toDTO(username) + return ConnectedPlayer(username, player.displayName) } companion object { diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/LobbyController.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/LobbyController.kt index b74913cb..04fb2228 100644 --- a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/LobbyController.kt +++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/LobbyController.kt @@ -45,7 +45,7 @@ class LobbyController @Autowired constructor( } logger.info("Player {} left game '{}'", player, lobby.name) - sendLobbyUpdateToPlayers(lobby, player) + sendLobbyUpdateToPlayers(lobby) } /** @@ -62,7 +62,7 @@ class LobbyController @Autowired constructor( lobby.reorderPlayers(action.orderedPlayers) logger.info("Players in game '{}' reordered to {}", lobby.name, action.orderedPlayers) - sendLobbyUpdateToPlayers(lobby, principal.player) + sendLobbyUpdateToPlayers(lobby) } /** @@ -75,19 +75,18 @@ class LobbyController @Autowired constructor( */ @MessageMapping("/lobby/updateSettings") fun updateSettings(@Validated action: UpdateSettingsAction, principal: Principal) { - val player = principal.player - val lobby = player.ownedLobby + val lobby = principal.player.ownedLobby lobby.settings = action.settings logger.info("Updated settings of game '{}'", lobby.name) - sendLobbyUpdateToPlayers(lobby, player) + sendLobbyUpdateToPlayers(lobby) } - internal fun sendLobbyUpdateToPlayers(lobby: Lobby, player: Player) { + internal fun sendLobbyUpdateToPlayers(lobby: Lobby) { lobby.getPlayers().forEach { - template.convertAndSendToUser(it.username, "/queue/lobby/updated", lobby.toDTO(it)) + template.convertAndSendToUser(it.username, "/queue/lobby/updated", lobby.toDTO()) } - template.convertAndSend("/topic/games", listOf(lobby.toDTO(player))) + template.convertAndSend("/topic/games", listOf(lobby.toDTO())) } /** diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/lobby/Lobby.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/lobby/Lobby.kt index 5368bedf..eadc1742 100644 --- a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/lobby/Lobby.kt +++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/lobby/Lobby.kt @@ -3,7 +3,6 @@ package org.luxons.sevenwonders.server.lobby import org.luxons.sevenwonders.engine.Game import org.luxons.sevenwonders.engine.data.GameDefinition import org.luxons.sevenwonders.model.CustomizableSettings -import org.luxons.sevenwonders.model.api.Actionability import org.luxons.sevenwonders.model.api.State class Lobby( @@ -14,8 +13,7 @@ class Lobby( ) { private val players: MutableList<Player> = ArrayList(gameDefinition.maxPlayers) - var settings: CustomizableSettings = - CustomizableSettings() + var settings: CustomizableSettings = CustomizableSettings() var state = State.LOBBY private set @@ -41,22 +39,9 @@ class Lobby( players.add(player) } - fun joinability(userDisplayName: String): Actionability = when { - hasStarted() -> Actionability(false, "Cannot join: the game has already started") - maxPlayersReached() -> Actionability( - false, - "Cannot join: the game is full (${gameDefinition.maxPlayers} players max)" - ) - playerNameAlreadyUsed(userDisplayName) -> Actionability( - false, - "Cannot join: already a player named '$userDisplayName' in this game" - ) - else -> Actionability(true, "Join game") - } - private fun hasStarted(): Boolean = state != State.LOBBY - private fun maxPlayersReached(): Boolean = players.size >= gameDefinition.maxPlayers + fun maxPlayersReached(): Boolean = players.size >= gameDefinition.maxPlayers private fun playerNameAlreadyUsed(name: String): Boolean = players.any { it.displayName == name } @@ -71,14 +56,7 @@ class Lobby( return game } - fun startability(username: String): Actionability = when { - !hasEnoughPlayers() -> Actionability(false, "Cannot start: min ${gameDefinition.minPlayers} players required" - ) - owner.username != username -> Actionability(false, "Cannot start the game: only the owner can") - else -> Actionability(true, "Start game") - } - - private fun hasEnoughPlayers(): Boolean = players.size >= gameDefinition.minPlayers + fun hasEnoughPlayers(): Boolean = players.size >= gameDefinition.minPlayers @Synchronized fun reorderPlayers(orderedUsernames: List<String>) { |