summaryrefslogtreecommitdiff
path: root/sw-ui-kt
diff options
context:
space:
mode:
authorJoffrey Bion <joffrey.bion@booking.com>2020-03-29 11:49:31 +0200
committerJoffrey Bion <joffrey.bion@booking.com>2020-03-29 11:49:42 +0200
commit2757073c66643fe1a34c8b6ca16ba004ddb29da6 (patch)
tree3e424043742dc4f02d18a8b80a958bfa00b0bcad /sw-ui-kt
parentImprove state updates for turn info (diff)
downloadseven-wonders-2757073c66643fe1a34c8b6ca16ba004ddb29da6.tar.gz
seven-wonders-2757073c66643fe1a34c8b6ca16ba004ddb29da6.tar.bz2
seven-wonders-2757073c66643fe1a34c8b6ca16ba004ddb29da6.zip
Simplify redux state
Diffstat (limited to 'sw-ui-kt')
-rw-r--r--sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/components/gameBrowser/GameList.kt1
-rw-r--r--sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Actions.kt2
-rw-r--r--sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Reducers.kt53
-rw-r--r--sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/LobbySagas.kt4
4 files changed, 22 insertions, 38 deletions
diff --git a/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/components/gameBrowser/GameList.kt b/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/components/gameBrowser/GameList.kt
index e2e70869..19cd961f 100644
--- a/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/components/gameBrowser/GameList.kt
+++ b/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/components/gameBrowser/GameList.kt
@@ -82,6 +82,7 @@ private fun RBuilder.gameStatus(state: State) {
val intent = when(state) {
State.LOBBY -> Intent.SUCCESS
State.PLAYING -> Intent.WARNING
+ State.FINISHED -> Intent.DANGER
}
bpTag(minimal = true, intent = intent) {
+state.toString()
diff --git a/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Actions.kt b/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Actions.kt
index 37238e0a..63fc26eb 100644
--- a/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Actions.kt
+++ b/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Actions.kt
@@ -14,8 +14,6 @@ data class UpdateLobbyAction(val lobby: LobbyDTO): RAction
data class EnterLobbyAction(val lobby: LobbyDTO): RAction
-data class UpdatePlayers(val players: Map<String, PlayerDTO>): RAction
-
data class EnterGameAction(val gameId: Long): RAction
data class TurnInfoEvent(val turnInfo: PlayerTurnInfo): RAction
diff --git a/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Reducers.kt b/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Reducers.kt
index 507a3941..f2a20cef 100644
--- a/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Reducers.kt
+++ b/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Reducers.kt
@@ -3,61 +3,46 @@ package org.luxons.sevenwonders.ui.redux
import org.luxons.sevenwonders.model.PlayerTurnInfo
import org.luxons.sevenwonders.model.api.LobbyDTO
import org.luxons.sevenwonders.model.api.PlayerDTO
+import org.luxons.sevenwonders.model.api.State
import redux.RAction
data class SwState(
- val playersByUsername: Map<String, PlayerDTO> = emptyMap(),
+ val currentPlayer: PlayerDTO? = null,
+ // they must be by ID to support updates to a sublist
val gamesById: Map<Long, LobbyDTO> = emptyMap(),
val currentPlayerUsername: String? = null,
- val currentLobbyId: Long? = null,
+ val currentLobby: LobbyDTO? = null,
val currentTurnInfo: PlayerTurnInfo? = null
) {
val games: List<LobbyDTO> = gamesById.values.toList()
- val currentLobby: LobbyDTO? = currentLobbyId?.let { gamesById[it] }
- val currentPlayer: PlayerDTO? = currentPlayerUsername?.let { playersByUsername[it] }
}
fun rootReducer(state: SwState, action: RAction): SwState = state.copy(
- playersByUsername = playersReducer(state.playersByUsername, action),
gamesById = gamesReducer(state.gamesById, action),
- currentPlayerUsername = currentPlayerReducer(state.currentPlayerUsername, action),
- currentLobbyId = currentLobbyReducer(state.currentLobbyId, action),
+ currentPlayer = currentPlayerReducer(state.currentPlayer, action),
+ currentLobby = currentLobbyReducer(state.currentLobby, action),
currentTurnInfo = currentTurnInfoReducer(state.currentTurnInfo, action)
)
-private fun playersReducer(playersByUsername: Map<String, PlayerDTO>, action: RAction): Map<String, PlayerDTO> = when (action) {
- is UpdatePlayers -> playersByUsername + action.players
- is UpdateLobbyAction -> playersByUsername + action.lobby.players.associateBy { it.username }
- is SetCurrentPlayerAction -> playersByUsername + (action.player.username to action.player)
- is PlayerReadyEvent -> playersByUsername + (action.username to playersByUsername.getValue(action.username)
- .copy(isReady = true))
- else -> playersByUsername
-}
-
private fun gamesReducer(games: Map<Long, LobbyDTO>, action: RAction): Map<Long, LobbyDTO> = when (action) {
- is UpdateGameListAction -> action.games.associateBy { it.id } // replaces because should remove deleted games
- is EnterLobbyAction -> games + (action.lobby.id to action.lobby)
- is UpdateLobbyAction -> games + (action.lobby.id to action.lobby)
- is PlayerReadyEvent -> games.mapValues { (_, l) ->
- if (l.players.any { it.username == action.username }) {
- l.copy(players = l.players.map { p ->
- if (p.username == action.username) p.copy(isReady = true) else p
- })
- } else {
- l
- }
- }
+ is UpdateGameListAction -> (games + action.games.associateBy { it.id }).filterValues { it.state != State.FINISHED }
else -> games
}
-private fun currentPlayerReducer(username: String?, action: RAction): String? = when (action) {
- is SetCurrentPlayerAction -> action.player.username
- else -> username
+private fun currentPlayerReducer(currentPlayer: PlayerDTO?, action: RAction): PlayerDTO? = when (action) {
+ is SetCurrentPlayerAction -> action.player
+ else -> currentPlayer
}
-private fun currentLobbyReducer(currentLobbyId: Long?, action: RAction): Long? = when (action) {
- is EnterLobbyAction -> action.lobby.id
- else -> currentLobbyId
+private fun currentLobbyReducer(currentLobby: LobbyDTO?, action: RAction): LobbyDTO? = when (action) {
+ is EnterLobbyAction -> action.lobby
+ is UpdateLobbyAction -> action.lobby
+ is PlayerReadyEvent -> currentLobby?.let { l ->
+ l.copy(players = l.players.map { p ->
+ if (p.username == action.username) p.copy(isReady = true) else p
+ })
+ }
+ else -> currentLobby
}
private fun currentTurnInfoReducer(currentTurnInfo: PlayerTurnInfo?, action: RAction): PlayerTurnInfo? = when (action) {
diff --git a/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/LobbySagas.kt b/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/LobbySagas.kt
index a51648c8..39c29722 100644
--- a/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/LobbySagas.kt
+++ b/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/LobbySagas.kt
@@ -12,13 +12,13 @@ import org.luxons.sevenwonders.ui.router.Navigate
import org.luxons.sevenwonders.ui.router.Route
suspend fun SwSagaContext.lobbySaga(session: SevenWondersSession) {
- val lobbyId = getState().currentLobbyId ?: error("Lobby saga run without a current lobby")
+ val lobby = getState().currentLobby ?: error("Lobby saga run without a current lobby")
coroutineScope {
val lobbyUpdatesSubscription = session.watchLobbyUpdates()
launch { watchLobbyUpdates(lobbyUpdatesSubscription) }
val startGameJob = launch { awaitStartGame(session) }
- awaitGameStart(session, lobbyId)
+ awaitGameStart(session, lobby.id)
lobbyUpdatesSubscription.unsubscribe()
startGameJob.cancel()
bgstack15