Merge pull request #2254 from royk/add-deck-support

Add support for .dek files
This commit is contained in:
LevelX2 2016-09-13 11:56:46 +02:00 committed by GitHub
commit 5e7a4d265e
3 changed files with 54 additions and 2 deletions

View file

@ -925,7 +925,7 @@ class ImportFilter extends FileFilter {
ext = s.substring(i + 1).toLowerCase();
}
if (ext != null) {
if (ext.toLowerCase().equals("dec") || ext.toLowerCase().equals("mwdeck") || ext.toLowerCase().equals("txt")) {
if (ext.toLowerCase().equals("dec") || ext.toLowerCase().equals("mwdeck") || ext.toLowerCase().equals("txt") || ext.toLowerCase().equals("dek")) {
return true;
}
}
@ -934,7 +934,7 @@ class ImportFilter extends FileFilter {
@Override
public String getDescription() {
return "*.dec | *.mwDeck | *.txt";
return "*.dec | *.mwDeck | *.txt | *.dek";
}
}

View file

@ -44,6 +44,8 @@ public class DeckImporterUtil {
return new TxtDeckImporter();
} else if (file.toLowerCase().endsWith("dck")) {
return new DckDeckImporter();
} else if (file.toLowerCase().endsWith("dek")) {
return new DekDeckImporter();
} else {
return null;
}

View file

@ -0,0 +1,50 @@
package mage.cards.decks.importer;
import mage.cards.decks.DeckCardInfo;
import mage.cards.decks.DeckCardLists;
import mage.cards.repository.CardInfo;
import mage.cards.repository.CardRepository;
/**
* Created by royk on 11-Sep-16.
*/
public class DekDeckImporter extends DeckImporter {
@Override
protected void readLine(String line, DeckCardLists deckList) {
if (line.length() == 0 || line.startsWith("#") || !line.contains("<Cards CatID")) {
return;
}
try {
// e.g. <Cards CatID="61202" Quantity="1" Sideboard="false" Name="Vildin-Pack Outcast" />
Integer cardCount = Integer.parseInt(extractAttribute(line, "Quantity"));
String cardName = extractAttribute(line, "Name");
Boolean isSideboard = "true".equals(extractAttribute(line, "Sideboard"));
CardInfo cardInfo = CardRepository.instance.findPreferedCoreExpansionCard(cardName, true);
if (cardInfo == null) {
sbMessage.append("Could not find card: '").append(cardName).append("' at line ").append(lineCount).append("\n");
} else {
for (int i = 0; i < cardCount; i++) {
DeckCardInfo deckCardInfo = new DeckCardInfo(cardInfo.getName(), cardInfo.getCardNumber(), cardInfo.getSetCode());
if(isSideboard) {
deckList.getSideboard().add(deckCardInfo);
} else {
deckList.getCards().add(deckCardInfo);
}
}
}
}catch (NumberFormatException nfe) {
sbMessage.append("Invalid number: ").append(extractAttribute(line, "Quantity")).append(" at line ").append(lineCount).append("\n");
}
}
private String extractAttribute(String line, String name) {
String searchString = name+"=\"";
int startDelim = line.indexOf(searchString)+searchString.length();
int endDelim = line.substring(startDelim).indexOf("\"");
return line.substring(startDelim, startDelim+endDelim);
}
}