Fixing limited: removed card lazy loading

This commit is contained in:
magenoxx 2012-06-23 09:41:32 +04:00
parent a186cc5c33
commit 6b6f718bc4
2 changed files with 39 additions and 16 deletions

View file

@ -120,9 +120,25 @@ public class Sets extends HashMap<String, ExpansionSet> {
private void addSet(ExpansionSet set) { private void addSet(ExpansionSet set) {
this.put(set.getCode(), 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 (type.equals("")) {
throw new IllegalStateException("Card with empty subtype: " + card.getName());
}
}
}
if (!card.getCardType().contains(CardType.LAND)) nonLandNames.add(card.getName());
}
if (creatureTypes.contains("")) {
creatureTypes.remove("");
}
} }
private static void loadCards() { /*private static void loadCards() {
if (cards.isEmpty()) { if (cards.isEmpty()) {
System.out.println("Loading cards..."); System.out.println("Loading cards...");
long t1 = System.currentTimeMillis(); long t1 = System.currentTimeMillis();
@ -146,33 +162,33 @@ public class Sets extends HashMap<String, ExpansionSet> {
} }
System.out.println("It took " + (System.currentTimeMillis() - t1) / 1000 + " ms to load all cards."); System.out.println("It took " + (System.currentTimeMillis() - t1) / 1000 + " ms to load all cards.");
} }
} }*/
public static Set<String> getCardNames() { public static Set<String> getCardNames() {
if (names.isEmpty()) { /*if (names.isEmpty()) {
loadCards(); loadCards();
} }*/
return names; return names;
} }
public static Set<String> getNonLandCardNames() { public static Set<String> getNonLandCardNames() {
if (nonLandNames.isEmpty()) { /*if (nonLandNames.isEmpty()) {
loadCards(); loadCards();
} }*/
return nonLandNames; return nonLandNames;
} }
public static Set<String> getCreatureTypes() { public static Set<String> getCreatureTypes() {
if (creatureTypes.isEmpty()) { /*if (creatureTypes.isEmpty()) {
loadCards(); loadCards();
} }*/
return creatureTypes; return creatureTypes;
} }
public static Card getRandomCard() { public static Card getRandomCard() {
if (cards.isEmpty()) { /*if (cards.isEmpty()) {
loadCards(); loadCards();
} }*/
return cards.get(rnd.nextInt(cards.size())); return cards.get(rnd.nextInt(cards.size()));
} }

View file

@ -77,19 +77,19 @@ public abstract class ExpansionSet implements Serializable {
this.releaseDate = releaseDate; this.releaseDate = releaseDate;
this.setType = setType; this.setType = setType;
this.packageName = packageName; this.packageName = packageName;
//this.cards = getCardClassesForPackage(packageName); this.cards = getCardClassesForPackage(packageName);
//this.rarities = getCardsByRarity(); this.rarities = getCardsByRarity();
} }
public List<Card> getCards() { public List<Card> getCards() {
if (cards == null) { /*if (cards == null) {
synchronized (this) { synchronized (this) {
if (cards == null) { if (cards == null) {
this.cards = getCardClassesForPackage(packageName); this.cards = getCardClassesForPackage(packageName);
this.rarities = getCardsByRarity(); this.rarities = getCardsByRarity();
} }
} }
} }*/
return cards; return cards;
} }
@ -301,7 +301,7 @@ public abstract class ExpansionSet implements Serializable {
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 : getCards()) {
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);
@ -426,7 +426,7 @@ public abstract class ExpansionSet implements Serializable {
} }
protected Card getRandom(Rarity rarity) { protected Card getRandom(Rarity rarity) {
if (!rarities.containsKey(rarity)) if (rarities.containsKey(rarity))
return null; return null;
int size = rarities.get(rarity).size(); int size = rarities.get(rarity).size();
if (size > 0) { if (size > 0) {
@ -452,4 +452,11 @@ public abstract class ExpansionSet implements Serializable {
} }
return null; return null;
} }
public Map<Rarity, List<Card>> getRarities() {
/*if (rarities == null) {
this.rarities = getCardsByRarity();
}*/
return rarities;
}
} }