From 844500accf5738141952774bebf72b4d9f37cb9d Mon Sep 17 00:00:00 2001 From: spjspj Date: Mon, 23 Dec 2019 01:12:31 +1100 Subject: [PATCH] Add an information format for deck --- .../client/deckeditor/DeckEditorPanel.java | 16 ++- .../java/mage/cards/decks/DeckFormats.java | 2 + .../decks/exporter/XmageInfoDeckExporter.java | 130 ++++++++++++++++++ 3 files changed, 145 insertions(+), 3 deletions(-) create mode 100644 Mage/src/main/java/mage/cards/decks/exporter/XmageInfoDeckExporter.java diff --git a/Mage.Client/src/main/java/mage/client/deckeditor/DeckEditorPanel.java b/Mage.Client/src/main/java/mage/client/deckeditor/DeckEditorPanel.java index 2a29199eeb..e3487b15cf 100644 --- a/Mage.Client/src/main/java/mage/client/deckeditor/DeckEditorPanel.java +++ b/Mage.Client/src/main/java/mage/client/deckeditor/DeckEditorPanel.java @@ -40,6 +40,7 @@ import java.util.*; import java.util.concurrent.*; import static mage.cards.decks.DeckFormats.XMAGE; +import static mage.cards.decks.DeckFormats.XMAGE_INFO; /** * @author BetaSteward_at_googlemail.com, JayDi85 @@ -67,6 +68,7 @@ public class DeckEditorPanel extends javax.swing.JPanel { fcSelectDeck = new JFileChooser(); fcSelectDeck.setAcceptAllFileFilterUsed(false); fcSelectDeck.addChoosableFileFilter(new DeckFileFilter("dck", "XMage's deck files (*.dck)")); + fcSelectDeck.addChoosableFileFilter(new DeckFileFilter("dck_info", "XMage's deck files with info (*.dck_info)")); fcImportDeck = new JFileChooser(); fcImportDeck.setAcceptAllFileFilterUsed(false); fcImportDeck.addChoosableFileFilter(new ImportFilter()); @@ -1204,14 +1206,22 @@ public class DeckEditorPanel extends javax.swing.JPanel { } try { String fileName = file.getPath(); - if (!fileName.endsWith(".dck")) { + if (!(fileName.endsWith(".dck") || fileName.endsWith(".dck_info"))) { fileName += ".dck"; } + boolean useDeckInfo = false; + if (fileName.endsWith(".dck_info")) { + useDeckInfo = true; + } MageFrame.getDesktop().setCursor(new Cursor(Cursor.WAIT_CURSOR)); DeckCardLists cardLists = deck.getDeckCardLists(); cardLists.setCardLayout(deckArea.getCardLayout()); cardLists.setSideboardLayout(deckArea.getSideboardLayout()); - XMAGE.getExporter().writeDeck(fileName, cardLists); + if (!useDeckInfo) { + XMAGE.getExporter().writeDeck(fileName, cardLists); + } else { + XMAGE_INFO.getExporter().writeDeck(fileName, cardLists); + } } catch (IOException ex) { JOptionPane.showMessageDialog(MageFrame.getDesktop(), ex.getMessage() + "\nTry ensuring that the selected directory is writable.", "Error saving deck", JOptionPane.ERROR_MESSAGE); } finally { @@ -1458,4 +1468,4 @@ class UpdateDeckTask extends SwingWorker { } catch (CancellationException ex) { } } -} \ No newline at end of file +} diff --git a/Mage/src/main/java/mage/cards/decks/DeckFormats.java b/Mage/src/main/java/mage/cards/decks/DeckFormats.java index 8100bb909b..1677fe79bd 100644 --- a/Mage/src/main/java/mage/cards/decks/DeckFormats.java +++ b/Mage/src/main/java/mage/cards/decks/DeckFormats.java @@ -11,10 +11,12 @@ import java.util.ArrayList; import java.util.List; import java.util.Locale; import java.util.Optional; +import mage.cards.decks.exporter.XmageInfoDeckExporter; public enum DeckFormats { XMAGE(new XmageDeckExporter()), + XMAGE_INFO(new XmageInfoDeckExporter()), MTG_ONLINE(new MtgOnlineDeckExporter()), MTG_ARENA(new MtgArenaDeckExporter()); diff --git a/Mage/src/main/java/mage/cards/decks/exporter/XmageInfoDeckExporter.java b/Mage/src/main/java/mage/cards/decks/exporter/XmageInfoDeckExporter.java new file mode 100644 index 0000000000..09d3278925 --- /dev/null +++ b/Mage/src/main/java/mage/cards/decks/exporter/XmageInfoDeckExporter.java @@ -0,0 +1,130 @@ +package mage.cards.decks.exporter; + +import mage.cards.decks.DeckCardInfo; +import mage.cards.decks.DeckCardLayout; +import mage.cards.decks.DeckCardLists; +import mage.cards.decks.DeckFileFilter; + +import javax.swing.filechooser.FileFilter; +import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import mage.cards.repository.CardInfo; +import mage.cards.repository.CardRepository; + +public class XmageInfoDeckExporter extends DeckExporter { + + private final String ext = "dck_info"; + private final String description = "XMage's deck format with information (*.dck_info)"; + private final FileFilter fileFilter = new DeckFileFilter(ext, description); + + @Override + public void writeDeck(PrintWriter out, DeckCardLists deck) { + List deckMain = new ArrayList<>(); + List deckSideboard = new ArrayList<>(); + Map amount = new HashMap<>(); + + // Info + if (deck.getName() != null && !deck.getName().isEmpty()) { + out.println("NAME:" + deck.getName()); + } + + // Author + if (deck.getAuthor() != null && !deck.getAuthor().isEmpty()) { + out.println("AUTHOR:" + deck.getAuthor()); + } + + // Main + for (DeckCardInfo card : deck.getCards()) { + String code = "M@" + card.getCardKey(); + int curAmount = amount.getOrDefault(code, 0); + if (curAmount == 0) { + deckMain.add(card); + } + amount.put(code, curAmount + card.getQuantity()); + } + + // Sideboard + for (DeckCardInfo card : deck.getSideboard()) { + String code = "S@" + card.getCardKey(); + int curAmount = amount.getOrDefault(code, 0); + if (curAmount == 0) { + deckSideboard.add(card); + } + amount.put(code, curAmount + card.getQuantity()); + } + + // Cards print + for (DeckCardInfo card : deckMain) { + CardInfo cardInfo = CardRepository.instance.findCard(card.getCardName()); + if (cardInfo == null) { + out.printf("%d [%s:%s] %s%n\n", amount.get("M@" + card.getCardKey()), card.getSetCode(), card.getCardNum(), card.getCardName()); + } else { + out.printf("%d [%s:%s] %s ;; %s ;; %s ;; %d %n", amount.get("M@" + card.getCardKey()), card.getSetCode(), card.getCardNum(), card.getCardName(), + cardInfo.getColor().getDescription(), cardInfo.getTypes().toString(), cardInfo.getConvertedManaCost()); + } + } + + for (DeckCardInfo card : deckSideboard) { + CardInfo cardInfo = CardRepository.instance.findCard(card.getCardName()); + if (cardInfo == null) { + out.printf("SB: %d [%s:%s] %s%n\n", amount.get("S@" + card.getCardKey()), card.getSetCode(), card.getCardNum(), card.getCardName()); + } else { + out.printf("SB: %d [%s:%s] %s ;; %s ;; %s ;; %d %n", amount.get("S@" + card.getCardKey()), card.getSetCode(), card.getCardNum(), card.getCardName(), + cardInfo.getColor().getDescription(), cardInfo.getTypes().toString(), cardInfo.getConvertedManaCost()); + } + } + + // layout print + if (deck.getCardLayout() != null) { + out.print("LAYOUT MAIN:"); + writeCardLayout(out, deck.getCardLayout()); + out.println(""); + out.print("LAYOUT SIDEBOARD:"); + writeCardLayout(out, deck.getSideboardLayout()); + out.println(""); + } + } + + private static void writeCardLayout(PrintWriter out, DeckCardLayout layout) { + if (layout == null) { + return; + } + List>> cardGrid = layout.getCards(); + int height = cardGrid.size(); + int width = (height > 0) ? cardGrid.get(0).size() : 0; + out.print("(" + height + ',' + width + ')'); + out.print(layout.getSettings()); + out.print("|"); + for (List> row : cardGrid) { + for (List stack : row) { + out.print("("); + for (int i = 0; i < stack.size(); ++i) { + DeckCardInfo info = stack.get(i); + out.printf("[%s:%s]", info.getSetCode(), info.getCardNum()); + if (i != stack.size() - 1) { + out.print(","); + } + } + out.print(")"); + } + } + } + + @Override + public FileFilter getFileFilter() { + return fileFilter; + } + + @Override + public String getDescription() { + return description; + } + + @Override + public String getDefaultFileExt() { + return ext; + } +}