Fixing format validation and collection serialization problems.
This commit is contained in:
@@ -229,8 +229,12 @@ public class CollectionSerializer {
|
||||
|
||||
private int convertToInt(int... bytes) {
|
||||
int result = 0;
|
||||
for (int i = 0; i < bytes.length; i++)
|
||||
result += (bytes[i] << ((bytes.length - i - 1) * 8));
|
||||
for (int i = 0; i < bytes.length; i++) {
|
||||
int value = bytes[i] << ((bytes.length - i - 1) * 8);
|
||||
if (value < 0)
|
||||
value +=256;
|
||||
result += value;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@@ -130,19 +130,25 @@ public class DefaultLotroFormat implements LotroFormat {
|
||||
if (_validCards.contains(blueprintId))
|
||||
return;
|
||||
|
||||
for (int validSet : _validSets)
|
||||
if (!blueprintId.startsWith(validSet + "_")
|
||||
&& !_library.hasAlternateInSet(blueprintId, validSet))
|
||||
throw new DeckInvalidException("Deck contains card not from valid set: " + GameUtils.getFullName(_library.getLotroCardBlueprint(blueprintId)));
|
||||
if (_validSets.size() > 0 && !isValidInSets(blueprintId))
|
||||
throw new DeckInvalidException("Deck contains card not from valid set: " + GameUtils.getFullName(_library.getLotroCardBlueprint(blueprintId)));
|
||||
|
||||
// Banned cards
|
||||
Set<String> allAlternates = _library.getAllAlternates(blueprintId);
|
||||
for (String bannedBlueprintId : _bannedCards) {
|
||||
if (bannedBlueprintId.equals(blueprintId) || allAlternates.contains(bannedBlueprintId))
|
||||
if (bannedBlueprintId.equals(blueprintId) || (allAlternates != null && allAlternates.contains(bannedBlueprintId)))
|
||||
throw new DeckInvalidException("Deck contains a copy of an X-listed card: " + GameUtils.getFullName(_library.getLotroCardBlueprint(bannedBlueprintId)));
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isValidInSets(String blueprintId) throws DeckInvalidException {
|
||||
for (int validSet : _validSets)
|
||||
if (blueprintId.startsWith(validSet + "_")
|
||||
|| _library.hasAlternateInSet(blueprintId, validSet))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void validateDeck(LotroDeck deck) throws DeckInvalidException {
|
||||
try {
|
||||
@@ -186,14 +192,12 @@ public class DefaultLotroFormat implements LotroFormat {
|
||||
}
|
||||
}
|
||||
|
||||
if (_validSets.size() > 0) {
|
||||
validateCard(deck.getRingBearer());
|
||||
validateCard(deck.getRing());
|
||||
for (String site : deck.getSites())
|
||||
validateCard(site);
|
||||
for (String card : deck.getAdventureCards())
|
||||
validateCard(card);
|
||||
}
|
||||
validateCard(deck.getRingBearer());
|
||||
validateCard(deck.getRing());
|
||||
for (String site : deck.getSites())
|
||||
validateCard(site);
|
||||
for (String card : deck.getAdventureCards())
|
||||
validateCard(card);
|
||||
|
||||
if (isOrderedSites()) {
|
||||
boolean[] sites = new boolean[9];
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.gempukku.lotro.collection;
|
||||
|
||||
import com.gempukku.lotro.game.CardCollection;
|
||||
import com.gempukku.lotro.game.DefaultCardCollection;
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
@@ -9,8 +10,6 @@ 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 {
|
||||
@@ -59,4 +58,40 @@ public class CollectionSerializerTest {
|
||||
assertEquals(1, result.size());
|
||||
assertEquals(8, (int) result.get("FotR - Booster").getCount());
|
||||
}
|
||||
|
||||
@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);
|
||||
|
||||
final Map<String, CardCollection.Item> result = resultCollection.getAll();
|
||||
assertEquals(1, result.size());
|
||||
assertEquals(500, (int) result.get("FotR - Booster").getCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLotsOfCurrency() throws IOException {
|
||||
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);
|
||||
|
||||
assertEquals(127*255, resultCollection.getCurrency());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -220,14 +220,18 @@ public class DeckResource extends AbstractResource {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("<b>Free People</b>: " + fpCount + ", <b>Shadow</b>: " + shadowCount + "<br/>");
|
||||
|
||||
StringBuilder valid = new StringBuilder();
|
||||
StringBuilder invalid = new StringBuilder();
|
||||
for (LotroFormat format : _formatLibrary.getHallFormats().values()) {
|
||||
try {
|
||||
format.validateDeck(deck);
|
||||
sb.append("<b>" + format.getName() + "</b>: <font color='green'>valid</font><br/>");
|
||||
valid.append("<b>" + format.getName() + "</b>: <font color='green'>valid</font><br/>");
|
||||
} catch (DeckInvalidException exp) {
|
||||
sb.append("<b>" + format.getName() + "</b>: <font color='red'>" + exp.getMessage() + "</font><br/>");
|
||||
invalid.append("<b>" + format.getName() + "</b>: <font color='red'>" + exp.getMessage() + "</font><br/>");
|
||||
}
|
||||
}
|
||||
sb.append(valid);
|
||||
sb.append(invalid);
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user