[CardRepository] moved Sets, DeckImporter and CardRepository to Mage Framework

This commit is contained in:
North 2012-11-04 12:52:02 +02:00
parent 670646de15
commit 25cd490be3
27 changed files with 126 additions and 152 deletions

View file

@ -46,7 +46,7 @@ import mage.client.util.DefaultActionCallback;
import mage.client.util.ImageHelper;
import mage.client.util.gui.ArrowBuilder;
import mage.remote.Session;
import mage.sets.Sets;
import mage.cards.Sets;
import mage.view.AbilityView;
import mage.view.CardView;
import mage.view.PermanentView;

View file

@ -56,7 +56,7 @@ import mage.cards.CardDimensions;
import mage.cards.MagePermanent;
import mage.client.util.Config;
import mage.client.util.ImageHelper;
import mage.sets.Sets;
import mage.cards.Sets;
import mage.view.CounterView;
import mage.view.PermanentView;

View file

@ -13,7 +13,7 @@ import mage.client.cards.CardsStorage;
import mage.client.util.gui.ColorsChooser;
import mage.client.util.sets.ConstructedFormats;
import mage.interfaces.rate.RateCallback;
import mage.sets.Sets;
import mage.cards.Sets;
import mage.utils.DeckBuilder;
import javax.swing.*;

View file

@ -45,7 +45,7 @@ import mage.client.util.Listener;
import mage.components.CardInfoPane;
import mage.game.GameException;
import mage.remote.Session;
import mage.sets.Sets;
import mage.cards.Sets;
import mage.view.CardView;
import mage.view.SimpleCardView;
import org.apache.log4j.Logger;

View file

@ -50,7 +50,7 @@ import mage.filter.predicate.mageobject.ColorPredicate;
import mage.filter.predicate.mageobject.ColorlessPredicate;
import mage.filter.predicate.other.CardTextPredicate;
import mage.filter.predicate.other.ExpansionSetPredicate;
import mage.sets.Sets;
import mage.cards.Sets;
import mage.view.CardsView;
import javax.swing.*;

View file

@ -44,7 +44,7 @@ import mage.game.draft.DraftOptions.TimingOption;
import mage.game.tournament.LimitedOptions;
import mage.game.tournament.TournamentOptions;
import mage.remote.Session;
import mage.sets.Sets;
import mage.cards.Sets;
import mage.view.TableView;
import mage.view.TournamentTypeView;
import org.apache.log4j.Logger;

View file

