Normalize card names, use specific cards when numbers when possible.

This commit is contained in:
John Hitchings 2019-05-19 13:26:12 -07:00
parent b62d4fd20d
commit ce23500ef8
4 changed files with 71 additions and 22 deletions

View file

@ -0,0 +1,22 @@
package mage.cards.decks;
import java.util.regex.Pattern;
public class CardNameUtil {
public static final Pattern CARD_NAME_PATTERN = Pattern.compile("[ !\"&',\\-./0-9:A-Za-z]+");
public static String normalizeCardName(String name) {
return name
.replace("&", "//")
.replace("Æ", "Ae")
.replace("ö", "o")
.replace("û", "u")
.replace("í", "i")
.replace("â", "a")
.replace("á", "a")
.replace("ú", "u")
.replace("\"", "'");
}
}

View file

@ -9,14 +9,47 @@ import mage.cards.repository.CardRepository;
public class CardLookup {
public static final CardLookup instance = new CardLookup();
public static final CardLookup instance = new CardLookup();
public Optional<CardInfo> lookupCardInfo(String name) {
return Optional.ofNullable(CardRepository.instance.findPreferedCoreExpansionCard(name, true));
}
public Optional<CardInfo> lookupCardInfo(String name) {
return Optional.ofNullable(CardRepository.instance.findPreferedCoreExpansionCard(name, true));
}
public List<CardInfo> lookupCardInfo(CardCriteria criteria) {
return CardRepository.instance.findCards(criteria);
}
public List<CardInfo> lookupCardInfo(CardCriteria criteria) {
return CardRepository.instance.findCards(criteria);
}
public Optional<CardInfo> lookupCardInfo(String name, String set) {
Optional<CardInfo> result = lookupCardInfo(new CardCriteria().name(name).setCodes(set))
.stream()
.findAny();
if (result.isPresent()) {
return result;
}
return lookupCardInfo(name);
}
public Optional<CardInfo> lookupCardInfo(String name, String set, String cardNumber) {
Optional<CardInfo> result;
try {
int intCardNumber = Integer.parseInt(cardNumber);
result = lookupCardInfo(
new CardCriteria()
.name(name)
.setCodes(set)
.minCardNumber(intCardNumber)
.maxCardNumber(intCardNumber))
.stream()
.findAny();
if (result.isPresent()) {
return result;
}
} catch (NumberFormatException ignored) {
/* ignored */
}
return lookupCardInfo(name, set);
}
}

View file

@ -1,6 +1,7 @@
package mage.cards.decks.importer;
import com.google.common.collect.ImmutableMap;
import mage.cards.decks.CardNameUtil;
import mage.cards.decks.DeckCardInfo;
import mage.cards.decks.DeckCardLists;
import mage.cards.repository.CardCriteria;
@ -13,13 +14,15 @@ import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static mage.cards.decks.CardNameUtil.CARD_NAME_PATTERN;
public class MtgaImporter extends PlainTextDeckImporter {
private static final Map<String, String> SET_REMAPPING = ImmutableMap.of("DAR", "DOM");
private static final Pattern MTGA_PATTERN = Pattern.compile(
"(\\p{Digit}+)" +
"\\p{javaWhitespace}+" +
"([ !\"&',\\-./0-9:A-Za-zé]+)" +
"(" + CARD_NAME_PATTERN.pattern() + ")" +
"\\p{javaWhitespace}+" +
"\\((\\p{Alnum}+)\\)" +
"\\p{javaWhitespace}+" +
@ -35,15 +38,14 @@ public class MtgaImporter extends PlainTextDeckImporter {
return;
}
Matcher m = MTGA_PATTERN.matcher(line);
Matcher m = MTGA_PATTERN.matcher(CardNameUtil.normalizeCardName(line));
if (m.matches()) {
int count = Integer.parseInt(m.group(1));
String name = m.group(2);
String set = SET_REMAPPING.getOrDefault(m.group(3), m.group(3));
String cardNumber = m.group(4);
final List<DeckCardInfo> zone = sideboard ? deckList.getSideboard() : deckList.getCards();
Optional<CardInfo> found = lookup.lookupCardInfo(new CardCriteria().name(name).setCodes(set))
.stream()
.findAny();
Optional<CardInfo> found = lookup.lookupCardInfo(name, set, cardNumber);
if (!found.isPresent()) {
sbMessage.append("Cound not find card for '").append(line).append("'\n");
} else {

View file

@ -1,5 +1,6 @@
package mage.cards.decks.importer;
import mage.cards.decks.CardNameUtil;
import mage.cards.decks.DeckCardInfo;
import mage.cards.decks.DeckCardLists;
import mage.cards.repository.CardInfo;
@ -109,16 +110,7 @@ public class TxtDeckImporter extends PlainTextDeckImporter {
cardAmount = 1;
}
lineName = lineName
.replace("&amp;", "//")
.replace("Æ", "Ae")
.replace("ö", "o")
.replace("û", "u")
.replace("í", "i")
.replace("â", "a")
.replace("á", "a")
.replace("ú", "u")
.replace("\"", "'");
lineName = CardNameUtil.normalizeCardName(lineName);
if (lineName.contains("//") && !lineName.contains(" // ")) {
lineName = lineName.replace("//", " // ");
}