Search in sources :

Example 1 with DeckCardInfo

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

the class MtgArenaDeckExporterTest 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.getCards().add(new DeckCardInfo("Goblin Chainwhirler", "129", "DOM", 4));
    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 MtgArenaDeckExporter();
    exporter.writeDeck(baos, deck);
    assertEquals("2 Forest (RNA) 1" + System.lineSeparator() + "8 Plains (RNA) 2" + System.lineSeparator() + "1 Mountain (RNA) 3" + System.lineSeparator() + "4 Goblin Chainwhirler (DAR) 129" + System.lineSeparator() + System.lineSeparator() + "7 Island (RNA) 1" + System.lineSeparator() + "3 Mountain (RNA) 2" + System.lineSeparator(), baos.toString());
}
Also used : DeckCardLists(mage.cards.decks.DeckCardLists) MtgArenaDeckExporter(mage.cards.decks.exporter.MtgArenaDeckExporter) DeckExporter(mage.cards.decks.exporter.DeckExporter) DeckCardInfo(mage.cards.decks.DeckCardInfo) MtgArenaDeckExporter(mage.cards.decks.exporter.MtgArenaDeckExporter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 2 with DeckCardInfo

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

the class TxtDeckImporterTest method testImportWithBlankLineAboveSideboard.

@Test
public void testImportWithBlankLineAboveSideboard() {
    TxtDeckImporter importer = new TxtDeckImporter(false);
    CardInfo card;
    DeckCardLists deck = new DeckCardLists();
    String[] cards = { "Plains", "Forest", "Island" };
    String[] sideboard = { "Swamp", "Mountain" };
    for (String c : cards) {
        card = CardRepository.instance.findPreferredCoreExpansionCard(c, true);
        assert card != null;
        deck.getCards().add(new DeckCardInfo(card.getName(), card.getCardNumber(), card.getSetCode()));
    }
    for (String s : sideboard) {
        card = CardRepository.instance.findPreferredCoreExpansionCard(s, true);
        assert card != null;
        deck.getSideboard().add(new DeckCardInfo(card.getName(), card.getCardNumber(), card.getSetCode()));
    }
    Assert.assertEquals("Deck does not contain 3 cards, found " + deck.getCards().size(), 3, deck.getCards().size());
    Assert.assertEquals("Sideboard does not contain 2 cards, found " + deck.getSideboard().size(), 2, deck.getSideboard().size());
    DeckCardLists imported = importer.importDeck("JustLands.txt", false);
    Assert.assertEquals("Imported deck does not contain 3 cards, found " + imported.getCards().size(), 3, imported.getCards().size());
    Assert.assertEquals("Imported sideboard does not contain 2 cards, found " + imported.getSideboard().size(), 2, imported.getSideboard().size());
}
Also used : DeckCardLists(mage.cards.decks.DeckCardLists) DeckCardInfo(mage.cards.decks.DeckCardInfo) TxtDeckImporter(mage.cards.decks.importer.TxtDeckImporter) DeckCardInfo(mage.cards.decks.DeckCardInfo) CardInfo(mage.cards.repository.CardInfo) Test(org.junit.Test)

Example 3 with DeckCardInfo

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

the class SelectionBox method setCards.

// Update the contents of the card grid
public void setCards(CardsView cardsView, DeckCardLayout layout, BigCard bigCard) {
    if (bigCard != null) {
        lastBigCard = bigCard;
    }
    // Remove all of the cards not in the cardsView
    // Until contested
    boolean didModify = false;
    for (int i = 0; i < cardGrid.size(); ++i) {
        List<List<CardView>> gridRow = cardGrid.get(i);
        for (int j = 0; j < gridRow.size(); ++j) {
            List<CardView> stack = gridRow.get(j);
            for (int k = 0; k < stack.size(); ++k) {
                CardView card = stack.get(k);
                if (!cardsView.containsKey(card.getId())) {
                    // Remove it
                    removeCardView(card);
                    stack.remove(k--);
                    // Mark
                    didModify = true;
                }
            }
        }
    }
    // clear grid from empty rows
    if (didModify) {
        trimGrid();
    }
    if (layout == null) {
        // No layout -> add any new card views one at a time as par the current sort
        for (CardView newCard : cardsView.values()) {
            if (!cardViews.containsKey(newCard.getId())) {
                // Is a new card
                addCardView(newCard, false);
                // Put it into the appropirate place in the grid given the current sort
                sortIntoGrid(newCard);
                // Mark
                didModify = true;
            }
        }
    } else {
        // Layout given -> Build card grid using layout, and set sort / separate
        // Always modify when given a layout
        didModify = true;
        // Load in settings
        loadSettings(Settings.parse(layout.getSettings()));
        // Traverse the cards once and track them so we can pick ones to insert into the grid
        Map<String, Map<String, List<CardView>>> trackedCards = new HashMap<>();
        for (CardView newCard : cardsView.values()) {
            if (!cardViews.containsKey(newCard.getId())) {
                // Add the new card
                addCardView(newCard, false);
                // Add the new card to tracking
                Map<String, List<CardView>> forSetCode;
                if (trackedCards.containsKey(newCard.getExpansionSetCode())) {
                    forSetCode = trackedCards.get(newCard.getExpansionSetCode());
                } else {
                    forSetCode = new HashMap<>();
                    trackedCards.put(newCard.getExpansionSetCode(), forSetCode);
                }
                List<CardView> list;
                if (forSetCode.containsKey(newCard.getCardNumber())) {
                    list = forSetCode.get(newCard.getCardNumber());
                } else {
                    list = new ArrayList<>();
                    forSetCode.put(newCard.getCardNumber(), list);
                }
                list.add(newCard);
            }
        }
        // Now go through the layout and use it to build the cardGrid
        cardGrid = new ArrayList<>();
        maxStackSize = new ArrayList<>();
        for (List<List<DeckCardInfo>> row : layout.getCards()) {
            List<List<CardView>> gridRow = new ArrayList<>();
            int thisMaxStackSize = 0;
            cardGrid.add(gridRow);
            for (List<DeckCardInfo> stack : row) {
                List<CardView> gridStack = new ArrayList<>();
                gridRow.add(gridStack);
                for (DeckCardInfo info : stack) {
                    if (trackedCards.containsKey(info.getSetCode()) && trackedCards.get(info.getSetCode()).containsKey(info.getCardNum())) {
                        List<CardView> candidates = trackedCards.get(info.getSetCode()).get(info.getCardNum());
                        if (!candidates.isEmpty()) {
                            gridStack.add(candidates.remove(0));
                            thisMaxStackSize = Math.max(thisMaxStackSize, gridStack.size());
                        }
                    }
                }
            }
            maxStackSize.add(thisMaxStackSize);
        }
        // the file in an invalid way, sort them into the grid so that they aren't just left hanging.
        for (Map<String, List<CardView>> tracked : trackedCards.values()) {
            for (List<CardView> orphans : tracked.values()) {
                for (CardView orphan : orphans) {
                    logger.info("Orphan when setting with layout: ");
                    sortIntoGrid(orphan);
                }
            }
        }
    }
    if (didModify) {
        // clear grid from empty rows
        trimGrid();
        // Update layout
        layoutGrid();
        repaintGrid();
    }
}
Also used : DeckCardInfo(mage.cards.decks.DeckCardInfo) CardView(mage.view.CardView) List(java.util.List)

Example 4 with DeckCardInfo

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

the class DckDeckImporter method readLine.

@Override
protected void readLine(String line, DeckCardLists deckList, FixedInfo fixedInfo) {
    if (line.isEmpty() || line.startsWith("#")) {
        return;
    }
    // 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()) {
        if (line.contains(fix.getKey())) {
            line = line.replace(fix.getKey(), fix.getValue());
        }
    }
    fixedInfo.setFixedLine(line);
    Matcher m = pattern.matcher(line);
    if (m.matches()) {
        boolean sideboard = false;
        if ("SB:".equals(m.group(1))) {
            sideboard = true;
        }
        int count = Integer.parseInt(m.group(2));
        String setCode = m.group(3);
        String cardNum = m.group(4);
        String cardName = m.group(5);
        cardNum = cardNum == null ? "" : cardNum.trim();
        setCode = setCode == null ? "" : setCode.trim();
        cardName = cardName == null ? "" : cardName.trim();
        // text for auto-fix (don't work on extra spaces between numbers and name -- it's ok)
        String originalNumbers = "";
        String originalNumbersWithName = "";
        if (!setCode.isEmpty() && !cardNum.isEmpty()) {
            // [ISD:144] card_name
            originalNumbers = "[" + setCode + ":" + cardNum + "]";
            originalNumbersWithName = originalNumbers + " " + cardName;
        }
        // search priority: set/number -> name
        // with bulletproof on card number or name changes
        DeckCardInfo deckCardInfo = null;
        // search by set/number
        CardInfo foundedCard = CardRepository.instance.findCard(setCode, cardNum, true);
        boolean wasOutdated = false;
        if ((foundedCard != null) && !foundedCard.getName().equals(cardName)) {
            sbMessage.append("Line ").append(lineCount).append(": ").append("found outdated card number or name, will try to replace: ").append(line).append('\n');
            wasOutdated = true;
            foundedCard = null;
        }
        // search by name
        if (foundedCard == null) {
            if (!wasOutdated) {
                sbMessage.append("Line ").append(lineCount).append(": ").append("can't find card by number, will try to replace: ").append(line).append('\n');
            }
            if (!cardName.equals("")) {
                foundedCard = CardRepository.instance.findPreferredCoreExpansionCard(cardName, false, setCode);
            }
            if (foundedCard != null) {
                if (foundedCard.isNightCard()) {
                    sbMessage.append("Line ").append(lineCount).append(": ").append("ERROR, you can't use night card in deck [").append(foundedCard.getName()).append("]").append('\n');
                } else {
                    sbMessage.append("Line ").append(lineCount).append(": ").append("replaced to [").append(foundedCard.getSetCode()).append(":").append(foundedCard.getCardNumberAsInt()).append("] ").append(foundedCard.getName()).append('\n');
                    // AUTO-FIX POSSIBLE (apply and save it for another lines like layout)
                    // [ISD:144] card
                    String fixNumbers = "[" + foundedCard.getSetCode() + ":" + foundedCard.getCardNumber() + "]";
                    String fixNumbersWithName = fixNumbers + " " + foundedCard.getName();
                    // name fix must goes first
                    this.possibleFixes.put(originalNumbersWithName, fixNumbersWithName);
                    this.possibleFixes.put(originalNumbers, fixNumbers);
                    String fixedLine = fixedInfo.getOriginalLine().replace(originalNumbersWithName, fixNumbersWithName).replace(originalNumbers, fixNumbers);
                    fixedInfo.setFixedLine(fixedLine);
                }
            } else {
                sbMessage.append("Line ").append(lineCount).append(": ").append("ERROR, can't find card [").append(cardName).append("]").append('\n');
            }
        }
        if (foundedCard != null) {
            deckCardInfo = new DeckCardInfo(foundedCard.getName(), foundedCard.getCardNumber(), foundedCard.getSetCode());
        }
        if (deckCardInfo != null) {
            for (int i = 0; i < count; i++) {
                if (!sideboard) {
                    deckList.getCards().add(deckCardInfo);
                } else {
                    deckList.getSideboard().add(deckCardInfo);
                }
            }
        }
    } else if (line.startsWith("NAME:")) {
        deckList.setName(line.substring(5));
    } else if (line.startsWith("AUTHOR:")) {
        deckList.setAuthor(line.substring(7));
    } else if (line.startsWith("LAYOUT")) {
        Matcher m2 = layoutPattern.matcher(line);
        if (m2.find()) {
            String target = m2.group(1);
            int rows = Integer.parseInt(m2.group(2));
            int cols = Integer.parseInt(m2.group(3));
            String settings = m2.group(4);
            String stackData = m2.group(5);
            Matcher stackMatcher = layoutStackPattern.matcher(stackData);
            // 
            List<List<List<DeckCardInfo>>> grid = new ArrayList<>();
            int totalCardCount = 0;
            for (int row = 0; row < rows; ++row) {
                List<List<DeckCardInfo>> rowData = new ArrayList<>();
                grid.add(rowData);
                for (int col = 0; col < cols; ++col) {
                    List<DeckCardInfo> stack = new ArrayList<>();
                    rowData.add(stack);
                    if (stackMatcher.find()) {
                        String thisStackData = stackMatcher.group(1);
                        Matcher stackEntries = layoutStackEntryPattern.matcher(thisStackData);
                        while (stackEntries.find()) {
                            ++totalCardCount;
                            stack.add(new DeckCardInfo("", stackEntries.group(2), stackEntries.group(1)));
                        }
                    } else {
                        sbMessage.append("Missing stack\n.");
                    }
                }
            }
            // 
            DeckCardLayout layout = new DeckCardLayout(grid, settings);
            int expectedCount = 0;
            switch(target) {
                case "MAIN":
                    deckList.setCardLayout(layout);
                    expectedCount = deckList.getCards().size();
                    break;
                case "SIDEBOARD":
                    deckList.setSideboardLayout(layout);
                    expectedCount = deckList.getSideboard().size();
                    break;
                default:
                    sbMessage.append("Bad target `").append(target).append("` for layout.\n");
                    break;
            }
            // 
            if (totalCardCount != expectedCount) {
                sbMessage.append("Layout mismatch: Expected ").append(expectedCount).append(" cards, but got ").append(totalCardCount).append(" in layout `").append(target).append("`\n.");
            }
        } else {
            sbMessage.append("Malformed layout line");
        }
    }
}
Also used : DeckCardLayout(mage.cards.decks.DeckCardLayout) DeckCardInfo(mage.cards.decks.DeckCardInfo) Matcher(java.util.regex.Matcher) DeckCardInfo(mage.cards.decks.DeckCardInfo) CardInfo(mage.cards.repository.CardInfo) List(java.util.List) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 5 with DeckCardInfo

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

