From 28d887dff2619548f2083f91c5882b55e38181f2 Mon Sep 17 00:00:00 2001 From: magenoxx Date: Wed, 12 Oct 2011 12:23:48 +0400 Subject: [PATCH] Updated booster generation for ISD. (Double-faced cards instead of "C" common). --- Mage.Sets/src/mage/sets/Innistrad.java | 30 +- Mage/src/mage/cards/ExpansionSet.java | 661 ++++++++++++++----------- 2 files changed, 378 insertions(+), 313 deletions(-) diff --git a/Mage.Sets/src/mage/sets/Innistrad.java b/Mage.Sets/src/mage/sets/Innistrad.java index 2af14b4b54..e1e15d9510 100644 --- a/Mage.Sets/src/mage/sets/Innistrad.java +++ b/Mage.Sets/src/mage/sets/Innistrad.java @@ -34,26 +34,26 @@ import mage.cards.ExpansionSet; import java.util.GregorianCalendar; /** - * * @author BetaSteward_at_googlemail.com */ public class Innistrad extends ExpansionSet { - private static final Innistrad fINSTANCE = new Innistrad(); + private static final Innistrad fINSTANCE = new Innistrad(); - public static Innistrad getInstance() { - return fINSTANCE; - } + public static Innistrad getInstance() { + return fINSTANCE; + } - private Innistrad() { - super("Innistrad", "ISD", "seticon_mtgisd", "mage.sets.innistrad", new GregorianCalendar(2011, 9, 30).getTime(), SetType.EXPANSION); - this.blockName = "Innistrad"; - this.hasBoosters = true; - this.numBoosterLands = 1; - this.numBoosterCommon = 10; - this.numBoosterUncommon = 3; - this.numBoosterRare = 1; - this.ratioBoosterMythic = 8; - } + private Innistrad() { + super("Innistrad", "ISD", "seticon_mtgisd", "mage.sets.innistrad", new GregorianCalendar(2011, 9, 30).getTime(), SetType.EXPANSION); + this.blockName = "Innistrad"; + this.hasBoosters = true; + this.numBoosterLands = 1; + this.numBoosterCommon = 9; + this.numBoosterUncommon = 3; + this.numBoosterRare = 1; + this.ratioBoosterMythic = 8; + this.numBoosterDoubleFaced = 1; + } } diff --git a/Mage/src/mage/cards/ExpansionSet.java b/Mage/src/mage/cards/ExpansionSet.java index f305905e54..055f83760e 100644 --- a/Mage/src/mage/cards/ExpansionSet.java +++ b/Mage/src/mage/cards/ExpansionSet.java @@ -28,14 +28,11 @@ package mage.cards; -import java.io.UnsupportedEncodingException; import mage.Constants.Rarity; import mage.Constants.SetType; +import org.apache.log4j.Logger; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.Serializable; +import java.io.*; import java.lang.reflect.Constructor; import java.net.URL; import java.net.URLClassLoader; @@ -43,337 +40,405 @@ import java.net.URLDecoder; import java.util.*; import java.util.jar.JarEntry; import java.util.jar.JarInputStream; -import org.apache.log4j.Logger; /** * @author BetaSteward_at_googlemail.com */ public abstract class ExpansionSet implements Serializable { - private final static Logger logger = Logger.getLogger(ExpansionSet.class); + private final static Logger logger = Logger.getLogger(ExpansionSet.class); - protected static Random rnd = new Random(); + protected static Random rnd = new Random(); - protected String name; - protected String code; - protected String symbolCode; - protected Date releaseDate; - protected ExpansionSet parentSet; - protected List cards; - protected SetType setType; - protected Map> rarities; + protected String name; + protected String code; + protected String symbolCode; + protected Date releaseDate; + protected ExpansionSet parentSet; + protected List cards; + protected SetType setType; + protected Map> rarities; - protected String blockName; - protected boolean hasBoosters = false; - protected int numBoosterLands; - protected int numBoosterCommon; - protected int numBoosterUncommon; - protected int numBoosterRare; - protected int ratioBoosterMythic; + protected String blockName; + protected boolean hasBoosters = false; + protected int numBoosterLands; + protected int numBoosterCommon; + protected int numBoosterUncommon; + protected int numBoosterRare; + protected int numBoosterDoubleFaced; + protected int ratioBoosterMythic; - public ExpansionSet(String name, String code, String symbolCode, String packageName, Date releaseDate, SetType setType) { - this.name = name; - this.code = code; - this.symbolCode = symbolCode; - this.releaseDate = releaseDate; - this.setType = setType; - this.cards = getCardClassesForPackage(packageName); - this.rarities = getCardsByRarity(); - } + public ExpansionSet(String name, String code, String symbolCode, String packageName, Date releaseDate, SetType setType) { + this.name = name; + this.code = code; + this.symbolCode = symbolCode; + this.releaseDate = releaseDate; + this.setType = setType; + this.cards = getCardClassesForPackage(packageName); + this.rarities = getCardsByRarity(); + } - public List getCards() { - return cards; - } + public List getCards() { + return cards; + } - public String getName() { - return name; - } + public String getName() { + return name; + } - public String getCode() { - return code; - } + public String getCode() { + return code; + } - public String getSymbolCode() { - return symbolCode; - } + public String getSymbolCode() { + return symbolCode; + } - public Date getReleaseDate() { - return releaseDate; - } + public Date getReleaseDate() { + return releaseDate; + } - public SetType getSetType() { - return setType; - } + public SetType getSetType() { + return setType; + } - private Card createCard(Class clazz) { - try { - Constructor con = clazz.getConstructor(new Class[]{UUID.class}); - return (Card) con.newInstance(new Object[]{null}); - } catch (Exception ex) { - logger.fatal("Error creating card:" + clazz.getName(), ex); - return null; - } - } + private Card createCard(Class clazz) { + try { + Constructor con = clazz.getConstructor(new Class[]{UUID.class}); + return (Card) con.newInstance(new Object[]{null}); + } catch (Exception ex) { + logger.fatal("Error creating card:" + clazz.getName(), ex); + return null; + } + } - @Override - public String toString() { - return name; - } + @Override + public String toString() { + return name; + } - public Card findCard(String name) { - for (Card card : cards) { - if (name.equalsIgnoreCase(card.getName())) { - Card newCard = card.copy(); - newCard.assignNewId(); - return newCard; - } - } - return null; - } + public Card findCard(String name) { + for (Card card : cards) { + if (name.equalsIgnoreCase(card.getName())) { + Card newCard = card.copy(); + newCard.assignNewId(); + return newCard; + } + } + return null; + } - public Card findCard(int cardNum) { - for (Card card : cards) { - if (cardNum == card.getCardNumber()) { - Card newCard = card.copy(); - newCard.assignNewId(); - return newCard; - } - } - return null; - } + public Card findCard(int cardNum) { + for (Card card : cards) { + if (cardNum == card.getCardNumber()) { + Card newCard = card.copy(); + newCard.assignNewId(); + return newCard; + } + } + return null; + } - public Card findCard(String name, boolean random) { - List foundCards = new ArrayList(); - for (Card card : cards) { - 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 Card findCard(String name, boolean random) { + List foundCards = new ArrayList(); + for (Card card : cards) { + 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 : cards) { - if (card.getCardNumber() == cardNum) - return card.getClass().getCanonicalName(); - } - return null; - } + public String findCardName(int cardNum) { + for (Card card : cards) { + if (card.getCardNumber() == cardNum) + return card.getClass().getCanonicalName(); + } + return null; + } - 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 = createCard(clazz); - if (card.isNightCard()) { - // skip second face of double-faced cards - continue; - } - newCards.add(card); - } - } - return newCards; - } + 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 = 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 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(); + private static List findClassesInJar(File file, String packageName) throws ClassNotFoundException { + List classes = new ArrayList(); - if (!file.exists()) { - return classes; - } + if (!file.exists()) { + return classes; + } - try { - URL url = file.toURL(); - URL[] urls = new URL[]{url}; - ClassLoader cl = new URLClassLoader(urls); + try { + URL url = file.toURL(); + URL[] urls = new URL[]{url}; + ClassLoader cl = new URLClassLoader(urls); - JarInputStream jarFile = new JarInputStream(new FileInputStream(file)); - JarEntry jarEntry; + 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); - } + 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; - } + return classes; + } - private Map> getCardsByRarity() { - Map> cardsByRarity = new HashMap>(); + private Map> getCardsByRarity() { + Map> cardsByRarity = new HashMap>(); - for (Card card : cards) { - if (!cardsByRarity.containsKey(card.getRarity())) - cardsByRarity.put(card.getRarity(), new ArrayList()); - cardsByRarity.get(card.getRarity()).add(card); - } + for (Card card : cards) { + if (!cardsByRarity.containsKey(card.getRarity())) + cardsByRarity.put(card.getRarity(), new ArrayList()); + cardsByRarity.get(card.getRarity()).add(card); + } - return cardsByRarity; - } + return cardsByRarity; + } - public List createBooster() { - List booster = new ArrayList(); + public List createBooster() { + List booster = new ArrayList(); - if (!hasBoosters) - return booster; + 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); - } - } - for (int i = 0; i < numBoosterCommon; i++) { - addToBooster(booster, this, Rarity.COMMON); - } - for (int i = 0; i < numBoosterUncommon; i++) { - addToBooster(booster, this, Rarity.UNCOMMON); - } - for (int i = 0; i < numBoosterRare; i++) { - if (ratioBoosterMythic > 0 && rnd.nextInt(ratioBoosterMythic) == 1) { - addToBooster(booster, this, Rarity.MYTHIC); - } else { - addToBooster(booster, this, Rarity.RARE); - } - } + 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); + } + } + for (int i = 0; i < numBoosterCommon; i++) { + addToBooster(booster, this, Rarity.COMMON); + } + for (int i = 0; i < numBoosterUncommon; i++) { + addToBooster(booster, this, Rarity.UNCOMMON); + } + for (int i = 0; i < numBoosterRare; i++) { + if (ratioBoosterMythic > 0 && rnd.nextInt(ratioBoosterMythic) == 1) { + addToBooster(booster, this, Rarity.MYTHIC); + } else { + addToBooster(booster, this, Rarity.RARE); + } + } + for (int i = 0; i < numBoosterDoubleFaced; i++) { + addToBoosterDoubleFaced(booster, this); + } - return booster; - } + return booster; + } - protected void addToBooster(List booster, ExpansionSet set, Rarity rarity) { - Card card = set.getRandom(rarity); - if (card != null) { - 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--; - } - Card newCard = card.copy(); - newCard.assignNewId(); - booster.add(newCard); - } - } + 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 boolean hasCardByName(List booster, String name) { - for (Card card : booster) { - if (card.getName().equals(name)) { - return true; - } - } - return false; - } + protected void addToBoosterDoubleFaced(List booster, ExpansionSet set) { + Card card = set.getRandomDoubleFaced(); + if (card != null) { + Card newCard = card.copy(); + newCard.assignNewId(); + booster.add(newCard); + } + } - protected Card getRandom(Rarity rarity) { - if (!rarities.containsKey(rarity)) - return null; - int size = rarities.get(rarity).size(); - if (size > 0) { - return rarities.get(rarity).get(rnd.nextInt(size)).copy(); - } - return null; - } + /** + * 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 (!rarities.containsKey(rarity)) + return null; + int size = rarities.get(rarity).size(); + if (size > 0) { + return rarities.get(rarity).get(rnd.nextInt(size)).copy(); + } + return null; + } + + protected Card getRandomDoubleFaced() { + int size = cards.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; + } + } + return card; + } + return null; + } }