Merge branch 'CardRepository'

This commit is contained in:
North 2012-11-05 19:37:51 +02:00
commit f64149971a
79 changed files with 1811 additions and 2686 deletions

View file

@ -121,13 +121,21 @@ public abstract class CardImpl<T extends CardImpl<T>> extends MageObjectImpl<T>
public static Card createCard(String name) {
try {
Class<?> theClass = Class.forName(name);
Constructor<?> con = theClass.getConstructor(new Class[]{UUID.class});
return createCard(Class.forName(name));
} catch (ClassNotFoundException ex) {
logger.fatal("Error loading card: " + name, ex);
return null;
}
}
public static Card createCard(Class<?> clazz) {
try {
Constructor<?> con = clazz.getConstructor(new Class[]{UUID.class});
Card card = (Card) con.newInstance(new Object[]{null});
card.build();
return card;
} catch (Exception e) {
logger.fatal("Error loading card: " + name, e);
logger.fatal("Error loading card: " + clazz.getCanonicalName(), e);
return null;
}
}

View file

@ -30,16 +30,13 @@ package mage.cards;
import mage.Constants.Rarity;
import mage.Constants.SetType;
import mage.cards.repository.CardCriteria;
import mage.cards.repository.CardInfo;
import mage.cards.repository.CardRepository;
import org.apache.log4j.Logger;
import java.io.*;
import java.lang.reflect.Constructor;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLDecoder;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
/**
* @author BetaSteward_at_googlemail.com
@ -77,26 +74,6 @@ public abstract class ExpansionSet implements Serializable {
this.releaseDate = releaseDate;
this.setType = setType;
this.packageName = packageName;
//this.cards = getCardClassesForPackage(packageName);
//this.rarities = getCardsByRarity();
}
public List<Card> getCards() {
if (cards == null) {
loadLazily();
}
return cards;
}
private void loadLazily() {
synchronized (this) {
if (cards == null) {
this.cards = getCardClassesForPackage(packageName);
}
if (rarities == null) {
this.rarities = getCardsByRarity();
}
}
}
public String getName() {
@ -119,16 +96,8 @@ public abstract class ExpansionSet implements Serializable {
return setType;
}
private Card createCard(Class clazz) {
try {
Constructor<?> con = clazz.getConstructor(new Class[]{UUID.class});
Card card = (Card) con.newInstance(new Object[]{null});
card.build();
return card;
} catch (Exception ex) {
logger.fatal("Error creating card:" + clazz.getName(), ex);
return null;
}
public String getPackageName() {
return packageName;
}
@Override
@ -136,335 +105,68 @@ public abstract class ExpansionSet implements Serializable {
return name;
}
public Card findCard(String name) {
for (Card card : getCards()) {
if (name.equalsIgnoreCase(card.getName())) {
Card newCard = card.copy();
newCard.assignNewId();
return newCard;
}
}
return null;
}
public Card findCard(int cardNum) {
for (Card card : getCards()) {
if (cardNum == card.getCardNumber()) {
Card newCard = card.copy();
newCard.assignNewId();
return newCard;
}
}
return null;
}
public Card findCard(String name, boolean random) {
List<Card> foundCards = new ArrayList<Card>();
for (Card card : getCards()) {
if (name.equalsIgnoreCase(card.getName())) {
foundCards.add(card);
}
}
if (foundCards.size() > 0) {
Card newCard = foundCards.get(rnd.nextInt(foundCards.size())).copy();
newCard.assignNewId();
return newCard;
}
return null;
}
public String findCardName(int cardNum) {
for (Card card : getCards()) {
if (card.getCardNumber() == cardNum)
return card.getClass().getCanonicalName();
}
return null;
}
private List<Card> getCardClassesForPackage(String packageName) {
ClassLoader classLoader = this.getClass().getClassLoader();
assert classLoader != null;
String path = packageName.replace(".", "/");
Enumeration<URL> resources = null;
try {
resources = classLoader.getResources(path);
} catch (IOException e) {
logger.fatal("Error loading resource - " + path, e);
}
List<File> dirs = new ArrayList<File>();
boolean isLoadingFromJar = false;
String jarPath = null;
while (resources.hasMoreElements()) {
URL resource = resources.nextElement();
if (resource.toString().startsWith("jar:")) {
isLoadingFromJar = true;
jarPath = resource.getFile();
break;
}
try {
dirs.add(new File(URLDecoder.decode(resource.getFile(), "UTF-8")));
} catch (UnsupportedEncodingException e) {
logger.fatal("Error decoding director - " + resource.getFile(), e);
}
}
List<Class> classes = new ArrayList<Class>();
if (isLoadingFromJar) {
if (jarPath.contains("!")) {
jarPath = jarPath.substring(0, jarPath.lastIndexOf('!'));
}
String filePathElement = "file:";
if (jarPath.startsWith(filePathElement)) {
try {
jarPath = URLDecoder.decode(jarPath.substring(jarPath.indexOf(filePathElement) + filePathElement.length()), "UTF-8");
} catch (UnsupportedEncodingException e) {
logger.fatal("Error decoding file - " + jarPath, e);
}
}
try {
classes.addAll(findClassesInJar(new File(jarPath), path));
} catch (ClassNotFoundException e) {
logger.fatal("Error loading classes - " + jarPath, e);
}
} else { // faster but doesn't work for jars
for (File directory : dirs) {
try {
classes.addAll(findClasses(directory, packageName));
} catch (ClassNotFoundException e) {
logger.fatal("Error loading classes - " + jarPath, e);
}
}
}
List<Card> newCards = new ArrayList<Card>();
for (Class clazz : classes) {
if (clazz.getPackage().getName().equals(packageName)) {
Card card = createCard(clazz);
if (card.isNightCard()) {
// skip second face of double-faced cards
continue;
}
newCards.add(card);
}
}
return newCards;
}
private static List<Class> findClasses(File directory, String packageName) throws ClassNotFoundException {
List<Class> classes = new ArrayList<Class>();
if (!directory.exists()) {
return classes;
}
File[] files = directory.listFiles();
if (files == null) {
return classes;
}
for (File file : files) {
if (file.isDirectory()) {
assert !file.getName().contains(".");
classes.addAll(findClasses(file, packageName + "." + file.getName()));
} else if (file.getName().endsWith(".class")) {
Class c = Class.forName(packageName + '.' + file.getName().substring(0, file.getName().length() - 6));
if (CardImpl.class.isAssignableFrom(c)) {
classes.add(c);
}
}
}
return classes;
}
private static List<Class> findClassesInJar(File file, String packageName) throws ClassNotFoundException {
List<Class> classes = new ArrayList<Class>();
if (!file.exists()) {
return classes;
}
try {
URL url = file.toURL();
URL[] urls = new URL[]{url};
ClassLoader cl = new URLClassLoader(urls);
JarInputStream jarFile = new JarInputStream(new FileInputStream(file));
JarEntry jarEntry;
while (true) {
jarEntry = jarFile.getNextJarEntry();
if (jarEntry == null) {
break;
}
if ((jarEntry.getName().startsWith(packageName)) && (jarEntry.getName().endsWith(".class"))) {
String clazz = jarEntry.getName().replaceAll("/", "\\.").replace(".class", "");
Class c = cl.loadClass(clazz);
if (CardImpl.class.isAssignableFrom(c)) {
classes.add(c);
}
}
}
} catch (Exception e) {
logger.fatal("Error loading classes - " + file, e);
}
return classes;
}
private Map<Rarity, List<Card>> getCardsByRarity() {
Map<Rarity, List<Card>> cardsByRarity = new HashMap<Rarity, List<Card>>();
for (Card card : getCards()) {
if (!cardsByRarity.containsKey(card.getRarity()))
cardsByRarity.put(card.getRarity(), new ArrayList<Card>());
cardsByRarity.get(card.getRarity()).add(card);
}
return cardsByRarity;
}
public List<Card> createBooster() {
List<Card> booster = new ArrayList<Card>();
if (!hasBoosters)
if (!hasBoosters) {
return booster;
}
if (parentSet != null) {
for (int i = 0; i < numBoosterLands; i++) {
addToBooster(booster, parentSet, Rarity.LAND);
}
} else {
for (int i = 0; i < numBoosterLands; i++) {
addToBooster(booster, this, Rarity.LAND);
}
CardCriteria criteria = new CardCriteria();
criteria.setCodes(parentSet != null ? parentSet.code : this.code).rarities(Rarity.LAND).doubleFaced(false);
List<CardInfo> basicLand = CardRepository.instance.findCards(criteria);
criteria = new CardCriteria();
criteria.setCodes(this.code).rarities(Rarity.COMMON).doubleFaced(false);
List<CardInfo> common = CardRepository.instance.findCards(criteria);
criteria = new CardCriteria();
criteria.setCodes(this.code).rarities(Rarity.UNCOMMON).doubleFaced(false);
List<CardInfo> uncommon = CardRepository.instance.findCards(criteria);
criteria = new CardCriteria();
criteria.setCodes(this.code).rarities(Rarity.RARE).doubleFaced(false);
List<CardInfo> rare = CardRepository.instance.findCards(criteria);
criteria = new CardCriteria();
criteria.setCodes(this.code).rarities(Rarity.MYTHIC).doubleFaced(false);
List<CardInfo> mythic = CardRepository.instance.findCards(criteria);
criteria = new CardCriteria();
criteria.setCodes(this.code).doubleFaced(true);
List<CardInfo> doubleFaced = CardRepository.instance.findCards(criteria);
for (int i = 0; i < numBoosterLands; i++) {
addToBooster(booster, basicLand);
}
for (int i = 0; i < numBoosterCommon; i++) {
addToBooster(booster, this, Rarity.COMMON);
addToBooster(booster, common);
}
for (int i = 0; i < numBoosterUncommon; i++) {
addToBooster(booster, this, Rarity.UNCOMMON);
addToBooster(booster, uncommon);
}
for (int i = 0; i < numBoosterRare; i++) {
if (ratioBoosterMythic > 0 && rnd.nextInt(ratioBoosterMythic) == 1) {
addToBooster(booster, this, Rarity.MYTHIC);
addToBooster(booster, mythic);
} else {
addToBooster(booster, this, Rarity.RARE);
addToBooster(booster, rare);
}
}
for (int i = 0; i < numBoosterDoubleFaced; i++) {
addToBoosterDoubleFaced(booster, this);
addToBooster(booster, doubleFaced);
}
return booster;
}
protected void addToBooster(List<Card> booster, ExpansionSet set, Rarity rarity) {
Card card = set.getRandom(rarity);
if (card != null) {
card = checkNotDoubleFaced(card, set, rarity);
card = checkNotDuplicate(card, booster, set, rarity);
Card newCard = card.copy();
newCard.assignNewId();
booster.add(newCard);
}
}
protected void addToBoosterDoubleFaced(List<Card> booster, ExpansionSet set) {
Card card = set.getRandomDoubleFaced();
if (card != null) {
Card newCard = card.copy();
newCard.assignNewId();
booster.add(newCard);
}
}
/**
* Checks that card doesn't already exist in the booster. If so, tries to generate new one several times.
*
* @param cardToCheck
* @param booster
* @param set
* @param rarity
* @return
*/
private Card checkNotDuplicate(Card cardToCheck, List<Card> booster, ExpansionSet set, Rarity rarity) {
Card card = cardToCheck;
boolean duplicate = true;
int retryCount = 5;
while (duplicate && retryCount > 0) {
if (!rarity.equals(Rarity.LAND)) {
// check for duplicates
if (hasCardByName(booster, card.getName())) {
card = set.getRandom(rarity);
} else {
duplicate = false; // no such card yet
}
} else {
duplicate = false;
}
retryCount--;
}
return card;
}
/**
* Checks that card is not double faced. If so, tries to generate new one several times.
*
* @param cardToCheck
* @param set
* @param rarity
* @return
*/
private Card checkNotDoubleFaced(Card cardToCheck, ExpansionSet set, Rarity rarity) {
int retryCount = 100;
Card card = cardToCheck;
while (card.canTransform()) {
card = set.getRandom(rarity);
retryCount--;
if (retryCount <= 0) {
logger.warn("Couldn't find non double faced card");
break;
}
}
return card;
}
protected boolean hasCardByName(List<Card> booster, String name) {
for (Card card : booster) {
if (card.getName().equals(name)) {
return true;
}
}
return false;
}
protected Card getRandom(Rarity rarity) {
if (!getRarities().containsKey(rarity))
return null;
int size = getRarities().get(rarity).size();
if (size > 0) {
return getRarities().get(rarity).get(rnd.nextInt(size)).copy();
}
return null;
}
protected Card getRandomDoubleFaced() {
int size = getCards().size();
if (size > 0) {
Card card = cards.get(rnd.nextInt(size));
int retryCount = 1000;
while (!card.canTransform()) {
card = cards.get(rnd.nextInt(size));
retryCount--;
if (retryCount <= 0) {
logger.warn("Couldn't find double-faced card.");
break;
private void addToBooster(List<Card> booster, List<CardInfo> cards) {
if (!cards.isEmpty()) {
CardInfo cardInfo = cards.remove(rnd.nextInt(cards.size()));
if (cardInfo != null) {
Card card = cardInfo.getCard();
if (card != null) {
booster.add(card);
}
}
return card;
}
return null;
}
public Map<Rarity, List<Card>> getRarities() {
if (rarities == null) {
loadLazily();
}
return rarities;
}
}

View file

@ -0,0 +1,185 @@
/*
* 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;
import mage.Constants.CardType;
import mage.Constants.ColoredManaSymbol;
import mage.cards.decks.DeckCardLists;
import mage.cards.repository.CardCriteria;
import mage.cards.repository.CardInfo;
import mage.cards.repository.CardRepository;
import mage.util.ClassScanner;
import org.apache.log4j.Logger;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.*;
/**
*
* @author BetaSteward_at_googlemail.com
*/
public class Sets extends HashMap<String, ExpansionSet> {
private final static Logger logger = Logger.getLogger(Sets.class);
private static final Sets fINSTANCE = new Sets();
protected static Random rnd = new Random();
public static Sets getInstance() {
return fINSTANCE;
}
private Sets() {
ArrayList<String> packages = new ArrayList<String>();
packages.add("mage.sets");
for (Class c : ClassScanner.findClasses(packages, ExpansionSet.class)) {
try {
addSet((ExpansionSet) c.getMethod("getInstance").invoke(null));
} catch (Exception ex) {
}
}
}
private void addSet(ExpansionSet set) {
this.put(set.getCode(), set);
}
/**
* Generates card pool of cardsCount cards that have manacost of allowed colors.
*
* @param cardsCount
* @param allowedColors
* @return
*/
public static List<Card> generateRandomCardPool(int cardsCount, List<ColoredManaSymbol> allowedColors) {
CardCriteria criteria = new CardCriteria();
criteria.notTypes(CardType.LAND);
for (ColoredManaSymbol color : allowedColors) {
switch (color) {
case W:
criteria.white(true);
break;
case U:
criteria.blue(true);
break;
case B:
criteria.black(true);
break;
case R:
criteria.red(true);
break;
case G:
criteria.green(true);
break;
}
}
List<CardInfo> cards = CardRepository.instance.findCards(criteria);
int count = 0;
int tries = 0;
List<Card> cardPool = new ArrayList<Card>();
while (count < cardsCount) {
CardInfo cardInfo = cards.get(rnd.nextInt(cards.size()));
Card card = cardInfo != null ? cardInfo.getCard() : null;
if (card != null) {
cardPool.add(card);
count++;
}
tries++;
if (tries > 4096) { // to avoid infinite loop
throw new IllegalStateException("Not enough cards for chosen colors to generate deck: " + allowedColors);
}
}
return cardPool;
}
public static ExpansionSet findSet(String code) {
if (fINSTANCE.containsKey(code)) {
return fINSTANCE.get(code);
}
return null;
}
public static void saveDeck(String file, DeckCardLists deck) throws FileNotFoundException {
PrintWriter out = new PrintWriter(file);
Map<String, Integer> deckCards = new HashMap<String, Integer>();
Map<String, Integer> sideboard = new HashMap<String, Integer>();
try {
if (deck.getName() != null && deck.getName().length() > 0) {
out.println("NAME:" + deck.getName());
}
if (deck.getAuthor() != null && deck.getAuthor().length() > 0) {
out.println("AUTHOR:" + deck.getAuthor());
}
for (String cardClass: deck.getCards()) {
if (deckCards.containsKey(cardClass)) {
deckCards.put(cardClass, deckCards.get(cardClass) + 1);
}
else {
deckCards.put(cardClass, 1);
}
}
for (String cardClass: deck.getSideboard()) {
if (sideboard.containsKey(cardClass)) {
sideboard.put(cardClass, sideboard.get(cardClass) + 1);
}
else {
sideboard.put(cardClass, 1);
}
}
for (Map.Entry<String, Integer> entry: deckCards.entrySet()) {
Card card = CardImpl.createCard(entry.getKey());
if (card != null) {
out.printf("%d [%s:%d] %s%n", entry.getValue(), card.getExpansionSetCode(), card.getCardNumber(), card.getName());
}
}
for (Map.Entry<String, Integer> entry: sideboard.entrySet()) {
Card card = CardImpl.createCard(entry.getKey());
if (card != null) {
out.printf("SB: %d [%s:%d] %s%n", entry.getValue(), card.getExpansionSetCode(), card.getCardNumber(), card.getName());
}
}
}
finally {
out.close();
}
}
public ExpansionSet[] getSortedByReleaseDate() {
ExpansionSet[] sets = Sets.getInstance().values().toArray(new ExpansionSet[0]);
Arrays.sort(sets, new Comparator<ExpansionSet>() {
@Override
public int compare(ExpansionSet o1, ExpansionSet o2) {
return o2.getReleaseDate().compareTo(o1.getReleaseDate());
}
});
return sets;
}
}

View file

@ -0,0 +1,83 @@
/*
* 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.util.regex.Matcher;
import java.util.regex.Pattern;
import mage.cards.decks.DeckCardLists;
import mage.cards.repository.CardInfo;
import mage.cards.repository.CardRepository;
/**
*
* @author North
*/
public class DckDeckImporter extends DeckImporter {
private static final Pattern pattern = Pattern.compile("(SB:)?\\s*(\\d*)\\s*\\[([a-zA-Z0-9]{3}):(\\d*)\\].*");
@Override
protected void readLine(String line, DeckCardLists deckList) {
if (line.length() == 0 || line.startsWith("#")) {
return;
}
Matcher m = pattern.matcher(line);
if (m.matches()) {
boolean sideboard = false;
if ("SB:".equals(m.group(1))) {
sideboard = true;
}
int count = Integer.parseInt(m.group(2));
String setCode = m.group(3);
int cardNum = Integer.parseInt(m.group(4));
String className = null;
CardInfo cardInfo = CardRepository.instance.findCard(setCode, cardNum);
if (cardInfo != null) {
className = cardInfo.getClassName();
}
if (className != null) {
for (int i = 0; i < count; i++) {
if (!sideboard) {
deckList.getCards().add(className);
} else {
deckList.getSideboard().add(className);
}
}
} else {
sbMessage.append("Could not find card '").append("' at line ").append(lineCount).append(": ").append(line).append("\n");
}
} else if (line.startsWith("NAME:")) {
deckList.setName(line.substring(5, line.length()));
} else if (line.startsWith("AUTHOR:")) {
deckList.setAuthor(line.substring(7, line.length()));
}
}
}

View file

@ -0,0 +1,79 @@
/*
* 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.util.List;
import java.util.Random;
import mage.cards.decks.DeckCardLists;
import mage.cards.repository.CardInfo;
import mage.cards.repository.CardRepository;
/**
*
* @author BetaSteward_at_googlemail.com
*/
public class DecDeckImporter extends DeckImporter {
@Override
protected void readLine(String line, DeckCardLists deckList) {
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);
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 {
Random random = new Random();
for (int i = 0; i < num; i++) {
String className = cards.get(random.nextInt(cards.size())).getClassName();
if (!sideboard) {
deckList.getCards().add(className);
} else {
deckList.getSideboard().add(className);
}
}
}
} catch (NumberFormatException nfe) {
sbMessage.append("Invalid number: ").append(lineNum).append(" at line ").append(lineCount).append("\n");
}
}
}

View file

@ -0,0 +1,80 @@
/*
* 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 DeckImporter {
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);
}

View file

@ -0,0 +1,60 @@
/*
* 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 mage.cards.decks.DeckCardLists;
/**
*
* @author North
*/
public class DeckImporterUtil {
public static 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 if (file.toLowerCase().endsWith("dck")) {
return new DckDeckImporter();
} else {
return null;
}
}
public static DeckCardLists importDeck(String file) {
DeckImporter deckImporter = getDeckImporter(file);
if (deckImporter != null) {
return deckImporter.importDeck(file);
} else {
return new DeckCardLists();
}
}
}

