diff options
Diffstat (limited to 'sw-ui/src')
3 files changed, 29 insertions, 4 deletions
diff --git a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/RouteBasedSagas.kt b/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/RouteBasedSagas.kt index 88ecdcc1..1970bc08 100644 --- a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/RouteBasedSagas.kt +++ b/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/RouteBasedSagas.kt @@ -15,15 +15,39 @@ suspend fun SwSagaContext.homeSaga(session: SevenWondersSession) { } suspend fun SwSagaContext.gameBrowserSaga(session: SevenWondersSession) { + // browser navigation could have brought us here: we should leave the game/lobby + ensureNoCurrentGameNorLobby(session) session.watchGames().map { UpdateGameListAction(it) }.dispatchAll() } +private suspend fun SwSagaContext.ensureNoCurrentGameNorLobby(session: SevenWondersSession) { + if (reduxState.gameState != null) { + console.warn("User left a game via browser navigation, cleaning up...") + session.leaveGame() + } else if (reduxState.currentLobby != null) { + console.warn("User left the lobby via browser navigation, cleaning up...") + session.leaveLobby() + } +} + suspend fun SwSagaContext.lobbySaga(session: SevenWondersSession) { + // browser navigation could have brought us here: we should leave the current game in that case + if (reduxState.gameState != null) { + console.warn("User left a game via browser navigation, telling the server...") + session.leaveGame() + return + } + // browser navigation could have brought us here: we should go back to game browser if no lobby + if (reduxState.currentLobby == null) { + console.warn("User went to lobby via browser navigation, cleaning up...") + dispatch(Navigate(Route.GAME_BROWSER)) + return + } session.watchLobbyUpdates().map { UpdateLobbyAction(it) }.dispatchAll() } suspend fun SwSagaContext.gameSaga(session: SevenWondersSession) { - val game = getState().gameState ?: error("Game saga run without a current game") + val game = reduxState.gameState ?: error("Game saga run without a current game") coroutineScope { session.watchPlayerReady(game.id).map { PlayerReadyEvent(it) }.dispatchAllIn(this) session.watchPreparedCards(game.id).map { PreparedCardEvent(it) }.dispatchAllIn(this) 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 53ade155..d897fa22 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 @@ -113,7 +113,7 @@ private fun SwSagaContext.launchApiEventHandlersIn(scope: CoroutineScope, sessio scope.launch { session.watchGameStarted().collect { turnInfo -> - val currentLobby = getState().currentLobby ?: error("Received game started event without being in a lobby") + val currentLobby = reduxState.currentLobby ?: error("Received game started event without being in a lobby") dispatch(EnterGameAction(currentLobby, turnInfo)) dispatch(Navigate(Route.GAME)) } diff --git a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/SagasFramework.kt b/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/SagasFramework.kt index cb15460d..ea3fc26e 100644 --- a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/SagasFramework.kt +++ b/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/SagasFramework.kt @@ -63,9 +63,10 @@ class SagaContext<S, A : RAction, R>( private val actions: BroadcastChannel<A>, ) { /** - * Gets the current redux state. + * The current redux state. */ - fun getState(): S = reduxApi.getState() + val reduxState: S + get() = reduxApi.getState() /** * Dispatches the given redux [action]. |