Storing additional information in collection is now possible
This commit is contained in:
@@ -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<String, Object> 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;
|
||||
|
||||
@@ -4,13 +4,15 @@ import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public interface CardCollection extends OwnershipCheck {
|
||||
public int getCurrency();
|
||||
int getCurrency();
|
||||
|
||||
public Map<String, Item> getAll();
|
||||
Map<String, Item> getAll();
|
||||
|
||||
public int getItemCount(String blueprintId);
|
||||
int getItemCount(String blueprintId);
|
||||
|
||||
public Set<BasicCardItem> getAllCardsInCollection();
|
||||
Set<BasicCardItem> getAllCardsInCollection();
|
||||
|
||||
Map<String, Object> getExtraInformation();
|
||||
|
||||
public static class Item implements CardItem {
|
||||
public enum Type {
|
||||
|
||||
@@ -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<String, Item> _counts = new LinkedHashMap<String, Item>();
|
||||
private Set<BasicCardItem> _basicItems = new HashSet<BasicCardItem>();
|
||||
private int _currency;
|
||||
private Map<String, Object> _extraInformation = new HashMap<String, Object>();
|
||||
|
||||
public DefaultCardCollection() {
|
||||
|
||||
@@ -25,6 +20,15 @@ public class DefaultCardCollection implements MutableCardCollection {
|
||||
_currency = cardCollection.getCurrency();
|
||||
}
|
||||
|
||||
public synchronized void setExtraInformation(Map<String, Object> extraInformation) {
|
||||
_extraInformation = extraInformation;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized Map<String, Object> getExtraInformation() {
|
||||
return Collections.unmodifiableMap(_extraInformation);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void addCurrency(int currency) {
|
||||
_currency += currency;
|
||||
|
||||
@@ -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<String, Object> extraInformation);
|
||||
}
|
||||
|
||||
@@ -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<String, Object> getExtraInformation() {
|
||||
Map<String, Object> result = new HashMap<String, Object>();
|
||||
for (CardCollection cardCollection : _cardCollections) {
|
||||
result.putAll(cardCollection.getExtraInformation());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Item> getAll() {
|
||||
Map<String, Item> sum = new HashMap<String, Item>();
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user