summaryrefslogtreecommitdiff
path: root/sw-ui/src/main/kotlin/org
diff options
context:
space:
mode:
authorjoffrey-bion <joffrey.bion@gmail.com>2020-12-12 16:18:28 +0100
committerjoffrey-bion <joffrey.bion@gmail.com>2020-12-12 16:18:28 +0100
commit71f2fc4f25bdfdeac7db9b8e62144c3110e3bf6a (patch)
treed8a88a126c74d3a36177980be4259b1a173fa1aa /sw-ui/src/main/kotlin/org
parentFix race in blinking test (diff)
downloadseven-wonders-71f2fc4f25bdfdeac7db9b8e62144c3110e3bf6a.tar.gz
seven-wonders-71f2fc4f25bdfdeac7db9b8e62144c3110e3bf6a.tar.bz2
seven-wonders-71f2fc4f25bdfdeac7db9b8e62144c3110e3bf6a.zip
Fix race conditions for game start and tests
Resolves: https://github.com/joffrey-bion/seven-wonders/issues/70
Diffstat (limited to 'sw-ui/src/main/kotlin/org')
-rw-r--r--sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/RouteBasedSagas.kt9
-rw-r--r--sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/Sagas.kt26
2 files changed, 18 insertions, 17 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 d4329f2f..88ecdcc1 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
@@ -19,14 +19,7 @@ suspend fun SwSagaContext.gameBrowserSaga(session: SevenWondersSession) {
}
suspend fun SwSagaContext.lobbySaga(session: SevenWondersSession) {
- val lobby = getState().currentLobby ?: error("Lobby saga run without a current lobby")
- coroutineScope {
- session.watchLobbyUpdates().map { UpdateLobbyAction(it) }.dispatchAllIn(this)
-
- val turnInfo = session.awaitGameStart(lobby.id)
- dispatch(EnterGameAction(getState().currentLobby!!, turnInfo))
- dispatch(Navigate(Route.GAME))
- }
+ session.watchLobbyUpdates().map { UpdateLobbyAction(it) }.dispatchAll()
}
suspend fun SwSagaContext.gameSaga(session: SevenWondersSession) {
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 c9f73111..7be6f65a 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
@@ -31,7 +31,7 @@ suspend fun SwSagaContext.rootSaga() = try {
}
launchApiActionHandlersIn(this, session)
- launchNavigationHandlers(this, session)
+ launchApiEventHandlersIn(this, session)
val player = session.chooseName(action.playerName, null)
dispatch(SetCurrentPlayerAction(player))
@@ -94,7 +94,14 @@ private fun SwSagaContext.launchApiActionHandlersIn(scope: CoroutineScope, sessi
scope.launchOnEach<RequestUnprepareMove> { session.unprepareMove() }
}
-private fun SwSagaContext.launchNavigationHandlers(scope: CoroutineScope, session: SevenWondersSession) {
+private fun SwSagaContext.launchApiEventHandlersIn(scope: CoroutineScope, session: SevenWondersSession) {
+
+ scope.launch {
+ session.watchLobbyJoined().collect { lobby ->
+ dispatch(EnterLobbyAction(lobby))
+ dispatch(Navigate(Route.LOBBY))
+ }
+ }
scope.launch {
session.watchLobbyLeft().collect { leftLobbyId ->
@@ -103,16 +110,17 @@ private fun SwSagaContext.launchNavigationHandlers(scope: CoroutineScope, sessio
}
}
+ scope.launch {
+ session.watchGameStarted().collect { turnInfo ->
+ val currentLobby = getState().currentLobby ?: error("Received game started event without being in a lobby")
+ dispatch(EnterGameAction(currentLobby, turnInfo))
+ dispatch(Navigate(Route.GAME))
+ }
+ }
+
// FIXME map this actions like others and await server event instead
scope.launchOnEach<RequestLeaveGame> {
session.leaveGame()
dispatch(Navigate(Route.GAME_BROWSER))
}
-
- scope.launch {
- session.watchLobbyJoined().collect { lobby ->
- dispatch(EnterLobbyAction(lobby))
- dispatch(Navigate(Route.LOBBY))
- }
- }
}
bgstack15