diff options
author | joffrey-bion <joffrey.bion@gmail.com> | 2020-12-10 23:06:00 +0100 |
---|---|---|
committer | joffrey-bion <joffrey.bion@gmail.com> | 2020-12-11 02:16:49 +0100 |
commit | eef32bd9307a3a9f1ee3b532db2fb1f7cf37927a (patch) | |
tree | f139487db3866a687e699a01f3ed4baa5e44faf2 /sw-ui/src/main | |
parent | Decouple some sagas from routes (diff) | |
download | seven-wonders-eef32bd9307a3a9f1ee3b532db2fb1f7cf37927a.tar.gz seven-wonders-eef32bd9307a3a9f1ee3b532db2fb1f7cf37927a.tar.bz2 seven-wonders-eef32bd9307a3a9f1ee3b532db2fb1f7cf37927a.zip |
Allow owner to leave/disband the game
Resolves:
https://github.com/joffrey-bion/seven-wonders/issues/51
Diffstat (limited to 'sw-ui/src/main')
5 files changed, 45 insertions, 29 deletions
diff --git a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/components/lobby/Lobby.kt b/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/components/lobby/Lobby.kt index 3fa85b0a..478f4f4f 100644 --- a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/components/lobby/Lobby.kt +++ b/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/components/lobby/Lobby.kt @@ -1,33 +1,21 @@ package org.luxons.sevenwonders.ui.components.lobby -import com.palantir.blueprintjs.Elevation -import com.palantir.blueprintjs.Intent -import com.palantir.blueprintjs.bpButton -import com.palantir.blueprintjs.bpButtonGroup -import com.palantir.blueprintjs.bpCard -import com.palantir.blueprintjs.bpDivider -import com.palantir.blueprintjs.bpNonIdealState +import com.palantir.blueprintjs.* import kotlinx.css.* -import kotlinx.css.properties.* +import kotlinx.css.properties.transform +import kotlinx.css.properties.translate import org.luxons.sevenwonders.model.api.LobbyDTO import org.luxons.sevenwonders.model.api.PlayerDTO -import org.luxons.sevenwonders.model.wonders.AssignedWonder -import org.luxons.sevenwonders.model.wonders.WonderSide -import org.luxons.sevenwonders.model.wonders.deal -import org.luxons.sevenwonders.model.wonders.withRandomSide -import org.luxons.sevenwonders.model.wonders.withSide +import org.luxons.sevenwonders.model.wonders.* import org.luxons.sevenwonders.ui.components.GlobalStyles -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.connectStateAndDispatch +import org.luxons.sevenwonders.ui.redux.* import react.RBuilder import react.RComponent import react.RProps import react.RState -import react.dom.* +import react.dom.h2 +import react.dom.h3 +import react.dom.h4 import styled.css import styled.styledDiv import styled.styledH2 @@ -43,6 +31,7 @@ interface LobbyDispatchProps : RProps { var startGame: () -> Unit var addBot: (displayName: String) -> Unit var leaveLobby: () -> Unit + var disbandLobby: () -> Unit var reorderPlayers: (orderedPlayers: List<String>) -> Unit var reassignWonders: (wonders: List<AssignedWonder>) -> Unit } @@ -84,6 +73,8 @@ class LobbyPresenter(props: LobbyProps) : RComponent<LobbyProps, RState>(props) bpButtonGroup { startButton(currentGame, currentPlayer) addBotButton(currentGame) + leaveButton() + disbandButton() } } else { leaveButton() @@ -217,7 +208,7 @@ class LobbyPresenter(props: LobbyProps) : RComponent<LobbyProps, RState>(props) private fun RBuilder.leaveButton() { bpButton( large = true, - intent = Intent.DANGER, + intent = Intent.WARNING, icon = "arrow-left", title = "Leave the lobby and go back to the game browser", onClick = { props.leaveLobby() }, @@ -225,6 +216,18 @@ class LobbyPresenter(props: LobbyProps) : RComponent<LobbyProps, RState>(props) +"LEAVE" } } + + private fun RBuilder.disbandButton() { + bpButton( + large = true, + intent = Intent.DANGER, + icon = "delete", + title = "Disband the group and go back to the game browser", + onClick = { props.disbandLobby() }, + ) { + +"DISBAND" + } + } } fun RBuilder.lobby() = lobby {} @@ -239,6 +242,7 @@ private val lobby = connectStateAndDispatch<LobbyStateProps, LobbyDispatchProps, startGame = { dispatch(RequestStartGame()) } addBot = { name -> dispatch(RequestAddBot(name)) } leaveLobby = { dispatch(RequestLeaveLobby()) } + disbandLobby = { dispatch(RequestDisbandLobby()) } reorderPlayers = { orderedPlayers -> dispatch(RequestReorderPlayers(orderedPlayers)) } reassignWonders = { wonders -> dispatch(RequestReassignWonders(wonders)) } }, 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 cd9443df..c5ffafa0 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 @@ -3,6 +3,7 @@ package org.luxons.sevenwonders.ui.redux import org.luxons.sevenwonders.model.PlayerMove import org.luxons.sevenwonders.model.PlayerTurnInfo 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.cards.PreparedCard import redux.RAction @@ -11,12 +12,14 @@ data class FatalError(val message: String) : RAction data class SetCurrentPlayerAction(val player: ConnectedPlayer) : RAction -data class UpdateGameListAction(val games: List<LobbyDTO>) : RAction +data class UpdateGameListAction(val event: GameListEvent) : RAction data class UpdateLobbyAction(val lobby: LobbyDTO) : RAction data class EnterLobbyAction(val lobby: LobbyDTO) : RAction +data class LeaveLobbyAction(val lobbyId: Long) : RAction + data class EnterGameAction(val lobby: LobbyDTO, val turnInfo: PlayerTurnInfo) : RAction data class TurnInfoEvent(val turnInfo: PlayerTurnInfo) : RAction diff --git a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/ApiActions.kt b/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/ApiActions.kt index d259da81..87bacf62 100644 --- a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/ApiActions.kt +++ b/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/ApiActions.kt @@ -23,6 +23,8 @@ class RequestStartGame : RAction class RequestLeaveLobby : RAction +class RequestDisbandLobby : RAction + class RequestLeaveGame : RAction class RequestSayReady : 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 7789cabb..b1d730a0 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 @@ -4,9 +4,9 @@ import org.luxons.sevenwonders.model.MoveType import org.luxons.sevenwonders.model.PlayerMove import org.luxons.sevenwonders.model.PlayerTurnInfo 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.State import org.luxons.sevenwonders.model.cards.CardBack import org.luxons.sevenwonders.model.cards.HandCard import org.luxons.sevenwonders.model.resources.ResourceTransactionOptions @@ -53,7 +53,11 @@ fun rootReducer(state: SwState, action: RAction): SwState = state.copy( ) private fun gamesReducer(games: Map<Long, LobbyDTO>, action: RAction): Map<Long, LobbyDTO> = when (action) { - is UpdateGameListAction -> (games + action.games.associateBy { it.id }).filterValues { it.state != State.FINISHED } + is UpdateGameListAction -> when (action.event) { + is GameListEvent.ReplaceList -> action.event.lobbies.associateBy { it.id } + is GameListEvent.CreateOrUpdate -> games + (action.event.lobby.id to action.event.lobby) + is GameListEvent.Delete -> games - action.event.lobbyId + } else -> games } @@ -64,6 +68,7 @@ private fun currentPlayerReducer(currentPlayer: ConnectedPlayer?, action: RActio private fun currentLobbyReducer(currentLobby: LobbyDTO?, action: RAction): LobbyDTO? = when (action) { is EnterLobbyAction -> action.lobby + is LeaveLobbyAction -> null 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 }) 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 c2d26e0f..c9f73111 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 @@ -80,6 +80,8 @@ private fun SwSagaContext.launchApiActionHandlersIn(scope: CoroutineScope, sessi scope.launchOnEach<RequestCreateGame> { session.createGame(it.gameName) } scope.launchOnEach<RequestJoinGame> { session.joinGame(it.gameId) } + scope.launchOnEach<RequestLeaveLobby> { session.leaveLobby() } + scope.launchOnEach<RequestDisbandLobby> { session.disbandLobby() } scope.launchOnEach<RequestAddBot> { session.addBot(it.botDisplayName) } scope.launchOnEach<RequestReorderPlayers> { session.reorderPlayers(it.orderedPlayers) } @@ -94,10 +96,11 @@ private fun SwSagaContext.launchApiActionHandlersIn(scope: CoroutineScope, sessi private fun SwSagaContext.launchNavigationHandlers(scope: CoroutineScope, session: SevenWondersSession) { - // FIXME map this actions like others and await server event instead - scope.launchOnEach<RequestLeaveLobby> { - session.leaveLobby() - dispatch(Navigate(Route.GAME_BROWSER)) + scope.launch { + session.watchLobbyLeft().collect { leftLobbyId -> + dispatch(LeaveLobbyAction(leftLobbyId)) + dispatch(Navigate(Route.GAME_BROWSER)) + } } // FIXME map this actions like others and await server event instead @@ -113,4 +116,3 @@ private fun SwSagaContext.launchNavigationHandlers(scope: CoroutineScope, sessio } } } - |