From 5a34ed86e0c2c8997c1a3d905bf4c41ba176c474 Mon Sep 17 00:00:00 2001 From: Joffrey BION Date: Sat, 10 Dec 2016 14:04:56 +0100 Subject: Wonders data parsing --- .../org/luxons/sevenwonders/game/boards/Board.java | 4 +- .../sevenwonders/game/data/GameDataLoader.java | 55 ++++--------------- .../game/data/definitions/WonderDefinition.java | 61 ++++++++++++++++++++++ .../data/definitions/WonderLevelDefinition.java | 20 +++++++ .../data/serializers/ResourceTypeSerializer.java | 30 +++++++++++ .../luxons/sevenwonders/game/wonders/Wonder.java | 10 ++++ .../sevenwonders/game/wonders/WonderLevel.java | 17 ++++-- 7 files changed, 145 insertions(+), 52 deletions(-) create mode 100644 src/main/java/org/luxons/sevenwonders/game/data/definitions/WonderDefinition.java create mode 100644 src/main/java/org/luxons/sevenwonders/game/data/definitions/WonderLevelDefinition.java create mode 100644 src/main/java/org/luxons/sevenwonders/game/data/serializers/ResourceTypeSerializer.java (limited to 'src/main/java') 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 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 loadWonders() { - List 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>() {}.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 comparator = - Comparator.comparing(CardDefinition::getColor).thenComparing(CardDefinition::getName); - List> decks = new ArrayList<>(); - decks.add(decksDef.getAge1()); - decks.add(decksDef.getAge2()); - decks.add(decksDef.getAge3()); - decks.add(decksDef.getGuildCards()); - - for (List 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 { + + 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 stages; + + public String image; + + public ResourceType getInitialResource() { + return initialResource; + } + + public List 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 { + + 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, JsonDeserializer { + + @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 levels; + private String image; + public Wonder() { } @@ -44,4 +46,12 @@ public class Wonder { public void setLevels(List 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 effects; public Requirements getRequirements() { return requirements; @@ -17,11 +20,15 @@ public class WonderLevel { this.requirements = requirements; } - public Effect getEffect() { - return effect; + public List getEffects() { + return effects; + } + + public void setEffects(List 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)); } } -- cgit