diff options
author | jbion <joffrey.bion@amadeus.com> | 2019-02-18 17:54:33 +0100 |
---|---|---|
committer | jbion <joffrey.bion@amadeus.com> | 2019-02-18 17:54:33 +0100 |
commit | 86f7101b6e6bafd5cbfb0b1db2d162a4510a097e (patch) | |
tree | 5a74a64e4ba7b705900be22ba1b71cd60c49fb05 /game-engine/src/main | |
parent | Reduce visibility where appropriate (diff) | |
download | seven-wonders-86f7101b6e6bafd5cbfb0b1db2d162a4510a097e.tar.gz seven-wonders-86f7101b6e6bafd5cbfb0b1db2d162a4510a097e.tar.bz2 seven-wonders-86f7101b6e6bafd5cbfb0b1db2d162a4510a097e.zip |
Simplify game init API
Diffstat (limited to 'game-engine/src/main')
8 files changed, 97 insertions, 108 deletions
diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/Settings.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/Settings.kt index 87ce323a..3cb14083 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/Settings.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/Settings.kt @@ -1,8 +1,8 @@ package org.luxons.sevenwonders.game import org.luxons.sevenwonders.game.api.CustomizableSettings -import org.luxons.sevenwonders.game.data.WonderSide -import org.luxons.sevenwonders.game.data.WonderSidePickMethod +import org.luxons.sevenwonders.game.data.definitions.WonderSide +import org.luxons.sevenwonders.game.api.WonderSidePickMethod import java.util.Random internal class Settings( diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/api/CustomizableSettings.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/api/CustomizableSettings.kt index e4efb9e3..eb72f971 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/api/CustomizableSettings.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/api/CustomizableSettings.kt @@ -1,6 +1,7 @@ package org.luxons.sevenwonders.game.api -import org.luxons.sevenwonders.game.data.WonderSidePickMethod +import org.luxons.sevenwonders.game.data.definitions.WonderSide +import java.util.Random data class CustomizableSettings( val randomSeedForTests: Long? = null, @@ -13,3 +14,28 @@ data class CustomizableSettings( val lostPointsPerDefeat: Int = 1, val wonPointsPerVictoryPerAge: Map<Int, Int> = mapOf(1 to 1, 2 to 3, 3 to 5) ) + +enum class WonderSidePickMethod { + ALL_A { + override fun pickSide(random: Random, lastPickedSide: WonderSide?): WonderSide { + return WonderSide.A + } + }, + ALL_B { + override fun pickSide(random: Random, lastPickedSide: WonderSide?): WonderSide { + return WonderSide.B + } + }, + EACH_RANDOM { + override fun pickSide(random: Random, lastPickedSide: WonderSide?): WonderSide { + return if (random.nextBoolean()) WonderSide.A else WonderSide.B + } + }, + SAME_RANDOM_FOR_ALL { + override fun pickSide(random: Random, lastPickedSide: WonderSide?): WonderSide { + return lastPickedSide ?: if (random.nextBoolean()) WonderSide.A else WonderSide.B + } + }; + + abstract fun pickSide(random: Random, lastPickedSide: WonderSide?): WonderSide +} diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/GameDefinition.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/GameDefinition.kt index 9c0ac87a..5a3c097f 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/GameDefinition.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/GameDefinition.kt @@ -1,11 +1,38 @@ package org.luxons.sevenwonders.game.data +import com.github.salomonbrys.kotson.typeToken +import com.google.gson.Gson +import com.google.gson.GsonBuilder import org.luxons.sevenwonders.game.Game import org.luxons.sevenwonders.game.Settings import org.luxons.sevenwonders.game.api.CustomizableSettings import org.luxons.sevenwonders.game.boards.Board import org.luxons.sevenwonders.game.data.definitions.DecksDefinition import org.luxons.sevenwonders.game.data.definitions.WonderDefinition +import org.luxons.sevenwonders.game.data.serializers.NumericEffectSerializer +import org.luxons.sevenwonders.game.data.serializers.ProductionIncreaseSerializer +import org.luxons.sevenwonders.game.data.serializers.ProductionSerializer +import org.luxons.sevenwonders.game.data.serializers.ResourceTypeSerializer +import org.luxons.sevenwonders.game.data.serializers.ResourceTypesSerializer +import org.luxons.sevenwonders.game.data.serializers.ResourcesSerializer +import org.luxons.sevenwonders.game.data.serializers.ScienceProgressSerializer +import org.luxons.sevenwonders.game.effects.GoldIncrease +import org.luxons.sevenwonders.game.effects.MilitaryReinforcements +import org.luxons.sevenwonders.game.effects.ProductionIncrease +import org.luxons.sevenwonders.game.effects.RawPointsIncrease +import org.luxons.sevenwonders.game.effects.ScienceProgress +import org.luxons.sevenwonders.game.resources.Production +import org.luxons.sevenwonders.game.resources.ResourceType +import org.luxons.sevenwonders.game.resources.Resources + +typealias Age = Int + +internal const val LAST_AGE: Age = 3 + +internal data class GlobalRules( + val minPlayers: Int, + val maxPlayers: Int +) class GameDefinition internal constructor( rules: GlobalRules, @@ -27,4 +54,40 @@ class GameDefinition internal constructor( .take(nbPlayers) .mapIndexed { i, wDef -> Board(wDef.create(settings.pickWonderSide()), i, settings) } } + + companion object { + + fun load(): GameDefinition { + val gson: Gson = createGson() + val rules = loadJson("global_rules.json", GlobalRules::class.java, gson) + val wonders = loadJson("wonders.json", Array<WonderDefinition>::class.java, gson) + val decksDefinition = loadJson("cards.json", DecksDefinition::class.java, gson) + return GameDefinition(rules, wonders.toList(), decksDefinition) + } + } } + +private fun <T> loadJson(filename: String, clazz: Class<T>, gson: Gson): T { + val packageAsPath = GameDefinition::class.java.`package`.name.replace('.', '/') + val resourcePath = "/$packageAsPath/$filename" + val resource = GameDefinition::class.java.getResource(resourcePath) + val json = resource.readText() + return gson.fromJson(json, clazz) +} + +private fun createGson(): Gson { + return GsonBuilder().disableHtmlEscaping() + .registerTypeAdapter<Resources>(ResourcesSerializer()) + .registerTypeAdapter<ResourceType>(ResourceTypeSerializer()) + .registerTypeAdapter<List<ResourceType>>(ResourceTypesSerializer()) + .registerTypeAdapter<Production>(ProductionSerializer()) + .registerTypeAdapter<ProductionIncrease>(ProductionIncreaseSerializer()) + .registerTypeAdapter<MilitaryReinforcements>(NumericEffectSerializer()) + .registerTypeAdapter<RawPointsIncrease>(NumericEffectSerializer()) + .registerTypeAdapter<GoldIncrease>(NumericEffectSerializer()) + .registerTypeAdapter<ScienceProgress>(ScienceProgressSerializer()) + .create() +} + +private inline fun <reified T : Any> GsonBuilder.registerTypeAdapter(typeAdapter: Any): GsonBuilder = + this.registerTypeAdapter(typeToken<T>(), typeAdapter) diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/GameDefinitionLoader.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/GameDefinitionLoader.kt deleted file mode 100644 index fc0f8a71..00000000 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/GameDefinitionLoader.kt +++ /dev/null @@ -1,60 +0,0 @@ -package org.luxons.sevenwonders.game.data - -import com.github.salomonbrys.kotson.typeToken -import com.google.gson.Gson -import com.google.gson.GsonBuilder -import org.luxons.sevenwonders.game.data.definitions.DecksDefinition -import org.luxons.sevenwonders.game.data.definitions.WonderDefinition -import org.luxons.sevenwonders.game.data.serializers.NumericEffectSerializer -import org.luxons.sevenwonders.game.data.serializers.ProductionIncreaseSerializer -import org.luxons.sevenwonders.game.data.serializers.ProductionSerializer -import org.luxons.sevenwonders.game.data.serializers.ResourceTypeSerializer -import org.luxons.sevenwonders.game.data.serializers.ResourceTypesSerializer -import org.luxons.sevenwonders.game.data.serializers.ResourcesSerializer -import org.luxons.sevenwonders.game.data.serializers.ScienceProgressSerializer -import org.luxons.sevenwonders.game.effects.GoldIncrease -import org.luxons.sevenwonders.game.effects.MilitaryReinforcements -import org.luxons.sevenwonders.game.effects.ProductionIncrease -import org.luxons.sevenwonders.game.effects.RawPointsIncrease -import org.luxons.sevenwonders.game.effects.ScienceProgress -import org.luxons.sevenwonders.game.resources.Production -import org.luxons.sevenwonders.game.resources.ResourceType -import org.luxons.sevenwonders.game.resources.Resources - -object GameDefinitionLoader { - - val gameDefinition: GameDefinition by lazy { load() } - - private fun load(): GameDefinition { - val gson: Gson = createGson() - val rules = loadJson("global_rules.json", GlobalRules::class.java, gson) - val wonders = loadJson("wonders.json", Array<WonderDefinition>::class.java, gson) - val decksDefinition = loadJson("cards.json", DecksDefinition::class.java, gson) - return GameDefinition(rules, wonders.toList(), decksDefinition) - } - - private fun <T> loadJson(filename: String, clazz: Class<T>, gson: Gson): T { - val packageAsPath = GameDefinitionLoader::class.java.`package`.name.replace('.', '/') - val resourcePath = "/$packageAsPath/$filename" - val resource = GameDefinitionLoader::class.java.getResource(resourcePath) - val json = resource.readText() - return gson.fromJson(json, clazz) - } - - private fun createGson(): Gson { - return GsonBuilder().disableHtmlEscaping() - .registerTypeAdapter<Resources>(ResourcesSerializer()) - .registerTypeAdapter<ResourceType>(ResourceTypeSerializer()) - .registerTypeAdapter<List<ResourceType>>(ResourceTypesSerializer()) - .registerTypeAdapter<Production>(ProductionSerializer()) - .registerTypeAdapter<ProductionIncrease>(ProductionIncreaseSerializer()) - .registerTypeAdapter<MilitaryReinforcements>(NumericEffectSerializer()) - .registerTypeAdapter<RawPointsIncrease>(NumericEffectSerializer()) - .registerTypeAdapter<GoldIncrease>(NumericEffectSerializer()) - .registerTypeAdapter<ScienceProgress>(ScienceProgressSerializer()) - .create() - } - - private inline fun <reified T : Any> GsonBuilder.registerTypeAdapter(typeAdapter: Any): GsonBuilder = - this.registerTypeAdapter(typeToken<T>(), typeAdapter) -} diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/GlobalRules.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/GlobalRules.kt deleted file mode 100644 index a590efc9..00000000 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/GlobalRules.kt +++ /dev/null @@ -1,10 +0,0 @@ -package org.luxons.sevenwonders.game.data - -typealias Age = Int - -const val LAST_AGE: Age = 3 - -internal data class GlobalRules( - val minPlayers: Int, - val maxPlayers: Int -) diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/WonderSide.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/WonderSide.kt deleted file mode 100644 index 4a818fc4..00000000 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/WonderSide.kt +++ /dev/null @@ -1,6 +0,0 @@ -package org.luxons.sevenwonders.game.data - -enum class WonderSide { - A, - B -} diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/WonderSidePickMethod.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/WonderSidePickMethod.kt deleted file mode 100644 index 29fe2126..00000000 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/WonderSidePickMethod.kt +++ /dev/null @@ -1,28 +0,0 @@ -package org.luxons.sevenwonders.game.data - -import java.util.Random - -enum class WonderSidePickMethod { - ALL_A { - override fun pickSide(random: Random, lastPickedSide: WonderSide?): WonderSide { - return WonderSide.A - } - }, - ALL_B { - override fun pickSide(random: Random, lastPickedSide: WonderSide?): WonderSide { - return WonderSide.B - } - }, - EACH_RANDOM { - override fun pickSide(random: Random, lastPickedSide: WonderSide?): WonderSide { - return if (random.nextBoolean()) WonderSide.A else WonderSide.B - } - }, - SAME_RANDOM_FOR_ALL { - override fun pickSide(random: Random, lastPickedSide: WonderSide?): WonderSide { - return lastPickedSide ?: if (random.nextBoolean()) WonderSide.A else WonderSide.B - } - }; - - abstract fun pickSide(random: Random, lastPickedSide: WonderSide?): WonderSide -} diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/definitions/WonderDefinition.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/definitions/WonderDefinition.kt index ece0fe5f..742cc5ae 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/definitions/WonderDefinition.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/definitions/WonderDefinition.kt @@ -1,11 +1,15 @@ package org.luxons.sevenwonders.game.data.definitions import org.luxons.sevenwonders.game.cards.Requirements -import org.luxons.sevenwonders.game.data.WonderSide import org.luxons.sevenwonders.game.resources.ResourceType import org.luxons.sevenwonders.game.wonders.Wonder import org.luxons.sevenwonders.game.wonders.WonderStage +enum class WonderSide { + A, + B +} + internal class WonderDefinition( private val name: String, private val sides: Map<WonderSide, WonderSideDefinition> |