From 839975bd8762a944e34a367e85ba98d74dee8e5c Mon Sep 17 00:00:00 2001 From: Joffrey BION Date: Wed, 25 Apr 2018 21:46:37 +0200 Subject: Extract min/max players as JSON rules --- .../sevenwonders/game/data/GameDefinition.java | 21 +++++++-------------- .../game/data/GameDefinitionLoader.java | 8 +++++++- .../luxons/sevenwonders/game/data/GlobalRules.java | 17 +++++++++++++++++ .../luxons/sevenwonders/game/data/global_rules.json | 4 ++++ .../sevenwonders/game/data/GameDefinitionTest.java | 3 +++ 5 files changed, 38 insertions(+), 15 deletions(-) create mode 100644 game-engine/src/main/java/org/luxons/sevenwonders/game/data/GlobalRules.java create mode 100644 game-engine/src/main/resources/org/luxons/sevenwonders/game/data/global_rules.json diff --git a/game-engine/src/main/java/org/luxons/sevenwonders/game/data/GameDefinition.java b/game-engine/src/main/java/org/luxons/sevenwonders/game/data/GameDefinition.java index 7604ca6a..0402a26d 100644 --- a/game-engine/src/main/java/org/luxons/sevenwonders/game/data/GameDefinition.java +++ b/game-engine/src/main/java/org/luxons/sevenwonders/game/data/GameDefinition.java @@ -16,31 +16,24 @@ import org.luxons.sevenwonders.game.wonders.Wonder; public class GameDefinition { - /** - * This value is heavily dependent on the JSON data. Any change must be carefully thought through. - */ - private static final int MIN_PLAYERS = 3; + private final GlobalRules rules; - /** - * This value is heavily dependent on the JSON data. Any change must be carefully thought through. - */ - private static final int MAX_PLAYERS = 7; + private final WonderDefinition[] wonders; - private WonderDefinition[] wonders; + private final DecksDefinition decksDefinition; - private DecksDefinition decksDefinition; - - GameDefinition(WonderDefinition[] wonders, DecksDefinition decksDefinition) { + GameDefinition(GlobalRules rules, WonderDefinition[] wonders, DecksDefinition decksDefinition) { + this.rules = rules; this.wonders = wonders; this.decksDefinition = decksDefinition; } public int getMinPlayers() { - return MIN_PLAYERS; + return rules.getMinPlayers(); } public int getMaxPlayers() { - return MAX_PLAYERS; + return rules.getMaxPlayers(); } public Game initGame(long id, CustomizableSettings customSettings, int nbPlayers) { diff --git a/game-engine/src/main/java/org/luxons/sevenwonders/game/data/GameDefinitionLoader.java b/game-engine/src/main/java/org/luxons/sevenwonders/game/data/GameDefinitionLoader.java index 7a9c5af0..a1bd7b8d 100644 --- a/game-engine/src/main/java/org/luxons/sevenwonders/game/data/GameDefinitionLoader.java +++ b/game-engine/src/main/java/org/luxons/sevenwonders/game/data/GameDefinitionLoader.java @@ -35,6 +35,8 @@ public class GameDefinitionLoader { private static final String BASE_PACKAGE_PATH = '/' + BASE_PACKAGE.replace('.', '/'); + private static final String GLOBAL_RULES_FILE = "global_rules.json"; + private static final String CARDS_FILE = "cards.json"; private static final String WONDERS_FILE = "wonders.json"; @@ -50,7 +52,11 @@ public class GameDefinitionLoader { } private static GameDefinition load() { - return new GameDefinition(loadWonders(), loadDecks()); + return new GameDefinition(loadGlobalRules(), loadWonders(), loadDecks()); + } + + private static GlobalRules loadGlobalRules() { + return readJsonFile(GLOBAL_RULES_FILE, GlobalRules.class); } private static WonderDefinition[] loadWonders() { diff --git a/game-engine/src/main/java/org/luxons/sevenwonders/game/data/GlobalRules.java b/game-engine/src/main/java/org/luxons/sevenwonders/game/data/GlobalRules.java new file mode 100644 index 00000000..526bebad --- /dev/null +++ b/game-engine/src/main/java/org/luxons/sevenwonders/game/data/GlobalRules.java @@ -0,0 +1,17 @@ +package org.luxons.sevenwonders.game.data; + +@SuppressWarnings("unused") // fields are set by Gson +class GlobalRules { + + private int minPlayers; + + private int maxPlayers; + + int getMinPlayers() { + return minPlayers; + } + + int getMaxPlayers() { + return maxPlayers; + } +} diff --git a/game-engine/src/main/resources/org/luxons/sevenwonders/game/data/global_rules.json b/game-engine/src/main/resources/org/luxons/sevenwonders/game/data/global_rules.json new file mode 100644 index 00000000..9b486fe6 --- /dev/null +++ b/game-engine/src/main/resources/org/luxons/sevenwonders/game/data/global_rules.json @@ -0,0 +1,4 @@ +{ + "minPlayers": 3, + "maxPlayers": 7 +} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/data/GameDefinitionTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/data/GameDefinitionTest.java index c693a2a2..08e2783b 100644 --- a/game-engine/src/test/java/org/luxons/sevenwonders/game/data/GameDefinitionTest.java +++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/data/GameDefinitionTest.java @@ -4,6 +4,7 @@ import org.junit.Test; import org.luxons.sevenwonders.game.Game; import org.luxons.sevenwonders.game.api.CustomizableSettings; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; public class GameDefinitionTest { @@ -12,6 +13,8 @@ public class GameDefinitionTest { public void successfulGameInit() { GameDefinition gameDefinition = new GameDefinitionLoader().getGameDefinition(); assertNotNull(gameDefinition); + assertEquals(3, gameDefinition.getMinPlayers()); + assertEquals(7, gameDefinition.getMaxPlayers()); Game game = gameDefinition.initGame(0, new CustomizableSettings(), 7); assertNotNull(game); -- cgit