From 880a4fff4232317dd00ab0ee9992a33d459b9862 Mon Sep 17 00:00:00 2001 From: Joffrey BION Date: Wed, 1 Aug 2018 02:24:40 +0200 Subject: Fix comparisons of ResourceTransactions This commit abstracts away the actual type of collection in the ResourceTransactions typealias. Incidentally, it also changes it to Set, so that tests can compare without concerns about order. This also means that duplicates are not tolerated anymore, and I still haven't decided whether this is good or bad. --- .../kotlin/org/luxons/sevenwonders/game/api/PlayerMove.kt | 5 +++-- .../sevenwonders/game/resources/ResourceTransactions.kt | 6 +++--- .../test/kotlin/org/luxons/sevenwonders/game/GameTest.kt | 6 +++--- .../luxons/sevenwonders/game/resources/TradingRulesTest.kt | 8 ++++---- .../kotlin/org/luxons/sevenwonders/game/test/TestUtils.kt | 13 ++++--------- 5 files changed, 17 insertions(+), 21 deletions(-) diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/api/PlayerMove.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/api/PlayerMove.kt index 95e6b13c..22cc36f0 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/api/PlayerMove.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/api/PlayerMove.kt @@ -1,10 +1,11 @@ package org.luxons.sevenwonders.game.api import org.luxons.sevenwonders.game.moves.MoveType -import org.luxons.sevenwonders.game.resources.ResourceTransaction +import org.luxons.sevenwonders.game.resources.ResourceTransactions +import org.luxons.sevenwonders.game.resources.noTransactions data class PlayerMove( val type: MoveType, val cardName: String, - val transactions: Collection = emptyList() + val transactions: ResourceTransactions = noTransactions() ) diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/resources/ResourceTransactions.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/resources/ResourceTransactions.kt index c7ab3636..d45d0ce7 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/resources/ResourceTransactions.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/resources/ResourceTransactions.kt @@ -4,10 +4,10 @@ import org.luxons.sevenwonders.game.Player typealias ResourceTransactions = Collection -fun noTransactions(): ResourceTransactions = emptyList() +fun noTransactions(): ResourceTransactions = emptySet() -fun Map.toTransactions() = - filter { (_, res) -> !res.isEmpty() }.map { (p, res) -> ResourceTransaction(p, res) } +fun Map.toTransactions(): ResourceTransactions = + filter { (_, res) -> !res.isEmpty() }.map { (p, res) -> ResourceTransaction(p, res) }.toSet() fun ResourceTransactions.asResources(): Resources = map { it.resources }.merge() diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/GameTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/GameTest.kt index fb797b77..e1d8b7f9 100644 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/GameTest.kt +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/GameTest.kt @@ -13,10 +13,10 @@ import org.luxons.sevenwonders.game.api.Table import org.luxons.sevenwonders.game.data.GameDefinitionLoader import org.luxons.sevenwonders.game.data.LAST_AGE import org.luxons.sevenwonders.game.moves.MoveType -import org.luxons.sevenwonders.game.resources.ResourceTransaction import org.luxons.sevenwonders.game.resources.ResourceTransactions import org.luxons.sevenwonders.game.resources.Resources import org.luxons.sevenwonders.game.resources.bestSolution +import org.luxons.sevenwonders.game.resources.noTransactions import org.luxons.sevenwonders.game.test.testCustomizableSettings import java.util.HashMap @@ -83,9 +83,9 @@ class GameTest { return PlayerMove(MoveType.DISCARD, firstCardInHand.card.name) } - private fun findResourcesToBuyFor(handCard: HandCard, turnInfo: PlayerTurnInfo): Collection { + private fun findResourcesToBuyFor(handCard: HandCard, turnInfo: PlayerTurnInfo): ResourceTransactions { if (handCard.isFree) { - return emptyList() + return noTransactions() } val requiredResources = handCard.card.requirements.resources val table = turnInfo.table diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/resources/TradingRulesTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/resources/TradingRulesTest.kt index e44c38f3..3e567493 100644 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/resources/TradingRulesTest.kt +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/resources/TradingRulesTest.kt @@ -8,7 +8,6 @@ import org.junit.experimental.theories.Theory import org.junit.runner.RunWith import org.luxons.sevenwonders.game.test.createTransaction import org.luxons.sevenwonders.game.test.createTransactions -import java.util.ArrayList @RunWith(Theories::class) class TradingRulesTest { @@ -102,9 +101,10 @@ class TradingRulesTest { val rules = TradingRules(defaultCost) rules.setCost(type, overriddenProvider, overriddenCost) - val boughtResources = ArrayList(2) - boughtResources.add(createTransaction(provider, type)) - boughtResources.add(createTransaction(overriddenProvider, type)) + val boughtResources = createTransactions( + createTransaction(provider, type), + createTransaction(overriddenProvider, type) + ) assertEquals(defaultCost + overriddenCost, rules.computeCost(boughtResources)) } diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/test/TestUtils.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/test/TestUtils.kt index 383c77fa..c67a7052 100644 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/test/TestUtils.kt +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/test/TestUtils.kt @@ -102,11 +102,11 @@ infix fun Int.of(type: ResourceType): Resources { internal fun createTransactions(provider: Provider, vararg resources: ResourceType): ResourceTransactions { val transaction = createTransaction(provider, *resources) - return listOf(transaction) + return createTransactions(transaction) } internal fun createTransactions(vararg transactions: ResourceTransaction): ResourceTransactions { - return transactions.toList() + return transactions.toSet() } fun createTransaction(provider: Provider, vararg resources: ResourceType): ResourceTransaction { @@ -209,13 +209,8 @@ internal fun playCardWithEffect(player: Player, color: Color, effect: Effect) { card.applyTo(player, noTransactions()) } -internal fun createMove( - context: PlayerContext, - card: Card, - type: MoveType, - vararg transactions: ResourceTransaction -): Move { - val playerMove = PlayerMove(type, card.name, Arrays.asList(*transactions)) +internal fun createMove(context: PlayerContext, card: Card, type: MoveType): Move { + val playerMove = PlayerMove(type, card.name) return type.create(playerMove, card, context) } -- cgit