diff options
author | Joffrey Bion <joffrey.bion@gmail.com> | 2021-02-23 18:31:20 +0100 |
---|---|---|
committer | Joffrey Bion <joffrey.bion@gmail.com> | 2021-02-23 18:45:04 +0100 |
commit | b9108dd5f848f13db157cdbe04a2b403e2d8ee7d (patch) | |
tree | 1708b619ed0687d638b6e1846770d9a2e5ef6e84 /sw-common-model/src | |
parent | Cleanup self board summary (diff) | |
download | seven-wonders-b9108dd5f848f13db157cdbe04a2b403e2d8ee7d.tar.gz seven-wonders-b9108dd5f848f13db157cdbe04a2b403e2d8ee7d.tar.bz2 seven-wonders-b9108dd5f848f13db157cdbe04a2b403e2d8ee7d.zip |
Use proper sealed class for TurnActions
Diffstat (limited to 'sw-common-model/src')
3 files changed, 112 insertions, 62 deletions
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 a1b3cb4e..ead54e96 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 @@ -1,71 +1,9 @@ package org.luxons.sevenwonders.model import kotlinx.serialization.Serializable -import org.luxons.sevenwonders.model.boards.Board -import org.luxons.sevenwonders.model.boards.RelativeBoardPosition -import org.luxons.sevenwonders.model.cards.HandCard import org.luxons.sevenwonders.model.cards.TableCard import org.luxons.sevenwonders.model.resources.ResourceTransactions import org.luxons.sevenwonders.model.resources.noTransactions -import org.luxons.sevenwonders.model.score.ScoreBoard -import org.luxons.sevenwonders.model.wonders.WonderBuildability - -enum class Action(val message: String) { - SAY_READY("Click 'READY' when you are ready to receive your next hand."), - PLAY("Pick the card you want to play or discard."), - PLAY_2("Pick the first card you want to play or discard. Note that you have the ability to play these 2 last cards. You will choose how to play the last one during your next turn."), - PLAY_LAST("You have the special ability to play your last card. Choose how you want to play it."), - PLAY_FREE_DISCARDED("Pick a card from the discarded deck, you can play it for free (but you cannot discard for 3 gold coins or upgrade your wonder with it)."), - PICK_NEIGHBOR_GUILD("Choose a Guild card (purple) that you want to copy from one of your neighbours."), - WAIT("Please wait for other players to perform extra actions."), - WATCH_SCORE("The game is over! Look at the scoreboard to see the final ranking!"); - - fun allowsBuildingWonder(): Boolean = when (this) { - PLAY, PLAY_2, PLAY_LAST -> true - else -> false - } - - fun allowsDiscarding(): Boolean = when (this) { - PLAY, PLAY_2, PLAY_LAST -> true - else -> false - } -} - -@Serializable -data class PlayerTurnInfo( - val playerIndex: Int, - val table: TableState, - val action: Action, - val hand: List<HandCard>?, - val neighbourGuildCards: List<HandCard>, - val discardedCards: List<HandCard>?, // only present when the player can actually see them - val scoreBoard: ScoreBoard? = null, -) { - val currentAge: Int = table.currentAge - val message: String = action.message - val wonderBuildability: WonderBuildability = table.boards[playerIndex].wonder.buildability - - val RelativeBoardPosition.index: Int - get() = getIndexFrom(playerIndex, table.boards.size) -} - -fun PlayerTurnInfo.getOwnBoard(): Board = table.boards[playerIndex] - -fun PlayerTurnInfo.getBoard(position: RelativeBoardPosition): Board = table.boards[position.index] - -fun PlayerTurnInfo.getNonNeighbourBoards(): List<Board> { - val nPlayers = table.boards.size - if (nPlayers <= 3) { - return emptyList() - } - val first = (playerIndex + 2) % nPlayers - val last = (playerIndex - 2 + nPlayers) % nPlayers - val range = if (first <= last) first..last else ((first until nPlayers) + (0..last)) - return range.map { table.boards[it] } -} - -// TODO move to server code -fun Collection<PlayerTurnInfo>.hideHandsAndWaitForReadiness() = map { it.copy(action = Action.SAY_READY, hand = null) } @Serializable data class PlayedMove( diff --git a/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/PlayerState.kt b/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/PlayerState.kt new file mode 100644 index 00000000..271b2a99 --- /dev/null +++ b/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/PlayerState.kt @@ -0,0 +1,38 @@ +package org.luxons.sevenwonders.model + +import kotlinx.serialization.Serializable +import org.luxons.sevenwonders.model.boards.Board +import org.luxons.sevenwonders.model.boards.RelativeBoardPosition +import org.luxons.sevenwonders.model.wonders.WonderBuildability + +@Serializable +data class PlayerTurnInfo( + val playerIndex: Int, + val table: TableState, + val action: TurnAction, +) { + val currentAge: Int = table.currentAge + val message: String = action.message + val wonderBuildability: WonderBuildability = table.boards[playerIndex].wonder.buildability + + val RelativeBoardPosition.index: Int + get() = getIndexFrom(playerIndex, table.boards.size) +} + +fun PlayerTurnInfo.getOwnBoard(): Board = table.boards[playerIndex] + +fun PlayerTurnInfo.getBoard(position: RelativeBoardPosition): Board = table.boards[position.index] + +fun PlayerTurnInfo.getNonNeighbourBoards(): List<Board> { + val nPlayers = table.boards.size + if (nPlayers <= 3) { + return emptyList() + } + val first = (playerIndex + 2) % nPlayers + val last = (playerIndex - 2 + nPlayers) % nPlayers + val range = if (first <= last) first..last else ((first until nPlayers) + (0..last)) + return range.map { table.boards[it] } +} + +// TODO move to server code +fun Collection<PlayerTurnInfo>.hideHandsAndWaitForReadiness() = map { it.copy(action = TurnAction.SayReady()) } diff --git a/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/TurnActions.kt b/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/TurnActions.kt new file mode 100644 index 00000000..bd94944b --- /dev/null +++ b/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/TurnActions.kt @@ -0,0 +1,74 @@ +package org.luxons.sevenwonders.model + +import kotlinx.serialization.Serializable +import org.luxons.sevenwonders.model.cards.HandCard +import org.luxons.sevenwonders.model.score.ScoreBoard + +object ActionMessages { + const val SAY_READY = "Click 'READY' when you are ready to receive your next hand." + const val PLAY = "Pick the card you want to play or discard. The rest of the cards will be passed on to the next player." + const val PLAY_LAST = "Pick the card you want to play or discard. The remaining card will be discarded." + const val PLAY_2 = "Pick the first card you want to play or discard. Note that you have the ability to play these 2 last cards. You will choose how to play the last one during your next turn." + const val PLAY_LAST_SPECIAL = "You have the special ability to play your last card. Choose how you want to play it." + const val PLAY_FREE_DISCARDED = "Pick a card from the discarded deck, you can play it for free (but you cannot discard for 3 gold coins or upgrade your wonder with it)." + const val PICK_NEIGHBOR_GUILD = "Choose a Guild card (purple) that you want to copy from one of your neighbours." + const val WAIT_OTHER_PLAY_LAST = "Another player can play his last card, please wait…" + const val WAIT_OTHER_PICK_GUILD = "Another player is picking a guild card to copy, please wait…" + const val WAIT_OTHER_PLAY_DISCARD = "Another player is playing a free card from the discard pile, please wait…" + const val WATCH_SCORE = "The game is over! Look at the scoreboard to see the final ranking!" +} + +@Serializable +sealed class TurnAction { + abstract val message: String + + @Serializable + data class SayReady( + override val message: String = ActionMessages.SAY_READY, + ) : TurnAction() + + @Serializable + data class PlayFromHand( + override val message: String, + val hand: List<HandCard>, + ) : TurnAction() { + override fun toString(): String = "${super.toString()} (${hand.size} cards)" + } + + @Serializable + data class PlayFromDiscarded( + val discardedCards: List<HandCard>, + ) : TurnAction() { + override val message: String = ActionMessages.PLAY_FREE_DISCARDED + + override fun toString(): String = "${super.toString()} (${discardedCards.size} cards)" + } + + @Serializable + data class PickNeighbourGuild( + val neighbourGuildCards: List<HandCard>, + ) : TurnAction() { + override val message: String = ActionMessages.PICK_NEIGHBOR_GUILD + + override fun toString(): String = "${super.toString()} (${neighbourGuildCards.size} cards)" + } + + @Serializable + data class Wait( + override val message: String, + ) : TurnAction() + + @Serializable + data class WatchScore( + override val message: String = ActionMessages.WATCH_SCORE, + val scoreBoard: ScoreBoard, + ) : TurnAction() { + override fun toString(): String = "${super.toString()} (winner: Player #${scoreBoard.scores[0].playerIndex})" + } + + fun allowsBuildingWonder(): Boolean = this is PlayFromHand + + fun allowsDiscarding(): Boolean = this is PlayFromHand + + override fun toString(): String = this::class.simpleName!! +} |