Search in sources :

Example 11 with DeckCardLists

use of mage.cards.decks.DeckCardLists in project mage by magefree.

the class XmageDeckExporterTest method writeDeck.

@Test
public void writeDeck() throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DeckCardLists deck = new DeckCardLists();
    deck.getCards().add(new DeckCardInfo("Forest", "1", "RNA", 2));
    deck.getCards().add(new DeckCardInfo("Plains", "2", "RNA", 3));
    // must combine
    deck.getCards().add(new DeckCardInfo("Plains", "2", "RNA", 5));
    deck.getCards().add(new DeckCardInfo("Mountain", "3", "RNA", 1));
    deck.getSideboard().add(new DeckCardInfo("Island", "1", "RNA", 2));
    // must combine
    deck.getSideboard().add(new DeckCardInfo("Island", "1", "RNA", 5));
    deck.getSideboard().add(new DeckCardInfo("Mountain", "2", "RNA", 3));
    DeckExporter exporter = new XmageDeckExporter();
    exporter.writeDeck(baos, deck);
    assertEquals("2 [RNA:1] Forest" + System.lineSeparator() + "8 [RNA:2] Plains" + System.lineSeparator() + "1 [RNA:3] Mountain" + System.lineSeparator() + "SB: 7 [RNA:1] Island" + System.lineSeparator() + "SB: 3 [RNA:2] Mountain" + System.lineSeparator(), baos.toString());
}
Also used : DeckCardLists(mage.cards.decks.DeckCardLists) DeckCardInfo(mage.cards.decks.DeckCardInfo) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 12 with DeckCardLists

use of mage.cards.decks.DeckCardLists in project mage by magefree.

the class MtgjsonDeckImportTest method testImport.

@Test
public void testImport() {
    StringBuilder errors = new StringBuilder();
    MtgjsonDeckImporter importer = new MtgjsonDeckImporter() {

        @Override
        public CardLookup getCardLookup() {
            return LOOKUP;
        }
    };
    // offline deck from https://mtgjson.com/api/v5/decks/ArcaneTempo_GRN.json
    DeckCardLists deck = importer.importDeck(Paths.get("src", "test", "data", "importer", "testdeck.json").toString(), errors, false);
    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());
}
Also used : DeckCardLists(mage.cards.decks.DeckCardLists) Test(org.junit.Test)

Example 13 with DeckCardLists

use of mage.cards.decks.DeckCardLists in project mage by magefree.

the class PlainTextDeckImporter method importDeck.

/**
 * Import deck from text file
 *
 * @param file              file to import
 * @param errorMessages     you can setup output messages to showup to user (set null for fatal exception on messages.count > 0)
 * @param saveAutoFixedFile save fixed deck file (if any fixes applied)
 * @return decks list
 */
public DeckCardLists importDeck(String fileName, StringBuilder errorMessages, boolean saveAutoFixedFile) {
    File f = new File(fileName);
    List<String> originalFile = new ArrayList<>();
    List<String> fixedFile = new ArrayList<>();
    DeckCardLists deckList = new DeckCardLists();
    if (!f.exists()) {
        logger.warn("Deckfile " + fileName + " not found.");
        return deckList;
    }
    lineCount = 0;
    sbMessage.setLength(0);
    try {
        try (Scanner scanner = new Scanner(f)) {
            boolean canFix = true;
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine().trim();
                FixedInfo fixedInfo = new FixedInfo(line);
                lineCount++;
                readLine(line, deckList, fixedInfo);
                originalFile.add(line);
                fixedFile.add(fixedInfo.getFixedLine());
                canFix = canFix && fixedInfo.getCanFix();
            }
            // auto-fix
            if (saveAutoFixedFile && canFix && !originalFile.equals(fixedFile)) {
                logger.warn("WARNING, deck file contains errors, try to apply auto-fix and save: " + f.getAbsolutePath());
                saveFixedDeckFile(fixedFile, f);
            }
            if (deckList.getCards().isEmpty() && deckList.getSideboard().isEmpty()) {
                sbMessage.append("ERROR, unknown deck format, can't find any cards").append("\n");
            }
            if (sbMessage.length() > 0) {
                if (errorMessages != null) {
                    // normal output for user
                    errorMessages.append(sbMessage);
                } else {
                    // fatal error
                    logger.fatal(sbMessage);
                }
            }
        } catch (Exception ex) {
            logger.fatal(null, ex);
        }
    } catch (Exception ex) {
        logger.fatal(null, ex);
    }
    return deckList;
}
Also used : DeckCardLists(mage.cards.decks.DeckCardLists) Scanner(java.util.Scanner) ArrayList(java.util.ArrayList) File(java.io.File)

