diff --git a/Mage.Server/cache/card_names.obj b/Mage.Server/cache/card_names.obj new file mode 100644 index 0000000000..50cc657fbd Binary files /dev/null and b/Mage.Server/cache/card_names.obj differ diff --git a/Mage.Server/cache/creature_types.obj b/Mage.Server/cache/creature_types.obj new file mode 100644 index 0000000000..d686b0f033 Binary files /dev/null and b/Mage.Server/cache/creature_types.obj differ diff --git a/Mage.Server/cache/nonland_names.obj b/Mage.Server/cache/nonland_names.obj new file mode 100644 index 0000000000..12aac239c4 Binary files /dev/null and b/Mage.Server/cache/nonland_names.obj differ diff --git a/Mage.Sets/src/mage/sets/Sets.java b/Mage.Sets/src/mage/sets/Sets.java index 20dd162ab6..a38c0fa295 100644 --- a/Mage.Sets/src/mage/sets/Sets.java +++ b/Mage.Sets/src/mage/sets/Sets.java @@ -31,6 +31,8 @@ package mage.sets; import mage.Constants.CardType; import mage.Constants.ColoredManaSymbol; import mage.Mana; +import mage.cache.Cache; +import mage.cache.CacheDataHelper; import mage.cards.Card; import mage.cards.CardImpl; import mage.cards.ExpansionSet; @@ -58,6 +60,15 @@ public class Sets extends HashMap { private static Map 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; } @@ -116,11 +127,15 @@ public class Sets extends HashMap { this.addSet(Weatherlight.getInstance()); this.addSet(Worldwake.getInstance()); this.addSet(Zendikar.getInstance()); + loadCardNames(); + loadCreatureTypes(); + loadNonLandNames(); } 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)) { @@ -135,9 +150,61 @@ public class Sets extends HashMap { } 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 cachedNames = (Set) 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 cachedCreatureTypes = (Set) 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 cachedNonLandNames = (Set) cache.getCacheObjects().get(NONLAND_NAMES_KEY); + nonLandNames.addAll(cachedNonLandNames); + } + } + /*private static void loadCards() { if (cards.isEmpty()) { System.out.println("Loading cards...");