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 { else {
if (CardDb.instance().isCardSupported(line)) { 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); result = DeckRecognizer.recognizeNonCard(line, 1);
} }
@@ -195,7 +195,7 @@ public class DeckRecognizer {
private static Token recognizePossibleNameAndNumber(final String name, final int n) { private static Token recognizePossibleNameAndNumber(final String name, final int n) {
if (CardDb.instance().isCardSupported(name)) { 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); 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) { private String makeHtmlViewOfToken(final DeckRecognizer.Token token) {
switch (token.getType()) { switch (token.getType()) {
case KnownCard: case KnownCard:
return String.format("<div class='knowncard'>%s * %s [%s]</div>", token.getNumber(), token.getCard() return String.format("<div class='knowncard'>%s * %s [%s] %s</div>", token.getNumber(), token.getCard()
.getName(), token.getCard().getSet()); .getName(), token.getCard().getSet(), token.getCard().isFoil() ? "<i>foil</i>" : "");
case UnknownCard: case UnknownCard:
return String.format("<div class='unknowncard'>%s * %s</div>", token.getNumber(), token.getText()); return String.format("<div class='unknowncard'>%s * %s</div>", token.getNumber(), token.getText());
case SectionName: case SectionName:

View File

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