Clipboard .mtga deck importing & fixes to .mtga and .dck importers (#9321)

This commit is contained in:
sprangg 2022-08-01 05:42:32 +03:00 committed by GitHub
parent 4728bac28e
commit 17deba2df3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 22 deletions

View file

@ -68,7 +68,14 @@ public class DeckImportClipboardDialog extends MageDialog {
} }
private void onOK() { private void onOK() {
tmpPath = DeckUtil.writeTextToTempFile(editData.getText()); String decklist = editData.getText();
decklist = decklist.replace(FORMAT_TEXT, "");
// This dialog also accepts a paste in .mtga format
if (decklist.startsWith("Deck\n")) { // An .mtga list always starts with the first line being "Deck". This kind of paste is processed as .mtga
tmpPath = DeckUtil.writeTextToTempFile("cbimportdeck", ".mtga", decklist);
} else { // If the paste is not .mtga format, it's processed as plaintext
tmpPath = DeckUtil.writeTextToTempFile(decklist);
}
this.removeDialog(); this.removeDialog();
} }

View file

@ -19,7 +19,7 @@ public class CardNameUtil {
.replace("à", "a") .replace("à", "a")
.replace("é", "e") .replace("é", "e")
.replace("ú", "u") .replace("ú", "u")
.replace("\"", "'"); .replace("", "+");
} }
} }

View file

@ -1,5 +1,6 @@
package mage.cards.decks.importer; package mage.cards.decks.importer;
import mage.cards.decks.CardNameUtil;
import mage.cards.decks.DeckCardInfo; import mage.cards.decks.DeckCardInfo;
import mage.cards.decks.DeckCardLayout; import mage.cards.decks.DeckCardLayout;
import mage.cards.decks.DeckCardLists; import mage.cards.decks.DeckCardLists;
@ -39,6 +40,8 @@ public class DckDeckImporter extends PlainTextDeckImporter {
return; return;
} }
line = CardNameUtil.normalizeCardName(line);
// AUTO-FIX apply (if card number was fixed before then it can be replaced in layout or other lines too) // AUTO-FIX apply (if card number was fixed before then it can be replaced in layout or other lines too)
for (Map.Entry<String, String> fix : this.possibleFixes.entrySet()) { for (Map.Entry<String, String> fix : this.possibleFixes.entrySet()) {
if (line.contains(fix.getKey())) { if (line.contains(fix.getKey())) {

View file

@ -22,10 +22,10 @@ public class MtgaImporter extends PlainTextDeckImporter {
"(\\p{Digit}+)" + "(\\p{Digit}+)" +
"\\p{javaWhitespace}+" + "\\p{javaWhitespace}+" +
"(" + CARD_NAME_PATTERN.pattern() + ")" + "(" + CARD_NAME_PATTERN.pattern() + ")" +
"\\p{javaWhitespace}+" + "(?:\\p{javaWhitespace}+\\()?" +
"\\((\\p{Alnum}+)\\)" + "(\\p{Alnum}+)?" +
"\\p{javaWhitespace}+" + "(?:\\)\\p{javaWhitespace}+)?" +
"(\\p{Digit}+)"); "(\\p{Graph}+)?");
private final CardLookup lookup = getCardLookup(); private final CardLookup lookup = getCardLookup();
private boolean sideboard = false; private boolean sideboard = false;
@ -33,28 +33,42 @@ public class MtgaImporter extends PlainTextDeckImporter {
@Override @Override
protected void readLine(String line, DeckCardLists deckList, FixedInfo fixedInfo) { protected void readLine(String line, DeckCardLists deckList, FixedInfo fixedInfo) {
if (line.trim().equals("")) { line = line.trim();
if (line.equals("Deck")) {
return;
}
if (line.equals("Sideboard") || line.equals("")) {
sideboard = true; sideboard = true;
return; return;
} }
Matcher m = MTGA_PATTERN.matcher(CardNameUtil.normalizeCardName(line)); Matcher pattern = MTGA_PATTERN.matcher(CardNameUtil.normalizeCardName(line));
if (m.matches()) {
int count = Integer.parseInt(m.group(1)); if (!pattern.matches()) {
String name = m.group(2); sbMessage.append("Error reading '").append(line).append("'\n");
String set = SET_REMAPPING.getOrDefault(m.group(3), m.group(3)); return;
String cardNumber = m.group(4); }
final List<DeckCardInfo> zone = sideboard ? deckList.getSideboard() : deckList.getCards();
Optional<CardInfo> found = lookup.lookupCardInfo(name, set, cardNumber); Optional<CardInfo> found;
int count = Integer.parseInt(pattern.group(1));
String name = pattern.group(2);
if (pattern.group(3) != null && pattern.group(4) != null) {
String set = SET_REMAPPING.getOrDefault(pattern.group(3), pattern.group(3));
String cardNumber = pattern.group(4);
found = lookup.lookupCardInfo(name, set, cardNumber);
} else {
found = lookup.lookupCardInfo(name);
}
if (!found.isPresent()) { if (!found.isPresent()) {
sbMessage.append("Cound not find card for '").append(line).append("'\n"); sbMessage.append("Cound not find card for '").append(line).append("'\n");
} else { } else {
final List<DeckCardInfo> zone = sideboard ? deckList.getSideboard() : deckList.getCards();
found.ifPresent(card -> zone.addAll(Collections.nCopies(count, found.ifPresent(card -> zone.addAll(Collections.nCopies(count,
new DeckCardInfo(card.getName(), card.getCardNumber(), card.getSetCode())))); new DeckCardInfo(card.getName(), card.getCardNumber(), card.getSetCode()))));
} }
} else {
sbMessage.append("Error reading '").append(line).append("'\n");
}
} }
} }