Add an information format for deck

This commit is contained in:
spjspj 2019-12-23 01:12:31 +11:00
parent db51b2a76c
commit 844500accf
3 changed files with 145 additions and 3 deletions

View file

@ -40,6 +40,7 @@ import java.util.*;
import java.util.concurrent.*; import java.util.concurrent.*;
import static mage.cards.decks.DeckFormats.XMAGE; import static mage.cards.decks.DeckFormats.XMAGE;
import static mage.cards.decks.DeckFormats.XMAGE_INFO;
/** /**
* @author BetaSteward_at_googlemail.com, JayDi85 * @author BetaSteward_at_googlemail.com, JayDi85
@ -67,6 +68,7 @@ public class DeckEditorPanel extends javax.swing.JPanel {
fcSelectDeck = new JFileChooser(); fcSelectDeck = new JFileChooser();
fcSelectDeck.setAcceptAllFileFilterUsed(false); fcSelectDeck.setAcceptAllFileFilterUsed(false);
fcSelectDeck.addChoosableFileFilter(new DeckFileFilter("dck", "XMage's deck files (*.dck)")); 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 = new JFileChooser();
fcImportDeck.setAcceptAllFileFilterUsed(false); fcImportDeck.setAcceptAllFileFilterUsed(false);
fcImportDeck.addChoosableFileFilter(new ImportFilter()); fcImportDeck.addChoosableFileFilter(new ImportFilter());
@ -1204,14 +1206,22 @@ public class DeckEditorPanel extends javax.swing.JPanel {
} }
try { try {
String fileName = file.getPath(); String fileName = file.getPath();
if (!fileName.endsWith(".dck")) { if (!(fileName.endsWith(".dck") || fileName.endsWith(".dck_info"))) {
fileName += ".dck"; fileName += ".dck";
} }
boolean useDeckInfo = false;
if (fileName.endsWith(".dck_info")) {
useDeckInfo = true;
}
MageFrame.getDesktop().setCursor(new Cursor(Cursor.WAIT_CURSOR)); MageFrame.getDesktop().setCursor(new Cursor(Cursor.WAIT_CURSOR));
DeckCardLists cardLists = deck.getDeckCardLists(); DeckCardLists cardLists = deck.getDeckCardLists();
cardLists.setCardLayout(deckArea.getCardLayout()); cardLists.setCardLayout(deckArea.getCardLayout());
cardLists.setSideboardLayout(deckArea.getSideboardLayout()); 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) { } catch (IOException ex) {
JOptionPane.showMessageDialog(MageFrame.getDesktop(), ex.getMessage() + "\nTry ensuring that the selected directory is writable.", "Error saving deck", JOptionPane.ERROR_MESSAGE); JOptionPane.showMessageDialog(MageFrame.getDesktop(), ex.getMessage() + "\nTry ensuring that the selected directory is writable.", "Error saving deck", JOptionPane.ERROR_MESSAGE);
} finally { } finally {
@ -1458,4 +1468,4 @@ class UpdateDeckTask extends SwingWorker<Void, Void> {
} catch (CancellationException ex) { } catch (CancellationException ex) {
} }
} }
} }

View file

@ -11,10 +11,12 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Optional; import java.util.Optional;
import mage.cards.decks.exporter.XmageInfoDeckExporter;
public enum DeckFormats { public enum DeckFormats {
XMAGE(new XmageDeckExporter()), XMAGE(new XmageDeckExporter()),
XMAGE_INFO(new XmageInfoDeckExporter()),
MTG_ONLINE(new MtgOnlineDeckExporter()), MTG_ONLINE(new MtgOnlineDeckExporter()),
MTG_ARENA(new MtgArenaDeckExporter()); MTG_ARENA(new MtgArenaDeckExporter());

View file

@ -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<DeckCardInfo> deckMain = new ArrayList<>();
List<DeckCardInfo> deckSideboard = new ArrayList<>();
Map<String, Integer> 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<List<List<DeckCardInfo>>> 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<List<DeckCardInfo>> row : cardGrid) {
for (List<DeckCardInfo> 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;
}
}