From 4178ba9700ffd6619f995482a369bd5133276e2e Mon Sep 17 00:00:00 2001 From: Joffrey Bion Date: Thu, 28 May 2020 13:03:36 +0200 Subject: Fix production alternative duplicates Having twice the same choice wasn't supported so far. This can happen with Alexandria's bonuses (4 primary resources, or 3 secondary) associated to the yellow Market/Caravansery cards. Resolves: https://github.com/joffrey-bion/seven-wonders/issues/19 --- .../org/luxons/sevenwonders/engine/converters/Boards.kt | 2 +- .../engine/data/serializers/ProductionSerializer.kt | 4 ++-- .../sevenwonders/engine/resources/BestPriceCalculator.kt | 2 +- .../org/luxons/sevenwonders/engine/resources/Production.kt | 11 ++++++----- 4 files changed, 10 insertions(+), 9 deletions(-) (limited to 'sw-engine/src/main') diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/converters/Boards.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/converters/Boards.kt index 7ff870cf..0d8369ac 100644 --- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/converters/Boards.kt +++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/converters/Boards.kt @@ -73,7 +73,7 @@ internal fun InternalWonderStage.toApiWonderStage(isLastBuiltStage: Boolean, las internal fun InternalProduction.toApiProduction(): ApiProduction = ApiProduction( fixedResources = getFixedResources().toCountedResourcesList(), - alternativeResources = getAlternativeResources() + alternativeResources = getAlternativeResources().sortedBy { it.size } ) internal fun InternalRequirements.toApiRequirements(): ApiRequirements = diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/data/serializers/ProductionSerializer.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/data/serializers/ProductionSerializer.kt index 467b0912..c98f20a8 100644 --- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/data/serializers/ProductionSerializer.kt +++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/data/serializers/ProductionSerializer.kt @@ -24,14 +24,14 @@ internal class ProductionSerializer : JsonSerializer, JsonDeserializ } } - private fun serializeAsChoice(choices: Set>, context: JsonSerializationContext): JsonElement { + private fun serializeAsChoice(choices: List>, context: JsonSerializationContext): JsonElement { if (choices.isEmpty()) { return JsonNull.INSTANCE } if (choices.size > 1) { throw IllegalArgumentException("Cannot serialize a production with more than one choice") } - val str = choices.flatten().map { it.symbol }.joinToString("/") + val str = choices[0].map { it.symbol }.joinToString("/") return context.serialize(str) } diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/resources/BestPriceCalculator.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/resources/BestPriceCalculator.kt index 85b07a80..dd2d2cae 100644 --- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/resources/BestPriceCalculator.kt +++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/resources/BestPriceCalculator.kt @@ -15,7 +15,7 @@ data class TransactionPlan(val price: Int, val possibleTransactions: Set> + choices: List> ) { val choices: Set> = choices.mapTo(HashSet()) { it.toMutableSet() } diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/resources/Production.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/resources/Production.kt index 46ec66f7..4f9bbe90 100644 --- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/resources/Production.kt +++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/resources/Production.kt @@ -5,11 +5,12 @@ import java.util.EnumSet data class Production internal constructor( private val fixedResources: MutableResources = mutableResourcesOf(), - private val alternativeResources: MutableSet> = mutableSetOf() + // cannot be a Set because the same choices can be there multiple times + private val alternativeResources: MutableList> = mutableListOf() ) { fun getFixedResources(): Resources = fixedResources - fun getAlternativeResources(): Set> = alternativeResources + fun getAlternativeResources(): List> = alternativeResources fun addFixedResource(type: ResourceType, quantity: Int) = fixedResources.add(type, quantity) @@ -25,8 +26,8 @@ data class Production internal constructor( alternativeResources.addAll(production.getAlternativeResources()) } - internal fun asChoices(): Set> { - val fixedAsChoices = fixedResources.toList().mapTo(HashSet()) { EnumSet.of(it) } + internal fun asChoices(): List> { + val fixedAsChoices = fixedResources.toList().map { EnumSet.of(it) } return fixedAsChoices + alternativeResources } @@ -42,7 +43,7 @@ data class Production internal constructor( private fun containedInAlternatives( resources: MutableResources, - alternatives: MutableSet> + alternatives: MutableList> ): Boolean { if (resources.isEmpty()) { return true -- cgit