reuse shared code for picking basic land sets

This commit is contained in:
Neil Gentleman 2015-11-19 22:23:55 -08:00
parent 05dd1daee6
commit a90d0e5597
5 changed files with 31 additions and 101 deletions

View file

@ -41,6 +41,7 @@ import mage.client.util.sets.ConstructedFormats;
import mage.constants.CardType; import mage.constants.CardType;
import mage.constants.ColoredManaSymbol; import mage.constants.ColoredManaSymbol;
import mage.constants.Rarity; import mage.constants.Rarity;
import mage.util.TournamentUtil;
/** /**
@ -285,42 +286,7 @@ public class DeckGenerator {
*/ */
private static Map<String, List<CardInfo>> generateBasicLands(List<String> setsToUse) { private static Map<String, List<CardInfo>> generateBasicLands(List<String> setsToUse) {
List<String> landSets = new LinkedList<>(); Set<String> landSets = TournamentUtil.getLandSetCodeForDeckSets(setsToUse);
// decide from which sets basic lands are taken from
for (String setCode :setsToUse) {
ExpansionInfo expansionInfo = ExpansionRepository.instance.getSetByCode(setCode);
if (expansionInfo.hasBasicLands()) {
landSets.add(expansionInfo.getCode());
}
}
// if sets have no basic land, take land from block
if (landSets.isEmpty()) {
for (String setCode :setsToUse) {
ExpansionInfo expansionInfo = ExpansionRepository.instance.getSetByCode(setCode);
List<ExpansionInfo> blockSets = ExpansionRepository.instance.getSetsFromBlock(expansionInfo.getBlockName());
for (ExpansionInfo blockSet: blockSets) {
if (blockSet.hasBasicLands()) {
landSets.add(blockSet.getCode());
}
}
}
}
// if still no set with lands found, take one by random
if (landSets.isEmpty()) {
// if sets have no basic lands and also it has no parent or parent has no lands get last set with lands
// select a set with basic lands by random
Random generator = new Random();
List<ExpansionInfo> basicLandSets = ExpansionRepository.instance.getSetsWithBasicLandsByReleaseDate();
if (basicLandSets.size() > 0) {
landSets.add(basicLandSets.get(generator.nextInt(basicLandSets.size())).getCode());
}
}
if (landSets.isEmpty()) {
throw new IllegalArgumentException("No set with basic land was found");
}
CardCriteria criteria = new CardCriteria(); CardCriteria criteria = new CardCriteria();
if (!landSets.isEmpty()) { if (!landSets.isEmpty()) {

View file

@ -57,7 +57,7 @@ public class AddLandDialog extends MageDialog {
private static final Logger logger = Logger.getLogger(MageDialog.class); private static final Logger logger = Logger.getLogger(MageDialog.class);
private Deck deck; private Deck deck;
private final Set<String> setCodesland = new HashSet<>(); private final Set<String> landSetCodes = new HashSet<>();
private static final int DEFAULT_SEALED_DECK_CARD_NUMBER = 40; private static final int DEFAULT_SEALED_DECK_CARD_NUMBER = 40;
@ -71,27 +71,27 @@ public class AddLandDialog extends MageDialog {
public void showDialog(Deck deck, DeckEditorMode mode) { public void showDialog(Deck deck, DeckEditorMode mode) {
this.deck = deck; this.deck = deck;
SortedSet<String> landSets = new TreeSet<>(); SortedSet<String> landSetNames = new TreeSet<>();
if (!mode.equals(DeckEditorMode.FREE_BUILDING)) { if (!mode.equals(DeckEditorMode.FREE_BUILDING)) {
// decide from which sets basic lands are taken from // decide from which sets basic lands are taken from
for (String setCode : deck.getExpansionSetCodes()) { for (String setCode : deck.getExpansionSetCodes()) {
ExpansionInfo expansionInfo = ExpansionRepository.instance.getSetByCode(setCode); ExpansionInfo expansionInfo = ExpansionRepository.instance.getSetByCode(setCode);
if (expansionInfo != null && expansionInfo.hasBasicLands()) { if (expansionInfo != null && expansionInfo.hasBasicLands()) {
this.setCodesland.add(expansionInfo.getCode()); this.landSetCodes.add(expansionInfo.getCode());
landSets.add(expansionInfo.getName()); landSetNames.add(expansionInfo.getName());
} }
} }
// if sets have no basic land, take land from block // if sets have no basic land, take land from block
if (this.setCodesland.isEmpty()) { if (this.landSetCodes.isEmpty()) {
for (String setCode : deck.getExpansionSetCodes()) { for (String setCode : deck.getExpansionSetCodes()) {
ExpansionInfo expansionInfo = ExpansionRepository.instance.getSetByCode(setCode); ExpansionInfo expansionInfo = ExpansionRepository.instance.getSetByCode(setCode);
if (expansionInfo != null) { if (expansionInfo != null) {
List<ExpansionInfo> blockSets = ExpansionRepository.instance.getSetsFromBlock(expansionInfo.getBlockName()); List<ExpansionInfo> blockSets = ExpansionRepository.instance.getSetsFromBlock(expansionInfo.getBlockName());
for (ExpansionInfo blockSet : blockSets) { for (ExpansionInfo blockSet : blockSets) {
if (blockSet.hasBasicLands()) { if (blockSet.hasBasicLands()) {
this.setCodesland.add(blockSet.getCode()); this.landSetCodes.add(blockSet.getCode());
landSets.add(blockSet.getName()); landSetNames.add(blockSet.getName());
} }
} }
} }
@ -99,19 +99,19 @@ public class AddLandDialog extends MageDialog {
} }
} }
// if still no set with lands found, add list of all available // if still no set with lands found, add list of all available
if (this.setCodesland.isEmpty()) { if (this.landSetCodes.isEmpty()) {
List<ExpansionInfo> basicLandSets = ExpansionRepository.instance.getSetsWithBasicLandsByReleaseDate(); List<ExpansionInfo> basicLandSets = ExpansionRepository.instance.getSetsWithBasicLandsByReleaseDate();
for (ExpansionInfo expansionInfo : basicLandSets) { for (ExpansionInfo expansionInfo : basicLandSets) {
landSets.add(expansionInfo.getName()); landSetNames.add(expansionInfo.getName());
} }
} }
if (landSets.isEmpty()) { if (landSetNames.isEmpty()) {
throw new IllegalArgumentException("No set with basic land was found"); throw new IllegalArgumentException("No set with basic land was found");
} }
if (landSets.size() > 1) { if (landSetNames.size() > 1) {
landSets.add("<Random lands>"); landSetNames.add("<Random lands>");
} }
cbLandSet.setModel(new DefaultComboBoxModel(landSets.toArray())); cbLandSet.setModel(new DefaultComboBoxModel(landSetNames.toArray()));
MageFrame.getDesktop().add(this, JLayeredPane.PALETTE_LAYER); MageFrame.getDesktop().add(this, JLayeredPane.PALETTE_LAYER);
this.setVisible(true); this.setVisible(true);
@ -123,7 +123,7 @@ public class AddLandDialog extends MageDialog {
CardCriteria criteria = new CardCriteria(); CardCriteria criteria = new CardCriteria();
if (landSetName.equals("<Random lands>")) { if (landSetName.equals("<Random lands>")) {
criteria.setCodes(setCodesland.toArray(new String[setCodesland.size()])); criteria.setCodes(landSetCodes.toArray(new String[landSetCodes.size()]));
} else { } else {
ExpansionInfo expansionInfo = ExpansionRepository.instance.getSetByName(landSetName); ExpansionInfo expansionInfo = ExpansionRepository.instance.getSetByName(landSetName);
if (expansionInfo == null) { if (expansionInfo == null) {

View file

@ -149,6 +149,7 @@ import mage.target.common.TargetPermanentOrPlayer;
import mage.target.common.TargetSpellOrPermanent; import mage.target.common.TargetSpellOrPermanent;
import mage.util.Copier; import mage.util.Copier;
import mage.util.MessageToClient; import mage.util.MessageToClient;
import mage.util.TournamentUtil;
import mage.util.TreeNode; import mage.util.TreeNode;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
@ -1637,42 +1638,7 @@ public class ComputerPlayer extends PlayerImpl implements Player {
private static void addBasicLands(Deck deck, String landName, int number) { private static void addBasicLands(Deck deck, String landName, int number) {
Random random = new Random(); Random random = new Random();
Set<String> landSets = new HashSet<>(); Set<String> landSets = TournamentUtil.getLandSetCodeForDeckSets(deck.getExpansionSetCodes());
// decide from which sets basic lands are taken from
for (String setCode : deck.getExpansionSetCodes()) {
ExpansionInfo expansionInfo = ExpansionRepository.instance.getSetByCode(setCode);
if (expansionInfo.hasBasicLands()) {
landSets.add(expansionInfo.getCode());
}
}
// if sets have no basic land, take land from block
if (landSets.isEmpty()) {
for (String setCode : deck.getExpansionSetCodes()) {
ExpansionInfo expansionInfo = ExpansionRepository.instance.getSetByCode(setCode);
List<ExpansionInfo> blockSets = ExpansionRepository.instance.getSetsFromBlock(expansionInfo.getBlockName());
for (ExpansionInfo blockSet : blockSets) {
if (blockSet.hasBasicLands()) {
landSets.add(blockSet.getCode());
}
}
}
}
// if still no set with lands found, take one by random
if (landSets.isEmpty()) {
// if sets have no basic lands and also it has no parent or parent has no lands get last set with lands
// select a set with basic lands by random
Random generator = new Random();
List<ExpansionInfo> basicLandSets = ExpansionRepository.instance.getSetsWithBasicLandsByReleaseDate();
if (basicLandSets.size() > 0) {
landSets.add(basicLandSets.get(generator.nextInt(basicLandSets.size())).getCode());
}
}
if (landSets.isEmpty()) {
throw new IllegalArgumentException("No set with basic land was found");
}
CardCriteria criteria = new CardCriteria(); CardCriteria criteria = new CardCriteria();
if (!landSets.isEmpty()) { if (!landSets.isEmpty()) {

View file

@ -130,15 +130,11 @@ public class Deck implements Serializable {
public Set<String> getExpansionSetCodes() { public Set<String> getExpansionSetCodes() {
Set<String> sets = new LinkedHashSet<>(); Set<String> sets = new LinkedHashSet<>();
for (Card card : getCards()) { for (Card card : getCards()) {
if (!sets.contains(card.getExpansionSetCode())) {
sets.add(card.getExpansionSetCode()); sets.add(card.getExpansionSetCode());
} }
}
for (Card card : getSideboard()) { for (Card card : getSideboard()) {
if (!sets.contains(card.getExpansionSetCode())) {
sets.add(card.getExpansionSetCode()); sets.add(card.getExpansionSetCode());
} }
}
return sets; return sets;
} }

View file

@ -7,10 +7,12 @@
package mage.util; package mage.util;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
import java.util.Set; import java.util.Set;
import mage.cards.Card; import mage.cards.Card;
import mage.cards.repository.CardCriteria; import mage.cards.repository.CardCriteria;
import mage.cards.repository.CardInfo; import mage.cards.repository.CardInfo;
@ -32,44 +34,44 @@ public class TournamentUtil {
* @return setCode for lands * @return setCode for lands
*/ */
public static Set<String> getLandSetCodeForDeckSets(Set<String> setCodesDeck) { public static Set<String> getLandSetCodeForDeckSets(Collection<String> setCodesDeck) {
Set<String> setCodesland = new HashSet<>(); Set<String> landSetCodes = new HashSet<>();
// decide from which sets basic lands are taken from // decide from which sets basic lands are taken from
for (String setCode :setCodesDeck) { for (String setCode :setCodesDeck) {
ExpansionInfo expansionInfo = ExpansionRepository.instance.getSetByCode(setCode); ExpansionInfo expansionInfo = ExpansionRepository.instance.getSetByCode(setCode);
if (expansionInfo.hasBasicLands()) { if (expansionInfo.hasBasicLands()) {
setCodesland.add(expansionInfo.getCode()); landSetCodes.add(expansionInfo.getCode());
} }
} }
// if sets have no basic land, take land from block // if sets have no basic land, take land from block
if (setCodesland.isEmpty()) { if (landSetCodes.isEmpty()) {
for (String setCode :setCodesDeck) { for (String setCode :setCodesDeck) {
ExpansionInfo expansionInfo = ExpansionRepository.instance.getSetByCode(setCode); ExpansionInfo expansionInfo = ExpansionRepository.instance.getSetByCode(setCode);
List<ExpansionInfo> blockSets = ExpansionRepository.instance.getSetsFromBlock(expansionInfo.getBlockName()); List<ExpansionInfo> blockSets = ExpansionRepository.instance.getSetsFromBlock(expansionInfo.getBlockName());
for (ExpansionInfo blockSet: blockSets) { for (ExpansionInfo blockSet: blockSets) {
if (blockSet.hasBasicLands()) { if (blockSet.hasBasicLands()) {
setCodesland.add(blockSet.getCode()); landSetCodes.add(blockSet.getCode());
} }
} }
} }
} }
// if still no set with lands found, take one by random // if still no set with lands found, take one by random
if (setCodesland.isEmpty()) { if (landSetCodes.isEmpty()) {
// if sets have no basic lands and also it has no parent or parent has no lands get last set with lands // if sets have no basic lands and also it has no parent or parent has no lands get last set with lands
// select a set with basic lands by random // select a set with basic lands by random
Random generator = new Random(); Random generator = new Random();
List<ExpansionInfo> basicLandSets = ExpansionRepository.instance.getSetsWithBasicLandsByReleaseDate(); List<ExpansionInfo> basicLandSets = ExpansionRepository.instance.getSetsWithBasicLandsByReleaseDate();
if (basicLandSets.size() > 0) { if (basicLandSets.size() > 0) {
setCodesland.add(basicLandSets.get(generator.nextInt(basicLandSets.size())).getCode()); landSetCodes.add(basicLandSets.get(generator.nextInt(basicLandSets.size())).getCode());
} }
} }
if (setCodesland.isEmpty()) { if (landSetCodes.isEmpty()) {
throw new IllegalArgumentException("No set with basic land was found"); throw new IllegalArgumentException("No set with basic land was found");
} }
return setCodesland; return landSetCodes;
} }
public static List<Card> getLands(String landName, int number, Set<String> landSets) { public static List<Card> getLands(String landName, int number, Set<String> landSets) {