From 2d5117911c11fafc3c287e2ee74149f866c41954 Mon Sep 17 00:00:00 2001 From: joffrey-bion Date: Sun, 13 Dec 2020 01:39:06 +0100 Subject: Only transfer ownership to humans Resolve: https://github.com/joffrey-bion/seven-wonders/issues/73 --- .../kotlin/org/luxons/sevenwonders/server/api/Converters.kt | 10 +++++++++- .../luxons/sevenwonders/server/controllers/HomeController.kt | 4 ++-- .../luxons/sevenwonders/server/controllers/LobbyController.kt | 9 +++++---- .../main/kotlin/org/luxons/sevenwonders/server/lobby/Lobby.kt | 4 ++-- .../main/kotlin/org/luxons/sevenwonders/server/lobby/Player.kt | 1 + .../sevenwonders/server/repositories/PlayerRepository.kt | 4 ++-- .../sevenwonders/server/controllers/HomeControllerTest.kt | 2 +- 7 files changed, 22 insertions(+), 12 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 5689247b..b653eec5 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 @@ -18,4 +18,12 @@ fun Lobby.toDTO(): LobbyDTO = LobbyDTO( maxPlayersReached = maxPlayersReached(), ) -private fun Player.toDTO(wonder: AssignedWonder) = PlayerDTO(username, displayName, icon, wonder, isGameOwner, isReady) +private fun Player.toDTO(wonder: AssignedWonder) = PlayerDTO( + username = username, + displayName = displayName, + isHuman = isHuman, + icon = icon, + wonder = wonder, + isGameOwner = isGameOwner, + isReady = isReady, +) 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 5cfedb17..b2fc122a 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 @@ -31,10 +31,10 @@ class HomeController( @SendToUser("/queue/nameChoice") fun chooseName(@Validated action: ChooseNameAction, principal: Principal): ConnectedPlayer { val username = principal.name - val player = playerRepository.createOrUpdate(username, action.playerName, action.icon) + val player = playerRepository.createOrUpdate(username, action.playerName, action.isHuman, action.icon) logger.info("Player '{}' chose the name '{}'", username, player.displayName) - return ConnectedPlayer(username, player.displayName, player.icon) + return ConnectedPlayer(username, player.displayName, player.isHuman, player.icon) } 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 4f5f45bf..aa01a23a 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,12 +45,13 @@ class LobbyController( fun leave(principal: Principal) { val player = principal.player val lobby = player.lobby - lobby.removePlayer(principal.name) - logger.info("Player {} left the lobby of game '{}'", player, lobby.name) - template.convertAndSendToUser(player.username, "/queue/lobby/left", lobby.id) synchronized(lobby) { - if (lobby.getPlayers().isEmpty()) { + lobby.removePlayer(principal.name) + logger.info("Player {} left the lobby of game '{}'", player, lobby.name) + template.convertAndSendToUser(player.username, "/queue/lobby/left", lobby.id) + + if (lobby.getPlayers().none { it.isHuman }) { deleteLobby(lobby) } else { sendLobbyUpdateToPlayers(lobby) 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 4176c485..e0dba284 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 @@ -109,8 +109,8 @@ class Lobby( val player = players.removeAt(playerIndex) player.leave() - if (player == owner && players.isNotEmpty()) { - owner = players[0] + if (player == owner && players.any { it.isHuman }) { + owner = players.first { it.isHuman } } return player } diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/lobby/Player.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/lobby/Player.kt index 1b63a155..2ab97b0d 100644 --- a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/lobby/Player.kt +++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/lobby/Player.kt @@ -7,6 +7,7 @@ import org.luxons.sevenwonders.server.ApiMisuseException class Player( val username: String, var displayName: String, + val isHuman: Boolean = true, var icon: Icon? = null, ) { var index: Int = -1 diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/PlayerRepository.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/PlayerRepository.kt index c5dd6215..c93b219c 100644 --- a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/PlayerRepository.kt +++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/PlayerRepository.kt @@ -13,8 +13,8 @@ class PlayerRepository { operator fun contains(username: String): Boolean = players.containsKey(username) - fun createOrUpdate(username: String, displayName: String, icon: Icon? = null): Player { - val p = players.computeIfAbsent(username) { Player(username, displayName, icon) } + fun createOrUpdate(username: String, displayName: String, isHuman: Boolean = true, icon: Icon? = null): Player { + val p = players.computeIfAbsent(username) { Player(username, displayName, isHuman, icon) } p.displayName = displayName p.icon = icon return p diff --git a/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/controllers/HomeControllerTest.kt b/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/controllers/HomeControllerTest.kt index d7206490..f820621a 100644 --- a/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/controllers/HomeControllerTest.kt +++ b/sw-server/src/test/kotlin/org/luxons/sevenwonders/server/controllers/HomeControllerTest.kt @@ -13,7 +13,7 @@ class HomeControllerTest { val playerRepository = PlayerRepository() val homeController = HomeController(playerRepository) - val action = ChooseNameAction("Test User", Icon("person")) + val action = ChooseNameAction("Test User", Icon("person"), isHuman = true) val principal = TestPrincipal("testuser") val player = homeController.chooseName(action, principal) -- cgit