diff options
Diffstat (limited to 'backend/src/main/java')
5 files changed, 125 insertions, 53 deletions
diff --git a/backend/src/main/java/org/luxons/sevenwonders/game/boards/Board.java b/backend/src/main/java/org/luxons/sevenwonders/game/boards/Board.java index ab557d38..6ba776c7 100644 --- a/backend/src/main/java/org/luxons/sevenwonders/game/boards/Board.java +++ b/backend/src/main/java/org/luxons/sevenwonders/game/boards/Board.java @@ -29,6 +29,8 @@ public class Board { private final Production production = new Production(); + private final Production publicProduction = new Production(); + private final Science science = new Science(); private final TradingRules tradingRules; @@ -53,6 +55,7 @@ public class Board { this.military = new Military(settings); this.pointsPer3Gold = settings.getPointsPer3Gold(); this.production.addFixedResource(wonder.getInitialResource(), 1); + this.publicProduction.addFixedResource(wonder.getInitialResource(), 1); } public Wonder getWonder() { @@ -71,7 +74,7 @@ public class Board { playedCards.add(card); } - public int getNbCardsOfColor(List<Color> colorFilter) { + int getNbCardsOfColor(List<Color> colorFilter) { return (int) playedCards.stream().filter(c -> colorFilter.contains(c.getColor())).count(); } @@ -83,6 +86,10 @@ public class Board { return production; } + public Production getPublicProduction() { + return publicProduction; + } + public TradingRules getTradingRules() { return tradingRules; } diff --git a/backend/src/main/java/org/luxons/sevenwonders/game/data/GameDefinitionLoader.java b/backend/src/main/java/org/luxons/sevenwonders/game/data/GameDefinitionLoader.java index 30457d86..f0da6d63 100644 --- a/backend/src/main/java/org/luxons/sevenwonders/game/data/GameDefinitionLoader.java +++ b/backend/src/main/java/org/luxons/sevenwonders/game/data/GameDefinitionLoader.java @@ -11,6 +11,7 @@ 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; @@ -20,6 +21,7 @@ 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; import org.springframework.stereotype.Component; @@ -74,6 +76,7 @@ public class GameDefinitionLoader { .registerTypeAdapter(Resources.class, new ResourcesSerializer()) .registerTypeAdapter(ResourceType.class, new ResourceTypeSerializer()) .registerTypeAdapter(resourceTypeList, new ResourceTypesSerializer()) + .registerTypeAdapter(Production.class, new ProductionSerializer()) .registerTypeAdapter(ProductionIncrease.class, new ProductionIncreaseSerializer()) .registerTypeAdapter(MilitaryReinforcements.class, new NumericEffectSerializer()) .registerTypeAdapter(RawPointsIncrease.class, new NumericEffectSerializer()) diff --git a/backend/src/main/java/org/luxons/sevenwonders/game/data/serializers/ProductionIncreaseSerializer.java b/backend/src/main/java/org/luxons/sevenwonders/game/data/serializers/ProductionIncreaseSerializer.java index 6c70a44d..b1467900 100644 --- a/backend/src/main/java/org/luxons/sevenwonders/game/data/serializers/ProductionIncreaseSerializer.java +++ b/backend/src/main/java/org/luxons/sevenwonders/game/data/serializers/ProductionIncreaseSerializer.java @@ -1,84 +1,55 @@ 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 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; 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; -public class ProductionIncreaseSerializer implements JsonSerializer<ProductionIncrease>, - JsonDeserializer<ProductionIncrease> { +public class ProductionIncreaseSerializer + implements JsonSerializer<ProductionIncrease>, JsonDeserializer<ProductionIncrease> { @Override public JsonElement serialize(ProductionIncrease productionIncrease, Type typeOfSrc, - JsonSerializationContext context) { + JsonSerializationContext context) { Production production = productionIncrease.getProduction(); - Resources fixedResources = production.getFixedResources(); - List<Set<ResourceType>> choices = production.getAlternativeResources(); - if (fixedResources.isEmpty()) { - return serializeAsChoice(choices, context); - } else if (choices.isEmpty()) { - return serializeAsResources(fixedResources, context); - } else { - throw new IllegalArgumentException("Cannot serialize a production with mixed fixed resources and choices"); + JsonElement json = context.serialize(production); + if (!json.isJsonNull() && !productionIncrease.isSellable()) { + return new JsonPrimitive(wrapInBrackets(json.getAsString())); } + return json; } - private JsonElement serializeAsResources(Resources fixedResources, JsonSerializationContext context) { - return context.serialize(fixedResources); - } - - private JsonElement serializeAsChoice(List<Set<ResourceType>> choices, JsonSerializationContext context) { - if (choices.isEmpty()) { - return JsonNull.INSTANCE; - } - if (choices.size() > 1) { - throw new IllegalArgumentException("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 context.serialize(str); + private String wrapInBrackets(String str) { + return '(' + str + ')'; } @Override - public ProductionIncrease deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws - JsonParseException { - String s = json.getAsString(); + public ProductionIncrease deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) + throws JsonParseException { 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); + + String resourcesStr = json.getAsString(); + boolean isSellable = !resourcesStr.startsWith("("); + if (!isSellable) { + resourcesStr = unwrapBrackets(resourcesStr); + json = new JsonPrimitive(resourcesStr); } + productionIncrease.setSellable(isSellable); + + Production production = context.deserialize(json, Production.class); 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++) { - if (symbols[i].length() != 1) { - throw new IllegalArgumentException("Choice elements must be resource types, got " + symbols[i]); - } - choice[i] = ResourceType.fromSymbol(symbols[i].charAt(0)); - } - return choice; + private static String unwrapBrackets(String str) { + return str.substring(1, str.length() - 1); } } diff --git a/backend/src/main/java/org/luxons/sevenwonders/game/data/serializers/ProductionSerializer.java b/backend/src/main/java/org/luxons/sevenwonders/game/data/serializers/ProductionSerializer.java new file mode 100644 index 00000000..eead39c3 --- /dev/null +++ b/backend/src/main/java/org/luxons/sevenwonders/game/data/serializers/ProductionSerializer.java @@ -0,0 +1,78 @@ +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 org.luxons.sevenwonders.game.resources.Production; +import org.luxons.sevenwonders.game.resources.ResourceType; +import org.luxons.sevenwonders.game.resources.Resources; + +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.JsonSerializationContext; +import com.google.gson.JsonSerializer; + +public class ProductionSerializer implements JsonSerializer<Production>, JsonDeserializer<Production> { + + @Override + public JsonElement serialize(Production production, Type typeOfSrc, JsonSerializationContext context) { + Resources fixedResources = production.getFixedResources(); + List<Set<ResourceType>> choices = production.getAlternativeResources(); + if (fixedResources.isEmpty()) { + return serializeAsChoice(choices, context); + } else if (choices.isEmpty()) { + return serializeAsResources(fixedResources, context); + } else { + throw new IllegalArgumentException("Cannot serialize a production with mixed fixed resources and choices"); + } + } + + private static JsonElement serializeAsChoice(List<Set<ResourceType>> choices, JsonSerializationContext context) { + if (choices.isEmpty()) { + return JsonNull.INSTANCE; + } + if (choices.size() > 1) { + throw new IllegalArgumentException("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 context.serialize(str); + } + + private static JsonElement serializeAsResources(Resources fixedResources, JsonSerializationContext context) { + return context.serialize(fixedResources); + } + + @Override + public Production deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws + JsonParseException { + String resourcesStr = json.getAsString(); + Production production = new Production(); + if (resourcesStr.contains("/")) { + production.addChoice(createChoice(resourcesStr)); + } else { + Resources fixedResources = context.deserialize(json, Resources.class); + production.addAll(fixedResources); + } + return production; + } + + private ResourceType[] createChoice(String choiceStr) { + String[] symbols = choiceStr.split("/"); + ResourceType[] choice = new ResourceType[symbols.length]; + for (int i = 0; i < symbols.length; i++) { + if (symbols[i].length() != 1) { + throw new IllegalArgumentException("Choice elements must be resource types, got " + symbols[i]); + } + choice[i] = ResourceType.fromSymbol(symbols[i].charAt(0)); + } + return choice; + } +} diff --git a/backend/src/main/java/org/luxons/sevenwonders/game/effects/ProductionIncrease.java b/backend/src/main/java/org/luxons/sevenwonders/game/effects/ProductionIncrease.java index 9724dfcd..30bcc625 100644 --- a/backend/src/main/java/org/luxons/sevenwonders/game/effects/ProductionIncrease.java +++ b/backend/src/main/java/org/luxons/sevenwonders/game/effects/ProductionIncrease.java @@ -9,6 +9,8 @@ public class ProductionIncrease extends InstantOwnBoardEffect { private Production production = new Production(); + private boolean sellable = true; + public Production getProduction() { return production; } @@ -17,9 +19,20 @@ public class ProductionIncrease extends InstantOwnBoardEffect { this.production = production; } + public boolean isSellable() { + return sellable; + } + + public void setSellable(boolean sellable) { + this.sellable = sellable; + } + @Override public void apply(Board board) { board.getProduction().addAll(production); + if (sellable) { + board.getPublicProduction().addAll(production); + } } @Override |