Support directory being included in Deck object

This commit is contained in:
drdev
2015-10-04 21:01:35 +00:00
parent 88cbb9a6f8
commit d89d6ae951
4 changed files with 72 additions and 48 deletions

View File

@@ -19,17 +19,16 @@ package forge.deck;
import forge.item.InventoryItem; import forge.item.InventoryItem;
import java.io.File;
import java.io.Serializable; import java.io.Serializable;
/**
* TODO: Write javadoc for this type.
*
*/
public abstract class DeckBase implements Serializable, Comparable<DeckBase>, InventoryItem { public abstract class DeckBase implements Serializable, Comparable<DeckBase>, InventoryItem {
private static final long serialVersionUID = -7538150536939660052L; private static final long serialVersionUID = -7538150536939660052L;
// gameType is from Constant.GameType, like GameType.Regular // gameType is from Constant.GameType, like GameType.Regular
private final String name; private final String name;
private transient String directory;
private String comment = null; private String comment = null;
/** /**
@@ -38,7 +37,7 @@ public abstract class DeckBase implements Serializable, Comparable<DeckBase>, In
* @param name0 the name0 * @param name0 the name0
*/ */
public DeckBase(final String name0) { public DeckBase(final String name0) {
this.name = name0.replace('/', '_'); name = name0.replace('/', '_');
} }
/* (non-Javadoc) /* (non-Javadoc)
@@ -46,7 +45,7 @@ public abstract class DeckBase implements Serializable, Comparable<DeckBase>, In
*/ */
@Override @Override
public int compareTo(final DeckBase d) { public int compareTo(final DeckBase d) {
return this.getName().compareTo(d.getName()); return name.compareTo(d.name);
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
@@ -54,7 +53,7 @@ public abstract class DeckBase implements Serializable, Comparable<DeckBase>, In
public boolean equals(final Object o) { public boolean equals(final Object o) {
if (o instanceof DeckBase) { if (o instanceof DeckBase) {
final DeckBase d = (DeckBase) o; final DeckBase d = (DeckBase) o;
return this.getName().equals(d.getName()); return name.equals(d.name);
} }
return false; return false;
} }
@@ -66,19 +65,28 @@ public abstract class DeckBase implements Serializable, Comparable<DeckBase>, In
*/ */
@Override @Override
public int hashCode() { public int hashCode() {
return (this.name.hashCode() * 17) + this.name.hashCode(); return (name.hashCode() * 17) + name.hashCode();
} }
/* (non-Javadoc)
* @see forge.util.IHasName#getName()
*/
public String getName() { public String getName() {
return this.name; return name;
}
public String getDirectory() {
return directory;
}
public void setDirectory(File file, String rootDir) {
directory = file.getParent().substring(rootDir.length());
}
public String getUniqueKey() {
if (directory == null) { return name; }
return directory + "/" + name;
} }
@Override @Override
public String toString() { public String toString() {
return this.name; return name;
} }
/** /**
@@ -86,8 +94,8 @@ public abstract class DeckBase implements Serializable, Comparable<DeckBase>, In
* *
* @param comment the new comment * @param comment the new comment
*/ */
public void setComment(final String comment) { public void setComment(final String comment0) {
this.comment = comment; comment = comment0;
} }
/** /**
@@ -98,7 +106,7 @@ public abstract class DeckBase implements Serializable, Comparable<DeckBase>, In
* @return a {@link java.lang.String} object. * @return a {@link java.lang.String} object.
*/ */
public String getComment() { public String getComment() {
return this.comment; return comment;
} }
/** /**
@@ -115,7 +123,8 @@ public abstract class DeckBase implements Serializable, Comparable<DeckBase>, In
* @param clone the clone * @param clone the clone
*/ */
protected void cloneFieldsTo(final DeckBase clone) { protected void cloneFieldsTo(final DeckBase clone) {
clone.comment = this.comment; clone.directory = directory;
clone.comment = comment;
} }
/** /**
@@ -125,8 +134,8 @@ public abstract class DeckBase implements Serializable, Comparable<DeckBase>, In
* @return the deck base * @return the deck base
*/ */
public DeckBase copyTo(final String name0) { public DeckBase copyTo(final String name0) {
final DeckBase obj = this.newInstance(name0); final DeckBase obj = newInstance(name0);
this.cloneFieldsTo(obj); cloneFieldsTo(obj);
return obj; return obj;
} }
@@ -136,7 +145,7 @@ public abstract class DeckBase implements Serializable, Comparable<DeckBase>, In
* @return the best file name * @return the best file name
*/ */
public final String getBestFileName() { public final String getBestFileName() {
return this.getName().replaceAll("[^-_$#@.,{[()]} a-zA-Z0-9]", ""); return name.replaceAll("[^-_$#@.,{[()]} a-zA-Z0-9]", "");
} }
public abstract boolean isEmpty(); public abstract boolean isEmpty();

View File

@@ -18,10 +18,12 @@
package forge.deck.io; package forge.deck.io;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import forge.deck.Deck; import forge.deck.Deck;
import forge.deck.DeckGroup; import forge.deck.DeckGroup;
import forge.util.IItemSerializer; import forge.util.IItemSerializer;
import forge.util.storage.StorageReaderFolder; import forge.util.storage.StorageReaderFolder;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import java.io.File; import java.io.File;
@@ -33,15 +35,18 @@ import java.util.List;
* *
*/ */
public class DeckGroupSerializer extends StorageReaderFolder<DeckGroup> implements IItemSerializer<DeckGroup> { public class DeckGroupSerializer extends StorageReaderFolder<DeckGroup> implements IItemSerializer<DeckGroup> {
private final String humanDeckFile = "human.dck"; private static final String humanDeckFile = "human.dck";
private final String rootDir;
/** /**
* Instantiates a new deck group serializer. * Instantiates a new deck group serializer.
* *
* @param deckDir0 the deck dir0 * @param deckDir0 the deck dir0
*/ */
public DeckGroupSerializer(final File deckDir0) { public DeckGroupSerializer(final File deckDir0, String rootDir0) {
super(deckDir0, DeckGroup.FN_NAME_SELECTOR); super(deckDir0, DeckGroup.FN_NAME_SELECTOR);
rootDir = rootDir0;
} }
/** The Constant MAX_DRAFT_PLAYERS. */ /** The Constant MAX_DRAFT_PLAYERS. */
@@ -54,9 +59,9 @@ public class DeckGroupSerializer extends StorageReaderFolder<DeckGroup> implemen
*/ */
@Override @Override
public void save(final DeckGroup unit) { public void save(final DeckGroup unit) {
final File f = this.makeFileFor(unit); final File f = makeFileFor(unit);
f.mkdir(); f.mkdir();
DeckSerializer.writeDeck(unit.getHumanDeck(), new File(f, this.humanDeckFile)); DeckSerializer.writeDeck(unit.getHumanDeck(), new File(f, humanDeckFile));
final List<Deck> aiDecks = unit.getAiDecks(); final List<Deck> aiDecks = unit.getAiDecks();
for (int i = 1; i <= aiDecks.size(); i++) { for (int i = 1; i <= aiDecks.size(); i++) {
DeckSerializer.writeDeck(aiDecks.get(i - 1), new File(f, "ai-" + i + ".dck")); DeckSerializer.writeDeck(aiDecks.get(i - 1), new File(f, "ai-" + i + ".dck"));
@@ -68,21 +73,24 @@ public class DeckGroupSerializer extends StorageReaderFolder<DeckGroup> implemen
*/ */
@Override @Override
protected final DeckGroup read(final File file) { protected final DeckGroup read(final File file) {
final Deck humanDeck = DeckSerializer.fromFile(new File(file, humanDeckFile));
if (humanDeck == null) { return null; }
final Deck human = DeckSerializer.fromFile(new File(file, this.humanDeckFile)); humanDeck.setDirectory(file, rootDir);
if (null == human) {
return null;
}
final DeckGroup d = new DeckGroup(human.getName()); final DeckGroup d = new DeckGroup(humanDeck.getName());
d.setHumanDeck(human); d.setHumanDeck(humanDeck);
for (int i = 1; i < DeckGroupSerializer.MAX_DRAFT_PLAYERS; i++) { for (int i = 1; i < DeckGroupSerializer.MAX_DRAFT_PLAYERS; i++) {
final File theFile = new File(file, "ai-" + i + ".dck"); final File theFile = new File(file, "ai-" + i + ".dck");
if (!theFile.exists()) { if (!theFile.exists()) {
break; break;
} }
d.addAiDeck(DeckSerializer.fromFile(theFile)); Deck aiDeck = DeckSerializer.fromFile(theFile);
if (aiDeck != null) {
aiDeck.setDirectory(theFile, rootDir);
d.addAiDeck(aiDeck);
}
} }
return d; return d;
} }
@@ -95,7 +103,7 @@ public class DeckGroupSerializer extends StorageReaderFolder<DeckGroup> implemen
*/ */
@Override @Override
public void erase(final DeckGroup unit) { public void erase(final DeckGroup unit) {
final File dir = this.makeFileFor(unit); final File dir = makeFileFor(unit);
final File[] files = dir.listFiles(); final File[] files = dir.listFiles();
for (final File f : files) { for (final File f : files) {
f.delete(); f.delete();
@@ -110,7 +118,7 @@ public class DeckGroupSerializer extends StorageReaderFolder<DeckGroup> implemen
* @return the file * @return the file
*/ */
public File makeFileFor(final DeckGroup decks) { public File makeFileFor(final DeckGroup decks) {
return new File(this.directory, decks.getBestFileName()); return new File(directory, decks.getBestFileName());
} }
/* /*
@@ -127,7 +135,7 @@ public class DeckGroupSerializer extends StorageReaderFolder<DeckGroup> implemen
final File testSubject = new File(dir, name); final File testSubject = new File(dir, name);
final boolean isVisibleFolder = testSubject.isDirectory() && !testSubject.isHidden(); final boolean isVisibleFolder = testSubject.isDirectory() && !testSubject.isHidden();
final boolean hasGoodName = StringUtils.isNotEmpty(name) && !name.startsWith("."); final boolean hasGoodName = StringUtils.isNotEmpty(name) && !name.startsWith(".");
final File fileHumanDeck = new File(testSubject, DeckGroupSerializer.this.humanDeckFile); final File fileHumanDeck = new File(testSubject, DeckGroupSerializer.humanDeckFile);
return isVisibleFolder && hasGoodName && fileHumanDeck.exists(); return isVisibleFolder && hasGoodName && fileHumanDeck.exists();
} }
}; };

View File

@@ -33,9 +33,11 @@ import java.util.Map;
* This class knows how to make a file out of a deck object and vice versa. * This class knows how to make a file out of a deck object and vice versa.
*/ */
public class DeckStorage extends StorageReaderFolder<Deck> implements IItemSerializer<Deck> { public class DeckStorage extends StorageReaderFolder<Deck> implements IItemSerializer<Deck> {
private final boolean moveWronglyNamedDecks;
public static final String FILE_EXTENSION = ".dck"; public static final String FILE_EXTENSION = ".dck";
private final String rootDir;
private final boolean moveWronglyNamedDecks;
/** Constant <code>DCKFileFilter</code>. */ /** Constant <code>DCKFileFilter</code>. */
public static final FilenameFilter DCK_FILE_FILTER = new FilenameFilter() { public static final FilenameFilter DCK_FILE_FILTER = new FilenameFilter() {
@Override @Override
@@ -44,12 +46,13 @@ public class DeckStorage extends StorageReaderFolder<Deck> implements IItemSeria
} }
}; };
public DeckStorage(final File deckDir0) { public DeckStorage(final File deckDir0, final String rootDir0) {
this(deckDir0, false); this(deckDir0, rootDir0, false);
} }
public DeckStorage(final File deckDir0, boolean moveWrongDecks) { public DeckStorage(final File deckDir0, final String rootDir0, boolean moveWrongDecks) {
super(deckDir0, Deck.FN_NAME_SELECTOR); super(deckDir0, Deck.FN_NAME_SELECTOR);
rootDir = rootDir0;
moveWronglyNamedDecks = moveWrongDecks; moveWronglyNamedDecks = moveWrongDecks;
} }
@@ -60,7 +63,7 @@ public class DeckStorage extends StorageReaderFolder<Deck> implements IItemSeria
public IItemReader<Deck> getReaderForFolder(File subfolder) { public IItemReader<Deck> getReaderForFolder(File subfolder) {
if ( !subfolder.getParentFile().equals(directory) ) if ( !subfolder.getParentFile().equals(directory) )
throw new UnsupportedOperationException("Only child folders of " + directory + " may be processed"); throw new UnsupportedOperationException("Only child folders of " + directory + " may be processed");
return new DeckStorage(subfolder, false); return new DeckStorage(subfolder, rootDir, false);
} }
@Override @Override
@@ -85,6 +88,10 @@ public class DeckStorage extends StorageReaderFolder<Deck> implements IItemSeria
if (moveWronglyNamedDecks) { if (moveWronglyNamedDecks) {
adjustFileLocation(file, result); adjustFileLocation(file, result);
} }
if (result != null) {
result.setDirectory(file, rootDir);
}
return result; return result;
} }

View File

@@ -47,15 +47,15 @@ public class CardCollections {
public CardCollections() { public CardCollections() {
final StopWatch sw = new StopWatch(); final StopWatch sw = new StopWatch();
sw.start(); sw.start();
constructed = new StorageImmediatelySerialized<Deck> ("Constructed decks", new DeckStorage(new File(ForgeConstants.DECK_CONSTRUCTED_DIR), true), true); constructed = new StorageImmediatelySerialized<Deck> ("Constructed decks", new DeckStorage(new File(ForgeConstants.DECK_CONSTRUCTED_DIR), ForgeConstants.DECK_BASE_DIR, true), true);
draft = new StorageImmediatelySerialized<DeckGroup>("Draft deck sets", new DeckGroupSerializer(new File(ForgeConstants.DECK_DRAFT_DIR))); draft = new StorageImmediatelySerialized<DeckGroup>("Draft deck sets", new DeckGroupSerializer(new File(ForgeConstants.DECK_DRAFT_DIR), ForgeConstants.DECK_BASE_DIR));
sealed = new StorageImmediatelySerialized<DeckGroup>("Sealed deck sets", new DeckGroupSerializer(new File(ForgeConstants.DECK_SEALED_DIR))); sealed = new StorageImmediatelySerialized<DeckGroup>("Sealed deck sets", new DeckGroupSerializer(new File(ForgeConstants.DECK_SEALED_DIR), ForgeConstants.DECK_BASE_DIR));
winston = new StorageImmediatelySerialized<DeckGroup>("Winston draft deck sets", new DeckGroupSerializer(new File(ForgeConstants.DECK_WINSTON_DIR))); winston = new StorageImmediatelySerialized<DeckGroup>("Winston draft deck sets", new DeckGroupSerializer(new File(ForgeConstants.DECK_WINSTON_DIR), ForgeConstants.DECK_BASE_DIR));
cube = new StorageImmediatelySerialized<Deck> ("Cubes", new DeckStorage(new File(ForgeConstants.DECK_CUBE_DIR))); cube = new StorageImmediatelySerialized<Deck> ("Cubes", new DeckStorage(new File(ForgeConstants.DECK_CUBE_DIR), ForgeConstants.RES_DIR));
scheme = new StorageImmediatelySerialized<Deck> ("Archenemy decks", new DeckStorage(new File(ForgeConstants.DECK_SCHEME_DIR))); scheme = new StorageImmediatelySerialized<Deck> ("Archenemy decks", new DeckStorage(new File(ForgeConstants.DECK_SCHEME_DIR), ForgeConstants.DECK_BASE_DIR));
plane = new StorageImmediatelySerialized<Deck> ("Planechase decks", new DeckStorage(new File(ForgeConstants.DECK_PLANE_DIR))); plane = new StorageImmediatelySerialized<Deck> ("Planechase decks", new DeckStorage(new File(ForgeConstants.DECK_PLANE_DIR), ForgeConstants.DECK_BASE_DIR));
commander = new StorageImmediatelySerialized<Deck> ("Commander decks", new DeckStorage(new File(ForgeConstants.DECK_COMMANDER_DIR))); commander = new StorageImmediatelySerialized<Deck> ("Commander decks", new DeckStorage(new File(ForgeConstants.DECK_COMMANDER_DIR), ForgeConstants.DECK_BASE_DIR));
tinyLeaders = new StorageImmediatelySerialized<Deck> ("Commander decks", new DeckStorage(new File(ForgeConstants.DECK_TINY_LEADERS_DIR))); tinyLeaders = new StorageImmediatelySerialized<Deck> ("Commander decks", new DeckStorage(new File(ForgeConstants.DECK_TINY_LEADERS_DIR), ForgeConstants.DECK_BASE_DIR));
sw.stop(); sw.stop();
System.out.printf("Read decks (%d ms): %d constructed, %d sealed, %d draft, %d cubes, %d scheme, %d planar, %d commander, %d tiny leaders.%n", sw.getTime(), constructed.size(), sealed.size(), draft.size(), cube.size(), scheme.size(), plane.size(), commander.size(), tinyLeaders.size()); System.out.printf("Read decks (%d ms): %d constructed, %d sealed, %d draft, %d cubes, %d scheme, %d planar, %d commander, %d tiny leaders.%n", sw.getTime(), constructed.size(), sealed.size(), draft.size(), cube.size(), scheme.size(), plane.size(), commander.size(), tinyLeaders.size());
} }