diff options
author | Joffrey Bion <joffrey.bion@booking.com> | 2020-03-30 11:30:32 +0200 |
---|---|---|
committer | Joffrey Bion <joffrey.bion@booking.com> | 2020-03-30 11:35:10 +0200 |
commit | 0ca4028cdb58cc7649da04c0c2b16479ecd83d79 (patch) | |
tree | f027e587b2c4d8d30acdb4e6205d0319fe5df9b4 | |
parent | Rename GameState -> TableState (diff) | |
download | seven-wonders-0ca4028cdb58cc7649da04c0c2b16479ecd83d79.tar.gz seven-wonders-0ca4028cdb58cc7649da04c0c2b16479ecd83d79.tar.bz2 seven-wonders-0ca4028cdb58cc7649da04c0c2b16479ecd83d79.zip |
Send turn info without hands when entering game
9 files changed, 56 insertions, 35 deletions
diff --git a/sw-client/src/commonMain/kotlin/org/luxons/sevenwonders/client/SevenWondersClient.kt b/sw-client/src/commonMain/kotlin/org/luxons/sevenwonders/client/SevenWondersClient.kt index b97413c3..d72d353b 100644 --- a/sw-client/src/commonMain/kotlin/org/luxons/sevenwonders/client/SevenWondersClient.kt +++ b/sw-client/src/commonMain/kotlin/org/luxons/sevenwonders/client/SevenWondersClient.kt @@ -99,10 +99,11 @@ class SevenWondersSession(private val stompSession: StompSessionWithKxSerializat suspend fun watchLobbyUpdates(): StompSubscription<LobbyDTO> = stompSession.subscribe("/user/queue/lobby/updated", LobbyDTO.serializer()) - suspend fun awaitGameStart(gameId: Long) { - val gameStartSubscription = stompSession.subscribeEmptyMsg("/topic/lobby/$gameId/started") - gameStartSubscription.messages.receive() + suspend fun awaitGameStart(gameId: Long): PlayerTurnInfo { + val gameStartSubscription = stompSession.subscribe("/user/queue/lobby/$gameId/started", PlayerTurnInfo.serializer()) + val turnInfo = gameStartSubscription.messages.receive() gameStartSubscription.unsubscribe() + return turnInfo } suspend fun startGame() { diff --git a/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/Moves.kt b/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/Moves.kt index d684505e..736ace86 100644 --- a/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/Moves.kt +++ b/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/Moves.kt @@ -30,6 +30,10 @@ data class PlayerTurnInfo( val wonderBuildability: WonderBuildability = table.boards[playerIndex].wonder.buildability } +// TODO move to server code +fun Collection<PlayerTurnInfo>.hideHandsAndWaitForReadiness() = + map { it.copy(action = Action.SAY_READY, hand = null) } + @Serializable data class PlayedMove( val playerIndex: Int, diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameController.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameController.kt index 803cca78..733259b9 100644 --- a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameController.kt +++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameController.kt @@ -6,6 +6,7 @@ import org.luxons.sevenwonders.model.Action import org.luxons.sevenwonders.model.PlayerTurnInfo import org.luxons.sevenwonders.model.api.actions.PrepareMoveAction import org.luxons.sevenwonders.model.cards.PreparedCard +import org.luxons.sevenwonders.model.hideHandsAndWaitForReadiness import org.luxons.sevenwonders.server.api.toDTO import org.luxons.sevenwonders.server.lobby.Player import org.luxons.sevenwonders.server.repositories.PlayerRepository @@ -92,9 +93,6 @@ class GameController @Autowired constructor( } } - private fun Collection<PlayerTurnInfo>.hideHandsAndWaitForReadiness() = - map { it.copy(action = Action.SAY_READY, hand = null) } - companion object { private val logger = LoggerFactory.getLogger(GameController::class.java) } diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/LobbyController.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/LobbyController.kt index 04fb2228..ee1c6271 100644 --- a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/LobbyController.kt +++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/LobbyController.kt @@ -3,6 +3,7 @@ package org.luxons.sevenwonders.server.controllers import org.hildan.livedoc.core.annotations.Api import org.luxons.sevenwonders.model.api.actions.ReorderPlayersAction import org.luxons.sevenwonders.model.api.actions.UpdateSettingsAction +import org.luxons.sevenwonders.model.hideHandsAndWaitForReadiness import org.luxons.sevenwonders.server.api.toDTO import org.luxons.sevenwonders.server.lobby.Lobby import org.luxons.sevenwonders.server.lobby.Player @@ -101,7 +102,10 @@ class LobbyController @Autowired constructor( val game = lobby.startGame() logger.info("Game {} successfully started", game.id) - template.convertAndSend("/topic/lobby/" + lobby.id + "/started", Unit) + game.getCurrentTurnInfo().hideHandsAndWaitForReadiness().forEach { + val player = lobby.getPlayers()[it.playerIndex] + template.convertAndSendToUser(player.username, "/queue/lobby/" + lobby.id + "/started", it) + } } companion object { 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) { |