diff options
author | Joffrey Bion <joffrey.bion@gmail.com> | 2021-03-08 01:31:26 +0100 |
---|---|---|
committer | Joffrey Bion <joffrey.bion@gmail.com> | 2021-03-08 01:31:59 +0100 |
commit | 3f8a672dcfefdd9304ba8b489865cb12153a75e3 (patch) | |
tree | 3b5e091d37b9f821e189ec26791db7a4eec177b4 /sw-bot/src/main/kotlin/org/luxons | |
parent | Make SayReady action an object (diff) | |
download | seven-wonders-3f8a672dcfefdd9304ba8b489865cb12153a75e3.tar.gz seven-wonders-3f8a672dcfefdd9304ba8b489865cb12153a75e3.tar.bz2 seven-wonders-3f8a672dcfefdd9304ba8b489865cb12153a75e3.zip |
Make turnInfo generic in its action type
Diffstat (limited to 'sw-bot/src/main/kotlin/org/luxons')
-rw-r--r-- | sw-bot/src/main/kotlin/org/luxons/sevenwonders/bot/SevenWondersBot.kt | 18 |
1 files changed, 10 insertions, 8 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) |