Updated booster generation for ISD.

(Double-faced cards instead of "C" common).
This commit is contained in:
magenoxx 2011-10-12 12:23:48 +04:00
parent e949d046df
commit 28d887dff2
2 changed files with 378 additions and 313 deletions

View file

@ -34,26 +34,26 @@ import mage.cards.ExpansionSet;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
/** /**
*
* @author BetaSteward_at_googlemail.com * @author BetaSteward_at_googlemail.com
*/ */
public class Innistrad extends ExpansionSet { public class Innistrad extends ExpansionSet {
private static final Innistrad fINSTANCE = new Innistrad(); private static final Innistrad fINSTANCE = new Innistrad();
public static Innistrad getInstance() { public static Innistrad getInstance() {
return fINSTANCE; return fINSTANCE;
} }
private Innistrad() { private Innistrad() {
super("Innistrad", "ISD", "seticon_mtgisd", "mage.sets.innistrad", new GregorianCalendar(2011, 9, 30).getTime(), SetType.EXPANSION); super("Innistrad", "ISD", "seticon_mtgisd", "mage.sets.innistrad", new GregorianCalendar(2011, 9, 30).getTime(), SetType.EXPANSION);
this.blockName = "Innistrad"; this.blockName = "Innistrad";
this.hasBoosters = true; this.hasBoosters = true;
this.numBoosterLands = 1; this.numBoosterLands = 1;
this.numBoosterCommon = 10; this.numBoosterCommon = 9;
this.numBoosterUncommon = 3; this.numBoosterUncommon = 3;
this.numBoosterRare = 1; this.numBoosterRare = 1;
this.ratioBoosterMythic = 8; this.ratioBoosterMythic = 8;
} this.numBoosterDoubleFaced = 1;
}
} }

View file