View file

@ -0,0 +1,93 @@
/*
* 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.util.List;
import java.util.Random;
import mage.cards.decks.DeckCardLists;
import mage.cards.repository.CardCriteria;
import mage.cards.repository.CardInfo;
import mage.cards.repository.CardRepository;
/**
*
* @author BetaSteward_at_googlemail.com
*/
public class MWSDeckImporter extends DeckImporter {
@Override
protected void readLine(String line, DeckCardLists deckList) {
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 setCode = "";
if (line.indexOf('[') != -1) {
int setStart = line.indexOf('[') + 1;
int setEnd = line.indexOf(']');
setCode = line.substring(setStart, setEnd).trim();
delim = setEnd;
}
String lineName = line.substring(delim + 1).trim();
try {
int num = Integer.parseInt(lineNum);
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);
}
if (cards.isEmpty()) {
sbMessage.append("Could not find card: '").append(lineName).append("' at line ").append(lineCount).append("\n");
} else {
Random random = new Random();
for (int i = 0; i < num; i++) {
String className = cards.get(random.nextInt(cards.size())).getClassName();
if (!sideboard) {
deckList.getCards().add(className);
} else {
deckList.getSideboard().add(className);
}
}
}
} catch (NumberFormatException nfe) {
sbMessage.append("Invalid number: ").append(lineNum).append(" at line ").append(lineCount).append("\n");
}
}
}

