From eef32bd9307a3a9f1ee3b532db2fb1f7cf37927a Mon Sep 17 00:00:00 2001 From: joffrey-bion Date: Thu, 10 Dec 2020 23:06:00 +0100 Subject: Allow owner to leave/disband the game Resolves: https://github.com/joffrey-bion/seven-wonders/issues/51 --- .../server/controllers/GameBrowserController.kt | 11 +++--- .../server/controllers/LobbyController.kt | 41 ++++++++++++++++++---- .../org/luxons/sevenwonders/server/lobby/Lobby.kt | 4 +-- 3 files changed, 41 insertions(+), 15 deletions(-) (limited to 'sw-server/src/main/kotlin/org/luxons') diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameBrowserController.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameBrowserController.kt index 587afc10..d6573f8f 100644 --- a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameBrowserController.kt +++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameBrowserController.kt @@ -1,8 +1,11 @@ package org.luxons.sevenwonders.server.controllers +import org.luxons.sevenwonders.model.api.GameListEvent +import org.luxons.sevenwonders.model.api.GameListEventWrapper import org.luxons.sevenwonders.model.api.LobbyDTO import org.luxons.sevenwonders.model.api.actions.CreateGameAction import org.luxons.sevenwonders.model.api.actions.JoinGameAction +import org.luxons.sevenwonders.model.api.wrap import org.luxons.sevenwonders.server.ApiMisuseException import org.luxons.sevenwonders.server.api.toDTO import org.luxons.sevenwonders.server.lobby.Lobby @@ -36,9 +39,9 @@ class GameBrowserController( * @return the current list of [Lobby]s */ @SubscribeMapping("/games") // prefix /topic not shown - fun listGames(principal: Principal): List { + fun listGames(principal: Principal): GameListEventWrapper { logger.info("Player '{}' subscribed to /topic/games", principal.name) - return lobbyRepository.list().map { it.toDTO() } + return GameListEvent.ReplaceList(lobbyRepository.list().map { it.toDTO() }).wrap() } /** @@ -61,9 +64,7 @@ class GameBrowserController( // notify everyone that a new game exists val lobbyDto = lobby.toDTO() - // we need to pass a non-generic type (array is fine) so that Spring doesn't break when trying to find a - // serializer from Kotlinx Serialization - template.convertAndSend("/topic/games", listOf(lobbyDto).toTypedArray()) + template.convertAndSend("/topic/games", GameListEvent.CreateOrUpdate(lobbyDto).wrap()) return lobbyDto } 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 7757c9d9..6cc404e9 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 @@ -3,10 +3,12 @@ package org.luxons.sevenwonders.server.controllers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch import org.luxons.sevenwonders.bot.SevenWondersBot +import org.luxons.sevenwonders.model.api.GameListEvent import org.luxons.sevenwonders.model.api.actions.AddBotAction import org.luxons.sevenwonders.model.api.actions.ReassignWondersAction import org.luxons.sevenwonders.model.api.actions.ReorderPlayersAction import org.luxons.sevenwonders.model.api.actions.UpdateSettingsAction +import org.luxons.sevenwonders.model.api.wrap import org.luxons.sevenwonders.model.hideHandsAndWaitForReadiness import org.luxons.sevenwonders.server.api.toDTO import org.luxons.sevenwonders.server.lobby.Lobby @@ -44,12 +46,38 @@ class LobbyController( val player = principal.player val lobby = player.lobby lobby.removePlayer(principal.name) + logger.info("Player {} left game '{}'", player, lobby.name) + template.convertAndSendToUser(player.username, "/queue/lobby/left", lobby.id) + if (lobby.getPlayers().isEmpty()) { - lobbyRepository.remove(lobby.id) + deleteLobby(lobby) + } else { + sendLobbyUpdateToPlayers(lobby) } + } - logger.info("Player {} left game '{}'", player, lobby.name) - sendLobbyUpdateToPlayers(lobby) + /** + * Disbands the current group, making everyone leave the lobby. + * + * @param principal the connected user's information + */ + @MessageMapping("/lobby/disband") + fun disband(principal: Principal) { + val player = principal.player + val lobby = player.ownedLobby + + lobby.getPlayers().forEach { + it.leave() + template.convertAndSendToUser(it.username, "/queue/lobby/left", lobby.id) + } + logger.info("Player {} disbanded game '{}'", player, lobby.name) + deleteLobby(lobby) + } + + private fun deleteLobby(lobby: Lobby) { + lobbyRepository.remove(lobby.id) + template.convertAndSend("/topic/games", GameListEvent.Delete(lobby.id).wrap()) + logger.info("Game '{}' removed", lobby.name) } /** @@ -110,12 +138,11 @@ class LobbyController( } internal fun sendLobbyUpdateToPlayers(lobby: Lobby) { + val lobbyDto = lobby.toDTO() lobby.getPlayers().forEach { - template.convertAndSendToUser(it.username, "/queue/lobby/updated", lobby.toDTO()) + template.convertAndSendToUser(it.username, "/queue/lobby/updated", lobbyDto) } - // we need to pass a non-generic type (array is fine) so that Spring doesn't break when trying to find a - // serializer from Kotlinx Serialization - template.convertAndSend("/topic/games", listOf(lobby.toDTO()).toTypedArray()) + template.convertAndSend("/topic/games", GameListEvent.CreateOrUpdate(lobbyDto).wrap()) } /** 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 6ebe39c1..4176c485 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 @@ -101,7 +101,7 @@ class Lobby( @Synchronized fun removePlayer(username: String): Player { - val playerIndex = find(username) + val playerIndex = players.indexOfFirst { it.username == username } if (playerIndex < 0) { throw UnknownPlayerException(username) } @@ -115,8 +115,6 @@ class Lobby( return player } - private fun find(username: String): Int = players.indexOfFirst { it.username == username } - fun setEndOfGame() { state = State.FINISHED } -- cgit