[CardRepository] rewritten booster generation

This commit is contained in:
North 2012-11-04 16:02:23 +02:00
parent 25cd490be3
commit 9ad2eff813
3 changed files with 73 additions and 285 deletions

View file

@ -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<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() {
return name;
}
@ -125,290 +105,68 @@ public abstract class ExpansionSet implements Serializable {
return name;
}
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 = CardImpl.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

@ -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<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;
@ -61,6 +64,7 @@ public class CardCriteria {
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>();
@ -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);
}

View file

@ -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();