Decks importer:

+ added support for new decklist text format from deckstats.net (see #4228);
  + added new checks to text format (too many empty lines, too big card number: more then 100);
  + fixed double window popup on cancel/close import from clipboard window;
This commit is contained in:
Oleg Agafonov 2017-12-07 22:46:31 +04:00
parent 5b7ab0de7a
commit 1efc062f66
2 changed files with 45 additions and 12 deletions

View file

@ -691,8 +691,10 @@ public class DeckEditorPanel extends javax.swing.JPanel {
break;
case 1:
btnImportFromClipboardActionPerformed(evt);
break;
case 2:
btnImportFromClipboardActionWAppendPerformed(evt);
break;
}
});
@ -814,9 +816,13 @@ public class DeckEditorPanel extends javax.swing.JPanel {
dialog.addWindowListener(new WindowAdapter() {
@Override
public void windowClosed(WindowEvent e) {
Deck newDeck = null;
try {
deck = Deck.load(DeckImporterUtil.importDeck(dialog.getTmpPath()), true, true);
refreshDeck();
newDeck = Deck.load(DeckImporterUtil.importDeck(dialog.getTmpPath()), true, true);
if (newDeck != null) {
deck = newDeck;
refreshDeck();
}
} catch (GameException e1) {
JOptionPane.showMessageDialog(MageFrame.getDesktop(), e1.getMessage(), "Error loading deck", JOptionPane.ERROR_MESSAGE);
}

View file

@ -47,27 +47,50 @@ public class TxtDeckImporter extends DeckImporter {
public static final Set<String> IGNORE_NAMES = new HashSet<>(Arrays.asList(SET_VALUES));
private boolean sideboard = false;
private boolean switchSideboardByEmptyLine = true;
private int nonEmptyLinesTotal = 0;
@Override
protected void readLine(String line, DeckCardLists deckList) {
if (line.toLowerCase().contains("sideboard")) {
sideboard = true;
return;
}
if (line.startsWith("//")) {
line = line.trim();
// switch sideboard by commands
String commentString = line.toLowerCase();
if (commentString.startsWith("//")){
// use start, not contains (card names may contain commands like "Legerdemain")
if (commentString.startsWith("//sideboard")) {
sideboard = true;
} else if (commentString.startsWith("//main")) {
sideboard = false;
// if there are commands then disable empty line switcher (see deckstats.net format)
if (nonEmptyLinesTotal > 0){
switchSideboardByEmptyLine = false;
}
}
return;
}
// Start the sideboard on empty line that follows
// at least 1 non-empty line
if (line.isEmpty() && nonEmptyLinesTotal > 0) {
// switch sideboard by empty line
if (switchSideboardByEmptyLine && line.isEmpty() && nonEmptyLinesTotal > 0) {
if(sideboard){
sbMessage.append("Found empty line at ").append(lineCount).append(", but sideboard already used. Use //main, //sideboard switcher OR empty line to devide your cards.").append('\n');
}
sideboard = true;
return;
} else {
nonEmptyLinesTotal++;
}
// single line sideboard cards see https://deckstats.net/
// SB: 3 Carnage Tyrant
boolean singleLineSideBoard = false;
if (line.startsWith("SB:")){
line = line.replace("SB:", "").trim();
singleLineSideBoard = true;
}
line = line.replace("\t", " "); // changing tabs to blanks as delimiter
int delim = line.indexOf(' ');
if (delim < 0) {
@ -87,12 +110,17 @@ public class TxtDeckImporter extends DeckImporter {
}
try {
int num = Integer.parseInt(lineNum.replaceAll("\\D+", ""));
if ((num < 0) || (num > 100)){
sbMessage.append("Invalid number (too small or too big): ").append(lineNum).append(" at line ").append(lineCount).append('\n');
return;
}
CardInfo cardInfo = CardRepository.instance.findPreferedCoreExpansionCard(lineName, true);
if (cardInfo == null) {
sbMessage.append("Could not find card: '").append(lineName).append("' at line ").append(lineCount).append('\n');
} else {
for (int i = 0; i < num; i++) {
if (!sideboard) {
if (!sideboard && !singleLineSideBoard) {
deckList.getCards().add(new DeckCardInfo(cardInfo.getName(), cardInfo.getCardNumber(), cardInfo.getSetCode()));
} else {
deckList.getSideboard().add(new DeckCardInfo(cardInfo.getName(), cardInfo.getCardNumber(), cardInfo.getSetCode()));
@ -103,5 +131,4 @@ public class TxtDeckImporter extends DeckImporter {
sbMessage.append("Invalid number: ").append(lineNum).append(" at line ").append(lineCount).append('\n');
}
}
}