[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

@ -1,83 +0,0 @@
/*
* 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.decks.importer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import mage.cards.decks.DeckCardLists;
import mage.cards.repository.CardInfo;
import mage.cards.repository.CardRepository;
/**
*
* @author North
*/
public class DckDeckImporter extends DeckImporter {
private static final Pattern pattern = Pattern.compile("(SB:)?\\s*(\\d*)\\s*\\[([a-zA-Z0-9]{3}):(\\d*)\\].*");
@Override
protected void readLine(String line, DeckCardLists deckList) {
if (line.length() == 0 || line.startsWith("#")) {
return;
}
Matcher m = pattern.matcher(line);
if (m.matches()) {
boolean sideboard = false;
if ("SB:".equals(m.group(1))) {
sideboard = true;
}
int count = Integer.parseInt(m.group(2));
String setCode = m.group(3);
int cardNum = Integer.parseInt(m.group(4));
String className = null;
CardInfo cardInfo = CardRepository.instance.findCard(setCode, cardNum);
if (cardInfo != null) {
className = cardInfo.getClassName();
}
if (className != null) {
for (int i = 0; i < count; i++) {
if (!sideboard) {
deckList.getCards().add(className);
} else {
deckList.getSideboard().add(className);
}
}
} else {
sbMessage.append("Could not find card '").append("' at line ").append(lineCount).append(": ").append(line).append("\n");
}
} else if (line.startsWith("NAME:")) {
deckList.setName(line.substring(5, line.length()));
} else if (line.startsWith("AUTHOR:")) {
deckList.setAuthor(line.substring(7, line.length()));
}
}
}

View file

@ -1,79 +0,0 @@
/*
* 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.decks.importer;
import java.util.List;
import java.util.Random;
import mage.cards.decks.DeckCardLists;
import mage.cards.repository.CardInfo;
import mage.cards.repository.CardRepository;
/**
*
* @author BetaSteward_at_googlemail.com
*/
public class DecDeckImporter extends DeckImporter {
@Override
protected void readLine(String line, DeckCardLists deckList) {
if (line.length() == 0 || line.startsWith("//")) {
return;
}
boolean sideboard = false;
if (line.startsWith("SB:")) {
line = line.substring(3).trim();
sideboard = true;
}
int delim = line.indexOf(' ');
String lineNum = line.substring(0, delim).trim();
String lineName = line.substring(delim).trim();
try {
int num = Integer.parseInt(lineNum);
List<CardInfo> cards = CardRepository.instance.findCards(lineName);
if (cards.isEmpty()) {
sbMessage.append("Could not find card: '").append(lineName).append("' at line ").append(lineCount).append("\n");
} else {
Random random = new Random();
for (int i = 0; i < num; i++) {
String className = cards.get(random.nextInt(cards.size())).getClassName();
if (!sideboard) {
deckList.getCards().add(className);
} else {
deckList.getSideboard().add(className);
}
}
}
} catch (NumberFormatException nfe) {
sbMessage.append("Invalid number: ").append(lineNum).append(" at line ").append(lineCount).append("\n");
}
}
}

View file

@ -1,80 +0,0 @@
/*
* 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.decks.importer;
import java.io.File;
import java.util.Scanner;
import mage.cards.decks.DeckCardLists;
import org.apache.log4j.Logger;
/**
*
* @author BetaSteward_at_googlemail.com
*/
public abstract class DeckImporter {
private final static Logger logger = Logger.getLogger(DeckImporter.class);
protected StringBuilder sbMessage = new StringBuilder();
protected int lineCount;
public DeckCardLists importDeck(String file) {
File f = new File(file);
DeckCardLists deckList = new DeckCardLists();
lineCount = 0;
sbMessage.setLength(0);
try {
Scanner scanner = new Scanner(f);
try {
while (scanner.hasNextLine()) {
String line = scanner.nextLine().trim();
lineCount++;
readLine(line, deckList);
}
if (sbMessage.length() > 0) {
logger.fatal(sbMessage);
}
}
catch (Exception ex) {
logger.fatal(null, ex);
}
finally {
scanner.close();
}
} catch (Exception ex) {
logger.fatal(null, ex);
}
return deckList;
}
public String getErrors(){
return sbMessage.toString();
}
protected abstract void readLine(String line, DeckCardLists deckList);
}

View file

@ -1,60 +0,0 @@
/*
* 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.decks.importer;
import mage.cards.decks.DeckCardLists;
/**
*
* @author North
*/
public class DeckImporterUtil {
public static DeckImporter getDeckImporter(String file) {
if (file.toLowerCase().endsWith("dec")) {
return new DecDeckImporter();
} else if (file.toLowerCase().endsWith("mwdeck")) {
return new MWSDeckImporter();
} else if (file.toLowerCase().endsWith("txt")) {
return new TxtDeckImporter();
} else if (file.toLowerCase().endsWith("dck")) {
return new DckDeckImporter();
} else {
return null;
}
}
public static DeckCardLists importDeck(String file) {
DeckImporter deckImporter = getDeckImporter(file);
if (deckImporter != null) {
return deckImporter.importDeck(file);
} else {
return new DeckCardLists();
}
}
}

View file

@ -1,93 +0,0 @@
/*
* 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.decks.importer;
import java.util.List;
import java.util.Random;
import mage.cards.decks.DeckCardLists;
import mage.cards.repository.CardCriteria;
import mage.cards.repository.CardInfo;
import mage.cards.repository.CardRepository;
/**
*
* @author BetaSteward_at_googlemail.com
*/
public class MWSDeckImporter extends DeckImporter {
@Override
protected void readLine(String line, DeckCardLists deckList) {
if (line.length() == 0 || line.startsWith("//")) {
return;
}
boolean sideboard = false;
if (line.startsWith("SB:")) {
line = line.substring(3).trim();
sideboard = true;
}
int delim = line.indexOf(' ');
String lineNum = line.substring(0, delim).trim();
String setCode = "";
if (line.indexOf('[') != -1) {
int setStart = line.indexOf('[') + 1;
int setEnd = line.indexOf(']');
setCode = line.substring(setStart, setEnd).trim();
delim = setEnd;
}
String lineName = line.substring(delim + 1).trim();
try {
int num = Integer.parseInt(lineNum);
CardCriteria criteria = new CardCriteria();
criteria.name(lineName);
criteria.setCodes(setCode);
List<CardInfo> cards = CardRepository.instance.findCards(criteria);
if (cards.isEmpty()) {
criteria = new CardCriteria();
criteria.name(lineName);
cards = CardRepository.instance.findCards(criteria);
}
if (cards.isEmpty()) {
sbMessage.append("Could not find card: '").append(lineName).append("' at line ").append(lineCount).append("\n");
} else {
Random random = new Random();
for (int i = 0; i < num; i++) {
String className = cards.get(random.nextInt(cards.size())).getClassName();
if (!sideboard) {
deckList.getCards().add(className);
} else {
deckList.getSideboard().add(className);
}
}
}
} catch (NumberFormatException nfe) {
sbMessage.append("Invalid number: ").append(lineNum).append(" at line ").append(lineCount).append("\n");
}
}
}

View file

@ -1,80 +0,0 @@
/*
* 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.decks.importer;
import java.util.List;
import java.util.Random;
import mage.cards.decks.DeckCardLists;
import mage.cards.repository.CardInfo;
import mage.cards.repository.CardRepository;
/**
*
* @author BetaSteward_at_googlemail.com
*/
public class TxtDeckImporter extends DeckImporter {
private boolean sideboard = false;
@Override
protected void readLine(String line, DeckCardLists deckList) {
if (line.length() == 0 || line.startsWith("//")) {
return;
}
if (line.startsWith("Sideboard")) {
sideboard = true;
return;
}
int delim = line.indexOf(' ');
String lineNum = line.substring(0, delim).trim();
String lineName = line.substring(delim).trim();
try {
int num = Integer.parseInt(lineNum);
List<CardInfo> cards = CardRepository.instance.findCards(lineName);
if (cards.isEmpty()) {
sbMessage.append("Could not find card: '").append(lineName).append("' at line ").append(lineCount).append("\n");
} else {
Random random = new Random();
for (int i = 0; i < num; i++) {
String className = cards.get(random.nextInt(cards.size())).getClassName();
if (!sideboard) {
deckList.getCards().add(className);
} else {
deckList.getSideboard().add(className);
}
}
}
} catch (NumberFormatException nfe) {
sbMessage.append("Invalid number: ").append(lineNum).append(" at line ").append(lineCount).append("\n");
}
}
}

View file

@ -1,247 +0,0 @@
/*
* 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 com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.SelectArg;
import com.j256.ormlite.stmt.Where;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import mage.Constants.CardType;
/**
*
* @author North
*/
public class CardCriteria {
private String name;
private String rules;
private List<String> setCodes;
private List<CardType> types;
private List<CardType> notTypes;
private List<String> supertypes;
private List<String> notSupertypes;
private List<String> subtypes;
private boolean black;
private boolean blue;
private boolean green;
private boolean red;
private boolean white;
private boolean colorless;
private Long start;
private Long count;
public CardCriteria() {
this.setCodes = new ArrayList<String>();
this.types = new ArrayList<CardType>();
this.notTypes = new ArrayList<CardType>();
this.supertypes = new ArrayList<String>();
this.notSupertypes = new ArrayList<String>();
this.subtypes = new ArrayList<String>();
this.black = true;
this.blue = true;
this.green = true;
this.red = true;
this.white = true;
this.colorless = true;
}
public CardCriteria black(boolean black) {
this.black = black;
return this;
}
public CardCriteria blue(boolean blue) {
this.blue = blue;
return this;
}
public CardCriteria green(boolean green) {
this.green = green;
return this;
}
public CardCriteria red(boolean red) {
this.red = red;
return this;
}
public CardCriteria white(boolean white) {
this.white = white;
return this;
}
public CardCriteria colorless(boolean colorless) {
this.colorless = colorless;
return this;
}
public CardCriteria name(String name) {
this.name = name;
return this;
}
public CardCriteria rules(String rules) {
this.rules = rules;
return this;
}
public CardCriteria start(Long start) {
this.start = start;
return this;
}
public CardCriteria count(Long count) {
this.count = count;
return this;
}
public CardCriteria setCodes(String... setCodes) {
this.setCodes.addAll(Arrays.asList(setCodes));
return this;
}
public CardCriteria types(CardType... types) {
this.types.addAll(Arrays.asList(types));
return this;
}
public CardCriteria notTypes(CardType... types) {
this.notTypes.addAll(Arrays.asList(types));
return this;
}
public CardCriteria supertypes(String... supertypes) {
this.supertypes.addAll(Arrays.asList(supertypes));
return this;
}
public CardCriteria notSupertypes(String... supertypes) {
this.notSupertypes.addAll(Arrays.asList(supertypes));
return this;
}
public CardCriteria subtypes(String... subtypes) {
this.subtypes.addAll(Arrays.asList(subtypes));
return this;
}
public void buildQuery(QueryBuilder qb) throws SQLException {
Where where = qb.where();
int clausesCount = 0;
if (name != null) {
where.like("name", new SelectArg('%' + name + '%'));
clausesCount++;
}
if (rules != null) {
where.like("rules", new SelectArg('%' + rules + '%'));
clausesCount++;
}
for (String setCode : setCodes) {
where.eq("setCode", setCode);
}
if (!setCodes.isEmpty()) {
where.or(setCodes.size());
clausesCount++;
}
for (CardType type : types) {
where.like("types", new SelectArg('%' + type.name() + '%'));
}
if (!types.isEmpty()) {
where.or(types.size());
clausesCount++;
}
for (CardType type : notTypes) {
where.not().like("types", new SelectArg('%' + type.name() + '%'));
clausesCount++;
}
for (String superType : supertypes) {
where.like("supertypes", new SelectArg('%' + superType + '%'));
clausesCount++;
}
for (String subType : notSupertypes) {
where.not().like("supertypes", new SelectArg('%' + subType + '%'));
clausesCount++;
}
for (String subType : subtypes) {
where.like("subtypes", new SelectArg('%' + subType + '%'));
clausesCount++;
}
if (!black || !blue || !green || !red || !white || !colorless) {
int colorClauses = 0;
if (black) {
where.eq("black", true);
colorClauses++;
}
if (blue) {
where.eq("blue", true);
colorClauses++;
}
if (green) {
where.eq("green", true);
colorClauses++;
}
if (red) {
where.eq("red", true);
colorClauses++;
}
if (white) {
where.eq("white", true);
colorClauses++;
}
if (colorless) {
where.eq("black", false).eq("blue", false).eq("green", false).eq("red", false).eq("white", false);
where.and(5);
colorClauses++;
}
where.or(colorClauses);
clausesCount++;
}
where.and(clausesCount);
if (start != null) {
qb.offset(start);
}
if (count != null) {
qb.limit(count);
}
qb.orderBy("cardNumber", true);
}
}

View file

@ -1,217 +0,0 @@
/*
* 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 com.j256.ormlite.field.DataType;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import mage.Constants.CardType;
import mage.Constants.Rarity;
import mage.ObjectColor;
import mage.cards.Card;
import mage.cards.CardImpl;
/**
*
* @author North
*/
@DatabaseTable(tableName = "card")
public class CardInfo {
private static final String SEPARATOR = "@@@";
@DatabaseField
protected String name;
@DatabaseField
protected int cardNumber;
@DatabaseField
protected String setCode;
@DatabaseField
protected String className;
@DatabaseField
protected String power;
@DatabaseField
protected String toughness;
@DatabaseField
protected int convertedManaCost;
@DatabaseField(dataType = DataType.ENUM_STRING)
protected Rarity rarity;
@DatabaseField
protected String types;
@DatabaseField
protected String subtypes;
@DatabaseField
protected String supertypes;
@DatabaseField
protected String manaCosts;
@DatabaseField
protected String rules;
@DatabaseField
protected boolean black;
@DatabaseField
protected boolean blue;
@DatabaseField
protected boolean green;
@DatabaseField
protected boolean red;
@DatabaseField
protected boolean white;
public CardInfo() {
}
public CardInfo(Card card) {
this.name = card.getName();
this.cardNumber = card.getCardNumber();
this.setCode = card.getExpansionSetCode();
this.className = card.getClass().getCanonicalName();
this.power = card.getPower().toString();
this.toughness = card.getToughness().toString();
this.convertedManaCost = card.getManaCost().convertedManaCost();
this.rarity = card.getRarity();
this.blue = card.getColor().isBlue();
this.black = card.getColor().isBlack();
this.green = card.getColor().isGreen();
this.red = card.getColor().isRed();
this.white = card.getColor().isWhite();
this.setTypes(card.getCardType());
this.setSubtypes(card.getSubtype());
this.setSuperTypes(card.getSupertype());
this.setManaCosts(card.getManaCost().getSymbols());
this.setRules(card.getRules());
}
public Card getCard() {
return CardImpl.createCard(className);
}
public ObjectColor getColor() {
ObjectColor color = new ObjectColor();
color.setBlack(black);
color.setBlue(blue);
color.setGreen(green);
color.setRed(red);
color.setWhite(white);
return color;
}
private String joinList(List items) {
StringBuilder sb = new StringBuilder();
for (Object item : items) {
sb.append(item.toString()).append(SEPARATOR);
}
return sb.toString();
}
private List<String> parseList(String list) {
return Arrays.asList(list.split(SEPARATOR));
}
public final List<CardType> getTypes() {
ArrayList<CardType> list = new ArrayList<CardType>();
for (String type : this.types.split(SEPARATOR)) {
try {
list.add(CardType.valueOf(type));
} catch (Exception e) {
}
}
return list;
}
public final void setTypes(List<CardType> types) {
StringBuilder sb = new StringBuilder();
for (CardType item : types) {
sb.append(item.name()).append(SEPARATOR);
}
this.types = sb.toString();
}
public int getConvertedManaCost() {
return convertedManaCost;
}
public final List<String> getManaCosts() {
return parseList(manaCosts);
}
public final void setManaCosts(List<String> manaCosts) {
this.manaCosts = joinList(manaCosts);
}
public String getName() {
return name;
}
public String getPower() {
return power;
}
public Rarity getRarity() {
return rarity;
}
public final List<String> getRules() {
return parseList(rules);
}
public final void setRules(List<String> rules) {
this.rules = joinList(rules);
}
public final List<String> getSubTypes() {
return parseList(subtypes);
}
public final void setSubtypes(List<String> subtypes) {
this.subtypes = joinList(subtypes);
}
public final List<String> getSupertypes() {
return parseList(supertypes);
}
public final void setSuperTypes(List<String> superTypes) {
this.supertypes = joinList(superTypes);
}
public String getToughness() {
return toughness;
}
public String getSetCode() {
return setCode;
}
public String getClassName() {
return className;
}
}

View file

@ -1,230 +0,0 @@
/*
* 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 com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.jdbc.JdbcConnectionSource;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.SelectArg;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.Callable;
import mage.Constants.CardType;
/**
*
* @author North
*/
public enum CardRepository {
instance;
private static final String JDBC_URL = "jdbc:sqlite:db/cards.db";
private Random random = new Random();
private Dao<CardInfo, Object> cardDao;
private Set<String> classNames;
private CardRepository() {
File file = new File("db");
if (!file.exists()) {
file.mkdirs();
}
try {
ConnectionSource connectionSource = new JdbcConnectionSource(JDBC_URL);
TableUtils.createTableIfNotExists(connectionSource, CardInfo.class);
cardDao = DaoManager.createDao(connectionSource, CardInfo.class);
} catch (SQLException ex) {
}
}
public void addCards(final List<CardInfo> cards) {
try {
cardDao.callBatchTasks(new Callable<Object>() {
@Override
public Object call() throws Exception {
try {
for (CardInfo card : cards) {
cardDao.create(card);
if (classNames != null) {
classNames.add(card.getClassName());
}
}
} catch (SQLException ex) {
}
return null;
}
});
} catch (Exception ex) {
}
}
public boolean cardExists(String className) {
try {
if (classNames == null) {
QueryBuilder<CardInfo, Object> qb = cardDao.queryBuilder();
qb.distinct().selectColumns("className").where().isNotNull("className");
List<CardInfo> results = cardDao.query(qb.prepare());
classNames = new TreeSet<String>();
for (CardInfo card : results) {
classNames.add(card.getClassName());
}
}
return classNames.contains(className);
} catch (SQLException ex) {
}
return false;
}
public List<String> getSetCodes() {
List<String> setCodes = new ArrayList<String>();
try {
QueryBuilder<CardInfo, Object> qb = cardDao.queryBuilder();
qb.distinct().selectColumns("setCode");
List<CardInfo> results = cardDao.query(qb.prepare());
for (CardInfo card : results) {
setCodes.add(card.getSetCode());
}
} catch (SQLException ex) {
} finally {
return setCodes;
}
}
public Set<String> getNames() {
Set<String> names = new TreeSet<String>();
try {
QueryBuilder<CardInfo, Object> qb = cardDao.queryBuilder();
qb.distinct().selectColumns("name");
List<CardInfo> results = cardDao.query(qb.prepare());
for (CardInfo card : results) {
names.add(card.getName());
}
} catch (SQLException ex) {
} finally {
return names;
}
}
public Set<String> getNonLandNames() {
Set<String> names = new TreeSet<String>();
try {
QueryBuilder<CardInfo, Object> qb = cardDao.queryBuilder();
qb.distinct().selectColumns("name");
qb.where().not().like("types", new SelectArg('%' + CardType.LAND.name() + '%'));
List<CardInfo> results = cardDao.query(qb.prepare());
for (CardInfo card : results) {
names.add(card.getName());
}
} catch (SQLException ex) {
} finally {
return names;
}
}
public Set<String> getCreatureTypes() {
TreeSet<String> subtypes = new TreeSet<String>();
try {
QueryBuilder<CardInfo, Object> qb = cardDao.queryBuilder();
qb.distinct().selectColumns("subtypes");
qb.where().like("types", new SelectArg('%' + CardType.CREATURE.name() + '%'));
List<CardInfo> results = cardDao.query(qb.prepare());
for (CardInfo card : results) {
subtypes.addAll(card.getSubTypes());
}
} catch (SQLException ex) {
} finally {
return subtypes;
}
}
public CardInfo findCard(String setCode, int cardNumber) {
try {
QueryBuilder<CardInfo, Object> queryBuilder = cardDao.queryBuilder();
queryBuilder.where().eq("setCode", new SelectArg(setCode)).and().eq("cardNumber", cardNumber);
List<CardInfo> result = cardDao.query(queryBuilder.prepare());
if (!result.isEmpty()) {
return result.get(0);
}
} catch (SQLException ex) {
}
return null;
}
/**
*
* @param name
* @return random card with the provided name or null if none is found
*/
public CardInfo findCard(String name) {
List<CardInfo> cards = findCards(name);
if (!cards.isEmpty()) {
return cards.get(random.nextInt(cards.size()));
}
return null;
}
public List<CardInfo> findCards(String name) {
try {
QueryBuilder<CardInfo, Object> queryBuilder = cardDao.queryBuilder();
queryBuilder.where().eq("name", new SelectArg(name));
return cardDao.query(queryBuilder.prepare());
} catch (SQLException ex) {
}
return new ArrayList<CardInfo>();
}
public List<CardInfo> findCards(CardCriteria criteria) {
try {
QueryBuilder<CardInfo, Object> queryBuilder = cardDao.queryBuilder();
criteria.buildQuery(queryBuilder);
return cardDao.query(queryBuilder.prepare());
} catch (SQLException ex) {
}
return new ArrayList<CardInfo>();
}
public List<CardInfo> getAllCards() {
try {
QueryBuilder<CardInfo, Object> queryBuilder = cardDao.queryBuilder();
return cardDao.query(queryBuilder.prepare());
} catch (SQLException ex) {
}
return new ArrayList<CardInfo>();
}
}

View file

@ -1,174 +0,0 @@
/*
* 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.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
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 {
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) {
List<Class> cards = new ArrayList<Class>();
try {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
assert classLoader != null;
HashMap<String, String> dirs = new HashMap<String, String>();
TreeSet<String> jars = new TreeSet<String>();
for (String packageName : packages) {
String path = packageName.replace('.', '/');
Enumeration<URL> resources = classLoader.getResources(path);
while (resources.hasMoreElements()) {
URL resource = resources.nextElement();
String filePath = resource.getFile();
if (filePath.startsWith("file:")) {
filePath = filePath.substring("file:".length(), filePath.lastIndexOf("!"));
jars.add(filePath);
} else {
dirs.put(filePath, packageName);
}
}
}
for (String filePath : dirs.keySet()) {
cards.addAll(findCards(new File(filePath), dirs.get(filePath)));
}
for (String filePath : jars) {
File file = new File(URLDecoder.decode(filePath, "UTF-8"));
cards.addAll(findCardsInJar(file, packages));
}
} catch (IOException ex) {
}
return cards;
}
private static List<Class> findCards(File directory, String packageName) {
List<Class> cards = new ArrayList<Class>();
if (!directory.exists()) {
return cards;
}
for (File file : directory.listFiles()) {
if (file.getName().endsWith(".class")) {
try {
Class<?> clazz = Class.forName(packageName + '.' + file.getName().substring(0, file.getName().length() - 6));
if (CardImpl.class.isAssignableFrom(clazz)) {
cards.add(clazz);
}
} catch (ClassNotFoundException ex) {
}
}
}
return cards;
}
private static List<Class> findCardsInJar(File file, List<String> packages) {
List<Class> cards = new ArrayList<Class>();
if (!file.exists()) {
return cards;
}
JarInputStream jarFile = null;
try {
jarFile = new JarInputStream(new FileInputStream(file));
while (true) {
JarEntry jarEntry = jarFile.getNextJarEntry();
if (jarEntry == null) {
break;
}
if (jarEntry.getName().endsWith(".class")) {
String className = jarEntry.getName().replace('/', '.').replace(".class", "");
String packageName = className.substring(0, className.lastIndexOf('.'));
if (packages.contains(packageName)) {
Class<?> clazz;
try {
clazz = Class.forName(className);
if (CardImpl.class.isAssignableFrom(clazz)) {
cards.add(clazz);
}
} catch (ClassNotFoundException ex) {
}
}
}
}
} catch (IOException ex) {
} finally {
try {
jarFile.close();
} catch (IOException ex) {
}
}
return cards;
}
}

View file

@ -1,251 +0,0 @@
/*
* 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.sets;
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 org.apache.log4j.Logger;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.*;
/**
*
* @author BetaSteward_at_googlemail.com
*/
public class Sets extends HashMap<String, ExpansionSet> {
private final static Logger logger = Logger.getLogger(Sets.class);
private static final Sets fINSTANCE = new Sets();
protected static Random rnd = new Random();
public static Sets getInstance() {
return fINSTANCE;
}
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());
}
private void addSet(ExpansionSet set) {
this.put(set.getCode(), set);
}
/**
* Generates card pool of cardsCount cards that have manacost of allowed colors.
*
* @param cardsCount
* @param allowedColors
* @return
*/
public static List<Card> generateRandomCardPool(int cardsCount, List<ColoredManaSymbol> allowedColors) {
CardCriteria criteria = new CardCriteria();
criteria.notTypes(CardType.LAND);
for (ColoredManaSymbol color : allowedColors) {
switch (color) {
case W:
criteria.white(true);
break;
case U:
criteria.blue(true);
break;
case B:
criteria.black(true);
break;
case R:
criteria.red(true);
break;
case G:
criteria.green(true);
break;
}
}
List<CardInfo> cards = CardRepository.instance.findCards(criteria);
int count = 0;
int tries = 0;
List<Card> cardPool = new ArrayList<Card>();
while (count < cardsCount) {
CardInfo cardInfo = cards.get(rnd.nextInt(cards.size()));
Card card = cardInfo != null ? cardInfo.getCard() : null;
if (card != null) {
cardPool.add(card);
count++;
}
tries++;
if (tries > 4096) { // to avoid infinite loop
throw new IllegalStateException("Not enough cards for chosen colors to generate deck: " + allowedColors);
}
}
return cardPool;
}
public static ExpansionSet findSet(String code) {
if (fINSTANCE.containsKey(code))
return fINSTANCE.get(code);
return null;
}
public static void saveDeck(String file, DeckCardLists deck) throws FileNotFoundException {
PrintWriter out = new PrintWriter(file);
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)
out.println("NAME:" + deck.getName());
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);
}
else {
deckCards.put(cardClass, 1);
}
}
for (String cardClass: deck.getSideboard()) {
if (sideboard.containsKey(cardClass)) {
sideboard.put(cardClass, sideboard.get(cardClass) + 1);
}
else {
sideboard.put(cardClass, 1);
}
}
for (Map.Entry<String, Integer> entry: deckCards.entrySet()) {
Card card = CardImpl.createCard(entry.getKey());
if (card != null) {
out.printf("%d [%s:%d] %s%n", entry.getValue(), card.getExpansionSetCode(), card.getCardNumber(), card.getName());
}
}
for (Map.Entry<String, Integer> entry: sideboard.entrySet()) {
Card card = CardImpl.createCard(entry.getKey());
if (card != null) {
out.printf("SB: %d [%s:%d] %s%n", entry.getValue(), card.getExpansionSetCode(), card.getCardNumber(), card.getName());
}
}
}
finally {
out.close();
}
}
public ExpansionSet[] getSortedByReleaseDate() {
ExpansionSet[] sets = Sets.getInstance().values().toArray(new ExpansionSet[0]);
Arrays.sort(sets, new Comparator<ExpansionSet>() {
@Override
public int compare(ExpansionSet o1, ExpansionSet o2) {
return o2.getReleaseDate().compareTo(o1.getReleaseDate());
}
});
return sets;
}
}