Let preferred art be respected again for unique cards only list

This commit is contained in:
drdev
2014-08-01 17:38:11 +00:00
parent 6534fcb575
commit d055da796d
5 changed files with 58 additions and 37 deletions

View File

@@ -79,12 +79,17 @@ public final class CardDb implements ICardDatabase {
this.isFoil = isFoil; this.isFoil = isFoil;
} }
public static CardRequest fromString(String name) { public static CardRequest fromString(String name) {
boolean isFoil = name.endsWith(foilSuffix); boolean isFoil = name.endsWith(foilSuffix);
if (isFoil) { if (isFoil) {
name = name.substring(0, name.length() - foilSuffix.length()); name = name.substring(0, name.length() - foilSuffix.length());
} }
String preferredArt = artPrefs.get(name);
if (preferredArt != null) { //account for preferred art if needed
name += NameSetSeparator + preferredArt;
}
String[] nameParts = TextUtil.split(name, NameSetSeparator); String[] nameParts = TextUtil.split(name, NameSetSeparator);
int setPos = nameParts.length >= 2 && !StringUtils.isNumeric(nameParts[1]) ? 1 : -1; int setPos = nameParts.length >= 2 && !StringUtils.isNumeric(nameParts[1]) ? 1 : -1;
@@ -189,10 +194,6 @@ public final class CardDb implements ICardDatabase {
@Override @Override
public PaperCard getCard(String cardName) { public PaperCard getCard(String cardName) {
String preferredArt = artPrefs.get(cardName);
if (preferredArt != null) { //account for preferred art if needed
cardName += NameSetSeparator + preferredArt;
}
CardRequest request = CardRequest.fromString(cardName); CardRequest request = CardRequest.fromString(cardName);
return tryGetCard(request); return tryGetCard(request);
} }
@@ -217,10 +218,10 @@ public final class CardDb implements ICardDatabase {
} }
return tryGetCard(request); return tryGetCard(request);
} }
private PaperCard tryGetCard(CardRequest request) { private PaperCard tryGetCard(CardRequest request) {
Collection<PaperCard> cards = allCardsByName.get(request.cardName); Collection<PaperCard> cards = allCardsByName.get(request.cardName);
if (null == cards) { return null; } if (cards == null) { return null; }
PaperCard result = null; PaperCard result = null;
@@ -228,18 +229,23 @@ public final class CardDb implements ICardDatabase {
if (reqEdition != null && !editions.contains(reqEdition)) { if (reqEdition != null && !editions.contains(reqEdition)) {
CardEdition edition = editions.get(reqEdition); CardEdition edition = editions.get(reqEdition);
if (edition != null) { if (edition != null) {
reqEdition = edition.getCode(); reqEdition = edition.getCode();
} }
} }
if (request.artIndex <= 0) { // this stands for 'random art' if (request.artIndex <= 0) { // this stands for 'random art'
List<PaperCard> candidates = new ArrayList<PaperCard>(9); // 9 cards with same name per set is a maximum of what has been printed (Arnchenemy) Collection<PaperCard> candidates;
for (PaperCard pc : cards) { if (reqEdition == null) {
if (pc.getEdition().equalsIgnoreCase(reqEdition) || reqEdition == null) { candidates = cards;
candidates.add(pc); }
else {
candidates = new ArrayList<PaperCard>();
for (PaperCard pc : cards) {
if (pc.getEdition().equalsIgnoreCase(reqEdition)) {
candidates.add(pc);
}
} }
} }
if (candidates.isEmpty()) { if (candidates.isEmpty()) {
return null; return null;
} }

View File

@@ -64,7 +64,7 @@ public class CardPool extends ItemPool<PaperCard> {
public void add(String cardName, String setCode, final int artIndex, final int amount) { public void add(String cardName, String setCode, final int artIndex, final int amount) {
PaperCard cp = StaticData.instance().getCommonCards().getCard(cardName, setCode, artIndex); PaperCard cp = StaticData.instance().getCommonCards().getCard(cardName, setCode, artIndex);
boolean isCommonCard = cp != null; boolean isCommonCard = cp != null;
if ( !isCommonCard ) { if (!isCommonCard) {
cp = StaticData.instance().getVariantCards().getCard(cardName, setCode); cp = StaticData.instance().getVariantCards().getCard(cardName, setCode);
} }

View File

@@ -118,7 +118,6 @@ public class Aggregates {
return result; return result;
} }
public static final <K, U> Iterable<U> uniqueByLast(final Iterable<U> source, final Function<U, K> fnUniqueKey) { // this might be exotic public static final <K, U> Iterable<U> uniqueByLast(final Iterable<U> source, final Function<U, K> fnUniqueKey) { // this might be exotic
final Map<K, U> uniques = new Hashtable<K, U>(); final Map<K, U> uniques = new Hashtable<K, U>();
for (final U c : source) { for (final U c : source) {

View File

@@ -1,5 +1,6 @@
package forge.itemmanager; package forge.itemmanager;
import java.util.HashSet;
import java.util.Map.Entry; import java.util.Map.Entry;
import forge.Graphics; import forge.Graphics;
@@ -7,6 +8,7 @@ import forge.assets.FSkinColor;
import forge.assets.FSkinFont; import forge.assets.FSkinFont;
import forge.card.CardRenderer; import forge.card.CardRenderer;
import forge.card.CardZoom; import forge.card.CardZoom;
import forge.deck.CardPool;
import forge.item.PaperCard; import forge.item.PaperCard;
import forge.itemmanager.filters.CardColorFilter; import forge.itemmanager.filters.CardColorFilter;
import forge.itemmanager.filters.CardFormatFilter; import forge.itemmanager.filters.CardFormatFilter;
@@ -34,6 +36,22 @@ public class CardManager extends ItemManager<PaperCard> {
return createSearchFilter(this); return createSearchFilter(this);
} }
@Override
protected Iterable<Entry<PaperCard, Integer>> createUniqueList(final Iterable<Entry<PaperCard, Integer>> source) {
boolean isInfinite = isInfinite();
HashSet<String> usedCardNames = isInfinite ? new HashSet<String>() : null;
String cardName;
final CardPool uniques = new CardPool();
for (Entry<PaperCard, Integer> item : source) {
cardName = item.getKey().getName();
if (isInfinite && !usedCardNames.add(cardName)) {
continue; //don't add card more than once if infinite
}
uniques.add(cardName, item.getValue());
}
return uniques;
}
/* Static overrides shared with SpellShopManager*/ /* Static overrides shared with SpellShopManager*/
public static void addDefaultFilters(final ItemManager<? super PaperCard> itemManager) { public static void addDefaultFilters(final ItemManager<? super PaperCard> itemManager) {

View File

@@ -847,6 +847,10 @@ public abstract class ItemManager<T extends InventoryItem> extends FContainer im
updateView(true, getSelectedItems()); updateView(true, getSelectedItems());
} }
protected Iterable<Entry<T, Integer>> createUniqueList(final Iterable<Entry<T, Integer>> source) {
return Aggregates.uniqueByLast(source, pool.FN_GET_NAME);
}
/** /**
* *
* updateView. * updateView.
@@ -858,43 +862,37 @@ public abstract class ItemManager<T extends InventoryItem> extends FContainer im
if (useFilter || wantUnique || forceFilter) { if (useFilter || wantUnique || forceFilter) {
model.clear(); model.clear();
}
if (useFilter && wantUnique) { Iterable<Entry<T, Integer>> items = pool;
Predicate<Entry<T, Integer>> filterForPool = Predicates.compose(filterPredicate, pool.FN_GET_KEY); if (useFilter) {
Iterable<Entry<T, Integer>> items = Aggregates.uniqueByLast(Iterables.filter(pool, filterForPool), pool.FN_GET_NAME); Predicate<Entry<T, Integer>> pred = Predicates.compose(filterPredicate, pool.FN_GET_KEY);
items = Iterables.filter(pool, pred);
}
if (wantUnique) {
items = createUniqueList(items);
}
model.addItems(items); model.addItems(items);
} }
else if (useFilter) {
Predicate<Entry<T, Integer>> pred = Predicates.compose(filterPredicate, pool.FN_GET_KEY);
model.addItems(Iterables.filter(pool, pred));
}
else if (wantUnique) {
Iterable<Entry<T, Integer>> items = Aggregates.uniqueByLast(pool, pool.FN_GET_NAME);
model.addItems(items);
}
else if (!useFilter && forceFilter) {
model.addItems(pool);
}
currentView.refresh(itemsToSelect, getSelectedIndex(), forceFilter ? 0 : currentView.getScrollValue()); currentView.refresh(itemsToSelect, getSelectedIndex(), forceFilter ? 0 : currentView.getScrollValue());
//update ratio of # in filtered pool / # in total pool //update ratio of # in filtered pool / # in total pool
int total; int totalCount;
int filteredCount = getFilteredItems().countAll();
if (!useFilter) { if (!useFilter) {
total = getFilteredItems().countAll(); totalCount = filteredCount;
} }
else if (wantUnique) { else if (wantUnique) {
total = 0; totalCount = 0;
Iterable<Entry<T, Integer>> items = Aggregates.uniqueByLast(pool, pool.FN_GET_NAME); Iterable<Entry<T, Integer>> items = createUniqueList(pool);
for (Entry<T, Integer> entry : items) { for (Entry<T, Integer> entry : items) {
total += entry.getValue(); totalCount += entry.getValue();
} }
} }
else { else {
total = pool.countAll(); totalCount = pool.countAll();
} }
searchFilter.setRatio("(" + getFilteredItems().countAll() + " / " + total + ")"); searchFilter.setRatio("(" + filteredCount + " / " + totalCount + ")");
} }
/** /**