Capping card count at 255.

This commit is contained in:
marcins78@gmail.com
2012-12-16 17:36:37 +00:00
parent 5e96bef638
commit 5b9bf203eb
2 changed files with 80 additions and 35 deletions

View File

@@ -85,7 +85,7 @@ public class CollectionSerializer {
}
public void serializeCollection(CardCollection collection, OutputStream outputStream) throws IOException {
byte version = 2;
byte version = 3;
outputStream.write(version);
int currency = collection.getCurrency();
@@ -111,8 +111,11 @@ public class CollectionSerializer {
final CardCollection.Item count = collectionCounts.get(cardId);
if (count == null)
outputStream.write(0);
else
outputStream.write(count.getCount());
else {
// Apply the maximum of 255
int cardCount = Math.min(255, count.getCount());
printInt(outputStream, cardCount, 1);
}
}
}
@@ -124,6 +127,8 @@ public class CollectionSerializer {
return deserializeCollectionVer1(new BufferedInputStream(inputStream));
} else if (version == 2) {
return deserializeCollectionVer2(new BufferedInputStream(inputStream));
} else if (version == 3) {
return deserializeCollectionVer3(new BufferedInputStream(inputStream));
} else {
throw new IllegalStateException("Unkown version of serialized collection: " + version);
}
@@ -227,6 +232,45 @@ public class CollectionSerializer {
return collection;
}
private MutableCardCollection deserializeCollectionVer3(BufferedInputStream inputStream) throws IOException {
int byte1 = inputStream.read();
int byte2 = inputStream.read();
int byte3 = inputStream.read();
int currency = convertToInt(byte1, byte2, byte3);
int packTypes = inputStream.read();
DefaultCardCollection collection = new DefaultCardCollection();
collection.addCurrency(currency);
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(_packIds.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 = _cardIds.get(i);
collection.addItem(blueprintId, count);
}
}
return collection;
}
private int convertToInt(int... bytes) {
int result = 0;
for (int i = 0; i < bytes.length; i++) {

View File

@@ -11,6 +11,8 @@ import java.io.IOException;
import java.util.Map;
public class CollectionSerializerTest {
private CollectionSerializer _serializer = new CollectionSerializer();
@Test
public void testSerializeDeserialize() throws IOException {
DefaultCardCollection collection = new DefaultCardCollection();
@@ -21,14 +23,7 @@ public class CollectionSerializerTest {
collection.addItem("1_237T*", 3);
collection.addItem("FotR - Booster", 2);
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);
CardCollection resultCollection = serializeAndDeserialize(collection);
final Map<String, CardCollection.Item> result = resultCollection.getAll();
assertEquals(5, result.size());
@@ -45,33 +40,29 @@ public class CollectionSerializerTest {
DefaultCardCollection collection = new DefaultCardCollection();
collection.addItem("FotR - Booster", 8);
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);
CardCollection resultCollection = serializeAndDeserialize(collection);
final Map<String, CardCollection.Item> result = resultCollection.getAll();
assertEquals(1, result.size());
assertEquals(8, result.get("FotR - Booster").getCount());
}
private CardCollection serializeAndDeserialize(DefaultCardCollection collection) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
_serializer.serializeCollection(collection, baos);
final byte[] bytes = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
CardCollection resultCollection = _serializer.deserializeCollection(bais);
return resultCollection;
}
@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);
CardCollection resultCollection = serializeAndDeserialize(collection);
final Map<String, CardCollection.Item> result = resultCollection.getAll();
assertEquals(1, result.size());
@@ -83,15 +74,25 @@ public class CollectionSerializerTest {
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);
CardCollection resultCollection = serializeAndDeserialize(collection);
assertEquals(127*255, resultCollection.getCurrency());
}
@Test
public void testCardCount() throws IOException {
DefaultCardCollection collection = new DefaultCardCollection();
collection.addItem("1_1", 127);
assertEquals(127, serializeAndDeserialize(collection).getItemCount("1_1"));
collection.addItem("1_1", 1);
assertEquals(128, serializeAndDeserialize(collection).getItemCount("1_1"));
collection.addItem("1_1", 127);
assertEquals(255, serializeAndDeserialize(collection).getItemCount("1_1"));
// Card number is capped at 255
collection.addItem("1_1", 1);
assertEquals(255, serializeAndDeserialize(collection).getItemCount("1_1"));
}
}