summaryrefslogtreecommitdiff
path: root/backend/src/main/kotlin/org/luxons
diff options
context:
space:
mode:
authorJoffrey BION <joffrey.bion@gmail.com>2018-07-14 03:36:03 +0200
committerJoffrey BION <joffrey.bion@gmail.com>2018-07-14 03:36:18 +0200
commit85544d85ec23b3827c9f37a33266d9096da9a07e (patch)
tree907f13b397bbf4aeef64dd175f5a94d9f7e62fc5 /backend/src/main/kotlin/org/luxons
parentKotlin migration: Spring server (diff)
downloadseven-wonders-85544d85ec23b3827c9f37a33266d9096da9a07e.tar.gz
seven-wonders-85544d85ec23b3827c9f37a33266d9096da9a07e.tar.bz2
seven-wonders-85544d85ec23b3827c9f37a33266d9096da9a07e.zip
Add DTO classes for webservice output
Diffstat (limited to 'backend/src/main/kotlin/org/luxons')
-rw-r--r--backend/src/main/kotlin/org/luxons/sevenwonders/api/LobbyDTO.kt17
-rw-r--r--backend/src/main/kotlin/org/luxons/sevenwonders/api/PlayerDTO.kt14
-rw-r--r--backend/src/main/kotlin/org/luxons/sevenwonders/controllers/GameBrowserController.kt23
-rw-r--r--backend/src/main/kotlin/org/luxons/sevenwonders/controllers/HomeController.kt9
-rw-r--r--backend/src/main/kotlin/org/luxons/sevenwonders/controllers/LobbyController.kt11
-rw-r--r--backend/src/main/kotlin/org/luxons/sevenwonders/lobby/Lobby.kt12
6 files changed, 60 insertions, 26 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
}
bgstack15