summaryrefslogtreecommitdiff
path: root/sw-ui/src/main/kotlin/org
diff options
context:
space:
mode:
authorJoffrey Bion <joffrey.bion@gmail.com>2021-03-09 00:06:23 +0100
committerJoffrey Bion <joffrey.bion@gmail.com>2021-03-09 00:06:23 +0100
commit10366ca0240bc6bb5d66cd06a0c18373c2c8e875 (patch)
tree4b491912b1e28d76a41f40d8d46bb3f328596abf /sw-ui/src/main/kotlin/org
parentUpgrade ktlint (diff)
downloadseven-wonders-10366ca0240bc6bb5d66cd06a0c18373c2c8e875.tar.gz
seven-wonders-10366ca0240bc6bb5d66cd06a0c18373c2c8e875.tar.bz2
seven-wonders-10366ca0240bc6bb5d66cd06a0c18373c2c8e875.zip
Unify game events
Diffstat (limited to 'sw-ui/src/main/kotlin/org')
-rw-r--r--sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Actions.kt2
-rw-r--r--sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Reducers.kt2
-rw-r--r--sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/RouteBasedSagas.kt40
-rw-r--r--sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/Sagas.kt58
4 files changed, 46 insertions, 56 deletions
diff --git a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Actions.kt b/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Actions.kt
index 6ef52ec6..b0c56a79 100644
--- a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Actions.kt
+++ b/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Actions.kt
@@ -4,8 +4,8 @@ import org.luxons.sevenwonders.model.PlayerMove
import org.luxons.sevenwonders.model.PlayerTurnInfo
import org.luxons.sevenwonders.model.TurnAction
import org.luxons.sevenwonders.model.api.ConnectedPlayer
-import org.luxons.sevenwonders.model.api.GameListEvent
import org.luxons.sevenwonders.model.api.LobbyDTO
+import org.luxons.sevenwonders.model.api.events.GameListEvent
import org.luxons.sevenwonders.model.cards.PreparedCard
import redux.RAction
diff --git a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Reducers.kt b/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Reducers.kt
index 3fb72904..e79b063e 100644
--- a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Reducers.kt
+++ b/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Reducers.kt
@@ -2,9 +2,9 @@ package org.luxons.sevenwonders.ui.redux
import org.luxons.sevenwonders.client.GameState
import org.luxons.sevenwonders.model.api.ConnectedPlayer
-import org.luxons.sevenwonders.model.api.GameListEvent
import org.luxons.sevenwonders.model.api.LobbyDTO
import org.luxons.sevenwonders.model.api.PlayerDTO
+import org.luxons.sevenwonders.model.api.events.GameListEvent
import redux.RAction
data class SwState(
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 b2fad7e1..a014a318 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
@@ -1,20 +1,11 @@
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.model.api.events.GameEvent
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<RequestChooseName>()
- val player = session.chooseName(action.playerName)
- dispatch(SetCurrentPlayerAction(player))
- dispatch(Navigate(Route.GAME_BROWSER))
-}
-
suspend fun SwSagaContext.gameBrowserSaga(session: SevenWondersSession) {
// browser navigation could have brought us here: we should leave the game/lobby
ensureNoCurrentGameNorLobby(session)
@@ -23,42 +14,29 @@ suspend fun SwSagaContext.gameBrowserSaga(session: SevenWondersSession) {
private suspend fun SwSagaContext.ensureNoCurrentGameNorLobby(session: SevenWondersSession) {
if (reduxState.gameState != null) {
- console.warn("User left a game via browser navigation, cleaning up...")
+ console.warn("User left a game via browser navigation, telling the server...")
session.leaveGame()
} else if (reduxState.currentLobby != null) {
- console.warn("User left the lobby via browser navigation, cleaning up...")
+ console.warn("User left the lobby via browser navigation, telling the server...")
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...")
+ } else if (reduxState.currentLobby == null) {
+ console.warn("User went to lobby page via browser navigation, redirecting to game browser...")
dispatch(Navigate(Route.GAME_BROWSER))
- return
}
- session.watchLobbyUpdates().map { UpdateLobbyAction(it) }.dispatchAll()
}
suspend fun SwSagaContext.gameSaga(session: SevenWondersSession) {
- val game = reduxState.gameState ?: error("Game saga run without a current game")
- coroutineScope {
- session.watchGameEvents(game.gameId).map {
- when (it) {
- is GameEvent.NewTurnStarted -> TurnInfoEvent(it.turnInfo)
- is GameEvent.MovePrepared -> PreparedMoveEvent(it.move)
- is GameEvent.CardPrepared -> PreparedCardEvent(it.preparedCard)
- is GameEvent.PlayerIsReady -> PlayerReadyEvent(it.username)
- }
- }.dispatchAllIn(this)
- session.sayReady()
+ if (reduxState.gameState == null) {
+ // TODO properly redirect somewhere
+ error("Game saga run without a current game")
}
- console.log("End of game saga")
+ // notifies the server that the client is ready to receive the first hand
+ session.sayReady()
}
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 13407247..ba3949cc 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
@@ -6,8 +6,8 @@ import kotlinx.coroutines.flow.collect
import org.hildan.krossbow.stomp.ConnectionException
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.client.*
+import org.luxons.sevenwonders.model.api.events.GameEvent
import org.luxons.sevenwonders.ui.redux.*
import org.luxons.sevenwonders.ui.router.Navigate
import org.luxons.sevenwonders.ui.router.Route
@@ -34,12 +34,12 @@ suspend fun SwSagaContext.rootSaga() = try {
launchApiActionHandlersIn(this, session)
launchApiEventHandlersIn(this, session)
- val player = session.chooseName(action.playerName)
+ val player = session.chooseNameAndAwait(action.playerName)
dispatch(SetCurrentPlayerAction(player))
routerSaga(Route.GAME_BROWSER) {
when (it) {
- Route.HOME -> homeSaga(session)
+ Route.HOME -> Unit
Route.LOBBY -> lobbySaga(session)
Route.GAME_BROWSER -> gameBrowserSaga(session)
Route.GAME -> gameSaga(session)
@@ -78,6 +78,7 @@ private suspend fun serverErrorSaga(session: SevenWondersSession) {
}
private fun SwSagaContext.launchApiActionHandlersIn(scope: CoroutineScope, session: SevenWondersSession) {
+ scope.launchOnEach<RequestChooseName> { session.chooseName(it.playerName) }
scope.launchOnEach<RequestCreateGame> { session.createGame(it.gameName) }
scope.launchOnEach<RequestJoinGame> { session.joinGame(it.gameId) }
@@ -96,26 +97,37 @@ private fun SwSagaContext.launchApiActionHandlersIn(scope: CoroutineScope, sessi
}
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 {
- dispatch(LeaveLobbyAction)
- dispatch(Navigate(Route.GAME_BROWSER))
- }
- }
-
- scope.launch {
- session.watchGameStarted().collect { turnInfo ->
- val currentLobby = reduxState.currentLobby ?: error("Received game started event without being in a lobby")
- dispatch(EnterGameAction(currentLobby, turnInfo))
- dispatch(Navigate(Route.GAME))
+ session.watchGameEvents().collect { event ->
+ when (event) {
+ is GameEvent.NameChosen -> {
+ dispatch(SetCurrentPlayerAction(event.player))
+ dispatch(Navigate(Route.GAME_BROWSER))
+ }
+ is GameEvent.LobbyJoined -> {
+ dispatch(EnterLobbyAction(event.lobby))
+ dispatch(Navigate(Route.LOBBY))
+ }
+ is GameEvent.LobbyUpdated -> {
+ dispatch(UpdateLobbyAction(event.lobby))
+ }
+ GameEvent.LobbyLeft -> {
+ dispatch(LeaveLobbyAction)
+ dispatch(Navigate(Route.GAME_BROWSER))
+ }
+ is GameEvent.GameStarted -> {
+ val currentLobby = reduxState.currentLobby ?: error("Received game started event without being in a lobby")
+ dispatch(EnterGameAction(currentLobby, event.turnInfo))
+ dispatch(Navigate(Route.GAME))
+ }
+ is GameEvent.NewTurnStarted -> dispatch(TurnInfoEvent(event.turnInfo))
+ is GameEvent.MovePrepared -> dispatch(PreparedMoveEvent(event.move))
+ is GameEvent.CardPrepared -> dispatch(PreparedCardEvent(event.preparedCard))
+ is GameEvent.PlayerIsReady -> dispatch(PlayerReadyEvent(event.username))
+ // Currently the move is already unprepared when launching the unprepare request
+ // TODO add a "unpreparing" state and only update redux when the move is successfully unprepared
+ GameEvent.MoveUnprepared -> {}
+ }
}
}
}
bgstack15