CardUtil File format

This commit is contained in:
Eradev
2025-10-08 12:14:59 -04:00
committed by Chris H
parent 0ee3dc4e67
commit caf8b0535f

View File

@@ -37,32 +37,32 @@ import static forge.adventure.data.RewardData.generateAllCards;
*/ */
public class CardUtil { public class CardUtil {
public static final class CardPredicate implements Predicate<PaperCard> { public static final class CardPredicate implements Predicate<PaperCard> {
enum ColorType enum ColorType {
{
Any, Any,
Colorless, Colorless,
MultiColor, MultiColor,
MonoColor MonoColor
} }
private final List<CardRarity> rarities=new ArrayList<>();
private final List<String> editions=new ArrayList<>(); private final List<CardRarity> rarities = new ArrayList<>();
private final List<String> subType=new ArrayList<>(); private final List<String> editions = new ArrayList<>();
private final List<String> keyWords=new ArrayList<>(); private final List<String> subType = new ArrayList<>();
private final List<CardType.CoreType> type=new ArrayList<>(); private final List<String> keyWords = new ArrayList<>();
private final List<CardType.Supertype> superType=new ArrayList<>(); private final List<CardType.CoreType> type = new ArrayList<>();
private final List<Integer> manaCosts =new ArrayList<>(); private final List<CardType.Supertype> superType = new ArrayList<>();
private final List<Integer> manaCosts = new ArrayList<>();
private final Pattern text; private final Pattern text;
private final boolean matchAllSubTypes; private final boolean matchAllSubTypes;
private final boolean matchAllColors; private final boolean matchAllColors;
private int colors; private int colors;
private final ColorType colorType; private final ColorType colorType;
private final boolean shouldBeEqual; private final boolean shouldBeEqual;
private final List<String> deckNeeds=new ArrayList<>(); private final List<String> deckNeeds = new ArrayList<>();
private final String minDate; private final String minDate;
private final static SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); private final static SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
private static Date parseDate(String date) { private static Date parseDate(String date) {
if( date.length() <= 7 ) if (date.length() <= 7)
date = date + "-01"; date = date + "-01";
try { try {
return formatter.parse(date); return formatter.parse(date);
@@ -73,12 +73,12 @@ public class CardUtil {
@Override @Override
public boolean test(final PaperCard card) { public boolean test(final PaperCard card) {
if(!this.rarities.isEmpty()&&!this.rarities.contains(card.getRarity())) if (!this.rarities.isEmpty() && !this.rarities.contains(card.getRarity()))
return !this.shouldBeEqual; return !this.shouldBeEqual;
if(!this.editions.isEmpty()&&!this.editions.contains(card.getEdition())) { if (!this.editions.isEmpty() && !this.editions.contains(card.getEdition())) {
boolean found = false; boolean found = false;
List<PaperCard> allPrintings = FModel.getMagicDb().getCommonCards().getAllCards(card.getCardName()); List<PaperCard> allPrintings = FModel.getMagicDb().getCommonCards().getAllCards(card.getCardName());
for (PaperCard c : allPrintings){ for (PaperCard c : allPrintings) {
if (this.editions.contains(c.getEdition())) { if (this.editions.contains(c.getEdition())) {
found = true; found = true;
break; break;
@@ -87,7 +87,7 @@ public class CardUtil {
if (!found) if (!found)
return !this.shouldBeEqual; return !this.shouldBeEqual;
} }
if(!this.minDate.isEmpty()) { if (!this.minDate.isEmpty()) {
boolean found = false; boolean found = false;
List<PaperCard> allPrintings = FModel.getMagicDb().getCommonCards().getAllCards(card.getCardName()); List<PaperCard> allPrintings = FModel.getMagicDb().getCommonCards().getAllCards(card.getCardName());
List<CardEdition> cardEditionList = new ArrayList<>(); List<CardEdition> cardEditionList = new ArrayList<>();
@@ -100,7 +100,7 @@ public class CardUtil {
cardEditionList.add(e); cardEditionList.add(e);
} }
for (PaperCard c : allPrintings){ for (PaperCard c : allPrintings) {
for (CardEdition e : cardEditionList) { for (CardEdition e : cardEditionList) {
if (e.getCode().equals(c.getEdition())) { if (e.getCode().equals(c.getEdition())) {
found = true; found = true;
@@ -111,144 +111,120 @@ public class CardUtil {
if (!found) if (!found)
return !this.shouldBeEqual; return !this.shouldBeEqual;
} }
if(!this.manaCosts.isEmpty()&&!this.manaCosts.contains(card.getRules().getManaCost().getCMC())) if (!this.manaCosts.isEmpty() && !this.manaCosts.contains(card.getRules().getManaCost().getCMC()))
return !this.shouldBeEqual; return !this.shouldBeEqual;
if(this.text!=null&& !this.text.matcher(card.getRules().getOracleText()).find()) if (this.text != null && !this.text.matcher(card.getRules().getOracleText()).find())
return !this.shouldBeEqual; return !this.shouldBeEqual;
if(this.matchAllColors) if (this.matchAllColors) {
{ if (!card.getRules().getColor().hasAllColors(this.colors)) {
if(!card.getRules().getColor().hasAllColors(this.colors))
{
return !this.shouldBeEqual; return !this.shouldBeEqual;
} }
} }
if(this.colors!= MagicColor.ALL_COLORS) if (this.colors != MagicColor.ALL_COLORS) {
{ if (!card.getRules().getColor().hasNoColorsExcept(this.colors)
if(!card.getRules().getColor().hasNoColorsExcept(this.colors)||(this.colors != MagicColor.COLORLESS && card.getRules().getColor().isColorless())) || (this.colors != MagicColor.COLORLESS && card.getRules().getColor().isColorless()))
return !this.shouldBeEqual; return !this.shouldBeEqual;
} }
if(colorType!=ColorType.Any) if (colorType != ColorType.Any) {
{ switch (colorType) {
switch (colorType)
{
case Colorless: case Colorless:
if(!card.getRules().getColor().isColorless()) if (!card.getRules().getColor().isColorless())
return !this.shouldBeEqual; return !this.shouldBeEqual;
break; break;
case MonoColor: case MonoColor:
if(!card.getRules().getColor().isMonoColor()) if (!card.getRules().getColor().isMonoColor())
return !this.shouldBeEqual; return !this.shouldBeEqual;
break; break;
case MultiColor: case MultiColor:
if(!card.getRules().getColor().isMulticolor()) if (!card.getRules().getColor().isMulticolor())
return !this.shouldBeEqual; return !this.shouldBeEqual;
break; break;
} }
} }
if(!this.type.isEmpty()) if (!this.type.isEmpty()) {
{ boolean found = false;
boolean found=false; for (CardType.CoreType type : card.getRules().getType().getCoreTypes()) {
for(CardType.CoreType type:card.getRules().getType().getCoreTypes()) if (this.type.contains(type)) {
{ found = true;
if(this.type.contains(type))
{
found=true;
break; break;
} }
} }
if(!found) if (!found)
return !this.shouldBeEqual; return !this.shouldBeEqual;
} }
if(!this.superType.isEmpty()) if (!this.superType.isEmpty()) {
{ boolean found = false;
boolean found=false; for (CardType.Supertype type : card.getRules().getType().getSupertypes()) {
for(CardType.Supertype type:card.getRules().getType().getSupertypes()) if (this.superType.contains(type)) {
{ found = true;
if(this.superType.contains(type))
{
found=true;
break; break;
} }
} }
if(!found) if (!found)
return !this.shouldBeEqual; return !this.shouldBeEqual;
} }
if(this.matchAllSubTypes) if (this.matchAllSubTypes) {
{ if (!this.subType.isEmpty()) {
if(!this.subType.isEmpty()) if (this.subType.size() != Iterables.size(card.getRules().getType().getSubtypes()))
{
if(this.subType.size()!= Iterables.size(card.getRules().getType().getSubtypes()))
return !this.shouldBeEqual; return !this.shouldBeEqual;
for(String subtype:card.getRules().getType().getSubtypes()) for (String subtype : card.getRules().getType().getSubtypes()) {
{ if (!this.subType.contains(subtype)) {
if(!this.subType.contains(subtype))
{
return !this.shouldBeEqual; return !this.shouldBeEqual;
} }
} }
} }
} } else {
else if (!this.subType.isEmpty()) {
{ boolean found = false;
if(!this.subType.isEmpty()) for (String subtype : card.getRules().getType().getSubtypes()) {
{ if (this.subType.contains(subtype)) {
boolean found=false; found = true;
for(String subtype:card.getRules().getType().getSubtypes())
{
if(this.subType.contains(subtype))
{
found=true;
break; break;
} }
} }
if(!found) if (!found)
return !this.shouldBeEqual; return !this.shouldBeEqual;
} }
} }
if(!this.keyWords.isEmpty()) if (!this.keyWords.isEmpty()) {
{ boolean found = false;
boolean found=false; for (String keyWord : this.keyWords) {
for(String keyWord:this.keyWords) if (card.getRules().hasKeyword(keyWord)) {
{ found = true;
if(card.getRules().hasKeyword(keyWord))
{
found=true;
break; break;
} }
} }
if(!found) if (!found)
return !this.shouldBeEqual; return !this.shouldBeEqual;
} }
if(!this.deckNeeds.isEmpty()) if (!this.deckNeeds.isEmpty()) {
{
boolean found = false; boolean found = false;
for(String need:this.deckNeeds) for (String need : this.deckNeeds) {
{ // FormatExpected: X$Y, where X is DeckHints.Type and Y is a string descriptor
//FormatExpected: X$Y, where X is DeckHints.Type and Y is a string descriptor
String[] parts = need.split("\\$"); String[] parts = need.split("\\$");
if (parts.length != 2){ if (parts.length != 2) {
continue; continue;
} }
DeckHints.Type t = DeckHints.Type.valueOf(parts[0].toUpperCase()); DeckHints.Type t = DeckHints.Type.valueOf(parts[0].toUpperCase());
DeckHints hints = card.getRules().getAiHints().getDeckHints(); DeckHints hints = card.getRules().getAiHints().getDeckHints();
if (hints != null && hints.contains(t, parts[1])){ if (hints != null && hints.contains(t, parts[1])) {
found=true; found = true;
break; break;
} }
} }
if(!found) if (!found)
return !this.shouldBeEqual; return !this.shouldBeEqual;
} }
return this.shouldBeEqual; return this.shouldBeEqual;
} }
private Pattern getPattern(RewardData type) { private Pattern getPattern(RewardData type) {
if (type.cardText == null || type.cardText.isEmpty()) if (type.cardText == null || type.cardText.isEmpty())
return null; return null;
@@ -259,106 +235,89 @@ public class CardUtil {
return null; return null;
} }
} }
public CardPredicate(final RewardData type, final boolean wantEqual) { public CardPredicate(final RewardData type, final boolean wantEqual) {
this.matchAllSubTypes=type.matchAllSubTypes; this.matchAllSubTypes = type.matchAllSubTypes;
this.matchAllColors=type.matchAllColors; this.matchAllColors = type.matchAllColors;
this.shouldBeEqual = wantEqual; this.shouldBeEqual = wantEqual;
for(int i=0;type.manaCosts!=null&&i<type.manaCosts.length;i++) for (int i = 0; type.manaCosts != null && i < type.manaCosts.length; i++)
manaCosts.add(type.manaCosts[i]); manaCosts.add(type.manaCosts[i]);
text = getPattern(type); text = getPattern(type);
if(type.colors==null||type.colors.length==0) if (type.colors == null || type.colors.length == 0) {
{ this.colors = MagicColor.ALL_COLORS;
this.colors=MagicColor.ALL_COLORS; } else {
} this.colors = 0;
else for (String color : type.colors) {
{ if ("colorID".equals(color))
this.colors=0;
for(String color:type.colors)
{
if("colorID".equals(color))
for (byte c : Current.player().getColorIdentity()) for (byte c : Current.player().getColorIdentity())
colors |= c; colors |= c;
else else
colors |= MagicColor.fromName(color.toLowerCase()); colors |= MagicColor.fromName(color.toLowerCase());
} }
} }
if(type.keyWords!=null&&type.keyWords.length!=0) if (type.keyWords != null && type.keyWords.length != 0) {
{
keyWords.addAll(Arrays.asList(type.keyWords)); keyWords.addAll(Arrays.asList(type.keyWords));
} }
if(type.rarity!=null&&type.rarity.length!=0) if (type.rarity != null) {
{ for (String rarity : type.rarity) {
for(String rarity:type.rarity)
{
rarities.add(CardRarity.smartValueOf(rarity)); rarities.add(CardRarity.smartValueOf(rarity));
} }
} }
if(type.subTypes!=null&&type.subTypes.length!=0) if (type.subTypes != null && type.subTypes.length != 0) {
{
subType.addAll(Arrays.asList(type.subTypes)); subType.addAll(Arrays.asList(type.subTypes));
} }
if(type.editions!=null&&type.editions.length!=0) if (type.editions != null && type.editions.length != 0) {
{
editions.addAll(Arrays.asList(type.editions)); editions.addAll(Arrays.asList(type.editions));
} }
if(type.superTypes!=null&&type.superTypes.length!=0) if (type.superTypes != null) {
{ for (String string : type.superTypes)
for(String string:type.superTypes)
superType.add(CardType.Supertype.getEnum(string)); superType.add(CardType.Supertype.getEnum(string));
} }
if(type.cardTypes!=null&&type.cardTypes.length!=0) if (type.cardTypes != null) {
{ for (String string : type.cardTypes)
for(String string:type.cardTypes)
this.type.add(CardType.CoreType.getEnum(string)); this.type.add(CardType.CoreType.getEnum(string));
} }
if(type.colorType!=null&&!type.colorType.isEmpty()) if (type.colorType != null && !type.colorType.isEmpty()) {
{ this.colorType = ColorType.valueOf(type.colorType);
this.colorType=ColorType.valueOf(type.colorType); } else {
this.colorType = ColorType.Any;
} }
else if (type.deckNeeds != null && type.deckNeeds.length != 0) {
{
this.colorType=ColorType.Any;
}
if(type.deckNeeds!=null&&type.deckNeeds.length!=0){
deckNeeds.addAll(Arrays.asList(type.deckNeeds)); deckNeeds.addAll(Arrays.asList(type.deckNeeds));
} }
if(type.minDate!=null&&!type.minDate.isEmpty()) if (type.minDate != null && !type.minDate.isEmpty()) {
{ this.minDate = type.minDate;
this.minDate=type.minDate; } else {
} this.minDate = "";
else
{
this.minDate="";
} }
} }
} }
public static List<PaperCard> getPredicateResult(Iterable<PaperCard> cards,final RewardData data) public static List<PaperCard> getPredicateResult(Iterable<PaperCard> cards, final RewardData data) {
{
List<PaperCard> result = new ArrayList<>(); List<PaperCard> result = new ArrayList<>();
CardPredicate pre = new CardPredicate(data, true); CardPredicate pre = new CardPredicate(data, true);
for (final PaperCard item : cards) for (final PaperCard item : cards) {
{ if (pre.test(item))
if(pre.test(item))
result.add(item); result.add(item);
} }
return result; return result;
} }
public static List<PaperCard> generateCards(Iterable<PaperCard> cards,final RewardData data, final int count, Random r) public static List<PaperCard> generateCards(Iterable<PaperCard> cards, final RewardData data, final int count,
{ Random r) {
boolean allCardVariants = Config.instance().getSettingData().useAllCardVariants; boolean allCardVariants = Config.instance().getSettingData().useAllCardVariants;
final List<PaperCard> result = new ArrayList<>(); final List<PaperCard> result = new ArrayList<>();
List<PaperCard> pool = getPredicateResult(cards, data); List<PaperCard> pool = getPredicateResult(cards, data);
if (pool.size() > 0) { if (!pool.isEmpty()) {
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
PaperCard candidate = pool.get(r.nextInt(pool.size())); PaperCard candidate = pool.get(r.nextInt(pool.size()));
if (candidate != null) { if (candidate != null) {
if (allCardVariants) { if (allCardVariants) {
PaperCard finalCandidate = CardUtil.getCardByName(candidate.getCardName()); // get a random set variant // Get a random set variant
PaperCard finalCandidate = CardUtil.getCardByName(candidate.getCardName());
result.add(finalCandidate); result.add(finalCandidate);
} else { } else {
result.add(candidate); result.add(candidate);
@@ -368,113 +327,102 @@ public class CardUtil {
} }
return result; return result;
} }
public static int getCardPrice(PaperCard card)
{
if(card==null)
return 0;
switch (card.getRarity())
{
case BasicLand:
return 5;
case Common:
return 50;
case Uncommon:
return 150;
case Rare:
return 300;
case MythicRare:
return 500;
default:
return 600;
}
}
public static int getRewardPrice(Reward reward)
{
PaperCard card=reward.getCard();
if(card!=null)
return getCardPrice(card);
if(reward.getItem()!=null)
return reward.getItem().cost;
if(reward.getType()== Reward.Type.Life)
return reward.getCount()*500;
if(reward.getType()== Reward.Type.Shards)
return reward.getCount()*500;
if(reward.getType()== Reward.Type.Gold)
return reward.getCount();
/*if(reward.getType() == Reward.Type.CardPack)
return reward.getDeck().get(DeckSection.Main).countAll()*70;*/
//TODO: Heitor - Price by card count and type of boosterPack.
public static int getCardPrice(PaperCard card) {
if (card == null)
return 0;
return switch (card.getRarity()) {
case BasicLand -> 5;
case Common -> 50;
case Uncommon -> 150;
case Rare -> 300;
case MythicRare -> 500;
default -> 600;
};
}
public static int getRewardPrice(Reward reward) {
PaperCard card = reward.getCard();
if (card != null)
return getCardPrice(card);
if (reward.getItem() != null)
return reward.getItem().cost;
if (reward.getType() == Reward.Type.Life)
return reward.getCount() * 500;
if (reward.getType() == Reward.Type.Shards)
return reward.getCount() * 500;
if (reward.getType() == Reward.Type.Gold)
return reward.getCount();
/*
* if(reward.getType() == Reward.Type.CardPack)
* return reward.getDeck().get(DeckSection.Main).countAll()*70;
*/
// TODO: Heitor - Price by card count and type of boosterPack.
return 1000; return 1000;
} }
public static Deck generateDeck(GeneratedDeckData data, CardEdition starterEdition, boolean discourageDuplicates) public static Deck generateDeck(GeneratedDeckData data, CardEdition starterEdition, boolean discourageDuplicates) {
{ List<String> editionCodes = (starterEdition != null)
List<String> editionCodes = (starterEdition != null)?Arrays.asList(starterEdition.getCode(), starterEdition.getCode2()):Arrays.asList("JMP", "J22", "DMU", "BRO", "ONE", "MOM"); ? Arrays.asList(starterEdition.getCode(), starterEdition.getCode2())
Deck deck= new Deck(data.name); : Arrays.asList("JMP", "J22", "DMU", "BRO", "ONE", "MOM");
if(data.mainDeck!=null) Deck deck = new Deck(data.name);
{ if (data.mainDeck != null) {
deck.getOrCreate(DeckSection.Main).addAllFlat(generateAllCards(Arrays.asList(data.mainDeck), true)); deck.getOrCreate(DeckSection.Main).addAllFlat(generateAllCards(Arrays.asList(data.mainDeck), true));
if(data.sideBoard!=null) if (data.sideBoard != null)
deck.getOrCreate(DeckSection.Sideboard).addAllFlat(generateAllCards(Arrays.asList(data.sideBoard), true)); deck.getOrCreate(DeckSection.Sideboard)
.addAllFlat(generateAllCards(Arrays.asList(data.sideBoard), true));
return deck; return deck;
} }
if(data.jumpstartPacks!=null) if (data.jumpstartPacks != null) {
{
deck.getOrCreate(DeckSection.Main); deck.getOrCreate(DeckSection.Main);
Map <String, List<PaperCard>> packCandidates=null; Map<String, List<PaperCard>> packCandidates = null;
List<String> usedPackNames=new ArrayList<String>(); List<String> usedPackNames = new ArrayList<String>();
for(int i=0;i<data.jumpstartPacks.length;i++)
{
for (int i = 0; i < data.jumpstartPacks.length; i++) {
final byte targetColor = MagicColor.fromName(data.jumpstartPacks[i]); final byte targetColor = MagicColor.fromName(data.jumpstartPacks[i]);
String targetName; String targetName = switch (targetColor) {
switch (targetColor) case MagicColor.BLUE -> "Island";
{ case MagicColor.BLACK -> "Swamp";
default: case MagicColor.RED -> "Mountain";
case MagicColor.WHITE: targetName = "Plains"; break; case MagicColor.GREEN -> "Forest";
case MagicColor.BLUE: targetName = "Island"; break; default -> "Plains";
case MagicColor.BLACK: targetName = "Swamp"; break; };
case MagicColor.RED: targetName = "Mountain";break;
case MagicColor.GREEN: targetName = "Forest"; break;
}
packCandidates=new HashMap<>(); packCandidates = new HashMap<>();
for(SealedTemplate template : StaticData.instance().getSpecialBoosters()) for (SealedTemplate template : StaticData.instance().getSpecialBoosters()) {
{ if (!editionCodes.contains(template.getEdition().split("\\s", 2)[0]))
if (!editionCodes.contains(template.getEdition().split("\\s",2)[0]))
continue; continue;
List<PaperCard> packContents = new UnOpenedProduct(template).get(); List<PaperCard> packContents = new UnOpenedProduct(template).get();
if (packContents.size() < 18 | packContents.size() > 25) if (packContents.size() < 18 | packContents.size() > 25)
continue; continue;
if (packContents.stream().filter(x -> x.getName().equals(targetName)).count() >=3) if (packContents.stream().filter(x -> x.getName().equals(targetName)).count() >= 3)
packCandidates.putIfAbsent(template.getEdition(), packContents); packCandidates.putIfAbsent(template.getEdition(), packContents);
} }
List<PaperCard> selectedPack; List<PaperCard> selectedPack;
if (discourageDuplicates) { if (discourageDuplicates) {
Map <String, List<PaperCard>> filteredPackCandidates= new HashMap<>(); Map<String, List<PaperCard>> filteredPackCandidates = new HashMap<>();
for (java.util.Map.Entry<String, List<PaperCard>> entry: packCandidates.entrySet()){ for (java.util.Map.Entry<String, List<PaperCard>> entry : packCandidates.entrySet()) {
if (!usedPackNames.contains(entry.getKey())){ if (!usedPackNames.contains(entry.getKey())) {
filteredPackCandidates.put(entry.getKey(), entry.getValue()); //deep copy so that packCandidates can be used if filtered ends up being empty // Deep copy so that packCandidates can be used if filtered ends up being empty
filteredPackCandidates.put(entry.getKey(), entry.getValue());
} }
} }
//Only re-use a pack if all possibilities have already been chosen // Only re-use a pack if all possibilities have already been chosen
if (filteredPackCandidates.size() == 0) if (filteredPackCandidates.isEmpty())
filteredPackCandidates = packCandidates; filteredPackCandidates = packCandidates;
Object[] keys = filteredPackCandidates.keySet().toArray(); Object[] keys = filteredPackCandidates.keySet().toArray();
String keyName = (String)keys[Current.world().getRandom().nextInt(keys.length)]; String keyName = (String) keys[Current.world().getRandom().nextInt(keys.length)];
usedPackNames.add(keyName); usedPackNames.add(keyName);
selectedPack = filteredPackCandidates.remove(keyName); selectedPack = filteredPackCandidates.remove(keyName);
} } else {
else{
Object[] keys = packCandidates.keySet().toArray(); Object[] keys = packCandidates.keySet().toArray();
selectedPack = packCandidates.get((String)keys[Current.world().getRandom().nextInt(keys.length)]); selectedPack = packCandidates.get((String) keys[Current.world().getRandom().nextInt(keys.length)]);
} }
//if the packContents size above is below 20, just get random card // If the packContents size above is below 20, add random cards
int size = 20 - selectedPack.size(); int size = 20 - selectedPack.size();
for (int c = 0; c < size; c++) { for (int c = 0; c < size; c++) {
selectedPack.add(Aggregates.random(selectedPack)); selectedPack.add(Aggregates.random(selectedPack));
@@ -483,19 +431,18 @@ public class CardUtil {
} }
return deck; return deck;
} }
if(data.template!=null) if (data.template != null) {
{ float count = data.template.count;
float count=data.template.count; float lands = count * 0.4f;
float lands=count*0.4f; float spells = count - lands;
float spells=count-lands; List<RewardData> dataArray = generateRewards(data.template, spells * 0.5f, new int[] { 1, 2 });
List<RewardData> dataArray= generateRewards(data.template,spells*0.5f,new int[]{1,2}); dataArray.addAll(generateRewards(data.template, spells * 0.3f, new int[] { 3, 4, 5 }));
dataArray.addAll(generateRewards(data.template,spells*0.3f,new int[]{3,4,5})); dataArray.addAll(generateRewards(data.template, spells * 0.2f, new int[] { 6, 7, 8 }));
dataArray.addAll(generateRewards(data.template,spells*0.2f,new int[]{6,7,8})); List<PaperCard> nonLand = generateAllCards(dataArray, true);
List<PaperCard> nonLand= generateAllCards(dataArray, true);
nonLand.addAll(fillWithLands(nonLand,data.template)); nonLand.addAll(fillWithLands(nonLand, data.template));
deck.getOrCreate(DeckSection.Main).addAllFlat(nonLand); deck.getOrCreate(DeckSection.Main).addAllFlat(nonLand);
} }
return deck; return deck;
} }
@@ -577,102 +524,93 @@ public class CardUtil {
return cards; return cards;
} }
private static Collection<PaperCard> generateDualLands(List<String> landName, int count) { private static Collection<PaperCard> generateDualLands(List<String> landName, int count) {
ArrayList<RewardData> rewards=new ArrayList<>(); ArrayList<RewardData> rewards = new ArrayList<>();
RewardData base= new RewardData(); RewardData base = new RewardData();
rewards.add(base); rewards.add(base);
base.cardTypes=new String[]{"Land"}; base.cardTypes = new String[] { "Land" };
base.count=count; base.count = count;
base.matchAllSubTypes=true; base.matchAllSubTypes = true;
if(landName.size()==1) if (landName.size() == 1) {
{ base.subTypes = new String[] { landName.get(0) };
base.subTypes=new String[]{landName.get(0)}; } else if (landName.size() == 2) {
} base.subTypes = new String[] { landName.get(0), landName.get(1) };
else if(landName.size()==2) } else if (landName.size() == 3) {
{ RewardData sub1 = new RewardData(base);
base.subTypes=new String[]{landName.get(0),landName.get(1)}; RewardData sub2 = new RewardData(base);
} sub1.count /= 3;
else if(landName.size()==3) sub2.count /= 3;
{ base.count -= sub1.count;
RewardData sub1= new RewardData(base); base.count -= sub2.count;
RewardData sub2= new RewardData(base);
sub1.count/=3;
sub2.count/=3;
base.count-=sub1.count;
base.count-=sub2.count;
base.subTypes=new String[]{landName.get(0),landName.get(1)}; base.subTypes = new String[] { landName.get(0), landName.get(1) };
sub1.subTypes=new String[]{landName.get(1),landName.get(2)}; sub1.subTypes = new String[] { landName.get(1), landName.get(2) };
sub2.subTypes=new String[]{landName.get(0),landName.get(2)}; sub2.subTypes = new String[] { landName.get(0), landName.get(2) };
rewards.addAll(Arrays.asList(sub1,sub2)); rewards.addAll(Arrays.asList(sub1, sub2));
} } else if (landName.size() == 4) {
else if(landName.size()==4) RewardData sub1 = new RewardData(base);
{ RewardData sub2 = new RewardData(base);
RewardData sub1= new RewardData(base); RewardData sub3 = new RewardData(base);
RewardData sub2= new RewardData(base); RewardData sub4 = new RewardData(base);
RewardData sub3= new RewardData(base); sub1.count /= 5;
RewardData sub4= new RewardData(base); sub2.count /= 5;
sub1.count/=5; sub3.count /= 5;
sub2.count/=5; sub4.count /= 5;
sub3.count/=5; base.count -= sub1.count;
sub4.count/=5; base.count -= sub2.count;
base.count-=sub1.count; base.count -= sub3.count;
base.count-=sub2.count; base.count -= sub4.count;
base.count-=sub3.count;
base.count-=sub4.count;
base.subTypes = new String[]{landName.get(0),landName.get(1)}; base.subTypes = new String[] { landName.get(0), landName.get(1) };
sub1.subTypes = new String[]{landName.get(0),landName.get(2)}; sub1.subTypes = new String[] { landName.get(0), landName.get(2) };
sub2.subTypes = new String[]{landName.get(0),landName.get(3)}; sub2.subTypes = new String[] { landName.get(0), landName.get(3) };
sub3.subTypes = new String[]{landName.get(1),landName.get(2)}; sub3.subTypes = new String[] { landName.get(1), landName.get(2) };
sub4.subTypes = new String[]{landName.get(1),landName.get(3)}; sub4.subTypes = new String[] { landName.get(1), landName.get(3) };
rewards.addAll(Arrays.asList(sub1,sub2,sub3,sub4)); rewards.addAll(Arrays.asList(sub1, sub2, sub3, sub4));
} } else if (landName.size() == 5) {
else if(landName.size()==5) RewardData sub1 = new RewardData(base);
{ RewardData sub2 = new RewardData(base);
RewardData sub1= new RewardData(base); RewardData sub3 = new RewardData(base);
RewardData sub2= new RewardData(base); RewardData sub4 = new RewardData(base);
RewardData sub3= new RewardData(base); RewardData sub5 = new RewardData(base);
RewardData sub4= new RewardData(base); RewardData sub6 = new RewardData(base);
RewardData sub5= new RewardData(base); RewardData sub7 = new RewardData(base);
RewardData sub6= new RewardData(base); RewardData sub8 = new RewardData(base);
RewardData sub7= new RewardData(base); RewardData sub9 = new RewardData(base);
RewardData sub8= new RewardData(base); sub1.count /= 10;
RewardData sub9= new RewardData(base); sub2.count /= 10;
sub1.count/=10; sub3.count /= 10;
sub2.count/=10; sub4.count /= 10;
sub3.count/=10; sub5.count /= 10;
sub4.count/=10; sub6.count /= 10;
sub5.count/=10; sub7.count /= 10;
sub6.count/=10; sub8.count /= 10;
sub7.count/=10; sub9.count /= 10;
sub8.count/=10; base.count -= sub1.count;
sub9.count/=10; base.count -= sub2.count;
base.count-=sub1.count; base.count -= sub3.count;
base.count-=sub2.count; base.count -= sub4.count;
base.count-=sub3.count; base.count -= sub5.count;
base.count-=sub4.count; base.count -= sub6.count;
base.count-=sub5.count; base.count -= sub7.count;
base.count-=sub6.count; base.count -= sub8.count;
base.count-=sub7.count; base.count -= sub9.count;
base.count-=sub8.count;
base.count-=sub9.count;
base.subTypes=new String[]{landName.get(0),landName.get(1)}; base.subTypes = new String[] { landName.get(0), landName.get(1) };
sub1.subTypes=new String[]{landName.get(0),landName.get(2)}; sub1.subTypes = new String[] { landName.get(0), landName.get(2) };
sub2.subTypes=new String[]{landName.get(0),landName.get(3)}; sub2.subTypes = new String[] { landName.get(0), landName.get(3) };
sub3.subTypes=new String[]{landName.get(0),landName.get(4)}; sub3.subTypes = new String[] { landName.get(0), landName.get(4) };
sub4.subTypes=new String[]{landName.get(1),landName.get(2)}; sub4.subTypes = new String[] { landName.get(1), landName.get(2) };
sub5.subTypes=new String[]{landName.get(1),landName.get(3)}; sub5.subTypes = new String[] { landName.get(1), landName.get(3) };
sub6.subTypes=new String[]{landName.get(1),landName.get(4)}; sub6.subTypes = new String[] { landName.get(1), landName.get(4) };
sub7.subTypes=new String[]{landName.get(2),landName.get(3)}; sub7.subTypes = new String[] { landName.get(2), landName.get(3) };
sub8.subTypes=new String[]{landName.get(2),landName.get(4)}; sub8.subTypes = new String[] { landName.get(2), landName.get(4) };
sub9.subTypes=new String[]{landName.get(3),landName.get(4)}; sub9.subTypes = new String[] { landName.get(3), landName.get(4) };
rewards.addAll(Arrays.asList(sub1,sub2,sub3,sub4,sub5,sub6,sub7,sub8,sub9)); rewards.addAll(Arrays.asList(sub1, sub2, sub3, sub4, sub5, sub6, sub7, sub8, sub9));
} }
Collection<PaperCard> ret = new ArrayList<>(generateAllCards(rewards, true)); return new ArrayList<>(generateAllCards(rewards, true));
return ret;
} }
private static Collection<PaperCard> generateLands(String landName, int count) { private static Collection<PaperCard> generateLands(String landName, int count) {
@@ -700,43 +638,44 @@ public class CardUtil {
} }
private static List<RewardData> generateRewards(GeneratedDeckTemplateData template, float count, int[] manaCosts) { private static List<RewardData> generateRewards(GeneratedDeckTemplateData template, float count, int[] manaCosts) {
ArrayList<RewardData> ret=new ArrayList<>(); ArrayList<RewardData> ret = new ArrayList<>();
ret.addAll(templateGenerate(template,count-(count*template.rares),manaCosts,new String[]{"Uncommon","Common"})); ret.addAll(templateGenerate(template, count - (count * template.rares), manaCosts,
ret.addAll(templateGenerate(template,count*template.rares,manaCosts,new String[]{"Rare","Mythic Rare"})); new String[] { "Uncommon", "Common" }));
ret.addAll(
templateGenerate(template, count * template.rares, manaCosts, new String[] { "Rare", "Mythic Rare" }));
return ret; return ret;
} }
private static ArrayList<RewardData> templateGenerate(GeneratedDeckTemplateData template, float count, int[] manaCosts, String[] strings) { private static ArrayList<RewardData> templateGenerate(GeneratedDeckTemplateData template, float count,
ArrayList<RewardData> ret=new ArrayList<>(); int[] manaCosts, String[] strings) {
RewardData base= new RewardData(); ArrayList<RewardData> ret = new ArrayList<>();
base.manaCosts=manaCosts; RewardData base = new RewardData();
base.rarity=strings; base.manaCosts = manaCosts;
base.colors=template.colors; base.rarity = strings;
if(template.tribe!=null&&!template.tribe.isEmpty()) base.colors = template.colors;
{ if (template.tribe != null && !template.tribe.isEmpty()) {
RewardData caresAbout= new RewardData(base); RewardData caresAbout = new RewardData(base);
caresAbout.cardText="\\b"+template.tribe+"\\b"; caresAbout.cardText = "\\b" + template.tribe + "\\b";
caresAbout.count= Math.round(count*template.tribeSynergyCards); caresAbout.count = Math.round(count * template.tribeSynergyCards);
ret.add(caresAbout); ret.add(caresAbout);
base.subTypes=new String[]{template.tribe}; base.subTypes = new String[] { template.tribe };
base.count= Math.round(count*(1-template.tribeSynergyCards)); base.count = Math.round(count * (1 - template.tribeSynergyCards));
} } else {
else base.count = Math.round(count);
{
base.count= Math.round(count);
} }
ret.add(base); ret.add(base);
return ret; return ret;
} }
public static Deck getDeck(String path, boolean forAI, boolean isFantasyMode, String colors, boolean isTheme, boolean useGeneticAI) { public static Deck getDeck(String path, boolean forAI, boolean isFantasyMode, String colors, boolean isTheme,
return getDeck(path, forAI, isFantasyMode, colors, isTheme, useGeneticAI, null,true); boolean useGeneticAI) {
return getDeck(path, forAI, isFantasyMode, colors, isTheme, useGeneticAI, null, true);
} }
public static Deck getDeck(String path, boolean forAI, boolean isFantasyMode, String colors, boolean isTheme, boolean useGeneticAI, CardEdition starterEdition, boolean discourageDuplicates) public static Deck getDeck(String path, boolean forAI, boolean isFantasyMode, String colors, boolean isTheme,
{ boolean useGeneticAI, CardEdition starterEdition, boolean discourageDuplicates) {
if(path.endsWith(".dck")) { if (path.endsWith(".dck")) {
FileHandle fileHandle = Config.instance().getFile(path); FileHandle fileHandle = Config.instance().getFile(path);
Deck deck = null; Deck deck = null;
if (fileHandle != null) { if (fileHandle != null) {
@@ -749,12 +688,10 @@ public class CardUtil {
return deck; return deck;
} }
if(forAI && (isFantasyMode||useGeneticAI)) { if (forAI && (isFantasyMode || useGeneticAI)) {
Deck deck = DeckgenUtil.getRandomOrPreconOrThemeDeck(colors, forAI, isTheme, useGeneticAI); return DeckgenUtil.getRandomOrPreconOrThemeDeck(colors, forAI, isTheme, useGeneticAI);
if (deck != null)
return deck;
} }
Json json = new Json(); Json json = new Json();
FileHandle handle = Config.instance().getFile(path); FileHandle handle = Config.instance().getFile(path);
if (handle.exists()) if (handle.exists())
@@ -764,41 +701,41 @@ public class CardUtil {
return deck; return deck;
} }
private static final GameFormat.Collection formats = FModel.getFormats(); private static final GameFormat.Collection formats = FModel.getFormats();
private static final Predicate<CardEdition> filterPioneer = formats.getPioneer().editionLegalPredicate; private static final Predicate<CardEdition> filterPioneer = formats.getPioneer().editionLegalPredicate;
private static final Predicate<CardEdition> filterModern= formats.getModern().editionLegalPredicate; private static final Predicate<CardEdition> filterModern = formats.getModern().editionLegalPredicate;
private static final Predicate<CardEdition> filterVintage = formats.getVintage().editionLegalPredicate; private static final Predicate<CardEdition> filterVintage = formats.getVintage().editionLegalPredicate;
private static final Predicate<CardEdition> filterStandard = formats.getStandard().editionLegalPredicate; private static final Predicate<CardEdition> filterStandard = formats.getStandard().editionLegalPredicate;
public static Deck generateStandardBoosterAsDeck(){
public static Deck generateStandardBoosterAsDeck() {
return generateRandomBoosterPackAsDeck(filterStandard); return generateRandomBoosterPackAsDeck(filterStandard);
} }
public static Deck generatePioneerBoosterAsDeck(){
public static Deck generatePioneerBoosterAsDeck() {
return generateRandomBoosterPackAsDeck(filterPioneer); return generateRandomBoosterPackAsDeck(filterPioneer);
} }
public static Deck generateModernBoosterAsDeck(){
public static Deck generateModernBoosterAsDeck() {
return generateRandomBoosterPackAsDeck(filterModern); return generateRandomBoosterPackAsDeck(filterModern);
} }
public static Deck generateVintageBoosterAsDeck(){
public static Deck generateVintageBoosterAsDeck() {
return generateRandomBoosterPackAsDeck(filterVintage); return generateRandomBoosterPackAsDeck(filterVintage);
} }
public static Deck generateBoosterPackAsDeck(String code){ public static Deck generateBoosterPackAsDeck(String code) {
ConfigData configData = Config.instance().getConfigData(); ConfigData configData = Config.instance().getConfigData();
if (configData.allowedEditions != null) { if (configData.allowedEditions != null) {
if (!Arrays.asList(configData.allowedEditions).contains(code)){ if (!Arrays.asList(configData.allowedEditions).contains(code)) {
System.err.println("Cannot generate booster pack, '" + code + "' is not an allowed edition"); System.err.println("Cannot generate booster pack, '" + code + "' is not an allowed edition");
} }
} else if (Arrays.asList(configData.restrictedEditions).contains(code)){ } else if (Arrays.asList(configData.restrictedEditions).contains(code)) {
System.err.println("Cannot generate booster pack, '" + code + "' is a restricted edition"); System.err.println("Cannot generate booster pack, '" + code + "' is a restricted edition");
} }
CardEdition edition = StaticData.instance().getEditions().get(code); CardEdition edition = StaticData.instance().getEditions().get(code);
if (edition == null){ if (edition == null) {
System.err.println("Set code '" + code + "' not found."); System.err.println("Set code '" + code + "' not found.");
return new Deck(); return new Deck();
} }
@@ -806,7 +743,7 @@ public class CardUtil {
return generateBoosterPackAsDeck(edition); return generateBoosterPackAsDeck(edition);
} }
public static Deck generateBoosterPackAsDeck(CardEdition edition){ public static Deck generateBoosterPackAsDeck(CardEdition edition) {
Deck d = new Deck("Booster pack"); Deck d = new Deck("Booster pack");
d.setComment(edition.getCode()); d.setComment(edition.getCode());
d.getMain().add(BoosterPack.fromSet(edition).getCards()); d.getMain().add(BoosterPack.fromSet(edition).getCards());
@@ -833,7 +770,7 @@ public class CardUtil {
public static PaperCard getCardByName(String cardName) { public static PaperCard getCardByName(String cardName) {
List<PaperCard> validCards; List<PaperCard> validCards;
//Faster to ask the CardDB for a card name than it is to search the pool. // Faster to ask the CardDB for a card name than it is to search the pool.
if (Config.instance().getSettingData().useAllCardVariants) if (Config.instance().getSettingData().useAllCardVariants)
validCards = FModel.getMagicDb().getCommonCards().getAllCards(cardName); validCards = FModel.getMagicDb().getCommonCards().getAllCards(cardName);
else else
@@ -854,7 +791,9 @@ public class CardUtil {
.filter(input -> input.getEdition().equals(edition)).collect(Collectors.toList()); .filter(input -> input.getEdition().equals(edition)).collect(Collectors.toList());
if (validCards.isEmpty()) { if (validCards.isEmpty()) {
System.err.println("Unexpected behavior: tried to call getCardByNameAndEdition for card " + cardName + " from the edition " + edition + ", but didn't find it in the DB. A random existing instance will be returned if found."); System.err.println("Unexpected behavior: tried to call getCardByNameAndEdition for card " + cardName
+ " from the edition " + edition
+ ", but didn't find it in the DB. A random existing instance will be returned if found.");
return getCardByName(cardName); return getCardByName(cardName);
} }
@@ -862,7 +801,8 @@ public class CardUtil {
} }
public static Collection<PaperCard> getFullCardPool(boolean allCardVariants) { public static Collection<PaperCard> getFullCardPool(boolean allCardVariants) {
return allCardVariants ? FModel.getMagicDb().getCommonCards().getAllCards() : FModel.getMagicDb().getCommonCards().getUniqueCardsNoAlt(); return allCardVariants
? FModel.getMagicDb().getCommonCards().getAllCards()
: FModel.getMagicDb().getCommonCards().getUniqueCardsNoAlt();
} }
} }