From 4f3eec40a87c5e4f52fc89238642dd7a17650540 Mon Sep 17 00:00:00 2001 From: Joffrey Bion Date: Thu, 21 May 2020 13:46:30 +0200 Subject: Allow custom player icons --- .../main/kotlin/org/luxons/sevenwonders/server/api/Converters.kt | 2 +- .../org/luxons/sevenwonders/server/controllers/HomeController.kt | 4 ++-- .../src/main/kotlin/org/luxons/sevenwonders/server/lobby/Player.kt | 4 +++- .../org/luxons/sevenwonders/server/repositories/PlayerRepository.kt | 6 ++++-- .../luxons/sevenwonders/server/controllers/HomeControllerTest.kt | 4 +++- 5 files changed, 13 insertions(+), 7 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 1386c855..6ef13b8e 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 @@ -15,4 +15,4 @@ fun Lobby.toDTO(): LobbyDTO = LobbyDTO( maxPlayersReached = maxPlayersReached() ) -fun Player.toDTO() = PlayerDTO(username, displayName, index, isGameOwner, isReady) +fun Player.toDTO() = PlayerDTO(username, displayName, icon, index, isGameOwner, 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 c2619f8c..ff668fd9 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 @@ -34,10 +34,10 @@ class HomeController @Autowired constructor( @SendToUser("/queue/nameChoice") fun chooseName(@Validated action: ChooseNameAction, principal: Principal): ConnectedPlayer { val username = principal.name - val player = playerRepository.createOrUpdate(username, action.playerName) + val player = playerRepository.createOrUpdate(username, action.playerName, action.icon) logger.info("Player '{}' chose the name '{}'", username, player.displayName) - return ConnectedPlayer(username, player.displayName) + return ConnectedPlayer(username, player.displayName, player.icon) } companion object { 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 8ed7d048..95b37643 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 @@ -1,11 +1,13 @@ package org.luxons.sevenwonders.server.lobby import org.luxons.sevenwonders.engine.Game +import org.luxons.sevenwonders.model.api.actions.Icon import org.luxons.sevenwonders.server.ApiMisuseException class Player( val username: String, - var displayName: String + var displayName: String, + 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 b31d2cee..e76bdaea 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 @@ -1,5 +1,6 @@ package org.luxons.sevenwonders.server.repositories +import org.luxons.sevenwonders.model.api.actions.Icon import org.luxons.sevenwonders.server.ApiMisuseException import org.luxons.sevenwonders.server.lobby.Player import org.springframework.stereotype.Repository @@ -12,9 +13,10 @@ class PlayerRepository { operator fun contains(username: String): Boolean = players.containsKey(username) - fun createOrUpdate(username: String, displayName: String): Player { - val p = players.computeIfAbsent(username) { Player(username, displayName) } + fun createOrUpdate(username: String, displayName: String, icon: Icon? = null): Player { + val p = players.computeIfAbsent(username) { Player(username, displayName, 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 31b957e4..d7206490 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 @@ -2,6 +2,7 @@ package org.luxons.sevenwonders.server.controllers import org.junit.Test import org.luxons.sevenwonders.model.api.actions.ChooseNameAction +import org.luxons.sevenwonders.model.api.actions.Icon import org.luxons.sevenwonders.server.repositories.PlayerRepository import kotlin.test.assertEquals @@ -12,12 +13,13 @@ class HomeControllerTest { val playerRepository = PlayerRepository() val homeController = HomeController(playerRepository) - val action = ChooseNameAction("Test User") + val action = ChooseNameAction("Test User", Icon("person")) val principal = TestPrincipal("testuser") val player = homeController.chooseName(action, principal) assertEquals("testuser", player.username) assertEquals("Test User", player.displayName) + assertEquals("person", player.icon?.name) } } -- cgit