the class MWSDeckImporter method readLine.

@Override
protected void readLine(String line, DeckCardLists deckList, FixedInfo fixedInfo) {
    if (line.isEmpty() || line.startsWith("//")) {
        return;
    }
    boolean sideboard = false;
    if (line.startsWith("SB:")) {
        line = line.substring(3).trim();
        sideboard = true;
    }
    int delim = line.indexOf(' ');
    String lineNum = line.substring(0, delim).trim();
    String setCode = "";
    if (line.indexOf('[') != -1) {
        int setStart = line.indexOf('[') + 1;
        int setEnd = line.indexOf(']');
        setCode = line.substring(setStart, setEnd).trim();
        delim = setEnd;
    }
    String lineName = line.substring(delim + 1).trim();
    try {
        int num = Integer.parseInt(lineNum);
        CardInfo cardInfo = null;
        if (!setCode.isEmpty()) {
            CardCriteria criteria = new CardCriteria();
            criteria.name(lineName);
            criteria.setCodes(setCode);
            List<CardInfo> cards = getCardLookup().lookupCardInfo(criteria);
            if (!cards.isEmpty()) {
                cardInfo = cards.get(RandomUtil.nextInt(cards.size()));
            }
        }
        if (cardInfo == null) {
            cardInfo = getCardLookup().lookupCardInfo(lineName).orElse(null);
        }
        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) {
                    deckList.getCards().add(new DeckCardInfo(cardInfo.getName(), cardInfo.getCardNumber(), cardInfo.getSetCode()));
                } else {
                    deckList.getSideboard().add(new DeckCardInfo(cardInfo.getName(), cardInfo.getCardNumber(), cardInfo.getSetCode()));
                }
            }
        }
    } catch (NumberFormatException nfe) {
        sbMessage.append("Invalid number: ").append(lineNum).append(" at line ").append(lineCount).append('\n');
    }
}
Also used : DeckCardInfo(mage.cards.decks.DeckCardInfo) CardCriteria(mage.cards.repository.CardCriteria) DeckCardInfo(mage.cards.decks.DeckCardInfo) CardInfo(mage.cards.repository.CardInfo)

Aggregations

DeckCardInfo (mage.cards.decks.DeckCardInfo)22 CardInfo (mage.cards.repository.CardInfo)11 ArrayList (java.util.ArrayList)7 List (java.util.List)5 DeckCardLists (mage.cards.decks.DeckCardLists)5 Test (org.junit.Test)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Matcher (java.util.regex.Matcher)3 HashMap (java.util.HashMap)2 DeckCardLayout (mage.cards.decks.DeckCardLayout)2 CardCriteria (mage.cards.repository.CardCriteria)2 CardView (mage.view.CardView)2 CharSource (com.google.common.io.CharSource)1 JsonObject (com.google.gson.JsonObject)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Card (mage.cards.Card)1 Deck (mage.cards.decks.Deck)1