View file

@ -0,0 +1,80 @@
/*
* 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.util.List;
import java.util.Random;
import mage.cards.decks.DeckCardLists;
import mage.cards.repository.CardInfo;
import mage.cards.repository.CardRepository;
/**
*
* @author BetaSteward_at_googlemail.com
*/
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.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);
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 {
Random random = new Random();
for (int i = 0; i < num; i++) {
String className = cards.get(random.nextInt(cards.size())).getClassName();
if (!sideboard) {
deckList.getCards().add(className);
} else {
deckList.getSideboard().add(className);
}
}
}
} catch (NumberFormatException nfe) {
sbMessage.append("Invalid number: ").append(lineNum).append(" at line ").append(lineCount).append("\n");
}
}
}

View file

@ -0,0 +1,274 @@
/*
* 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.repository;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.SelectArg;
import com.j256.ormlite.stmt.Where;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import mage.Constants.CardType;
import mage.Constants.Rarity;
/**
*
* @author North
*/
public class CardCriteria {
private String name;
private String rules;
private List<String> setCodes;
private List<CardType> types;
private List<CardType> notTypes;
private List<String> supertypes;
private List<String> notSupertypes;
private List<String> subtypes;
private List<Rarity> rarities;
private Boolean doubleFaced;
private boolean black;
private boolean blue;
private boolean green;
private boolean red;
private boolean white;
private boolean colorless;
private Long start;
private Long count;
public CardCriteria() {
this.setCodes = new ArrayList<String>();
this.rarities = new ArrayList<Rarity>();
this.types = new ArrayList<CardType>();
this.notTypes = new ArrayList<CardType>();
this.supertypes = new ArrayList<String>();
this.notSupertypes = new ArrayList<String>();
this.subtypes = new ArrayList<String>();
this.black = true;
this.blue = true;
this.green = true;
this.red = true;
this.white = true;
this.colorless = true;
}
public CardCriteria black(boolean black) {
this.black = black;
return this;
}
public CardCriteria blue(boolean blue) {
this.blue = blue;
return this;
}
public CardCriteria green(boolean green) {
this.green = green;
return this;
}
public CardCriteria red(boolean red) {
this.red = red;
return this;
}
public CardCriteria white(boolean white) {
this.white = white;
return this;
}
public CardCriteria colorless(boolean colorless) {
this.colorless = colorless;
return this;
}
public CardCriteria doubleFaced(boolean doubleFaced) {
this.doubleFaced = doubleFaced;
return this;
}
public CardCriteria name(String name) {
this.name = name;
return this;
}
public CardCriteria rules(String rules) {
this.rules = rules;
return this;
}
public CardCriteria start(Long start) {
this.start = start;
return this;
}
public CardCriteria count(Long count) {
this.count = count;
return this;
}
public CardCriteria rarities(Rarity... rarities) {
this.rarities.addAll(Arrays.asList(rarities));
return this;
}
public CardCriteria setCodes(String... setCodes) {
this.setCodes.addAll(Arrays.asList(setCodes));
return this;
}
public CardCriteria types(CardType... types) {
this.types.addAll(Arrays.asList(types));
return this;
}
public CardCriteria notTypes(CardType... types) {
this.notTypes.addAll(Arrays.asList(types));
return this;
}
public CardCriteria supertypes(String... supertypes) {
this.supertypes.addAll(Arrays.asList(supertypes));
return this;
}
public CardCriteria notSupertypes(String... supertypes) {
this.notSupertypes.addAll(Arrays.asList(supertypes));
return this;
}
public CardCriteria subtypes(String... subtypes) {
this.subtypes.addAll(Arrays.asList(subtypes));
return this;
}
public void buildQuery(QueryBuilder qb) throws SQLException {
Where where = qb.where();
int clausesCount = 0;
if (name != null) {
where.like("name", new SelectArg('%' + name + '%'));
clausesCount++;
}
if (rules != null) {
where.like("rules", new SelectArg('%' + rules + '%'));
clausesCount++;
}
if (doubleFaced != null) {
where.eq("doubleFaced", doubleFaced);
clausesCount++;
}
for (Rarity rarity : rarities) {
where.eq("rarity", rarity);
}
if (!rarities.isEmpty()) {
where.or(rarities.size());
clausesCount++;
}
for (String setCode : setCodes) {
where.eq("setCode", setCode);
}
if (!setCodes.isEmpty()) {
where.or(setCodes.size());
clausesCount++;
}
for (CardType type : types) {
where.like("types", new SelectArg('%' + type.name() + '%'));
}
if (!types.isEmpty()) {
where.or(types.size());
clausesCount++;
}
for (CardType type : notTypes) {
where.not().like("types", new SelectArg('%' + type.name() + '%'));
clausesCount++;
}
for (String superType : supertypes) {
where.like("supertypes", new SelectArg('%' + superType + '%'));
clausesCount++;
}
for (String subType : notSupertypes) {
where.not().like("supertypes", new SelectArg('%' + subType + '%'));
clausesCount++;
}
for (String subType : subtypes) {
where.like("subtypes", new SelectArg('%' + subType + '%'));
clausesCount++;
}
if (!black || !blue || !green || !red || !white || !colorless) {
int colorClauses = 0;
if (black) {
where.eq("black", true);
colorClauses++;
}
if (blue) {
where.eq("blue", true);
colorClauses++;
}
if (green) {
where.eq("green", true);
colorClauses++;
}
if (red) {
where.eq("red", true);
colorClauses++;
}
if (white) {
where.eq("white", true);
colorClauses++;
}
if (colorless) {
where.eq("black", false).eq("blue", false).eq("green", false).eq("red", false).eq("white", false);
where.and(5);
colorClauses++;
}
where.or(colorClauses);
clausesCount++;
}
where.and(clausesCount);
if (start != null) {
qb.offset(start);
}
if (count != null) {
qb.limit(count);
}
qb.orderBy("cardNumber", true);
}
}

