summaryrefslogtreecommitdiff
path: root/sw-ui/src/main/kotlin/org/luxons
diff options
context:
space:
mode:
authorJoffrey Bion <joffrey.bion@booking.com>2020-04-08 00:53:46 +0200
committerJoffrey Bion <joffrey.bion@booking.com>2020-04-08 00:54:08 +0200
commitb454850bbb6db19cc387b52dce5c1dfd1aecc21b (patch)
tree64d0dc97c0801711df0094cd04695bc3332bc7c1 /sw-ui/src/main/kotlin/org/luxons
parentFix tests (diff)
downloadseven-wonders-b454850bbb6db19cc387b52dce5c1dfd1aecc21b.tar.gz
seven-wonders-b454850bbb6db19cc387b52dce5c1dfd1aecc21b.tar.bz2
seven-wonders-b454850bbb6db19cc387b52dce5c1dfd1aecc21b.zip
Add "leave lobby" button
Resolves: https://github.com/joffrey-bion/seven-wonders/issues/8
Diffstat (limited to 'sw-ui/src/main/kotlin/org/luxons')
-rw-r--r--sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/components/lobby/Lobby.kt43
-rw-r--r--sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/ApiActions.kt2
-rw-r--r--sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/LobbySagas.kt26
3 files changed, 55 insertions, 16 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 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<LobbyProps, RState>(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<LobbyStateProps, LobbyDispatchProps,
},
mapDispatchToProps = { dispatch, _ ->
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<RequestStartGame>()
session.startGame()
}
+
+private suspend fun SwSagaContext.awaitLeaveLobby(session: SevenWondersSession) {
+ next<RequestLeaveLobby>()
+ session.leaveLobby()
+}
bgstack15