save/delete methods implemented on DeckProxy

This commit is contained in:
Maxmtg
2014-01-22 05:06:56 +00:00
parent 9636e3d2e4
commit 9bcc6cbc64
2 changed files with 25 additions and 11 deletions

View File

@@ -139,4 +139,12 @@ public class DeckGroup extends DeckBase {
return arg1.getName(); return arg1.getName();
} }
}; };
public static final Function<DeckGroup, Deck> FN_HUMAN_DECK = new Function<DeckGroup, Deck>() {
@Override
public Deck apply(DeckGroup arg1) {
return arg1.getHumanDeck();
}
};
} }

View File

@@ -26,7 +26,9 @@ import forge.quest.QuestController;
import forge.quest.QuestEvent; import forge.quest.QuestEvent;
import forge.util.IHasName; import forge.util.IHasName;
import forge.util.storage.IStorage; import forge.util.storage.IStorage;
import forge.util.storage.StorageImmediatelySerialized;
// Adding a generic to this class creates compile problems in ItemManager (that I can not fix)
public class DeckProxy implements InventoryItem { public class DeckProxy implements InventoryItem {
protected final IHasName deck; protected final IHasName deck;
protected final IStorage<?> storage; protected final IStorage<?> storage;
@@ -133,16 +135,7 @@ public class DeckProxy implements InventoryItem {
return false; return false;
} }
public void updateInStorage() {
// if storage is not readonly, save the deck there.
}
public void deleteFromStorage() {
// if storage is not readonly, delete the deck from there.
}
// TODO: The methods below should not take the decks collections from singletons, instead they are supposed to use data passed in parameters // 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(IStorage<Deck> storageRoot) { public static Iterable<DeckProxy> getAllConstructedDecks(IStorage<Deck> storageRoot) {
List<DeckProxy> result = new ArrayList<DeckProxy>(); List<DeckProxy> result = new ArrayList<DeckProxy>();
addDecksRecursivelly(result, "", storageRoot); addDecksRecursivelly(result, "", storageRoot);
@@ -185,6 +178,17 @@ public class DeckProxy implements InventoryItem {
}; };
} }
@SuppressWarnings("unchecked")
public void updateInStorage() {
if ( storage instanceof StorageImmediatelySerialized<?> )
((StorageImmediatelySerialized<IHasName>)storage).add(deck);
}
public void deleteFromStorage() {
if ( storage instanceof StorageImmediatelySerialized<?> )
storage.delete(getName());
}
private static class ThemeDeckGenerator extends DeckProxy { private static class ThemeDeckGenerator extends DeckProxy {
private final String name; private final String name;
public ThemeDeckGenerator(String name0) { public ThemeDeckGenerator(String name0) {
@@ -245,13 +249,14 @@ public class DeckProxy implements InventoryItem {
return decks; return decks;
} }
@SuppressWarnings("unchecked")
public static Iterable<DeckProxy> getAllSealedDecks(IStorage<DeckGroup> sealed) { public static Iterable<DeckProxy> getAllSealedDecks(IStorage<DeckGroup> sealed) {
final List<DeckProxy> humanDecks = new ArrayList<DeckProxy>(); final List<DeckProxy> humanDecks = new ArrayList<DeckProxy>();
// Since AI decks are tied directly to the human choice, // Since AI decks are tied directly to the human choice,
// they're just mapped in a parallel map and grabbed when the game starts. // they're just mapped in a parallel map and grabbed when the game starts.
for (final DeckGroup d : sealed) { for (final DeckGroup d : sealed) {
humanDecks.add(new DeckProxy(d.getHumanDeck(), GameType.Sealed, sealed)); humanDecks.add(new DeckProxy(d, (Function<IHasName, Deck>)(Object)DeckGroup.FN_HUMAN_DECK, GameType.Sealed, sealed));
} }
return humanDecks; return humanDecks;
} }
@@ -266,10 +271,11 @@ public class DeckProxy implements InventoryItem {
return decks; return decks;
} }
@SuppressWarnings("unchecked")
public static Iterable<DeckProxy> getDraftDecks(IStorage<DeckGroup> draft) { public static Iterable<DeckProxy> getDraftDecks(IStorage<DeckGroup> draft) {
ArrayList<DeckProxy> decks = new ArrayList<DeckProxy>(); ArrayList<DeckProxy> decks = new ArrayList<DeckProxy>();
for (DeckGroup d : draft) { for (DeckGroup d : draft) {
decks.add(new DeckProxy(d.getHumanDeck(), GameType.Draft, draft)); decks.add(new DeckProxy(d, ((Function<IHasName, Deck>)(Object)DeckGroup.FN_HUMAN_DECK), GameType.Draft, draft));
} }
return decks; return decks;
} }