diff options
author | Joffrey BION <joffrey.bion@gmail.com> | 2016-12-10 14:04:56 +0100 |
---|---|---|
committer | Joffrey BION <joffrey.bion@gmail.com> | 2016-12-10 16:02:01 +0100 |
commit | 5a34ed86e0c2c8997c1a3d905bf4c41ba176c474 (patch) | |
tree | 9d70413f1d58ae8e50b80dcb0e188a8c44a31986 | |
parent | Normalize wonders.json for easier parsing (diff) | |
download | seven-wonders-5a34ed86e0c2c8997c1a3d905bf4c41ba176c474.tar.gz seven-wonders-5a34ed86e0c2c8997c1a3d905bf4c41ba176c474.tar.bz2 seven-wonders-5a34ed86e0c2c8997c1a3d905bf4c41ba176c474.zip |
Wonders data parsing
8 files changed, 234 insertions, 85 deletions
diff --git a/src/main/java/org/luxons/sevenwonders/game/boards/Board.java b/src/main/java/org/luxons/sevenwonders/game/boards/Board.java index b405a566..d2fe5c7d 100644 --- a/src/main/java/org/luxons/sevenwonders/game/boards/Board.java +++ b/src/main/java/org/luxons/sevenwonders/game/boards/Board.java @@ -77,13 +77,13 @@ public class Board { return wonderLevel; } - public void upgradeWonderLevel() { + public void upgradeWonderLevel(Board leftNeighbourBoard, Board rightNeighbourBoard) { int maxLevel = wonder.getLevels().size(); if (maxLevel == wonderLevel) { throw new IllegalStateException("This wonder has already reached its maximum level"); } this.wonderLevel++; - wonder.getLevels().get(wonderLevel).getEffect().apply(this, null, null); + wonder.getLevels().get(wonderLevel).activate(this, leftNeighbourBoard, rightNeighbourBoard); } public int getNbWarSymbols() { diff --git a/src/main/java/org/luxons/sevenwonders/game/data/GameDataLoader.java b/src/main/java/org/luxons/sevenwonders/game/data/GameDataLoader.java index bd154071..a4c14a7b 100644 --- a/src/main/java/org/luxons/sevenwonders/game/data/GameDataLoader.java +++ b/src/main/java/org/luxons/sevenwonders/game/data/GameDataLoader.java @@ -5,18 +5,19 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.lang.reflect.Type; -import java.util.ArrayList; -import java.util.Comparator; +import java.util.Arrays; import java.util.List; +import java.util.stream.Collectors; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.reflect.TypeToken; import org.luxons.sevenwonders.game.Settings; -import org.luxons.sevenwonders.game.data.definitions.CardDefinition; 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.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; @@ -27,7 +28,6 @@ import org.luxons.sevenwonders.game.effects.RawPointsIncrease; import org.luxons.sevenwonders.game.effects.ScienceProgress; import org.luxons.sevenwonders.game.resources.ResourceType; import org.luxons.sevenwonders.game.resources.Resources; -import org.luxons.sevenwonders.game.wonders.Wonder; public class GameDataLoader { @@ -42,25 +42,16 @@ public class GameDataLoader { public static GameData load(Settings settings) { GameData data = new GameData(); - List<Wonder> wonders = loadWonders(); - data.setWonders(wonders); + WonderDefinition[] wonders = loadWonders(); + data.setWonders(Arrays.stream(wonders).map(def -> def.create(settings)).collect(Collectors.toList())); DecksDefinition decksDefinition = loadDecks(); - data.setDecks(new Decks(decksDefinition, settings)); + data.setDecks(decksDefinition.create(settings)); return data; } - private static List<Wonder> loadWonders() { - List<Wonder> wonders = new ArrayList<>(); - // TODO load actual file - wonders.add(new Wonder("TestWonder W", ResourceType.WOOD)); - wonders.add(new Wonder("TestWonder S", ResourceType.STONE)); - wonders.add(new Wonder("TestWonder O", ResourceType.ORE)); - wonders.add(new Wonder("TestWonder C", ResourceType.CLAY)); - wonders.add(new Wonder("TestWonder G", ResourceType.GLASS)); - wonders.add(new Wonder("TestWonder L", ResourceType.LOOM)); - wonders.add(new Wonder("TestWonder P", ResourceType.PAPYRUS)); - return wonders; + private static WonderDefinition[] loadWonders() { + return readJsonFile(WONDERS_FILE, WonderDefinition[].class); } private static DecksDefinition loadDecks() { @@ -78,39 +69,13 @@ public class GameDataLoader { Type resourceTypeList = new TypeToken<List<ResourceType>>() {}.getType(); return new GsonBuilder().disableHtmlEscaping() .registerTypeAdapter(Resources.class, new ResourcesSerializer()) + .registerTypeAdapter(ResourceType.class, new ResourceTypeSerializer()) .registerTypeAdapter(resourceTypeList, new ResourceTypesSerializer()) .registerTypeAdapter(ProductionIncrease.class, new ProductionIncreaseSerializer()) .registerTypeAdapter(MilitaryReinforcements.class, new NumericEffectSerializer()) .registerTypeAdapter(RawPointsIncrease.class, new NumericEffectSerializer()) .registerTypeAdapter(GoldIncrease.class, new NumericEffectSerializer()) .registerTypeAdapter(ScienceProgress.class, new ScienceProgressSerializer()) -// .setPrettyPrinting() .create(); } - - public static void main(String[] args) { - DecksDefinition decksDef = loadDecks(); - Comparator<CardDefinition> comparator = - Comparator.comparing(CardDefinition::getColor).thenComparing(CardDefinition::getName); - List<List<CardDefinition>> decks = new ArrayList<>(); - decks.add(decksDef.getAge1()); - decks.add(decksDef.getAge2()); - decks.add(decksDef.getAge3()); - decks.add(decksDef.getGuildCards()); - - for (List<CardDefinition> deck : decks) { - deck.sort(comparator); - for (CardDefinition cardDefinition : deck) { - cardDefinition.setImage(computeImageName(cardDefinition.getName())); - } - } - Gson gson = createGson(); - - System.out.println(gson.toJson(decksDef)); -// System.out.println(load(5)); - } - - private static String computeImageName(String name) { - return name.toLowerCase().replaceAll("\\s", "") + ".png"; - } } diff --git a/src/main/java/org/luxons/sevenwonders/game/data/definitions/WonderDefinition.java b/src/main/java/org/luxons/sevenwonders/game/data/definitions/WonderDefinition.java new file mode 100644 index 00000000..89a58db0 --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/game/data/definitions/WonderDefinition.java @@ -0,0 +1,61 @@ +package org.luxons.sevenwonders.game.data.definitions; + +import java.util.List; +import java.util.stream.Collectors; + +import org.luxons.sevenwonders.game.Settings; +import org.luxons.sevenwonders.game.resources.ResourceType; +import org.luxons.sevenwonders.game.wonders.Wonder; +import org.luxons.sevenwonders.game.wonders.WonderLevel; + +public class WonderDefinition implements Definition<Wonder> { + + public String name; + + public WonderSideDefinition a; + + public WonderSideDefinition b; + + @Override + public Wonder create(Settings settings) { + Wonder wonder = new Wonder(); + wonder.setName(name); + + WonderSideDefinition wonderSideDef = pickSide(settings); + wonder.setInitialResource(wonderSideDef.getInitialResource()); + wonder.setLevels(wonderSideDef.createStages(settings)); + wonder.setImage(wonderSideDef.getImage()); + return wonder; + } + + private WonderSideDefinition pickSide(Settings settings) { + switch (settings.getWonderSide()){ + case A: + return a; + case B: + return b; + } + throw new IllegalArgumentException("Unsupported wonder side " + settings.getWonderSide()); + } + + public static class WonderSideDefinition { + + private ResourceType initialResource; + + private List<WonderLevelDefinition> stages; + + public String image; + + public ResourceType getInitialResource() { + return initialResource; + } + + public List<WonderLevel> createStages(Settings settings) { + return stages.stream().map(def -> def.create(settings)).collect(Collectors.toList()); + } + + public String getImage() { + return image; + } + } +} diff --git a/src/main/java/org/luxons/sevenwonders/game/data/definitions/WonderLevelDefinition.java b/src/main/java/org/luxons/sevenwonders/game/data/definitions/WonderLevelDefinition.java new file mode 100644 index 00000000..6064734c --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/game/data/definitions/WonderLevelDefinition.java @@ -0,0 +1,20 @@ +package org.luxons.sevenwonders.game.data.definitions; + +import org.luxons.sevenwonders.game.Settings; +import org.luxons.sevenwonders.game.cards.Requirements; +import org.luxons.sevenwonders.game.wonders.WonderLevel; + +public class WonderLevelDefinition implements Definition<WonderLevel> { + + private Requirements requirements; + + private EffectsDefinition effects; + + @Override + public WonderLevel create(Settings settings) { + WonderLevel level = new WonderLevel(); + level.setRequirements(requirements); + level.setEffects(effects.create(settings)); + return level; + } +} diff --git a/src/main/java/org/luxons/sevenwonders/game/data/serializers/ResourceTypeSerializer.java b/src/main/java/org/luxons/sevenwonders/game/data/serializers/ResourceTypeSerializer.java new file mode 100644 index 00000000..145063eb --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/game/data/serializers/ResourceTypeSerializer.java @@ -0,0 +1,30 @@ +package org.luxons.sevenwonders.game.data.serializers; + +import java.lang.reflect.Type; + +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonParseException; +import com.google.gson.JsonPrimitive; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; +import org.luxons.sevenwonders.game.resources.ResourceType; + +public class ResourceTypeSerializer implements JsonSerializer<ResourceType>, JsonDeserializer<ResourceType> { + + @Override + public JsonElement serialize(ResourceType type, Type typeOfSrc, JsonSerializationContext context) { + return new JsonPrimitive(type.getSymbol()); + } + + @Override + public ResourceType deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws + JsonParseException { + String str = json.getAsString(); + if (str.isEmpty()) { + throw new IllegalArgumentException("Empty string is not a valid resource type"); + } + return ResourceType.fromSymbol(str.charAt(0)); + } +} diff --git a/src/main/java/org/luxons/sevenwonders/game/wonders/Wonder.java b/src/main/java/org/luxons/sevenwonders/game/wonders/Wonder.java index 27d06435..8896b4b7 100644 --- a/src/main/java/org/luxons/sevenwonders/game/wonders/Wonder.java +++ b/src/main/java/org/luxons/sevenwonders/game/wonders/Wonder.java @@ -13,6 +13,8 @@ public class Wonder { private List<WonderLevel> levels; + private String image; + public Wonder() { } @@ -44,4 +46,12 @@ public class Wonder { public void setLevels(List<WonderLevel> levels) { this.levels = levels; } + + public String getImage() { + return image; + } + + public void setImage(String image) { + this.image = image; + } } diff --git a/src/main/java/org/luxons/sevenwonders/game/wonders/WonderLevel.java b/src/main/java/org/luxons/sevenwonders/game/wonders/WonderLevel.java index 23571149..8f8d6a55 100644 --- a/src/main/java/org/luxons/sevenwonders/game/wonders/WonderLevel.java +++ b/src/main/java/org/luxons/sevenwonders/game/wonders/WonderLevel.java @@ -1,5 +1,8 @@ package org.luxons.sevenwonders.game.wonders; +import java.util.List; + +import org.luxons.sevenwonders.game.boards.Board; import org.luxons.sevenwonders.game.cards.Requirements; import org.luxons.sevenwonders.game.effects.Effect; @@ -7,7 +10,7 @@ public class WonderLevel { private Requirements requirements; - private Effect effect; + private List<Effect> effects; public Requirements getRequirements() { return requirements; @@ -17,11 +20,15 @@ public class WonderLevel { this.requirements = requirements; } - public Effect getEffect() { - return effect; + public List<Effect> getEffects() { + return effects; + } + + public void setEffects(List<Effect> effects) { + this.effects = effects; } - public void setEffect(Effect effect) { - this.effect = effect; + public void activate(Board board, Board leftNeighbourBoard, Board rightNeighbourBoard) { + effects.forEach(e -> e.apply(board, leftNeighbourBoard, rightNeighbourBoard)); } } diff --git a/src/main/resources/org/luxons/sevenwonders/game/data/wonders.json b/src/main/resources/org/luxons/sevenwonders/game/data/wonders.json index b6796ce2..cac9e82d 100644 --- a/src/main/resources/org/luxons/sevenwonders/game/data/wonders.json +++ b/src/main/resources/org/luxons/sevenwonders/game/data/wonders.json @@ -6,6 +6,7 @@ "stages": [ { "requirements": { + "gold": 0, "resources": "SS" }, "effects": { @@ -14,50 +15,57 @@ }, { "requirements": { + "gold": 0, "resources": "OO" }, "effects": { - "production": "C/O/W/S" + "production": "W/S/O/C" } }, { "requirements": { + "gold": 0, "resources": "GG" }, "effects": { "points": 7 } } - ] + ], + "image": "alexandriaA.png" }, "b": { "initialResource": "G", "stages": [ { "requirements": { + "gold": 0, "resources": "CC" }, "effects": { - "production": "C/O/W/S" + "production": "W/S/O/C" } }, { "requirements": { + "gold": 0, "resources": "WW" }, "effects": { - "production": "L/G/P" + "production": "G/P/L" } }, { "requirements": { + "gold": 0, "resources": "SSS" }, "effects": { "points": 7 } } - ] + ], + "image": "alexandriaB.png" } }, { @@ -67,6 +75,7 @@ "stages": [ { "requirements": { + "gold": 0, "resources": "CC" }, "effects": { @@ -75,6 +84,7 @@ }, { "requirements": { + "gold": 0, "resources": "WWW" }, "effects": { @@ -83,19 +93,22 @@ }, { "requirements": { + "gold": 0, "resources": "CCCC" }, "effects": { "points": 7 } } - ] + ], + "image": "babylonA.png" }, "b": { "initialResource": "C", "stages": [ { "requirements": { + "gold": 0, "resources": "CL" }, "effects": { @@ -104,7 +117,8 @@ }, { "requirements": { - "resources": "GWW" + "gold": 0, + "resources": "WWG" }, "effects": { "custom": "play2" @@ -112,13 +126,15 @@ }, { "requirements": { + "gold": 0, "resources": "CCCP" }, "effects": { "science": "any" } } - ] + ], + "image": "babylonB.png" } }, { @@ -128,6 +144,7 @@ "stages": [ { "requirements": { + "gold": 0, "resources": "SS" }, "effects": { @@ -136,53 +153,60 @@ }, { "requirements": { + "gold": 0, "resources": "WW" }, "effects": { - "coins": 9 + "gold": 9 } }, { "requirements": { + "gold": 0, "resources": "PP" }, "effects": { "points": 7 } } - ] + ], + "image": "ephesosA.png" }, "b": { "initialResource": "P", "stages": [ { "requirements": { + "gold": 0, "resources": "SS" }, "effects": { - "points": 2, - "coins": 4 + "gold": 4, + "points": 2 } }, { "requirements": { + "gold": 0, "resources": "WW" }, "effects": { - "points": 3, - "coins": 4 + "gold": 4, + "points": 3 } }, { "requirements": { + "gold": 0, "resources": "GPL" }, "effects": { - "points": 5, - "coins": 4 + "gold": 4, + "points": 5 } } - ] + ], + "image": "ephesosB.png" } }, { @@ -192,6 +216,7 @@ "stages": [ { "requirements": { + "gold": 0, "resources": "SS" }, "effects": { @@ -200,6 +225,7 @@ }, { "requirements": { + "gold": 0, "resources": "WWW" }, "effects": { @@ -208,19 +234,22 @@ }, { "requirements": { + "gold": 0, "resources": "SSSS" }, "effects": { "points": 7 } } - ] + ], + "image": "gizahA.png" }, "b": { "initialResource": "S", "stages": [ { "requirements": { + "gold": 0, "resources": "WW" }, "effects": { @@ -229,6 +258,7 @@ }, { "requirements": { + "gold": 0, "resources": "SSS" }, "effects": { @@ -237,6 +267,7 @@ }, { "requirements": { + "gold": 0, "resources": "CCC" }, "effects": { @@ -245,13 +276,15 @@ }, { "requirements": { - "resources": "PSSSS" + "gold": 0, + "resources": "SSSSP" }, "effects": { "points": 7 } } - ] + ], + "image": "gizahB.png" } }, { @@ -261,6 +294,7 @@ "stages": [ { "requirements": { + "gold": 0, "resources": "CC" }, "effects": { @@ -269,6 +303,7 @@ }, { "requirements": { + "gold": 0, "resources": "OOO" }, "effects": { @@ -277,19 +312,22 @@ }, { "requirements": { + "gold": 0, "resources": "LL" }, "effects": { "points": 7 } } - ] + ], + "image": "halikarnassusA.png" }, "b": { "initialResource": "L", "stages": [ { "requirements": { + "gold": 0, "resources": "OO" }, "effects": { @@ -299,6 +337,7 @@ }, { "requirements": { + "gold": 0, "resources": "CCC" }, "effects": { @@ -308,13 +347,15 @@ }, { "requirements": { + "gold": 0, "resources": "GPL" }, "effects": { "custom": "discard" } } - ] + ], + "image": "halikarnassusB.png" } }, { @@ -324,6 +365,7 @@ "stages": [ { "requirements": { + "gold": 0, "resources": "WW" }, "effects": { @@ -332,6 +374,7 @@ }, { "requirements": { + "gold": 0, "resources": "SS" }, "effects": { @@ -340,19 +383,22 @@ }, { "requirements": { + "gold": 0, "resources": "OO" }, "effects": { "points": 7 } } - ] + ], + "image": "olympiaA.png" }, "b": { "initialResource": "W", "stages": [ { "requirements": { + "gold": 0, "resources": "WW" }, "effects": { @@ -361,6 +407,7 @@ }, { "requirements": { + "gold": 0, "resources": "SS" }, "effects": { @@ -369,13 +416,15 @@ }, { "requirements": { - "resources": "LOO" + "gold": 0, + "resources": "OOL" }, "effects": { "custom": "guild" } } - ] + ], + "image": "olympiaB.png" } }, { @@ -385,6 +434,7 @@ "stages": [ { "requirements": { + "gold": 0, "resources": "WW" }, "effects": { @@ -393,6 +443,7 @@ }, { "requirements": { + "gold": 0, "resources": "CCC" }, "effects": { @@ -401,38 +452,43 @@ }, { "requirements": { + "gold": 0, "resources": "OOOO" }, "effects": { "points": 7 } } - ] + ], + "image": "rhodosA.png" }, "b": { "initialResource": "O", "stages": [ { "requirements": { + "gold": 0, "resources": "SSS" }, "effects": { + "gold": 3, "military": 1, - "points": 3, - "coins": 3 + "points": 3 } }, { "requirements": { + "gold": 0, "resources": "OOOO" }, "effects": { - "military": 1, - "points": 4, - "coins": 4 + "gold": 4, + "military": 1, + "points": 4 } } - ] + ], + "image": "rhodosB.png" } } -] +]
\ No newline at end of file |