Implement new way to generate boosters using box mapping info (WIP) (#7529)

* [THB] added initial common/uncommon collation mechanism

* [THB] added rare/mythic and lands to pack generation

* fixed some card names

* broke out collation into its own separate classes

* built collation into ExpansionSet

* added note about collation information

* [KHM] added collation info

* updated collation to use collector number rather than name

* added shuffle to set constructor

* added some notes on collation methods
This commit is contained in:
Evan Kranzler 2021-02-12 17:35:28 -05:00 committed by GitHub
parent 10e557b873
commit 8a16eda062
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 500 additions and 3 deletions

View file

@ -6,6 +6,7 @@ import mage.abilities.keyword.PartnerWithAbility;
import mage.cards.repository.CardCriteria;
import mage.cards.repository.CardInfo;
import mage.cards.repository.CardRepository;
import mage.collation.BoosterCollator;
import mage.constants.Rarity;
import mage.constants.SetType;
import mage.util.CardUtil;
@ -26,7 +27,7 @@ public abstract class ExpansionSet implements Serializable {
public static final CardGraphicInfo FULL_ART_BFZ_VARIOUS = new CardGraphicInfo(FrameStyle.BFZ_FULL_ART_BASIC, true);
public static final CardGraphicInfo FULL_ART_ZEN_VARIOUS = new CardGraphicInfo(FrameStyle.ZEN_FULL_ART_BASIC, true);
public class SetCardInfo implements Serializable {
public static class SetCardInfo implements Serializable {
private final String name;
private final String cardNumber;
@ -92,6 +93,7 @@ public abstract class ExpansionSet implements Serializable {
protected Date releaseDate;
protected ExpansionSet parentSet;
protected SetType setType;
protected BoosterCollator boosterCollator;
// TODO: 03.10.2018, hasBasicLands can be removed someday -- it's uses to optimize lands search in deck generation and lands adding (search all available lands from sets)
protected boolean hasBasicLands = true;
@ -120,14 +122,23 @@ public abstract class ExpansionSet implements Serializable {
protected int maxCardNumberInBooster; // used to omit cards with collector numbers beyond the regular cards in a set for boosters
protected final EnumMap<Rarity, List<CardInfo>> savedCards;
protected final Map<Integer, CardInfo> inBoosterMap = new HashMap<>();
public ExpansionSet(String name, String code, Date releaseDate, SetType setType) {
this(name, code, releaseDate, setType, null);
}
public ExpansionSet(String name, String code, Date releaseDate, SetType setType, BoosterCollator boosterCollator) {
this.name = name;
this.code = code;
this.releaseDate = releaseDate;
this.setType = setType;
this.maxCardNumberInBooster = Integer.MAX_VALUE;
savedCards = new EnumMap<>(Rarity.class);
this.boosterCollator = boosterCollator;
if (this.boosterCollator != null) {
this.boosterCollator.shuffle();
}
}
public String getName() {
@ -240,6 +251,9 @@ public abstract class ExpansionSet implements Serializable {
}
public List<Card> createBooster() {
if (boosterCollator != null) {
return createBoosterUsingCollator();
}
for (int i = 0; i < 100; i++) {//don't want to somehow loop forever
@ -262,6 +276,30 @@ public abstract class ExpansionSet implements Serializable {
return tryBooster();
}
public void shuffleCollator() {
if (boosterCollator != null) {
boosterCollator.shuffle();
}
}
private List<Card> createBoosterUsingCollator() {
if (inBoosterMap.isEmpty()) {
CardCriteria criteria = new CardCriteria();
criteria.setCodes(code);
CardRepository
.instance
.findCards(criteria)
.stream()
.forEach(cardInfo -> inBoosterMap.put(cardInfo.getCardNumberAsInt(), cardInfo));
}
return boosterCollator
.makeBooster()
.stream()
.map(inBoosterMap::get)
.map(CardInfo::getCard)
.collect(Collectors.toList());
}
protected boolean boosterIsValid(List<Card> booster) {
if (validateBoosterColors) {
if (!validateColors(booster)) {

View file

@ -0,0 +1,13 @@
package mage.collation;
import java.util.List;
/**
* @author TheElk801
*/
public interface BoosterCollator {
public void shuffle();
public List<Integer> makeBooster();
}

View file

@ -0,0 +1,36 @@
package mage.collation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Current implementation is built only for sequential collation
* Boosters are collated through a variety of different methods
* Striped collation not supported yet
* For more information: https://www.lethe.xyz/mtg/collation/
*
* @author TheElk801
*/
public abstract class BoosterStructure {
private final List<CardRun> slots;
protected BoosterStructure(CardRun... runs) {
this.slots = Arrays.asList(runs);
}
public List<Integer> makeRun() {
List<Integer> cards = new ArrayList<>();
for (CardRun run : this.slots) {
cards.add(run.getNext());
}
return cards;
}
public void shuffle() {
for (CardRun run : this.slots) {
run.shuffle();
}
}
}

View file

@ -0,0 +1,11 @@
package mage.collation;
/**
* @author TheElk801
*/
public abstract class CardRun extends Rotater<Integer> {
public CardRun(boolean keepOrder, Integer... names) {
super(keepOrder, names);
}
}

View file

@ -0,0 +1,27 @@
package mage.collation;
/**
* @author TheElk801
*/
public class RarityConfiguration extends Rotater<BoosterStructure> {
public RarityConfiguration(BoosterStructure item) {
super(item);
}
public RarityConfiguration(BoosterStructure item1, BoosterStructure item2) {
super(item1, item2);
}
public RarityConfiguration(boolean keepOrder, BoosterStructure... items) {
super(keepOrder, items);
}
@Override
public void shuffle() {
for (BoosterStructure structure : this.items) {
structure.shuffle();
}
super.shuffle();
}
}

View file

@ -0,0 +1,50 @@
package mage.collation;
import mage.util.RandomUtil;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* A class for shuffling a list by choosing a random starting point and looping through it
*
* @author TheElk801
*/
public class Rotater<T> {
protected final List<T> items;
private final boolean keepOrder;
private int position = 0;
public Rotater(T item) {
this(true, item);
}
public Rotater(T item1, T item2) {
this(true, item1, item2);
}
public Rotater(boolean keepOrder, T... items) {
this.items = Arrays.asList(items);
this.keepOrder = keepOrder;
}
public int iterate() {
int i = position;
position++;
position %= items.size();
return i;
}
public T getNext() {
return items.get(iterate());
}
public void shuffle() {
position = RandomUtil.nextInt(items.size());
if (!keepOrder) {
Collections.shuffle(items, RandomUtil.getRandom());
}
}
}