mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-18 19:58:00 +00:00
Code cleanup
This commit is contained in:
@@ -28,22 +28,21 @@ import com.google.common.collect.Iterables;
|
||||
|
||||
import forge.util.IItemReader;
|
||||
|
||||
//reads and writeDeck Deck objects
|
||||
/**
|
||||
* <p>
|
||||
* DeckManager class.
|
||||
* StorageBase class.
|
||||
* </p>
|
||||
*
|
||||
* @param <T> the generic type
|
||||
* @author Forge
|
||||
* @version $Id: DeckManager.java 13590 2012-01-27 20:46:27Z Max mtg $
|
||||
* @version $Id: StorageBase.java 13590 2012-01-27 20:46:27Z Max mtg $
|
||||
*/
|
||||
public class StorageBase<T> implements IStorage<T> {
|
||||
protected final Map<String, T> map;
|
||||
|
||||
public final static StorageBase<?> emptyMap = new StorageBase<Object>("Empty", new HashMap<String, Object>());
|
||||
public final String name;
|
||||
|
||||
|
||||
public StorageBase(final String name, final IItemReader<T> io) {
|
||||
this.name = name;
|
||||
this.map = io.readAll();
|
||||
@@ -52,13 +51,12 @@ public class StorageBase<T> implements IStorage<T> {
|
||||
public StorageBase(final String name, final Map<String, T> inMap) {
|
||||
this.name = name;
|
||||
this.map = inMap;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public T get(final String name) {
|
||||
return this.map.get(name);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public final Collection<String> getItemNames() {
|
||||
@@ -84,16 +82,14 @@ public class StorageBase<T> implements IStorage<T> {
|
||||
public T find(Predicate<T> condition) {
|
||||
return Iterables.tryFind(map.values(), condition).orNull();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void add(T deck) {
|
||||
public void add(T item) {
|
||||
throw new UnsupportedOperationException("This is a read-only storage");
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String deckName) {
|
||||
public void delete(String itemName) {
|
||||
throw new UnsupportedOperationException("This is a read-only storage");
|
||||
}
|
||||
|
||||
|
||||
@@ -34,10 +34,8 @@ import forge.util.FileUtil;
|
||||
* the generic type
|
||||
*/
|
||||
public abstract class StorageReaderFile<T> extends StorageReaderBase<T> {
|
||||
|
||||
private final File file;
|
||||
|
||||
|
||||
/**
|
||||
* Instantiates a new storage reader file.
|
||||
*
|
||||
|
||||
@@ -36,7 +36,6 @@ import forge.util.FileUtil;
|
||||
* the generic type
|
||||
*/
|
||||
public abstract class StorageReaderFileSections<T> extends StorageReaderBase<T> {
|
||||
|
||||
private final File file;
|
||||
|
||||
public StorageReaderFileSections(final String pathname, final Function<? super T, String> keySelector0) {
|
||||
@@ -46,7 +45,6 @@ public abstract class StorageReaderFileSections<T> extends StorageReaderBase<T>
|
||||
public StorageReaderFileSections(final File file0, final Function<? super T, String> keySelector0) {
|
||||
super(keySelector0);
|
||||
this.file = file0;
|
||||
|
||||
}
|
||||
|
||||
protected Map<String, T> createMap() {
|
||||
@@ -136,5 +134,4 @@ public abstract class StorageReaderFileSections<T> extends StorageReaderBase<T>
|
||||
public String getItemKey(final T item) {
|
||||
return this.keySelector.apply(item);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -38,7 +38,6 @@ import com.google.common.base.Function;
|
||||
* @param <T> the generic type
|
||||
*/
|
||||
public abstract class StorageReaderFolder<T> extends StorageReaderBase<T> {
|
||||
|
||||
/**
|
||||
* @return the directory
|
||||
*/
|
||||
@@ -46,18 +45,17 @@ public abstract class StorageReaderFolder<T> extends StorageReaderBase<T> {
|
||||
return directory;
|
||||
}
|
||||
|
||||
|
||||
protected final File directory;
|
||||
|
||||
/**
|
||||
* Instantiates a new storage reader folder.
|
||||
*
|
||||
* @param deckDir0 the deck dir0
|
||||
* @param itemDir0 the item dir0
|
||||
*/
|
||||
public StorageReaderFolder(final File deckDir0, Function<? super T, String> keySelector0) {
|
||||
public StorageReaderFolder(final File itemDir0, Function<? super T, String> keySelector0) {
|
||||
super(keySelector0);
|
||||
|
||||
this.directory = deckDir0;
|
||||
this.directory = itemDir0;
|
||||
|
||||
if (this.directory == null) {
|
||||
throw new IllegalArgumentException("No directory specified");
|
||||
@@ -125,7 +123,6 @@ public abstract class StorageReaderFolder<T> extends StorageReaderBase<T> {
|
||||
public String getItemKey(T item) {
|
||||
return keySelector.apply(item);
|
||||
}
|
||||
|
||||
|
||||
// methods handling nested folders are provided. It's up to consumer whether to use these or not.
|
||||
@Override
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package forge.gui.deckchooser;
|
||||
package forge.gui.deckchooser;
|
||||
|
||||
import java.awt.Toolkit;
|
||||
import java.awt.datatransfer.StringSelection;
|
||||
@@ -58,29 +58,32 @@ public class DeckgenUtil {
|
||||
* @return {@link forge.deck.Deck}
|
||||
*/
|
||||
public static Deck buildColorDeck(List<String> selection, boolean forAi) {
|
||||
|
||||
final Deck deck;
|
||||
String deckName = null;
|
||||
|
||||
String deckName = null;
|
||||
|
||||
DeckGeneratorBase gen = null;
|
||||
CardDb cardDb = Singletons.getMagicDb().getCommonCards();
|
||||
if (selection.size() == 1) {
|
||||
gen = new DeckGeneratorMonoColor(cardDb, selection.get(0));
|
||||
} else if (selection.size() == 2) {
|
||||
}
|
||||
else if (selection.size() == 2) {
|
||||
gen = new DeckGenerator2Color(cardDb, selection.get(0), selection.get(1));
|
||||
} else if (selection.size() == 3) {
|
||||
}
|
||||
else if (selection.size() == 3) {
|
||||
gen = new DeckGenerator3Color(cardDb, selection.get(0), selection.get(1), selection.get(2));
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
gen = new DeckGenerator5Color(cardDb);
|
||||
deckName = "5 colors";
|
||||
}
|
||||
gen.setSingleton(Singletons.getModel().getPreferences().getPrefBoolean(FPref.DECKGEN_SINGLETONS));
|
||||
gen.setUseArtifacts(Singletons.getModel().getPreferences().getPrefBoolean(FPref.DECKGEN_ARTIFACTS));
|
||||
ItemPoolView<PaperCard> cards = gen == null ? null : gen.getDeck(60, forAi);
|
||||
|
||||
if(null == deckName)
|
||||
|
||||
if (null == deckName) {
|
||||
deckName = Lang.joinHomogenous(Arrays.asList(selection));
|
||||
|
||||
}
|
||||
|
||||
// After generating card lists, build deck.
|
||||
deck = new Deck("Random deck : " + deckName);
|
||||
deck.getMain().addAll(cards);
|
||||
@@ -112,30 +115,32 @@ public class DeckgenUtil {
|
||||
IStorage<Deck> path = Singletons.getModel().getDecks().getConstructed();
|
||||
String name = selection;
|
||||
int idxSlash = name.indexOf('/');
|
||||
while( idxSlash > 0 && path != null) {
|
||||
while (idxSlash > 0 && path != null) {
|
||||
String sf = name.substring(0, idxSlash).trim();
|
||||
path = path.getFolders().get(sf);
|
||||
name = name.substring(idxSlash+1).trim();
|
||||
idxSlash = name.indexOf('/');
|
||||
};
|
||||
if ( path == null )
|
||||
if (path == null) {
|
||||
throw new IllegalArgumentException("Path not found - " + selection);
|
||||
}
|
||||
return path.get(name);
|
||||
}
|
||||
|
||||
|
||||
public static Deck getPreconDeck(String selection) {
|
||||
return QuestController.getPrecons().get(selection).getDeck();
|
||||
}
|
||||
|
||||
public static QuestEvent getQuestEvent(final String name) {
|
||||
QuestController qCtrl = Singletons.getModel().getQuest();
|
||||
for(QuestEventChallenge challenge : qCtrl.getChallenges()) {
|
||||
if( challenge.getTitle().equals(name) )
|
||||
for (QuestEventChallenge challenge : qCtrl.getChallenges()) {
|
||||
if (challenge.getTitle().equals(name)) {
|
||||
return challenge;
|
||||
}
|
||||
}
|
||||
|
||||
QuestEventDuel duel = Iterables.find(qCtrl.getDuelsManager().getAllDuels(), new Predicate<QuestEventDuel>() {
|
||||
@Override public boolean apply(QuestEventDuel in) { return in.getName().equals(name); }
|
||||
@Override public boolean apply(QuestEventDuel in) { return in.getName().equals(name); }
|
||||
});
|
||||
return duel;
|
||||
}
|
||||
@@ -167,13 +172,12 @@ public class DeckgenUtil {
|
||||
final String name = allDecks.getItemNames().toArray(new String[0])[rand];
|
||||
return allDecks.get(name);
|
||||
}
|
||||
|
||||
|
||||
/** @return {@link forge.deck.Deck} */
|
||||
public static Deck getRandomQuestDeck() {
|
||||
final List<Deck> allQuestDecks = new ArrayList<Deck>();
|
||||
QuestController qCtrl = Singletons.getModel().getQuest();
|
||||
|
||||
|
||||
for (final QuestEvent e : qCtrl.getDuelsManager().getAllDuels()) {
|
||||
allQuestDecks.add(e.getEventDeck());
|
||||
}
|
||||
@@ -189,20 +193,22 @@ public class DeckgenUtil {
|
||||
public static int[] randomSelectColors(int maxColors) {
|
||||
int nColors = MyRandom.getRandom().nextInt(3) + 1;
|
||||
int[] result = new int[nColors];
|
||||
for(int i = 0; i < nColors; i++) {
|
||||
for (int i = 0; i < nColors; i++) {
|
||||
int next = MyRandom.getRandom().nextInt(maxColors);
|
||||
|
||||
boolean isUnique = true;
|
||||
for(int j = 0; j < i; j++) {
|
||||
if(result[j] == next) {
|
||||
for (int j = 0; j < i; j++) {
|
||||
if (result[j] == next) {
|
||||
isUnique = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(isUnique)
|
||||
if (isUnique) {
|
||||
result[i] = next;
|
||||
else
|
||||
i--; // try over with this number
|
||||
}
|
||||
else {
|
||||
i--; // try over with this number
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -224,9 +230,8 @@ public class DeckgenUtil {
|
||||
* @param lst0 {@link javax.swing.JList}
|
||||
*/
|
||||
public static void showDecklist(final Deck deck) {
|
||||
|
||||
if (deck == null) { return; }
|
||||
|
||||
|
||||
// Dump into map and display.
|
||||
final String nl = System.getProperty("line.separator");
|
||||
final StringBuilder deckList = new StringBuilder();
|
||||
@@ -234,16 +239,17 @@ public class DeckgenUtil {
|
||||
deckList.append(dName == null ? "" : dName + nl + nl);
|
||||
|
||||
int nLines = 0;
|
||||
for(DeckSection s : DeckSection.values()){
|
||||
for (DeckSection s : DeckSection.values()){
|
||||
CardPool cp = deck.get(s);
|
||||
if ( cp == null || cp.isEmpty() )
|
||||
if (cp == null || cp.isEmpty()) {
|
||||
continue;
|
||||
|
||||
}
|
||||
deckList.append(s.toString()).append(": ");
|
||||
if ( s.isSingleCard() ) {
|
||||
if (s.isSingleCard()) {
|
||||
deckList.append(cp.get(0).getName()).append(nl);
|
||||
nLines++;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
deckList.append(nl);
|
||||
nLines++;
|
||||
for (final Entry<PaperCard, Integer> ev : cp) {
|
||||
@@ -255,11 +261,11 @@ public class DeckgenUtil {
|
||||
nLines++;
|
||||
}
|
||||
|
||||
|
||||
final StringBuilder msg = new StringBuilder();
|
||||
if (nLines <= 32) {
|
||||
msg.append(deckList.toString());
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
msg.append("Decklist too long for dialog." + nl + nl);
|
||||
}
|
||||
|
||||
@@ -315,14 +321,15 @@ public class DeckgenUtil {
|
||||
if (appearances < 2) {
|
||||
schemes.add(cp);
|
||||
schemesToAdd--;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
attemptsLeft--;
|
||||
}
|
||||
}
|
||||
|
||||
return schemes;
|
||||
}
|
||||
|
||||
|
||||
public static CardPool generatePlanarDeck() {
|
||||
CardPool res = new CardPool();
|
||||
List<PaperCard> allPlanars = new ArrayList<PaperCard>();
|
||||
@@ -334,23 +341,19 @@ public class DeckgenUtil {
|
||||
|
||||
int phenoms = 0;
|
||||
int targetsize = MyRandom.getRandom().nextInt(allPlanars.size()-10)+10;
|
||||
while(true)
|
||||
{
|
||||
while (true) {
|
||||
PaperCard rndPlane = Aggregates.random(allPlanars);
|
||||
allPlanars.remove(rndPlane);
|
||||
|
||||
if(rndPlane.getRules().getType().isPhenomenon() && phenoms < 2)
|
||||
{
|
||||
|
||||
if (rndPlane.getRules().getType().isPhenomenon() && phenoms < 2) {
|
||||
res.add(rndPlane);
|
||||
phenoms++;
|
||||
}
|
||||
else if (rndPlane.getRules().getType().isPlane())
|
||||
{
|
||||
else if (rndPlane.getRules().getType().isPlane()) {
|
||||
res.add(rndPlane);
|
||||
}
|
||||
|
||||
if(allPlanars.isEmpty() || res.countAll() == targetsize)
|
||||
{
|
||||
|
||||
if (allPlanars.isEmpty() || res.countAll() == targetsize) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,7 +41,6 @@ import forge.util.storage.IStorage;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class FDeckChooser extends JPanel implements IDecksComboBoxListener {
|
||||
|
||||
private final Color BORDER_COLOR = FSkin.getColor(FSkin.Colors.CLR_TEXT).getColor().darker();
|
||||
|
||||
private boolean isUISetup = false;
|
||||
@@ -116,7 +115,6 @@ public class FDeckChooser extends JPanel implements IDecksComboBoxListener {
|
||||
lst.setSelectedIndices(getSelectedDeckIndices(Arrays.asList(listData), new int[]{0, 1}));
|
||||
lst.ensureIndexIsVisible(lst.getSelectedIndices()[0]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void updateThemes() {
|
||||
@@ -141,7 +139,6 @@ public class FDeckChooser extends JPanel implements IDecksComboBoxListener {
|
||||
lst.setSelectedIndices(getSelectedDeckIndices(listData, new int[]{0}));
|
||||
lst.ensureIndexIsVisible(lst.getSelectedIndices()[0]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void updateCustom() {
|
||||
@@ -226,7 +223,6 @@ public class FDeckChooser extends JPanel implements IDecksComboBoxListener {
|
||||
lst.setSelectedIndices(getSelectedDeckIndices(listData, new int[]{0}));
|
||||
lst.ensureIndexIsVisible(lst.getSelectedIndices()[0]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public Deck getDeck() {
|
||||
@@ -236,17 +232,21 @@ public class FDeckChooser extends JPanel implements IDecksComboBoxListener {
|
||||
if (selection.isEmpty()) { return null; }
|
||||
|
||||
// Special branch for quest events
|
||||
if (lst0.getName().equals(DeckgenUtil.DeckTypes.QUESTEVENTS.toString()))
|
||||
if (lst0.getName().equals(DeckgenUtil.DeckTypes.QUESTEVENTS.toString())) {
|
||||
return DeckgenUtil.getQuestEvent(selection.get(0)).getEventDeck();
|
||||
if (lst0.getName().equals(DeckgenUtil.DeckTypes.COLORS.toString()) && DeckgenUtil.colorCheck(selection))
|
||||
}
|
||||
if (lst0.getName().equals(DeckgenUtil.DeckTypes.COLORS.toString()) && DeckgenUtil.colorCheck(selection)) {
|
||||
return DeckgenUtil.buildColorDeck(selection, isAi);
|
||||
if (lst0.getName().equals(DeckgenUtil.DeckTypes.THEMES.toString()))
|
||||
}
|
||||
if (lst0.getName().equals(DeckgenUtil.DeckTypes.THEMES.toString())) {
|
||||
return DeckgenUtil.buildThemeDeck(selection.get(0));
|
||||
if (lst0.getName().equals(DeckgenUtil.DeckTypes.CUSTOM.toString()))
|
||||
}
|
||||
if (lst0.getName().equals(DeckgenUtil.DeckTypes.CUSTOM.toString())) {
|
||||
return DeckgenUtil.getConstructedDeck(selection.get(0));
|
||||
if (lst0.getName().equals(DeckgenUtil.DeckTypes.PRECON.toString()))
|
||||
}
|
||||
if (lst0.getName().equals(DeckgenUtil.DeckTypes.PRECON.toString())) {
|
||||
return DeckgenUtil.getPreconDeck(selection.get(0));
|
||||
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -268,7 +268,6 @@ public class FDeckChooser extends JPanel implements IDecksComboBoxListener {
|
||||
return new RegisteredPlayer(getDeck());
|
||||
}
|
||||
|
||||
|
||||
public final boolean isAi() {
|
||||
return isAi;
|
||||
}
|
||||
@@ -424,5 +423,4 @@ public class FDeckChooser extends JPanel implements IDecksComboBoxListener {
|
||||
return defaultSelection;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -23,10 +23,9 @@ import com.google.common.base.Function;
|
||||
|
||||
import forge.util.IItemSerializer;
|
||||
|
||||
//reads and writeDeck Deck objects
|
||||
/**
|
||||
* <p>
|
||||
* DeckManager class.
|
||||
* StorageImmediatelySerialized class.
|
||||
* </p>
|
||||
*
|
||||
* @param <T> the generic type
|
||||
@@ -34,7 +33,6 @@ import forge.util.IItemSerializer;
|
||||
* @version $Id$
|
||||
*/
|
||||
public class StorageImmediatelySerialized<T> extends StorageBase<T> {
|
||||
|
||||
private final IItemSerializer<T> serializer;
|
||||
private final IStorage<IStorage<T>> subfolders;
|
||||
|
||||
@@ -44,10 +42,10 @@ public class StorageImmediatelySerialized<T> extends StorageBase<T> {
|
||||
return new StorageImmediatelySerialized<T>(file.getName(), (IItemSerializer<T>) serializer.getReaderForFolder(file), true);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Constructor for DeckManager.
|
||||
* Constructor for StorageImmediatelySerialized.
|
||||
* </p>
|
||||
*
|
||||
* @param io the io
|
||||
@@ -55,8 +53,7 @@ public class StorageImmediatelySerialized<T> extends StorageBase<T> {
|
||||
public StorageImmediatelySerialized(String name, final IItemSerializer<T> io) {
|
||||
this(name, io, false);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public StorageImmediatelySerialized(String name, final IItemSerializer<T> io, boolean withSubFolders) {
|
||||
super(name, io);
|
||||
this.serializer = io;
|
||||
@@ -66,7 +63,7 @@ public class StorageImmediatelySerialized<T> extends StorageBase<T> {
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see forge.deck.IFolderMap#add(T)
|
||||
* @see forge.util.storage.StorageBase#add(T)
|
||||
*/
|
||||
@Override
|
||||
public final void add(final T deck) {
|
||||
@@ -78,13 +75,13 @@ public class StorageImmediatelySerialized<T> extends StorageBase<T> {
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see forge.deck.IFolderMap#delete(java.lang.String)
|
||||
* @see forge.util.storage.StorageBase#delete(java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public final void delete(final String deckName) {
|
||||
this.serializer.erase(this.map.remove(deckName));
|
||||
}
|
||||
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see forge.util.storage.StorageBase#getFolders()
|
||||
*/
|
||||
|
||||
@@ -5,16 +5,13 @@ import java.util.HashMap;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
|
||||
|
||||
public class StorageNestedFolders<T> extends StorageBase<IStorage<T>> {
|
||||
|
||||
private final File thisFolder;
|
||||
|
||||
public StorageNestedFolders(File thisFolder, Iterable<File> subfolders, Function<File, IStorage<T>> factory) {
|
||||
super("<Subfolders>", new HashMap<String, IStorage<T>>());
|
||||
this.thisFolder = thisFolder;
|
||||
for(File sf : subfolders )
|
||||
{
|
||||
for (File sf : subfolders ) {
|
||||
IStorage<T> newUnit = factory.apply(sf);
|
||||
map.put(sf.getName(), newUnit);
|
||||
}
|
||||
@@ -37,8 +34,8 @@ public class StorageNestedFolders<T> extends StorageBase<IStorage<T>> {
|
||||
IStorage<T> f = map.remove(deckName);
|
||||
|
||||
// TODO: Clear all that files from disk
|
||||
if ( f != null )
|
||||
if (f != null) {
|
||||
subdir.delete(); // won't work if not empty;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user