From 747911a542b5971de182e4491393a8f53eb8e9a8 Mon Sep 17 00:00:00 2001 From: joffrey-bion Date: Thu, 10 Dec 2020 01:30:51 +0100 Subject: Decouple some sagas from routes Resolves: https://github.com/joffrey-bion/seven-wonders/issues/13 --- .../ui/redux/sagas/GameBrowserSagas.kt | 52 ----------------- .../sevenwonders/ui/redux/sagas/GameSagas.kt | 35 ------------ .../sevenwonders/ui/redux/sagas/LobbySagas.kt | 65 ---------------------- .../sevenwonders/ui/redux/sagas/RouteBasedSagas.kt | 41 ++++++++++++++ .../luxons/sevenwonders/ui/redux/sagas/Sagas.kt | 50 ++++++++++++++--- .../sevenwonders/ui/redux/sagas/SagasFramework.kt | 8 +++ 6 files changed, 91 insertions(+), 160 deletions(-) delete mode 100644 sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/GameBrowserSagas.kt delete mode 100644 sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/GameSagas.kt delete mode 100644 sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/LobbySagas.kt create mode 100644 sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/RouteBasedSagas.kt (limited to 'sw-ui/src/main/kotlin/org') diff --git a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/GameBrowserSagas.kt b/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/GameBrowserSagas.kt deleted file mode 100644 index b6f3662a..00000000 --- a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/GameBrowserSagas.kt +++ /dev/null @@ -1,52 +0,0 @@ -package org.luxons.sevenwonders.ui.redux.sagas - -import kotlinx.coroutines.cancelAndJoin -import kotlinx.coroutines.coroutineScope -import kotlinx.coroutines.flow.map -import kotlinx.coroutines.launch -import org.luxons.sevenwonders.client.SevenWondersSession -import org.luxons.sevenwonders.model.api.LobbyDTO -import org.luxons.sevenwonders.ui.redux.EnterLobbyAction -import org.luxons.sevenwonders.ui.redux.RequestCreateGame -import org.luxons.sevenwonders.ui.redux.RequestJoinGame -import org.luxons.sevenwonders.ui.redux.UpdateGameListAction -import org.luxons.sevenwonders.ui.router.Navigate -import org.luxons.sevenwonders.ui.router.Route -import org.luxons.sevenwonders.ui.utils.awaitFirst - -suspend fun SwSagaContext.gameBrowserSaga(session: SevenWondersSession) { - GameBrowserSaga(session, this).run() -} - -private class GameBrowserSaga( - private val session: SevenWondersSession, - private val sagaContext: SwSagaContext, -) { - suspend fun run() { - coroutineScope { - val gamesDispatch = launch { dispatchGameUpdates() } - val lobby = awaitCreateOrJoinGame() - gamesDispatch.cancelAndJoin() - sagaContext.dispatch(EnterLobbyAction(lobby)) - sagaContext.dispatch(Navigate(Route.LOBBY)) - } - } - - private suspend fun dispatchGameUpdates() { - with(sagaContext) { - session.watchGames().map { UpdateGameListAction(it) }.dispatchAll() - } - } - - private suspend fun awaitCreateOrJoinGame(): LobbyDTO = awaitFirst(this::awaitCreateGame, this::awaitJoinGame) - - private suspend fun awaitCreateGame(): LobbyDTO { - val action = sagaContext.next() - return session.createGame(action.gameName) - } - - private suspend fun awaitJoinGame(): LobbyDTO { - val action = sagaContext.next() - return session.joinGame(action.gameId) - } -} diff --git a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/GameSagas.kt b/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/GameSagas.kt deleted file mode 100644 index fb9bdfe2..00000000 --- a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/GameSagas.kt +++ /dev/null @@ -1,35 +0,0 @@ -package org.luxons.sevenwonders.ui.redux.sagas - -import kotlinx.coroutines.coroutineScope -import kotlinx.coroutines.flow.map -import kotlinx.coroutines.launch -import org.luxons.sevenwonders.client.SevenWondersSession -import org.luxons.sevenwonders.ui.redux.PlayerReadyEvent -import org.luxons.sevenwonders.ui.redux.PreparedCardEvent -import org.luxons.sevenwonders.ui.redux.PreparedMoveEvent -import org.luxons.sevenwonders.ui.redux.RequestLeaveGame -import org.luxons.sevenwonders.ui.redux.RequestPrepareMove -import org.luxons.sevenwonders.ui.redux.RequestSayReady -import org.luxons.sevenwonders.ui.redux.RequestUnprepareMove -import org.luxons.sevenwonders.ui.redux.TurnInfoEvent -import org.luxons.sevenwonders.ui.router.Navigate -import org.luxons.sevenwonders.ui.router.Route - -suspend fun SwSagaContext.gameSaga(session: SevenWondersSession) { - val game = getState().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) - session.watchOwnMoves().map { PreparedMoveEvent(it) }.dispatchAllIn(this) - session.watchTurns().map { TurnInfoEvent(it) }.dispatchAllIn(this) - - launch { onEach { session.sayReady() } } - launch { onEach { session.prepareMove(it.move) } } - launch { onEach { session.unprepareMove() } } - - next() - session.leaveGame() - dispatch(Navigate(Route.GAME_BROWSER)) - } - console.log("End of game saga") -} diff --git a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/LobbySagas.kt b/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/LobbySagas.kt deleted file mode 100644 index 37872017..00000000 --- a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/LobbySagas.kt +++ /dev/null @@ -1,65 +0,0 @@ -package org.luxons.sevenwonders.ui.redux.sagas - -import kotlinx.coroutines.coroutineScope -import kotlinx.coroutines.flow.map -import kotlinx.coroutines.launch -import org.luxons.sevenwonders.client.SevenWondersSession -import org.luxons.sevenwonders.ui.redux.EnterGameAction -import org.luxons.sevenwonders.ui.redux.RequestAddBot -import org.luxons.sevenwonders.ui.redux.RequestLeaveLobby -import org.luxons.sevenwonders.ui.redux.RequestReassignWonders -import org.luxons.sevenwonders.ui.redux.RequestReorderPlayers -import org.luxons.sevenwonders.ui.redux.RequestStartGame -import org.luxons.sevenwonders.ui.redux.UpdateLobbyAction -import org.luxons.sevenwonders.ui.router.Navigate -import org.luxons.sevenwonders.ui.router.Route -import org.luxons.sevenwonders.ui.utils.awaitFirst - -suspend fun SwSagaContext.lobbySaga(session: SevenWondersSession) { - val lobby = getState().currentLobby ?: error("Lobby saga run without a current lobby") - coroutineScope { - val lobbyUpdatesSubscription = session.watchLobbyUpdates().map { UpdateLobbyAction(it) }.dispatchAllIn(this) - - launch { - onEach { session.addBot(it.botDisplayName) } - } - launch { - onEach { session.reorderPlayers(it.orderedPlayers) } - } - launch { - onEach { session.reassignWonders(it.wonders) } - } - val startGameJob = launch { awaitStartGame(session) } - - awaitFirst( - { - awaitLeaveLobby(session) - lobbyUpdatesSubscription.cancel() - startGameJob.cancel() - dispatch(Navigate(Route.GAME_BROWSER)) - }, - { - awaitGameStart(session, lobby.id) - lobbyUpdatesSubscription.cancel() - startGameJob.cancel() - dispatch(Navigate(Route.GAME)) - }, - ) - } -} - -private suspend fun SwSagaContext.awaitGameStart(session: SevenWondersSession, lobbyId: Long) { - val turnInfo = session.awaitGameStart(lobbyId) - val lobby = getState().currentLobby!! - dispatch(EnterGameAction(lobby, turnInfo)) -} - -private suspend fun SwSagaContext.awaitStartGame(session: SevenWondersSession) { - next() - session.startGame() -} - -private suspend fun SwSagaContext.awaitLeaveLobby(session: SevenWondersSession) { - next() - session.leaveLobby() -} 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 new file mode 100644 index 00000000..d4329f2f --- /dev/null +++ b/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/RouteBasedSagas.kt @@ -0,0 +1,41 @@ +package org.luxons.sevenwonders.ui.redux.sagas + +import kotlinx.coroutines.coroutineScope +import kotlinx.coroutines.flow.map +import org.luxons.sevenwonders.client.SevenWondersSession +import org.luxons.sevenwonders.ui.redux.* +import org.luxons.sevenwonders.ui.router.Navigate +import org.luxons.sevenwonders.ui.router.Route + +suspend fun SwSagaContext.homeSaga(session: SevenWondersSession) { + val action = next() + val player = session.chooseName(action.playerName) + dispatch(SetCurrentPlayerAction(player)) + dispatch(Navigate(Route.GAME_BROWSER)) +} + +suspend fun SwSagaContext.gameBrowserSaga(session: SevenWondersSession) { + session.watchGames().map { UpdateGameListAction(it) }.dispatchAll() +} + +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)) + } +} + +suspend fun SwSagaContext.gameSaga(session: SevenWondersSession) { + val game = getState().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) + session.watchOwnMoves().map { PreparedMoveEvent(it) }.dispatchAllIn(this) + session.watchTurns().map { TurnInfoEvent(it) }.dispatchAllIn(this) + } + console.log("End of game saga") +} 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 6339f051..c2d26e0f 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 @@ -8,10 +8,8 @@ import org.hildan.krossbow.stomp.MissingHeartBeatException import org.hildan.krossbow.stomp.WebSocketClosedUnexpectedly import org.luxons.sevenwonders.client.SevenWondersClient import org.luxons.sevenwonders.client.SevenWondersSession -import org.luxons.sevenwonders.ui.redux.FatalError -import org.luxons.sevenwonders.ui.redux.RequestChooseName -import org.luxons.sevenwonders.ui.redux.SetCurrentPlayerAction -import org.luxons.sevenwonders.ui.redux.SwState +import org.luxons.sevenwonders.ui.redux.* +import org.luxons.sevenwonders.ui.router.Navigate import org.luxons.sevenwonders.ui.router.Route import org.luxons.sevenwonders.ui.router.routerSaga import redux.RAction @@ -32,6 +30,9 @@ suspend fun SwSagaContext.rootSaga() = try { serverErrorSaga(session) } + launchApiActionHandlersIn(this, session) + launchNavigationHandlers(this, session) + val player = session.chooseName(action.playerName, null) dispatch(SetCurrentPlayerAction(player)) @@ -75,8 +76,41 @@ private suspend fun serverErrorSaga(session: SevenWondersSession) { } } -private suspend fun SwSagaContext.homeSaga(session: SevenWondersSession) { - val action = next() - val player = session.chooseName(action.playerName) - dispatch(SetCurrentPlayerAction(player)) +private fun SwSagaContext.launchApiActionHandlersIn(scope: CoroutineScope, session: SevenWondersSession) { + + scope.launchOnEach { session.createGame(it.gameName) } + scope.launchOnEach { session.joinGame(it.gameId) } + + scope.launchOnEach { session.addBot(it.botDisplayName) } + scope.launchOnEach { session.reorderPlayers(it.orderedPlayers) } + scope.launchOnEach { session.reassignWonders(it.wonders) } + // mapAction { updateSettings(it.settings) } + scope.launchOnEach { session.startGame() } + + scope.launchOnEach { session.sayReady() } + scope.launchOnEach { session.prepareMove(it.move) } + scope.launchOnEach { session.unprepareMove() } +} + +private fun SwSagaContext.launchNavigationHandlers(scope: CoroutineScope, session: SevenWondersSession) { + + // FIXME map this actions like others and await server event instead + scope.launchOnEach { + session.leaveLobby() + dispatch(Navigate(Route.GAME_BROWSER)) + } + + // FIXME map this actions like others and await server event instead + scope.launchOnEach { + session.leaveGame() + dispatch(Navigate(Route.GAME_BROWSER)) + } + + scope.launch { + session.watchLobbyJoined().collect { lobby -> + dispatch(EnterLobbyAction(lobby)) + dispatch(Navigate(Route.LOBBY)) + } + } } + 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 3acf68e8..0a2d7fa5 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 @@ -119,6 +119,14 @@ class SagaContext( } } + /** + * Launches a coroutine in the receiver scope that executes [handle] on every action dispatched of the type [T]. + * The returned [Job] can be used to cancel that coroutine (just like a regular [launch]) + */ + inline fun CoroutineScope.launchOnEach( + crossinline handle: suspend SagaContext.(T) -> Unit, + ): Job = launch { onEach(handle) } + /** * Suspends until the next action matching the given [predicate] is dispatched, and returns that action. */ -- cgit