View file

@ -0,0 +1,220 @@
/*
* 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.repository;
import com.j256.ormlite.field.DataType;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import mage.Constants.CardType;
import mage.Constants.Rarity;
import mage.ObjectColor;
import mage.cards.Card;
import mage.cards.CardImpl;
/**
*
* @author North
*/
@DatabaseTable(tableName = "card")
public class CardInfo {
private static final String SEPARATOR = "@@@";
@DatabaseField
protected String name;
@DatabaseField
protected int cardNumber;
@DatabaseField
protected String setCode;
@DatabaseField
protected String className;
@DatabaseField
protected String power;
@DatabaseField
protected String toughness;
@DatabaseField
protected int convertedManaCost;
@DatabaseField(dataType = DataType.ENUM_STRING)
protected Rarity rarity;
@DatabaseField
protected String types;
@DatabaseField
protected String subtypes;
@DatabaseField
protected String supertypes;
@DatabaseField
protected String manaCosts;
@DatabaseField
protected String rules;
@DatabaseField
protected boolean black;
@DatabaseField
protected boolean blue;
@DatabaseField
protected boolean green;
@DatabaseField
protected boolean red;
@DatabaseField
protected boolean white;
@DatabaseField
protected boolean doubleFaced;
public CardInfo() {
}
public CardInfo(Card card) {
this.name = card.getName();
this.cardNumber = card.getCardNumber();
this.setCode = card.getExpansionSetCode();
this.className = card.getClass().getCanonicalName();
this.power = card.getPower().toString();
this.toughness = card.getToughness().toString();
this.convertedManaCost = card.getManaCost().convertedManaCost();
this.rarity = card.getRarity();
this.doubleFaced = card.canTransform();
this.blue = card.getColor().isBlue();
this.black = card.getColor().isBlack();
this.green = card.getColor().isGreen();
this.red = card.getColor().isRed();
this.white = card.getColor().isWhite();
this.setTypes(card.getCardType());
this.setSubtypes(card.getSubtype());
this.setSuperTypes(card.getSupertype());
this.setManaCosts(card.getManaCost().getSymbols());
this.setRules(card.getRules());
}
public Card getCard() {
return CardImpl.createCard(className);
}
public ObjectColor getColor() {
ObjectColor color = new ObjectColor();
color.setBlack(black);
color.setBlue(blue);
color.setGreen(green);
color.setRed(red);
color.setWhite(white);
return color;
}
private String joinList(List items) {
StringBuilder sb = new StringBuilder();
for (Object item : items) {
sb.append(item.toString()).append(SEPARATOR);
}
return sb.toString();
}
private List<String> parseList(String list) {
return Arrays.asList(list.split(SEPARATOR));
}
public final List<CardType> getTypes() {
ArrayList<CardType> list = new ArrayList<CardType>();
for (String type : this.types.split(SEPARATOR)) {
try {
list.add(CardType.valueOf(type));
} catch (Exception e) {
}
}
return list;
}
public final void setTypes(List<CardType> types) {
StringBuilder sb = new StringBuilder();
for (CardType item : types) {
sb.append(item.name()).append(SEPARATOR);
}
this.types = sb.toString();
}
public int getConvertedManaCost() {
return convertedManaCost;
}
public final List<String> getManaCosts() {
return parseList(manaCosts);
}
public final void setManaCosts(List<String> manaCosts) {
this.manaCosts = joinList(manaCosts);
}
public String getName() {
return name;
}
public String getPower() {
return power;
}
public Rarity getRarity() {
return rarity;
}
public final List<String> getRules() {
return parseList(rules);
}
public final void setRules(List<String> rules) {
this.rules = joinList(rules);
}
public final List<String> getSubTypes() {
return parseList(subtypes);
}
public final void setSubtypes(List<String> subtypes) {
this.subtypes = joinList(subtypes);
}
public final List<String> getSupertypes() {
return parseList(supertypes);
}
public final void setSuperTypes(List<String> superTypes) {
this.supertypes = joinList(superTypes);
}
public String getToughness() {
return toughness;
}
public String getSetCode() {
return setCode;
}
public String getClassName() {
return className;
}
}

