From b9108dd5f848f13db157cdbe04a2b403e2d8ee7d Mon Sep 17 00:00:00 2001 From: Joffrey Bion Date: Tue, 23 Feb 2021 18:31:20 +0100 Subject: Use proper sealed class for TurnActions --- .../sevenwonders/ui/components/game/GameScene.kt | 7 ++++--- .../luxons/sevenwonders/ui/components/game/Hand.kt | 24 +++++++++++++--------- .../org/luxons/sevenwonders/ui/redux/Reducers.kt | 7 ++++++- 3 files changed, 24 insertions(+), 14 deletions(-) (limited to 'sw-ui/src/main/kotlin') diff --git a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/components/game/GameScene.kt b/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/components/game/GameScene.kt index ccbb2958..ed1c14d1 100644 --- a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/components/game/GameScene.kt +++ b/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/components/game/GameScene.kt @@ -61,8 +61,9 @@ private class GameScene(props: GameSceneProps) : RComponent(props) { override fun RBuilder.render() { - val hand = props.turnInfo.cardsToPlay() ?: return + val hand = props.turnInfo.action.cardsToPlay() ?: return styledDiv { css { handStyle() @@ -52,11 +52,13 @@ class HandComponent(props: HandProps) : RComponent(props) { } } - private fun PlayerTurnInfo.cardsToPlay(): List? = when (action) { - Action.PLAY, Action.PLAY_2, Action.PLAY_LAST -> hand - Action.PLAY_FREE_DISCARDED -> discardedCards - Action.PICK_NEIGHBOR_GUILD -> neighbourGuildCards - Action.WAIT, Action.WATCH_SCORE, Action.SAY_READY -> null + private fun TurnAction.cardsToPlay(): List? = when (this) { + is TurnAction.PlayFromHand -> hand + is TurnAction.PlayFromDiscarded -> discardedCards + is TurnAction.PickNeighbourGuild -> neighbourGuildCards + is TurnAction.SayReady, + is TurnAction.Wait, + is TurnAction.WatchScore -> null } private fun RBuilder.handCard( @@ -94,15 +96,17 @@ class HandComponent(props: HandProps) : RComponent(props) { bpButtonGroup { val action = props.turnInfo.action when (action) { - Action.PLAY, Action.PLAY_2, Action.PLAY_LAST -> { + is TurnAction.PlayFromHand -> { playCardButton(card, HandAction.PLAY) if (props.turnInfo.getOwnBoard().canPlayAnyCardForFree) { playCardButton(card.copy(playability = CardPlayability.SPECIAL_FREE), HandAction.PLAY_FREE) } } - Action.PLAY_FREE_DISCARDED -> playCardButton(card, HandAction.PLAY_FREE_DISCARDED) - Action.PICK_NEIGHBOR_GUILD -> playCardButton(card, HandAction.COPY_GUILD) - else -> error("unsupported action in hand card: $action") + is TurnAction.PlayFromDiscarded -> playCardButton(card, HandAction.PLAY_FREE_DISCARDED) + is TurnAction.PickNeighbourGuild -> playCardButton(card, HandAction.COPY_GUILD) + is TurnAction.SayReady, + is TurnAction.Wait, + is TurnAction.WatchScore -> error("unsupported action in hand card: $action") } if (action.allowsBuildingWonder()) { diff --git a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Reducers.kt b/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Reducers.kt index 91dbf61f..5d345136 100644 --- a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Reducers.kt +++ b/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Reducers.kt @@ -3,6 +3,7 @@ package org.luxons.sevenwonders.ui.redux import org.luxons.sevenwonders.model.MoveType import org.luxons.sevenwonders.model.PlayerMove import org.luxons.sevenwonders.model.PlayerTurnInfo +import org.luxons.sevenwonders.model.TurnAction import org.luxons.sevenwonders.model.api.ConnectedPlayer import org.luxons.sevenwonders.model.api.GameListEvent import org.luxons.sevenwonders.model.api.LobbyDTO @@ -32,10 +33,14 @@ data class GameState( val turnInfo: PlayerTurnInfo?, val preparedCardsByUsername: Map = emptyMap(), val currentPreparedMove: PlayerMove? = null, + // UI val transactionSelector: TransactionSelectorState? = null, ) { val currentPreparedCard: HandCard? - get() = turnInfo?.hand?.firstOrNull { it.name == currentPreparedMove?.cardName } + get() { + val hand = (turnInfo?.action as? TurnAction.PlayFromHand)?.hand + return hand?.firstOrNull { it.name == currentPreparedMove?.cardName } + } } data class TransactionSelectorState( -- cgit