From 3858d1c771a97fba15a63eb5e18eb8eb114bcea7 Mon Sep 17 00:00:00 2001 From: Joffrey Bion Date: Wed, 1 Jul 2020 02:01:23 +0200 Subject: Add buttons to randomly reassign wonders and reorder players --- .../sevenwonders/ui/components/lobby/Lobby.kt | 44 ++++++++++++++++++++-- .../org/luxons/sevenwonders/ui/redux/ApiActions.kt | 3 ++ .../sevenwonders/ui/redux/sagas/LobbySagas.kt | 12 +++--- 3 files changed, 50 insertions(+), 9 deletions(-) (limited to 'sw-ui/src/main/kotlin') 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 9043537c..910fd71d 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 @@ -8,10 +8,9 @@ import kotlinx.css.properties.* import kotlinx.html.DIV import org.luxons.sevenwonders.model.api.LobbyDTO import org.luxons.sevenwonders.model.api.PlayerDTO -import org.luxons.sevenwonders.ui.redux.RequestAddBot -import org.luxons.sevenwonders.ui.redux.RequestLeaveLobby -import org.luxons.sevenwonders.ui.redux.RequestStartGame -import org.luxons.sevenwonders.ui.redux.connectStateAndDispatch +import org.luxons.sevenwonders.model.wonders.AssignedWonder +import org.luxons.sevenwonders.model.wonders.deal +import org.luxons.sevenwonders.ui.redux.* import react.RBuilder import react.RComponent import react.RProps @@ -31,6 +30,8 @@ interface LobbyDispatchProps : RProps { var startGame: () -> Unit var addBot: (displayName: String) -> Unit var leaveLobby: () -> Unit + var reorderPlayers: (orderedPlayers: List) -> Unit + var reassignWonders: (wonders: List) -> Unit } interface LobbyProps : LobbyDispatchProps, LobbyStateProps @@ -65,6 +66,8 @@ class LobbyPresenter(props: LobbyProps) : RComponent(props) if (currentPlayer.isGameOwner) { startButton(currentGame, currentPlayer) addBotButton(currentGame) + reorderPlayersButton(currentGame) + randomizeWondersButton(currentGame) } else { leaveButton() } @@ -104,6 +107,37 @@ class LobbyPresenter(props: LobbyProps) : RComponent(props) props.addBot(availableBotNames.random()) } + private fun RBuilder.reorderPlayersButton(currentGame: LobbyDTO) { + bpButton( + large = true, + intent = Intent.NONE, + icon = "random", + rightIcon = "people", + title = "Re-order players randomly", + onClick = { reorderPlayers(currentGame) } + ) + } + + private fun reorderPlayers(currentGame: LobbyDTO) { + props.reorderPlayers(currentGame.players.map { it.username }.shuffled()) + } + + private fun RBuilder.randomizeWondersButton(currentGame: LobbyDTO) { + bpButton( + large = true, + intent = Intent.NONE, + icon = "random", + title = "Re-assign wonders to players randomly", + onClick = { randomizeWonders(currentGame) } + ) { + +"W" + } + } + + private fun randomizeWonders(currentGame: LobbyDTO) { + props.reassignWonders(currentGame.allWonders.deal(currentGame.players.size)) + } + private fun RBuilder.leaveButton() { bpButton( large = true, @@ -129,5 +163,7 @@ private val lobby = connectStateAndDispatch dispatch(RequestAddBot(name)) } leaveLobby = { dispatch(RequestLeaveLobby()) } + reorderPlayers = { orderedPlayers -> dispatch(RequestReorderPlayers(orderedPlayers)) } + reassignWonders = { wonders -> dispatch(RequestReassignWonders(wonders)) } } ) 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 7badb11c..53cdeaf4 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 @@ -2,6 +2,7 @@ package org.luxons.sevenwonders.ui.redux import org.luxons.sevenwonders.model.Settings import org.luxons.sevenwonders.model.PlayerMove +import org.luxons.sevenwonders.model.wonders.AssignedWonder import redux.RAction data class RequestChooseName(val playerName: String) : RAction @@ -14,6 +15,8 @@ data class RequestAddBot(val botDisplayName: String) : RAction data class RequestReorderPlayers(val orderedPlayers: List) : RAction +data class RequestReassignWonders(val wonders: List) : RAction + data class RequestUpdateSettings(val settings: Settings) : RAction class RequestStartGame : 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 d6d41881..044f8e78 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 @@ -4,11 +4,7 @@ import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.flow.map import kotlinx.coroutines.launch import org.luxons.sevenwonders.client.SevenWondersSession -import org.luxons.sevenwonders.ui.redux.EnterGameAction -import org.luxons.sevenwonders.ui.redux.RequestAddBot -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.redux.* import org.luxons.sevenwonders.ui.router.Navigate import org.luxons.sevenwonders.ui.router.Route import org.luxons.sevenwonders.ui.utils.awaitFirst @@ -23,6 +19,12 @@ suspend fun SwSagaContext.lobbySaga(session: SevenWondersSession) { launch { onEach { session.addBot(it.botDisplayName) } } + launch { + onEach { session.reorderPlayers(it.orderedPlayers) } + } + launch { + onEach { session.reassignWonders(it.wonders) } + } val startGameJob = launch { awaitStartGame(session) } awaitFirst( -- cgit