@ -28,14 +28,11 @@
package mage.cards; package mage.cards;
import java.io.UnsupportedEncodingException;
import mage.Constants.Rarity; import mage.Constants.Rarity;
import mage.Constants.SetType; import mage.Constants.SetType;
import org.apache.log4j.Logger;
import java.io.File; import java.io.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
@ -43,337 +40,405 @@ import java.net.URLDecoder;
import java.util.*; import java.util.*;
import java.util.jar.JarEntry; import java.util.jar.JarEntry;
import java.util.jar.JarInputStream; import java.util.jar.JarInputStream;
import org.apache.log4j.Logger;
/** /**
* @author BetaSteward_at_googlemail.com * @author BetaSteward_at_googlemail.com
*/ */
public abstract class ExpansionSet implements Serializable { 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 name;
protected String code; protected String code;
protected String symbolCode; protected String symbolCode;
protected Date releaseDate; protected Date releaseDate;
protected ExpansionSet parentSet; protected ExpansionSet parentSet;
protected List<Card> cards; protected List<Card> cards;
protected SetType setType; protected SetType setType;
protected Map<Rarity, List<Card>> rarities; protected Map<Rarity, List<Card>> rarities;
protected String blockName; protected String blockName;
protected boolean hasBoosters = false; protected boolean hasBoosters = false;
protected int numBoosterLands; protected int numBoosterLands;
protected int numBoosterCommon; protected int numBoosterCommon;
protected int numBoosterUncommon; protected int numBoosterUncommon;
protected int numBoosterRare; protected int numBoosterRare;
protected int ratioBoosterMythic; protected int numBoosterDoubleFaced;
protected int ratioBoosterMythic;
public ExpansionSet(String name, String code, String symbolCode, String packageName, Date releaseDate, SetType setType) { public ExpansionSet(String name, String code, String symbolCode, String packageName, Date releaseDate, SetType setType) {
this.name = name; this.name = name;
this.code = code; this.code = code;
this.symbolCode = symbolCode; this.symbolCode = symbolCode;
this.releaseDate = releaseDate; this.releaseDate = releaseDate;
this.setType = setType; this.setType = setType;
this.cards = getCardClassesForPackage(packageName); this.cards = getCardClassesForPackage(packageName);
this.rarities = getCardsByRarity(); this.rarities = getCardsByRarity();
} }
public List<Card> getCards() { public List<Card> getCards() {
return cards; return cards;
} }
public String getName() { public String getName() {
return name; return name;
} }
public String getCode() { public String getCode() {
return code; return code;
} }
public String getSymbolCode() { public String getSymbolCode() {
return symbolCode; return symbolCode;
} }
public Date getReleaseDate() { public Date getReleaseDate() {
return releaseDate; return releaseDate;
} }
public SetType getSetType() { public SetType getSetType() {
return setType; return setType;
} }
private Card createCard(Class clazz) { private Card createCard(Class clazz) {
try { try {
Constructor<?> con = clazz.getConstructor(new Class[]{UUID.class}); Constructor<?> con = clazz.getConstructor(new Class[]{UUID.class});
return (Card) con.newInstance(new Object[]{null}); return (Card) con.newInstance(new Object[]{null});
} catch (Exception ex) { } catch (Exception ex) {
logger.fatal("Error creating card:" + clazz.getName(), ex); logger.fatal("Error creating card:" + clazz.getName(), ex);
return null; return null;
} }
} }
@Override @Override
public String toString() { public String toString() {
return name; return name;
} }
public Card findCard(String name) { public Card findCard(String name) {
for (Card card : cards) { for (Card card : cards) {
if (name.equalsIgnoreCase(card.getName())) { if (name.equalsIgnoreCase(card.getName())) {
Card newCard = card.copy(); Card newCard = card.copy();
newCard.assignNewId(); newCard.assignNewId();
return newCard; return newCard;
} }
} }
return null; return null;
} }
public Card findCard(int cardNum) { public Card findCard(int cardNum) {
for (Card card : cards) { for (Card card : cards) {
if (cardNum == card.getCardNumber()) { if (cardNum == card.getCardNumber()) {
Card newCard = card.copy(); Card newCard = card.copy();
newCard.assignNewId(); newCard.assignNewId();
return newCard; return newCard;
} }
} }
return null; return null;
} }
public Card findCard(String name, boolean random) { public Card findCard(String name, boolean random) {
List<Card> foundCards = new ArrayList<Card>(); List<Card> foundCards = new ArrayList<Card>();
for (Card card : cards) { for (Card card : cards) {
if (name.equalsIgnoreCase(card.getName())) { if (name.equalsIgnoreCase(card.getName())) {
foundCards.add(card); foundCards.add(card);
} }
} }
if (foundCards.size() > 0) { if (foundCards.size() > 0) {
Card newCard = foundCards.get(rnd.nextInt(foundCards.size())).copy(); Card newCard = foundCards.get(rnd.nextInt(foundCards.size())).copy();
newCard.assignNewId(); newCard.assignNewId();
return newCard; return newCard;
} }
return null; return null;
} }
public String findCardName(int cardNum) { public String findCardName(int cardNum) {
for (Card card : cards) { for (Card card : cards) {
if (card.getCardNumber() == cardNum) if (card.getCardNumber() == cardNum)
return card.getClass().getCanonicalName(); return card.getClass().getCanonicalName();
} }
return null; return null;
} }
private List<Card> getCardClassesForPackage(String packageName) { private List<Card> getCardClassesForPackage(String packageName) {
ClassLoader classLoader = this.getClass().getClassLoader(); ClassLoader classLoader = this.getClass().getClassLoader();
assert classLoader != null; assert classLoader != null;
String path = packageName.replace(".", "/"); String path = packageName.replace(".", "/");
Enumeration<URL> resources = null; Enumeration<URL> resources = null;
try { try {
resources = classLoader.getResources(path); resources = classLoader.getResources(path);
} catch (IOException e) { } catch (IOException e) {
logger.fatal("Error loading resource - " + path, e); logger.fatal("Error loading resource - " + path, e);
} }
List<File> dirs = new ArrayList<File>(); List<File> dirs = new ArrayList<File>();
boolean isLoadingFromJar = false; boolean isLoadingFromJar = false;
String jarPath = null; String jarPath = null;
while (resources.hasMoreElements()) { while (resources.hasMoreElements()) {
URL resource = resources.nextElement(); URL resource = resources.nextElement();
if (resource.toString().startsWith("jar:")) { if (resource.toString().startsWith("jar:")) {
isLoadingFromJar = true; isLoadingFromJar = true;
jarPath = resource.getFile(); jarPath = resource.getFile();
break; break;
} }
try { try {
dirs.add(new File(URLDecoder.decode(resource.getFile(), "UTF-8"))); dirs.add(new File(URLDecoder.decode(resource.getFile(), "UTF-8")));
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
logger.fatal("Error decoding director - " + resource.getFile(), e); logger.fatal("Error decoding director - " + resource.getFile(), e);
} }
} }
List<Class> classes = new ArrayList<Class>(); List<Class> classes = new ArrayList<Class>();
if (isLoadingFromJar) { if (isLoadingFromJar) {
if (jarPath.contains("!")) { if (jarPath.contains("!")) {
jarPath = jarPath.substring(0, jarPath.lastIndexOf('!')); jarPath = jarPath.substring(0, jarPath.lastIndexOf('!'));
} }
String filePathElement = "file:"; String filePathElement = "file:";
if (jarPath.startsWith(filePathElement)) { if (jarPath.startsWith(filePathElement)) {
try { try {
jarPath = URLDecoder.decode(jarPath.substring(jarPath.indexOf(filePathElement) + filePathElement.length()), "UTF-8"); jarPath = URLDecoder.decode(jarPath.substring(jarPath.indexOf(filePathElement) + filePathElement.length()), "UTF-8");
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
logger.fatal("Error decoding file - " + jarPath, e); logger.fatal("Error decoding file - " + jarPath, e);
} }
} }
try { try {
classes.addAll(findClassesInJar(new File(jarPath), path)); classes.addAll(findClassesInJar(new File(jarPath), path));
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
logger.fatal("Error loading classes - " + jarPath, e); logger.fatal("Error loading classes - " + jarPath, e);
} }
} else { // faster but doesn't work for jars } else { // faster but doesn't work for jars
for (File directory : dirs) { for (File directory : dirs) {
try { try {
classes.addAll(findClasses(directory, packageName)); classes.addAll(findClasses(directory, packageName));
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
logger.fatal("Error loading classes - " + jarPath, e); logger.fatal("Error loading classes - " + jarPath, e);
} }
} }
} }
List<Card> newCards = new ArrayList<Card>(); List<Card> newCards = new ArrayList<Card>();
for (Class clazz : classes) { for (Class clazz : classes) {
if (clazz.getPackage().getName().equals(packageName)) { if (clazz.getPackage().getName().equals(packageName)) {
Card card = createCard(clazz); Card card = createCard(clazz);
if (card.isNightCard()) { if (card.isNightCard()) {
// skip second face of double-faced cards // skip second face of double-faced cards
continue; continue;
} }
newCards.add(card); newCards.add(card);
} }
} }
return newCards; return newCards;
} }
private static List<Class> findClasses(File directory, String packageName) throws ClassNotFoundException { private static List<Class> findClasses(File directory, String packageName) throws ClassNotFoundException {
List<Class> classes = new ArrayList<Class>(); List<Class> classes = new ArrayList<Class>();
if (!directory.exists()) { if (!directory.exists()) {
return classes; return classes;
} }
File[] files = directory.listFiles(); File[] files = directory.listFiles();
if (files == null) { if (files == null) {
return classes; return classes;
} }
for (File file : files) { for (File file : files) {
if (file.isDirectory()) { if (file.isDirectory()) {
assert !file.getName().contains("."); assert !file.getName().contains(".");
classes.addAll(findClasses(file, packageName + "." + file.getName())); classes.addAll(findClasses(file, packageName + "." + file.getName()));
} else if (file.getName().endsWith(".class")) { } else if (file.getName().endsWith(".class")) {
Class c = Class.forName(packageName + '.' + file.getName().substring(0, file.getName().length() - 6)); Class c = Class.forName(packageName + '.' + file.getName().substring(0, file.getName().length() - 6));
if (CardImpl.class.isAssignableFrom(c)) { if (CardImpl.class.isAssignableFrom(c)) {
classes.add(c); classes.add(c);
} }
} }
} }
return classes; return classes;
} }
private static List<Class> findClassesInJar(File file, String packageName) throws ClassNotFoundException { private static List<Class> findClassesInJar(File file, String packageName) throws ClassNotFoundException {
List<Class> classes = new ArrayList<Class>(); List<Class> classes = new ArrayList<Class>();
if (!file.exists()) { if (!file.exists()) {
return classes; return classes;
} }
try { try {
URL url = file.toURL(); URL url = file.toURL();
URL[] urls = new URL[]{url}; URL[] urls = new URL[]{url};
ClassLoader cl = new URLClassLoader(urls); ClassLoader cl = new URLClassLoader(urls);
JarInputStream jarFile = new JarInputStream(new FileInputStream(file)); JarInputStream jarFile = new JarInputStream(new FileInputStream(file));
JarEntry jarEntry; JarEntry jarEntry;
while (true) { while (true) {
jarEntry = jarFile.getNextJarEntry(); jarEntry = jarFile.getNextJarEntry();
if (jarEntry == null) { if (jarEntry == null) {
break; break;
} }
if ((jarEntry.getName().startsWith(packageName)) && (jarEntry.getName().endsWith(".class"))) { if ((jarEntry.getName().startsWith(packageName)) && (jarEntry.getName().endsWith(".class"))) {
String clazz = jarEntry.getName().replaceAll("/", "\\.").replace(".class", ""); String clazz = jarEntry.getName().replaceAll("/", "\\.").replace(".class", "");
Class c = cl.loadClass(clazz); Class c = cl.loadClass(clazz);
if (CardImpl.class.isAssignableFrom(c)) { if (CardImpl.class.isAssignableFrom(c)) {
classes.add(c); classes.add(c);
} }
} }
} }
} catch (Exception e) { } catch (Exception e) {
logger.fatal("Error loading classes - " + file, e); logger.fatal("Error loading classes - " + file, e);
} }
return classes; return classes;
} }
private Map<Rarity, List<Card>> getCardsByRarity() { private Map<Rarity, List<Card>> getCardsByRarity() {
Map<Rarity, List<Card>> cardsByRarity = new HashMap<Rarity, List<Card>>(); Map<Rarity, List<Card>> cardsByRarity = new HashMap<Rarity, List<Card>>();
for (Card card : cards) { for (Card card : cards) {
if (!cardsByRarity.containsKey(card.getRarity())) if (!cardsByRarity.containsKey(card.getRarity()))
cardsByRarity.put(card.getRarity(), new ArrayList<Card>()); cardsByRarity.put(card.getRarity(), new ArrayList<Card>());
cardsByRarity.get(card.getRarity()).add(card); cardsByRarity.get(card.getRarity()).add(card);
} }
return cardsByRarity; return cardsByRarity;
} }
public List<Card> createBooster() { public List<Card> createBooster() {
List<Card> booster = new ArrayList<Card>(); List<Card> booster = new ArrayList<Card>();
if (!hasBoosters) if (!hasBoosters)
return booster; return booster;
if (parentSet != null) { if (parentSet != null) {
for (int i = 0; i < numBoosterLands; i++) { for (int i = 0; i < numBoosterLands; i++) {
addToBooster(booster, parentSet, Rarity.LAND); addToBooster(booster, parentSet, Rarity.LAND);
} }
} else { } else {
for (int i = 0; i < numBoosterLands; i++) { for (int i = 0; i < numBoosterLands; i++) {
addToBooster(booster, this, Rarity.LAND); addToBooster(booster, this, Rarity.LAND);
} }
} }
for (int i = 0; i < numBoosterCommon; i++) { for (int i = 0; i < numBoosterCommon; i++) {
addToBooster(booster, this, Rarity.COMMON); addToBooster(booster, this, Rarity.COMMON);
} }
for (int i = 0; i < numBoosterUncommon; i++) { for (int i = 0; i < numBoosterUncommon; i++) {
addToBooster(booster, this, Rarity.UNCOMMON); addToBooster(booster, this, Rarity.UNCOMMON);
} }
for (int i = 0; i < numBoosterRare; i++) { for (int i = 0; i < numBoosterRare; i++) {
if (ratioBoosterMythic > 0 && rnd.nextInt(ratioBoosterMythic) == 1) { if (ratioBoosterMythic > 0 && rnd.nextInt(ratioBoosterMythic) == 1) {
addToBooster(booster, this, Rarity.MYTHIC); addToBooster(booster, this, Rarity.MYTHIC);
} else { } else {
addToBooster(booster, this, Rarity.RARE); addToBooster(booster, this, Rarity.RARE);
} }
} }
for (int i = 0; i < numBoosterDoubleFaced; i++) {
addToBoosterDoubleFaced(booster, this);
}
return booster; return booster;
} }
protected void addToBooster(List<Card> booster, ExpansionSet set, Rarity rarity) { protected void addToBooster(List<Card> booster, ExpansionSet set, Rarity rarity) {
Card card = set.getRandom(rarity); Card card = set.getRandom(rarity);
if (card != null) { if (card != null) {
boolean duplicate = true; card = checkNotDoubleFaced(card, set, rarity);
int retryCount = 5; card = checkNotDuplicate(card, booster, set, rarity);
while (duplicate && retryCount > 0) { Card newCard = card.copy();
if (!rarity.equals(Rarity.LAND)) { newCard.assignNewId();
// check for duplicates booster.add(newCard);
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 boolean hasCardByName(List<Card> booster, String name) { protected void addToBoosterDoubleFaced(List<Card> booster, ExpansionSet set) {
for (Card card : booster) { Card card = set.getRandomDoubleFaced();
if (card.getName().equals(name)) { if (card != null) {
return true; Card newCard = card.copy();
} newCard.assignNewId();
} booster.add(newCard);
return false; }
} }
protected Card getRandom(Rarity rarity) { /**
if (!rarities.containsKey(rarity)) * Checks that card doesn't already exist in the booster. If so, tries to generate new one several times.
return null; *
int size = rarities.get(rarity).size(); * @param cardToCheck
if (size > 0) { * @param booster
return rarities.get(rarity).get(rnd.nextInt(size)).copy(); * @param set
} * @param rarity
return null; * @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 (!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;
}
} }