Example 14 with DeckCardLists

use of mage.cards.decks.DeckCardLists in project mage by magefree.

the class O8dDeckImporter method importDeck.

/**
 * @param filename
 * @param errorMessages
 * @param saveAutoFixedFile do not supported for current format
 * @return
 */
@Override
public DeckCardLists importDeck(String fileName, StringBuilder errorMessages, boolean saveAutoFixedFile) {
    try {
        Document doc = getXmlDocument(fileName);
        DeckCardLists decklist = new DeckCardLists();
        List<Node> mainCards = getNodes(doc, "/deck/section[@name='Main']/card");
        decklist.setCards(mainCards.stream().flatMap(toDeckCardInfo(getCardLookup(), errorMessages)).collect(Collectors.toList()));
        List<Node> sideboardCards = getNodes(doc, "/deck/section[@name='Sideboard']/card");
        decklist.setSideboard(sideboardCards.stream().flatMap(toDeckCardInfo(getCardLookup(), errorMessages)).collect(Collectors.toList()));
        return decklist;
    } catch (Exception e) {
        logger.error("Error loading deck", e);
        errorMessages.append("There was an error loading the deck.");
        return new DeckCardLists();
    }
}
Also used : DeckCardLists(mage.cards.decks.DeckCardLists) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document)

Example 15 with DeckCardLists

use of mage.cards.decks.DeckCardLists in project mage by magefree.

the class CodDeckImporter method importDeck.

/**
 * @param filename
 * @param errorMessages
 * @param saveAutoFixedFile do not supported for current format
 * @return
 */
@Override
public DeckCardLists importDeck(String fileName, StringBuilder errorMessages, boolean saveAutoFixedFile) {
    try {
        Document doc = getXmlDocument(fileName);
        DeckCardLists decklist = new DeckCardLists();
        List<Node> mainCards = getNodes(doc, "/cockatrice_deck/zone[@name='main']/card");
        decklist.setCards(mainCards.stream().flatMap(toDeckCardInfo(getCardLookup(), errorMessages)).collect(Collectors.toList()));
        List<Node> sideboardCards = getNodes(doc, "/cockatrice_deck/zone[@name='side']/card");
        decklist.setSideboard(sideboardCards.stream().flatMap(toDeckCardInfo(getCardLookup(), errorMessages)).collect(Collectors.toList()));
        getNodes(doc, "/cockatrice_deck/deckname").forEach(n -> decklist.setName(n.getTextContent().trim()));
        return decklist;
    } catch (Exception e) {
        logger.error("Error loading deck", e);
        errorMessages.append("There was an error loading the deck.");
        return new DeckCardLists();
    }
}
Also used : DeckCardLists(mage.cards.decks.DeckCardLists) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document)

Aggregations

DeckCardLists (mage.cards.decks.DeckCardLists)23 Test (org.junit.Test)14 DeckCardInfo (mage.cards.decks.DeckCardInfo)5 File (java.io.File)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Deck (mage.cards.decks.Deck)3 MatchOptions (mage.game.match.MatchOptions)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 CardInfo (mage.cards.repository.CardInfo)2 Ignore (org.junit.Ignore)2 Document (org.w3c.dom.Document)2 Node (org.w3c.dom.Node)2 java.awt (java.awt)1 BufferedImage (java.awt.image.BufferedImage)1 FileReader (java.io.FileReader)1 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)1 SecureRandom (java.security.SecureRandom)1 List (java.util.List)1 Scanner (java.util.Scanner)1