From 54102658758b64f4ff53e1735a60b5e09117db21 Mon Sep 17 00:00:00 2001 From: "marcins78@gmail.com" Date: Thu, 8 Nov 2012 00:35:03 +0000 Subject: [PATCH] Fixing format validation and collection serialization problems. --- .../collection/CollectionSerializer.java | 8 +++- .../game/formats/DefaultLotroFormat.java | 30 +++++++------- .../collection/CollectionSerializerTest.java | 39 ++++++++++++++++++- .../gempukku/lotro/server/DeckResource.java | 8 +++- 4 files changed, 66 insertions(+), 19 deletions(-) diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/collection/CollectionSerializer.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/collection/CollectionSerializer.java index 1e91aa7d2..e48dc9e1f 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/collection/CollectionSerializer.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/collection/CollectionSerializer.java @@ -229,8 +229,12 @@ public class CollectionSerializer { private int convertToInt(int... bytes) { int result = 0; - for (int i = 0; i < bytes.length; i++) - result += (bytes[i] << ((bytes.length - i - 1) * 8)); + for (int i = 0; i < bytes.length; i++) { + int value = bytes[i] << ((bytes.length - i - 1) * 8); + if (value < 0) + value +=256; + result += value; + } return result; } diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/formats/DefaultLotroFormat.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/formats/DefaultLotroFormat.java index d495ccffd..5b797de28 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/formats/DefaultLotroFormat.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/formats/DefaultLotroFormat.java @@ -130,19 +130,25 @@ public class DefaultLotroFormat implements LotroFormat { if (_validCards.contains(blueprintId)) return; - for (int validSet : _validSets) - if (!blueprintId.startsWith(validSet + "_") - && !_library.hasAlternateInSet(blueprintId, validSet)) - throw new DeckInvalidException("Deck contains card not from valid set: " + GameUtils.getFullName(_library.getLotroCardBlueprint(blueprintId))); + if (_validSets.size() > 0 && !isValidInSets(blueprintId)) + throw new DeckInvalidException("Deck contains card not from valid set: " + GameUtils.getFullName(_library.getLotroCardBlueprint(blueprintId))); // Banned cards Set allAlternates = _library.getAllAlternates(blueprintId); for (String bannedBlueprintId : _bannedCards) { - if (bannedBlueprintId.equals(blueprintId) || allAlternates.contains(bannedBlueprintId)) + if (bannedBlueprintId.equals(blueprintId) || (allAlternates != null && allAlternates.contains(bannedBlueprintId))) throw new DeckInvalidException("Deck contains a copy of an X-listed card: " + GameUtils.getFullName(_library.getLotroCardBlueprint(bannedBlueprintId))); } } + private boolean isValidInSets(String blueprintId) throws DeckInvalidException { + for (int validSet : _validSets) + if (blueprintId.startsWith(validSet + "_") + || _library.hasAlternateInSet(blueprintId, validSet)) + return true; + return false; + } + @Override public void validateDeck(LotroDeck deck) throws DeckInvalidException { try { @@ -186,14 +192,12 @@ public class DefaultLotroFormat implements LotroFormat { } } - if (_validSets.size() > 0) { - validateCard(deck.getRingBearer()); - validateCard(deck.getRing()); - for (String site : deck.getSites()) - validateCard(site); - for (String card : deck.getAdventureCards()) - validateCard(card); - } + validateCard(deck.getRingBearer()); + validateCard(deck.getRing()); + for (String site : deck.getSites()) + validateCard(site); + for (String card : deck.getAdventureCards()) + validateCard(card); if (isOrderedSites()) { boolean[] sites = new boolean[9]; diff --git a/gemp-lotr/gemp-lotr-server/src/test/java/com/gempukku/lotro/collection/CollectionSerializerTest.java b/gemp-lotr/gemp-lotr-server/src/test/java/com/gempukku/lotro/collection/CollectionSerializerTest.java index 2dcad64ba..5fdc4182c 100644 --- a/gemp-lotr/gemp-lotr-server/src/test/java/com/gempukku/lotro/collection/CollectionSerializerTest.java +++ b/gemp-lotr/gemp-lotr-server/src/test/java/com/gempukku/lotro/collection/CollectionSerializerTest.java @@ -2,6 +2,7 @@ package com.gempukku.lotro.collection; import com.gempukku.lotro.game.CardCollection; import com.gempukku.lotro.game.DefaultCardCollection; +import static junit.framework.Assert.assertEquals; import org.junit.Test; import java.io.ByteArrayInputStream; @@ -9,8 +10,6 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.Map; -import static junit.framework.Assert.assertEquals; - public class CollectionSerializerTest { @Test public void testSerializeDeserialize() throws IOException { @@ -59,4 +58,40 @@ public class CollectionSerializerTest { assertEquals(1, result.size()); assertEquals(8, (int) result.get("FotR - Booster").getCount()); } + + @Test + public void testLotsOfPacks() throws IOException { + DefaultCardCollection collection = new DefaultCardCollection(); + collection.addItem("FotR - Booster", 500); + + CollectionSerializer serializer = new CollectionSerializer(); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + serializer.serializeCollection(collection, baos); + + final byte[] bytes = baos.toByteArray(); + ByteArrayInputStream bais = new ByteArrayInputStream(bytes); + CardCollection resultCollection = serializer.deserializeCollection(bais); + + final Map result = resultCollection.getAll(); + assertEquals(1, result.size()); + assertEquals(500, (int) result.get("FotR - Booster").getCount()); + } + + @Test + public void testLotsOfCurrency() throws IOException { + DefaultCardCollection collection = new DefaultCardCollection(); + collection.addCurrency(127*255); + + CollectionSerializer serializer = new CollectionSerializer(); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + serializer.serializeCollection(collection, baos); + + final byte[] bytes = baos.toByteArray(); + ByteArrayInputStream bais = new ByteArrayInputStream(bytes); + CardCollection resultCollection = serializer.deserializeCollection(bais); + + assertEquals(127*255, resultCollection.getCurrency()); + } } diff --git a/gemp-lotr/gemp-lotr-web-server/src/main/java/com/gempukku/lotro/server/DeckResource.java b/gemp-lotr/gemp-lotr-web-server/src/main/java/com/gempukku/lotro/server/DeckResource.java index 6accfca38..6f48868ca 100644 --- a/gemp-lotr/gemp-lotr-web-server/src/main/java/com/gempukku/lotro/server/DeckResource.java +++ b/gemp-lotr/gemp-lotr-web-server/src/main/java/com/gempukku/lotro/server/DeckResource.java @@ -220,14 +220,18 @@ public class DeckResource extends AbstractResource { StringBuilder sb = new StringBuilder(); sb.append("Free People: " + fpCount + ", Shadow: " + shadowCount + "
"); + StringBuilder valid = new StringBuilder(); + StringBuilder invalid = new StringBuilder(); for (LotroFormat format : _formatLibrary.getHallFormats().values()) { try { format.validateDeck(deck); - sb.append("" + format.getName() + ": valid
"); + valid.append("" + format.getName() + ": valid
"); } catch (DeckInvalidException exp) { - sb.append("" + format.getName() + ": " + exp.getMessage() + "
"); + invalid.append("" + format.getName() + ": " + exp.getMessage() + "
"); } } + sb.append(valid); + sb.append(invalid); return sb.toString(); }