diff options
Diffstat (limited to 'backend')
11 files changed, 383 insertions, 105 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 diff --git a/backend/src/main/resources/org/luxons/sevenwonders/game/data/cards.json b/backend/src/main/resources/org/luxons/sevenwonders/game/data/cards.json index bf48e95a..83777d9e 100644 --- a/backend/src/main/resources/org/luxons/sevenwonders/game/data/cards.json +++ b/backend/src/main/resources/org/luxons/sevenwonders/game/data/cards.json @@ -733,7 +733,7 @@ "name": "Caravansery", "color": "YELLOW", "effect": { - "production": "W/S/O/C" + "production": "(W/S/O/C)" }, "requirements": { "gold": 0, @@ -756,7 +756,7 @@ "name": "Forum", "color": "YELLOW", "effect": { - "production": "G/P/L" + "production": "(G/P/L)" }, "requirements": { "gold": 0, diff --git a/backend/src/main/resources/org/luxons/sevenwonders/game/data/wonders.json b/backend/src/main/resources/org/luxons/sevenwonders/game/data/wonders.json index 9b4d0587..5beceadd 100644 --- a/backend/src/main/resources/org/luxons/sevenwonders/game/data/wonders.json +++ b/backend/src/main/resources/org/luxons/sevenwonders/game/data/wonders.json @@ -20,7 +20,7 @@ "resources": "OO" }, "effects": { - "production": "W/S/O/C" + "production": "(W/S/O/C)" } }, { @@ -44,7 +44,7 @@ "resources": "CC" }, "effects": { - "production": "W/S/O/C" + "production": "(W/S/O/C)" } }, { @@ -53,7 +53,7 @@ "resources": "WW" }, "effects": { - "production": "G/P/L" + "production": "(G/P/L)" } }, { diff --git a/backend/src/test/java/org/luxons/sevenwonders/game/boards/BoardTest.java b/backend/src/test/java/org/luxons/sevenwonders/game/boards/BoardTest.java index f9117146..5756b36a 100644 --- a/backend/src/test/java/org/luxons/sevenwonders/game/boards/BoardTest.java +++ b/backend/src/test/java/org/luxons/sevenwonders/game/boards/BoardTest.java @@ -62,6 +62,7 @@ public class BoardTest { Board board = new Board(TestUtils.createWonder(type), null, new Settings(5)); Resources resources = TestUtils.createResources(type); assertTrue(board.getProduction().contains(resources)); + assertTrue(board.getPublicProduction().contains(resources)); } @Theory diff --git a/backend/src/test/java/org/luxons/sevenwonders/game/data/serializers/ProductionIncreaseSerializerTest.java b/backend/src/test/java/org/luxons/sevenwonders/game/data/serializers/ProductionIncreaseSerializerTest.java index 17940361..f4b41628 100644 --- a/backend/src/test/java/org/luxons/sevenwonders/game/data/serializers/ProductionIncreaseSerializerTest.java +++ b/backend/src/test/java/org/luxons/sevenwonders/game/data/serializers/ProductionIncreaseSerializerTest.java @@ -26,11 +26,12 @@ public class ProductionIncreaseSerializerTest { gson = new GsonBuilder().registerTypeAdapter(Resources.class, new ResourcesSerializer()) .registerTypeAdapter(ResourceType.class, new ResourceTypeSerializer()) .registerTypeAdapter(resourceTypeList, new ResourceTypesSerializer()) + .registerTypeAdapter(Production.class, new ProductionSerializer()) .registerTypeAdapter(ProductionIncrease.class, new ProductionIncreaseSerializer()) .create(); } - private static ProductionIncrease create(int wood, int stone, int clay) { + private static ProductionIncrease create(boolean sellable, int wood, int stone, int clay) { Production production = new Production(); if (wood > 0) { production.addFixedResource(ResourceType.WOOD, wood); @@ -43,14 +44,16 @@ public class ProductionIncreaseSerializerTest { } ProductionIncrease prodIncrease = new ProductionIncrease(); prodIncrease.setProduction(production); + prodIncrease.setSellable(sellable); return prodIncrease; } - private static ProductionIncrease createChoice(ResourceType... types) { + private static ProductionIncrease createChoice(boolean sellable, ResourceType... types) { Production production = new Production(); production.addChoice(types); ProductionIncrease prodIncrease = new ProductionIncrease(); prodIncrease.setProduction(production); + prodIncrease.setSellable(sellable); return prodIncrease; } @@ -67,62 +70,62 @@ public class ProductionIncreaseSerializerTest { @Test public void serialize_singleType() { - ProductionIncrease prodIncrease = create(1, 0, 0); + ProductionIncrease prodIncrease = create(true, 1, 0, 0); assertEquals("\"W\"", gson.toJson(prodIncrease, ProductionIncrease.class)); } @Test - public void serialize_multipleTimesSameType() { - ProductionIncrease prodIncrease = create(3, 0, 0); - assertEquals("\"WWW\"", gson.toJson(prodIncrease, ProductionIncrease.class)); - } - - @Test public void serialize_mixedTypes() { - ProductionIncrease prodIncrease = create(1, 1, 1); + ProductionIncrease prodIncrease = create(true, 1, 1, 1); assertEquals("\"WSC\"", gson.toJson(prodIncrease, ProductionIncrease.class)); } @Test - public void serialize_mixedTypesMultiple() { - ProductionIncrease prodIncrease = create(2, 1, 2); - assertEquals("\"WWSCC\"", gson.toJson(prodIncrease, ProductionIncrease.class)); + public void serialize_mixedTypes_notSellable() { + ProductionIncrease prodIncrease = create(false, 1, 1, 1); + assertEquals("\"(WSC)\"", gson.toJson(prodIncrease, ProductionIncrease.class)); } @Test public void serialize_choice2() { - ProductionIncrease prodIncrease = createChoice(ResourceType.WOOD, ResourceType.CLAY); + ProductionIncrease prodIncrease = createChoice(true, ResourceType.WOOD, ResourceType.CLAY); assertEquals("\"W/C\"", gson.toJson(prodIncrease, ProductionIncrease.class)); } @Test public void serialize_choice3() { - ProductionIncrease prodIncrease = createChoice(ResourceType.WOOD, ResourceType.ORE, ResourceType.CLAY); + ProductionIncrease prodIncrease = createChoice(true, ResourceType.WOOD, ResourceType.ORE, ResourceType.CLAY); assertEquals("\"W/O/C\"", gson.toJson(prodIncrease, ProductionIncrease.class)); } @Test + public void serialize_choice3_notSellable() { + ProductionIncrease prodIncrease = createChoice(false, ResourceType.WOOD, ResourceType.ORE, ResourceType.CLAY); + assertEquals("\"(W/O/C)\"", gson.toJson(prodIncrease, ProductionIncrease.class)); + } + + @Test public void serialize_choice2_unordered() { - ProductionIncrease prodIncrease = createChoice(ResourceType.CLAY, ResourceType.WOOD); + ProductionIncrease prodIncrease = createChoice(true, ResourceType.CLAY, ResourceType.WOOD); assertEquals("\"W/C\"", gson.toJson(prodIncrease, ProductionIncrease.class)); } @Test public void serialize_choice3_unordered() { - ProductionIncrease prodIncrease = createChoice(ResourceType.WOOD, ResourceType.CLAY, ResourceType.ORE); + ProductionIncrease prodIncrease = createChoice(true, ResourceType.WOOD, ResourceType.CLAY, ResourceType.ORE); assertEquals("\"W/O/C\"", gson.toJson(prodIncrease, ProductionIncrease.class)); } @Test(expected = IllegalArgumentException.class) public void serialize_failIfMultipleChoices() { - ProductionIncrease prodIncrease = createChoice(ResourceType.WOOD, ResourceType.CLAY); + ProductionIncrease prodIncrease = createChoice(true, ResourceType.WOOD, ResourceType.CLAY); prodIncrease.getProduction().addChoice(ResourceType.ORE, ResourceType.GLASS); gson.toJson(prodIncrease, ProductionIncrease.class); } @Test(expected = IllegalArgumentException.class) public void serialize_failIfMixedFixedAndChoices() { - ProductionIncrease prodIncrease = create(1, 0, 0); + ProductionIncrease prodIncrease = create(true, 1, 0, 0); prodIncrease.getProduction().addChoice(ResourceType.WOOD, ResourceType.CLAY); gson.toJson(prodIncrease, ProductionIncrease.class); } @@ -150,50 +153,32 @@ public class ProductionIncreaseSerializerTest { @Test public void deserialize_singleType() { - ProductionIncrease prodIncrease = create(1, 0, 0); + ProductionIncrease prodIncrease = create(true, 1, 0, 0); assertEquals(prodIncrease, gson.fromJson("\"W\"", ProductionIncrease.class)); } @Test - public void deserialize_multipleTimesSameType() { - ProductionIncrease prodIncrease = create(3, 0, 0); - assertEquals(prodIncrease, gson.fromJson("\"WWW\"", ProductionIncrease.class)); + public void deserialize_multipleTimesSameType_notSellable() { + ProductionIncrease prodIncrease = create(false, 3, 0, 0); + assertEquals(prodIncrease, gson.fromJson("\"(WWW)\"", ProductionIncrease.class)); } @Test public void deserialize_mixedTypes() { - ProductionIncrease prodIncrease = create(1, 1, 1); + ProductionIncrease prodIncrease = create(true, 1, 1, 1); assertEquals(prodIncrease, gson.fromJson("\"WCS\"", ProductionIncrease.class)); } @Test - public void deserialize_mixedTypes_unordered() { - ProductionIncrease prodIncrease = create(1, 3, 2); - assertEquals(prodIncrease, gson.fromJson("\"SCWCSS\"", ProductionIncrease.class)); - } - - @Test public void deserialize_choice2() { - ProductionIncrease prodIncrease = createChoice(ResourceType.WOOD, ResourceType.CLAY); - assertEquals(prodIncrease, gson.fromJson("\"W/C\"", ProductionIncrease.class)); - } - - @Test - public void deserialize_choice3() { - ProductionIncrease prodIncrease = createChoice(ResourceType.WOOD, ResourceType.ORE, ResourceType.CLAY); - assertEquals(prodIncrease, gson.fromJson("\"W/O/C\"", ProductionIncrease.class)); - } - - @Test - public void deserialize_choice2_unordered() { - ProductionIncrease prodIncrease = createChoice(ResourceType.CLAY, ResourceType.WOOD); + ProductionIncrease prodIncrease = createChoice(true, ResourceType.WOOD, ResourceType.CLAY); assertEquals(prodIncrease, gson.fromJson("\"W/C\"", ProductionIncrease.class)); } @Test - public void deserialize_choice3_unordered() { - ProductionIncrease prodIncrease = createChoice(ResourceType.WOOD, ResourceType.CLAY, ResourceType.ORE); - assertEquals(prodIncrease, gson.fromJson("\"W/O/C\"", ProductionIncrease.class)); + public void deserialize_choice3_notSellable() { + ProductionIncrease prodIncrease = createChoice(false, ResourceType.WOOD, ResourceType.ORE, ResourceType.CLAY); + assertEquals(prodIncrease, gson.fromJson("\"(W/O/C)\"", ProductionIncrease.class)); } @Test(expected = IllegalArgumentException.class) diff --git a/backend/src/test/java/org/luxons/sevenwonders/game/data/serializers/ProductionSerializerTest.java b/backend/src/test/java/org/luxons/sevenwonders/game/data/serializers/ProductionSerializerTest.java new file mode 100644 index 00000000..34a06ce4 --- /dev/null +++ b/backend/src/test/java/org/luxons/sevenwonders/game/data/serializers/ProductionSerializerTest.java @@ -0,0 +1,198 @@ +package org.luxons.sevenwonders.game.data.serializers; + +import java.lang.reflect.Type; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +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.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.reflect.TypeToken; +import static org.junit.Assert.*; + +public class ProductionSerializerTest { + + private Gson gson; + + @Before + public void setUp() { + Type resourceTypeList = new TypeToken<List<ResourceType>>() {}.getType(); + gson = new GsonBuilder().registerTypeAdapter(Resources.class, new ResourcesSerializer()) + .registerTypeAdapter(ResourceType.class, new ResourceTypeSerializer()) + .registerTypeAdapter(resourceTypeList, new ResourceTypesSerializer()) + .registerTypeAdapter(Production.class, new ProductionSerializer()) + .create(); + } + + private static Production create(int wood, int stone, int clay) { + Production production = new Production(); + if (wood > 0) { + production.addFixedResource(ResourceType.WOOD, wood); + } + if (stone > 0) { + production.addFixedResource(ResourceType.STONE, stone); + } + if (clay > 0) { + production.addFixedResource(ResourceType.CLAY, clay); + } + return production; + } + + private static Production createChoice(ResourceType... types) { + Production production = new Production(); + production.addChoice(types); + return production; + } + + @Test + public void serialize_nullAsNull() { + assertEquals("null", gson.toJson(null, Production.class)); + } + + @Test + public void serialize_emptyProdIncreaseAsNull() { + Production prodIncrease = new Production(); + assertEquals("null", gson.toJson(prodIncrease, Production.class)); + } + + @Test + public void serialize_singleType() { + Production prodIncrease = create(1, 0, 0); + assertEquals("\"W\"", gson.toJson(prodIncrease, Production.class)); + } + + @Test + public void serialize_multipleTimesSameType() { + Production prodIncrease = create(3, 0, 0); + assertEquals("\"WWW\"", gson.toJson(prodIncrease, Production.class)); + } + + @Test + public void serialize_mixedTypes() { + Production prodIncrease = create(1, 1, 1); + assertEquals("\"WSC\"", gson.toJson(prodIncrease, Production.class)); + } + + @Test + public void serialize_mixedTypesMultiple() { + Production prodIncrease = create(2, 1, 2); + assertEquals("\"WWSCC\"", gson.toJson(prodIncrease, Production.class)); + } + + @Test + public void serialize_choice2() { + Production prodIncrease = createChoice(ResourceType.WOOD, ResourceType.CLAY); + assertEquals("\"W/C\"", gson.toJson(prodIncrease, Production.class)); + } + + @Test + public void serialize_choice3() { + Production prodIncrease = createChoice(ResourceType.WOOD, ResourceType.ORE, ResourceType.CLAY); + assertEquals("\"W/O/C\"", gson.toJson(prodIncrease, Production.class)); + } + + @Test + public void serialize_choice2_unordered() { + Production prodIncrease = createChoice(ResourceType.CLAY, ResourceType.WOOD); + assertEquals("\"W/C\"", gson.toJson(prodIncrease, Production.class)); + } + + @Test + public void serialize_choice3_unordered() { + Production prodIncrease = createChoice(ResourceType.WOOD, ResourceType.CLAY, ResourceType.ORE); + assertEquals("\"W/O/C\"", gson.toJson(prodIncrease, Production.class)); + } + + @Test(expected = IllegalArgumentException.class) + public void serialize_failIfMultipleChoices() { + Production production = createChoice(ResourceType.WOOD, ResourceType.CLAY); + production.addChoice(ResourceType.ORE, ResourceType.GLASS); + gson.toJson(production, Production.class); + } + + @Test(expected = IllegalArgumentException.class) + public void serialize_failIfMixedFixedAndChoices() { + Production production = create(1, 0, 0); + production.addChoice(ResourceType.WOOD, ResourceType.CLAY); + gson.toJson(production, Production.class); + } + + @Test + public void deserialize_nullFromNull() { + assertNull(gson.fromJson("null", Production.class)); + } + + @Test + public void deserialize_emptyList() { + Production prodIncrease = new Production(); + assertEquals(prodIncrease, gson.fromJson("\"\"", Production.class)); + } + + @Test(expected = IllegalArgumentException.class) + public void deserialize_failOnGarbageString() { + gson.fromJson("\"this is garbage\"", Production.class); + } + + @Test(expected = IllegalArgumentException.class) + public void deserialize_failOnGarbageStringWithSlashes() { + gson.fromJson("\"this/is/garbage\"", Production.class); + } + + @Test + public void deserialize_singleType() { + Production prodIncrease = create(1, 0, 0); + assertEquals(prodIncrease, gson.fromJson("\"W\"", Production.class)); + } + + @Test + public void deserialize_multipleTimesSameType() { + Production prodIncrease = create(3, 0, 0); + assertEquals(prodIncrease, gson.fromJson("\"WWW\"", Production.class)); + } + + @Test + public void deserialize_mixedTypes() { + Production prodIncrease = create(1, 1, 1); + assertEquals(prodIncrease, gson.fromJson("\"WCS\"", Production.class)); + } + + @Test + public void deserialize_mixedTypes_unordered() { + Production prodIncrease = create(1, 3, 2); + assertEquals(prodIncrease, gson.fromJson("\"SCWCSS\"", Production.class)); + } + + @Test + public void deserialize_choice2() { + Production prodIncrease = createChoice(ResourceType.WOOD, ResourceType.CLAY); + assertEquals(prodIncrease, gson.fromJson("\"W/C\"", Production.class)); + } + + @Test + public void deserialize_choice3() { + Production prodIncrease = createChoice(ResourceType.WOOD, ResourceType.ORE, ResourceType.CLAY); + assertEquals(prodIncrease, gson.fromJson("\"W/O/C\"", Production.class)); + } + + @Test + public void deserialize_choice2_unordered() { + Production prodIncrease = createChoice(ResourceType.CLAY, ResourceType.WOOD); + assertEquals(prodIncrease, gson.fromJson("\"W/C\"", Production.class)); + } + + @Test + public void deserialize_choice3_unordered() { + Production prodIncrease = createChoice(ResourceType.WOOD, ResourceType.CLAY, ResourceType.ORE); + assertEquals(prodIncrease, gson.fromJson("\"W/O/C\"", Production.class)); + } + + @Test(expected = IllegalArgumentException.class) + public void deserialize_failOnMultipleResourcesInChoice() { + gson.fromJson("\"W/SS/C\"", Production.class); + } + +}
\ No newline at end of file diff --git a/backend/src/test/java/org/luxons/sevenwonders/game/effects/ProductionIncreaseTest.java b/backend/src/test/java/org/luxons/sevenwonders/game/effects/ProductionIncreaseTest.java index 6031e112..d19f6288 100644 --- a/backend/src/test/java/org/luxons/sevenwonders/game/effects/ProductionIncreaseTest.java +++ b/backend/src/test/java/org/luxons/sevenwonders/game/effects/ProductionIncreaseTest.java @@ -28,17 +28,39 @@ public class ProductionIncreaseTest { } @Theory - public void apply_boardContainsAddedResourceType(ResourceType initialType, ResourceType addedType, ResourceType extraType) { + public void apply_boardContainsAddedResourceType(ResourceType initialType, ResourceType addedType, + ResourceType extraType) { Board board = TestUtils.createBoard(initialType); ProductionIncrease effect = createProductionIncrease(addedType); + effect.setSellable(false); effect.apply(board); Resources resources = TestUtils.createResources(initialType, addedType); assertTrue(board.getProduction().contains(resources)); + assertFalse(board.getPublicProduction().contains(resources)); Resources moreResources = TestUtils.createResources(initialType, addedType, extraType); assertFalse(board.getProduction().contains(moreResources)); + assertFalse(board.getPublicProduction().contains(moreResources)); + } + + @Theory + public void apply_boardContainsAddedResourceType_sellable(ResourceType initialType, ResourceType addedType, + ResourceType extraType) { + Board board = TestUtils.createBoard(initialType); + ProductionIncrease effect = createProductionIncrease(addedType); + effect.setSellable(true); + + effect.apply(board); + + Resources resources = TestUtils.createResources(initialType, addedType); + assertTrue(board.getProduction().contains(resources)); + assertTrue(board.getPublicProduction().contains(resources)); + + Resources moreResources = TestUtils.createResources(initialType, addedType, extraType); + assertFalse(board.getProduction().contains(moreResources)); + assertFalse(board.getPublicProduction().contains(moreResources)); } @Theory |