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 c1e6a34a6..f795543af 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 @@ -3,6 +3,10 @@ package com.gempukku.lotro.collection; import com.gempukku.lotro.game.CardCollection; import com.gempukku.lotro.game.DefaultCardCollection; import com.gempukku.lotro.game.MutableCardCollection; +import com.gempukku.lotro.game.formats.LotroFormatLibrary; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; import java.io.*; import java.util.ArrayList; @@ -96,7 +100,7 @@ public class CollectionSerializer { } public void serializeCollection(CardCollection collection, OutputStream outputStream) throws IOException { - byte version = 3; + byte version = 4; outputStream.write(version); int currency = collection.getCurrency(); @@ -129,6 +133,14 @@ public class CollectionSerializer { printInt(outputStream, cardCount, 1); } } + + Map extraInformation = collection.getExtraInformation(); + JSONObject json= new JSONObject(); + json.putAll(extraInformation); + + OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8"); + writer.write(json.toJSONString()); + writer.flush(); } public MutableCardCollection deserializeCollection(InputStream inputStream) throws IOException { @@ -141,6 +153,8 @@ public class CollectionSerializer { return deserializeCollectionVer2(new BufferedInputStream(inputStream)); } else if (version == 3) { return deserializeCollectionVer3(new BufferedInputStream(inputStream)); + } else if (version == 4) { + return deserializeCollectionVer4(new BufferedInputStream(inputStream)); } else { throw new IllegalStateException("Unkown version of serialized collection: " + version); } @@ -273,7 +287,7 @@ public class CollectionSerializer { throw new IllegalArgumentException("Under-read the cards information"); for (int i = 0; i < cards.length; i++) { int count = convertToInt(cards[i]); - if (count>0) { + if (count > 0) { final String blueprintId = _singleByteCountItems.get(i); collection.addItem(blueprintId, count); } @@ -282,12 +296,60 @@ public class CollectionSerializer { return collection; } + private MutableCardCollection deserializeCollectionVer4(BufferedInputStream inputStream) throws IOException { + DefaultCardCollection collection = new DefaultCardCollection(); + + int byte1 = inputStream.read(); + int byte2 = inputStream.read(); + int byte3 = inputStream.read(); + int currency = convertToInt(byte1, byte2, byte3); + collection.addCurrency(currency); + + int packTypes = convertToInt(inputStream.read()); + + byte[] packs = new byte[packTypes * 2]; + + int read = inputStream.read(packs); + if (read != packTypes * 2) + throw new IllegalStateException("Under-read the packs information"); + for (int i = 0; i < packTypes; i++) { + int count = convertToInt(packs[i * 2], packs[i * 2 + 1]); + if (count > 0) + collection.addItem(_doubleByteCountItems.get(i), count); + } + + int cardBytes = convertToInt(inputStream.read(), inputStream.read()); + byte[] cards = new byte[cardBytes]; + read = inputStream.read(cards); + if (read != cardBytes) + throw new IllegalArgumentException("Under-read the cards information"); + for (int i = 0; i < cards.length; i++) { + int count = convertToInt(cards[i]); + if (count > 0) { + final String blueprintId = _singleByteCountItems.get(i); + collection.addItem(blueprintId, count); + } + } + + Reader reader = new InputStreamReader(inputStream, "UTF-8"); + + JSONParser parser = new JSONParser(); + try { + JSONObject object = (JSONObject) parser.parse(reader); + collection.setExtraInformation(object); + } catch (ParseException exp) { + throw new IOException(exp); + } + + return collection; + } + private int convertToInt(int... bytes) { int result = 0; for (int i = 0; i < bytes.length; i++) { int value = bytes[i] << ((bytes.length - i - 1) * 8); if (value < 0) - value +=256; + value += 256; result += value; } return result; diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/CardCollection.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/CardCollection.java index ca236ae6d..47c363ae9 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/CardCollection.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/CardCollection.java @@ -4,13 +4,15 @@ import java.util.Map; import java.util.Set; public interface CardCollection extends OwnershipCheck { - public int getCurrency(); + int getCurrency(); - public Map getAll(); + Map getAll(); - public int getItemCount(String blueprintId); + int getItemCount(String blueprintId); - public Set getAllCardsInCollection(); + Set getAllCardsInCollection(); + + Map getExtraInformation(); public static class Item implements CardItem { public enum Type { diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/DefaultCardCollection.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/DefaultCardCollection.java index 21a2133a7..59e28da19 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/DefaultCardCollection.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/DefaultCardCollection.java @@ -2,18 +2,13 @@ package com.gempukku.lotro.game; import com.gempukku.lotro.packs.PacksStorage; -import java.util.Collections; -import java.util.HashSet; -import java.util.LinkedHashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; public class DefaultCardCollection implements MutableCardCollection { private Map _counts = new LinkedHashMap(); private Set _basicItems = new HashSet(); private int _currency; + private Map _extraInformation = new HashMap(); public DefaultCardCollection() { @@ -25,6 +20,15 @@ public class DefaultCardCollection implements MutableCardCollection { _currency = cardCollection.getCurrency(); } + public synchronized void setExtraInformation(Map extraInformation) { + _extraInformation = extraInformation; + } + + @Override + public synchronized Map getExtraInformation() { + return Collections.unmodifiableMap(_extraInformation); + } + @Override public synchronized void addCurrency(int currency) { _currency += currency; diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/MutableCardCollection.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/MutableCardCollection.java index d216e2473..04ff1ed04 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/MutableCardCollection.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/MutableCardCollection.java @@ -2,14 +2,18 @@ package com.gempukku.lotro.game; import com.gempukku.lotro.packs.PacksStorage; +import java.util.Map; + public interface MutableCardCollection extends CardCollection { - public void addItem(String itemId, int count); + void addItem(String itemId, int count); - public boolean removeItem(String itemId, int count); + boolean removeItem(String itemId, int count); - public void addCurrency(int currency); + void addCurrency(int currency); - public boolean removeCurrency(int currency); + boolean removeCurrency(int currency); - public CardCollection openPack(String packId, String selection, PacksStorage packBox); + CardCollection openPack(String packId, String selection, PacksStorage packBox); + + void setExtraInformation(Map extraInformation); } diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/SumCardCollection.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/SumCardCollection.java index 939641db0..2cbe8a4a4 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/SumCardCollection.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/game/SumCardCollection.java @@ -1,5 +1,7 @@ package com.gempukku.lotro.game; +import org.apache.commons.collections.map.HashedMap; + import java.util.*; public class SumCardCollection implements CardCollection { @@ -18,6 +20,15 @@ public class SumCardCollection implements CardCollection { return sum; } + @Override + public Map getExtraInformation() { + Map result = new HashMap(); + for (CardCollection cardCollection : _cardCollections) { + result.putAll(cardCollection.getExtraInformation()); + } + return result; + } + @Override public Map getAll() { Map sum = new HashMap(); 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 d769941e7..7045de89c 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 @@ -9,6 +9,7 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; +import java.util.Collections; import java.util.Map; import static junit.framework.Assert.assertEquals; @@ -61,6 +62,22 @@ public class CollectionSerializerTest { } } + @Test + public void testExtraInfo() throws IOException { + DefaultCardCollection collection = new DefaultCardCollection(); + collection.addCurrency(12); + collection.addItem("15_4*", 2); + + collection.setExtraInformation(Collections.singletonMap("a", (Object)"b")); + + CardCollection resultCollection = serializeAndDeserialize(collection); + assertEquals(12, resultCollection.getCurrency()); + assertEquals(1, resultCollection.getAll().size()); + assertEquals(2, resultCollection.getAll().get("15_4*").getCount()); + assertEquals(1, resultCollection.getExtraInformation().size()); + assertEquals("b", resultCollection.getExtraInformation().get("a")); + } + @Test public void testJustPack() throws IOException { DefaultCardCollection collection = new DefaultCardCollection();