summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoffrey Bion <joffrey.bion@gmail.com>2021-03-08 01:31:26 +0100
committerJoffrey Bion <joffrey.bion@gmail.com>2021-03-08 01:31:59 +0100
commit3f8a672dcfefdd9304ba8b489865cb12153a75e3 (patch)
tree3b5e091d37b9f821e189ec26791db7a4eec177b4
parentMake SayReady action an object (diff)
downloadseven-wonders-3f8a672dcfefdd9304ba8b489865cb12153a75e3.tar.gz
seven-wonders-3f8a672dcfefdd9304ba8b489865cb12153a75e3.tar.bz2
seven-wonders-3f8a672dcfefdd9304ba8b489865cb12153a75e3.zip
Make turnInfo generic in its action type
-rw-r--r--sw-bot/src/main/kotlin/org/luxons/sevenwonders/bot/SevenWondersBot.kt18
-rw-r--r--sw-common-model/src/commonMain/kotlin/org/luxons/sevenwonders/model/PlayerState.kt12
-rw-r--r--sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/Game.kt10
-rw-r--r--sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/GameTest.kt12
-rw-r--r--sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Actions.kt5
5 files changed, 35 insertions, 22 deletions
diff --git a/sw-bot/src/main/kotlin/org/luxons/sevenwonders/bot/SevenWondersBot.kt b/sw-bot/src/main/kotlin/org/luxons/sevenwonders/bot/SevenWondersBot.kt
index a10ca152..7530b40e 100644
--- a/sw-bot/src/main/kotlin/org/luxons/sevenwonders/bot/SevenWondersBot.kt
+++ b/sw-bot/src/main/kotlin/org/luxons/sevenwonders/bot/SevenWondersBot.kt
@@ -7,7 +7,6 @@ import org.luxons.sevenwonders.model.*
import org.luxons.sevenwonders.model.api.ConnectedPlayer
import org.luxons.sevenwonders.model.api.actions.BotConfig
import org.luxons.sevenwonders.model.api.actions.Icon
-import org.luxons.sevenwonders.model.cards.HandCard
import org.luxons.sevenwonders.model.resources.noTransactions
import org.luxons.sevenwonders.model.wonders.AssignedWonder
import org.slf4j.LoggerFactory
@@ -46,7 +45,7 @@ class SevenWondersBot(
otherBots: List<SevenWondersBot>,
customWonders: List<AssignedWonder>? = null,
customSettings: Settings? = null,
- ): PlayerTurnInfo {
+ ): PlayerTurnInfo<*> {
val nJoinerBots = otherBots.size
require(nJoinerBots >= 2) { "At least 2 more bots must join the game" }
require(customWonders == null || customWonders.size == nJoinerBots + 1) {
@@ -73,12 +72,12 @@ class SevenWondersBot(
}
}
- suspend fun joinAndAutoPlay(gameId: Long): PlayerTurnInfo {
+ suspend fun joinAndAutoPlay(gameId: Long): PlayerTurnInfo<*> {
val firstTurn = session.joinGameAndAwaitFirstTurn(gameId)
return autoPlayUntilEnd(firstTurn)
}
- private suspend fun autoPlayUntilEnd(currentTurn: PlayerTurnInfo) = coroutineScope {
+ private suspend fun autoPlayUntilEnd(currentTurn: PlayerTurnInfo<*>) = coroutineScope {
val endGameTurnInfo = async {
session.watchTurns().filter { it.action is TurnAction.WatchScore }.first()
}
@@ -112,18 +111,21 @@ class SevenWondersBot(
}
}
-private suspend fun SevenWondersSession.autoPlayTurn(turn: PlayerTurnInfo) {
+@Suppress("UNCHECKED_CAST")
+private suspend fun SevenWondersSession.autoPlayTurn(turn: PlayerTurnInfo<*>) {
when (val action = turn.action) {
- is TurnAction.PlayFromHand -> prepareMove(createPlayCardMove(turn, action.hand))
+ is TurnAction.PlayFromHand -> prepareMove(createPlayCardMove(turn as PlayerTurnInfo<TurnAction.PlayFromHand>))
is TurnAction.PlayFromDiscarded -> prepareMove(createPlayFreeDiscardedCardMove(action))
is TurnAction.PickNeighbourGuild -> prepareMove(createPickGuildMove(action))
is TurnAction.SayReady -> sayReady()
- is TurnAction.Wait, is TurnAction.WatchScore -> Unit
+ is TurnAction.Wait,
+ is TurnAction.WatchScore -> Unit
}
}
-private fun createPlayCardMove(turnInfo: PlayerTurnInfo, hand: List<HandCard>): PlayerMove {
+private fun createPlayCardMove(turnInfo: PlayerTurnInfo<TurnAction.PlayFromHand>): PlayerMove {
val wonderBuildability = turnInfo.wonderBuildability
+ val hand = turnInfo.action.hand
if (wonderBuildability.isBuildable) {
val transactions = wonderBuildability.transactionsOptions.random()
return PlayerMove(MoveType.UPGRADE_WONDER, hand.random().name, transactions)
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 1e483a56..db0a2eb6 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
@@ -4,14 +4,20 @@ import kotlinx.serialization.Serializable
import org.luxons.sevenwonders.model.wonders.WonderBuildability
@Serializable
-data class PlayerTurnInfo(
+data class PlayerTurnInfo<out A : TurnAction>(
val playerIndex: Int,
val table: TableState,
- val action: TurnAction,
+ val action: A,
) {
val currentAge: Int = table.currentAge
val wonderBuildability: WonderBuildability = table.boards[playerIndex].wonder.buildability
}
// TODO move to server code
-fun Collection<PlayerTurnInfo>.hideHandsAndWaitForReadiness() = map { it.copy(action = TurnAction.SayReady) }
+fun Collection<PlayerTurnInfo<*>>.hideHandsAndWaitForReadiness() = map {
+ PlayerTurnInfo(
+ playerIndex = it.playerIndex,
+ table = it.table,
+ action = TurnAction.SayReady,
+ )
+}
diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/Game.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/Game.kt
index 6c44c7b3..5281c753 100644
--- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/Game.kt
+++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/Game.kt
@@ -26,7 +26,7 @@ class Game internal constructor(
private val players: List<Player> = boards.map { SimplePlayer(it.playerIndex, table) }
private val discardedCards: MutableList<Card> = mutableListOf()
private val preparedMoves: MutableMap<Int, Move> = mutableMapOf()
- private var currentTurnInfo: List<PlayerTurnInfo> = emptyList()
+ private var currentTurnInfo: List<PlayerTurnInfo<*>> = emptyList()
private var hands: Hands = Hands(emptyList())
private var militaryConflictsResolved = false
@@ -88,7 +88,11 @@ class Game internal constructor(
}
val scoreBoard = computeScore()
currentTurnInfo = currentTurnInfo.map {
- it.copy(action = TurnAction.WatchScore(message = ActionMessages.WATCH_SCORE, scoreBoard = scoreBoard))
+ PlayerTurnInfo(
+ playerIndex = it.playerIndex,
+ table = it.table,
+ action = TurnAction.WatchScore(message = ActionMessages.WATCH_SCORE, scoreBoard = scoreBoard),
+ )
}
}
@@ -105,7 +109,7 @@ class Game internal constructor(
/**
* Returns information for each player about the current turn.
*/
- fun getCurrentTurnInfo(): List<PlayerTurnInfo> = currentTurnInfo
+ fun getCurrentTurnInfo(): List<PlayerTurnInfo<*>> = currentTurnInfo
/**
* Prepares the given [move] for the player at the given [playerIndex].
diff --git a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/GameTest.kt b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/GameTest.kt
index 09cdd970..5368fbc0 100644
--- a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/GameTest.kt
+++ b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/GameTest.kt
@@ -52,7 +52,7 @@ class GameTest {
} while (!game.getCurrentTurnInfo().first().isStartOfAge(age + 1))
}
- private fun PlayerTurnInfo.isStartOfAge(age: Int) = action is TurnAction.WatchScore || currentAge == age
+ private fun PlayerTurnInfo<*>.isStartOfAge(age: Int) = action is TurnAction.WatchScore || currentAge == age
private fun playTurn(nbPlayers: Int, game: Game, ageToCheck: Int) {
val turnInfos = game.getCurrentTurnInfo()
@@ -72,7 +72,7 @@ class GameTest {
assertEquals(expectedMoves, game.getCurrentTurnInfo()[0].table.lastPlayedMoves)
}
- private fun PlayerTurnInfo.firstAvailableMove(): MoveExpectation? = when (val a = action) {
+ private fun PlayerTurnInfo<*>.firstAvailableMove(): MoveExpectation? = when (val a = action) {
is TurnAction.PlayFromHand -> createPlayCardMove(this, a.hand)
is TurnAction.PlayFromDiscarded -> createPlayFreeDiscardedCardMove(this, a.discardedCards)
is TurnAction.PickNeighbourGuild -> createPickGuildMove(this, a.neighbourGuildCards)
@@ -81,7 +81,7 @@ class GameTest {
is TurnAction.WatchScore -> fail("should not have WATCH_SCORE action before end of game")
}
- private fun createPlayCardMove(turnInfo: PlayerTurnInfo, hand: List<HandCard>): MoveExpectation {
+ private fun createPlayCardMove(turnInfo: PlayerTurnInfo<*>, hand: List<HandCard>): MoveExpectation {
val wonderBuildability = turnInfo.wonderBuildability
if (wonderBuildability.isBuildable) {
val transactions = wonderBuildability.transactionsOptions.first()
@@ -95,7 +95,7 @@ class GameTest {
}
}
- private fun createPlayFreeDiscardedCardMove(turn: PlayerTurnInfo, discardedCards: List<HandCard>): MoveExpectation {
+ private fun createPlayFreeDiscardedCardMove(turn: PlayerTurnInfo<*>, discardedCards: List<HandCard>): MoveExpectation {
val card = discardedCards.random()
return MoveExpectation(
turn.playerIndex,
@@ -105,7 +105,7 @@ class GameTest {
}
private fun planMove(
- turnInfo: PlayerTurnInfo,
+ turnInfo: PlayerTurnInfo<*>,
moveType: MoveType,
card: HandCard,
transactions: ResourceTransactions,
@@ -115,7 +115,7 @@ class GameTest {
PlayedMove(turnInfo.playerIndex, moveType, card.toPlayedCard(), transactions),
)
- private fun createPickGuildMove(turnInfo: PlayerTurnInfo, neighbourGuilds: List<HandCard>): MoveExpectation {
+ private fun createPickGuildMove(turnInfo: PlayerTurnInfo<*>, neighbourGuilds: List<HandCard>): MoveExpectation {
// the game should send action WAIT if no guild cards are available around
assertFalse(neighbourGuilds.isEmpty())
val card = neighbourGuilds.first()
diff --git a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Actions.kt b/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Actions.kt
index 81ac14b8..9cda6992 100644
--- a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Actions.kt
+++ b/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Actions.kt
@@ -2,6 +2,7 @@ package org.luxons.sevenwonders.ui.redux
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
@@ -20,9 +21,9 @@ data class EnterLobbyAction(val lobby: LobbyDTO) : RAction
data class LeaveLobbyAction(val lobbyId: Long) : RAction
-data class EnterGameAction(val lobby: LobbyDTO, val turnInfo: PlayerTurnInfo) : RAction
+data class EnterGameAction(val lobby: LobbyDTO, val turnInfo: PlayerTurnInfo<TurnAction.SayReady>) : RAction
-data class TurnInfoEvent(val turnInfo: PlayerTurnInfo) : RAction
+data class TurnInfoEvent(val turnInfo: PlayerTurnInfo<*>) : RAction
data class PreparedMoveEvent(val move: PlayerMove) : RAction
bgstack15