From 594a7037af815020658ab127b6649b45bca0cbc1 Mon Sep 17 00:00:00 2001 From: Joffrey Bion Date: Wed, 24 Feb 2021 01:36:50 +0100 Subject: Improved state --- .../org/luxons/sevenwonders/model/PlayerState.kt | 46 ++++++++++++++++------ .../org/luxons/sevenwonders/model/TableState.kt | 4 +- 2 files changed, 35 insertions(+), 15 deletions(-) (limited to 'sw-common-model/src/commonMain') 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 index 271b2a99..725b1654 100644 --- 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 @@ -1,37 +1,59 @@ package org.luxons.sevenwonders.model import kotlinx.serialization.Serializable +import org.luxons.sevenwonders.model.api.PlayerDTO import org.luxons.sevenwonders.model.boards.Board import org.luxons.sevenwonders.model.boards.RelativeBoardPosition +import org.luxons.sevenwonders.model.cards.CardBack +import org.luxons.sevenwonders.model.cards.HandCard +import org.luxons.sevenwonders.model.cards.HandRotationDirection import org.luxons.sevenwonders.model.wonders.WonderBuildability @Serializable -data class PlayerTurnInfo( +data class GameState( + val gameId: Long, val playerIndex: Int, - val table: TableState, + val currentAge: Age, + val players: List, + val boards: List, + val handRotationDirection: HandRotationDirection, val action: TurnAction, + val preparedCardsByUsername: Map = emptyMap(), + val currentPreparedMove: PlayerMove? = null, ) { - val currentAge: Int = table.currentAge - val message: String = action.message - val wonderBuildability: WonderBuildability = table.boards[playerIndex].wonder.buildability + val currentPreparedCard: HandCard? + get() { + val hand = (action as? TurnAction.PlayFromHand)?.hand + return hand?.firstOrNull { it.name == currentPreparedMove?.cardName } + } - val RelativeBoardPosition.index: Int - get() = getIndexFrom(playerIndex, table.boards.size) + val RelativeBoardPosition.absoluteIndex: Int + get() = getIndexFrom(playerIndex, boards.size) } -fun PlayerTurnInfo.getOwnBoard(): Board = table.boards[playerIndex] +fun GameState.getOwnBoard(): Board = boards[playerIndex] -fun PlayerTurnInfo.getBoard(position: RelativeBoardPosition): Board = table.boards[position.index] +fun GameState.getBoard(position: RelativeBoardPosition): Board = boards[position.absoluteIndex] -fun PlayerTurnInfo.getNonNeighbourBoards(): List { - val nPlayers = table.boards.size +fun GameState.getNonNeighbourBoards(): List { + val nPlayers = 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] } + return range.map { boards[it] } +} + +@Serializable +data class PlayerTurnInfo( + val playerIndex: Int, + val table: TableState, + val action: TurnAction, +) { + val currentAge: Int = table.currentAge + val wonderBuildability: WonderBuildability = table.boards[playerIndex].wonder.buildability } // TODO move to server code diff --git a/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/TableState.kt b/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/TableState.kt index d27fccb5..e19ce272 100644 --- a/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/TableState.kt +++ b/sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/TableState.kt @@ -12,6 +12,4 @@ data class TableState( val currentAge: Age, val handRotationDirection: HandRotationDirection, val lastPlayedMoves: List, -) { - val nbPlayers: Int = boards.size -} +) -- cgit