Skip to content

Commit

Permalink
Use the new enum entries property instead of values()
Browse files Browse the repository at this point in the history
  • Loading branch information
joffrey-bion committed Jul 8, 2023
1 parent acca482 commit d9721bf
Show file tree
Hide file tree
Showing 21 changed files with 38 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ enum class ResourceType(val symbol: Char) {

companion object {

private val typesPerSymbol = values().associateBy { it.symbol }
private val typesPerSymbol = entries.associateBy { it.symbol }

fun fromSymbol(symbol: String): ResourceType {
if (symbol.length != 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ enum class WonderSide {
fun List<PreGameWonder>.deal(nbPlayers: Int, random: Random = Random): List<AssignedWonder> =
shuffled(random).take(nbPlayers).map { it.withRandomSide(random) }

fun PreGameWonder.withRandomSide(random: Random = Random): AssignedWonder = withSide(WonderSide.values().random(random))
fun PreGameWonder.withRandomSide(random: Random = Random): AssignedWonder = withSide(WonderSide.entries.random(random))

fun PreGameWonder.withSide(side: WonderSide): AssignedWonder = AssignedWonder(name, side)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ internal class Science {
fun getQuantity(type: ScienceType): Int = quantities.getOrDefault(type, 0)

fun computePoints(): Int {
val values = ScienceType.values().map(::getQuantity).toMutableList()
val values = ScienceType.entries.map(::getQuantity).toMutableList()
return computePoints(values, jokers)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal object ScienceProgressSerializer : KSerializer<ScienceProgress> {
encoder.encodeString("any")
return
}
for (type in ScienceType.values()) {
for (type in ScienceType.entries) {
val quantity = value.science.getQuantity(type)
if (quantity == 1) {
encoder.encodeSerializableValue(serializer(), type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ data class Production internal constructor(
if (resources.isEmpty()) {
return true
}
for (type in ResourceType.values()) {
for (type in ResourceType.entries) {
if (resources[type] <= 0) {
continue
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ interface Resources {
fun containsAll(resources: Resources): Boolean = resources.quantities.all { it.value <= this[it.key] }

operator fun plus(resources: Resources): Resources =
ResourceType.values().map { it to this[it] + resources[it] }.toResources()
ResourceType.entries.map { it to this[it] + resources[it] }.toResources()

/**
* Returns new resources containing these resources minus the given [resources]. If the given resources contain
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private class TransactionOptionsCalculator(resourcesToPay: Resources, player: Pl
// we only take alternative resources here, because fixed resources were already removed for optimization
val ownBoardChoices = player.board.production.getAlternativeResources()
val ownPool = ResourcePool(null, player.board.tradingRules, ownBoardChoices)
val providerPools = Provider.values().map { it.toResourcePoolFor(player) }
val providerPools = Provider.entries.map { it.toResourcePoolFor(player) }

return providerPools + ownPool
}
Expand All @@ -60,7 +60,7 @@ private class TransactionOptionsCalculator(resourcesToPay: Resources, player: Pl
addCurrentOption()
return
}
for (type in ResourceType.values()) {
for (type in ResourceType.entries) {
if (resourcesLeftToPay[type] > 0) {
for (pool in pools) {
if (pool.provider == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import org.luxons.sevenwonders.model.boards.RelativeBoardPosition
import org.luxons.sevenwonders.model.cards.Color
import org.luxons.sevenwonders.model.resources.ResourceType
import org.luxons.sevenwonders.model.score.ScoreCategory
import kotlin.enums.EnumEntries
import kotlin.test.assertFailsWith
import kotlin.test.assertFalse
import kotlin.test.assertSame
Expand Down Expand Up @@ -215,14 +216,14 @@ class BoardTest {

@JvmStatic
@DataPoints
fun resourceTypes(): Array<ResourceType> = ResourceType.values()
fun resourceTypes(): EnumEntries<ResourceType> = ResourceType.entries

@JvmStatic
@DataPoints
fun colors(): Array<Color> = Color.values()
fun colors(): EnumEntries<Color> = Color.entries

@JvmStatic
@DataPoints
fun specialAbilities(): Array<SpecialAbility> = SpecialAbility.values()
fun specialAbilities(): EnumEntries<SpecialAbility> = SpecialAbility.entries
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import org.luxons.sevenwonders.engine.test.testBoard
import org.luxons.sevenwonders.model.resources.Provider
import org.luxons.sevenwonders.model.resources.ResourceType
import org.luxons.sevenwonders.model.resources.noTransactions
import kotlin.enums.EnumEntries
import kotlin.test.assertEquals
import kotlin.test.assertSame
import kotlin.test.assertTrue
Expand Down Expand Up @@ -190,6 +191,6 @@ class RequirementsTest {

@JvmStatic
@DataPoints
fun resourceTypes(): Array<ResourceType> = ResourceType.values()
fun resourceTypes(): EnumEntries<ResourceType> = ResourceType.entries
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ class ResourceTypeSerializerTest {

@Test
fun serialize_useSymbolForEachType() {
ResourceType.values().forEach { type ->
ResourceType.entries.forEach { type ->
val expectedjson = "\"" + type.symbol + "\""
assertEquals(expectedjson, Json.encodeToString(ResourceTypeSerializer, type))
}
}

@Test
fun deserialize_useSymbolForEachType() {
ResourceType.values().forEach { type ->
ResourceType.entries.forEach { type ->
val typeInjson = "\"" + type.symbol + "\""
assertEquals(type, Json.decodeFromString(ResourceTypeSerializer, typeInjson))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.luxons.sevenwonders.engine.test.testTable
import org.luxons.sevenwonders.model.boards.RelativeBoardPosition
import org.luxons.sevenwonders.model.cards.CardBack
import org.luxons.sevenwonders.model.cards.Color
import kotlin.enums.EnumEntries
import kotlin.test.assertEquals

@RunWith(Theories::class)
Expand Down Expand Up @@ -133,10 +134,10 @@ class BonusPerBoardElementTest {

@JvmStatic
@DataPoints
fun colors(): Array<Color> = Color.values()
fun colors(): EnumEntries<Color> = Color.entries

@JvmStatic
@DataPoints
fun positions(): Array<RelativeBoardPosition> = RelativeBoardPosition.values()
fun positions(): EnumEntries<RelativeBoardPosition> = RelativeBoardPosition.entries
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.luxons.sevenwonders.engine.test.createTransactions
import org.luxons.sevenwonders.engine.test.testBoard
import org.luxons.sevenwonders.model.resources.Provider
import org.luxons.sevenwonders.model.resources.ResourceType
import kotlin.enums.EnumEntries
import kotlin.test.assertEquals

@RunWith(Theories::class)
Expand Down Expand Up @@ -60,10 +61,10 @@ class DiscountTest {

@JvmStatic
@DataPoints
fun resourceTypes(): Array<ResourceType> = ResourceType.values()
fun resourceTypes(): EnumEntries<ResourceType> = ResourceType.entries

@JvmStatic
@DataPoints
fun providers(): Array<Provider> = Provider.values()
fun providers(): EnumEntries<Provider> = Provider.entries
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.luxons.sevenwonders.engine.SimplePlayer
import org.luxons.sevenwonders.engine.test.testBoard
import org.luxons.sevenwonders.engine.test.testTable
import org.luxons.sevenwonders.model.resources.ResourceType
import kotlin.enums.EnumEntries
import kotlin.test.assertEquals

@RunWith(Theories::class)
Expand Down Expand Up @@ -38,6 +39,6 @@ class GoldIncreaseTest {

@JvmStatic
@DataPoints
fun resourceTypes(): Array<ResourceType> = ResourceType.values()
fun resourceTypes(): EnumEntries<ResourceType> = ResourceType.entries
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.luxons.sevenwonders.engine.SimplePlayer
import org.luxons.sevenwonders.engine.test.testBoard
import org.luxons.sevenwonders.engine.test.testTable
import org.luxons.sevenwonders.model.resources.ResourceType
import kotlin.enums.EnumEntries
import kotlin.test.assertEquals

@RunWith(Theories::class)
Expand Down Expand Up @@ -39,6 +40,6 @@ class MilitaryReinforcementsTest {

@JvmStatic
@DataPoints
fun resourceTypes(): Array<ResourceType> = ResourceType.values()
fun resourceTypes(): EnumEntries<ResourceType> = ResourceType.entries
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.luxons.sevenwonders.engine.test.fixedProduction
import org.luxons.sevenwonders.engine.test.testBoard
import org.luxons.sevenwonders.engine.test.testTable
import org.luxons.sevenwonders.model.resources.ResourceType
import kotlin.enums.EnumEntries
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue
Expand Down Expand Up @@ -68,6 +69,6 @@ class ProductionIncreaseTest {

@JvmStatic
@DataPoints
fun resourceTypes(): Array<ResourceType> = ResourceType.values()
fun resourceTypes(): EnumEntries<ResourceType> = ResourceType.entries
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.luxons.sevenwonders.engine.test.createGuildCard
import org.luxons.sevenwonders.engine.test.testTable
import org.luxons.sevenwonders.model.boards.RelativeBoardPosition
import org.luxons.sevenwonders.model.cards.Color
import kotlin.enums.EnumEntries
import kotlin.test.assertEquals
import kotlin.test.assertTrue

Expand Down Expand Up @@ -64,7 +65,7 @@ class SpecialAbilityActivationTest {

@JvmStatic
@DataPoints
fun abilities(): Array<SpecialAbility> = SpecialAbility.values()
fun abilities(): EnumEntries<SpecialAbility> = SpecialAbility.entries

@JvmStatic
@DataPoints
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ResourcesTest {
@Test
fun init_shouldBeEmpty() {
val resources = emptyResources()
for (resourceType in ResourceType.values()) {
for (resourceType in ResourceType.entries) {
assertEquals(0, resources[resourceType])
}
assertEquals(0, resources.size)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.luxons.sevenwonders.engine.test.createTransactions
import org.luxons.sevenwonders.model.resources.Provider
import org.luxons.sevenwonders.model.resources.ResourceType
import org.luxons.sevenwonders.model.resources.noTransactions
import kotlin.enums.EnumEntries
import kotlin.test.assertEquals

@RunWith(Theories::class)
Expand Down Expand Up @@ -120,10 +121,10 @@ class TradingRulesTest {

@JvmStatic
@DataPoints
fun providers(): Array<Provider> = Provider.values()
fun providers(): EnumEntries<Provider> = Provider.entries

@JvmStatic
@DataPoints
fun resourceTypes(): Array<ResourceType> = ResourceType.values()
fun resourceTypes(): EnumEntries<ResourceType> = ResourceType.entries
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ internal fun addCards(board: Board, nbCards: Int, color: Color) {
}

internal fun getDifferentColorFrom(vararg colors: Color): Color =
Color.values().firstOrNull { it !in colors } ?: throw IllegalArgumentException("All colors are forbidden!")
Color.entries.firstOrNull { it !in colors } ?: throw IllegalArgumentException("All colors are forbidden!")

internal fun createScienceProgress(compasses: Int, wheels: Int, tablets: Int, jokers: Int): ScienceProgress =
ScienceProgress(createScience(compasses, wheels, tablets, jokers))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private val ScoreTable = FC<ScoreTableProps>("ScoreTable") { props ->
fullCenterInlineStyle()
+"Score"
}
ScoreCategory.values().forEach {
ScoreCategory.entries.forEach {
th {
fullCenterInlineStyle()
+it.title
Expand Down Expand Up @@ -132,7 +132,7 @@ private val ScoreTable = FC<ScoreTableProps>("ScoreTable") { props ->
+"${score.totalPoints}"
}
}
ScoreCategory.values().forEach { cat ->
ScoreCategory.entries.forEach { cat ->
td {
fullCenterInlineStyle()
BpTag {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ enum class SwRoute(val path: String) {
GAME("/game");

companion object {
private val all = values().associateBy { it.path }
private val all = entries.associateBy { it.path }

fun from(path: String) = all.getValue(path)
}
Expand Down

0 comments on commit d9721bf

Please sign in to comment.