mirror of
https://github.com/correl/mage.git
synced 2025-04-13 17:00:09 -09:00
[CardRepository] refactored deck importers
This commit is contained in:
parent
9039ba19c9
commit
042e4baa1f
6 changed files with 124 additions and 149 deletions
Mage.Sets/src/mage/cards/decks/importer
|
@ -29,15 +29,15 @@ package mage.cards.decks.importer;
|
|||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import mage.cards.ExpansionSet;
|
||||
import mage.cards.decks.DeckCardLists;
|
||||
import mage.sets.Sets;
|
||||
import mage.cards.repository.CardInfo;
|
||||
import mage.cards.repository.CardRepository;
|
||||
|
||||
/**
|
||||
*
|
||||
* @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*)\\].*");
|
||||
|
||||
|
@ -57,17 +57,18 @@ public class DckDeckImporter extends DeckImporterImpl {
|
|||
int count = Integer.parseInt(m.group(2));
|
||||
String setCode = m.group(3);
|
||||
int cardNum = Integer.parseInt(m.group(4));
|
||||
ExpansionSet set = Sets.findSet(setCode);
|
||||
String card = null;
|
||||
if (set != null) {
|
||||
card = set.findCardName(cardNum);
|
||||
|
||||
String className = null;
|
||||
CardInfo cardInfo = CardRepository.instance.findCard(setCode, cardNum);
|
||||
if (cardInfo != null) {
|
||||
className = cardInfo.getClassName();
|
||||
}
|
||||
if (card != null) {
|
||||
if (className != null) {
|
||||
for (int i = 0; i < count; i++) {
|
||||
if (!sideboard) {
|
||||
deckList.getCards().add(card);
|
||||
deckList.getCards().add(className);
|
||||
} else {
|
||||
deckList.getSideboard().add(card);
|
||||
deckList.getSideboard().add(className);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
|
|
@ -28,43 +28,50 @@
|
|||
|
||||
package mage.cards.decks.importer;
|
||||
|
||||
import mage.cards.Card;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import mage.cards.decks.DeckCardLists;
|
||||
import mage.sets.Sets;
|
||||
import mage.cards.repository.CardInfo;
|
||||
import mage.cards.repository.CardRepository;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author BetaSteward_at_googlemail.com
|
||||
*/
|
||||
public class DecDeckImporter extends DeckImporterImpl {
|
||||
public class DecDeckImporter extends DeckImporter {
|
||||
|
||||
@Override
|
||||
protected void readLine(String line, DeckCardLists deckList) {
|
||||
if (line.length() == 0 || line.startsWith("//")) return;
|
||||
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();
|
||||
String lineName = line.substring(delim).trim();
|
||||
try {
|
||||
int num = Integer.parseInt(lineNum);
|
||||
Card card = Sets.findCard(lineName);
|
||||
if (card == null)
|
||||
List<CardInfo> cards = CardRepository.instance.findCards(lineName);
|
||||
if (cards.isEmpty()) {
|
||||
sbMessage.append("Could not find card: '").append(lineName).append("' at line ").append(lineCount).append("\n");
|
||||
else {
|
||||
String cardName = card.getClass().getCanonicalName();
|
||||
} else {
|
||||
Random random = new Random();
|
||||
for (int i = 0; i < num; i++) {
|
||||
if (!sideboard)
|
||||
deckList.getCards().add(cardName);
|
||||
else
|
||||
deckList.getSideboard().add(cardName);
|
||||
String className = cards.get(random.nextInt(cards.size())).getClassName();
|
||||
if (!sideboard) {
|
||||
deckList.getCards().add(className);
|
||||
} else {
|
||||
deckList.getSideboard().add(className);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (NumberFormatException nfe) {
|
||||
} catch (NumberFormatException nfe) {
|
||||
sbMessage.append("Invalid number: ").append(lineNum).append(" at line ").append(lineCount).append("\n");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,15 +28,53 @@
|
|||
|
||||
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 interface DeckImporter {
|
||||
public abstract class DeckImporter {
|
||||
|
||||
public DeckCardLists importDeck(String file);
|
||||
public String getErrors();
|
||||
private final static Logger logger = Logger.getLogger(DeckImporter.class);
|
||||
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
|
||||
* or implied, of BetaSteward_at_googlemail.com.
|
||||
*/
|
||||
|
||||
package mage.cards.decks.importer;
|
||||
|
||||
import mage.cards.Card;
|
||||
import mage.cards.ExpansionSet;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
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
|
||||
*/
|
||||
public class MWSDeckImporter extends DeckImporterImpl {
|
||||
public class MWSDeckImporter extends DeckImporter {
|
||||
|
||||
@Override
|
||||
protected void readLine(String line, DeckCardLists deckList) {
|
||||
if (line.length() == 0 || line.startsWith("//")) return;
|
||||
if (line.length() == 0 || line.startsWith("//")) {
|
||||
return;
|
||||
}
|
||||
boolean sideboard = false;
|
||||
if (line.startsWith("SB:")) {
|
||||
line = line.substring(3).trim();
|
||||
|
@ -50,7 +53,7 @@ public class MWSDeckImporter extends DeckImporterImpl {
|
|||
int delim = line.indexOf(' ');
|
||||
String lineNum = line.substring(0, delim).trim();
|
||||
String setCode = "";
|
||||
if (line.indexOf('[') != -1 ) {
|
||||
if (line.indexOf('[') != -1) {
|
||||
int setStart = line.indexOf('[') + 1;
|
||||
int setEnd = line.indexOf(']');
|
||||
setCode = line.substring(setStart, setEnd).trim();
|
||||
|
@ -59,31 +62,32 @@ public class MWSDeckImporter extends DeckImporterImpl {
|
|||
String lineName = line.substring(delim + 1).trim();
|
||||
try {
|
||||
int num = Integer.parseInt(lineNum);
|
||||
ExpansionSet set = null;
|
||||
if (setCode.length() > 0)
|
||||
set = Sets.findSet(setCode);
|
||||
Card card;
|
||||
if (set != null) {
|
||||
card = set.findCard(lineName);
|
||||
|
||||
CardCriteria criteria = new CardCriteria();
|
||||
criteria.name(lineName);
|
||||
criteria.setCodes(setCode);
|
||||
List<CardInfo> cards = CardRepository.instance.findCards(criteria);
|
||||
if (cards.isEmpty()) {
|
||||
criteria = new CardCriteria();
|
||||
criteria.name(lineName);
|
||||
cards = CardRepository.instance.findCards(criteria);
|
||||
}
|
||||
else {
|
||||
card = Sets.findCard(lineName);
|
||||
}
|
||||
if (card == null)
|
||||
|
||||
if (cards.isEmpty()) {
|
||||
sbMessage.append("Could not find card: '").append(lineName).append("' at line ").append(lineCount).append("\n");
|
||||
else {
|
||||
String cardName = card.getClass().getCanonicalName();
|
||||
} else {
|
||||
Random random = new Random();
|
||||
for (int i = 0; i < num; i++) {
|
||||
if (!sideboard)
|
||||
deckList.getCards().add(cardName);
|
||||
else
|
||||
deckList.getSideboard().add(cardName);
|
||||
String className = cards.get(random.nextInt(cards.size())).getClassName();
|
||||
if (!sideboard) {
|
||||
deckList.getCards().add(className);
|
||||
} else {
|
||||
deckList.getSideboard().add(className);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (NumberFormatException nfe) {
|
||||
} catch (NumberFormatException nfe) {
|
||||
sbMessage.append("Invalid number: ").append(lineNum).append(" at line ").append(lineCount).append("\n");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -28,44 +28,51 @@
|
|||
|
||||
package mage.cards.decks.importer;
|
||||
|
||||
import mage.cards.Card;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import mage.cards.decks.DeckCardLists;
|
||||
import mage.sets.Sets;
|
||||
import mage.cards.repository.CardInfo;
|
||||
import mage.cards.repository.CardRepository;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author BetaSteward_at_googlemail.com
|
||||
*/
|
||||
public class TxtDeckImporter extends DeckImporterImpl {
|
||||
public class TxtDeckImporter extends DeckImporter {
|
||||
|
||||
private boolean sideboard = false;
|
||||
|
||||
@Override
|
||||
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")) {
|
||||
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);
|
||||
Card card = Sets.findCard(lineName);
|
||||
if (card == null)
|
||||
List<CardInfo> cards = CardRepository.instance.findCards(lineName);
|
||||
if (cards.isEmpty()) {
|
||||
sbMessage.append("Could not find card: '").append(lineName).append("' at line ").append(lineCount).append("\n");
|
||||
else {
|
||||
String cardName = card.getClass().getCanonicalName();
|
||||
} else {
|
||||
Random random = new Random();
|
||||
for (int i = 0; i < num; i++) {
|
||||
if (!sideboard)
|
||||
deckList.getCards().add(cardName);
|
||||
else
|
||||
deckList.getSideboard().add(cardName);
|
||||
String className = cards.get(random.nextInt(cards.size())).getClassName();
|
||||
if (!sideboard) {
|
||||
deckList.getCards().add(className);
|
||||
} else {
|
||||
deckList.getSideboard().add(className);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (NumberFormatException nfe) {
|
||||
} catch (NumberFormatException nfe) {
|
||||
sbMessage.append("Invalid number: ").append(lineNum).append(" at line ").append(lineCount).append("\n");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue