diff options
Diffstat (limited to 'backend')
9 files changed, 70 insertions, 36 deletions
diff --git a/backend/src/main/kotlin/org/luxons/sevenwonders/api/LobbyDTO.kt b/backend/src/main/kotlin/org/luxons/sevenwonders/api/LobbyDTO.kt new file mode 100644 index 00000000..b4445f32 --- /dev/null +++ b/backend/src/main/kotlin/org/luxons/sevenwonders/api/LobbyDTO.kt @@ -0,0 +1,17 @@ +package org.luxons.sevenwonders.api + +import org.luxons.sevenwonders.lobby.Lobby +import org.luxons.sevenwonders.lobby.State + +data class LobbyDTO( + val id: Long, + val name: String, + val owner: String, + val players: List<PlayerDTO>, + val state: State +) + +fun Lobby.toDTO(currentUser: String): LobbyDTO { + val players = getPlayers().map { it.toDTO(currentUser) } + return LobbyDTO(id, name, owner.username, players, state) +} diff --git a/backend/src/main/kotlin/org/luxons/sevenwonders/api/PlayerDTO.kt b/backend/src/main/kotlin/org/luxons/sevenwonders/api/PlayerDTO.kt new file mode 100644 index 00000000..54c69122 --- /dev/null +++ b/backend/src/main/kotlin/org/luxons/sevenwonders/api/PlayerDTO.kt @@ -0,0 +1,14 @@ +package org.luxons.sevenwonders.api + +import org.luxons.sevenwonders.lobby.Player + +data class PlayerDTO( + val username: String, + val displayName: String, + val index: Int, + val isGameOwner: Boolean, + val isUser: Boolean +) + +fun Player.toDTO(currentUser: String) = + PlayerDTO(username, displayName, index, isGameOwner, username === currentUser) diff --git a/backend/src/main/kotlin/org/luxons/sevenwonders/controllers/GameBrowserController.kt b/backend/src/main/kotlin/org/luxons/sevenwonders/controllers/GameBrowserController.kt index b8e4e732..f856365f 100644 --- a/backend/src/main/kotlin/org/luxons/sevenwonders/controllers/GameBrowserController.kt +++ b/backend/src/main/kotlin/org/luxons/sevenwonders/controllers/GameBrowserController.kt @@ -3,6 +3,8 @@ package org.luxons.sevenwonders.controllers import org.hildan.livedoc.core.annotations.Api import org.luxons.sevenwonders.actions.CreateGameAction import org.luxons.sevenwonders.actions.JoinGameAction +import org.luxons.sevenwonders.api.LobbyDTO +import org.luxons.sevenwonders.api.toDTO import org.luxons.sevenwonders.errors.ApiMisuseException import org.luxons.sevenwonders.lobby.Lobby import org.luxons.sevenwonders.repositories.LobbyRepository @@ -38,9 +40,9 @@ class GameBrowserController @Autowired constructor( * @return the current list of [Lobby]s */ @SubscribeMapping("/games") // prefix /topic not shown - fun listGames(principal: Principal): Collection<Lobby> { + fun listGames(principal: Principal): Collection<LobbyDTO> { logger.info("Player '{}' subscribed to /topic/games", principal.name) - return lobbyRepository.list() + return lobbyRepository.list().map { it.toDTO(principal.name) } } /** @@ -53,7 +55,7 @@ class GameBrowserController @Autowired constructor( */ @MessageMapping("/lobby/create") @SendToUser("/queue/lobby/joined") - fun createGame(@Validated action: CreateGameAction, principal: Principal): Lobby { + fun createGame(@Validated action: CreateGameAction, principal: Principal): LobbyDTO { checkThatUserIsNotInAGame(principal, "cannot create another game") val gameOwner = playerRepository.find(principal.name) @@ -64,8 +66,9 @@ class GameBrowserController @Autowired constructor( ) // notify everyone that a new game exists - template.convertAndSend("/topic/games", listOf(lobby)) - return lobby + val lobbyDto = lobby.toDTO(principal.name) + template.convertAndSend("/topic/games", listOf(lobbyDto)) + return lobbyDto } /** @@ -78,18 +81,19 @@ class GameBrowserController @Autowired constructor( */ @MessageMapping("/lobby/join") @SendToUser("/queue/lobby/joined") - fun joinGame(@Validated action: JoinGameAction, principal: Principal): Lobby { + fun joinGame(@Validated action: JoinGameAction, principal: Principal): LobbyDTO { checkThatUserIsNotInAGame(principal, "cannot join another game") - val lobby = lobbyRepository.find(action.gameId!!) + val lobby = lobbyRepository.find(action.gameId) val newPlayer = playerRepository.find(principal.name) lobby.addPlayer(newPlayer) logger.info( "Player '{}' ({}) joined game {}", newPlayer.displayName, newPlayer.username, lobby.name ) - lobbyController.sendLobbyUpdateToPlayers(lobby) - return lobby + val lobbyDTO = lobby.toDTO(principal.name) + lobbyController.sendLobbyUpdateToPlayers(lobbyDTO) + return lobbyDTO } private fun checkThatUserIsNotInAGame(principal: Principal, impossibleActionDescription: String) { @@ -103,7 +107,6 @@ class GameBrowserController @Autowired constructor( ApiMisuseException("Client already in game '$gameName', $impossibleActionDescription") companion object { - private val logger = LoggerFactory.getLogger(GameBrowserController::class.java) } } diff --git a/backend/src/main/kotlin/org/luxons/sevenwonders/controllers/HomeController.kt b/backend/src/main/kotlin/org/luxons/sevenwonders/controllers/HomeController.kt index e658a26b..a3ccd148 100644 --- a/backend/src/main/kotlin/org/luxons/sevenwonders/controllers/HomeController.kt +++ b/backend/src/main/kotlin/org/luxons/sevenwonders/controllers/HomeController.kt @@ -2,7 +2,8 @@ package org.luxons.sevenwonders.controllers import org.hildan.livedoc.core.annotations.Api import org.luxons.sevenwonders.actions.ChooseNameAction -import org.luxons.sevenwonders.lobby.Player +import org.luxons.sevenwonders.api.PlayerDTO +import org.luxons.sevenwonders.api.toDTO import org.luxons.sevenwonders.repositories.PlayerRepository import org.slf4j.LoggerFactory import org.springframework.beans.factory.annotation.Autowired @@ -29,16 +30,16 @@ class HomeController @Autowired constructor( * @param principal * the connected user's information * - * @return the created [Player] object + * @return the created [PlayerDTO] object */ @MessageMapping("/chooseName") @SendToUser("/queue/nameChoice") - fun chooseName(@Validated action: ChooseNameAction, principal: Principal): Player { + fun chooseName(@Validated action: ChooseNameAction, principal: Principal): PlayerDTO { val username = principal.name val player = playerRepository.createOrUpdate(username, action.playerName) logger.info("Player '{}' chose the name '{}'", username, player.displayName) - return player + return player.toDTO(username) } companion object { diff --git a/backend/src/main/kotlin/org/luxons/sevenwonders/controllers/LobbyController.kt b/backend/src/main/kotlin/org/luxons/sevenwonders/controllers/LobbyController.kt index 3b15d68a..4e4120a9 100644 --- a/backend/src/main/kotlin/org/luxons/sevenwonders/controllers/LobbyController.kt +++ b/backend/src/main/kotlin/org/luxons/sevenwonders/controllers/LobbyController.kt @@ -3,7 +3,8 @@ package org.luxons.sevenwonders.controllers import org.hildan.livedoc.core.annotations.Api import org.luxons.sevenwonders.actions.ReorderPlayersAction import org.luxons.sevenwonders.actions.UpdateSettingsAction -import org.luxons.sevenwonders.lobby.Lobby +import org.luxons.sevenwonders.api.LobbyDTO +import org.luxons.sevenwonders.api.toDTO import org.luxons.sevenwonders.lobby.Player import org.luxons.sevenwonders.repositories.LobbyRepository import org.luxons.sevenwonders.repositories.PlayerRepository @@ -43,7 +44,7 @@ class LobbyController @Autowired constructor( } logger.info("Player {} left game '{}'", player, lobby.name) - sendLobbyUpdateToPlayers(lobby) + sendLobbyUpdateToPlayers(lobby.toDTO(principal.name)) } /** @@ -60,7 +61,7 @@ class LobbyController @Autowired constructor( lobby.reorderPlayers(action.orderedPlayers) logger.info("Players in game '{}' reordered to {}", lobby.name, action.orderedPlayers) - sendLobbyUpdateToPlayers(lobby) + sendLobbyUpdateToPlayers(lobby.toDTO(principal.name)) } /** @@ -77,10 +78,10 @@ class LobbyController @Autowired constructor( lobby.settings = action.settings logger.info("Updated settings of game '{}'", lobby.name) - sendLobbyUpdateToPlayers(lobby) + sendLobbyUpdateToPlayers(lobby.toDTO(principal.name)) } - internal fun sendLobbyUpdateToPlayers(lobby: Lobby) { + internal fun sendLobbyUpdateToPlayers(lobby: LobbyDTO) { template.convertAndSend("/topic/lobby/" + lobby.id + "/updated", lobby) template.convertAndSend("/topic/games", listOf(lobby)) } diff --git a/backend/src/main/kotlin/org/luxons/sevenwonders/lobby/Lobby.kt b/backend/src/main/kotlin/org/luxons/sevenwonders/lobby/Lobby.kt index aaafd517..173ced64 100644 --- a/backend/src/main/kotlin/org/luxons/sevenwonders/lobby/Lobby.kt +++ b/backend/src/main/kotlin/org/luxons/sevenwonders/lobby/Lobby.kt @@ -12,20 +12,18 @@ enum class State { class Lobby( val id: Long, val name: String, - private var _owner: Player, + var owner: Player, @field:Transient private val gameDefinition: GameDefinition ) { private var players: MutableList<Player> = ArrayList(gameDefinition.maxPlayers) var settings: CustomizableSettings = CustomizableSettings() - var owner = _owner.username - var state = State.LOBBY private set init { - addPlayer(_owner) + addPlayer(owner) } fun getPlayers(): List<Player> = players @@ -73,7 +71,7 @@ class Lobby( players.firstOrNull { it.username == username } ?: throw UnknownPlayerException(username) @Synchronized - fun isOwner(username: String?): Boolean = _owner.username == username + fun isOwner(username: String?): Boolean = owner.username == username @Synchronized fun containsUser(username: String): Boolean = players.any { it.username == username } @@ -84,8 +82,8 @@ class Lobby( players.remove(player) player.leave() - if (player == _owner && !players.isEmpty()) { - _owner = players[0] + if (player == owner && !players.isEmpty()) { + owner = players[0] } return player } diff --git a/backend/src/test/kotlin/org/luxons/sevenwonders/controllers/GameBrowserControllerTest.kt b/backend/src/test/kotlin/org/luxons/sevenwonders/controllers/GameBrowserControllerTest.kt index 96d4bc85..97873bc1 100644 --- a/backend/src/test/kotlin/org/luxons/sevenwonders/controllers/GameBrowserControllerTest.kt +++ b/backend/src/test/kotlin/org/luxons/sevenwonders/controllers/GameBrowserControllerTest.kt @@ -5,6 +5,7 @@ import org.junit.Before import org.junit.Test import org.luxons.sevenwonders.actions.CreateGameAction import org.luxons.sevenwonders.actions.JoinGameAction +import org.luxons.sevenwonders.api.toDTO import org.luxons.sevenwonders.controllers.GameBrowserController.UserAlreadyInGameException import org.luxons.sevenwonders.repositories.LobbyRepository import org.luxons.sevenwonders.repositories.PlayerNotFoundException @@ -47,8 +48,8 @@ class GameBrowserControllerTest { val games = gameBrowserController.listGames(principal) assertFalse(games.isEmpty()) val lobby = games.iterator().next() - assertSame(lobby, createdLobby) - assertSame(player, lobby.getPlayers()[0]) + assertEquals(lobby, createdLobby) + assertEquals(player.toDTO(principal.name), lobby.players[0]) } @Test(expected = PlayerNotFoundException::class) @@ -82,6 +83,7 @@ class GameBrowserControllerTest { val createGameAction = CreateGameAction("Test Game") val createdLobby = gameBrowserController.createGame(createGameAction, ownerPrincipal) + assertEquals(owner.toDTO(ownerPrincipal.name), createdLobby.players[0]) val joiner = playerRepository.createOrUpdate("testjoiner", "Test User Joiner") val joinerPrincipal = TestPrincipal("testjoiner") @@ -89,9 +91,8 @@ class GameBrowserControllerTest { val joinedLobby = gameBrowserController.joinGame(joinGameAction, joinerPrincipal) - assertSame(createdLobby, joinedLobby) - assertSame(owner, joinedLobby.getPlayers()[0]) - assertSame(joiner, joinedLobby.getPlayers()[1]) + assertEquals(owner.toDTO(joinerPrincipal.name), joinedLobby.players[0]) + assertEquals(joiner.toDTO(joinerPrincipal.name), joinedLobby.players[1]) } @Test(expected = UserAlreadyInGameException::class) diff --git a/backend/src/test/kotlin/org/luxons/sevenwonders/controllers/HomeControllerTest.kt b/backend/src/test/kotlin/org/luxons/sevenwonders/controllers/HomeControllerTest.kt index b11ef878..65989db6 100644 --- a/backend/src/test/kotlin/org/luxons/sevenwonders/controllers/HomeControllerTest.kt +++ b/backend/src/test/kotlin/org/luxons/sevenwonders/controllers/HomeControllerTest.kt @@ -1,6 +1,6 @@ package org.luxons.sevenwonders.controllers -import org.junit.Assert.* +import org.junit.Assert.assertEquals import org.junit.Test import org.luxons.sevenwonders.actions.ChooseNameAction import org.luxons.sevenwonders.repositories.PlayerRepository @@ -17,10 +17,9 @@ class HomeControllerTest { val player = homeController.chooseName(action, principal) - assertSame(player, playerRepository.find("testuser")) assertEquals("testuser", player.username) assertEquals("Test User", player.displayName) - assertFalse(player.isInLobby) - assertFalse(player.isInGame) + assertEquals(false, player.isGameOwner) + assertEquals(true, player.isUser) } } diff --git a/backend/src/test/kotlin/org/luxons/sevenwonders/controllers/LobbyControllerTest.kt b/backend/src/test/kotlin/org/luxons/sevenwonders/controllers/LobbyControllerTest.kt index 6269ae40..e366048f 100644 --- a/backend/src/test/kotlin/org/luxons/sevenwonders/controllers/LobbyControllerTest.kt +++ b/backend/src/test/kotlin/org/luxons/sevenwonders/controllers/LobbyControllerTest.kt @@ -42,7 +42,7 @@ class LobbyControllerTest { assertTrue(lobby.getPlayers().contains(owner)) assertSame(lobby, owner.lobby) - assertEquals(owner.username, lobby.owner) + assertEquals(owner, lobby.owner) assertTrue(owner.isInLobby) assertFalse(owner.isInGame) } |