Refactor Sets.saveDeck into a deck exporter series of classes.

This commit is contained in:
John Hitchings 2019-03-17 14:28:14 -07:00
parent 83d8f5a538
commit 0ea73b19df
9 changed files with 310 additions and 80 deletions

View file

@ -11,7 +11,6 @@ import javax.swing.*;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import mage.cards.Sets;
import mage.cards.decks.Deck;
import mage.client.MageFrame;
import mage.client.dialog.PreferencesDialog;
@ -19,6 +18,8 @@ import mage.client.util.gui.ColorsChooser;
import mage.client.util.gui.FastSearchUtil;
import mage.client.util.sets.ConstructedFormats;
import static mage.cards.decks.DeckFormats.DCK;
/**
*
* @author Simown
@ -328,7 +329,7 @@ public class DeckGeneratorDialog {
tmp.getParentFile().mkdirs();
tmp.createNewFile();
deck.setName(deckName);
Sets.saveDeck(tmp.getAbsolutePath(), deck.getDeckCardLists());
DCK.getExporter().writeDeck(tmp.getAbsolutePath(), deck.getDeckCardLists());
cleanUp();
return tmp.getAbsolutePath();
} catch (Exception e) {

View file

@ -1,7 +1,6 @@
package mage.client.deckeditor;
import mage.cards.Card;
import mage.cards.Sets;
import mage.cards.decks.Deck;
import mage.cards.decks.DeckCardLists;
import mage.cards.decks.DnDDeckTargetListener;
@ -35,12 +34,13 @@ import java.awt.*;
import java.awt.dnd.DropTarget;
import java.awt.event.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.*;
import java.util.concurrent.*;
import static mage.cards.decks.DeckFormats.DCK;
/**
* @author BetaSteward_at_googlemail.com
*/
@ -965,8 +965,8 @@ public class DeckEditorPanel extends javax.swing.JPanel {
DeckCardLists cardLists = deck.getDeckCardLists();
cardLists.setCardLayout(deckArea.getCardLayout());
cardLists.setSideboardLayout(deckArea.getSideboardLayout());
Sets.saveDeck(fileName, cardLists);
} catch (FileNotFoundException ex) {
DCK.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 {
MageFrame.getDesktop().setCursor(new Cursor(Cursor.DEFAULT_CURSOR));

View file

@ -1,9 +1,6 @@
package mage.cards;
import mage.Mana;
import mage.cards.decks.DeckCardInfo;
import mage.cards.decks.DeckCardLayout;
import mage.cards.decks.DeckCardLists;
import mage.cards.repository.CardCriteria;
import mage.cards.repository.CardInfo;
import mage.cards.repository.CardRepository;
@ -16,8 +13,6 @@ import mage.util.RandomUtil;
import org.apache.log4j.Logger;
import org.junit.Assert;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.*;
/**
@ -205,73 +200,4 @@ public class Sets extends HashMap<String, ExpansionSet> {
return null;
}
public static void saveDeck(String file, DeckCardLists deck) throws FileNotFoundException {
Map<String, DeckCardInfo> deckCards = new HashMap<>();
Map<String, DeckCardInfo> sideboard = new HashMap<>();
try (PrintWriter out = new PrintWriter(file)) {
if (deck.getName() != null && !deck.getName().isEmpty()) {
out.println("NAME:" + deck.getName());
}
if (deck.getAuthor() != null && !deck.getAuthor().isEmpty()) {
out.println("AUTHOR:" + deck.getAuthor());
}
for (DeckCardInfo deckCardInfo : deck.getCards()) {
if (deckCards.containsKey(deckCardInfo.getCardKey())) {
deckCards.put(deckCardInfo.getCardKey(), deckCards.get(deckCardInfo.getCardKey()).increaseQuantity());
} else {
deckCards.put(deckCardInfo.getCardKey(), deckCardInfo);
}
}
for (DeckCardInfo deckCardInfo : deck.getSideboard()) {
if (sideboard.containsKey(deckCardInfo.getCardKey())) {
sideboard.put(deckCardInfo.getCardKey(), sideboard.get(deckCardInfo.getCardKey()).increaseQuantity());
} else {
sideboard.put(deckCardInfo.getCardKey(), deckCardInfo);
}
}
// Write out all of the cards
for (Entry<String, DeckCardInfo> entry : deckCards.entrySet()) {
out.printf("%d [%s:%s] %s%n", entry.getValue().getQuantity(), entry.getValue().getSetCode(), entry.getValue().getCardNum(), entry.getValue().getCardName());
}
for (Entry<String, DeckCardInfo> entry : sideboard.entrySet()) {
out.printf("SB: %d [%s:%s] %s%n", entry.getValue().getQuantity(), entry.getValue().getSetCode(), entry.getValue().getCardNum(), entry.getValue().getCardName());
}
// Write out the layout
out.print("LAYOUT MAIN:");
writeCardLayout(out, deck.getCardLayout());
out.print("\n");
out.print("LAYOUT SIDEBOARD:");
writeCardLayout(out, deck.getSideboardLayout());
out.print("\n");
}
}
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(")");
}
}
}
}

View file

@ -0,0 +1,90 @@
package mage.cards.decks;
import mage.cards.decks.exporter.DckExporter;
import mage.cards.decks.exporter.DeckExporter;
import mage.cards.decks.exporter.MtgoExporter;
import java.io.*;
import java.util.Optional;
public enum DeckFormats {
DCK(new DckExporter()),
MTGO(new MtgoExporter());
private final DeckExporter exporter;
DeckFormats(DeckExporter exporter) {
this.exporter = exporter;
}
public DeckExporter getExporter() {
return exporter;
}
public static Optional<DeckFormats> getFormatForExtension(String filename) {
return getExtension(filename).map(c -> {
try {
return DeckFormats.valueOf(c);
} catch (IllegalArgumentException e) {
return null;
}
});
}
public static Optional<String> getExtension(String filename) {
int i = filename.lastIndexOf('.');
if (i > 0) {
return Optional.of(filename.substring(i+1).toUpperCase());
} else {
return Optional.empty();
}
}
public static void writeDeck(String file, DeckCardLists deck) throws IOException {
writeDeck(new File(file), deck);
}
public static void writeDeck(String file, DeckCardLists deck, DeckFormats format) throws IOException {
writeDeck(new File(file), deck, format);
}
public static void writeDeck(String file, DeckCardLists deck, DeckExporter exporter) throws IOException {
writeDeck(new File(file), deck, exporter);
}
public static void writeDeck(File file, DeckCardLists deck) throws IOException {
DeckFormats format = DeckFormats.getFormatForExtension(file.getName()).orElseGet(() -> {
throw new IllegalArgumentException("Could not determine deck export format.");
});
writeDeck(file, deck, format);
}
public static void writeDeck(File file, DeckCardLists deck, DeckFormats format) throws IOException {
writeDeck(file, deck, format.getExporter());
}
public static void writeDeck(File file, DeckCardLists deck, DeckExporter exporter) throws IOException {
try (FileOutputStream out = new FileOutputStream(file)){
writeDeck(out, deck, exporter);
}
}
public static void writeDeck(OutputStream out, DeckCardLists deck, DeckFormats format) {
writeDeck(new PrintWriter(out), deck, format);
}
public static void writeDeck(OutputStream out, DeckCardLists deck, DeckExporter exporter) {
writeDeck(new PrintWriter(out), deck, exporter);
}
public static void writeDeck(PrintWriter out, DeckCardLists deck, DeckFormats format) {
writeDeck(out, deck, format.getExporter());
}
public static void writeDeck(PrintWriter out, DeckCardLists deck, DeckExporter exporter) {
exporter.writeDeck(out, deck);
out.flush();
}
}

View file

@ -0,0 +1,82 @@
package mage.cards.decks.exporter;
import mage.cards.decks.DeckCardInfo;
import mage.cards.decks.DeckCardLayout;
import mage.cards.decks.DeckCardLists;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DckExporter extends DeckExporter {
public void writeDeck(PrintWriter out, DeckCardLists deck) {
Map<String, DeckCardInfo> deckCards = new HashMap<>();
Map<String, DeckCardInfo> sideboard = new HashMap<>();
if (deck.getName() != null && !deck.getName().isEmpty()) {
out.println("NAME:" + deck.getName());
}
if (deck.getAuthor() != null && !deck.getAuthor().isEmpty()) {
out.println("AUTHOR:" + deck.getAuthor());
}
for (DeckCardInfo deckCardInfo : deck.getCards()) {
if (deckCards.containsKey(deckCardInfo.getCardKey())) {
deckCards.put(deckCardInfo.getCardKey(), deckCards.get(deckCardInfo.getCardKey()).increaseQuantity());
} else {
deckCards.put(deckCardInfo.getCardKey(), deckCardInfo);
}
}
for (DeckCardInfo deckCardInfo : deck.getSideboard()) {
if (sideboard.containsKey(deckCardInfo.getCardKey())) {
sideboard.put(deckCardInfo.getCardKey(), sideboard.get(deckCardInfo.getCardKey()).increaseQuantity());
} else {
sideboard.put(deckCardInfo.getCardKey(), deckCardInfo);
}
}
// Write out all of the cards
for (Map.Entry<String, DeckCardInfo> entry : deckCards.entrySet()) {
out.printf("%d [%s:%s] %s%n", entry.getValue().getQuantity(), entry.getValue().getSetCode(), entry.getValue().getCardNum(), entry.getValue().getCardName());
}
for (Map.Entry<String, DeckCardInfo> entry : sideboard.entrySet()) {
out.printf("SB: %d [%s:%s] %s%n", entry.getValue().getQuantity(), entry.getValue().getSetCode(), entry.getValue().getCardNum(), entry.getValue().getCardName());
}
// Write out the layout
out.print("LAYOUT MAIN:");
writeCardLayout(out, deck.getCardLayout());
out.print("\n");
out.print("LAYOUT SIDEBOARD:");
writeCardLayout(out, deck.getSideboardLayout());
out.print("\n");
}
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(")");
}
}
}
}

View file

@ -0,0 +1,24 @@
package mage.cards.decks.exporter;
import mage.cards.decks.DeckCardLists;
import mage.cards.decks.DeckFormats;
import java.io.*;
public abstract class DeckExporter {
public void writeDeck(String file, DeckCardLists deck) throws IOException {
DeckFormats.writeDeck(file, deck, this);
}
public void writeDeck(File file, DeckCardLists deck) throws IOException {
DeckFormats.writeDeck(file, deck, this);
}
public void writeDeck(OutputStream out, DeckCardLists deck) {
DeckFormats.writeDeck(out, deck, this);
}
public abstract void writeDeck(PrintWriter out, DeckCardLists deck);
}

View file

@ -0,0 +1,43 @@
package mage.cards.decks.exporter;
import mage.cards.decks.DeckCardInfo;
import mage.cards.decks.DeckCardLists;
import java.io.PrintWriter;
import java.util.List;
import java.util.TreeMap;
public class MtgoExporter extends DeckExporter {
@Override
public void writeDeck(PrintWriter out, DeckCardLists deck) {
TreeMap<String, Integer> deckCards = toCardMap(deck.getCards());
TreeMap<String, Integer> sideboard = toCardMap(deck.getSideboard());
deckCards.forEach((name, count) -> {
out.print(count);
out.print(' ');
out.println(name);
});
out.println();
out.println();
sideboard.forEach((name, count) -> {
out.print(count);
out.print(' ');
out.println(name);
});
out.println();
}
private TreeMap<String, Integer> toCardMap(List<DeckCardInfo> cards) {
TreeMap<String, Integer> counts = new TreeMap<>();
for (DeckCardInfo card : cards) {
int count = counts.getOrDefault(card.getCardName(), 0) + card.getQuantity();
counts.put(card.getCardName(), count);
}
return counts;
}
}

View file

@ -0,0 +1,32 @@
package mage.cards.decks.exporter;
import mage.cards.decks.DeckCardInfo;
import mage.cards.decks.DeckCardLists;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import static org.junit.Assert.assertEquals;
public class DckExporterTest {
@Test
public void writeDeck() throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DeckCardLists deck = new DeckCardLists();
deck.getCards().add(new DeckCardInfo("Forest", "RNA", "1", 2));
deck.getCards().add(new DeckCardInfo("Plains", "RNA", "2", 3));
deck.getSideboard().add(new DeckCardInfo("Island", "RNA", "3", 2));
DckExporter exporter = new DckExporter();
exporter.writeDeck(baos, deck);
assertEquals(
"2 [1:RNA] Forest\n" +
"3 [2:RNA] Plains\n" +
"SB: 2 [3:RNA] Island\n" +
"LAYOUT MAIN:\n" +
"LAYOUT SIDEBOARD:\n",
new String(baos.toByteArray()));
}
}

View file

@ -0,0 +1,32 @@
package mage.cards.decks.exporter;
import mage.cards.decks.DeckCardInfo;
import mage.cards.decks.DeckCardLists;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import static org.junit.Assert.assertEquals;
public class MtgoExporterTest {
@Test
public void writeDeck() throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DeckCardLists deck = new DeckCardLists();
deck.getCards().add(new DeckCardInfo("Forest", "RNA", "1", 2));
deck.getCards().add(new DeckCardInfo("Plains", "RNA", "2", 3));
deck.getSideboard().add(new DeckCardInfo("Island", "RNA", "3", 2));
MtgoExporter exporter = new MtgoExporter();
exporter.writeDeck(baos, deck);
assertEquals(
"2 Forest\n" +
"3 Plains\n" +
"\n" +
"\n" +
"2 Island\n" +
"\n", new String(baos.toByteArray()));
}
}