Decks now store metadata in key-value pairs. New metadata types can be added to the deck without breaking backward compatibility.

This commit is contained in:
jendave
2011-08-06 22:49:10 +00:00
parent cb4434b4b9
commit 5ed08a85c5
2 changed files with 50 additions and 33 deletions

View File

@@ -12,8 +12,6 @@ public class Deck implements Comparable{
private List<String> main;
private List<String> sideboard;
private transient List<String> mainView;
private transient List<String> sideboardView;
public static final String NAME = "Name";
public static final String DECK_TYPE = "Deck Type";
@@ -23,15 +21,9 @@ public class Deck implements Comparable{
//gameType is from Constant.GameType, like Constant.GameType.Regular
public Deck(String gameType) {
setDeckType(gameType);
setName("");
public Deck() {
main = new ArrayList<String>();
mainView = Collections.unmodifiableList(main);
sideboard = new ArrayList<String>();
sideboardView = Collections.unmodifiableList(sideboard);
}
public Deck(String deckType, List<String> main, List<String> sideboard, String name) {
@@ -39,18 +31,20 @@ public class Deck implements Comparable{
setName(name);
this.main = main;
mainView = Collections.unmodifiableList(main);
this.sideboard = sideboard;
}
this.sideboard = main;
sideboardView = Collections.unmodifiableList(sideboard);
public Deck(String type) {
this();
setDeckType(type);
}
public List<String> getMain() {
return mainView;
return Collections.unmodifiableList(main);
}
public List<String> getSideboard() {
return sideboardView;
return Collections.unmodifiableList(sideboard);
}
public String getDeckType() {
@@ -58,7 +52,7 @@ public class Deck implements Comparable{
}
//can only call this method ONCE
private void setDeckType(String deckType) {
void setDeckType(String deckType) {
if (this.getDeckType() != null) {
throw new IllegalStateException(
"Deck : setDeckType() error, deck type has already been set");
@@ -78,7 +72,7 @@ public class Deck implements Comparable{
public String getName() {
return metadata.get(NAME);
}//may return null
}
public void setComment(String comment) {
metadata.put(COMMENT, comment);
@@ -174,4 +168,12 @@ public class Deck implements Comparable{
}
return 0;
}
public Set<Map.Entry<String,String>> getMetadata() {
return metadata.entrySet();
}
public void addMetaData(String key, String value) {
metadata.put(key, value);
}
}

View File

@@ -34,10 +34,6 @@ public class DeckManager {
Map<String, Deck> deckMap;
Map<String, Deck[]> boosterMap;
public DeckManager(String fileName) {
this(new File(fileName));
}
public DeckManager(File deckDir) {
if (deckDir == null) {
throw new IllegalArgumentException("No deck directory specified");
@@ -173,15 +169,25 @@ public class DeckManager {
ListIterator<String> lineIterator = lines.listIterator();
String firstLine = lineIterator.next();
String line = lineIterator.next();
//Old text-based format
if (!firstLine.equals("[Metadata]")) {
if (!line.equals("[metadata]")) {
lineIterator.previous();
return readDeckOld(lineIterator);
}
return null;
Deck d = new Deck();
//read metadata
while(!(line = lineIterator.next()).equals("[main]")){
String[] linedata = line.split("=",2);
d.addMetaData(linedata[0], linedata[1]);
}
addCardList(lineIterator, d);
return d;
}
@@ -206,19 +212,28 @@ public class DeckManager {
//readDeck deck type
String deckType = iterator.next();
Deck d = new Deck(deckType);
Deck d = new Deck();
d.setName(name);
d.setComment(comment);
d.setDeckType(deckType);
//go to [main]
while ((line = iterator.next()) != null && !line.equals("[main]")) {
System.err.println("unexpected line: " + line);
}
addCardList(iterator, d);
return d;
}
private static void addCardList(ListIterator<String> lineIterator, Deck d) {
String line;
Pattern p = Pattern.compile("\\s*((\\d+)\\s+)?(.*?)\\s*");
//readDeck main deck
while (iterator.hasNext() && !(line = iterator.next()).equals("[sideboard]")) {
while (lineIterator.hasNext() && !(line = lineIterator.next()).equals("[sideboard]")) {
Matcher m = p.matcher(line);
m.matches();
String s = m.group(2);
@@ -230,7 +245,8 @@ public class DeckManager {
}
//readDeck sideboard
while (iterator.hasNext()) {
while (lineIterator.hasNext()) {
line = lineIterator.next();
Matcher m = p.matcher(line);
m.matches();
String s = m.group(2);
@@ -239,8 +255,6 @@ public class DeckManager {
d.addSideboard(m.group(3));
}
}
return d;
}
private String deriveFileName(String deckName) {
@@ -296,12 +310,13 @@ public class DeckManager {
}
private void write(Deck d, BufferedWriter out) throws IOException {
out.write(format("%s%n", d.getName()));
if (d.getComment() != null) {
out.write(format("%s%n", d.getComment()));
out.write("[metadata]\n");
for (Entry<String, String> entry : d.getMetadata()) {
if (entry.getValue() != null)
out.write(format("%s=%s%n", entry.getKey(), entry.getValue().replaceAll("\n", "")));
}
out.write(format("%s%n", "[general]"));
out.write(format("%s%n", d.getDeckType()));
out.write(format("%s%n", "[main]"));
for (Entry<String, Integer> e : count(d.getMain()).entrySet()) {
out.write(format("%d %s%n", e.getValue(), e.getKey()));