foil keyword in decklists - partial support

This commit is contained in:
Maxmtg
2012-01-28 20:58:28 +00:00
parent 0dfb333230
commit b4e42984cd
3 changed files with 78 additions and 60 deletions

View File

@@ -186,7 +186,7 @@ public class DeckRecognizer {
*/
else {
if (CardDb.instance().isCardSupported(line)) {
return Token.knownCard(CardDb.instance().getCardFromLatestSet(line), 1);
return Token.knownCard(CardDb.instance().getCard(line, true), 1);
}
result = DeckRecognizer.recognizeNonCard(line, 1);
}
@@ -195,7 +195,7 @@ public class DeckRecognizer {
private static Token recognizePossibleNameAndNumber(final String name, final int n) {
if (CardDb.instance().isCardSupported(name)) {
return Token.knownCard(CardDb.instance().getCardFromLatestSet(name), n);
return Token.knownCard(CardDb.instance().getCard(name, true), n);
}
final Token known = DeckRecognizer.recognizeNonCard(name, n);

View File

@@ -276,8 +276,8 @@ public class DeckImport extends JDialog {
private String makeHtmlViewOfToken(final DeckRecognizer.Token token) {
switch (token.getType()) {
case KnownCard:
return String.format("<div class='knowncard'>%s * %s [%s]</div>", token.getNumber(), token.getCard()
.getName(), token.getCard().getSet());
return String.format("<div class='knowncard'>%s * %s [%s] %s</div>", token.getNumber(), token.getCard()
.getName(), token.getCard().getSet(), token.getCard().isFoil() ? "<i>foil</i>" : "");
case UnknownCard:
return String.format("<div class='unknowncard'>%s * %s</div>", token.getNumber(), token.getText());
case SectionName:

View File

@@ -48,6 +48,7 @@ import forge.card.MtgDataParser;
public final class CardDb {
private static volatile CardDb onlyInstance = null; // 'volatile' keyword
// makes this working
private final String FOIL_SUFFIX = " foil";
/**
* Instance.
@@ -171,28 +172,6 @@ public final class CardDb {
return lastAdded;
}
/**
* Checks if is card supported.
*
* @param cardName
* the card name
* @return true, if is card supported
*/
public boolean isCardSupported(final String cardName) {
final ImmutablePair<String, String> nameWithSet = CardDb.splitCardName(cardName);
if (nameWithSet.right == null) {
return this.uniqueCards.containsKey(nameWithSet.left.toLowerCase());
}
// Set exists?
final Map<String, CardPrinted[]> cardsFromset = this.allCardsBySet.get(nameWithSet.right.toUpperCase());
if (cardsFromset == null) {
return false;
}
// Card exists?
final CardPrinted[] cardCopies = cardsFromset.get(nameWithSet.left.toLowerCase());
return (cardCopies != null) && (cardCopies.length > 0);
}
/**
* Splits cardname into Name and set whenever deck line reads as name|set.
*/
@@ -211,6 +190,40 @@ public final class CardDb {
return new ImmutablePair<String, String>(cardName, null);
}
private boolean isFoil(String cardName)
{
return cardName.toLowerCase().endsWith(FOIL_SUFFIX) && cardName.length() > 5;
}
public String removeFoilSuffix(String cardName)
{
return cardName.substring(0, cardName.length() - 5);
}
/**
* Checks if is card supported.
*
* @param cardName
* the card name
* @return true, if is card supported
*/
public boolean isCardSupported(final String cardName0) {
boolean isFoil = isFoil(cardName0);
String cardName = isFoil ? removeFoilSuffix(cardName0) : cardName0;
final ImmutablePair<String, String> nameWithSet = CardDb.splitCardName(cardName);
if (nameWithSet.right == null) {
return this.uniqueCards.containsKey(nameWithSet.left.toLowerCase());
}
// Set exists?
final Map<String, CardPrinted[]> cardsFromset = this.allCardsBySet.get(nameWithSet.right.toUpperCase());
if (cardsFromset == null) {
return false;
}
// Card exists?
final CardPrinted[] cardCopies = cardsFromset.get(nameWithSet.left.toLowerCase());
return (cardCopies != null) && (cardCopies.length > 0);
}
// Single fetch
/**
* Gets the card.
@@ -220,18 +233,7 @@ public final class CardDb {
* @return the card
*/
public CardPrinted getCard(final String name) {
// Sometimes they read from decks things like "CardName|Set" - but we
// can handle it
final ImmutablePair<String, String> nameWithSet = CardDb.splitCardName(name);
if (nameWithSet.right != null) {
return this.getCard(nameWithSet.left, nameWithSet.right);
}
// OK, plain name here
final CardPrinted card = this.uniqueCards.get(nameWithSet.left.toLowerCase());
if (card != null) {
return card;
}
throw new NoSuchElementException(String.format("Card '%s' not found in our database.", name));
return getCard(name, false);
}
// Advanced fetch by name+set
@@ -317,6 +319,14 @@ public final class CardDb {
return result;
}
public List<CardPrinted> getCardsFromLatestSets(final Iterable<String> names) {
final List<CardPrinted> result = new ArrayList<CardPrinted>();
for (final String name : names) {
result.add(this.getCard(name, true));
}
return result;
}
// returns a list of all cards from their respective latest editions
/**
* Gets the all unique cards.
@@ -338,37 +348,45 @@ public final class CardDb {
return this.allCardsFlat;
}
/**
* TODO: Write javadoc for this method.
* @param name
* @return
*/
public CardPrinted getCardFromLatestSet(String name) {
public CardPrinted getCard(String name0, boolean fromLatestSet) {
// Sometimes they read from decks things like "CardName|Set" - but we
// can handle it
boolean isFoil = isFoil(name0);
String name = isFoil ? removeFoilSuffix(name0) : name0;
CardPrinted result = null;
final ImmutablePair<String, String> nameWithSet = CardDb.splitCardName(name);
if (nameWithSet.right != null) {
return this.getCard(nameWithSet.left, nameWithSet.right);
}
result = this.getCard(nameWithSet.left, nameWithSet.right);
} else {
if( !fromLatestSet ) {
result = this.uniqueCards.get(nameWithSet.left.toLowerCase());
if ( null == result )
throw new NoSuchElementException(String.format("Card '%s' not found in our database.", name));
} else {
// OK, plain name here
Predicate<CardPrinted> predicate = CardPrinted.Predicates.name(nameWithSet.left);
List<CardPrinted> namedCards = predicate.select(this.allCardsFlat);
if ( namedCards.isEmpty() )
throw new NoSuchElementException(String.format("Card '%s' not found in our database.", name));
// OK, plain name here
Predicate<CardPrinted> predicate = CardPrinted.Predicates.name(nameWithSet.left);
List<CardPrinted> namedCards = predicate.select(this.allCardsFlat);
if ( namedCards.isEmpty() )
throw new NoSuchElementException(String.format("Card '%s' not found in our database.", name));
// Find card with maximal set index
CardPrinted result = namedCards.get(0);
int resIndex = SetUtils.getSetByCode((result).getSet()).getIndex();
for(CardPrinted card : namedCards)
{
int thisIndex = SetUtils.getSetByCode((card).getSet()).getIndex();
if ( thisIndex > resIndex ) {
result = card;
resIndex = thisIndex;
// Find card with maximal set index
result = namedCards.get(0);
int resIndex = SetUtils.getSetByCode((result).getSet()).getIndex();
for(CardPrinted card : namedCards)
{
int thisIndex = SetUtils.getSetByCode((card).getSet()).getIndex();
if ( thisIndex > resIndex ) {
result = card;
resIndex = thisIndex;
}
}
}
}
if ( isFoil )
result = CardPrinted.makeFoiled(result);
return result;
}