diff --git a/Mage/src/mage/cards/ExpansionSet.java b/Mage/src/mage/cards/ExpansionSet.java index da4f6dcccc..1a56cdc682 100644 --- a/Mage/src/mage/cards/ExpansionSet.java +++ b/Mage/src/mage/cards/ExpansionSet.java @@ -30,15 +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.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 @@ -78,24 +76,6 @@ public abstract class ExpansionSet implements Serializable { this.packageName = packageName; } - public List 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() { return name; } @@ -125,290 +105,68 @@ public abstract class ExpansionSet implements Serializable { return name; } - private List getCardClassesForPackage(String packageName) { - ClassLoader classLoader = this.getClass().getClassLoader(); - assert classLoader != null; - String path = packageName.replace(".", "/"); - Enumeration resources = null; - try { - resources = classLoader.getResources(path); - } catch (IOException e) { - logger.fatal("Error loading resource - " + path, e); - } - List dirs = new ArrayList(); - 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 classes = new ArrayList(); - 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 newCards = new ArrayList(); - for (Class clazz : classes) { - if (clazz.getPackage().getName().equals(packageName)) { - Card card = CardImpl.createCard(clazz); - if (card.isNightCard()) { - // skip second face of double-faced cards - continue; - } - newCards.add(card); - } - } - return newCards; - } - - private static List findClasses(File directory, String packageName) throws ClassNotFoundException { - List classes = new ArrayList(); - 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 findClassesInJar(File file, String packageName) throws ClassNotFoundException { - List classes = new ArrayList(); - - 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> getCardsByRarity() { - Map> cardsByRarity = new HashMap>(); - - for (Card card : getCards()) { - if (!cardsByRarity.containsKey(card.getRarity())) - cardsByRarity.put(card.getRarity(), new ArrayList()); - cardsByRarity.get(card.getRarity()).add(card); - } - - return cardsByRarity; - } - public List createBooster() { List booster = new ArrayList(); - - 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 basicLand = CardRepository.instance.findCards(criteria); + + criteria = new CardCriteria(); + criteria.setCodes(this.code).rarities(Rarity.COMMON).doubleFaced(false); + List common = CardRepository.instance.findCards(criteria); + + criteria = new CardCriteria(); + criteria.setCodes(this.code).rarities(Rarity.UNCOMMON).doubleFaced(false); + List uncommon = CardRepository.instance.findCards(criteria); + + criteria = new CardCriteria(); + criteria.setCodes(this.code).rarities(Rarity.RARE).doubleFaced(false); + List rare = CardRepository.instance.findCards(criteria); + + criteria = new CardCriteria(); + criteria.setCodes(this.code).rarities(Rarity.MYTHIC).doubleFaced(false); + List mythic = CardRepository.instance.findCards(criteria); + + criteria = new CardCriteria(); + criteria.setCodes(this.code).doubleFaced(true); + List 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 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 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 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 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 booster, List 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> getRarities() { - if (rarities == null) { - loadLazily(); - } - return rarities; } } diff --git a/Mage/src/mage/cards/repository/CardCriteria.java b/Mage/src/mage/cards/repository/CardCriteria.java index 99f7790c6b..11d2d2ac4d 100644 --- a/Mage/src/mage/cards/repository/CardCriteria.java +++ b/Mage/src/mage/cards/repository/CardCriteria.java @@ -35,6 +35,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; import mage.Constants.CardType; +import mage.Constants.Rarity; /** * @@ -50,6 +51,8 @@ public class CardCriteria { private List supertypes; private List notSupertypes; private List subtypes; + private List rarities; + private Boolean doubleFaced; private boolean black; private boolean blue; private boolean green; @@ -61,6 +64,7 @@ public class CardCriteria { public CardCriteria() { this.setCodes = new ArrayList(); + this.rarities = new ArrayList(); this.types = new ArrayList(); this.notTypes = new ArrayList(); this.supertypes = new ArrayList(); @@ -105,6 +109,11 @@ public class CardCriteria { return this; } + public CardCriteria doubleFaced(boolean doubleFaced) { + this.doubleFaced = doubleFaced; + return this; + } + public CardCriteria name(String name) { this.name = name; return this; @@ -125,6 +134,11 @@ public class CardCriteria { 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; @@ -167,6 +181,19 @@ public class CardCriteria { 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); } diff --git a/Mage/src/mage/cards/repository/CardInfo.java b/Mage/src/mage/cards/repository/CardInfo.java index bbea5e2811..b11a698093 100644 --- a/Mage/src/mage/cards/repository/CardInfo.java +++ b/Mage/src/mage/cards/repository/CardInfo.java @@ -83,6 +83,8 @@ public class CardInfo { protected boolean red; @DatabaseField protected boolean white; + @DatabaseField + protected boolean doubleFaced; public CardInfo() { } @@ -96,6 +98,7 @@ public class CardInfo { 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();