Adding support for currency to collection.
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -4,6 +4,8 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public interface CardCollection extends OwnershipCheck {
|
||||
public int getCurrency();
|
||||
|
||||
public Map<String, Integer> getAll();
|
||||
|
||||
public int getItemCount(String blueprintId);
|
||||
|
||||
@@ -6,6 +6,7 @@ import java.util.*;
|
||||
|
||||
public class DefaultCardCollection implements MutableCardCollection {
|
||||
private Map<String, Integer> _counts = new LinkedHashMap<String, Integer>();
|
||||
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);
|
||||
|
||||
@@ -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<String, Integer> 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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user