optimized formats calculation for decks,

added some comments to deckmanagers
This commit is contained in:
Maxmtg
2014-01-24 06:55:38 +00:00
parent 7c105ca398
commit 34b36a8ef2
3 changed files with 29 additions and 31 deletions

View File

@@ -170,14 +170,7 @@ public class GameFormat implements Comparable<GameFormat> {
return this.allowedSetCodes_ro.isEmpty() || this.allowedSetCodes_ro.contains(setCode); return this.allowedSetCodes_ro.isEmpty() || this.allowedSetCodes_ro.contains(setCode);
} }
public boolean isDeckLegal(final Deck deck) { private boolean isPoolLegal(final CardPool allCards) {
CardPool allCards = new CardPool(); // will count cards in this pool to enforce restricted
allCards.addAll(deck.getMain());
if (deck.has(DeckSection.Sideboard))
allCards.addAll(deck.get(DeckSection.Sideboard));
if (deck.has(DeckSection.Commander))
allCards.addAll(deck.get(DeckSection.Commander));
for (Entry<PaperCard, Integer> poolEntry : allCards) { for (Entry<PaperCard, Integer> poolEntry : allCards) {
if (!filterRules.apply(poolEntry.getKey())) { if (!filterRules.apply(poolEntry.getKey())) {
return false; //all cards in deck must pass card predicate to pass deck predicate return false; //all cards in deck must pass card predicate to pass deck predicate
@@ -193,6 +186,20 @@ public class GameFormat implements Comparable<GameFormat> {
return true; return true;
} }
private static CardPool getAllDecksCards(final Deck deck) {
CardPool allCards = new CardPool(); // will count cards in this pool to enforce restricted
allCards.addAll(deck.getMain());
if (deck.has(DeckSection.Sideboard))
allCards.addAll(deck.get(DeckSection.Sideboard));
if (deck.has(DeckSection.Commander))
allCards.addAll(deck.get(DeckSection.Commander));
return allCards;
}
public boolean isDeckLegal(final Deck deck) {
return isPoolLegal(getAllDecksCards(deck));
}
/* /*
* (non-Javadoc) * (non-Javadoc)
* *
@@ -297,8 +304,9 @@ public class GameFormat implements Comparable<GameFormat> {
public Iterable<GameFormat> getAllFormatsOfDeck(Deck deck) { public Iterable<GameFormat> getAllFormatsOfDeck(Deck deck) {
List<GameFormat> result = new ArrayList<GameFormat>(); List<GameFormat> result = new ArrayList<GameFormat>();
CardPool allCards = GameFormat.getAllDecksCards(deck);
for(GameFormat gf : naturallyOrdered) { for(GameFormat gf : naturallyOrdered) {
if (gf.isDeckLegal(deck)) if (gf.isPoolLegal(allCards))
result.add(gf); result.add(gf);
} }
if( result.isEmpty()) if( result.isEmpty())

View File

@@ -17,8 +17,6 @@
*/ */
package forge.gui.deckeditor.controllers; package forge.gui.deckeditor.controllers;
import java.util.ArrayList;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
@@ -38,7 +36,7 @@ public class DeckController<T extends DeckBase> {
private boolean saved; private boolean saved;
private boolean modelInStorage; private boolean modelInStorage;
private final IStorage<T> rootFolder; private final IStorage<T> rootFolder;
private IStorage<T> folder; private IStorage<T> currentFolder;
private final ACEditorBase<?, T> view; private final ACEditorBase<?, T> view;
private final Supplier<T> newModelCreator; private final Supplier<T> newModelCreator;
@@ -52,7 +50,7 @@ public class DeckController<T extends DeckBase> {
public DeckController(final IStorage<T> folder0, final ACEditorBase<?, T> view0, public DeckController(final IStorage<T> folder0, final ACEditorBase<?, T> view0,
final Supplier<T> newModelCreator0) { final Supplier<T> newModelCreator0) {
this.rootFolder = folder0; this.rootFolder = folder0;
this.folder = rootFolder; this.currentFolder = rootFolder;
this.view = view0; this.view = view0;
this.model = null; this.model = null;
this.saved = true; this.saved = true;
@@ -97,7 +95,7 @@ public class DeckController<T extends DeckBase> {
return true; return true;
} }
final T modelStored = this.folder.get(this.model.getName()); final T modelStored = this.currentFolder.get(this.model.getName());
// checks presence in dictionary only. // checks presence in dictionary only.
if (modelStored == this.model) { if (modelStored == this.model) {
return true; return true;
@@ -130,15 +128,6 @@ public class DeckController<T extends DeckBase> {
updateCaptions(); updateCaptions();
} }
/**
* Gets the saved names.
*
* @return the saved names
*/
public ArrayList<String> getSavedNames() {
return new ArrayList<String>(this.folder.getItemNames());
}
/** /**
* Reload current model * Reload current model
*/ */
@@ -154,9 +143,9 @@ public class DeckController<T extends DeckBase> {
public void load(final String path, final String name) { public void load(final String path, final String name) {
if ( StringUtils.isBlank(path)) if ( StringUtils.isBlank(path))
folder = rootFolder; currentFolder = rootFolder;
else else
folder = rootFolder.tryGetFolder(path); currentFolder = rootFolder.tryGetFolder(path);
load(name); load(name);
} }
@@ -166,7 +155,7 @@ public class DeckController<T extends DeckBase> {
* @param name the name * @param name the name
*/ */
@SuppressWarnings("unchecked") private void load(final String name) { @SuppressWarnings("unchecked") private void load(final String name) {
T newModel = this.folder.get(name); T newModel = this.currentFolder.get(name);
if (newModel != null) { if (newModel != null) {
this.setModel((T) newModel.copyTo(name), true); this.setModel((T) newModel.copyTo(name), true);
} }
@@ -184,7 +173,7 @@ public class DeckController<T extends DeckBase> {
return; return;
} }
this.folder.add(this.model); this.currentFolder.add(this.model);
// copy to new instance which will be edited and left if unsaved // copy to new instance which will be edited and left if unsaved
this.model = (T)this.model.copyTo(this.model.getName()); this.model = (T)this.model.copyTo(this.model.getName());
this.modelInStorage = true; this.modelInStorage = true;
@@ -217,7 +206,7 @@ public class DeckController<T extends DeckBase> {
*/ */
public void delete() { public void delete() {
if (StringUtils.isNotBlank(this.model.getName())) { if (StringUtils.isNotBlank(this.model.getName())) {
this.folder.delete(this.model.getName()); this.currentFolder.delete(this.model.getName());
} }
this.modelInStorage = false; this.modelInStorage = false;
this.newModel(); this.newModel();
@@ -230,7 +219,7 @@ public class DeckController<T extends DeckBase> {
* @return true, if successful * @return true, if successful
*/ */
public boolean fileExists(final String deckName) { public boolean fileExists(final String deckName) {
return this.folder.contains(deckName); return this.currentFolder.contains(deckName);
} }
/** /**

View File

@@ -213,7 +213,7 @@ public final class DeckManager extends ItemManager<DeckProxy> {
}, getFilter(DeckColorFilter.class) == null); }, getFilter(DeckColorFilter.class) == null);
} }
private <T extends DeckBase> void editDeck(final DeckProxy deck) { private void editDeck(final DeckProxy deck) {
if (deck == null || this.preventEdit) { return; } if (deck == null || this.preventEdit) { return; }
ACEditorBase<? extends InventoryItem, ? extends DeckBase> editorCtrl = null; ACEditorBase<? extends InventoryItem, ? extends DeckBase> editorCtrl = null;
@@ -261,8 +261,9 @@ public final class DeckManager extends ItemManager<DeckProxy> {
return false; return false;
} }
final CardCollections deckManager = Singletons.getModel().getDecks();
// consider using deck proxy's method to delete deck
final CardCollections deckManager = Singletons.getModel().getDecks();
switch(this.gametype) { switch(this.gametype) {
case Constructed: case Constructed:
deckManager.getConstructed().delete(deck.getName()); break; deckManager.getConstructed().delete(deck.getName()); break;