diff options
author | Joffrey Bion <joffrey.bion@booking.com> | 2020-03-26 22:25:02 +0100 |
---|---|---|
committer | Joffrey Bion <joffrey.bion@booking.com> | 2020-03-27 10:59:39 +0100 |
commit | b52f3f67ec4f4e1bc852c8c650ee9c95f7fe8268 (patch) | |
tree | 9c1eaf1fd1039bdfc6ec2d34526933338056b64d | |
parent | Rework sagas and router to sub/unsubscribe properly (diff) | |
download | seven-wonders-b52f3f67ec4f4e1bc852c8c650ee9c95f7fe8268.tar.gz seven-wonders-b52f3f67ec4f4e1bc852c8c650ee9c95f7fe8268.tar.bz2 seven-wonders-b52f3f67ec4f4e1bc852c8c650ee9c95f7fe8268.zip |
Fix lobby updates
4 files changed, 18 insertions, 14 deletions
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 c4293c2a..c0d59ac0 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 @@ -85,8 +85,8 @@ class SevenWondersSession(private val stompSession: StompSessionWithKxSerializat stompSession.convertAndSend("/app/lobby/reorderPlayers", UpdateSettingsAction(settings), UpdateSettingsAction.serializer()) } - suspend fun watchLobbyUpdates(gameId: Long): StompSubscription<LobbyDTO> = - stompSession.subscribe("/topic/lobby/$gameId/updated", LobbyDTO.serializer()) + suspend fun watchLobbyUpdates(): StompSubscription<LobbyDTO> = + stompSession.subscribe("/user/queue/lobby/updated", LobbyDTO.serializer()) suspend fun awaitGameStart(gameId: Long) { val gameStartSubscription = stompSession.subscribeEmptyMsg("/topic/lobby/$gameId/started") 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 aa98f121..0bd1de79 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 @@ -88,7 +88,7 @@ class GameBrowserController @Autowired constructor( logger.info("Player '{}' ({}) joined game {}", player.displayName, player.username, lobby.name) val lobbyDTO = lobby.toDTO(player) - lobbyController.sendLobbyUpdateToPlayers(lobbyDTO) + lobbyController.sendLobbyUpdateToPlayers(lobby, player) 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 477a6739..b74913cb 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 @@ -1,10 +1,10 @@ package org.luxons.sevenwonders.server.controllers import org.hildan.livedoc.core.annotations.Api -import org.luxons.sevenwonders.model.api.LobbyDTO import org.luxons.sevenwonders.model.api.actions.ReorderPlayersAction import org.luxons.sevenwonders.model.api.actions.UpdateSettingsAction import org.luxons.sevenwonders.server.api.toDTO +import org.luxons.sevenwonders.server.lobby.Lobby import org.luxons.sevenwonders.server.lobby.Player import org.luxons.sevenwonders.server.repositories.LobbyRepository import org.luxons.sevenwonders.server.repositories.PlayerRepository @@ -37,14 +37,15 @@ class LobbyController @Autowired constructor( */ @MessageMapping("/lobby/leave") fun leave(principal: Principal) { - val lobby = principal.player.lobby - val player = lobby.removePlayer(principal.name) + val player = principal.player + val lobby = player.lobby + lobby.removePlayer(principal.name) if (lobby.getPlayers().isEmpty()) { lobbyRepository.remove(lobby.id) } logger.info("Player {} left game '{}'", player, lobby.name) - sendLobbyUpdateToPlayers(lobby.toDTO(principal.player)) + sendLobbyUpdateToPlayers(lobby, player) } /** @@ -61,7 +62,7 @@ class LobbyController @Autowired constructor( lobby.reorderPlayers(action.orderedPlayers) logger.info("Players in game '{}' reordered to {}", lobby.name, action.orderedPlayers) - sendLobbyUpdateToPlayers(lobby.toDTO(principal.player)) + sendLobbyUpdateToPlayers(lobby, principal.player) } /** @@ -74,16 +75,19 @@ class LobbyController @Autowired constructor( */ @MessageMapping("/lobby/updateSettings") fun updateSettings(@Validated action: UpdateSettingsAction, principal: Principal) { - val lobby = principal.player.ownedLobby + val player = principal.player + val lobby = player.ownedLobby lobby.settings = action.settings logger.info("Updated settings of game '{}'", lobby.name) - sendLobbyUpdateToPlayers(lobby.toDTO(principal.player)) + sendLobbyUpdateToPlayers(lobby, player) } - internal fun sendLobbyUpdateToPlayers(lobby: LobbyDTO) { - template.convertAndSend("/topic/lobby/" + lobby.id + "/updated", lobby) - template.convertAndSend("/topic/games", listOf(lobby)) + internal fun sendLobbyUpdateToPlayers(lobby: Lobby, player: Player) { + lobby.getPlayers().forEach { + template.convertAndSendToUser(it.username, "/queue/lobby/updated", lobby.toDTO(it)) + } + template.convertAndSend("/topic/games", listOf(lobby.toDTO(player))) } /** diff --git a/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/LobbySagas.kt b/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/LobbySagas.kt index 143fecd8..b081e61d 100644 --- a/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/LobbySagas.kt +++ b/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/LobbySagas.kt @@ -14,7 +14,7 @@ import org.luxons.sevenwonders.ui.router.Route suspend fun SwSagaContext.lobbySaga(session: SevenWondersSession) { val lobbyId = getState().currentLobbyId ?: error("Lobby saga run without a current lobby") coroutineScope { - val lobbyUpdatesSubscription = session.watchLobbyUpdates(lobbyId) + val lobbyUpdatesSubscription = session.watchLobbyUpdates() launch { watchLobbyUpdates(lobbyUpdatesSubscription) } val startGameJob = launch { awaitStartGame(session) } |