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 2098fd91b..ed241c8f5 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 @@ -85,8 +85,14 @@ public class CollectionSerializer { } public void serializeCollection(CardCollection collection, OutputStream outputStream) throws IOException { - byte version = 0; + byte version = 1; outputStream.write(version); + + int currency = collection.getCurrency(); + outputStream.write((currency >> 16) & 0x000000ff); + outputStream.write((currency >> 8) & 0x000000ff); + outputStream.write(currency & 0x000000ff); + byte packBytes = (byte) _packIds.size(); outputStream.write(packBytes); @@ -116,6 +122,8 @@ public class CollectionSerializer { int version = inputStream.read(); if (version == 0) { return deserializeCollectionVer0(new BufferedInputStream(inputStream)); + } else if (version == 1) { + return deserializeCollectionVer1(new BufferedInputStream(inputStream)); } else { throw new IllegalStateException("Unkown version of serialized collection: " + version); } @@ -138,7 +146,41 @@ public class CollectionSerializer { byte[] cards = new byte[cardBytes]; read = inputStream.read(cards); if (read != cardBytes) - throw new IllegalArgumentException("Under-read the packs information"); + throw new IllegalArgumentException("Under-read the cards information"); + for (int i = 0; i < cards.length; i++) + if (cards[i] > 0) { + final String blueprintId = _cardIds.get(i); + collection.addItem(blueprintId, cards[i]); + } + + return collection; + } + + private MutableCardCollection deserializeCollectionVer1(BufferedInputStream inputStream) throws IOException { + int byte1 = inputStream.read(); + int byte2 = inputStream.read(); + int byte3 = inputStream.read(); + + int currency = (byte1 << 16) + (byte2 << 8) + byte3; + + int packBytes = inputStream.read(); + + DefaultCardCollection collection = new DefaultCardCollection(); + collection.addCurrency(currency); + + byte[] packs = new byte[packBytes]; + int read = inputStream.read(packs); + if (read != packBytes) + throw new IllegalStateException("Under-read the packs information"); + for (int i = 0; i < packs.length; i++) + if (packs[i] > 0) + collection.addItem(_packIds.get(i), packs[i]); + + int cardBytes = (inputStream.read() << 8) + 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++) if (cards[i] > 0) { final String blueprintId = _cardIds.get(i); 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 749cf6e49..ec24d5968 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,6 +4,8 @@ import java.util.List; import java.util.Map; public interface CardCollection extends OwnershipCheck { + public int getCurrency(); + public Map getAll(); public int getItemCount(String blueprintId); 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 6f416dbc5..0392d5331 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 @@ -6,6 +6,7 @@ import java.util.*; public class DefaultCardCollection implements MutableCardCollection { private Map _counts = new LinkedHashMap(); + private int _currency; public DefaultCardCollection() { @@ -15,6 +16,22 @@ public class DefaultCardCollection implements MutableCardCollection { _counts.putAll(cardCollection.getAll()); } + public void addCurrency(int currency) { + _currency += currency; + } + + public boolean removeCurrency(int currency) { + if (_currency < currency) + return false; + _currency -= currency; + return true; + } + + @Override + public int getCurrency() { + return _currency; + } + @Override public void addItem(String itemId, int count) { Integer oldCount = _counts.get(itemId); 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 2ba612773..aa597c2b8 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,7 +2,7 @@ package com.gempukku.lotro.collection; import com.gempukku.lotro.game.CardCollection; import com.gempukku.lotro.game.DefaultCardCollection; -import com.gempukku.lotro.game.LotroCardBlueprintLibrary; +import static junit.framework.Assert.assertEquals; import org.junit.Test; import java.io.ByteArrayInputStream; @@ -10,14 +10,11 @@ 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 { - LotroCardBlueprintLibrary library = new LotroCardBlueprintLibrary(); - DefaultCardCollection collection = new DefaultCardCollection(); + collection.addCurrency(256 * 256 * 250); collection.addItem("1_1", 2); collection.addItem("1_231T", 3); collection.addItem("1_23*", 3); @@ -35,6 +32,7 @@ public class CollectionSerializerTest { final Map result = resultCollection.getAll(); assertEquals(5, result.size()); + assertEquals(256 * 256 * 250, resultCollection.getCurrency()); assertEquals(2, (int) result.get("1_1")); assertEquals(3, (int) result.get("1_231T")); assertEquals(3, (int) result.get("1_23*")); @@ -44,8 +42,6 @@ public class CollectionSerializerTest { @Test public void testJustPack() throws IOException { - LotroCardBlueprintLibrary library = new LotroCardBlueprintLibrary(); - DefaultCardCollection collection = new DefaultCardCollection(); collection.addItem("FotR - Booster", 8);