summaryrefslogtreecommitdiff
path: root/sw-ui-kt/src
diff options
context:
space:
mode:
authorJoffrey Bion <joffrey.bion@booking.com>2020-03-30 11:30:32 +0200
committerJoffrey Bion <joffrey.bion@booking.com>2020-03-30 11:35:10 +0200
commit0ca4028cdb58cc7649da04c0c2b16479ecd83d79 (patch)
treef027e587b2c4d8d30acdb4e6205d0319fe5df9b4 /sw-ui-kt/src
parentRename GameState -> TableState (diff)
downloadseven-wonders-0ca4028cdb58cc7649da04c0c2b16479ecd83d79.tar.gz
seven-wonders-0ca4028cdb58cc7649da04c0c2b16479ecd83d79.tar.bz2
seven-wonders-0ca4028cdb58cc7649da04c0c2b16479ecd83d79.zip
Send turn info without hands when entering game
Diffstat (limited to 'sw-ui-kt/src')
-rw-r--r--sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/components/game/GameScene.kt20
-rw-r--r--sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Actions.kt2
-rw-r--r--sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Reducers.kt37
-rw-r--r--sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/GameSagas.kt6
-rw-r--r--sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/LobbySagas.kt5
5 files changed, 42 insertions, 28 deletions
diff --git a/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/components/game/GameScene.kt b/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/components/game/GameScene.kt
index d9de85d9..fecb8be2 100644
--- a/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/components/game/GameScene.kt
+++ b/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/components/game/GameScene.kt
@@ -3,7 +3,6 @@ package org.luxons.sevenwonders.ui.components.game
import com.palantir.blueprintjs.Intent
import com.palantir.blueprintjs.bpButton
import com.palantir.blueprintjs.bpButtonGroup
-import com.palantir.blueprintjs.bpNonIdealState
import com.palantir.blueprintjs.org.luxons.sevenwonders.ui.components.game.handComponent
import kotlinx.css.CSSBuilder
import kotlinx.css.Overflow
@@ -65,24 +64,13 @@ private class GameScene(props: GameSceneProps) : RComponent<GameSceneProps, RSta
}
val turnInfo = props.turnInfo
if (turnInfo == null) {
- gamePreStart()
+ p { +"Error: no turn info data"}
} else {
turnInfoScene(turnInfo)
}
}
}
- private fun RBuilder.gamePreStart() {
- bpNonIdealState(
- description = createElement {
- p { +"Click 'ready' when you are"}
- },
- action = createElement {
- sayReadyButton()
- }
- )
- }
-
private fun RBuilder.sayReadyButton(block: StyledDOMBuilder<DIV>.() -> Unit = {}): ReactElement {
val isReady = props.playerIsReady
val intent = if (isReady) Intent.SUCCESS else Intent.PRIMARY
@@ -128,7 +116,7 @@ private class GameScene(props: GameSceneProps) : RComponent<GameSceneProps, RSta
sayReadyButton {
css {
position = Position.absolute
- bottom = 4.rem
+ bottom = 6.rem
left = 50.pct
transform { translate(tx = (-50).pct) }
}
@@ -150,8 +138,8 @@ private val gameScene: RClass<GameSceneProps> = connectStateAndDispatch<GameScen
},
mapStateToProps = { state, _ ->
playerIsReady = state.currentPlayer?.isReady == true
- players = state.currentLobby?.players ?: emptyList()
- turnInfo = state.currentTurnInfo
+ players = state.gameState?.players ?: emptyList()
+ turnInfo = state.gameState?.turnInfo
}
)
diff --git a/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Actions.kt b/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Actions.kt
index 4947fa9b..85b48e61 100644
--- a/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Actions.kt
+++ b/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Actions.kt
@@ -14,7 +14,7 @@ data class UpdateLobbyAction(val lobby: LobbyDTO): RAction
data class EnterLobbyAction(val lobby: LobbyDTO): RAction
-data class EnterGameAction(val gameId: Long): RAction
+data class EnterGameAction(val lobby: LobbyDTO, val turnInfo: PlayerTurnInfo): RAction
data class TurnInfoEvent(val turnInfo: PlayerTurnInfo): RAction
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 18d34d78..f7c27eda 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,6 +5,7 @@ import org.luxons.sevenwonders.model.api.ConnectedPlayer
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.PreparedCard
import redux.RAction
data class SwState(
@@ -12,17 +13,26 @@ data class SwState(
// they must be by ID to support updates to a sublist
val gamesById: Map<Long, LobbyDTO> = emptyMap(),
val currentLobby: LobbyDTO? = null,
- val currentTurnInfo: PlayerTurnInfo? = null
+ val gameState: GameState? = null
) {
- val currentPlayer: PlayerDTO? = currentLobby?.players?.first { it.username == connectedPlayer?.username }
+ val currentPlayer: PlayerDTO? = (gameState?.players ?: currentLobby?.players)?.first {
+ it.username == connectedPlayer?.username
+ }
val games: List<LobbyDTO> = gamesById.values.toList()
}
+data class GameState(
+ val id: Long,
+ val players: List<PlayerDTO>,
+ val preparedCardsByUsername: Map<String, PreparedCard>,
+ val turnInfo: PlayerTurnInfo?
+)
+
fun rootReducer(state: SwState, action: RAction): SwState = state.copy(
gamesById = gamesReducer(state.gamesById, action),
connectedPlayer = currentPlayerReducer(state.connectedPlayer, action),
currentLobby = currentLobbyReducer(state.currentLobby, action),
- currentTurnInfo = currentTurnInfoReducer(state.currentTurnInfo, action)
+ gameState = currentTurnInfoReducer(state.gameState, action)
)
private fun gamesReducer(games: Map<Long, LobbyDTO>, action: RAction): Map<Long, LobbyDTO> = when (action) {
@@ -46,7 +56,22 @@ private fun currentLobbyReducer(currentLobby: LobbyDTO?, action: RAction): Lobby
else -> currentLobby
}
-private fun currentTurnInfoReducer(currentTurnInfo: PlayerTurnInfo?, action: RAction): PlayerTurnInfo? = when (action) {
- is TurnInfoEvent -> action.turnInfo
- else -> currentTurnInfo
+private fun currentTurnInfoReducer(gameState: GameState?, action: RAction): GameState? = when (action) {
+ is EnterGameAction -> GameState(
+ id = action.lobby.id,
+ players = action.lobby.players,
+ preparedCardsByUsername = emptyMap(),
+ turnInfo = action.turnInfo
+ )
+ is PreparedCardEvent -> gameState?.copy(
+ preparedCardsByUsername = gameState.preparedCardsByUsername + (action.card.player.username to action.card)
+ )
+ is PlayerReadyEvent -> gameState?.copy(players = gameState.players.map { p ->
+ if (p.username == action.username) p.copy(isReady = true) else p
+ })
+ is TurnInfoEvent -> gameState?.copy(
+ players = gameState.players.map { p -> p.copy(isReady = false) },
+ turnInfo = action.turnInfo
+ )
+ else -> gameState
}
diff --git a/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/GameSagas.kt b/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/GameSagas.kt
index cadf56e9..e2c2d4d1 100644
--- a/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/GameSagas.kt
+++ b/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/GameSagas.kt
@@ -10,10 +10,10 @@ import org.luxons.sevenwonders.ui.redux.RequestSayReady
import org.luxons.sevenwonders.ui.redux.TurnInfoEvent
suspend fun SwSagaContext.gameSaga(session: SevenWondersSession) {
- val lobby = getState().currentLobby ?: error("Game saga run without a current game")
+ val game = getState().gameState ?: error("Game saga run without a current game")
coroutineScope {
- val playerReadySub = session.watchPlayerReady(lobby.id)
- val preparedCardsSub = session.watchPreparedCards(lobby.id)
+ val playerReadySub = session.watchPlayerReady(game.id)
+ val preparedCardsSub = session.watchPreparedCards(game.id)
val turnInfoSub = session.watchTurns()
val sayReadyJob = launch { onEach<RequestSayReady> { session.sayReady() } }
val prepareMoveJob = launch { onEach<RequestPrepareMove> { session.prepareMove(it.move) } }
diff --git a/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/LobbySagas.kt b/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/LobbySagas.kt
index 39c29722..678276dc 100644
--- a/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/LobbySagas.kt
+++ b/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/LobbySagas.kt
@@ -31,8 +31,9 @@ private suspend fun SwSagaContext.watchLobbyUpdates(lobbyUpdatesSubscription: St
}
private suspend fun SwSagaContext.awaitGameStart(session: SevenWondersSession, lobbyId: Long) {
- session.awaitGameStart(lobbyId)
- dispatch(EnterGameAction(lobbyId))
+ val turnInfo = session.awaitGameStart(lobbyId)
+ val lobby = getState().currentLobby!!
+ dispatch(EnterGameAction(lobby, turnInfo))
}
private suspend fun SwSagaContext.awaitStartGame(session: SevenWondersSession) {
bgstack15