@ -7,7 +7,7 @@ import java.util.GregorianCalendar;
import java.util.List;
import mage.cards.ExpansionSet;
import mage.cards.repository.CardRepository;
import mage.sets.Sets;
import mage.cards.Sets;
/**
* Utility class for constructed formats.

View file

@ -33,7 +33,7 @@ import java.util.GregorianCalendar;
import mage.Constants.SetType;
import mage.cards.ExpansionSet;
import mage.cards.decks.Constructed;
import mage.sets.Sets;
import mage.cards.Sets;
/**
*

View file

@ -33,7 +33,7 @@ import java.util.GregorianCalendar;
import mage.Constants.SetType;
import mage.cards.ExpansionSet;
import mage.cards.decks.Constructed;
import mage.sets.Sets;
import mage.cards.Sets;
/**
*

View file

@ -36,7 +36,7 @@ import java.util.Map;
import mage.game.tournament.Tournament;
import mage.game.tournament.TournamentOptions;
import mage.game.tournament.TournamentType;
import mage.sets.Sets;
import mage.cards.Sets;
import mage.view.TournamentTypeView;
import org.apache.log4j.Logger;

View file

@ -34,22 +34,6 @@
<artifactId>junit</artifactId>
<version>4.8.2</version>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.7.2</version>
</dependency>
<dependency>
<groupId>com.j256.ormlite</groupId>
<artifactId>ormlite-core</artifactId>
<version>4.42</version>
</dependency>
<dependency>
<groupId>com.j256.ormlite</groupId>
<artifactId>ormlite-jdbc</artifactId>
<version>4.42</version>
</dependency>
</dependencies>
<build>

View file

@ -11,7 +11,7 @@ import mage.player.ai.ComputerPlayer;
import mage.remote.Connection;
import mage.remote.Session;
import mage.remote.SessionImpl;
import mage.sets.Sets;
import mage.cards.Sets;
import mage.view.GameTypeView;
import mage.view.TableView;
import org.apache.log4j.Logger;

View file

@ -10,7 +10,7 @@ import mage.game.GameOptions;
import mage.game.TwoPlayerDuel;
import mage.player.ai.ComputerPlayer;
import mage.players.Player;
import mage.sets.Sets;
import mage.cards.Sets;
import org.junit.Ignore;
import org.junit.Test;
import org.mage.test.serverside.base.MageTestBase;

View file

@ -10,7 +10,7 @@ import mage.game.GameOptions;
import mage.game.TwoPlayerDuel;
import mage.player.ai.ComputerPlayer;
import mage.players.Player;
import mage.sets.Sets;
import mage.cards.Sets;
import org.junit.Ignore;
import org.junit.Test;
import org.mage.test.serverside.base.MageTestBase;

View file

@ -21,6 +21,22 @@
<version>1.2.14</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.7.2</version>
</dependency>
<dependency>
<groupId>com.j256.ormlite</groupId>
<artifactId>ormlite-core</artifactId>
<version>4.42</version>
</dependency>
<dependency>
<groupId>com.j256.ormlite</groupId>
<artifactId>ormlite-jdbc</artifactId>
<version>4.42</version>
</dependency>
</dependencies>
<build>

View file

@ -26,17 +26,15 @@
* or implied, of BetaSteward_at_googlemail.com.
*/
package mage.sets;
package mage.cards;
import mage.Constants.CardType;
import mage.Constants.ColoredManaSymbol;
import mage.cards.Card;
import mage.cards.CardImpl;
import mage.cards.ExpansionSet;
import mage.cards.decks.DeckCardLists;
import mage.cards.repository.CardCriteria;
import mage.cards.repository.CardInfo;
import mage.cards.repository.CardRepository;
import mage.util.ClassScanner;
import org.apache.log4j.Logger;
import java.io.FileNotFoundException;
@ -58,81 +56,14 @@ public class Sets extends HashMap<String, ExpansionSet> {
}
private Sets() {
this.addSet(AlaraReborn.getInstance());
this.addSet(Alliances.getInstance());
this.addSet(Antiquities.getInstance());
this.addSet(Apocalypse.getInstance());
this.addSet(ArabianNights.getInstance());
this.addSet(AvacynRestored.getInstance());
this.addSet(BetrayersOfKamigawa.getInstance());
this.addSet(ChampionsOfKamigawa.getInstance());
this.addSet(Coldsnap.getInstance());
this.addSet(Conflux.getInstance());
this.addSet(DarkAscension.getInstance());
this.addSet(Darksteel.getInstance());
this.addSet(Dissension.getInstance());
this.addSet(EighthEdition.getInstance());
this.addSet(ElspethvsTezzeret.getInstance());
this.addSet(Eventide.getInstance());
this.addSet(Exodus.getInstance());
this.addSet(FifthDawn.getInstance());
this.addSet(FifthEdition.getInstance());
this.addSet(FallenEmpires.getInstance());
this.addSet(FourthEdition.getInstance());
this.addSet(FutureSight.getInstance());
this.addSet(Guildpact.getInstance());
this.addSet(Guru.getInstance());
this.addSet(Homelands.getInstance());
this.addSet(IceAge.getInstance());
this.addSet(Innistrad.getInstance());
this.addSet(Invasion.getInstance());
this.addSet(Judgment.getInstance());
this.addSet(Legends.getInstance());
this.addSet(Legions.getInstance());
this.addSet(Lorwyn.getInstance());
this.addSet(Magic2010.getInstance());
this.addSet(Magic2011.getInstance());
this.addSet(Magic2012.getInstance());
this.addSet(Magic2013.getInstance());
this.addSet(MagicPlayerRewards.getInstance());
this.addSet(MercadianMasques.getInstance());
this.addSet(Mirage.getInstance());
this.addSet(Mirrodin.getInstance());
this.addSet(MirrodinBesieged.getInstance());
this.addSet(Morningtide.getInstance());
this.addSet(Nemesis.getInstance());
this.addSet(NewPhyrexia.getInstance());
this.addSet(NinthEdition.getInstance());
this.addSet(Odyssey.getInstance());
this.addSet(Onslaught.getInstance());
this.addSet(PlanarChaos.getInstance());
this.addSet(Planechase.getInstance());
this.addSet(Planeshift.getInstance());
this.addSet(Prophecy.getInstance());
this.addSet(RavnicaCityOfGuilds.getInstance());
this.addSet(ReturnToRavnica.getInstance());
this.addSet(RiseOfTheEldrazi.getInstance());
this.addSet(SaviorsOfKamigawa.getInstance());
this.addSet(ScarsOfMirrodin.getInstance());
this.addSet(Scourge.getInstance());
this.addSet(SeventhEdition.getInstance());
this.addSet(ShardsOfAlara.getInstance());
this.addSet(Shadowmoor.getInstance());
this.addSet(SixthEdition.getInstance());
this.addSet(Stronghold.getInstance());
this.addSet(Tenth.getInstance());
this.addSet(Tempest.getInstance());
this.addSet(TheDark.getInstance());
this.addSet(TimeSpiral.getInstance());
this.addSet(TimeSpiralTimeshifted.getInstance());
this.addSet(Torment.getInstance());
this.addSet(UrzasSaga.getInstance());
this.addSet(UrzasLegacy.getInstance());
this.addSet(UrzasDestiny.getInstance());
this.addSet(Visions.getInstance());
this.addSet(Weatherlight.getInstance());
this.addSet(Worldwake.getInstance());
this.addSet(Zendikar.getInstance());
ArrayList<String> packages = new ArrayList<String>();
packages.add("mage.sets");
for (Class c : ClassScanner.findClasses(packages, ExpansionSet.class)) {
try {
addSet((ExpansionSet) c.getMethod("getInstance").invoke(null));
} catch (Exception ex) {
}
}
}
private void addSet(ExpansionSet set) {
@ -190,8 +121,9 @@ public class Sets extends HashMap<String, ExpansionSet> {
}
public static ExpansionSet findSet(String code) {
if (fINSTANCE.containsKey(code))
if (fINSTANCE.containsKey(code)) {
return fINSTANCE.get(code);
}
return null;
}
@ -200,10 +132,12 @@ public class Sets extends HashMap<String, ExpansionSet> {
Map<String, Integer> deckCards = new HashMap<String, Integer>();
Map<String, Integer> sideboard = new HashMap<String, Integer>();
try {
if (deck.getName() != null && deck.getName().length() > 0)
if (deck.getName() != null && deck.getName().length() > 0) {
out.println("NAME:" + deck.getName());
if (deck.getAuthor() != null && deck.getAuthor().length() > 0)
}
if (deck.getAuthor() != null && deck.getAuthor().length() > 0) {
out.println("AUTHOR:" + deck.getAuthor());
}
for (String cardClass: deck.getCards()) {
if (deckCards.containsKey(cardClass)) {
deckCards.put(cardClass, deckCards.get(cardClass) + 1);

View file

@ -0,0 +1,70 @@
/*
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of BetaSteward_at_googlemail.com.
*/
package mage.cards.repository;
import java.util.ArrayList;
import java.util.List;
import mage.cards.Card;
import mage.cards.CardImpl;
import mage.cards.ExpansionSet;
import mage.cards.Sets;
import mage.util.ClassScanner;
/**
*
* @author North
*/
public class CardScanner {
private static boolean scanned = false;
public static void scan() {
if (scanned) {
return;
}
scanned = true;
List<CardInfo> cardsToAdd = new ArrayList<CardInfo>();
List<String> packages = new ArrayList<String>();
for (ExpansionSet set : Sets.getInstance().values()) {
packages.add(set.getPackageName());
}
for (Class c : ClassScanner.findClasses(packages, CardImpl.class)) {
if (!CardRepository.instance.cardExists(c.getCanonicalName())) {
Card card = CardImpl.createCard(c);
if (card != null && !card.isNightCard()) {
cardsToAdd.add(new CardInfo(card));
}
}
}
if (!cardsToAdd.isEmpty()) {
CardRepository.instance.addCards(cardsToAdd);
}
}
}

View file

@ -25,7 +25,7 @@
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of BetaSteward_at_googlemail.com.
*/
package mage.cards.repository;
package mage.util;
import java.io.File;
import java.io.FileInputStream;
@ -39,45 +39,14 @@ import java.util.List;
import java.util.TreeSet;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import mage.cards.Card;
import mage.cards.CardImpl;
import mage.cards.ExpansionSet;
import mage.sets.Sets;
/**
*
* @author North
*/
public class CardScanner {
public class ClassScanner {
private static boolean scanned = false;
public static void scan() {
if (scanned) {
return;
}
scanned = true;
List<CardInfo> cardsToAdd = new ArrayList<CardInfo>();
List<String> packages = new ArrayList<String>();
for (ExpansionSet set : Sets.getInstance().values()) {
packages.add(set.getPackageName());
}
for (Class c : getCards(packages)) {
if (!CardRepository.instance.cardExists(c.getCanonicalName())) {
Card card = CardImpl.createCard(c);
if (card != null && !card.isNightCard()) {
cardsToAdd.add(new CardInfo(card));
}
}
}
if (!cardsToAdd.isEmpty()) {
CardRepository.instance.addCards(cardsToAdd);
}
}
private static List<Class> getCards(List<String> packages) {
public static List<Class> findClasses(List<String> packages, Class<?> type) {
List<Class> cards = new ArrayList<Class>();
try {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
@ -102,19 +71,19 @@ public class CardScanner {
}
for (String filePath : dirs.keySet()) {
cards.addAll(findCards(new File(filePath), dirs.get(filePath)));
cards.addAll(findClasses(new File(filePath), dirs.get(filePath), type));
}
for (String filePath : jars) {
File file = new File(URLDecoder.decode(filePath, "UTF-8"));
cards.addAll(findCardsInJar(file, packages));
cards.addAll(findClassesInJar(file, packages, type));
}
} catch (IOException ex) {
}
return cards;
}
private static List<Class> findCards(File directory, String packageName) {
private static List<Class> findClasses(File directory, String packageName, Class<?> type) {
List<Class> cards = new ArrayList<Class>();
if (!directory.exists()) {
return cards;
@ -124,7 +93,7 @@ public class CardScanner {
if (file.getName().endsWith(".class")) {
try {
Class<?> clazz = Class.forName(packageName + '.' + file.getName().substring(0, file.getName().length() - 6));
if (CardImpl.class.isAssignableFrom(clazz)) {
if (type.isAssignableFrom(clazz)) {
cards.add(clazz);
}
} catch (ClassNotFoundException ex) {
@ -134,7 +103,7 @@ public class CardScanner {
return cards;
}
private static List<Class> findCardsInJar(File file, List<String> packages) {
private static List<Class> findClassesInJar(File file, List<String> packages, Class<?> type) {
List<Class> cards = new ArrayList<Class>();
if (!file.exists()) {
return cards;
@ -149,12 +118,13 @@ public class CardScanner {
}
if (jarEntry.getName().endsWith(".class")) {
String className = jarEntry.getName().replace('/', '.').replace(".class", "");
String packageName = className.substring(0, className.lastIndexOf('.'));
int packageNameEnd = className.lastIndexOf('.');
String packageName = packageNameEnd != -1 ? className.substring(0, packageNameEnd) : "";
if (packages.contains(packageName)) {
Class<?> clazz;
try {
clazz = Class.forName(className);
if (CardImpl.class.isAssignableFrom(clazz)) {
if (type.isAssignableFrom(clazz)) {
cards.add(clazz);
}
} catch (ClassNotFoundException ex) {