From 9bad1dcd17fff8be59a39f2c3a9f4efc52ca9e9a Mon Sep 17 00:00:00 2001 From: Joffrey BION Date: Sat, 10 Dec 2016 12:39:45 +0100 Subject: Card data parsing, part 2 This adds support for all cards effects, and normalizes the serialization of all card fields --- .../org/luxons/sevenwonders/game/Settings.java | 26 +++++++ .../org/luxons/sevenwonders/game/cards/Color.java | 2 +- .../sevenwonders/game/data/CardDefinition.java | 63 --------------- .../org/luxons/sevenwonders/game/data/Decks.java | 57 ++++++++++++++ .../sevenwonders/game/data/DecksDefinition.java | 30 -------- .../sevenwonders/game/data/EffectParser.java | 11 --- .../luxons/sevenwonders/game/data/GameData.java | 30 ++------ .../sevenwonders/game/data/GameDataLoader.java | 90 ++++++++++++++-------- .../game/data/ResourcesSerializer.java | 40 ---------- .../game/data/definitions/CardDefinition.java | 53 +++++++++++++ .../game/data/definitions/DecksDefinition.java | 38 +++++++++ .../game/data/definitions/Definition.java | 15 ++++ .../game/data/definitions/EffectsDefinition.java | 58 ++++++++++++++ .../data/serializers/NumericEffectSerializer.java | 45 +++++++++++ .../serializers/ProductionIncreaseSerializer.java | 79 +++++++++++++++++++ .../data/serializers/ResourceTypesSerializer.java | 38 +++++++++ .../game/data/serializers/ResourcesSerializer.java | 41 ++++++++++ .../serializers/ScienceProgressSerializer.java | 55 +++++++++++++ .../sevenwonders/game/effects/ScienceProgress.java | 4 + .../sevenwonders/game/resources/Production.java | 8 ++ 20 files changed, 580 insertions(+), 203 deletions(-) delete mode 100644 src/main/java/org/luxons/sevenwonders/game/data/CardDefinition.java create mode 100644 src/main/java/org/luxons/sevenwonders/game/data/Decks.java delete mode 100644 src/main/java/org/luxons/sevenwonders/game/data/DecksDefinition.java delete mode 100644 src/main/java/org/luxons/sevenwonders/game/data/EffectParser.java delete mode 100644 src/main/java/org/luxons/sevenwonders/game/data/ResourcesSerializer.java create mode 100644 src/main/java/org/luxons/sevenwonders/game/data/definitions/CardDefinition.java create mode 100644 src/main/java/org/luxons/sevenwonders/game/data/definitions/DecksDefinition.java create mode 100644 src/main/java/org/luxons/sevenwonders/game/data/definitions/Definition.java create mode 100644 src/main/java/org/luxons/sevenwonders/game/data/definitions/EffectsDefinition.java create mode 100644 src/main/java/org/luxons/sevenwonders/game/data/serializers/NumericEffectSerializer.java create mode 100644 src/main/java/org/luxons/sevenwonders/game/data/serializers/ProductionIncreaseSerializer.java create mode 100644 src/main/java/org/luxons/sevenwonders/game/data/serializers/ResourceTypesSerializer.java create mode 100644 src/main/java/org/luxons/sevenwonders/game/data/serializers/ResourcesSerializer.java create mode 100644 src/main/java/org/luxons/sevenwonders/game/data/serializers/ScienceProgressSerializer.java (limited to 'src/main/java/org') diff --git a/src/main/java/org/luxons/sevenwonders/game/Settings.java b/src/main/java/org/luxons/sevenwonders/game/Settings.java index 4d06c3e8..30e779df 100644 --- a/src/main/java/org/luxons/sevenwonders/game/Settings.java +++ b/src/main/java/org/luxons/sevenwonders/game/Settings.java @@ -1,5 +1,7 @@ package org.luxons.sevenwonders.game; +import java.util.Random; + import org.luxons.sevenwonders.game.wonders.WonderSide; public class Settings { @@ -12,6 +14,10 @@ public class Settings { private WonderSide wonderSide = WonderSide.A; + private Integer nbGuildCards = null; + + private long randomSeedForTests = 0; + public int getNbPlayers() { return nbPlayers; } @@ -43,4 +49,24 @@ public class Settings { public void setWonderSide(WonderSide wonderSide) { this.wonderSide = wonderSide; } + + public int getNbGuildCards() { + return nbGuildCards == null ? nbPlayers + 2 : nbGuildCards; + } + + public void setNbGuildCards(int nbGuildCards) { + this.nbGuildCards = nbGuildCards; + } + + public long getRandomSeedForTests() { + return randomSeedForTests; + } + + public void setRandomSeedForTests(long randomSeedForTests) { + this.randomSeedForTests = randomSeedForTests; + } + + public Random getRandom() { + return new Random(randomSeedForTests); + } } diff --git a/src/main/java/org/luxons/sevenwonders/game/cards/Color.java b/src/main/java/org/luxons/sevenwonders/game/cards/Color.java index 87d5b5b1..5b4e4473 100644 --- a/src/main/java/org/luxons/sevenwonders/game/cards/Color.java +++ b/src/main/java/org/luxons/sevenwonders/game/cards/Color.java @@ -1,5 +1,5 @@ package org.luxons.sevenwonders.game.cards; public enum Color { - BROWN, GREY, YELLOW, BLUE, GREEN, RED, PURPLE; + BROWN, GREY, YELLOW, BLUE, GREEN, RED, PURPLE } diff --git a/src/main/java/org/luxons/sevenwonders/game/data/CardDefinition.java b/src/main/java/org/luxons/sevenwonders/game/data/CardDefinition.java deleted file mode 100644 index 1254bb55..00000000 --- a/src/main/java/org/luxons/sevenwonders/game/data/CardDefinition.java +++ /dev/null @@ -1,63 +0,0 @@ -package org.luxons.sevenwonders.game.data; - -import java.util.List; -import java.util.Map; - -import org.luxons.sevenwonders.game.cards.Card; -import org.luxons.sevenwonders.game.cards.Color; -import org.luxons.sevenwonders.game.cards.Requirements; - -public class CardDefinition { - - private String name; - - private Color color; - - private String effect; - - private Requirements requirements; - - private String chainParent; - - private List chainChildren; - - private Map countPerNbPlayer; - - public Card createCard() { - return new Card(name, color, requirements, EffectParser.parse(effect)); - } - - private Requirements createRequirements() { - Requirements req = new Requirements(); - - return req; - } - - public String getName() { - return name; - } - - public Color getColor() { - return color; - } - - public String getEffect() { - return effect; - } - - public Requirements getRequirements() { - return requirements; - } - - public String getChainParent() { - return chainParent; - } - - public List getChainChildren() { - return chainChildren; - } - - public Map getCountPerNbPlayer() { - return countPerNbPlayer; - } -} diff --git a/src/main/java/org/luxons/sevenwonders/game/data/Decks.java b/src/main/java/org/luxons/sevenwonders/game/data/Decks.java new file mode 100644 index 00000000..ea12ee8f --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/game/data/Decks.java @@ -0,0 +1,57 @@ +package org.luxons.sevenwonders.game.data; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import org.luxons.sevenwonders.game.Settings; +import org.luxons.sevenwonders.game.cards.Card; +import org.luxons.sevenwonders.game.data.definitions.CardDefinition; +import org.luxons.sevenwonders.game.data.definitions.DecksDefinition; + +public class Decks { + + private Map> cardsPerAge = new HashMap<>(); + + public Decks(DecksDefinition decksDefinition, Settings settings) { + cardsPerAge.put(1, prepareStandardDeck(decksDefinition.getAge1(), settings)); + cardsPerAge.put(2, prepareStandardDeck(decksDefinition.getAge2(), settings)); + cardsPerAge.put(3, prepareAge3Deck(decksDefinition, settings)); + } + + public List getCards(int age) { + return cardsPerAge.getOrDefault(age, Collections.emptyList()); + } + + private static List prepareStandardDeck(List defs, Settings settings) { + List cards = createDeck(defs, settings); + Collections.shuffle(cards, settings.getRandom()); + return cards; + } + + private static List prepareAge3Deck(DecksDefinition decksDefinition, Settings settings) { + List age3deck = createDeck(decksDefinition.getAge3(), settings); + age3deck.addAll(createGuildCards(decksDefinition.getGuildCards(), settings)); + Collections.shuffle(age3deck, settings.getRandom()); + return age3deck; + } + + private static List createDeck(List defs, Settings settings) { + List cards = new ArrayList<>(); + for (CardDefinition def : defs) { + for (int i = 0; i < def.getCountPerNbPlayer().get(settings.getNbPlayers()); i++) { + cards.add(def.create(settings)); + } + } + return cards; + } + + private static List createGuildCards(List defs, Settings settings) { + List guild = defs.stream().map((def) -> def.create(settings)).collect(Collectors.toList()); + Collections.shuffle(guild, settings.getRandom()); + return guild.subList(0, settings.getNbPlayers()); + } +} diff --git a/src/main/java/org/luxons/sevenwonders/game/data/DecksDefinition.java b/src/main/java/org/luxons/sevenwonders/game/data/DecksDefinition.java deleted file mode 100644 index 914217a8..00000000 --- a/src/main/java/org/luxons/sevenwonders/game/data/DecksDefinition.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.luxons.sevenwonders.game.data; - -import java.util.List; - -public class DecksDefinition { - - private List age1; - - private List age2; - - private List age3; - - private List guild; - - public List getAge1() { - return age1; - } - - public List getAge2() { - return age2; - } - - public List getAge3() { - return age3; - } - - public List getGuild() { - return guild; - } -} diff --git a/src/main/java/org/luxons/sevenwonders/game/data/EffectParser.java b/src/main/java/org/luxons/sevenwonders/game/data/EffectParser.java deleted file mode 100644 index 62fe3d13..00000000 --- a/src/main/java/org/luxons/sevenwonders/game/data/EffectParser.java +++ /dev/null @@ -1,11 +0,0 @@ -package org.luxons.sevenwonders.game.data; - -import org.luxons.sevenwonders.game.effects.Effect; - -public class EffectParser { - - static Effect parse(String effectStr) { - // TODO - return null; - } -} diff --git a/src/main/java/org/luxons/sevenwonders/game/data/GameData.java b/src/main/java/org/luxons/sevenwonders/game/data/GameData.java index 20a353cd..9abf69c4 100644 --- a/src/main/java/org/luxons/sevenwonders/game/data/GameData.java +++ b/src/main/java/org/luxons/sevenwonders/game/data/GameData.java @@ -1,33 +1,19 @@ package org.luxons.sevenwonders.game.data; import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; import java.util.List; -import java.util.Map; -import org.luxons.sevenwonders.game.cards.Card; import org.luxons.sevenwonders.game.wonders.Wonder; public class GameData { - private int nbAges = 3; - private int minPlayers = 3; private int maxPlayers = 7; private List wonders = new ArrayList<>(); - private Map> cardsPerAge = new HashMap<>(); - - public int getNbAges() { - return nbAges; - } - - public void setNbAges(int nbAges) { - this.nbAges = nbAges; - } + private Decks decks; public int getMinPlayers() { return minPlayers; @@ -53,17 +39,11 @@ public class GameData { this.wonders = wonders; } - public List getCards(int age) { - return cardsPerAge.getOrDefault(age, Collections.emptyList()); - } - - public void setCards(int age, List cards) { - cardsPerAge.put(age, cards); + public Decks getDecks() { + return decks; } - @Override - public String toString() { - return "GameData{" + "nbAges=" + nbAges + ", minPlayers=" + minPlayers + ", maxPlayers=" + maxPlayers - + ", wonders=" + wonders + ", cardsPerAge=" + cardsPerAge + '}'; + public void setDecks(Decks decks) { + this.decks = decks; } } 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 7702a9f6..bd154071 100644 --- a/src/main/java/org/luxons/sevenwonders/game/data/GameDataLoader.java +++ b/src/main/java/org/luxons/sevenwonders/game/data/GameDataLoader.java @@ -4,15 +4,27 @@ import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; +import java.lang.reflect.Type; import java.util.ArrayList; -import java.util.Collections; +import java.util.Comparator; 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.cards.Card; +import org.luxons.sevenwonders.game.data.definitions.CardDefinition; +import org.luxons.sevenwonders.game.data.definitions.DecksDefinition; +import org.luxons.sevenwonders.game.data.serializers.NumericEffectSerializer; +import org.luxons.sevenwonders.game.data.serializers.ProductionIncreaseSerializer; +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.ResourceType; import org.luxons.sevenwonders.game.resources.Resources; import org.luxons.sevenwonders.game.wonders.Wonder; @@ -23,22 +35,18 @@ public class GameDataLoader { private static final String BASE_PACKAGE_PATH = '/' + BASE_PACKAGE.replace('.', '/'); - private static final String wondersFile = "wonders.json"; + private static final String CARDS_FILE = "cards.json"; + + private static final String WONDERS_FILE = "wonders.json"; public static GameData load(Settings settings) { GameData data = new GameData(); - data.setWonders(loadWonders()); - - int nbPlayers = settings.getNbPlayers(); - DecksDefinition defs = loadDecks(); - data.setCards(1, createCards(defs.getAge1(), nbPlayers)); - data.setCards(2, createCards(defs.getAge2(), nbPlayers)); + List wonders = loadWonders(); + data.setWonders(wonders); - List age3 = createCards(defs.getAge3(), nbPlayers); - List guild = createGuildCards(defs.getGuild(), nbPlayers); - age3.addAll(guild); - data.setCards(3, age3); + DecksDefinition decksDefinition = loadDecks(); + data.setDecks(new Decks(decksDefinition, settings)); return data; } @@ -55,24 +63,8 @@ public class GameDataLoader { return wonders; } - private static List createCards(List defs, int nbPlayers) { - List cards = new ArrayList<>(); - for (CardDefinition def : defs) { - for (int i = 0; i < def.getCountPerNbPlayer().get(nbPlayers); i++) { - cards.add(def.createCard()); - } - } - return cards; - } - - private static List createGuildCards(List defs, int nbPlayers) { - List guild = defs.stream().map(CardDefinition::createCard).collect(Collectors.toList()); - Collections.shuffle(guild); - return guild.subList(0, nbPlayers + 2); - } - private static DecksDefinition loadDecks() { - return readJsonFile("cards.json", DecksDefinition.class); + return readJsonFile(CARDS_FILE, DecksDefinition.class); } private static T readJsonFile(String filename, Class clazz) { @@ -83,10 +75,42 @@ public class GameDataLoader { } private static Gson createGson() { + Type resourceTypeList = new TypeToken>() {}.getType(); return new GsonBuilder().disableHtmlEscaping() - .serializeNulls() .registerTypeAdapter(Resources.class, new ResourcesSerializer()) - .setPrettyPrinting() + .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/ResourcesSerializer.java b/src/main/java/org/luxons/sevenwonders/game/data/ResourcesSerializer.java deleted file mode 100644 index ff4a8ee3..00000000 --- a/src/main/java/org/luxons/sevenwonders/game/data/ResourcesSerializer.java +++ /dev/null @@ -1,40 +0,0 @@ -package org.luxons.sevenwonders.game.data; - -import java.lang.reflect.Type; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -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; -import org.luxons.sevenwonders.game.resources.Resources; - -public class ResourcesSerializer implements JsonSerializer, JsonDeserializer { - - @Override - public JsonElement serialize(Resources resources, Type typeOfSrc, JsonSerializationContext context) { - String s = resources.getQuantities() - .entrySet() - .stream() - .flatMap(e -> Stream.generate(() -> e.getKey().getSymbol()).limit(e.getValue())) - .map(Object::toString) - .collect(Collectors.joining()); - return new JsonPrimitive(s); - } - - @Override - public Resources deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws - JsonParseException { - String s = json.getAsString(); - Resources resources = new Resources(); - for (char c : s.toCharArray()) { - resources.add(ResourceType.fromSymbol(c), 1); - } - return resources; - } -} diff --git a/src/main/java/org/luxons/sevenwonders/game/data/definitions/CardDefinition.java b/src/main/java/org/luxons/sevenwonders/game/data/definitions/CardDefinition.java new file mode 100644 index 00000000..a23db2fc --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/game/data/definitions/CardDefinition.java @@ -0,0 +1,53 @@ +package org.luxons.sevenwonders.game.data.definitions; + +import java.util.List; +import java.util.Map; + +import org.luxons.sevenwonders.game.Settings; +import org.luxons.sevenwonders.game.cards.Card; +import org.luxons.sevenwonders.game.cards.Color; +import org.luxons.sevenwonders.game.cards.Requirements; + +public class CardDefinition implements Definition { + + private String name; + + private Color color; + + private EffectsDefinition effect; + + private Requirements requirements; + + private String chainParent; + + private List chainChildren; + + private Map countPerNbPlayer; + + private String image; + + @Override + public Card create(Settings settings) { + return new Card(name, color, requirements, effect.create(settings)); + } + + public String getName() { + return name; + } + + public Color getColor() { + return color; + } + + public Map getCountPerNbPlayer() { + return countPerNbPlayer; + } + + public String getImage() { + return image; + } + + public void setImage(String image) { + this.image = image; + } +} diff --git a/src/main/java/org/luxons/sevenwonders/game/data/definitions/DecksDefinition.java b/src/main/java/org/luxons/sevenwonders/game/data/definitions/DecksDefinition.java new file mode 100644 index 00000000..bfe89d12 --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/game/data/definitions/DecksDefinition.java @@ -0,0 +1,38 @@ +package org.luxons.sevenwonders.game.data.definitions; + +import java.util.List; + +import org.luxons.sevenwonders.game.Settings; +import org.luxons.sevenwonders.game.data.Decks; + +public class DecksDefinition implements Definition { + + private List age1; + + private List age2; + + private List age3; + + private List guildCards; + + public List getAge1() { + return age1; + } + + public List getAge2() { + return age2; + } + + public List getAge3() { + return age3; + } + + public List getGuildCards() { + return guildCards; + } + + @Override + public Decks create(Settings settings) { + return new Decks(this, settings); + } +} diff --git a/src/main/java/org/luxons/sevenwonders/game/data/definitions/Definition.java b/src/main/java/org/luxons/sevenwonders/game/data/definitions/Definition.java new file mode 100644 index 00000000..15ecb255 --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/game/data/definitions/Definition.java @@ -0,0 +1,15 @@ +package org.luxons.sevenwonders.game.data.definitions; + +import org.luxons.sevenwonders.game.Settings; + +/** + * Represents a deserialized JSON definition of some data about the game. It is settings-agnostic. An instance of + * in-game data can be generated from this, given specific game settings. + * + * @param + * the type of in-game object that can be generated from this definition + */ +public interface Definition { + + T create(Settings settings); +} diff --git a/src/main/java/org/luxons/sevenwonders/game/data/definitions/EffectsDefinition.java b/src/main/java/org/luxons/sevenwonders/game/data/definitions/EffectsDefinition.java new file mode 100644 index 00000000..f7fe5573 --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/game/data/definitions/EffectsDefinition.java @@ -0,0 +1,58 @@ +package org.luxons.sevenwonders.game.data.definitions; + +import java.util.ArrayList; +import java.util.List; + +import org.luxons.sevenwonders.game.Settings; +import org.luxons.sevenwonders.game.effects.BonusPerBoardElement; +import org.luxons.sevenwonders.game.effects.Discount; +import org.luxons.sevenwonders.game.effects.Effect; +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; + +public class EffectsDefinition implements Definition> { + + private GoldIncrease gold; + + private MilitaryReinforcements military; + + private ScienceProgress science; + + private Discount discount; + + private BonusPerBoardElement perBoardElement; + + private ProductionIncrease production; + + private RawPointsIncrease points; + + @Override + public List create(Settings settings) { + List effects = new ArrayList<>(); + if (gold != null) { + effects.add(gold); + } + if (military != null) { + effects.add(military); + } + if (science != null) { + effects.add(science); + } + if (discount != null) { + effects.add(discount); + } + if (perBoardElement != null) { + effects.add(perBoardElement); + } + if (production != null) { + effects.add(production); + } + if (points != null) { + effects.add(points); + } + return effects; + } +} diff --git a/src/main/java/org/luxons/sevenwonders/game/data/serializers/NumericEffectSerializer.java b/src/main/java/org/luxons/sevenwonders/game/data/serializers/NumericEffectSerializer.java new file mode 100644 index 00000000..e8e5d163 --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/game/data/serializers/NumericEffectSerializer.java @@ -0,0 +1,45 @@ +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.effects.Effect; +import org.luxons.sevenwonders.game.effects.GoldIncrease; +import org.luxons.sevenwonders.game.effects.MilitaryReinforcements; +import org.luxons.sevenwonders.game.effects.RawPointsIncrease; + +public class NumericEffectSerializer implements JsonSerializer, JsonDeserializer { + + @Override + public JsonElement serialize(Effect effect, Type typeOfSrc, JsonSerializationContext context) { + int value = 0; + if (MilitaryReinforcements.class.equals(typeOfSrc)) { + value = ((MilitaryReinforcements)effect).getCount(); + } else if (GoldIncrease.class.equals(typeOfSrc)) { + value = ((GoldIncrease)effect).getAmount(); + } else if (RawPointsIncrease.class.equals(typeOfSrc)) { + value = ((RawPointsIncrease)effect).getPoints(); + } + return new JsonPrimitive(value); + } + + @Override + public Effect deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws + JsonParseException { + int value = json.getAsInt(); + if (MilitaryReinforcements.class.equals(typeOfT)) { + return new MilitaryReinforcements(value); + } else if (GoldIncrease.class.equals(typeOfT)) { + return new GoldIncrease(value); + } else if (RawPointsIncrease.class.equals(typeOfT)) { + return new RawPointsIncrease(value); + } + throw new IllegalArgumentException("Unknown numeric effet " + typeOfT.getTypeName()); + } +} \ No newline at end of file diff --git a/src/main/java/org/luxons/sevenwonders/game/data/serializers/ProductionIncreaseSerializer.java b/src/main/java/org/luxons/sevenwonders/game/data/serializers/ProductionIncreaseSerializer.java new file mode 100644 index 00000000..645958d3 --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/game/data/serializers/ProductionIncreaseSerializer.java @@ -0,0 +1,79 @@ +package org.luxons.sevenwonders.game.data.serializers; + +import java.lang.reflect.Type; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonNull; +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.effects.ProductionIncrease; +import org.luxons.sevenwonders.game.resources.Production; +import org.luxons.sevenwonders.game.resources.ResourceType; +import org.luxons.sevenwonders.game.resources.Resources; + +public class ProductionIncreaseSerializer implements JsonSerializer, + JsonDeserializer { + + @Override + public JsonElement serialize(ProductionIncrease productionIncrease, Type typeOfSrc, + JsonSerializationContext context) { + Production production = productionIncrease.getProduction(); + Resources fixedResources = production.getFixedResources(); + List> choices = production.getAlternativeResources(); + if (fixedResources.isEmpty()) { + return serializeAsChoice(choices, context); + } else { + return serializeAsResources(fixedResources, context); + } + } + + private JsonElement serializeAsResources(Resources fixedResources, JsonSerializationContext context) { + return context.serialize(fixedResources); + } + + private JsonElement serializeAsChoice(List> choices, JsonSerializationContext context) { + if (choices.isEmpty()) { + return JsonNull.INSTANCE; + } + if (choices.size() > 1) { + throw new UnsupportedOperationException("Cannot serialize a production with more than one choice"); + } + String str = choices.get(0).stream() + .map(ResourceType::getSymbol) + .map(Object::toString) + .collect(Collectors.joining("/")); + return new JsonPrimitive(str); + } + + @Override + public ProductionIncrease deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws + JsonParseException { + String s = json.getAsString(); + ProductionIncrease productionIncrease = new ProductionIncrease(); + Production production = new Production(); + if (s.contains("/")) { + production.addChoice(createChoice(s)); + } else { + Resources fixedResources = context.deserialize(json, Resources.class); + production.addAll(fixedResources); + } + productionIncrease.setProduction(production); + return productionIncrease; + } + + private ResourceType[] createChoice(String choiceStr) { + String[] symbols = choiceStr.split("/"); + ResourceType[] choice = new ResourceType[symbols.length]; + for (int i = 0; i < symbols.length; i++) { + choice[i] = ResourceType.fromSymbol(symbols[i].charAt(0)); + } + return choice; + } +} diff --git a/src/main/java/org/luxons/sevenwonders/game/data/serializers/ResourceTypesSerializer.java b/src/main/java/org/luxons/sevenwonders/game/data/serializers/ResourceTypesSerializer.java new file mode 100644 index 00000000..8aca5561 --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/game/data/serializers/ResourceTypesSerializer.java @@ -0,0 +1,38 @@ +package org.luxons.sevenwonders.game.data.serializers; + +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +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 ResourceTypesSerializer implements JsonSerializer>, JsonDeserializer> { + + @Override + public JsonElement serialize(List resources, Type typeOfSrc, JsonSerializationContext context) { + String s = resources.stream() + .map(ResourceType::getSymbol) + .map(Object::toString) + .collect(Collectors.joining()); + return new JsonPrimitive(s); + } + + @Override + public List deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws + JsonParseException { + String s = json.getAsString(); + List resources = new ArrayList<>(); + for (char c : s.toCharArray()) { + resources.add(ResourceType.fromSymbol(c)); + } + return resources; + } +} diff --git a/src/main/java/org/luxons/sevenwonders/game/data/serializers/ResourcesSerializer.java b/src/main/java/org/luxons/sevenwonders/game/data/serializers/ResourcesSerializer.java new file mode 100644 index 00000000..efeafd15 --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/game/data/serializers/ResourcesSerializer.java @@ -0,0 +1,41 @@ +package org.luxons.sevenwonders.game.data.serializers; + +import java.lang.reflect.Type; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonNull; +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; +import org.luxons.sevenwonders.game.resources.Resources; + +public class ResourcesSerializer implements JsonSerializer, JsonDeserializer { + + @Override + public JsonElement serialize(Resources resources, Type typeOfSrc, JsonSerializationContext context) { + String s = resources.getQuantities() + .entrySet() + .stream() + .flatMap(e -> Stream.generate(() -> e.getKey().getSymbol()).limit(e.getValue())) + .map(Object::toString) + .collect(Collectors.joining()); + return s.isEmpty() ? JsonNull.INSTANCE : new JsonPrimitive(s); + } + + @Override + public Resources deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws + JsonParseException { + String s = json.getAsString(); + Resources resources = new Resources(); + for (char c : s.toCharArray()) { + resources.add(ResourceType.fromSymbol(c), 1); + } + return resources; + } +} diff --git a/src/main/java/org/luxons/sevenwonders/game/data/serializers/ScienceProgressSerializer.java b/src/main/java/org/luxons/sevenwonders/game/data/serializers/ScienceProgressSerializer.java new file mode 100644 index 00000000..0535b90a --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/game/data/serializers/ScienceProgressSerializer.java @@ -0,0 +1,55 @@ +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.JsonNull; +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.boards.Science; +import org.luxons.sevenwonders.game.boards.ScienceType; +import org.luxons.sevenwonders.game.effects.ScienceProgress; + +public class ScienceProgressSerializer implements JsonSerializer, JsonDeserializer { + + @Override + public JsonElement serialize(ScienceProgress scienceProgress, Type typeOfSrc, JsonSerializationContext context) { + Science science = scienceProgress.getScience(); + + if (science.size() > 1) { + throw new UnsupportedOperationException("Cannot serialize science containing more than one element"); + } + + for (ScienceType type : ScienceType.values()) { + int quantity = science.getQuantity(type); + if (quantity == 1) { + return context.serialize(type); + } + } + + if (science.getJokers() == 1) { + return new JsonPrimitive("any"); + } + + return JsonNull.INSTANCE; + } + + @Override + public ScienceProgress deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws + JsonParseException { + String s = json.getAsString(); + ScienceProgress scienceProgress = new ScienceProgress(); + Science science = new Science(); + if ("any".equals(s)) { + science.addJoker(1); + } else { + science.add(context.deserialize(json, ScienceType.class), 1); + } + scienceProgress.setScience(science); + return scienceProgress; + } +} diff --git a/src/main/java/org/luxons/sevenwonders/game/effects/ScienceProgress.java b/src/main/java/org/luxons/sevenwonders/game/effects/ScienceProgress.java index 8feba09d..3eee7a9b 100644 --- a/src/main/java/org/luxons/sevenwonders/game/effects/ScienceProgress.java +++ b/src/main/java/org/luxons/sevenwonders/game/effects/ScienceProgress.java @@ -7,6 +7,10 @@ public class ScienceProgress extends InstantEffect { private Science science; + public Science getScience() { + return science; + } + public void setScience(Science science) { this.science = science; } diff --git a/src/main/java/org/luxons/sevenwonders/game/resources/Production.java b/src/main/java/org/luxons/sevenwonders/game/resources/Production.java index f4d46b59..26f5a58e 100644 --- a/src/main/java/org/luxons/sevenwonders/game/resources/Production.java +++ b/src/main/java/org/luxons/sevenwonders/game/resources/Production.java @@ -32,6 +32,14 @@ public class Production { alternativeResources.addAll(production.alternativeResources); } + public Resources getFixedResources() { + return fixedResources; + } + + public List> getAlternativeResources() { + return alternativeResources; + } + public boolean contains(Resources resources) { if (fixedResources.contains(resources)) { return true; -- cgit