summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjoffrey-bion <joffrey.bion@gmail.com>2020-12-13 01:39:06 +0100
committerjoffrey-bion <joffrey.bion@gmail.com>2020-12-13 01:40:51 +0100
commit2d5117911c11fafc3c287e2ee74149f866c41954 (patch)
tree5b4dce07c2097a3f0e6b140ce5005d154d2c9c57
parentAdd logging of BOT status (diff)
downloadseven-wonders-2d5117911c11fafc3c287e2ee74149f866c41954.tar.gz
seven-wonders-2d5117911c11fafc3c287e2ee74149f866c41954.tar.bz2
seven-wonders-2d5117911c11fafc3c287e2ee74149f866c41954.zip
Only transfer ownership to humans
Resolve: https://github.com/joffrey-bion/seven-wonders/issues/73
-rw-r--r--sw-bot/src/main/kotlin/org/luxons/sevenwonders/bot/SevenWondersBot.kt2
-rw-r--r--sw-client/src/commonMain/kotlin/org/luxons/sevenwonders/client/SevenWondersClient.kt4
-rw-r--r--sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/api/Player.kt3
-rw-r--r--sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/api/actions/Actions.kt4
-rw-r--r--sw-server/src/main/kotlin/org/luxons/sevenwonders/server/api/Converters.kt10
-rw-r--r--sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/HomeController.kt4
-rw-r--r--sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/LobbyController.kt9
-rw-r--r--sw-server/src/main/kotlin/org/luxons/sevenwonders/server/lobby/Lobby.kt4
-rw-r--r--sw-server/src/main/kotlin/org/luxons/sevenwonders/server/lobby/Player.kt1
-rw-r--r--sw-server/src/main/kotlin/org/luxons/sevenwonders/server/repositories/PlayerRepository.kt4
-rw-r--r--sw-server/src/test/kotlin/org/luxons/sevenwonders/server/controllers/HomeControllerTest.kt2
-rw-r--r--sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/Sagas.kt2
12 files changed, 33 insertions, 16 deletions
diff --git a/sw-bot/src/main/kotlin/org/luxons/sevenwonders/bot/SevenWondersBot.kt b/sw-bot/src/main/kotlin/org/luxons/sevenwonders/bot/SevenWondersBot.kt
index d0331c15..17619f6a 100644
--- a/sw-bot/src/main/kotlin/org/luxons/sevenwonders/bot/SevenWondersBot.kt
+++ b/sw-bot/src/main/kotlin/org/luxons/sevenwonders/bot/SevenWondersBot.kt
@@ -37,7 +37,7 @@ class SevenWondersBot(
suspend fun play(serverUrl: String, gameId: Long) = withTimeout(botConfig.globalTimeout) {
val session = client.connect(serverUrl)
- val player = session.chooseName(displayName, Icon("desktop"))
+ val player = session.chooseName(displayName, Icon("desktop"), isHuman = false)
val gameStartedEvents = session.watchGameStarted()
session.joinGameAndWaitLobby(gameId)
val firstTurn = gameStartedEvents.first()
diff --git a/sw-client/src/commonMain/kotlin/org/luxons/sevenwonders/client/SevenWondersClient.kt b/sw-client/src/commonMain/kotlin/org/luxons/sevenwonders/client/SevenWondersClient.kt
index 090d1ee2..5e627ecd 100644
--- a/sw-client/src/commonMain/kotlin/org/luxons/sevenwonders/client/SevenWondersClient.kt
+++ b/sw-client/src/commonMain/kotlin/org/luxons/sevenwonders/client/SevenWondersClient.kt
@@ -42,14 +42,14 @@ class SevenWondersSession(private val stompSession: StompSessionWithKxSerializat
suspend fun watchErrors(): Flow<ErrorDTO> = stompSession.subscribe("/user/queue/errors", ErrorDTO.serializer())
- suspend fun chooseName(displayName: String, icon: Icon? = null): ConnectedPlayer {
+ suspend fun chooseName(displayName: String, icon: Icon? = null, isHuman: Boolean = true): ConnectedPlayer {
val sub = stompSession.subscribe(
destination = "/user/queue/nameChoice",
deserializer = ConnectedPlayer.serializer(),
)
stompSession.convertAndSend(
destination = "/app/chooseName",
- body = ChooseNameAction(displayName, icon),
+ body = ChooseNameAction(displayName, icon, isHuman),
serializer = ChooseNameAction.serializer(),
)
return sub.first()
diff --git a/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/api/Player.kt b/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/api/Player.kt
index db4365f5..3963112a 100644
--- a/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/api/Player.kt
+++ b/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/api/Player.kt
@@ -7,6 +7,7 @@ import org.luxons.sevenwonders.model.wonders.AssignedWonder
interface BasicPlayerInfo {
val username: String
val displayName: String
+ val isHuman: Boolean
val icon: Icon?
}
@@ -14,6 +15,7 @@ interface BasicPlayerInfo {
data class ConnectedPlayer(
override val username: String,
override val displayName: String,
+ override val isHuman: Boolean,
override val icon: Icon?,
) : BasicPlayerInfo
@@ -21,6 +23,7 @@ data class ConnectedPlayer(
data class PlayerDTO(
override val username: String,
override val displayName: String,
+ override val isHuman: Boolean,
override val icon: Icon?,
val wonder: AssignedWonder,
val isGameOwner: Boolean,
diff --git a/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/api/actions/Actions.kt b/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/api/actions/Actions.kt
index ee5caef8..b0be3ae0 100644
--- a/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/api/actions/Actions.kt
+++ b/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/api/actions/Actions.kt
@@ -18,6 +18,10 @@ class ChooseNameAction(
* The player's icon.
*/
val icon: Icon?,
+ /**
+ * Whether the new player is a human (as opposed to a bot).
+ */
+ val isHuman: Boolean,
)
@Serializable
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)
diff --git a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/Sagas.kt b/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/Sagas.kt
index 7be6f65a..02ff6e10 100644
--- a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/Sagas.kt
+++ b/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/Sagas.kt
@@ -33,7 +33,7 @@ suspend fun SwSagaContext.rootSaga() = try {
launchApiActionHandlersIn(this, session)
launchApiEventHandlersIn(this, session)
- val player = session.chooseName(action.playerName, null)
+ val player = session.chooseName(action.playerName)
dispatch(SetCurrentPlayerAction(player))
routerSaga(Route.GAME_BROWSER) {
bgstack15