diff options
author | Joffrey Bion <joffrey.bion@booking.com> | 2020-05-28 20:26:29 +0200 |
---|---|---|
committer | Joffrey Bion <joffrey.bion@booking.com> | 2020-05-31 11:16:00 +0200 |
commit | 36103b92ce4b6cd00925d861f4fb5e381cdc4833 (patch) | |
tree | 4c2bfced8317526b24da23fcef9febe6368c736e /sw-engine | |
parent | Attempt at fixing race conditions with bot subscriptions (diff) | |
download | seven-wonders-36103b92ce4b6cd00925d861f4fb5e381cdc4833.tar.gz seven-wonders-36103b92ce4b6cd00925d861f4fb5e381cdc4833.tar.bz2 seven-wonders-36103b92ce4b6cd00925d861f4fb5e381cdc4833.zip |
Simplify settings and allow choosing wonders
Diffstat (limited to 'sw-engine')
16 files changed, 59 insertions, 79 deletions
diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/Settings.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/Settings.kt deleted file mode 100644 index 7461762a..00000000 --- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/Settings.kt +++ /dev/null @@ -1,29 +0,0 @@ -package org.luxons.sevenwonders.engine - -import org.luxons.sevenwonders.model.CustomizableSettings -import org.luxons.sevenwonders.model.WonderSide -import org.luxons.sevenwonders.model.WonderSidePickMethod -import kotlin.random.Random - -internal class Settings( - val nbPlayers: Int, - customSettings: CustomizableSettings = CustomizableSettings() -) { - val random: Random = customSettings.randomSeedForTests?.let { Random(it) } ?: Random - val timeLimitInSeconds: Int = customSettings.timeLimitInSeconds - val initialGold: Int = customSettings.initialGold - val discardedCardGold: Int = customSettings.discardedCardGold - val defaultTradingCost: Int = customSettings.defaultTradingCost - val pointsPer3Gold: Int = customSettings.pointsPer3Gold - val lostPointsPerDefeat: Int = customSettings.lostPointsPerDefeat - val wonPointsPerVictoryPerAge: Map<Int, Int> = customSettings.wonPointsPerVictoryPerAge - - private val wonderSidePickMethod: WonderSidePickMethod = customSettings.wonderSidePickMethod - private var lastPickedSide: WonderSide? = null - - fun pickWonderSide(): WonderSide { - val newSide = wonderSidePickMethod.pickSide(random, lastPickedSide) - lastPickedSide = newSide - return newSide - } -} diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/boards/Board.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/boards/Board.kt index 3f8f0437..a78d6d4b 100644 --- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/boards/Board.kt +++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/boards/Board.kt @@ -1,7 +1,6 @@ package org.luxons.sevenwonders.engine.boards import org.luxons.sevenwonders.engine.Player -import org.luxons.sevenwonders.engine.Settings import org.luxons.sevenwonders.engine.cards.Card import org.luxons.sevenwonders.engine.effects.SpecialAbility import org.luxons.sevenwonders.engine.resources.Production @@ -9,6 +8,7 @@ import org.luxons.sevenwonders.engine.resources.TradingRules import org.luxons.sevenwonders.engine.resources.mutableResourcesOf import org.luxons.sevenwonders.engine.wonders.Wonder import org.luxons.sevenwonders.model.Age +import org.luxons.sevenwonders.model.Settings import org.luxons.sevenwonders.model.cards.Color import org.luxons.sevenwonders.model.score.PlayerScore import org.luxons.sevenwonders.model.score.ScoreCategory diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/data/GameDefinition.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/data/GameDefinition.kt index 962b88e1..b33c9d50 100644 --- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/data/GameDefinition.kt +++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/data/GameDefinition.kt @@ -4,7 +4,6 @@ import com.github.salomonbrys.kotson.typeToken import com.google.gson.Gson import com.google.gson.GsonBuilder import org.luxons.sevenwonders.engine.Game -import org.luxons.sevenwonders.engine.Settings import org.luxons.sevenwonders.engine.boards.Board import org.luxons.sevenwonders.engine.data.definitions.DecksDefinition import org.luxons.sevenwonders.engine.data.definitions.WonderDefinition @@ -23,7 +22,9 @@ import org.luxons.sevenwonders.engine.effects.ScienceProgress import org.luxons.sevenwonders.engine.resources.Production import org.luxons.sevenwonders.engine.resources.Resources import org.luxons.sevenwonders.model.Age -import org.luxons.sevenwonders.model.CustomizableSettings +import org.luxons.sevenwonders.model.wonders.AssignedWonder +import org.luxons.sevenwonders.model.Settings +import org.luxons.sevenwonders.model.wonders.PreGameWonder import org.luxons.sevenwonders.model.resources.ResourceType internal const val LAST_AGE: Age = 3 @@ -35,23 +36,30 @@ internal data class GlobalRules( class GameDefinition internal constructor( rules: GlobalRules, - private val wonders: List<WonderDefinition>, + wonderDefinitions: List<WonderDefinition>, private val decksDefinition: DecksDefinition ) { val minPlayers: Int = rules.minPlayers val maxPlayers: Int = rules.maxPlayers - fun initGame(id: Long, customSettings: CustomizableSettings, nbPlayers: Int): Game { - val settings = Settings(nbPlayers, customSettings) - val boards = assignBoards(settings, nbPlayers) - val decks = decksDefinition.prepareDecks(settings.nbPlayers, settings.random) + val allWonders: List<PreGameWonder> = wonderDefinitions.map { w -> + PreGameWonder( + w.name, + w.sides.mapValues { (_, def) -> def.image }) + } + + private val wondersByName = wonderDefinitions.associateBy { it.name } + + fun createGame(id: Long, wonders: Collection<AssignedWonder>, settings: Settings): Game { + val nbPlayers = wonders.size + val boards = wonders.mapIndexed { index, wonder -> wonder.createBoard(index, settings) } + val decks = decksDefinition.prepareDecks(nbPlayers, settings.random) return Game(id, settings, boards, decks) } - private fun assignBoards(settings: Settings, nbPlayers: Int): List<Board> { - return wonders.shuffled(settings.random) - .take(nbPlayers) - .mapIndexed { i, wDef -> Board(wDef.create(settings.pickWonderSide()), i, settings) } + private fun AssignedWonder.createBoard(playerIndex: Int, settings: Settings): Board { + val wonder = wondersByName[name] ?: error("Unknown wonder '$name'") + return Board(wonder.create(side), playerIndex, settings) } companion object { diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/data/definitions/WonderDefinition.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/data/definitions/WonderDefinition.kt index 4c5c8e09..58964e83 100644 --- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/data/definitions/WonderDefinition.kt +++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/data/definitions/WonderDefinition.kt @@ -3,12 +3,13 @@ package org.luxons.sevenwonders.engine.data.definitions import org.luxons.sevenwonders.engine.cards.Requirements import org.luxons.sevenwonders.engine.wonders.Wonder import org.luxons.sevenwonders.engine.wonders.WonderStage -import org.luxons.sevenwonders.model.WonderSide +import org.luxons.sevenwonders.model.wonders.WonderName +import org.luxons.sevenwonders.model.wonders.WonderSide import org.luxons.sevenwonders.model.resources.ResourceType internal class WonderDefinition( - private val name: String, - private val sides: Map<WonderSide, WonderSideDefinition> + val name: WonderName, + val sides: Map<WonderSide, WonderSideDefinition> ) { fun create(wonderSide: WonderSide): Wonder = sides[wonderSide]!!.createWonder(name) } @@ -16,7 +17,7 @@ internal class WonderDefinition( internal class WonderSideDefinition( private val initialResource: ResourceType, private val stages: List<WonderStageDefinition>, - private val image: String + val image: String ) { fun createWonder(name: String): Wonder = Wonder(name, initialResource, stages.map { it.create() }, image) } diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/BuildWonderMove.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/BuildWonderMove.kt index 942d9db1..aaa26f7e 100644 --- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/BuildWonderMove.kt +++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/BuildWonderMove.kt @@ -1,8 +1,8 @@ package org.luxons.sevenwonders.engine.moves import org.luxons.sevenwonders.engine.PlayerContext -import org.luxons.sevenwonders.engine.Settings import org.luxons.sevenwonders.engine.cards.Card +import org.luxons.sevenwonders.model.Settings import org.luxons.sevenwonders.model.PlayerMove internal class BuildWonderMove(move: PlayerMove, card: Card, player: PlayerContext) : diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/CopyGuildMove.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/CopyGuildMove.kt index 9cb57481..c3ba8e9a 100644 --- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/CopyGuildMove.kt +++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/CopyGuildMove.kt @@ -1,9 +1,9 @@ package org.luxons.sevenwonders.engine.moves import org.luxons.sevenwonders.engine.PlayerContext -import org.luxons.sevenwonders.engine.Settings import org.luxons.sevenwonders.engine.cards.Card import org.luxons.sevenwonders.engine.effects.SpecialAbility +import org.luxons.sevenwonders.model.Settings import org.luxons.sevenwonders.model.PlayerMove import org.luxons.sevenwonders.model.boards.RelativeBoardPosition import org.luxons.sevenwonders.model.cards.Color diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/DiscardMove.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/DiscardMove.kt index a8f2aebf..92fb4952 100644 --- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/DiscardMove.kt +++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/DiscardMove.kt @@ -1,8 +1,8 @@ package org.luxons.sevenwonders.engine.moves import org.luxons.sevenwonders.engine.PlayerContext -import org.luxons.sevenwonders.engine.Settings import org.luxons.sevenwonders.engine.cards.Card +import org.luxons.sevenwonders.model.Settings import org.luxons.sevenwonders.model.PlayerMove internal class DiscardMove(move: PlayerMove, card: Card, player: PlayerContext) : diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/Move.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/Move.kt index 1035c1b8..a1790baa 100644 --- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/Move.kt +++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/Move.kt @@ -1,8 +1,8 @@ package org.luxons.sevenwonders.engine.moves import org.luxons.sevenwonders.engine.PlayerContext -import org.luxons.sevenwonders.engine.Settings import org.luxons.sevenwonders.engine.cards.Card +import org.luxons.sevenwonders.model.Settings import org.luxons.sevenwonders.model.MoveType import org.luxons.sevenwonders.model.PlayerMove import org.luxons.sevenwonders.model.resources.ResourceTransactions diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/PlayCardMove.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/PlayCardMove.kt index 17b1a48a..250424c7 100644 --- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/PlayCardMove.kt +++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/PlayCardMove.kt @@ -1,8 +1,8 @@ package org.luxons.sevenwonders.engine.moves import org.luxons.sevenwonders.engine.PlayerContext -import org.luxons.sevenwonders.engine.Settings import org.luxons.sevenwonders.engine.cards.Card +import org.luxons.sevenwonders.model.Settings import org.luxons.sevenwonders.model.PlayerMove internal class PlayCardMove(move: PlayerMove, card: Card, player: PlayerContext) : diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/PlayFreeCardMove.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/PlayFreeCardMove.kt index 310500c7..acaa2c95 100644 --- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/PlayFreeCardMove.kt +++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/PlayFreeCardMove.kt @@ -1,8 +1,8 @@ package org.luxons.sevenwonders.engine.moves import org.luxons.sevenwonders.engine.PlayerContext -import org.luxons.sevenwonders.engine.Settings import org.luxons.sevenwonders.engine.cards.Card +import org.luxons.sevenwonders.model.Settings import org.luxons.sevenwonders.model.PlayerMove internal class PlayFreeCardMove(move: PlayerMove, card: Card, playerContext: PlayerContext) : diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/PlayFreeDiscardedCardMove.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/PlayFreeDiscardedCardMove.kt index 5147ed82..2fc03c92 100644 --- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/PlayFreeDiscardedCardMove.kt +++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/moves/PlayFreeDiscardedCardMove.kt @@ -1,9 +1,9 @@ package org.luxons.sevenwonders.engine.moves import org.luxons.sevenwonders.engine.PlayerContext -import org.luxons.sevenwonders.engine.Settings import org.luxons.sevenwonders.engine.cards.Card import org.luxons.sevenwonders.engine.effects.SpecialAbility +import org.luxons.sevenwonders.model.Settings import org.luxons.sevenwonders.model.PlayerMove internal class PlayFreeDiscardedCardMove( 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 51a6376a..595ca1e0 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 @@ -1,17 +1,16 @@ package org.luxons.sevenwonders.engine -import org.luxons.sevenwonders.model.Action -import org.luxons.sevenwonders.model.PlayedMove -import org.luxons.sevenwonders.model.PlayerMove -import org.luxons.sevenwonders.model.PlayerTurnInfo import org.luxons.sevenwonders.model.cards.HandCard import org.luxons.sevenwonders.model.cards.TableCard import org.luxons.sevenwonders.engine.data.GameDefinition import org.luxons.sevenwonders.engine.data.LAST_AGE -import org.luxons.sevenwonders.model.MoveType +import org.luxons.sevenwonders.engine.test.SEED import org.luxons.sevenwonders.model.resources.ResourceTransactions import org.luxons.sevenwonders.model.resources.noTransactions -import org.luxons.sevenwonders.engine.test.testCustomizableSettings +import org.luxons.sevenwonders.engine.test.testSettings +import org.luxons.sevenwonders.model.* +import org.luxons.sevenwonders.model.wonders.deal +import kotlin.random.Random import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFalse @@ -46,8 +45,10 @@ class GameTest { } } - private fun createGame(nbPlayers: Int): Game = - GameDefinition.load().initGame(0, testCustomizableSettings(), nbPlayers) + private fun createGame(nbPlayers: Int): Game = GameDefinition.load().let { + val wonders = it.allWonders.deal(nbPlayers, random = Random(SEED)) + it.createGame(0, wonders, testSettings()) + } private fun playAge(nbPlayers: Int, game: Game, age: Int) { do { diff --git a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/cards/CardTest.kt b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/cards/CardTest.kt index 8fd673b2..2b0e5462 100644 --- a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/cards/CardTest.kt +++ b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/cards/CardTest.kt @@ -20,7 +20,7 @@ class CardTest { fun playCardCostingMoney() { val initialGold = 3 val price = 1 - val settings = testSettings(3, initialGold) + val settings = testSettings(initialGold) val boards = listOf( Board(Wonder("TestWonder", ResourceType.WOOD, emptyList(), ""), 0, settings), diff --git a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/GameDefinitionTest.kt b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/GameDefinitionTest.kt index b9b7bb3c..d91c3b50 100644 --- a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/GameDefinitionTest.kt +++ b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/data/GameDefinitionTest.kt @@ -1,7 +1,8 @@ package org.luxons.sevenwonders.engine.data import org.junit.Test -import org.luxons.sevenwonders.model.CustomizableSettings +import org.luxons.sevenwonders.model.Settings +import org.luxons.sevenwonders.model.wonders.deal import kotlin.test.assertEquals import kotlin.test.assertNotNull @@ -14,7 +15,8 @@ class GameDefinitionTest { assertEquals(3, gameDefinition.minPlayers) assertEquals(7, gameDefinition.maxPlayers) - val game = gameDefinition.initGame(0, CustomizableSettings(), 7) + val wonders = gameDefinition.allWonders.deal(7) + val game = gameDefinition.createGame(0, wonders, Settings()) assertNotNull(game) } } diff --git a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/moves/BuildWonderMoveTest.kt b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/moves/BuildWonderMoveTest.kt index 764d7890..8ff9aa33 100644 --- a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/moves/BuildWonderMoveTest.kt +++ b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/moves/BuildWonderMoveTest.kt @@ -2,7 +2,6 @@ package org.luxons.sevenwonders.engine.moves import org.junit.Test import org.luxons.sevenwonders.engine.PlayerContext -import org.luxons.sevenwonders.engine.Settings import org.luxons.sevenwonders.model.MoveType import org.luxons.sevenwonders.engine.boards.Table import org.luxons.sevenwonders.engine.cards.Card @@ -11,6 +10,7 @@ import org.luxons.sevenwonders.engine.test.sampleCards import org.luxons.sevenwonders.engine.test.testCard import org.luxons.sevenwonders.engine.test.testSettings import org.luxons.sevenwonders.engine.test.testTable +import org.luxons.sevenwonders.model.Settings import kotlin.test.assertEquals import kotlin.test.assertFailsWith import kotlin.test.fail @@ -31,8 +31,8 @@ class BuildWonderMoveTest { @Test fun init_failsWhenWonderIsCompletelyBuilt() { - val settings = testSettings(3) - val table = testTable(settings) + val settings = testSettings() + val table = testTable(3, settings) val hand = sampleCards(7) fillPlayerWonderLevels(settings, table, hand) @@ -64,8 +64,8 @@ class BuildWonderMoveTest { @Test fun place_increasesWonderLevel() { - val settings = testSettings(3) - val table = testTable(settings) + val settings = testSettings() + val table = testTable(3, settings) val hand = sampleCards(7) val cardToUse = hand[0] val playerContext = PlayerContext(0, table, hand) diff --git a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/test/TestUtils.kt b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/test/TestUtils.kt index cc97dc54..4db574e2 100644 --- a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/test/TestUtils.kt +++ b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/test/TestUtils.kt @@ -2,9 +2,8 @@ package org.luxons.sevenwonders.engine.test import org.luxons.sevenwonders.engine.Player import org.luxons.sevenwonders.engine.PlayerContext -import org.luxons.sevenwonders.engine.Settings import org.luxons.sevenwonders.model.resources.CountedResource -import org.luxons.sevenwonders.model.CustomizableSettings +import org.luxons.sevenwonders.model.Settings import org.luxons.sevenwonders.model.PlayerMove import org.luxons.sevenwonders.engine.boards.Board import org.luxons.sevenwonders.model.boards.RelativeBoardPosition @@ -30,18 +29,16 @@ import org.luxons.sevenwonders.engine.resources.resourcesOf import org.luxons.sevenwonders.engine.wonders.Wonder import org.luxons.sevenwonders.engine.wonders.WonderStage -private const val SEED: Long = 42 +internal const val SEED: Long = 42 -internal fun testCustomizableSettings(initialGold: Int = 0): CustomizableSettings = - CustomizableSettings(randomSeedForTests = SEED).copy(initialGold = initialGold) +internal fun testSettings(initialGold: Int = 0): Settings = Settings( + randomSeedForTests = SEED, + initialGold = initialGold +) -internal fun testSettings(nbPlayers: Int = 5, initialGold: Int = 0): Settings = - Settings(nbPlayers, testCustomizableSettings(initialGold)) +internal fun testTable(nbPlayers: Int = 5): Table = testTable(nbPlayers, testSettings()) -internal fun testTable(nbPlayers: Int = 5): Table = testTable(testSettings(nbPlayers)) - -internal fun testTable(settings: Settings): Table = - Table(testBoards(settings.nbPlayers, settings)) +internal fun testTable(nbPlayers: Int, settings: Settings): Table = Table(testBoards(nbPlayers, settings)) private fun testBoards(count: Int, settings: Settings): List<Board> = List(count) { testBoard(settings) } |