From 7aa5a9f48d28853f0236d6de0016d0122c86e7ba Mon Sep 17 00:00:00 2001 From: Joffrey Bion Date: Mon, 23 Mar 2020 21:43:03 +0100 Subject: Reorganize reducers to fix game start button --- .../ui/components/gameBrowser/PlayerInfo.kt | 2 +- .../sevenwonders/ui/components/lobby/Lobby.kt | 12 +++--- .../org/luxons/sevenwonders/ui/redux/Reducers.kt | 45 +++++++++++++++++----- 3 files changed, 43 insertions(+), 16 deletions(-) diff --git a/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/components/gameBrowser/PlayerInfo.kt b/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/components/gameBrowser/PlayerInfo.kt index eefaec30..1cb52b50 100644 --- a/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/components/gameBrowser/PlayerInfo.kt +++ b/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/components/gameBrowser/PlayerInfo.kt @@ -31,6 +31,6 @@ fun RBuilder.playerInfo() = playerInfo {} private val playerInfo = connectState( clazz = PlayerInfoPresenter::class, mapStateToProps = { state, _ -> - currentPlayer = state.player + currentPlayer = state.currentPlayer } ) diff --git a/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/components/lobby/Lobby.kt b/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/components/lobby/Lobby.kt index de9d1454..959085f3 100644 --- a/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/components/lobby/Lobby.kt +++ b/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/components/lobby/Lobby.kt @@ -15,7 +15,6 @@ import react.dom.* interface LobbyStateProps : RProps { var currentGame: LobbyDTO? var currentPlayer: PlayerDTO? - var players: List } interface LobbyDispatchProps : RProps { @@ -35,7 +34,7 @@ class LobbyPresenter(props: LobbyProps) : RComponent(props) } div { h2 { +"${currentGame.name} — Lobby" } - radialPlayerList(props.players) + radialPlayerList(currentGame.players) if (currentPlayer.isGameOwner) { bpButton( large = true, @@ -43,7 +42,9 @@ class LobbyPresenter(props: LobbyProps) : RComponent(props) icon = "play", disabled = !currentGame.canBeStarted, onClick = { props.startGame() } - ) + ) { + + "START" + } } } } @@ -54,9 +55,8 @@ fun RBuilder.lobby() = lobby {} private val lobby = connect( clazz = LobbyPresenter::class, mapStateToProps = { state, _ -> - currentGame = state.lobby - currentPlayer = state.player - players = state.lobby?.players ?: emptyList() + currentGame = state.currentLobby + currentPlayer = state.currentPlayer }, mapDispatchToProps = { dispatch, _ -> startGame = { dispatch(RequestStartGameAction()) } 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 21fc7480..69941c91 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 @@ -5,15 +5,42 @@ import org.luxons.sevenwonders.model.api.PlayerDTO import redux.RAction data class SwState( - val player: PlayerDTO? = null, - val lobby: LobbyDTO? = null, - val games: List = emptyList() + val currentPlayerUsername: String? = null, + val currentLobbyId: Long? = null, + val playersByUsername: Map = emptyMap(), + val gamesById: Map = emptyMap() +) { + val games: List = 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( + currentPlayerUsername = currentPlayerReducer(state.currentPlayerUsername, action), + currentLobbyId = currentLobbyReducer(state.currentLobbyId, action), + gamesById = gamesReducer(state.gamesById, action), + playersByUsername = playersReducer(state.playersByUsername, action) ) -fun rootReducer(state: SwState, action: RAction) = when (action) { - is SetCurrentPlayerAction -> state.copy(player = action.player) - is UpdateGameListAction -> state.copy(games = action.games) - is UpdateLobbyAction -> state.copy(lobby = action.lobby) - is UpdatePlayers -> TODO() - else -> state +private fun currentPlayerReducer(username: String?, action: RAction): String? = when (action) { + is SetCurrentPlayerAction -> action.player.username + else -> username +} + +private fun currentLobbyReducer(currentLobby: Long?, action: RAction): Long? = when (action) { + is EnterLobbyAction -> action.gameId + else -> currentLobby +} + +private fun gamesReducer(games: Map, action: RAction): Map = when (action) { + is UpdateGameListAction -> action.games.associateBy { it.id } // replaces because should remove deleted games + is UpdateLobbyAction -> games + (action.lobby.id to action.lobby) + else -> games +} + +private fun playersReducer(playersByUsername: Map, action: RAction): Map = 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) + else -> playersByUsername } -- cgit