cardSet's members are finalized

M10-Booster added to quest cardshop (experimental and non-functional yet)
BoosterGenerator uses now rare-slots together with explicit numbers for rares and mythics
ItemPool has to be parametrized by Class<T> (unfortunatelly java erases types of generics)
This commit is contained in:
Maxmtg
2011-09-14 09:40:34 +00:00
parent 49b21e09ad
commit ab0f24d97d
26 changed files with 236 additions and 162 deletions

View File

@@ -27,16 +27,14 @@ public class BoosterGenerator {
private List<CardPrinted> mythics = new ArrayList<CardPrinted>();
private List<CardPrinted> specials = new ArrayList<CardPrinted>();
private int iCommons = 0;
private int iUncommons = 0;
private int iRares = 0;
private int iMythics = 0;
private int iSpecials = 0;
private int numCommons = 0;
private int numUncommons = 0;
// these two used when numbers of rares/myths is specified explicitly
private int numRares = 0;
private int numMythics = 0;
// this is used to specify number of slots for a rare - generator will decide by itself which to take
private int numRareSlots = 0;
private int numSpecials = 0;
//private Random r = new Random();
@@ -44,18 +42,15 @@ public class BoosterGenerator {
/**
* <p>Constructor for BoosterGenerator.</p>
*/
public BoosterGenerator() {
public BoosterGenerator(Iterable<CardPrinted> cards) {
numCommons = 11;
numUncommons = 3;
numRares = 1;
numMythics = 0;
numRareSlots = 1;
numSpecials = 0;
for (CardPrinted c : CardDb.instance().getAllUniqueCards()) {
for (CardPrinted c : cards) {
addToRarity(c);
}
shuffleAll();
}
/**
@@ -70,13 +65,13 @@ public class BoosterGenerator {
* @param ignoreRarity a boolean
*/
public BoosterGenerator(final String deckFile, final int nCommons, final int nUncommons, final int nRares,
final int nMythics, final int nSpecials, final boolean ignoreRarity)
final int nMythics, final int nSpecials, final boolean ignoreRarity)
{
numCommons = nCommons;
numUncommons = nUncommons;
numRares = nRares;
numMythics = nMythics;
numSpecials = nSpecials;
numMythics = nMythics;
//DeckManager dio = new DeckManager(ForgeProps.getFile(NewConstants.NEW_DECKS));
DeckManager dio = AllZone.getDeckManager();
@@ -91,7 +86,6 @@ public class BoosterGenerator {
else { addToRarity(e.getKey()); }
}
shuffleAll();
}
/**
@@ -103,7 +97,6 @@ public class BoosterGenerator {
numCommons = 0;
numUncommons = 0;
numRares = 0;
numMythics = 0;
numSpecials = 0;
List<String> setsList = Arrays.asList(new String[]{setCode});
@@ -114,8 +107,6 @@ public class BoosterGenerator {
addToRarity(c);
}
shuffleAll();
ArrayList<String> bpData = FileUtil.readFile("res/boosterdata/" + setCode + ".pack");
for (String line : bpData) {
@@ -124,9 +115,7 @@ public class BoosterGenerator {
} else if (line.startsWith("Uncommons:")) {
numUncommons = Integer.parseInt(line.substring(10));
} else if (line.startsWith("Rares:")) {
numRares = Integer.parseInt(line.substring(6));
} else if (line.startsWith("Mythics:")) {
numMythics = Integer.parseInt(line.substring(8));
numRareSlots = Integer.parseInt(line.substring(6));
} else if (line.startsWith("Specials:")) {
numSpecials = Integer.parseInt(line.substring(9));
}
@@ -137,32 +126,61 @@ public class BoosterGenerator {
System.out.println("numCommons: " + numCommons);
System.out.println("numUncommons: " + numUncommons);
System.out.println("numRares: " + numRares);
System.out.println("numMythics: " + numMythics);
System.out.println("numSpecials: " + numSpecials);
}
}
/**
* <p>shuffleAll.</p>
*/
private void shuffleAll() {
if (!commons.isEmpty()) { Collections.shuffle(commons, MyRandom.random); }
if (!uncommons.isEmpty()) { Collections.shuffle(uncommons, MyRandom.random); }
if (!rares.isEmpty()) { Collections.shuffle(rares, MyRandom.random); }
if (!mythics.isEmpty()) { Collections.shuffle(mythics, MyRandom.random); }
if (!specials.isEmpty()) { Collections.shuffle(specials, MyRandom.random); }
if (Constant.Runtime.DevMode[0]) {
System.out.println("commons.size: " + commons.size());
System.out.println("uncommons.size: " + uncommons.size());
System.out.println("rares.size: " + rares.size());
System.out.println("mythics.size: " + mythics.size());
System.out.println("specials.size: " + specials.size());
private List<CardPrinted> pickRandomCards(List<CardPrinted> source, int count)
{
List<CardPrinted> result = new ArrayList<CardPrinted>(count);
if (count <= 0 || source == null || source.isEmpty()) { return result; }
int listSize = source.size();
int index = Integer.MAX_VALUE;
for (int iCard = 0; iCard < count; iCard++) {
if (index >= listSize) {
Collections.shuffle(source, MyRandom.random);
index = 0;
}
result.add(source.get(index));
index++;
}
return result;
}
private List<CardPrinted> pickRandomRaresOrMythics(List<CardPrinted> rares, List<CardPrinted> mythics, int count)
{
List<CardPrinted> result = new ArrayList<CardPrinted>(count);
int raresSize = rares == null ? 0 : rares.size();
int mythicsSize = mythics == null ? 0 : mythics.size();
if (count <= 0 || raresSize == 0) { return result; }
int indexRares = Integer.MAX_VALUE;
int indexMythics = Integer.MAX_VALUE;
for (int iCard = 0; iCard < count; iCard++) {
boolean takeMythic = mythicsSize > 0 && MyRandom.random.nextInt(8) <= 1;
if (takeMythic) {
if (indexRares >= raresSize) {
Collections.shuffle(mythics, MyRandom.random);
indexMythics = 0;
}
result.add(mythics.get(indexMythics));
indexMythics++;
}
else
{
if (indexRares >= raresSize) {
Collections.shuffle(rares, MyRandom.random);
indexRares = 0;
}
result.add(rares.get(indexRares));
indexRares++;
}
}
return result;
}
/**
* <p>getBoosterPack.</p>
*
@@ -171,65 +189,18 @@ public class BoosterGenerator {
public final List<CardPrinted> getBoosterPack() {
List<CardPrinted> temp = new ArrayList<CardPrinted>();
int i = 0;
if (commons.size() > numCommons) {
for (i = 0; i < numCommons; i++) {
if (iCommons >= commons.size()) {
iCommons = 0;
}
temp.add(commons.get(iCommons++));
}
temp.addAll(pickRandomCards(commons, numCommons));
temp.addAll(pickRandomCards(uncommons, numUncommons));
// You can specify number of rare-slots or number of rares and mythics explicitly... or both, but they'll sum up
if (numRareSlots > 0) {
temp.addAll(pickRandomRaresOrMythics(rares, mythics, numRareSlots));
}
if (uncommons.size() > numUncommons) {
for (i = 0; i < numUncommons; i++) {
if (iUncommons >= uncommons.size()) {
iUncommons = 0;
}
temp.add(uncommons.get(iUncommons++));
}
}
for (i = 0; i < numRares; i++) {
if (numMythics > 0) {
if (mythics.size() > numMythics) {
if (MyRandom.random.nextInt(8) <= 1) {
if (iMythics >= mythics.size()) {
iMythics = 0;
}
temp.add(mythics.get(iMythics++));
} else {
if (iRares >= rares.size()) {
iRares = 0;
}
temp.add(rares.get(iRares++));
}
}
} else {
if (rares.size() > numRares) {
if (iRares >= rares.size()) {
iRares = 0;
}
temp.add(rares.get(iRares++));
}
}
}
if (specials.size() > numSpecials) {
for (i = 0; i < numSpecials; i++) {
if (iSpecials >= specials.size()) {
iSpecials = 0;
}
temp.add(specials.get(iSpecials++));
}
if (numRares > 0 || numMythics > 0) {
temp.addAll(pickRandomCards(rares, numRares));
temp.addAll(pickRandomCards(mythics, numMythics));
}
temp.addAll(pickRandomCards(specials, numSpecials));
return temp;
}

View File

@@ -55,7 +55,7 @@ public class ReadBoosterPack implements NewConstants {
* @return a {@link forge.CardList} object.
*/
public ItemPoolView<CardPrinted> getBoosterPack5() {
ItemPool<CardPrinted> list = new ItemPool<CardPrinted>();
ItemPool<CardPrinted> list = new ItemPool<CardPrinted>(CardPrinted.class);
for (int i = 0; i < 5; i++) { list.addAll(getBoosterPack()); }
addBasicLands(list, 20);
@@ -92,7 +92,7 @@ public class ReadBoosterPack implements NewConstants {
* @return a {@link forge.CardList} object.
*/
public ItemPoolView<CardPrinted> getBoosterPack() {
ItemPool<CardPrinted> pack = new ItemPool<CardPrinted>();
ItemPool<CardPrinted> pack = new ItemPool<CardPrinted>(CardPrinted.class);
pack.add(getRandomCard(rares));
@@ -126,7 +126,7 @@ public class ReadBoosterPack implements NewConstants {
* @return a {@link forge.CardList} object.
*/
public ItemPoolView<CardPrinted> getShopCards(int totalPacks) {
ItemPool<CardPrinted> list = new ItemPool<CardPrinted>();
ItemPool<CardPrinted> list = new ItemPool<CardPrinted>(CardPrinted.class);
// Number of Packs granted

View File

@@ -46,7 +46,7 @@ public class ReadDraftBoosterPack implements NewConstants {
}
public ItemPoolView<CardPrinted> getBoosterPack5() {
ItemPool<CardPrinted> list = new ItemPool<CardPrinted>();
ItemPool<CardPrinted> list = new ItemPool<CardPrinted>(CardPrinted.class);
for (int i = 0; i < 5; i++) { list.addAll(getBoosterPack()); }
addBasicLands(list, 20);
@@ -82,7 +82,7 @@ public class ReadDraftBoosterPack implements NewConstants {
* @return a {@link forge.CardList} object.
*/
public final ItemPoolView<CardPrinted> getBoosterPack() {
ItemPool<CardPrinted> pack = new ItemPool<CardPrinted>();
ItemPool<CardPrinted> pack = new ItemPool<CardPrinted>(CardPrinted.class);
pack.add(getRandomCard(rareList));

View File

@@ -9,10 +9,10 @@ import net.slightlymagic.braids.util.lambda.Lambda1;
* @version $Id: CardSet.java 9708 2011-08-09 19:34:12Z jendave $
*/
public final class CardSet implements Comparable<CardSet> { // immutable
private int index;
private String code;
private String code2;
private String name;
private final int index;
private final String code;
private final String code2;
private final String name;
public CardSet(final int index, final String name, final String code, final String code2) {
this.code = code;

View File

@@ -41,8 +41,8 @@ public final class Deck implements Comparable<Deck>, Serializable {
* <p>Constructor for Deck.</p>
*/
public Deck() {
main = new ItemPool<CardPrinted>();
sideboard = new ItemPool<CardPrinted>();
main = new ItemPool<CardPrinted>(CardPrinted.class);
sideboard = new ItemPool<CardPrinted>(CardPrinted.class);
}
/**

View File

@@ -10,7 +10,9 @@ import forge.SetUtils;
import forge.card.CardBlock;
import forge.card.CardSet;
import forge.deck.Deck;
import forge.deck.DeckManager;
import forge.gui.GuiUtils;
import forge.item.CardDb;
import forge.item.CardPrinted;
import forge.item.ItemPool;
import forge.item.ItemPoolView;
@@ -70,7 +72,7 @@ public class BoosterDraft_1 implements BoosterDraft {
draftFormat[0] = draftType;
if (draftType.equals("Full")) { // Draft from all cards in Forge
BoosterGenerator bpFull = new BoosterGenerator();
BoosterGenerator bpFull = new BoosterGenerator(CardDb.instance().getAllUniqueCards());
for (int i = 0; i < 3; i++) {
packs.add(bpFull);
}

View File

@@ -44,7 +44,7 @@ public class SealedDeck {
public SealedDeck(String sealedType) {
if (sealedType.equals("Full")) {
BoosterGenerator bpFull = new BoosterGenerator();
BoosterGenerator bpFull = new BoosterGenerator(CardDb.instance().getAllUniqueCards());
for (int i = 0; i < 6; i++)
packs.add(bpFull);
@@ -182,7 +182,7 @@ public class SealedDeck {
* @return a {@link forge.CardList} object.
*/
public ItemPool<CardPrinted> getCardpool() {
ItemPool<CardPrinted> pool = new ItemPool<CardPrinted>();
ItemPool<CardPrinted> pool = new ItemPool<CardPrinted>(CardPrinted.class);
for (int i = 0; i < packs.size(); i++)
pool.addAllCards(packs.get(i).getBoosterPack());

View File

@@ -16,6 +16,7 @@ import forge.item.ItemPoolView;
*/
public interface DeckDisplay {
void setDeck(ItemPoolView<CardPrinted> top, ItemPoolView<CardPrinted> bottom, GameType gameType);
<T extends InventoryItem> void setItems(ItemPoolView<T> topParam, ItemPoolView<T> bottomParam, GameType gt);
//top shows available card pool
//if constructed, top shows all cards

View File

@@ -62,18 +62,24 @@ public abstract class DeckEditorBase extends JFrame implements DeckDisplay {
g.setEnabled(false);
}
}
public DeckEditorBase(GameType gametype)
{
gameType = gametype;
}
public void setDeck(ItemPoolView<CardPrinted> topParam, ItemPoolView<CardPrinted> bottomParam, GameType gt) {
gameType = gt;
top.setDeck(topParam);
bottom.setDeck(bottomParam);
}
public <T extends InventoryItem> void setItems(ItemPoolView<T> topParam, ItemPoolView<T> bottomParam, GameType gt) {
gameType = gt;
top.setDeck(topParam);
bottom.setDeck(bottomParam);
}
public void updateDisplay() {
top.setFilter(buildFilter());
}
@@ -84,6 +90,7 @@ public abstract class DeckEditorBase extends JFrame implements DeckDisplay {
}
};
/** This class is used for a feature: when you start typing card name, the list gets auto-filtered. */
protected class OnChangeTextUpdateDisplay implements DocumentListener {
private void onChange() { if (isFiltersChangeFiringUpdate) { updateDisplay(); } }
@Override public void insertUpdate(DocumentEvent e) { onChange(); }

View File

@@ -147,7 +147,7 @@ public final class DeckEditorMenu extends JMenuBar implements NewConstants {
// The only remaining reference to global variable!
CardList random = new CardList(forge.AllZone.getCardFactory().getRandomCombinationWithoutRepetition(15 * 5));
ItemPool<CardPrinted> cpRandom = new ItemPool<CardPrinted>();
ItemPool<CardPrinted> cpRandom = new ItemPool<CardPrinted>(CardPrinted.class);
for (Card c : random) { cpRandom.add(CardDb.instance().getCard(c)); }
cpRandom.add(CardDb.instance().getCard("Forest"));
cpRandom.add(CardDb.instance().getCard("Island"));
@@ -166,7 +166,7 @@ public final class DeckEditorMenu extends JMenuBar implements NewConstants {
GenerateConstructedDeck gen = new GenerateConstructedDeck();
ItemPool<CardPrinted> generated = new ItemPool<CardPrinted>();
ItemPool<CardPrinted> generated = new ItemPool<CardPrinted>(CardPrinted.class);
for (Card c : gen.generateDeck()) { generated.add(CardDb.instance().getCard(c)); }
deckDisplay.setDeck(null, generated, GameType.Constructed);
}

View File

@@ -99,7 +99,7 @@ public final class DeckEditorQuest extends DeckEditorBase implements NewConstant
customMenu.setPlayerDeckName(deck.getName());
ItemPoolView<CardPrinted> bottomPool = deck.getMain();
ItemPool<CardPrinted> cardpool = new ItemPool<CardPrinted>();
ItemPool<CardPrinted> cardpool = new ItemPool<CardPrinted>(CardPrinted.class);
cardpool.addAll(questData.getCards().getCardpool());
// remove bottom cards that are in the deck from the card pool

View File

@@ -271,7 +271,7 @@ public class DeckEditorQuestMenu extends JMenuBar {
questData.addDeck(newDeck);
ItemPool<CardPrinted> cardpool = ItemPool.createFrom(questData.getCards().getCardpool(), CardPrinted.class);
ItemPool<CardPrinted> decklist = new ItemPool<CardPrinted>();
ItemPool<CardPrinted> decklist = new ItemPool<CardPrinted>(CardPrinted.class);
for (Entry<CardPrinted, Integer> s : newDeck.getMain()) {
CardPrinted cp = s.getKey();
decklist.add(cp, s.getValue());
@@ -348,7 +348,7 @@ public class DeckEditorQuestMenu extends JMenuBar {
private final ActionListener newDeckActionListener = new ActionListener() {
public void actionPerformed(final ActionEvent a) {
deckDisplay.setDeck(questData.getCards().getCardpool().getView(), null, GameType.Quest);
deckDisplay.setItems(questData.getCards().getCardpool().getView(), null, GameType.Quest);
setPlayerDeckName("");
}
};
@@ -431,7 +431,7 @@ public class DeckEditorQuestMenu extends JMenuBar {
questData.removeDeck(currentDeck.getName());
//show card pool
deckDisplay.setDeck(questData.getCards().getCardpool().getView(), null, GameType.Quest);
deckDisplay.setItems(questData.getCards().getCardpool().getView(), null, GameType.Quest);
setPlayerDeckName("");
}

View File

@@ -88,15 +88,15 @@ public final class DeckEditorShop extends DeckEditorBase {
multiplier = questData.getCards().getSellMutliplier();
ItemPoolView<CardPrinted> forSale = questData.getCards().getShopList();
ItemPoolView<InventoryItem> forSale = questData.getCards().getShopList();
if (forSale.isEmpty()) {
questData.getCards().generateCardsInShop();
forSale = questData.getCards().getShopList();
}
ItemPoolView<CardPrinted> owned = questData.getCards().getCardpool().getView();
ItemPoolView<InventoryItem> owned = questData.getCards().getCardpool().getView();
//newCardsList = questData.getCards().getNewCards();
setDeck(forSale, owned, GameType.Quest);
setItems(forSale, owned, GameType.Quest);
double multiPercent = multiplier * 100;
NumberFormat formatter = new DecimalFormat("#0.00");

View File

@@ -10,6 +10,7 @@ import forge.card.CardRarity;
import forge.card.CardSet;
import forge.item.CardPrinted;
import forge.item.InventoryItem;
import forge.item.InventoryItemFromSet;
/**
* TODO: Write javadoc for this type.
@@ -22,8 +23,8 @@ public abstract class PresetColumns {
private static String toType(InventoryItem i) { return i instanceof CardPrinted ? ((CardPrinted) i).getCard().getType().toString() : i.getClass().toString(); }
private static String toPTL(InventoryItem i) { return i instanceof CardPrinted ? ((CardPrinted) i).getCard().getPTorLoyalty() : ""; }
private static CardRarity toRarity(InventoryItem i) { return i instanceof CardPrinted ? ((CardPrinted) i).getRarity() : CardRarity.Unknown; }
private static CardSet toSetCmp(InventoryItem i) { return i instanceof CardPrinted ? SetUtils.getSetByCode(((CardPrinted) i).getSet()) : CardSet.unknown; }
private static String toSetStr(InventoryItem i) { return i instanceof CardPrinted ? ((CardPrinted) i).getSet() : "n/a"; }
private static CardSet toSetCmp(InventoryItem i) { return i instanceof InventoryItemFromSet ? SetUtils.getSetByCode(((InventoryItemFromSet) i).getSet()) : CardSet.unknown; }
private static String toSetStr(InventoryItem i) { return i instanceof InventoryItemFromSet ? ((InventoryItemFromSet) i).getSet() : "n/a"; }
private static Integer toAiCmp(InventoryItem i) { return i instanceof CardPrinted ? ((CardPrinted) i).getCard().getAiStatusComparable() : Integer.valueOf(-1); }
private static String toAiStr(InventoryItem i) { return i instanceof CardPrinted ? ((CardPrinted) i).getCard().getAiStatus() : "n/a"; }

View File

@@ -91,12 +91,13 @@ public final class TableModel<T extends InventoryItem> extends AbstractTableMode
}
}
private ItemPool<T> data = new ItemPool<T>();
private ItemPool<T> data;
private final CardPanelBase cardDisplay;
private final List<TableColumnInfo<T>> columns;
private final SortOrders sortOrders = new SortOrders();
public TableModel(final CardPanelBase cd, final List<TableColumnInfo<T>> columnsToShow) {
public TableModel(final CardPanelBase cd, final List<TableColumnInfo<T>> columnsToShow, Class<T> cls) {
data = new ItemPool<T>(cls);
cardDisplay = cd;
columns = columnsToShow;
columns.get(4).isMinMaxApplied = false;

View File

@@ -66,7 +66,7 @@ public final class TableWithCards {
public void setup(final List<TableColumnInfo<InventoryItem>> columns, final CardPanelBase cardView)
{
model = new TableModel<InventoryItem>(cardView, columns);
model = new TableModel<InventoryItem>(cardView, columns, InventoryItem.class);
model.addListeners(table);
table.setModel(model);
model.resizeCols(table);

View File

@@ -0,0 +1,43 @@
package forge.item;
import java.util.List;
import forge.BoosterGenerator;
import forge.SetUtils;
/**
* TODO: Write javadoc for this type.
*
*/
public class Booster implements InventoryItemFromSet {
private final String cardSet;
private final String name;
private List<CardPrinted> cards = null;
public Booster(String set) {
cardSet = set;
name = SetUtils.getSetByCodeOrThrow(set).getName() + " booster";
}
@Override public String getSet() { return cardSet; }
@Override public String getName() { return name; }
@Override public String getImageFilename() {
// TODO: images for boosters
return null;
}
public List<CardPrinted> getCards() {
if (null == cards)
{
BoosterGenerator gen = new BoosterGenerator(cardSet);
cards = gen.getBoosterPack();
// TODO: Add land here!
}
return cards;
}
}

View File

@@ -20,7 +20,7 @@ import forge.card.CardRules;
* @author Forge
* @version $Id: CardReference.java 9708 2011-08-09 19:34:12Z jendave $
*/
public final class CardPrinted implements Comparable<CardPrinted>, InventoryItem {
public final class CardPrinted implements Comparable<CardPrinted>, InventoryItemFromSet {
// Reference to rules
private final transient CardRules card;

View File

@@ -0,0 +1,14 @@
package forge.item;
/**
* Interface to define a player's inventory may hold.
* Should include CardPrinted, Booster, Pets, Plants... etc
*/
public interface InventoryItemFromSet extends InventoryItem {
/** An inventory item has to provide a name. */
String getName();
/** An inventory item has to provide a picture. */
String getImageFilename();
/** An item belonging to a set should return its set as well*/
String getSet();
}

View File

@@ -12,16 +12,16 @@ import java.util.Map.Entry;
public final class ItemPool<T extends InventoryItem> extends ItemPoolView<T> {
// Constructors here
public ItemPool() { super(); }
public ItemPool(final Class<T> cls) { super(cls); }
@SuppressWarnings("unchecked") // conversion here must be safe
public ItemPool(final List<String> names) { super(); addAllCards((Iterable<T>) CardDb.instance().getCards(names)); }
public ItemPool(final List<String> names, final Class<T> cls) { super(cls); addAllCards((Iterable<T>) CardDb.instance().getCards(names)); }
@SuppressWarnings("unchecked")
public static <Tin extends InventoryItem, Tout extends InventoryItem> ItemPool<Tout>
createFrom(ItemPoolView<Tin> from, Class<Tout> clsHint)
{
ItemPool<Tout> result = new ItemPool<Tout>();
ItemPool<Tout> result = new ItemPool<Tout>(clsHint);
if (from != null) {
for (Entry<Tin, Integer> e : from) {
Tin srcKey = e.getKey();
@@ -35,8 +35,9 @@ public final class ItemPool<T extends InventoryItem> extends ItemPoolView<T> {
@SuppressWarnings("unchecked")
public static <Tin extends InventoryItem, Tout extends InventoryItem> ItemPool<Tout>
createFrom(Iterable<Tin> from, Class<Tout> clsHint) {
ItemPool<Tout> result = new ItemPool<Tout>();
createFrom(Iterable<Tin> from, Class<Tout> clsHint)
{
ItemPool<Tout> result = new ItemPool<Tout>(clsHint);
if (from != null) {
for (Tin srcKey : from) {
if (clsHint.isInstance(srcKey)) {
@@ -48,7 +49,7 @@ public final class ItemPool<T extends InventoryItem> extends ItemPoolView<T> {
}
// get
public ItemPoolView<T> getView() { return new ItemPoolView<T>(Collections.unmodifiableMap(cards)); }
public ItemPoolView<T> getView() { return new ItemPoolView<T>(Collections.unmodifiableMap(cards), myClass); }
// Cards manipulation
public void add(final T card) { add(card, 1); }
@@ -65,11 +66,17 @@ public final class ItemPool<T extends InventoryItem> extends ItemPoolView<T> {
for (T cr : cards) { add(cr); }
isListInSync = false;
}
public void addAll(final Iterable<Entry<T, Integer>> map) {
for (Entry<T, Integer> e : map) { add(e.getKey(), e.getValue()); }
@SuppressWarnings("unchecked")
public <U extends InventoryItem> void addAll(final Iterable<Entry<U, Integer>> map) {
for (Entry<U, Integer> e : map) {
if (myClass.isInstance(e.getKey())) {
add((T) e.getKey(), e.getValue());
}
}
isListInSync = false;
}
public void remove(final T card) { remove(card, 1); }
public void remove(final T card, final int amount) {
int count = count(card);

View File

@@ -42,11 +42,12 @@ public class ItemPoolView<T extends InventoryItem> implements Iterable<Entry<T,
};
// Constructors
public ItemPoolView() { cards = new Hashtable<T, Integer>(); }
public ItemPoolView(final Map<T, Integer> inMap) { cards = inMap; }
public ItemPoolView(final Class<T> cls) { cards = new Hashtable<T, Integer>(); myClass = cls; }
public ItemPoolView(final Map<T, Integer> inMap, final Class<T> cls) { cards = inMap; myClass = cls; }
// Data members
protected Map<T, Integer> cards;
protected final Class<T> myClass; // class does not keep this in runtime by itself
// same thing as above, it was copied to provide sorting (needed by table views in deck editors)
protected List<Entry<T, Integer>> cardsListOrdered = new ArrayList<Map.Entry<T,Integer>>();

View File

@@ -0,0 +1,2 @@
/** Forge Card Game */
package forge.item;

View File

@@ -68,9 +68,9 @@ public final class QuestData {
Map<String, Deck> myDecks = new HashMap<String, Deck>();
// Cards associated with quest
ItemPool<CardPrinted> cardPool = new ItemPool<CardPrinted>(); // player's belonging
ItemPool<CardPrinted> shopList = new ItemPool<CardPrinted>(); // the current shop list
ItemPool<InventoryItem> newCardList = new ItemPool<InventoryItem>(); // cards acquired since last game-win/loss
ItemPool<InventoryItem> cardPool = new ItemPool<InventoryItem>(InventoryItem.class); // player's belonging
ItemPool<InventoryItem> shopList = new ItemPool<InventoryItem>(InventoryItem.class); // the current shop list
ItemPool<InventoryItem> newCardList = new ItemPool<InventoryItem>(InventoryItem.class); // cards acquired since last game-win/loss
// Quests history
int questsPlayed;
@@ -116,8 +116,8 @@ public final class QuestData {
myRewards = new QuestUtilRewards(this);
// to avoid NPE some pools will be created here if they are null
if (null == newCardList) { newCardList = new ItemPool<InventoryItem>(); }
if (null == shopList) { shopList = new ItemPool<CardPrinted>(); }
if (null == newCardList) { newCardList = new ItemPool<InventoryItem>(InventoryItem.class); }
if (null == shopList) { shopList = new ItemPool<InventoryItem>(InventoryItem.class); }
}

View File

@@ -10,6 +10,7 @@ import com.thoughtworks.xstream.converters.UnmarshallingContext;
import forge.error.ErrorViewer;
import forge.game.GameType;
import forge.item.Booster;
import forge.item.CardDb;
import forge.item.CardPrinted;
import forge.item.InventoryItem;
@@ -222,50 +223,68 @@ public class QuestDataIO {
return clasz.equals(ItemPool.class);
}
private void writeCardRef(CardPrinted cref, HierarchicalStreamWriter writer)
private void write(CardPrinted cref, Integer count, HierarchicalStreamWriter writer)
{
writer.startNode("card");
writer.addAttribute("c", cref.getName());
writer.addAttribute("s", cref.getSet());
if (cref.isFoil()) { writer.addAttribute("foil", "1"); }
if (cref.getArtIndex() > 0) { writer.addAttribute("i", Integer.toString(cref.getArtIndex())); }
writer.addAttribute("n", count.toString());
writer.endNode();
}
private void write(Booster booster, Integer count, HierarchicalStreamWriter writer)
{
writer.startNode("booster");
writer.addAttribute("s", booster.getSet());
writer.addAttribute("n", count.toString());
writer.endNode();
}
@Override
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
@SuppressWarnings("unchecked")
ItemPool<InventoryItem> pool = (ItemPool<InventoryItem>) source;
for (Entry<InventoryItem, Integer> e : pool) {
if ( e.getKey() instanceof CardPrinted)
{
writer.startNode("card");
writeCardRef((CardPrinted) e.getKey(), writer);
writer.addAttribute("n", e.getValue().toString());
writer.endNode();
}
InventoryItem item = e.getKey();
Integer count = e.getValue();
if (item instanceof CardPrinted) {
write((CardPrinted) item, count, writer);
} else if (item instanceof Booster) {
write((Booster) item, count, writer);
}
}
}
@Override
public Object unmarshal(final HierarchicalStreamReader reader, final UnmarshallingContext context) {
ItemPool<CardPrinted> result = new ItemPool<CardPrinted>();
ItemPool<InventoryItem> result = new ItemPool<InventoryItem>(InventoryItem.class);
while (reader.hasMoreChildren()) {
reader.moveDown();
String sCnt = reader.getAttribute("n");
int cnt = StringUtils.isNumeric(sCnt) ? Integer.parseInt(sCnt) : 1;
String nodename = reader.getNodeName();
if("string".equals(nodename)) {
CardPrinted cp = CardDb.instance().getCard(reader.getValue());
result.add(cp);
} else { // new format
CardPrinted cp = readCardPrinted(reader);
String sCnt = reader.getAttribute("n");
int cnt = StringUtils.isNumeric(sCnt) ? Integer.parseInt(sCnt) : 1;
result.add(cp, cnt);
result.add(CardDb.instance().getCard(reader.getValue()));
} else if ("card".equals(nodename)) { // new format
result.add(readCardPrinted(reader), cnt);
} else if ("booster".equals(nodename)) {
result.add(readBooster(reader), cnt);
}
reader.moveUp();
}
return result;
}
private Booster readBooster(final HierarchicalStreamReader reader)
{
String set = reader.getAttribute("s");
return new Booster(set);
}
private CardPrinted readCardPrinted(final HierarchicalStreamReader reader)
{

View File

@@ -10,6 +10,7 @@ import net.slightlymagic.maxmtg.Predicate;
import forge.ReadBoosterPack;
import forge.card.CardRarity;
import forge.deck.Deck;
import forge.item.Booster;
import forge.item.CardDb;
import forge.item.CardPrinted;
import forge.item.InventoryItem;
@@ -149,13 +150,14 @@ public final class QuestUtilCards {
ItemPoolView<CardPrinted> fromBoosters = pack.getShopCards(totalPacks);
q.shopList.clear();
q.shopList.addAll(fromBoosters);
q.shopList.add(new Booster("M10"));
}
public ItemPool<CardPrinted> getCardpool() {
public ItemPool<InventoryItem> getCardpool() {
return q.cardPool;
}
public ItemPoolView<CardPrinted> getShopList() {
public ItemPoolView<InventoryItem> getShopList() {
if (q.shopList.isEmpty()) {
generateCardsInShop();
}