Merge pull request #6490 from timhae/issue6475

add import support for mtgjson deck files
This commit is contained in:
Oleg Agafonov 2020-04-29 20:54:18 +02:00 committed by GitHub
commit 90d0c989b1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 4772 additions and 2 deletions

View file

@ -1425,6 +1425,7 @@ class ImportFilter extends FileFilter {
|| ext.equalsIgnoreCase("dek")
|| ext.equalsIgnoreCase("cod")
|| ext.equalsIgnoreCase("o8d")
|| ext.equalsIgnoreCase("json")
|| ext.equalsIgnoreCase("draft")
|| ext.equalsIgnoreCase("mtga");
}
@ -1433,7 +1434,7 @@ class ImportFilter extends FileFilter {
@Override
public String getDescription() {
return "All formats (*.dec; *.mwDeck; *.txt; *.dek; *.cod; *.o8d; *.draft; *.mtga)";
return "All formats (*.dec; *.mwDeck; *.txt; *.dek; *.cod; *.o8d; *.json; *.draft; *.mtga;)";
}
}

View file

@ -14,12 +14,16 @@
<name>Mage Framework</name>
<dependencies>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>

View file

@ -34,6 +34,8 @@ public abstract class DeckImporter {
return new CodDeckImporter();
} else if (file.toLowerCase(Locale.ENGLISH).endsWith("o8d")) {
return new O8dDeckImporter();
} else if (file.toLowerCase(Locale.ENGLISH).endsWith("json")) {
return new MtgjsonDeckImporter();
} else if (file.toLowerCase(Locale.ENGLISH).endsWith("draft")) {
return new DraftLogImporter();
} else if (file.toLowerCase(Locale.ENGLISH).endsWith("mtga")) {

View file

@ -0,0 +1,70 @@
package mage.cards.decks.importer;
import java.io.File;
import java.io.FileReader;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import mage.cards.decks.DeckCardLists;
/**
* @author github: timhae
*/
public abstract class JsonDeckImporter extends DeckImporter {
protected StringBuilder sbMessage = new StringBuilder();
/**
*
* @param file file to import
* @param errorMessages you can setup output messages to showup to user
* @return decks list
*/
public DeckCardLists importDeck(String file, StringBuilder errorMessages) {
File f = new File(file);
DeckCardLists deckList = new DeckCardLists();
if (!f.exists()) {
logger.warn("Deckfile " + file + " not found.");
return deckList;
}
sbMessage.setLength(0);
try {
try (FileReader reader = new FileReader(f)) {
try { // Json parsing
JSONParser parser = new JSONParser();
JSONObject rootObj = (JSONObject) parser.parse(reader);
deckList.setName((String) rootObj.get("name"));
readJson(rootObj, deckList);
if (sbMessage.length() > 0) {
if (errorMessages != null) {
// normal output for user
errorMessages.append(sbMessage);
} else {
// fatal error
logger.fatal(sbMessage);
}
}
} catch (ParseException ex) {
logger.fatal(null, ex);
}
} catch (Exception ex) {
logger.fatal(null, ex);
}
} catch (Exception ex) {
logger.fatal(null, ex);
}
return deckList;
}
@Override
public DeckCardLists importDeck(String file) {
return importDeck(file, null);
}
protected abstract void readJson(JSONObject line, DeckCardLists decklist);
}

View file

@ -0,0 +1,51 @@
package mage.cards.decks.importer;
import java.util.Optional;
import java.util.List;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import mage.cards.decks.DeckCardInfo;
import mage.cards.decks.DeckCardLists;
import mage.cards.repository.CardInfo;
import mage.cards.repository.CardRepository;
/**
*
* @author github: timhae
*/
public class MtgjsonDeckImporter extends JsonDeckImporter {
@Override
protected void readJson(JSONObject rootObj, DeckCardLists deckList) {
// set
String set = (String) rootObj.get("code");
// mainboard
JSONArray mainBoard = (JSONArray) rootObj.get("mainBoard");
List<mage.cards.decks.DeckCardInfo> mainDeckList = deckList.getCards();
addBoardToList(mainBoard, mainDeckList, set);
// sideboard
JSONArray sideBoard = (JSONArray) rootObj.get("sideBoard");
List<mage.cards.decks.DeckCardInfo> sideDeckList = deckList.getSideboard();
addBoardToList(sideBoard, sideDeckList, set);
}
private void addBoardToList(JSONArray board, List<mage.cards.decks.DeckCardInfo> list, String set) {
board.forEach(arrayCard -> {
JSONObject card = (JSONObject) arrayCard;
String name = (String) card.get("name");
int num = ((Number) card.get("count")).intValue();
Optional<CardInfo> cardLookup = getCardLookup().lookupCardInfo(name, set);
if (!cardLookup.isPresent()) {
sbMessage.append("Could not find card: '").append(name).append("'\n");
} else {
CardInfo cardInfo = cardLookup.get();
for (int i = 0; i < num; i++) {
list.add(new DeckCardInfo(cardInfo.getName(), cardInfo.getCardNumber(), cardInfo.getSetCode()));
}
}
});
}
}

View file

@ -0,0 +1,57 @@
package mage.cards.decks.importer;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import mage.cards.decks.DeckCardLists;
public class MtgjsonDeckImportTest {
private static final FakeCardLookup LOOKUP = new FakeCardLookup();
@Test
public void testImport() {
StringBuilder errors = new StringBuilder();
MtgjsonDeckImporter importer = new MtgjsonDeckImporter() {
@Override
public CardLookup getCardLookup() {
return LOOKUP;
}
};
DeckCardLists deck = importer.importDeck(
"src/test/java/mage/cards/decks/importer/samples/testdeck.json", errors);
assertEquals("Arcane Tempo", deck.getName());
TestDeckChecker.checker()
.addMain("Goblin Electromancer", 4)
.addMain("Crackling Drake", 4)
.addMain("Murmuring Mystic", 2)
.addMain("Arclight Phoenix", 1)
.addMain("Niv-Mizzet, Parun", 2)
.addMain("Chart a Course", 4)
.addMain("Lava Coil", 4)
.addMain("Beacon Bolt", 1)
.addMain("Opt", 4)
.addMain("Radical Idea", 4)
.addMain("Shock", 4)
.addMain("Dive Down", 2)
.addMain("Blink of an Eye", 1)
.addMain("The Mirari Conjecture", 1)
.addMain("Sulfur Falls", 3)
.addMain("Izzet Guildgate", 4)
.addMain("Island", 8)
.addMain("Mountain", 7)
.addSide("The Mirari Conjecture", 1)
.addSide("Beacon Bolt", 1)
.addSide("Negate", 3)
.addSide("Entrancing Melody", 3)
.addSide("Fiery Cannonade", 3)
.addSide("Shivan Fire", 2)
.addSide("Disdainful Stroke", 2)
.verify(deck, 60, 15);
assertEquals("", errors.toString());
}
}

File diff suppressed because it is too large Load diff