View file

@ -0,0 +1,230 @@
/*
* 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.repository;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.jdbc.JdbcConnectionSource;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.SelectArg;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.Callable;
import mage.Constants.CardType;
/**
*
* @author North
*/
public enum CardRepository {
instance;
private static final String JDBC_URL = "jdbc:sqlite:db/cards.db";
private Random random = new Random();
private Dao<CardInfo, Object> cardDao;
private Set<String> classNames;
private CardRepository() {
File file = new File("db");
if (!file.exists()) {
file.mkdirs();
}
try {
ConnectionSource connectionSource = new JdbcConnectionSource(JDBC_URL);
TableUtils.createTableIfNotExists(connectionSource, CardInfo.class);
cardDao = DaoManager.createDao(connectionSource, CardInfo.class);
} catch (SQLException ex) {
}
}
public void addCards(final List<CardInfo> cards) {
try {
cardDao.callBatchTasks(new Callable<Object>() {
@Override
public Object call() throws Exception {
try {
for (CardInfo card : cards) {
cardDao.create(card);
if (classNames != null) {
classNames.add(card.getClassName());
}
}
} catch (SQLException ex) {
}
return null;
}
});
} catch (Exception ex) {
}
}
public boolean cardExists(String className) {
try {
if (classNames == null) {
QueryBuilder<CardInfo, Object> qb = cardDao.queryBuilder();
qb.distinct().selectColumns("className").where().isNotNull("className");
List<CardInfo> results = cardDao.query(qb.prepare());
classNames = new TreeSet<String>();
for (CardInfo card : results) {
classNames.add(card.getClassName());
}
}
return classNames.contains(className);
} catch (SQLException ex) {
}
return false;
}
public List<String> getSetCodes() {
List<String> setCodes = new ArrayList<String>();
try {
QueryBuilder<CardInfo, Object> qb = cardDao.queryBuilder();
qb.distinct().selectColumns("setCode");
List<CardInfo> results = cardDao.query(qb.prepare());
for (CardInfo card : results) {
setCodes.add(card.getSetCode());
}
} catch (SQLException ex) {
} finally {
return setCodes;
}
}
public Set<String> getNames() {
Set<String> names = new TreeSet<String>();
try {
QueryBuilder<CardInfo, Object> qb = cardDao.queryBuilder();
qb.distinct().selectColumns("name");
List<CardInfo> results = cardDao.query(qb.prepare());
for (CardInfo card : results) {
names.add(card.getName());
}
} catch (SQLException ex) {
} finally {
return names;
}
}
public Set<String> getNonLandNames() {
Set<String> names = new TreeSet<String>();
try {
QueryBuilder<CardInfo, Object> qb = cardDao.queryBuilder();
qb.distinct().selectColumns("name");
qb.where().not().like("types", new SelectArg('%' + CardType.LAND.name() + '%'));
List<CardInfo> results = cardDao.query(qb.prepare());
for (CardInfo card : results) {
names.add(card.getName());
}
} catch (SQLException ex) {
} finally {
return names;
}
}
public Set<String> getCreatureTypes() {
TreeSet<String> subtypes = new TreeSet<String>();
try {
QueryBuilder<CardInfo, Object> qb = cardDao.queryBuilder();
qb.distinct().selectColumns("subtypes");
qb.where().like("types", new SelectArg('%' + CardType.CREATURE.name() + '%'));
List<CardInfo> results = cardDao.query(qb.prepare());
for (CardInfo card : results) {
subtypes.addAll(card.getSubTypes());
}
} catch (SQLException ex) {
} finally {
return subtypes;
}
}
public CardInfo findCard(String setCode, int cardNumber) {
try {
QueryBuilder<CardInfo, Object> queryBuilder = cardDao.queryBuilder();
queryBuilder.where().eq("setCode", new SelectArg(setCode)).and().eq("cardNumber", cardNumber);
List<CardInfo> result = cardDao.query(queryBuilder.prepare());
if (!result.isEmpty()) {
return result.get(0);
}
} catch (SQLException ex) {
}
return null;
}
/**
*
* @param name
* @return random card with the provided name or null if none is found
*/
public CardInfo findCard(String name) {
List<CardInfo> cards = findCards(name);
if (!cards.isEmpty()) {
return cards.get(random.nextInt(cards.size()));
}
return null;
}
public List<CardInfo> findCards(String name) {
try {
QueryBuilder<CardInfo, Object> queryBuilder = cardDao.queryBuilder();
queryBuilder.where().eq("name", new SelectArg(name));
return cardDao.query(queryBuilder.prepare());
} catch (SQLException ex) {
}
return new ArrayList<CardInfo>();
}
public List<CardInfo> findCards(CardCriteria criteria) {
try {
QueryBuilder<CardInfo, Object> queryBuilder = cardDao.queryBuilder();
criteria.buildQuery(queryBuilder);
return cardDao.query(queryBuilder.prepare());
} catch (SQLException ex) {
}
return new ArrayList<CardInfo>();
}
public List<CardInfo> getAllCards() {
try {
QueryBuilder<CardInfo, Object> queryBuilder = cardDao.queryBuilder();
return cardDao.query(queryBuilder.prepare());
} catch (SQLException ex) {
}
return new ArrayList<CardInfo>();
}
}

