summaryrefslogtreecommitdiff
path: root/sw-ui
diff options
context:
space:
mode:
authorjoffrey-bion <joffrey.bion@gmail.com>2021-02-10 20:19:22 +0100
committerjoffrey-bion <joffrey.bion@gmail.com>2021-02-10 20:19:22 +0100
commit3d429c049a4f6a6612e51001603b8c54ff428ce1 (patch)
tree5eb97e1572a4f26286cbd48f40bd5df3d3a475ff /sw-ui
parentFix null value in input group (diff)
downloadseven-wonders-3d429c049a4f6a6612e51001603b8c54ff428ce1.tar.gz
seven-wonders-3d429c049a4f6a6612e51001603b8c54ff428ce1.tar.bz2
seven-wonders-3d429c049a4f6a6612e51001603b8c54ff428ce1.zip
Fix some browser navigation paths
Related: https://github.com/joffrey-bion/seven-wonders/issues/60
Diffstat (limited to 'sw-ui')
-rw-r--r--sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/RouteBasedSagas.kt26
-rw-r--r--sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/Sagas.kt2
-rw-r--r--sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/SagasFramework.kt5
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].
bgstack15