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 | |
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
15 files changed, 104 insertions, 131 deletions
diff --git a/backend/src/main/kotlin/org/luxons/sevenwonders/repositories/LobbyRepository.kt b/backend/src/main/kotlin/org/luxons/sevenwonders/repositories/LobbyRepository.kt index 7ca78233..768aa659 100644 --- a/backend/src/main/kotlin/org/luxons/sevenwonders/repositories/LobbyRepository.kt +++ b/backend/src/main/kotlin/org/luxons/sevenwonders/repositories/LobbyRepository.kt @@ -1,6 +1,6 @@ package org.luxons.sevenwonders.repositories -import org.luxons.sevenwonders.game.data.GameDefinitionLoader +import org.luxons.sevenwonders.game.data.GameDefinition import org.luxons.sevenwonders.lobby.Lobby import org.luxons.sevenwonders.lobby.Player import org.springframework.beans.factory.annotation.Autowired @@ -18,7 +18,7 @@ class LobbyRepository @Autowired constructor() { fun create(gameName: String, owner: Player): Lobby { val id = lastGameId++ - val lobby = Lobby(id, gameName, owner, GameDefinitionLoader.gameDefinition) + val lobby = Lobby(id, gameName, owner, GameDefinition.load()) lobbies[id] = lobby return lobby } diff --git a/backend/src/test/kotlin/org/luxons/sevenwonders/controllers/LobbyControllerTest.kt b/backend/src/test/kotlin/org/luxons/sevenwonders/controllers/LobbyControllerTest.kt index 4780c533..5ace12af 100644 --- a/backend/src/test/kotlin/org/luxons/sevenwonders/controllers/LobbyControllerTest.kt +++ b/backend/src/test/kotlin/org/luxons/sevenwonders/controllers/LobbyControllerTest.kt @@ -9,7 +9,7 @@ import org.junit.Test import org.luxons.sevenwonders.actions.ReorderPlayersAction import org.luxons.sevenwonders.actions.UpdateSettingsAction import org.luxons.sevenwonders.game.api.CustomizableSettings -import org.luxons.sevenwonders.game.data.WonderSidePickMethod.ALL_A +import org.luxons.sevenwonders.game.api.WonderSidePickMethod.ALL_A import org.luxons.sevenwonders.lobby.Lobby import org.luxons.sevenwonders.lobby.Player import org.luxons.sevenwonders.lobby.PlayerIsNotOwnerException diff --git a/backend/src/test/kotlin/org/luxons/sevenwonders/lobby/LobbyTest.kt b/backend/src/test/kotlin/org/luxons/sevenwonders/lobby/LobbyTest.kt index f989edbc..1d8b0f4d 100644 --- a/backend/src/test/kotlin/org/luxons/sevenwonders/lobby/LobbyTest.kt +++ b/backend/src/test/kotlin/org/luxons/sevenwonders/lobby/LobbyTest.kt @@ -17,7 +17,6 @@ import org.junit.rules.ExpectedException import org.junit.runner.RunWith import org.luxons.sevenwonders.game.api.CustomizableSettings import org.luxons.sevenwonders.game.data.GameDefinition -import org.luxons.sevenwonders.game.data.GameDefinitionLoader import org.luxons.sevenwonders.lobby.Lobby.GameAlreadyStartedException import org.luxons.sevenwonders.lobby.Lobby.PlayerNameAlreadyUsedException import org.luxons.sevenwonders.lobby.Lobby.PlayerOverflowException @@ -231,7 +230,7 @@ class LobbyTest { @JvmStatic @BeforeClass fun loadDefinition() { - gameDefinition = GameDefinitionLoader.gameDefinition + gameDefinition = GameDefinition.load() } } } 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> 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 e1d8b7f9..9db75e56 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 @@ -10,7 +10,6 @@ import org.luxons.sevenwonders.game.api.HandCard import org.luxons.sevenwonders.game.api.PlayerMove import org.luxons.sevenwonders.game.api.PlayerTurnInfo 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.ResourceTransactions @@ -41,7 +40,7 @@ class GameTest { private fun createGame(nbPlayers: Int): Game { val settings = testCustomizableSettings() - return GameDefinitionLoader.gameDefinition.initGame(0, settings, nbPlayers) + return GameDefinition.load().initGame(0, settings, nbPlayers) } private fun playTurn(nbPlayers: Int, game: Game, ageToCheck: Int, handSize: Int) { diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/GameDefinitionLoaderTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/GameDefinitionLoaderTest.kt deleted file mode 100644 index c312c12f..00000000 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/GameDefinitionLoaderTest.kt +++ /dev/null @@ -1,13 +0,0 @@ -package org.luxons.sevenwonders.game.data - -import org.junit.Assert.assertNotNull -import org.junit.Test - -class GameDefinitionLoaderTest { - - @Test - fun successfulLoad() { - val gameDefinition = GameDefinitionLoader.gameDefinition - assertNotNull(gameDefinition) - } -} diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/GameDefinitionTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/GameDefinitionTest.kt index 162af7a3..0359938a 100644 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/GameDefinitionTest.kt +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/GameDefinitionTest.kt @@ -9,7 +9,7 @@ class GameDefinitionTest { @Test fun successfulGameInit() { - val gameDefinition = GameDefinitionLoader.gameDefinition + val gameDefinition = GameDefinition.load() assertNotNull(gameDefinition) assertEquals(3, gameDefinition.minPlayers.toLong()) assertEquals(7, gameDefinition.maxPlayers.toLong()) diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/definitions/WonderSidePickMethodTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/definitions/WonderSidePickMethodTest.kt index 5b84d466..2d992ba6 100644 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/definitions/WonderSidePickMethodTest.kt +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/data/definitions/WonderSidePickMethodTest.kt @@ -7,8 +7,7 @@ import org.junit.experimental.theories.DataPoints import org.junit.experimental.theories.Theories import org.junit.experimental.theories.Theory import org.junit.runner.RunWith -import org.luxons.sevenwonders.game.data.WonderSide -import org.luxons.sevenwonders.game.data.WonderSidePickMethod +import org.luxons.sevenwonders.game.api.WonderSidePickMethod import java.util.Random @RunWith(Theories::class) |