View file

@ -0,0 +1,70 @@
/*
* 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.repository;
import java.util.ArrayList;
import java.util.List;
import mage.cards.Card;
import mage.cards.CardImpl;
import mage.cards.ExpansionSet;
import mage.cards.Sets;
import mage.util.ClassScanner;
/**
*
* @author North
*/
public class CardScanner {
private static boolean scanned = false;
public static void scan() {
if (scanned) {
return;
}
scanned = true;
List<CardInfo> cardsToAdd = new ArrayList<CardInfo>();
List<String> packages = new ArrayList<String>();
for (ExpansionSet set : Sets.getInstance().values()) {
packages.add(set.getPackageName());
}
for (Class c : ClassScanner.findClasses(packages, CardImpl.class)) {
if (!CardRepository.instance.cardExists(c.getCanonicalName())) {
Card card = CardImpl.createCard(c);
if (card != null && !card.isNightCard()) {
cardsToAdd.add(new CardInfo(card));
}
}
}
if (!cardsToAdd.isEmpty()) {
CardRepository.instance.addCards(cardsToAdd);
}
}
}

View file

@ -0,0 +1,144 @@
/*
* 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.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.TreeSet;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
/**
*
* @author North
*/
public class ClassScanner {
public static List<Class> findClasses(List<String> packages, Class<?> type) {
List<Class> cards = new ArrayList<Class>();
try {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
assert classLoader != null;
HashMap<String, String> dirs = new HashMap<String, String>();
TreeSet<String> jars = new TreeSet<String>();
for (String packageName : packages) {
String path = packageName.replace('.', '/');
Enumeration<URL> resources = classLoader.getResources(path);
while (resources.hasMoreElements()) {
URL resource = resources.nextElement();
String filePath = resource.getFile();
if (filePath.startsWith("file:")) {
filePath = filePath.substring("file:".length(), filePath.lastIndexOf("!"));
jars.add(filePath);
} else {
dirs.put(filePath, packageName);
}
}
}
for (String filePath : dirs.keySet()) {
cards.addAll(findClasses(new File(filePath), dirs.get(filePath), type));
}
for (String filePath : jars) {
File file = new File(URLDecoder.decode(filePath, "UTF-8"));
cards.addAll(findClassesInJar(file, packages, type));
}
} catch (IOException ex) {
}
return cards;
}
private static List<Class> findClasses(File directory, String packageName, Class<?> type) {
List<Class> cards = new ArrayList<Class>();
if (!directory.exists()) {
return cards;
}
for (File file : directory.listFiles()) {
if (file.getName().endsWith(".class")) {
try {
Class<?> clazz = Class.forName(packageName + '.' + file.getName().substring(0, file.getName().length() - 6));
if (type.isAssignableFrom(clazz)) {
cards.add(clazz);
}
} catch (ClassNotFoundException ex) {
}
}
}
return cards;
}
private static List<Class> findClassesInJar(File file, List<String> packages, Class<?> type) {
List<Class> cards = new ArrayList<Class>();
if (!file.exists()) {
return cards;
}
JarInputStream jarFile = null;
try {
jarFile = new JarInputStream(new FileInputStream(file));
while (true) {
JarEntry jarEntry = jarFile.getNextJarEntry();
if (jarEntry == null) {
break;
}
if (jarEntry.getName().endsWith(".class")) {
String className = jarEntry.getName().replace('/', '.').replace(".class", "");
int packageNameEnd = className.lastIndexOf('.');
String packageName = packageNameEnd != -1 ? className.substring(0, packageNameEnd) : "";
if (packages.contains(packageName)) {
Class<?> clazz;
try {
clazz = Class.forName(className);
if (type.isAssignableFrom(clazz)) {
cards.add(clazz);
}
} catch (ClassNotFoundException ex) {
}
}
}
}
} catch (IOException ex) {
} finally {
try {
jarFile.close();
} catch (IOException ex) {
}
}
return cards;
}
}