Read foiled cards from deck (they mush have a + sign appended right to name, ex: Island+|ZEN)

This commit is contained in:
Maxmtg
2013-06-26 07:57:09 +00:00
parent 6e2803a614
commit 2c98474e16
7 changed files with 58 additions and 40 deletions

View File

@@ -47,7 +47,7 @@ import forge.util.maps.TreeMapOfLists;
public final class CardDb implements ICardDatabase {
private static volatile CardDb commonCards = null; // 'volatile' keyword makes this working
private static volatile CardDb variantCards = null; // 'volatile' keyword makes this working
public final static String foilSuffix = " foil";
public final static String foilSuffix = "+";
private final static int foilSuffixLength = foilSuffix.length();
public static ICardDatabase instance() {
@@ -171,7 +171,7 @@ public final class CardDb implements ICardDatabase {
}
private boolean isFoil(final String cardName) {
return cardName.toLowerCase().endsWith(CardDb.foilSuffix) && (cardName.length() > CardDb.foilSuffixLength);
return cardName.toLowerCase().endsWith(CardDb.foilSuffix);
}
/**
@@ -181,7 +181,7 @@ public final class CardDb implements ICardDatabase {
* @return the string
*/
public String removeFoilSuffix(final String cardName) {
return cardName.substring(0, cardName.length() - CardDb.foilSuffixLength);
return cardName.toLowerCase().endsWith(CardDb.foilSuffix) ? cardName.substring(0, cardName.length() - CardDb.foilSuffixLength) : cardName;
}
@@ -202,12 +202,13 @@ public final class CardDb implements ICardDatabase {
final boolean isFoil = this.isFoil(cardName0);
final String cardName = isFoil ? this.removeFoilSuffix(cardName0) : cardName0;
final ImmutablePair<String, String> nameWithSet = CardDb.splitCardName(cardName);
final PaperCard res = nameWithSet.right == null
? ( fromLastSet ? this.uniqueCardsByName.get(nameWithSet.left) : Aggregates.random(this.allCardsByName.get(nameWithSet.left)) )
: tryGetCard(nameWithSet.left, nameWithSet.right);
return null != res && isFoil ? PaperCard.makeFoiled(res) : res;
return null != res && isFoil ? getFoiled(res) : res;
}
@Override
@@ -215,6 +216,41 @@ public final class CardDb implements ICardDatabase {
return tryGetCard(cardName, setName, -1);
}
@Override
public PaperCard tryGetCard(final String cardName0, String setName, int index) {
final boolean isFoil = this.isFoil(cardName0);
final String cardName = isFoil ? this.removeFoilSuffix(cardName0) : cardName0;
Collection<PaperCard> cards = allCardsByName.get(cardName);
if ( null == cards ) return null;
PaperCard result = null;
if ( index < 0 ) { // this stands for 'random art'
PaperCard[] candidates = new PaperCard[9]; // 9 cards with same name per set is a maximum of what has been printed (Arnchenemy)
int cnt = 0;
for( PaperCard pc : cards ) {
if( pc.getEdition().equals(setName) )
candidates[cnt++] = pc;
}
if (cnt == 0 ) return null;
result = cnt == 1 ? candidates[0] : candidates[MyRandom.getRandom().nextInt(cnt)];
} else
for( PaperCard pc : cards ) {
if( pc.getEdition().equals(setName) && index == pc.getArtIndex() ) {
result = pc;
break;
}
}
if ( result == null ) return null;
return isFoil ? getFoiled(result) : result;
}
public PaperCard getFoiled(PaperCard card0) {
// Here - I am still unsure if there should be a cache Card->Card from unfoiled to foiled, to avoid creation of N instances of single plains
return new PaperCard(card0.getRules(), card0.getEdition(), card0.getRarity(), card0.getArtIndex(), true);
}
@Override
public int getPrintCount(String cardName, String edition) {
int cnt = 0;
@@ -235,30 +271,6 @@ public final class CardDb implements ICardDatabase {
return max + 1;
}
@Override
public PaperCard tryGetCard(final String cardName, String setName, int index) {
Collection<PaperCard> cards = allCardsByName.get(cardName);
if ( null == cards ) return null;
if ( index < 0 ) { // this stands for 'random art'
PaperCard[] candidates = new PaperCard[9]; // 9 cards with same name per set is a maximum of what has been printed (Arnchenemy)
int cnt = 0;
for( PaperCard pc : cards ) {
if( pc.getEdition().equals(setName) )
candidates[cnt++] = pc;
}
if (cnt == 0 ) return null;
if (cnt == 1 ) return candidates[0];
return candidates[MyRandom.getRandom().nextInt(cnt)];
} else
for( PaperCard pc : cards ) {
if( pc.getEdition().equals(setName) && index == pc.getArtIndex() )
return pc;
}
return null;
}
// Single fetch
@Override
public PaperCard getCard(final String name) {

View File

@@ -11,17 +11,27 @@ public interface ICardDatabase {
PaperCard tryGetCard(String cardName);
PaperCard tryGetCard(String cardName, boolean fromLastSet);
PaperCard tryGetCard(String cardName, String edition);
int getPrintCount(String cardName, String edition);
int getMaxPrintCount(String cardName);
PaperCard tryGetCard(String cardName, String edition, int artIndex);
PaperCard getCard(String cardName);
PaperCard getCard(String cardName, boolean fromLastSet);
PaperCard getCard(String cardName, String edition);
PaperCard getCard(String cardName, String edition, int artIndex);
PaperCard getFoiled(PaperCard cpi);
int getPrintCount(String cardName, String edition);
int getMaxPrintCount(String cardName);
Collection<PaperCard> getUniqueCards();
List<PaperCard> getAllCards();
List<PaperCard> getAllCards(Predicate<PaperCard> predicate);
Predicate<? super PaperCard> wasPrintedInSets(List<String> allowedSetCodes);
/**
* TODO: Write javadoc for this method.
* @param cpi
* @return
*/
}

View File

@@ -124,7 +124,7 @@ public class GameNew {
if (preferences.getPrefBoolean(FPref.UI_RANDOM_CARD_ART)) {
cpi = CardDb.instance().getCard(cp.getName(), cp.getEdition(), -1);
if ( cp.isFoil() )
cpi = PaperCard.makeFoiled(cpi);
cpi = CardDb.instance().getFoiled(cpi);
}
final Card card = cpi.toForgeCard(player);

View File

@@ -184,7 +184,7 @@ public class GauntletIO {
final short index = StringUtils.isNumeric(sIndex) ? Short.parseShort(sIndex) : 0;
final boolean foil = "1".equals(reader.getAttribute("foil"));
final PaperCard card = CardDb.instance().getCard(name, set, index);
return foil ? PaperCard.makeFoiled(card) : card;
return foil ? CardDb.instance().getFoiled(card) : card;
}
}
}

View File

@@ -129,10 +129,6 @@ public final class PaperCard implements Comparable<IPaperCard>, InventoryItemFro
this.rarity = rare;
}
public static PaperCard makeFoiled(final PaperCard c) {
return new PaperCard(c.card, c.edition, c.rarity, c.artIndex, true);
}
// Want this class to be a key for HashTable
@Override
public boolean equals(final Object obj) {

View File

@@ -684,7 +684,7 @@ public class QuestDataIO {
final boolean foil = "1".equals(reader.getAttribute("foil"));
PaperCard c = CardDb.instance().tryGetCard(name, set, index);
if ( null == c ) c = CardDb.instance().getCard(name);
return foil ? PaperCard.makeFoiled(c) : c;
return foil ? CardDb.instance().getFoiled(c) : c;
}
}
}

View File

@@ -429,7 +429,7 @@ public class CardPanel extends JPanel implements CardContainer {
(this.cardYOffset + (this.cardHeight / 2)) - 20);
}
if (this.getGameCard().getFoil() > 0) {
if (card.getFoil() > 0) {
final String fl = String.format("foil%02d", card.getFoil());
final int z = Math.round(this.cardWidth * CardPanel.BLACK_BORDER_SIZE);
CardFaceSymbols.drawOther(g, fl, this.cardXOffset + z, this.cardYOffset + z, this.cardWidth - (2 * z),