mirror of
https://github.com/correl/mage.git
synced 2025-04-08 09:11:04 -09:00
added mwDeck and txt deck importers
This commit is contained in:
parent
d787bd70db
commit
437bdaca7f
5 changed files with 147 additions and 17 deletions
Mage.Client/src/main/java/mage/client/deckeditor
Mage.Sets/src/mage/sets
Mage/src/mage/cards
|
@ -337,7 +337,13 @@ public class DeckEditorPanel extends javax.swing.JPanel {
|
|||
File file = fcImportDeck.getSelectedFile();
|
||||
try {
|
||||
setCursor(new Cursor(Cursor.WAIT_CURSOR));
|
||||
deck = Deck.load(importDeck(file.getPath()));
|
||||
DeckImporter importer = getDeckImporter(file.getPath());
|
||||
if (importer != null) {
|
||||
deck = Deck.load(importer.importDeck(file.getPath()));
|
||||
}
|
||||
else {
|
||||
JOptionPane.showMessageDialog(MageFrame.getDesktop(), "Unknown deck format", "Error importing deck", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
Logger.getLogger(DeckEditorPanel.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
|
@ -352,13 +358,15 @@ public class DeckEditorPanel extends javax.swing.JPanel {
|
|||
fcImportDeck.setSelectedFile(null);
|
||||
}//GEN-LAST:event_btnImportActionPerformed
|
||||
|
||||
public DeckCardLists importDeck(String file) {
|
||||
DeckImporter importer;
|
||||
if (file.endsWith("dec"))
|
||||
importer = new DecDeckImporter();
|
||||
public DeckImporter getDeckImporter(String file) {
|
||||
if (file.toLowerCase().endsWith("dec"))
|
||||
return new DecDeckImporter();
|
||||
else if (file.toLowerCase().endsWith("mwdeck"))
|
||||
return new MWSDeckImporter();
|
||||
else if (file.toLowerCase().endsWith("txt"))
|
||||
return new TxtDeckImporter();
|
||||
else
|
||||
importer = new MWSDeckImporter();
|
||||
return importer.importDeck(file);
|
||||
return null;
|
||||
}
|
||||
|
||||
// Variables declaration - do not modify//GEN-BEGIN:variables
|
||||
|
@ -416,7 +424,7 @@ class ImportFilter extends FileFilter {
|
|||
ext = s.substring(i+1).toLowerCase();
|
||||
}
|
||||
if (ext != null) {
|
||||
if (ext.equals("dec") || ext.equals("mwDeck"))
|
||||
if (ext.toLowerCase().equals("dec") || ext.toLowerCase().equals("mwdeck") || ext.toLowerCase().equals("txt"))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
@ -424,6 +432,6 @@ class ImportFilter extends FileFilter {
|
|||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "*.dec | *.mwDeck";
|
||||
return "*.dec | *.mwDeck | *.txt";
|
||||
}
|
||||
}
|
|
@ -28,7 +28,9 @@
|
|||
|
||||
package mage.client.deckeditor;
|
||||
|
||||
import mage.cards.ExpansionSet;
|
||||
import mage.cards.decks.DeckCardLists;
|
||||
import mage.sets.Sets;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -38,7 +40,42 @@ public class MWSDeckImporter extends DeckImporterImpl {
|
|||
|
||||
@Override
|
||||
protected void readLine(String line, DeckCardLists deckList) {
|
||||
//TODO: implement this
|
||||
if (line.length() == 0 || 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();
|
||||
int setStart = line.indexOf('[') + 1;
|
||||
int setEnd = line.indexOf(']');
|
||||
String setCode = line.substring(setStart, setEnd).trim();
|
||||
String lineName = line.substring(setEnd + 1).trim();
|
||||
try {
|
||||
int num = Integer.parseInt(lineNum);
|
||||
ExpansionSet set = Sets.findSet(setCode);
|
||||
String cardName;
|
||||
if (set != null) {
|
||||
cardName = set.findCard(lineName);
|
||||
}
|
||||
else {
|
||||
cardName = Sets.findCard(lineName);
|
||||
}
|
||||
if (cardName == 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(cardName);
|
||||
else
|
||||
deckList.getSideboard().add(cardName);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (NumberFormatException nfe) {
|
||||
sbMessage.append("Invalid number: ").append(lineNum).append(" at line ").append(lineCount).append("\n");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,71 @@
|
|||
/*
|
||||
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are
|
||||
* permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* The views and conclusions contained in the software and documentation are those of the
|
||||
* authors and should not be interpreted as representing official policies, either expressed
|
||||
* or implied, of BetaSteward_at_googlemail.com.
|
||||
*/
|
||||
|
||||
package mage.client.deckeditor;
|
||||
|
||||
import mage.cards.decks.DeckCardLists;
|
||||
import mage.sets.Sets;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author BetaSteward_at_googlemail.com
|
||||
*/
|
||||
public class TxtDeckImporter extends DeckImporterImpl {
|
||||
|
||||
private boolean sideboard = false;
|
||||
|
||||
@Override
|
||||
protected void readLine(String line, DeckCardLists deckList) {
|
||||
if (line.length() == 0 || line.startsWith("//")) return;
|
||||
if (line.startsWith("Sideboard")) {
|
||||
sideboard = true;
|
||||
return;
|
||||
}
|
||||
int delim = line.indexOf(' ');
|
||||
String lineNum = line.substring(0, delim).trim();
|
||||
String lineName = line.substring(delim).trim();
|
||||
try {
|
||||
int num = Integer.parseInt(lineNum);
|
||||
String cardName = Sets.findCard(lineName);
|
||||
if (cardName == 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(cardName);
|
||||
else
|
||||
deckList.getSideboard().add(cardName);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (NumberFormatException nfe) {
|
||||
sbMessage.append("Invalid number: ").append(lineNum).append(" at line ").append(lineCount).append("\n");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -29,8 +29,8 @@
|
|||
package mage.sets;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import mage.cards.Card;
|
||||
import mage.cards.ExpansionSet;
|
||||
|
||||
|
@ -48,12 +48,11 @@ public class Sets extends HashMap<String, ExpansionSet> {
|
|||
}
|
||||
|
||||
private Sets() {
|
||||
names = new HashSet<String>();
|
||||
names = new TreeSet<String>();
|
||||
this.addSet(AlaraReborn.getInstance());
|
||||
this.addSet(Conflux.getInstance());
|
||||
this.addSet(Magic2010.getInstance());
|
||||
this.addSet(Magic2011.getInstance());
|
||||
// this.addSet(Planechase.getInstance());
|
||||
this.addSet(RiseOfTheEldrazi.getInstance());
|
||||
this.addSet(ShardsOfAlara.getInstance());
|
||||
this.addSet(Tenth.getInstance());
|
||||
|
@ -74,10 +73,17 @@ public class Sets extends HashMap<String, ExpansionSet> {
|
|||
|
||||
public static String findCard(String name) {
|
||||
for (ExpansionSet set: fINSTANCE.values()) {
|
||||
for (Card card: set.createCards()) {
|
||||
if (name.equals(card.getName()))
|
||||
return card.getClass().getCanonicalName();
|
||||
}
|
||||
String cardName = set.findCard(name);
|
||||
if (cardName != null)
|
||||
return cardName;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static ExpansionSet findSet(String code) {
|
||||
for (ExpansionSet set: fINSTANCE.values()) {
|
||||
if (set.getCode().equals(code))
|
||||
return set;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -133,6 +133,14 @@ public abstract class ExpansionSet implements Serializable {
|
|||
return name;
|
||||
}
|
||||
|
||||
public String findCard(String name) {
|
||||
for (Card card: createCards()) {
|
||||
if (name.equals(card.getName()))
|
||||
return card.getClass().getCanonicalName();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
protected ArrayList<Class> getCardClassesForPackage(String packageName) {
|
||||
ArrayList<Class> classes = new ArrayList<Class>();
|
||||
// Get a File object for the package
|
||||
|
|
Loading…
Add table
Reference in a new issue