diff options
author | Joffrey Bion <joffrey.bion@booking.com> | 2020-03-23 21:43:03 +0100 |
---|---|---|
committer | Joffrey Bion <joffrey.bion@booking.com> | 2020-03-23 22:00:52 +0100 |
commit | 7aa5a9f48d28853f0236d6de0016d0122c86e7ba (patch) | |
tree | c545712c690076675bbd3f6a58cfa86afe101a7a /sw-ui-kt/src/main/kotlin/org | |
parent | Remove hardcoded min players in Lobby component (diff) | |
download | seven-wonders-7aa5a9f48d28853f0236d6de0016d0122c86e7ba.tar.gz seven-wonders-7aa5a9f48d28853f0236d6de0016d0122c86e7ba.tar.bz2 seven-wonders-7aa5a9f48d28853f0236d6de0016d0122c86e7ba.zip |
Reorganize reducers to fix game start button
Diffstat (limited to 'sw-ui-kt/src/main/kotlin/org')
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<PlayerDTO> } interface LobbyDispatchProps : RProps { @@ -35,7 +34,7 @@ class LobbyPresenter(props: LobbyProps) : RComponent<LobbyProps, RState>(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<LobbyProps, RState>(props) icon = "play", disabled = !currentGame.canBeStarted, onClick = { props.startGame() } - ) + ) { + + "START" + } } } } @@ -54,9 +55,8 @@ fun RBuilder.lobby() = lobby {} private val lobby = connect<LobbyStateProps, LobbyDispatchProps, LobbyProps>( 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<LobbyDTO> = emptyList() + val currentPlayerUsername: String? = null, + val currentLobbyId: Long? = null, + val playersByUsername: Map<String, PlayerDTO> = emptyMap(), + val gamesById: Map<Long, LobbyDTO> = emptyMap() +) { + 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( + 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<Long, LobbyDTO>, action: RAction): Map<Long, LobbyDTO> = 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<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) + else -> playersByUsername } |