Lazy card loading

This commit is contained in:
magenoxx 2012-06-05 19:41:17 +04:00
parent b04c8e485b
commit d895b3e8d0
3 changed files with 55 additions and 17 deletions

View file

@ -121,6 +121,8 @@ public class MageFrame extends javax.swing.JFrame implements MageClient {
private static ScheduledExecutorService pingTaskExecutor = Executors.newSingleThreadScheduledExecutor();
private static long startTime;
/**
* @return the session
*/
@ -300,6 +302,7 @@ public class MageFrame extends javax.swing.JFrame implements MageClient {
disableButtons();
if (PreferencesDialog.getCachedValue(PreferencesDialog.KEY_CARD_IMAGES_CHECK, "true").equals("true"))
checkForNewImages();
logger.info("Client start up time: " + ((System.currentTimeMillis() - startTime) / 1000 + " ms"));
if (autoConnect())
enableButtons();
else {
@ -930,6 +933,8 @@ public class MageFrame extends javax.swing.JFrame implements MageClient {
*/
public static void main(final String args[]) {
startTime = System.currentTimeMillis();
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
public void uncaughtException(Thread t, Throwable e) {
logger.fatal(null, e);

View file

@ -120,31 +120,53 @@ public class Sets extends HashMap<String, ExpansionSet> {
private void addSet(ExpansionSet set) {
this.put(set.getCode(), set);
cards.addAll(set.getCards());
for (Card card: set.getCards()) {
names.add(card.getName());
if (card.getCardType().contains(CardType.CREATURE)) {
for (String type : card.getSubtype()) {
creatureTypes.add(type);
}
}
if (!card.getCardType().contains(CardType.LAND)) nonLandNames.add(card.getName());
}
}
private static void loadCards() {
if (cards.isEmpty()) {
System.out.println("Loading cards...");
long t1 = System.currentTimeMillis();
for (ExpansionSet set : getInstance().values()) {
cards.addAll(set.getCards());
for (Card card : set.getCards()) {
names.add(card.getName());
if (card.getCardType().contains(CardType.CREATURE)) {
for (String type : card.getSubtype()) {
creatureTypes.add(type);
}
}
if (!card.getCardType().contains(CardType.LAND)) nonLandNames.add(card.getName());
}
}
System.out.println("It took " + (System.currentTimeMillis() - t1) / 1000 + " ms to load all cards.");
}
}
public static Set<String> getCardNames() {
if (names.isEmpty()) {
loadCards();
}
return names;
}
public static Set<String> getNonLandCardNames() {
if (nonLandNames.isEmpty()) {
loadCards();
}
return nonLandNames;
}
public static Set<String> getCreatureTypes() {
if (creatureTypes.isEmpty()) {
loadCards();
}
return creatureTypes;
}
public static Card getRandomCard() {
if (cards.isEmpty()) {
loadCards();
}
return cards.get(rnd.nextInt(cards.size()));
}

View file

@ -68,17 +68,28 @@ public abstract class ExpansionSet implements Serializable {
protected int numBoosterDoubleFaced;
protected int ratioBoosterMythic;
protected String packageName;
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();
this.packageName = packageName;
//this.cards = getCardClassesForPackage(packageName);
//this.rarities = getCardsByRarity();
}
public List<Card> getCards() {
if (cards == null) {
synchronized (this) {
if (cards == null) {
this.cards = getCardClassesForPackage(packageName);
this.rarities = getCardsByRarity();
}
}
}
return cards;
}
@ -118,7 +129,7 @@ public abstract class ExpansionSet implements Serializable {
}
public Card findCard(String name) {
for (Card card : cards) {
for (Card card : getCards()) {
if (name.equalsIgnoreCase(card.getName())) {
Card newCard = card.copy();
newCard.assignNewId();
@ -129,7 +140,7 @@ public abstract class ExpansionSet implements Serializable {
}
public Card findCard(int cardNum) {
for (Card card : cards) {
for (Card card : getCards()) {
if (cardNum == card.getCardNumber()) {
Card newCard = card.copy();
newCard.assignNewId();
@ -141,7 +152,7 @@ public abstract class ExpansionSet implements Serializable {
public Card findCard(String name, boolean random) {
List<Card> foundCards = new ArrayList<Card>();
for (Card card : cards) {
for (Card card : getCards()) {
if (name.equalsIgnoreCase(card.getName())) {
foundCards.add(card);
}
@ -155,7 +166,7 @@ public abstract class ExpansionSet implements Serializable {
}
public String findCardName(int cardNum) {
for (Card card : cards) {
for (Card card : getCards()) {
if (card.getCardNumber() == cardNum)
return card.getClass().getCanonicalName();
}
@ -425,7 +436,7 @@ public abstract class ExpansionSet implements Serializable {
}
protected Card getRandomDoubleFaced() {
int size = cards.size();
int size = getCards().size();
if (size > 0) {
Card card = cards.get(rnd.nextInt(size));
int retryCount = 1000;