From b454850bbb6db19cc387b52dce5c1dfd1aecc21b Mon Sep 17 00:00:00 2001 From: Joffrey Bion Date: Wed, 8 Apr 2020 00:53:46 +0200 Subject: Add "leave lobby" button Resolves: https://github.com/joffrey-bion/seven-wonders/issues/8 --- .../sevenwonders/ui/components/lobby/Lobby.kt | 43 ++++++++++++++++------ .../org/luxons/sevenwonders/ui/redux/ApiActions.kt | 2 + .../sevenwonders/ui/redux/sagas/LobbySagas.kt | 26 ++++++++++--- 3 files changed, 55 insertions(+), 16 deletions(-) (limited to 'sw-ui/src') 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 5b13d8b1..d015e2a2 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 @@ -4,6 +4,7 @@ import com.palantir.blueprintjs.Intent import com.palantir.blueprintjs.bpButton import org.luxons.sevenwonders.model.api.LobbyDTO import org.luxons.sevenwonders.model.api.PlayerDTO +import org.luxons.sevenwonders.ui.redux.RequestLeaveLobby import org.luxons.sevenwonders.ui.redux.RequestStartGame import org.luxons.sevenwonders.ui.redux.connectStateAndDispatch import react.RBuilder @@ -19,6 +20,7 @@ interface LobbyStateProps : RProps { interface LobbyDispatchProps : RProps { var startGame: () -> Unit + var leaveLobby: () -> Unit } interface LobbyProps : LobbyDispatchProps, LobbyStateProps @@ -36,20 +38,38 @@ class LobbyPresenter(props: LobbyProps) : RComponent(props) h2 { +"${currentGame.name} — Lobby" } radialPlayerList(currentGame.players, currentPlayer) if (currentPlayer.isGameOwner) { - val startability = currentGame.startability(currentPlayer.username) - bpButton( - large = true, - intent = Intent.PRIMARY, - icon = "play", - title = startability.tooltip, - disabled = !startability.canDo, - onClick = { props.startGame() } - ) { - + "START" - } + startButton(currentGame, currentPlayer) + } else { + leaveButton() } } } + + private fun RBuilder.startButton(currentGame: LobbyDTO, currentPlayer: PlayerDTO) { + val startability = currentGame.startability(currentPlayer.username) + bpButton( + large = true, + intent = Intent.PRIMARY, + icon = "play", + title = startability.tooltip, + disabled = !startability.canDo, + onClick = { props.startGame() } + ) { + +"START" + } + } + + private fun RBuilder.leaveButton() { + bpButton( + large = true, + intent = Intent.DANGER, + icon = "arrow-left", + title = "Leave the lobby and go back to the game browser", + onClick = { props.leaveLobby() } + ) { + +"LEAVE" + } + } } fun RBuilder.lobby() = lobby {} @@ -62,5 +82,6 @@ private val lobby = connectStateAndDispatch startGame = { dispatch(RequestStartGame()) } + leaveLobby = { dispatch(RequestLeaveLobby()) } } ) 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 9e2593d4..eef77585 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 @@ -16,6 +16,8 @@ data class RequestUpdateSettings(val settings: CustomizableSettings) : RAction class RequestStartGame : RAction +class RequestLeaveLobby : RAction + class RequestSayReady : RAction data class RequestPrepareMove(val move: PlayerMove) : RAction diff --git a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/LobbySagas.kt b/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/LobbySagas.kt index 678276dc..c3996af7 100644 --- a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/LobbySagas.kt +++ b/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/LobbySagas.kt @@ -6,10 +6,12 @@ import org.hildan.krossbow.stomp.StompSubscription import org.luxons.sevenwonders.client.SevenWondersSession import org.luxons.sevenwonders.model.api.LobbyDTO import org.luxons.sevenwonders.ui.redux.EnterGameAction +import org.luxons.sevenwonders.ui.redux.RequestLeaveLobby import org.luxons.sevenwonders.ui.redux.RequestStartGame import org.luxons.sevenwonders.ui.redux.UpdateLobbyAction import org.luxons.sevenwonders.ui.router.Navigate import org.luxons.sevenwonders.ui.router.Route +import org.luxons.sevenwonders.ui.utils.awaitFirst suspend fun SwSagaContext.lobbySaga(session: SevenWondersSession) { val lobby = getState().currentLobby ?: error("Lobby saga run without a current lobby") @@ -18,11 +20,20 @@ suspend fun SwSagaContext.lobbySaga(session: SevenWondersSession) { launch { watchLobbyUpdates(lobbyUpdatesSubscription) } val startGameJob = launch { awaitStartGame(session) } - awaitGameStart(session, lobby.id) - - lobbyUpdatesSubscription.unsubscribe() - startGameJob.cancel() - dispatch(Navigate(Route.GAME)) + awaitFirst( + { + awaitLeaveLobby(session) + lobbyUpdatesSubscription.unsubscribe() + startGameJob.cancel() + dispatch(Navigate(Route.GAME_BROWSER)) + }, + { + awaitGameStart(session, lobby.id) + lobbyUpdatesSubscription.unsubscribe() + startGameJob.cancel() + dispatch(Navigate(Route.GAME)) + } + ) } } @@ -40,3 +51,8 @@ private suspend fun SwSagaContext.awaitStartGame(session: SevenWondersSession) { next() session.startGame() } + +private suspend fun SwSagaContext.awaitLeaveLobby(session: SevenWondersSession) { + next() + session.leaveLobby() +} -- cgit