forked from External/mage
Refactored card cache;
This commit is contained in:
parent
96daa4e0a9
commit
5fa99262bf
4 changed files with 132 additions and 123 deletions
|
|
@ -31,8 +31,7 @@ package mage.sets;
|
|||
import mage.Constants.CardType;
|
||||
import mage.Constants.ColoredManaSymbol;
|
||||
import mage.Mana;
|
||||
import mage.cache.Cache;
|
||||
import mage.cache.CacheDataHelper;
|
||||
import mage.cache.CacheService;
|
||||
import mage.cards.Card;
|
||||
import mage.cards.CardImpl;
|
||||
import mage.cards.ExpansionSet;
|
||||
|
|
@ -60,15 +59,6 @@ public class Sets extends HashMap<String, ExpansionSet> {
|
|||
private static Map<String, Card> cardMap;
|
||||
protected static Random rnd = new Random();
|
||||
|
||||
private static final String NAMES_CACHE_OBJECT_NAME = "card_names";
|
||||
private static final String NAMES_KEY = "card_names_key";
|
||||
private static final String CREATURE_TYPES_CACHE_OBJECT_NAME = "creature_types";
|
||||
private static final String CREATURE_TYPES_KEY = "creature_types_key";
|
||||
private static final String NONLAND_NAMES_CACHE_OBJECT_NAME = "nonland_names";
|
||||
private static final String NONLAND_NAMES_KEY = "nonland_names_key";
|
||||
|
||||
private static final int CACHE_VERSION = 1;
|
||||
|
||||
public static Sets getInstance() {
|
||||
return fINSTANCE;
|
||||
}
|
||||
|
|
@ -127,135 +117,29 @@ public class Sets extends HashMap<String, ExpansionSet> {
|
|||
this.addSet(Weatherlight.getInstance());
|
||||
this.addSet(Worldwake.getInstance());
|
||||
this.addSet(Zendikar.getInstance());
|
||||
loadCardNames();
|
||||
loadCreatureTypes();
|
||||
loadNonLandNames();
|
||||
names = CacheService.loadCardNames(cards);
|
||||
creatureTypes = CacheService.loadCreatureTypes(cards);
|
||||
nonLandNames = CacheService.loadNonLandNames(cards);
|
||||
}
|
||||
|
||||
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 (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 void loadCardNames() {
|
||||
Cache cache = CacheDataHelper.getCachedObject(NAMES_CACHE_OBJECT_NAME);
|
||||
if (cache == null || cache.getVersion() != CACHE_VERSION) {
|
||||
for (Card card : cards) {
|
||||
names.add(card.getName());
|
||||
}
|
||||
cache = new Cache(NAMES_CACHE_OBJECT_NAME, CACHE_VERSION);
|
||||
cache.getCacheObjects().put(NAMES_KEY, names);
|
||||
CacheDataHelper.cacheObject(cache, NAMES_CACHE_OBJECT_NAME);
|
||||
} else {
|
||||
Set<String> cachedNames = (Set<String>) cache.getCacheObjects().get(NAMES_KEY);
|
||||
names.addAll(cachedNames);
|
||||
}
|
||||
}
|
||||
|
||||
private void loadCreatureTypes() {
|
||||
Cache cache = CacheDataHelper.getCachedObject(CREATURE_TYPES_CACHE_OBJECT_NAME);
|
||||
if (cache == null || cache.getVersion() != CACHE_VERSION) {
|
||||
for (Card card : cards) {
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
cache = new Cache(CREATURE_TYPES_CACHE_OBJECT_NAME, CACHE_VERSION);
|
||||
cache.getCacheObjects().put(CREATURE_TYPES_KEY, creatureTypes);
|
||||
CacheDataHelper.cacheObject(cache, CREATURE_TYPES_CACHE_OBJECT_NAME);
|
||||
} else {
|
||||
Set<String> cachedCreatureTypes = (Set<String>) cache.getCacheObjects().get(CREATURE_TYPES_KEY);
|
||||
creatureTypes.addAll(cachedCreatureTypes);
|
||||
}
|
||||
}
|
||||
|
||||
private void loadNonLandNames() {
|
||||
Cache cache = CacheDataHelper.getCachedObject(NONLAND_NAMES_CACHE_OBJECT_NAME);
|
||||
if (cache == null || cache.getVersion() != CACHE_VERSION) {
|
||||
for (Card card : cards) {
|
||||
if (!card.getCardType().contains(CardType.LAND)) nonLandNames.add(card.getName());
|
||||
}
|
||||
cache = new Cache(NONLAND_NAMES_CACHE_OBJECT_NAME, CACHE_VERSION);
|
||||
cache.getCacheObjects().put(NONLAND_NAMES_KEY, nonLandNames);
|
||||
CacheDataHelper.cacheObject(cache, NONLAND_NAMES_CACHE_OBJECT_NAME);
|
||||
} else {
|
||||
Set<String> cachedNonLandNames = (Set<String>) cache.getCacheObjects().get(NONLAND_NAMES_KEY);
|
||||
nonLandNames.addAll(cachedNonLandNames);
|
||||
}
|
||||
}
|
||||
|
||||
/*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 (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("");
|
||||
}
|
||||
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()));
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue