Filtering based on format (sets and banned list).

This commit is contained in:
marcins78
2012-11-07 18:40:17 +00:00
parent af236233a1
commit 613bc10b33
10 changed files with 96 additions and 62 deletions

View File

@@ -4,9 +4,9 @@ import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class LotroCardBlueprintLibrary {
private String[] _packageNames =
@@ -17,7 +17,7 @@ public class LotroCardBlueprintLibrary {
private Map<String, LotroCardBlueprint> _blueprintMap = new HashMap<String, LotroCardBlueprint>();
private Map<String, String> _blueprintMapping = new HashMap<String, String>();
private Map<String, List<String>> _fullBlueprintMapping = new HashMap<String, List<String>>();
private Map<String, Set<String>> _fullBlueprintMapping = new HashMap<String, Set<String>>();
public LotroCardBlueprintLibrary() {
try {
@@ -49,7 +49,7 @@ public class LotroCardBlueprintLibrary {
}
private void addAlternatives(String newBlueprint, String existingBlueprint) {
List<String> existingAlternates = _fullBlueprintMapping.get(existingBlueprint);
Set<String> existingAlternates = _fullBlueprintMapping.get(existingBlueprint);
if (existingAlternates != null) {
for (String existingAlternate : existingAlternates) {
addAlternative(newBlueprint, existingAlternate);
@@ -61,16 +61,20 @@ public class LotroCardBlueprintLibrary {
}
private void addAlternative(String from, String to) {
List<String> list = _fullBlueprintMapping.get(from);
Set<String> list = _fullBlueprintMapping.get(from);
if (list == null) {
list = new LinkedList<String>();
list = new HashSet<String>();
_fullBlueprintMapping.put(from, list);
}
list.add(to);
}
public Set<String> getAllAlternates(String blueprintId) {
return _fullBlueprintMapping.get(blueprintId);
}
public boolean hasAlternateInSet(String blueprintId, int setNo) {
List<String> alternatives = _fullBlueprintMapping.get(blueprintId);
Set<String> alternatives = _fullBlueprintMapping.get(blueprintId);
if (alternatives != null)
for (String alternative : alternatives)
if (alternative.startsWith(setNo + "_"))

View File

@@ -14,6 +14,8 @@ public interface LotroFormat {
public String getName();
public void validateCard(String cardId) throws DeckInvalidException;
public void validateDeck(LotroDeck deck) throws DeckInvalidException;
public List<Integer> getValidSets();

View File

@@ -5,13 +5,14 @@ import com.gempukku.lotro.common.CardType;
import com.gempukku.lotro.common.Culture;
import com.gempukku.lotro.common.Keyword;
import com.gempukku.lotro.common.Side;
import com.gempukku.lotro.game.formats.LotroFormatLibrary;
import com.gempukku.lotro.logic.GameUtils;
import com.gempukku.util.MultipleComparator;
import java.util.*;
public class SortAndFilterCards {
public <T extends CardItem> List<T> process(String filter, Collection<T> items, LotroCardBlueprintLibrary library, Map<String, SetRarity> rarities) {
public <T extends CardItem> List<T> process(String filter, Collection<T> items, LotroCardBlueprintLibrary cardLibrary, LotroFormatLibrary formatLibrary, Map<String, SetRarity> rarities) {
if (filter == null)
filter = "";
String[] filterParams = filter.split(" ");
@@ -31,7 +32,7 @@ public class SortAndFilterCards {
for (T item : items) {
String blueprintId = item.getBlueprintId();
if (acceptsFilters(library, rarities, blueprintId, side, type, rarity, sets, cardTypes, cultures, keywords, words, siteNumber))
if (acceptsFilters(cardLibrary, formatLibrary, rarities, blueprintId, side, type, rarity, sets, cardTypes, cultures, keywords, words, siteNumber))
result.add(item);
}
@@ -44,21 +45,21 @@ public class SortAndFilterCards {
MultipleComparator<CardItem> comparators = new MultipleComparator<CardItem>();
for (String oneSort : sortSplit) {
if (oneSort.equals("twilight"))
comparators.addComparator(new PacksFirstComparator(new TwilightComparator(library)));
comparators.addComparator(new PacksFirstComparator(new TwilightComparator(cardLibrary)));
else if (oneSort.equals("siteNumber"))
comparators.addComparator(new PacksFirstComparator(new SiteNumberComparator(library)));
comparators.addComparator(new PacksFirstComparator(new SiteNumberComparator(cardLibrary)));
else if (oneSort.equals("strength"))
comparators.addComparator(new PacksFirstComparator(new StrengthComparator(library)));
comparators.addComparator(new PacksFirstComparator(new StrengthComparator(cardLibrary)));
else if (oneSort.equals("vitality"))
comparators.addComparator(new PacksFirstComparator(new VitalityComparator(library)));
comparators.addComparator(new PacksFirstComparator(new VitalityComparator(cardLibrary)));
else if (oneSort.equals("collectorInfo"))
comparators.addComparator(new PacksFirstComparator(new CardBlueprintIdComparator()));
else if (oneSort.equals("cardType"))
comparators.addComparator(new PacksFirstComparator(new CardTypeComparator(library)));
comparators.addComparator(new PacksFirstComparator(new CardTypeComparator(cardLibrary)));
else if (oneSort.equals("culture"))
comparators.addComparator(new PacksFirstComparator(new CultureComparator(library)));
comparators.addComparator(new PacksFirstComparator(new CultureComparator(cardLibrary)));
else if (oneSort.equals("name"))
comparators.addComparator(new PacksFirstComparator(new NameComparator(library)));
comparators.addComparator(new PacksFirstComparator(new NameComparator(cardLibrary)));
}
Collections.sort(result, comparators);
@@ -67,7 +68,7 @@ public class SortAndFilterCards {
}
private boolean acceptsFilters(
LotroCardBlueprintLibrary library, Map<String, SetRarity> rarities, String blueprintId, Side side, String type, String rarity, String[] sets,
LotroCardBlueprintLibrary library, LotroFormatLibrary formatLibrary, Map<String, SetRarity> rarities, String blueprintId, Side side, String type, String rarity, String[] sets,
Set<CardType> cardTypes, Set<Culture> cultures, Set<Keyword> keywords, List<String> words, Integer siteNumber) {
if (isPack(blueprintId)) {
if (type == null || type.equals("pack"))
@@ -81,7 +82,7 @@ public class SortAndFilterCards {
final LotroCardBlueprint blueprint = library.getLotroCardBlueprint(blueprintId);
if (side == null || blueprint.getSide() == side)
if (rarity == null || isRarity(blueprintId, rarity, library, rarities))
if (sets == null || isInSets(blueprintId, sets, library))
if (sets == null || isInSets(blueprintId, sets, library, formatLibrary))
if (cardTypes == null || cardTypes.contains(blueprint.getCardType()))
if (cultures == null || cultures.contains(blueprint.getCulture()))
if (containsAllKeywords(blueprint, keywords))
@@ -135,10 +136,21 @@ public class SortAndFilterCards {
return true;
}
private boolean isInSets(String blueprintId, String[] sets, LotroCardBlueprintLibrary library) {
for (String set : sets)
if (blueprintId.startsWith(set + "_") || library.hasAlternateInSet(blueprintId, Integer.parseInt(set)))
return true;
private boolean isInSets(String blueprintId, String[] sets, LotroCardBlueprintLibrary library, LotroFormatLibrary formatLibrary) {
for (String set : sets) {
LotroFormat format = formatLibrary.getFormat(set);
if (format != null) {
try {
format.validateCard(blueprintId);
return true;
} catch (DeckInvalidException exp) {
return false;
}
} else {
if (blueprintId.startsWith(set + "_") || library.hasAlternateInSet(blueprintId, Integer.parseInt(set)))
return true;
}
}
return false;
}

View File

@@ -123,16 +123,24 @@ public class DefaultLotroFormat implements LotroFormat {
_validSets.add(setNo);
}
private void validateSet(String blueprintId) throws DeckInvalidException {
@Override
public void validateCard(String blueprintId) throws DeckInvalidException {
blueprintId = _library.getBaseBlueprintId(blueprintId);
if (_validCards.contains(blueprintId))
return;
for (int validSet : _validSets)
if (blueprintId.startsWith(validSet + "_")
|| _library.hasAlternateInSet(blueprintId, validSet))
return;
throw new DeckInvalidException("Deck contains card not from valid set: " + GameUtils.getFullName(_library.getLotroCardBlueprint(blueprintId)));
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)));
// Banned cards
Set<String> allAlternates = _library.getAllAlternates(blueprintId);
for (String bannedBlueprintId : _bannedCards) {
if (bannedBlueprintId.equals(blueprintId) || allAlternates.contains(bannedBlueprintId))
throw new DeckInvalidException("Deck contains a copy of an X-listed card: " + GameUtils.getFullName(_library.getLotroCardBlueprint(bannedBlueprintId)));
}
}
@Override
@@ -179,12 +187,12 @@ public class DefaultLotroFormat implements LotroFormat {
}
if (_validSets.size() > 0) {
validateSet(deck.getRingBearer());
validateSet(deck.getRing());
validateCard(deck.getRingBearer());
validateCard(deck.getRing());
for (String site : deck.getSites())
validateSet(site);
validateCard(site);
for (String card : deck.getAdventureCards())
validateSet(card);
validateCard(card);
}
if (isOrderedSites()) {
@@ -260,13 +268,6 @@ public class DefaultLotroFormat implements LotroFormat {
throw new DeckInvalidException("Deck contains more than one copy of an R-listed card: " + GameUtils.getFullName(_library.getLotroCardBlueprint(blueprintId)));
}
// Banned cards
for (String blueprintId : _bannedCards) {
Integer count = cardCountByBaseBlueprintId.get(blueprintId);
if (count != null && count > 0)
throw new DeckInvalidException("Deck contains a copy of an X-listed card: " + GameUtils.getFullName(_library.getLotroCardBlueprint(blueprintId)));
}
} catch (IllegalArgumentException exp) {
throw new DeckInvalidException("Deck contains unrecognizable card");
}

View File

@@ -1,5 +1,8 @@
package com.gempukku.lotro.game.formats;
import com.gempukku.lotro.game.DeckInvalidException;
import com.gempukku.lotro.game.LotroCardBlueprintLibrary;
import com.gempukku.lotro.game.LotroFormat;
import org.junit.Test;
public class LotroFormatLibraryTest {
@@ -7,4 +10,11 @@ public class LotroFormatLibraryTest {
public void testLoad() {
LotroFormatLibrary library = new LotroFormatLibrary(null);
}
@Test(expected = DeckInvalidException.class)
public void legolasGreenleafNotLegalInStandard() throws DeckInvalidException {
LotroFormatLibrary library = new LotroFormatLibrary(new LotroCardBlueprintLibrary());
LotroFormat standard = library.getFormat("standard");
standard.validateCard("1_50");
}
}

View File

@@ -7,6 +7,7 @@ import com.gempukku.lotro.common.Side;
import com.gempukku.lotro.db.vo.CollectionType;
import com.gempukku.lotro.db.vo.League;
import com.gempukku.lotro.game.*;
import com.gempukku.lotro.game.formats.LotroFormatLibrary;
import com.gempukku.lotro.league.LeagueSerieData;
import com.gempukku.lotro.league.LeagueService;
import com.gempukku.lotro.packs.PacksStorage;
@@ -41,6 +42,8 @@ public class CollectionResource extends AbstractResource {
@Context
private LotroCardBlueprintLibrary _library;
@Context
private LotroFormatLibrary _formatLibrary;
@Context
private LeagueService _leagueService;
@Context
private PacksStorage _packStorage;
@@ -92,7 +95,7 @@ public class CollectionResource extends AbstractResource {
sendError(Response.Status.NOT_FOUND);
Collection<CardCollection.Item> items = collection.getAll().values();
List<CardCollection.Item> filteredResult = _sortAndFilterCards.process(filter, items, _library, _rarities);
List<CardCollection.Item> filteredResult = _sortAndFilterCards.process(filter, items, _library, _formatLibrary, _rarities);
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();

View File

@@ -121,17 +121,17 @@ public class DeckResource extends AbstractResource {
result.append("<br/>");
result.append("<b>Adventure deck:</b><br/>");
for (CardCollection.Item item : _sortAndFilterCards.process("cardType:SITE sort:siteNumber,twilight", deckCards.getAll().values(), _library, null))
for (CardCollection.Item item : _sortAndFilterCards.process("cardType:SITE sort:siteNumber,twilight", deckCards.getAll().values(), _library, _formatLibrary, null))
result.append(GameUtils.getFullName(_library.getLotroCardBlueprint(item.getBlueprintId())) + "<br/>");
result.append("<br/>");
result.append("<b>Free Peoples Draw Deck:</b><br/>");
for (CardCollection.Item item : _sortAndFilterCards.process("side:FREE_PEOPLE sort:cardType,culture,name", deckCards.getAll().values(), _library, null))
for (CardCollection.Item item : _sortAndFilterCards.process("side:FREE_PEOPLE sort:cardType,culture,name", deckCards.getAll().values(), _library, _formatLibrary, null))
result.append(item.getCount() + "x " + GameUtils.getFullName(_library.getLotroCardBlueprint(item.getBlueprintId())) + "<br/>");
result.append("<br/>");
result.append("<b>Shadow Draw Deck:</b><br/>");
for (CardCollection.Item item : _sortAndFilterCards.process("side:SHADOW sort:cardType,culture,name", deckCards.getAll().values(), _library, null))
for (CardCollection.Item item : _sortAndFilterCards.process("side:SHADOW sort:cardType,culture,name", deckCards.getAll().values(), _library, _formatLibrary, null))
result.append(item.getCount() + "x " + GameUtils.getFullName(_library.getLotroCardBlueprint(item.getBlueprintId())) + "<br/>");
result.append("</body></html>");
@@ -252,13 +252,13 @@ public class DeckResource extends AbstractResource {
deckElem.appendChild(ring);
}
for (CardItem cardItem : _sortAndFilterCards.process("sort:siteNumber,twilight", createCardItems(deck.getSites()), _library, null)) {
for (CardItem cardItem : _sortAndFilterCards.process("sort:siteNumber,twilight", createCardItems(deck.getSites()), _library, _formatLibrary, null)) {
Element site = doc.createElement("site");
site.setAttribute("blueprintId", cardItem.getBlueprintId());
deckElem.appendChild(site);
}
for (CardItem cardItem : _sortAndFilterCards.process("sort:cardType,culture,name", createCardItems(deck.getAdventureCards()), _library, null)) {
for (CardItem cardItem : _sortAndFilterCards.process("sort:cardType,culture,name", createCardItems(deck.getAdventureCards()), _library, _formatLibrary, null)) {
Element card = doc.createElement("card");
card.setAttribute("side", _library.getLotroCardBlueprint(cardItem.getBlueprintId()).getSide().toString());
card.setAttribute("blueprintId", cardItem.getBlueprintId());

View File

@@ -4,6 +4,7 @@ import com.gempukku.lotro.cards.packs.RarityReader;
import com.gempukku.lotro.cards.packs.SetRarity;
import com.gempukku.lotro.collection.CollectionsManager;
import com.gempukku.lotro.game.*;
import com.gempukku.lotro.game.formats.LotroFormatLibrary;
import com.gempukku.lotro.merchant.MerchantException;
import com.gempukku.lotro.merchant.MerchantService;
import com.sun.jersey.spi.resource.Singleton;
@@ -29,6 +30,8 @@ public class MerchantResource extends AbstractResource {
private MerchantService _merchantService;
@Context
private LotroCardBlueprintLibrary _library;
@Context
private LotroFormatLibrary _formatLibrary;
private Map<String, SetRarity> _rarities;
private SortAndFilterCards _sortAndFilterCards = new SortAndFilterCards();
@@ -86,7 +89,7 @@ public class MerchantResource extends AbstractResource {
cardItems.add(item);
}
}
List<CardItem> filteredResult = _sortAndFilterCards.process(filter, cardItems, _library, _rarities);
List<CardItem> filteredResult = _sortAndFilterCards.process(filter, cardItems, _library, _formatLibrary, _rarities);
List<CardItem> pageToDisplay = new ArrayList<CardItem>();
for (int i = start; i < start + count; i++) {

View File

@@ -33,7 +33,7 @@ public class TournamentResource extends AbstractResource {
@Context
private TournamentService _tournamentService;
@Context
private LotroFormatLibrary _lotroFormatLibrary;
private LotroFormatLibrary _formatLibrary;
@Context
private LotroCardBlueprintLibrary _library;
@@ -55,7 +55,7 @@ public class TournamentResource extends AbstractResource {
tournamentElem.setAttribute("id", tournament.getTournamentId());
tournamentElem.setAttribute("name", tournament.getTournamentName());
tournamentElem.setAttribute("format", _lotroFormatLibrary.getFormat(tournament.getFormat()).getName());
tournamentElem.setAttribute("format", _formatLibrary.getFormat(tournament.getFormat()).getName());
tournamentElem.setAttribute("collection", tournament.getCollectionType().getFullName());
tournamentElem.setAttribute("round", String.valueOf(tournament.getCurrentRound()));
tournamentElem.setAttribute("stage", tournament.getTournamentStage().getHumanReadable());
@@ -85,7 +85,7 @@ public class TournamentResource extends AbstractResource {
tournamentElem.setAttribute("id", tournament.getTournamentId());
tournamentElem.setAttribute("name", tournament.getTournamentName());
tournamentElem.setAttribute("format", _lotroFormatLibrary.getFormat(tournament.getFormat()).getName());
tournamentElem.setAttribute("format", _formatLibrary.getFormat(tournament.getFormat()).getName());
tournamentElem.setAttribute("collection", tournament.getCollectionType().getFullName());
tournamentElem.setAttribute("round", String.valueOf(tournament.getCurrentRound()));
tournamentElem.setAttribute("stage", tournament.getTournamentStage().getHumanReadable());
@@ -119,7 +119,7 @@ public class TournamentResource extends AbstractResource {
tournamentElem.setAttribute("id", tournament.getTournamentId());
tournamentElem.setAttribute("name", tournament.getTournamentName());
tournamentElem.setAttribute("format", _lotroFormatLibrary.getFormat(tournament.getFormat()).getName());
tournamentElem.setAttribute("format", _formatLibrary.getFormat(tournament.getFormat()).getName());
tournamentElem.setAttribute("collection", tournament.getCollectionType().getFullName());
tournamentElem.setAttribute("round", String.valueOf(tournament.getCurrentRound()));
tournamentElem.setAttribute("stage", tournament.getTournamentStage().getHumanReadable());
@@ -176,17 +176,17 @@ public class TournamentResource extends AbstractResource {
result.append("<br/>");
result.append("<b>Adventure deck:</b><br/>");
for (CardCollection.Item item : _sortAndFilterCards.process("cardType:SITE sort:siteNumber,twilight", deckCards.getAll().values(), _library, null))
for (CardCollection.Item item : _sortAndFilterCards.process("cardType:SITE sort:siteNumber,twilight", deckCards.getAll().values(), _library, _formatLibrary, null))
result.append(GameUtils.getFullName(_library.getLotroCardBlueprint(item.getBlueprintId())) + "<br/>");
result.append("<br/>");
result.append("<b>Free Peoples Draw Deck:</b><br/>");
for (CardCollection.Item item : _sortAndFilterCards.process("side:FREE_PEOPLE sort:cardType,culture,name", deckCards.getAll().values(), _library, null))
for (CardCollection.Item item : _sortAndFilterCards.process("side:FREE_PEOPLE sort:cardType,culture,name", deckCards.getAll().values(), _library, _formatLibrary, null))
result.append(item.getCount() + "x " + GameUtils.getFullName(_library.getLotroCardBlueprint(item.getBlueprintId())) + "<br/>");
result.append("<br/>");
result.append("<b>Shadow Draw Deck:</b><br/>");
for (CardCollection.Item item : _sortAndFilterCards.process("side:SHADOW sort:cardType,culture,name", deckCards.getAll().values(), _library, null))
for (CardCollection.Item item : _sortAndFilterCards.process("side:SHADOW sort:cardType,culture,name", deckCards.getAll().values(), _library, _formatLibrary, null))
result.append(item.getCount() + "x " + GameUtils.getFullName(_library.getLotroCardBlueprint(item.getBlueprintId())) + "<br/>");
result.append("</body></html>");

View File

@@ -105,15 +105,14 @@ var CardFilter = Class.extend({
this.fullFilterDiv = $("<div></div>");
this.setSelect = $("<select style='width: 130px; font-size: 80%;'>"
+ "<option value=''>All Sets</option>"
+ "<option value='1,2,3'>Fellowship Block</option>"
+ "<option value='4,5,6'>Towers Block</option>"
+ "<option value='7,8,10'>King Block</option>"
+ "<option value='11,12,13'>War of the Ring Block</option>"
+ "<option value='15,17,18'>Hunters Block</option>"
+ "<option value='1,2,3,4,5,6'>Towers Standard</option>"
+ "<option value='1,2,3,4,5,6,7,8,9,10'>Movie Block</option>"
+ "<option value='4,5,6,7,8,9,10,11,12,13,14'>War of the Ring standard</option>"
+ "<option value='0,7,8,9,10,11,12,13,14,15,16,17,18,19'>Standard</option>"
+ "<option value='fotr_block'>Fellowship Block</option>"
+ "<option value='ttt_block'>Towers Block</option>"
+ "<option value='king_block'>King Block</option>"
+ "<option value='war_block'>War of the Ring Block</option>"
+ "<option value='towers_standard'>Towers Standard</option>"
+ "<option value='movie'>Movie Block</option>"
+ "<option value='war_standard'>War of the Ring standard</option>"
+ "<option value='standard'>Standard</option>"
+ "<option value='0'>00 - Promo</option>"
+ "<option value='1'>01 - The Fellowship of the Ring</option>"
+ "<option value='2'>02 - Mines of Moria</option>"