mirror of
https://github.com/magefree/mage.git
synced 2025-12-22 11:32:00 -08:00
[CardRepository] refactored deck importers
This commit is contained in:
parent
9039ba19c9
commit
042e4baa1f
6 changed files with 124 additions and 149 deletions
|
|
@ -29,15 +29,15 @@ package mage.cards.decks.importer;
|
||||||
|
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import mage.cards.ExpansionSet;
|
|
||||||
import mage.cards.decks.DeckCardLists;
|
import mage.cards.decks.DeckCardLists;
|
||||||
import mage.sets.Sets;
|
import mage.cards.repository.CardInfo;
|
||||||
|
import mage.cards.repository.CardRepository;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author North
|
* @author North
|
||||||
*/
|
*/
|
||||||
public class DckDeckImporter extends DeckImporterImpl {
|
public class DckDeckImporter extends DeckImporter {
|
||||||
|
|
||||||
private static final Pattern pattern = Pattern.compile("(SB:)?\\s*(\\d*)\\s*\\[([a-zA-Z0-9]{3}):(\\d*)\\].*");
|
private static final Pattern pattern = Pattern.compile("(SB:)?\\s*(\\d*)\\s*\\[([a-zA-Z0-9]{3}):(\\d*)\\].*");
|
||||||
|
|
||||||
|
|
@ -57,17 +57,18 @@ public class DckDeckImporter extends DeckImporterImpl {
|
||||||
int count = Integer.parseInt(m.group(2));
|
int count = Integer.parseInt(m.group(2));
|
||||||
String setCode = m.group(3);
|
String setCode = m.group(3);
|
||||||
int cardNum = Integer.parseInt(m.group(4));
|
int cardNum = Integer.parseInt(m.group(4));
|
||||||
ExpansionSet set = Sets.findSet(setCode);
|
|
||||||
String card = null;
|
String className = null;
|
||||||
if (set != null) {
|
CardInfo cardInfo = CardRepository.instance.findCard(setCode, cardNum);
|
||||||
card = set.findCardName(cardNum);
|
if (cardInfo != null) {
|
||||||
|
className = cardInfo.getClassName();
|
||||||
}
|
}
|
||||||
if (card != null) {
|
if (className != null) {
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < count; i++) {
|
||||||
if (!sideboard) {
|
if (!sideboard) {
|
||||||
deckList.getCards().add(card);
|
deckList.getCards().add(className);
|
||||||
} else {
|
} else {
|
||||||
deckList.getSideboard().add(card);
|
deckList.getSideboard().add(className);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -28,43 +28,50 @@
|
||||||
|
|
||||||
package mage.cards.decks.importer;
|
package mage.cards.decks.importer;
|
||||||
|
|
||||||
import mage.cards.Card;
|
import java.util.List;
|
||||||
|
import java.util.Random;
|
||||||
import mage.cards.decks.DeckCardLists;
|
import mage.cards.decks.DeckCardLists;
|
||||||
import mage.sets.Sets;
|
import mage.cards.repository.CardInfo;
|
||||||
|
import mage.cards.repository.CardRepository;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author BetaSteward_at_googlemail.com
|
* @author BetaSteward_at_googlemail.com
|
||||||
*/
|
*/
|
||||||
public class DecDeckImporter extends DeckImporterImpl {
|
public class DecDeckImporter extends DeckImporter {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void readLine(String line, DeckCardLists deckList) {
|
protected void readLine(String line, DeckCardLists deckList) {
|
||||||
if (line.length() == 0 || line.startsWith("//")) return;
|
if (line.length() == 0 || line.startsWith("//")) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
boolean sideboard = false;
|
boolean sideboard = false;
|
||||||
if (line.startsWith("SB:")) {
|
if (line.startsWith("SB:")) {
|
||||||
line = line.substring(3).trim();
|
line = line.substring(3).trim();
|
||||||
sideboard = true;
|
sideboard = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
int delim = line.indexOf(' ');
|
int delim = line.indexOf(' ');
|
||||||
String lineNum = line.substring(0, delim).trim();
|
String lineNum = line.substring(0, delim).trim();
|
||||||
String lineName = line.substring(delim).trim();
|
String lineName = line.substring(delim).trim();
|
||||||
try {
|
try {
|
||||||
int num = Integer.parseInt(lineNum);
|
int num = Integer.parseInt(lineNum);
|
||||||
Card card = Sets.findCard(lineName);
|
List<CardInfo> cards = CardRepository.instance.findCards(lineName);
|
||||||
if (card == null)
|
if (cards.isEmpty()) {
|
||||||
sbMessage.append("Could not find card: '").append(lineName).append("' at line ").append(lineCount).append("\n");
|
sbMessage.append("Could not find card: '").append(lineName).append("' at line ").append(lineCount).append("\n");
|
||||||
else {
|
} else {
|
||||||
String cardName = card.getClass().getCanonicalName();
|
Random random = new Random();
|
||||||
for (int i = 0; i < num; i++) {
|
for (int i = 0; i < num; i++) {
|
||||||
if (!sideboard)
|
String className = cards.get(random.nextInt(cards.size())).getClassName();
|
||||||
deckList.getCards().add(cardName);
|
if (!sideboard) {
|
||||||
else
|
deckList.getCards().add(className);
|
||||||
deckList.getSideboard().add(cardName);
|
} else {
|
||||||
|
deckList.getSideboard().add(className);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} catch (NumberFormatException nfe) {
|
||||||
catch (NumberFormatException nfe) {
|
|
||||||
sbMessage.append("Invalid number: ").append(lineNum).append(" at line ").append(lineCount).append("\n");
|
sbMessage.append("Invalid number: ").append(lineNum).append(" at line ").append(lineCount).append("\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -28,15 +28,53 @@
|
||||||
|
|
||||||
package mage.cards.decks.importer;
|
package mage.cards.decks.importer;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.Scanner;
|
||||||
import mage.cards.decks.DeckCardLists;
|
import mage.cards.decks.DeckCardLists;
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author BetaSteward_at_googlemail.com
|
* @author BetaSteward_at_googlemail.com
|
||||||
*/
|
*/
|
||||||
public interface DeckImporter {
|
public abstract class DeckImporter {
|
||||||
|
|
||||||
public DeckCardLists importDeck(String file);
|
private final static Logger logger = Logger.getLogger(DeckImporter.class);
|
||||||
public String getErrors();
|
protected StringBuilder sbMessage = new StringBuilder();
|
||||||
|
protected int lineCount;
|
||||||
|
|
||||||
|
public DeckCardLists importDeck(String file) {
|
||||||
|
File f = new File(file);
|
||||||
|
DeckCardLists deckList = new DeckCardLists();
|
||||||
|
lineCount = 0;
|
||||||
|
sbMessage.setLength(0);
|
||||||
|
try {
|
||||||
|
Scanner scanner = new Scanner(f);
|
||||||
|
try {
|
||||||
|
while (scanner.hasNextLine()) {
|
||||||
|
String line = scanner.nextLine().trim();
|
||||||
|
lineCount++;
|
||||||
|
readLine(line, deckList);
|
||||||
|
}
|
||||||
|
if (sbMessage.length() > 0) {
|
||||||
|
logger.fatal(sbMessage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
|
logger.fatal(null, ex);
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
scanner.close();
|
||||||
|
}
|
||||||
|
} catch (Exception ex) {
|
||||||
|
logger.fatal(null, ex);
|
||||||
|
}
|
||||||
|
return deckList;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getErrors(){
|
||||||
|
return sbMessage.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract void readLine(String line, DeckCardLists deckList);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,82 +0,0 @@
|
||||||
/*
|
|
||||||
* 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.cards.decks.importer;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.util.Scanner;
|
|
||||||
import mage.cards.decks.DeckCardLists;
|
|
||||||
import org.apache.log4j.Logger;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @author BetaSteward_at_googlemail.com
|
|
||||||
*/
|
|
||||||
public abstract class DeckImporterImpl implements DeckImporter {
|
|
||||||
|
|
||||||
private final static Logger logger = Logger.getLogger(DeckImporterImpl.class);
|
|
||||||
protected StringBuilder sbMessage = new StringBuilder();
|
|
||||||
protected int lineCount;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public DeckCardLists importDeck(String file) {
|
|
||||||
File f = new File(file);
|
|
||||||
DeckCardLists deckList = new DeckCardLists();
|
|
||||||
lineCount = 0;
|
|
||||||
sbMessage.setLength(0);
|
|
||||||
try {
|
|
||||||
Scanner scanner = new Scanner(f);
|
|
||||||
try {
|
|
||||||
while (scanner.hasNextLine()) {
|
|
||||||
String line = scanner.nextLine().trim();
|
|
||||||
lineCount++;
|
|
||||||
readLine(line, deckList);
|
|
||||||
}
|
|
||||||
if (sbMessage.length() > 0) {
|
|
||||||
logger.fatal(sbMessage);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex) {
|
|
||||||
logger.fatal(null, ex);
|
|
||||||
}
|
|
||||||
finally {
|
|
||||||
scanner.close();
|
|
||||||
}
|
|
||||||
} catch (Exception ex) {
|
|
||||||
logger.fatal(null, ex);
|
|
||||||
}
|
|
||||||
return deckList;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getErrors(){
|
|
||||||
return sbMessage.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected abstract void readLine(String line, DeckCardLists deckList);
|
|
||||||
}
|
|
||||||
|
|
@ -25,23 +25,26 @@
|
||||||
* authors and should not be interpreted as representing official policies, either expressed
|
* authors and should not be interpreted as representing official policies, either expressed
|
||||||
* or implied, of BetaSteward_at_googlemail.com.
|
* or implied, of BetaSteward_at_googlemail.com.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package mage.cards.decks.importer;
|
package mage.cards.decks.importer;
|
||||||
|
|
||||||
import mage.cards.Card;
|
import java.util.List;
|
||||||
import mage.cards.ExpansionSet;
|
import java.util.Random;
|
||||||
import mage.cards.decks.DeckCardLists;
|
import mage.cards.decks.DeckCardLists;
|
||||||
import mage.sets.Sets;
|
import mage.cards.repository.CardCriteria;
|
||||||
|
import mage.cards.repository.CardInfo;
|
||||||
|
import mage.cards.repository.CardRepository;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author BetaSteward_at_googlemail.com
|
* @author BetaSteward_at_googlemail.com
|
||||||
*/
|
*/
|
||||||
public class MWSDeckImporter extends DeckImporterImpl {
|
public class MWSDeckImporter extends DeckImporter {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void readLine(String line, DeckCardLists deckList) {
|
protected void readLine(String line, DeckCardLists deckList) {
|
||||||
if (line.length() == 0 || line.startsWith("//")) return;
|
if (line.length() == 0 || line.startsWith("//")) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
boolean sideboard = false;
|
boolean sideboard = false;
|
||||||
if (line.startsWith("SB:")) {
|
if (line.startsWith("SB:")) {
|
||||||
line = line.substring(3).trim();
|
line = line.substring(3).trim();
|
||||||
|
|
@ -50,7 +53,7 @@ public class MWSDeckImporter extends DeckImporterImpl {
|
||||||
int delim = line.indexOf(' ');
|
int delim = line.indexOf(' ');
|
||||||
String lineNum = line.substring(0, delim).trim();
|
String lineNum = line.substring(0, delim).trim();
|
||||||
String setCode = "";
|
String setCode = "";
|
||||||
if (line.indexOf('[') != -1 ) {
|
if (line.indexOf('[') != -1) {
|
||||||
int setStart = line.indexOf('[') + 1;
|
int setStart = line.indexOf('[') + 1;
|
||||||
int setEnd = line.indexOf(']');
|
int setEnd = line.indexOf(']');
|
||||||
setCode = line.substring(setStart, setEnd).trim();
|
setCode = line.substring(setStart, setEnd).trim();
|
||||||
|
|
@ -59,31 +62,32 @@ public class MWSDeckImporter extends DeckImporterImpl {
|
||||||
String lineName = line.substring(delim + 1).trim();
|
String lineName = line.substring(delim + 1).trim();
|
||||||
try {
|
try {
|
||||||
int num = Integer.parseInt(lineNum);
|
int num = Integer.parseInt(lineNum);
|
||||||
ExpansionSet set = null;
|
|
||||||
if (setCode.length() > 0)
|
CardCriteria criteria = new CardCriteria();
|
||||||
set = Sets.findSet(setCode);
|
criteria.name(lineName);
|
||||||
Card card;
|
criteria.setCodes(setCode);
|
||||||
if (set != null) {
|
List<CardInfo> cards = CardRepository.instance.findCards(criteria);
|
||||||
card = set.findCard(lineName);
|
if (cards.isEmpty()) {
|
||||||
|
criteria = new CardCriteria();
|
||||||
|
criteria.name(lineName);
|
||||||
|
cards = CardRepository.instance.findCards(criteria);
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
card = Sets.findCard(lineName);
|
if (cards.isEmpty()) {
|
||||||
}
|
|
||||||
if (card == null)
|
|
||||||
sbMessage.append("Could not find card: '").append(lineName).append("' at line ").append(lineCount).append("\n");
|
sbMessage.append("Could not find card: '").append(lineName).append("' at line ").append(lineCount).append("\n");
|
||||||
else {
|
} else {
|
||||||
String cardName = card.getClass().getCanonicalName();
|
Random random = new Random();
|
||||||
for (int i = 0; i < num; i++) {
|
for (int i = 0; i < num; i++) {
|
||||||
if (!sideboard)
|
String className = cards.get(random.nextInt(cards.size())).getClassName();
|
||||||
deckList.getCards().add(cardName);
|
if (!sideboard) {
|
||||||
else
|
deckList.getCards().add(className);
|
||||||
deckList.getSideboard().add(cardName);
|
} else {
|
||||||
|
deckList.getSideboard().add(className);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} catch (NumberFormatException nfe) {
|
||||||
catch (NumberFormatException nfe) {
|
|
||||||
sbMessage.append("Invalid number: ").append(lineNum).append(" at line ").append(lineCount).append("\n");
|
sbMessage.append("Invalid number: ").append(lineNum).append(" at line ").append(lineCount).append("\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -28,44 +28,51 @@
|
||||||
|
|
||||||
package mage.cards.decks.importer;
|
package mage.cards.decks.importer;
|
||||||
|
|
||||||
import mage.cards.Card;
|
import java.util.List;
|
||||||
|
import java.util.Random;
|
||||||
import mage.cards.decks.DeckCardLists;
|
import mage.cards.decks.DeckCardLists;
|
||||||
import mage.sets.Sets;
|
import mage.cards.repository.CardInfo;
|
||||||
|
import mage.cards.repository.CardRepository;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author BetaSteward_at_googlemail.com
|
* @author BetaSteward_at_googlemail.com
|
||||||
*/
|
*/
|
||||||
public class TxtDeckImporter extends DeckImporterImpl {
|
public class TxtDeckImporter extends DeckImporter {
|
||||||
|
|
||||||
private boolean sideboard = false;
|
private boolean sideboard = false;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void readLine(String line, DeckCardLists deckList) {
|
protected void readLine(String line, DeckCardLists deckList) {
|
||||||
if (line.length() == 0 || line.startsWith("//")) return;
|
if (line.length() == 0 || line.startsWith("//")) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (line.startsWith("Sideboard")) {
|
if (line.startsWith("Sideboard")) {
|
||||||
sideboard = true;
|
sideboard = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int delim = line.indexOf(' ');
|
int delim = line.indexOf(' ');
|
||||||
String lineNum = line.substring(0, delim).trim();
|
String lineNum = line.substring(0, delim).trim();
|
||||||
String lineName = line.substring(delim).trim();
|
String lineName = line.substring(delim).trim();
|
||||||
try {
|
try {
|
||||||
int num = Integer.parseInt(lineNum);
|
int num = Integer.parseInt(lineNum);
|
||||||
Card card = Sets.findCard(lineName);
|
List<CardInfo> cards = CardRepository.instance.findCards(lineName);
|
||||||
if (card == null)
|
if (cards.isEmpty()) {
|
||||||
sbMessage.append("Could not find card: '").append(lineName).append("' at line ").append(lineCount).append("\n");
|
sbMessage.append("Could not find card: '").append(lineName).append("' at line ").append(lineCount).append("\n");
|
||||||
else {
|
} else {
|
||||||
String cardName = card.getClass().getCanonicalName();
|
Random random = new Random();
|
||||||
for (int i = 0; i < num; i++) {
|
for (int i = 0; i < num; i++) {
|
||||||
if (!sideboard)
|
String className = cards.get(random.nextInt(cards.size())).getClassName();
|
||||||
deckList.getCards().add(cardName);
|
if (!sideboard) {
|
||||||
else
|
deckList.getCards().add(className);
|
||||||
deckList.getSideboard().add(cardName);
|
} else {
|
||||||
|
deckList.getSideboard().add(className);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} catch (NumberFormatException nfe) {
|
||||||
catch (NumberFormatException nfe) {
|
|
||||||
sbMessage.append("Invalid number: ").append(lineNum).append(" at line ").append(lineCount).append("\n");
|
sbMessage.append("Invalid number: ").append(lineNum).append(" at line ").append(lineCount).append("\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue