load decks from subfolders

display formats, sb/main sizes
This commit is contained in:
Maxmtg
2014-01-21 18:38:17 +00:00
parent 556bb2caa7
commit 47a719147c
6 changed files with 84 additions and 73 deletions

View File

@@ -84,19 +84,6 @@ public class DeckgenUtil {
return deck;
}
/**
* @param selection {@link java.lang.String}
* @return {@link forge.deck.Deck}
*/
public static Deck buildThemeDeck(final String selection) {
final GenerateThemeDeck gen = new GenerateThemeDeck();
final Deck deck = new Deck();
gen.setSingleton(Singletons.getModel().getPreferences().getPrefBoolean(FPref.DECKGEN_SINGLETONS));
gen.setUseArtifacts(Singletons.getModel().getPreferences().getPrefBoolean(FPref.DECKGEN_ARTIFACTS));
deck.getMain().addAll(gen.getThemeDeck(selection, 60));
return deck;
}
public static QuestEvent getQuestEvent(final String name) {
QuestController qCtrl = Singletons.getModel().getQuest();
@@ -124,14 +111,6 @@ public class DeckgenUtil {
return DeckgenUtil.buildColorDeck(selection, forAi);
}
/** @return {@link forge.deck.Deck} */
public static Deck getRandomThemeDeck() {
final List<String> themeNames = new ArrayList<String>();
for (final String s : GenerateThemeDeck.getThemeNames()) { themeNames.add(s); }
final int rand = (int) (Math.floor(Math.random() * themeNames.size()));
return DeckgenUtil.buildThemeDeck(themeNames.get(rand));
}
/** @return {@link forge.deck.Deck} */
public static Deck getRandomCustomDeck() {
final IStorage<Deck> allDecks = Singletons.getModel().getDecks().getConstructed();

View File

@@ -22,6 +22,7 @@ import forge.gui.toolbox.itemmanager.DeckManager;
import forge.gui.toolbox.itemmanager.ItemManagerContainer;
import forge.properties.ForgePreferences;
import forge.properties.ForgePreferences.FPref;
import forge.quest.QuestController;
import forge.quest.QuestEvent;
import forge.quest.QuestEventChallenge;
import forge.quest.QuestUtil;
@@ -77,7 +78,7 @@ public class FDeckChooser extends JPanel implements IDecksComboBoxListener {
private void updateCustom() {
lstDecks.setAllowMultipleSelections(false);
lstDecks.setPool(DeckProxy.getAllConstructedDecks());
lstDecks.setPool(DeckProxy.getAllConstructedDecks(Singletons.getModel().getDecks().getConstructed()));
lstDecks.update();
btnRandom.setText("Random Deck");
@@ -175,7 +176,7 @@ public class FDeckChooser extends JPanel implements IDecksComboBoxListener {
private void updatePrecons() {
lstDecks.setAllowMultipleSelections(false);
lstDecks.setPool(DeckProxy.getAllPreconstructedDecks());
lstDecks.setPool(DeckProxy.getAllPreconstructedDecks(QuestController.getPrecons()));
lstDecks.update(false, true);
btnRandom.setText("Random Deck");

View File

@@ -4,10 +4,9 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import org.apache.commons.lang3.StringUtils;
import com.google.common.base.Predicate;
import forge.Singletons;
import forge.card.ColorSet;
import forge.deck.CardPool;
@@ -16,29 +15,37 @@ import forge.deck.DeckGroup;
import forge.deck.DeckSection;
import forge.game.GameFormat;
import forge.game.GameType;
import forge.gui.deckchooser.DeckgenUtil;
import forge.gui.deckchooser.GenerateThemeDeck;
import forge.item.InventoryItem;
import forge.item.PaperCard;
import forge.item.PreconDeck;
import forge.properties.ForgePreferences.FPref;
import forge.quest.QuestController;
import forge.quest.QuestEvent;
import forge.util.storage.IStorage;
public class DeckProxy implements InventoryItem {
public final Deck deck;
protected final Deck deck;
protected final IStorage<?> storage;
// cached values
private ColorSet color;
private Iterable<GameFormat> formats;
protected ColorSet color;
protected Iterable<GameFormat> formats;
private int mainSize = Integer.MIN_VALUE;
private int sbSize = Integer.MIN_VALUE;
private String path;
public DeckProxy(Deck deck, GameType type, IStorage<Deck> storage) {
this.deck = deck;
// gametype could give us a hint whether the storage is updateable and enable choice of right editor for this deck
public DeckProxy(Deck deck, GameType type, IStorage<?> storage) {
this(deck, type, null, storage);
}
public DeckProxy(Deck deck, GameType type, String path, IStorage<?> storage) {
this.deck = deck;
this.storage = storage;
this.path = path;
// gametype could give us a hint whether the storage is updateable and enable choice of right editor for this deck
}
@Override
public String getName() {
return deck.getName();
@@ -54,6 +61,10 @@ public class DeckProxy implements InventoryItem {
return deck;
}
public String getPath() {
return path;
}
public void invalidateCache() {
color = null;
formats = null;
@@ -75,21 +86,29 @@ public class DeckProxy implements InventoryItem {
}
public int getMainSize() {
if ( mainSize < 0 )
mainSize = deck.getMain().countAll();
if ( mainSize == Integer.MIN_VALUE ) {
if( deck == null )
mainSize = -1;
else
mainSize = deck.getMain().countAll();
}
return mainSize;
}
public int getSideSize() {
if ( sbSize < 0 ) {
if ( deck.has(DeckSection.Sideboard) )
if ( sbSize == Integer.MIN_VALUE ) {
if ( deck != null && deck.has(DeckSection.Sideboard) )
sbSize = deck.get(DeckSection.Sideboard).countAll();
else
sbSize = 0;
sbSize = -1;
}
return sbSize;
}
public boolean isGeneratedDeck() {
return false;
}
public void updateInStorage() {
// if storage is not readonly, save the deck there.
}
@@ -100,16 +119,27 @@ public class DeckProxy implements InventoryItem {
// TODO: The methods below should not take the decks collections from singletons, instead they are supposed to use data passed in parameters
public static Iterable<DeckProxy> getAllConstructedDecks() {
// This is a temporary solution that does not iterate over subfolders. - will improve
public static Iterable<DeckProxy> getAllConstructedDecks(IStorage<Deck> storageRoot) {
return Iterables.transform(Singletons.getModel().getDecks().getConstructed(), new Function<Deck, DeckProxy>(){
public DeckProxy apply(Deck deck) { return new DeckProxy(deck, GameType.Constructed, Singletons.getModel().getDecks().getConstructed());
}});
List<DeckProxy> result = new ArrayList<DeckProxy>();
addDecksRecursivelly(result, "", storageRoot);
return result;
}
private static void addDecksRecursivelly(List<DeckProxy> list, String path, IStorage<Deck> folder) {
for(IStorage<Deck> f : folder.getFolders())
{
String subPath = (StringUtils.isBlank(path) ? "" : path ) + "/" + f.getName();
addDecksRecursivelly(list, subPath, f);
}
for (Deck d : folder) {
list.add(new DeckProxy(d, GameType.Constructed, path, folder));
}
}
//create predicate that applys a card predicate to all cards in deck
// Consider using a direct predicate to manage DeckProxies (not this tunnel to collection of paper cards)
public static final Predicate<DeckProxy> createPredicate(final Predicate<PaperCard> cardPredicate) {
return new Predicate<DeckProxy>() {
@Override
@@ -143,12 +173,23 @@ public class DeckProxy implements InventoryItem {
@Override
public Deck getDeck() {
return DeckgenUtil.buildThemeDeck(this.getName());
final GenerateThemeDeck gen = new GenerateThemeDeck();
final Deck deck = new Deck();
gen.setSingleton(Singletons.getModel().getPreferences().getPrefBoolean(FPref.DECKGEN_SINGLETONS));
gen.setUseArtifacts(Singletons.getModel().getPreferences().getPrefBoolean(FPref.DECKGEN_ARTIFACTS));
deck.getMain().addAll(gen.getThemeDeck(this.getName(), 60));
return deck;
}
@Override
public String getName() { return name; }
}
@Override
public String toString() { return name; }
public boolean isGeneratedDeck() {
return true;
}
}
public static Iterable<DeckProxy> getAllThemeDecks() {
ArrayList<DeckProxy> decks = new ArrayList<DeckProxy>();
@@ -158,10 +199,10 @@ public class DeckProxy implements InventoryItem {
return decks;
}
public static Iterable<DeckProxy> getAllPreconstructedDecks() {
public static Iterable<DeckProxy> getAllPreconstructedDecks(IStorage<PreconDeck> iStorage) {
ArrayList<DeckProxy> decks = new ArrayList<DeckProxy>();
for (final PreconDeck preconDeck : QuestController.getPrecons()) {
decks.add(new DeckProxy(preconDeck.getDeck(), null, null));
for (final PreconDeck preconDeck : iStorage) {
decks.add(new DeckProxy(preconDeck.getDeck(), null, iStorage));
}
return decks;
}
@@ -184,7 +225,7 @@ public class DeckProxy implements InventoryItem {
// Since AI decks are tied directly to the human choice,
// they're just mapped in a parallel map and grabbed when the game starts.
for (final DeckGroup d : sealed) {
humanDecks.add(new DeckProxy(d.getHumanDeck(), GameType.Sealed, null));
humanDecks.add(new DeckProxy(d.getHumanDeck(), GameType.Sealed, sealed));
}
return humanDecks;
}
@@ -201,7 +242,7 @@ public class DeckProxy implements InventoryItem {
public static Iterable<DeckProxy> getDraftDecks(IStorage<DeckGroup> draft) {
ArrayList<DeckProxy> decks = new ArrayList<DeckProxy>();
for (DeckGroup d : draft) {
decks.add(new DeckProxy(d.getHumanDeck(), GameType.Draft, null));
decks.add(new DeckProxy(d.getHumanDeck(), GameType.Draft, draft));
}
return decks;
}

View File

@@ -1,6 +1,7 @@
package forge.gui.deckeditor.controllers;
import forge.Command;
import forge.Singletons;
import forge.gui.deckeditor.DeckProxy;
import forge.gui.deckeditor.views.VAllDecks;
import forge.gui.framework.ICDoc;
@@ -30,7 +31,7 @@ public enum CAllDecks implements ICDoc {
*/
@Override
public void initialize() {
VAllDecks.SINGLETON_INSTANCE.getLstDecks().setPool(DeckProxy.getAllConstructedDecks());
VAllDecks.SINGLETON_INSTANCE.getLstDecks().setPool(DeckProxy.getAllConstructedDecks(Singletons.getModel().getDecks().getConstructed()));
}
/* (non-Javadoc)

View File

@@ -116,10 +116,6 @@ public enum CSubmenuGauntletQuick implements ICDoc {
tempDeck = DeckgenUtil.getRandomColorDeck(true);
lstEventNames.add("Random colors deck");
}
else if (lstDecktypes.get(randType).equals(DeckTypes.THEMES)) {
tempDeck = DeckgenUtil.getRandomThemeDeck();
lstEventNames.add("Random theme deck");
}
else if (lstDecktypes.get(randType).equals(DeckTypes.CUSTOM)) {
tempDeck = DeckgenUtil.getRandomCustomDeck();
lstEventNames.add(tempDeck.getName());

View File

@@ -34,8 +34,6 @@ import forge.card.CardRarity;
import forge.card.CardRules;
import forge.card.ColorSet;
import forge.card.mana.ManaCost;
import forge.deck.Deck;
import forge.deck.DeckSection;
import forge.game.GameFormat;
import forge.gui.CardPreferences;
import forge.gui.deckeditor.DeckProxy;
@@ -415,11 +413,11 @@ public class ItemColumn extends TableColumn {
new Function<Entry<InventoryItem, Integer>, Comparable<?>>() {
@Override
public Comparable<?> apply(final Entry<InventoryItem, Integer> from) {
Deck deck = toDeck(from.getKey());
DeckProxy deck = toDeck(from.getKey());
if (deck == null) {
return -1;
}
Iterable<GameFormat> all = Singletons.getModel().getFormats().getAllFormatsOfDeck(deck);
Iterable<GameFormat> all = deck.getFormats();
int acc = 0;
for(GameFormat gf : all) {
int ix = gf.getIndex();
@@ -432,38 +430,37 @@ public class ItemColumn extends TableColumn {
new Function<Entry<? extends InventoryItem, Integer>, Object>() {
@Override
public Object apply(final Entry<? extends InventoryItem, Integer> from) {
Deck deck = toDeck(from.getKey());
DeckProxy deck = toDeck(from.getKey());
if (deck == null) {
return null;
}
Iterable<GameFormat> all = Singletons.getModel().getFormats().getAllFormatsOfDeck(deck);
return StringUtils.join(Iterables.transform(all, GameFormat.FN_GET_NAME) , ", ");
return StringUtils.join(Iterables.transform(deck.getFormats(), GameFormat.FN_GET_NAME) , ", ");
}
}),
DECK_MAIN("Main", "Main Deck", 30, 30, 30, SortState.ASC, new IntegerRenderer(),
new Function<Entry<InventoryItem, Integer>, Comparable<?>>() {
@Override
public Comparable<?> apply(final Entry<InventoryItem, Integer> from) {
return toDeckCount(from.getKey(), DeckSection.Main);
return toDeck(from.getKey()).getMainSize();
}
},
new Function<Entry<? extends InventoryItem, Integer>, Object>() {
@Override
public Object apply(final Entry<? extends InventoryItem, Integer> from) {
return toDeckCount(from.getKey(), DeckSection.Main);
return toDeck(from.getKey()).getMainSize();
}
}),
DECK_SIDE("Side", "Sideboard", 30, 30, 30, SortState.ASC, new IntegerRenderer(),
new Function<Entry<InventoryItem, Integer>, Comparable<?>>() {
@Override
public Comparable<?> apply(final Entry<InventoryItem, Integer> from) {
return toDeckCount(from.getKey(), DeckSection.Sideboard);
return toDeck(from.getKey()).getSideSize();
}
},
new Function<Entry<? extends InventoryItem, Integer>, Object>() {
@Override
public Object apply(final Entry<? extends InventoryItem, Integer> from) {
return toDeckCount(from.getKey(), DeckSection.Sideboard);
return toDeck(from.getKey()).getSideSize();
}
});
@@ -543,15 +540,11 @@ public class ItemColumn extends TableColumn {
return ranking;
}
private static Deck toDeck(final InventoryItem i) {
return i instanceof Deck ? ((Deck) i) : null;
private static DeckProxy toDeck(final InventoryItem i) {
return i instanceof DeckProxy ? ((DeckProxy) i) : null;
}
private static ColorSet toDeckColor(final InventoryItem i) {
return i instanceof DeckProxy ? ((DeckProxy) i).getColor() : null;
}
private static int toDeckCount(final InventoryItem i, DeckSection section) {
Deck deck = toDeck(i);
return deck != null && deck.has(section) ? deck.get(section).countAll() : -1;
}
}
}