Support for importing mtga decks.

This commit is contained in:
John Hitchings 2019-05-16 13:42:00 -07:00
parent 4d95d72c66
commit b62d4fd20d
5 changed files with 116 additions and 2 deletions

View file

@ -1415,14 +1415,15 @@ class ImportFilter extends FileFilter {
|| ext.equalsIgnoreCase("dek")
|| ext.equalsIgnoreCase("cod")
|| ext.equalsIgnoreCase("o8d")
|| ext.equalsIgnoreCase("draft");
|| ext.equalsIgnoreCase("draft")
|| ext.equalsIgnoreCase("mtga");
}
return false;
}
@Override
public String getDescription() {
return "All formats (*.dec; *.mwDeck; *.txt; *.dek; *.cod; *.o8d; *.draft)";
return "All formats (*.dec; *.mwDeck; *.txt; *.dek; *.cod; *.o8d; *.draft; *.mtga)";
}
}

View file

@ -36,6 +36,8 @@ public abstract class DeckImporter {
return new O8dDeckImporter();
} else if (file.toLowerCase(Locale.ENGLISH).endsWith("draft")) {
return new DraftLogImporter();
} else if (file.toLowerCase(Locale.ENGLISH).endsWith("mtga")) {
return new MtgaImporter();
} else {
return null;
}

View file

@ -0,0 +1,58 @@
package mage.cards.decks.importer;
import com.google.common.collect.ImmutableMap;
import mage.cards.decks.DeckCardInfo;
import mage.cards.decks.DeckCardLists;
import mage.cards.repository.CardCriteria;
import mage.cards.repository.CardInfo;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.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é]+)" +
"\\p{javaWhitespace}+" +
"\\((\\p{Alnum}+)\\)" +
"\\p{javaWhitespace}+" +
"(\\p{Digit}+)");
private final CardLookup lookup = getCardLookup();
private boolean sideboard = false;
@Override
protected void readLine(String line, DeckCardLists deckList) {
if (line.trim().equals("")) {
sideboard = true;
return;
}
Matcher m = MTGA_PATTERN.matcher(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));
final List<DeckCardInfo> zone = sideboard ? deckList.getSideboard() : deckList.getCards();
Optional<CardInfo> found = lookup.lookupCardInfo(new CardCriteria().name(name).setCodes(set))
.stream()
.findAny();
if (!found.isPresent()) {
sbMessage.append("Cound not find card for '").append(line).append("'\n");
} else {
found.ifPresent(card -> zone.addAll(Collections.nCopies(count,
new DeckCardInfo(card.getName(), card.getCardNumber(), card.getSetCode()))));
}
} else {
sbMessage.append("Error reading '").append(line).append("'\n");
}
}
}

View file

@ -0,0 +1,43 @@
package mage.cards.decks.importer;
import mage.cards.decks.DeckCardLists;
import org.junit.Test;
import java.io.File;
import static org.junit.Assert.*;
public class MtgaImporterTest {
private static final FakeCardLookup LOOKUP = new FakeCardLookup();
@Test
public void testImport() {
MtgaImporter importer = new MtgaImporter() {
@Override
public CardLookup getCardLookup() {
return LOOKUP;
}
};
StringBuilder errors = new StringBuilder();
DeckCardLists deck = importer.importDeck(
"src/test/java/mage/cards/decks/importer/samples/testdeck.mtga", errors);
TestDeckChecker.checker()
.addMain("Niv-Mizzet Reborn", 1)
.addMain("Teferi, Time Raveler", 1)
.addMain("Dovin's Veto", 1)
.addMain("Knight of Autumn", 1)
.addMain("Expansion // Explosion", 1)
.addMain("Forest", 1)
.addMain("Teferi, Hero of Dominaria", 1)
.addSide("Unmoored Ego", 1)
.addSide("Beacon Bolt", 1)
.verify(deck, 7, 2);
assertEquals("", errors.toString());
}
}

View file

@ -0,0 +1,10 @@
1 Niv-Mizzet Reborn (WAR) 208
1 Teferi, Time Raveler (WAR) 221
1 Dovin's Veto (WAR) 193
1 Knight of Autumn (GRN) 183
1 Expansion // Explosion (GRN) 224
1 Forest (XLN) 277
1 Teferi, Hero of Dominaria (DAR) 207
1 Unmoored Ego (GRN) 212
1 Beacon Bolt (GRN) 154