mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-18 19:58:00 +00:00
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:
@@ -12,8 +12,6 @@ public class Deck implements Comparable{
|
|||||||
|
|
||||||
private List<String> main;
|
private List<String> main;
|
||||||
private List<String> sideboard;
|
private List<String> sideboard;
|
||||||
private transient List<String> mainView;
|
|
||||||
private transient List<String> sideboardView;
|
|
||||||
|
|
||||||
public static final String NAME = "Name";
|
public static final String NAME = "Name";
|
||||||
public static final String DECK_TYPE = "Deck Type";
|
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
|
//gameType is from Constant.GameType, like Constant.GameType.Regular
|
||||||
public Deck(String gameType) {
|
public Deck() {
|
||||||
setDeckType(gameType);
|
|
||||||
setName("");
|
|
||||||
|
|
||||||
main = new ArrayList<String>();
|
main = new ArrayList<String>();
|
||||||
mainView = Collections.unmodifiableList(main);
|
|
||||||
|
|
||||||
sideboard = new ArrayList<String>();
|
sideboard = new ArrayList<String>();
|
||||||
sideboardView = Collections.unmodifiableList(sideboard);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Deck(String deckType, List<String> main, List<String> sideboard, String name) {
|
public Deck(String deckType, List<String> main, List<String> sideboard, String name) {
|
||||||
@@ -39,18 +31,20 @@ public class Deck implements Comparable{
|
|||||||
setName(name);
|
setName(name);
|
||||||
|
|
||||||
this.main = main;
|
this.main = main;
|
||||||
mainView = Collections.unmodifiableList(main);
|
this.sideboard = sideboard;
|
||||||
|
}
|
||||||
|
|
||||||
this.sideboard = main;
|
public Deck(String type) {
|
||||||
sideboardView = Collections.unmodifiableList(sideboard);
|
this();
|
||||||
|
setDeckType(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> getMain() {
|
public List<String> getMain() {
|
||||||
return mainView;
|
return Collections.unmodifiableList(main);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> getSideboard() {
|
public List<String> getSideboard() {
|
||||||
return sideboardView;
|
return Collections.unmodifiableList(sideboard);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDeckType() {
|
public String getDeckType() {
|
||||||
@@ -58,7 +52,7 @@ public class Deck implements Comparable{
|
|||||||
}
|
}
|
||||||
|
|
||||||
//can only call this method ONCE
|
//can only call this method ONCE
|
||||||
private void setDeckType(String deckType) {
|
void setDeckType(String deckType) {
|
||||||
if (this.getDeckType() != null) {
|
if (this.getDeckType() != null) {
|
||||||
throw new IllegalStateException(
|
throw new IllegalStateException(
|
||||||
"Deck : setDeckType() error, deck type has already been set");
|
"Deck : setDeckType() error, deck type has already been set");
|
||||||
@@ -78,7 +72,7 @@ public class Deck implements Comparable{
|
|||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return metadata.get(NAME);
|
return metadata.get(NAME);
|
||||||
}//may return null
|
}
|
||||||
|
|
||||||
public void setComment(String comment) {
|
public void setComment(String comment) {
|
||||||
metadata.put(COMMENT, comment);
|
metadata.put(COMMENT, comment);
|
||||||
@@ -174,4 +168,12 @@ public class Deck implements Comparable{
|
|||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Set<Map.Entry<String,String>> getMetadata() {
|
||||||
|
return metadata.entrySet();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addMetaData(String key, String value) {
|
||||||
|
metadata.put(key, value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -34,10 +34,6 @@ public class DeckManager {
|
|||||||
Map<String, Deck> deckMap;
|
Map<String, Deck> deckMap;
|
||||||
Map<String, Deck[]> boosterMap;
|
Map<String, Deck[]> boosterMap;
|
||||||
|
|
||||||
public DeckManager(String fileName) {
|
|
||||||
this(new File(fileName));
|
|
||||||
}
|
|
||||||
|
|
||||||
public DeckManager(File deckDir) {
|
public DeckManager(File deckDir) {
|
||||||
if (deckDir == null) {
|
if (deckDir == null) {
|
||||||
throw new IllegalArgumentException("No deck directory specified");
|
throw new IllegalArgumentException("No deck directory specified");
|
||||||
@@ -173,15 +169,25 @@ public class DeckManager {
|
|||||||
|
|
||||||
ListIterator<String> lineIterator = lines.listIterator();
|
ListIterator<String> lineIterator = lines.listIterator();
|
||||||
|
|
||||||
String firstLine = lineIterator.next();
|
String line = lineIterator.next();
|
||||||
|
|
||||||
//Old text-based format
|
//Old text-based format
|
||||||
if (!firstLine.equals("[Metadata]")) {
|
if (!line.equals("[metadata]")) {
|
||||||
lineIterator.previous();
|
lineIterator.previous();
|
||||||
return readDeckOld(lineIterator);
|
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
|
//readDeck deck type
|
||||||
String deckType = iterator.next();
|
String deckType = iterator.next();
|
||||||
|
|
||||||
Deck d = new Deck(deckType);
|
Deck d = new Deck();
|
||||||
d.setName(name);
|
d.setName(name);
|
||||||
d.setComment(comment);
|
d.setComment(comment);
|
||||||
|
d.setDeckType(deckType);
|
||||||
|
|
||||||
//go to [main]
|
//go to [main]
|
||||||
while ((line = iterator.next()) != null && !line.equals("[main]")) {
|
while ((line = iterator.next()) != null && !line.equals("[main]")) {
|
||||||
System.err.println("unexpected line: " + line);
|
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*");
|
Pattern p = Pattern.compile("\\s*((\\d+)\\s+)?(.*?)\\s*");
|
||||||
|
|
||||||
//readDeck main deck
|
//readDeck main deck
|
||||||
while (iterator.hasNext() && !(line = iterator.next()).equals("[sideboard]")) {
|
while (lineIterator.hasNext() && !(line = lineIterator.next()).equals("[sideboard]")) {
|
||||||
Matcher m = p.matcher(line);
|
Matcher m = p.matcher(line);
|
||||||
m.matches();
|
m.matches();
|
||||||
String s = m.group(2);
|
String s = m.group(2);
|
||||||
@@ -230,7 +245,8 @@ public class DeckManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//readDeck sideboard
|
//readDeck sideboard
|
||||||
while (iterator.hasNext()) {
|
while (lineIterator.hasNext()) {
|
||||||
|
line = lineIterator.next();
|
||||||
Matcher m = p.matcher(line);
|
Matcher m = p.matcher(line);
|
||||||
m.matches();
|
m.matches();
|
||||||
String s = m.group(2);
|
String s = m.group(2);
|
||||||
@@ -239,8 +255,6 @@ public class DeckManager {
|
|||||||
d.addSideboard(m.group(3));
|
d.addSideboard(m.group(3));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return d;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private String deriveFileName(String deckName) {
|
private String deriveFileName(String deckName) {
|
||||||
@@ -296,12 +310,13 @@ public class DeckManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void write(Deck d, BufferedWriter out) throws IOException {
|
private void write(Deck d, BufferedWriter out) throws IOException {
|
||||||
out.write(format("%s%n", d.getName()));
|
out.write("[metadata]\n");
|
||||||
if (d.getComment() != null) {
|
|
||||||
out.write(format("%s%n", d.getComment()));
|
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]"));
|
out.write(format("%s%n", "[main]"));
|
||||||
for (Entry<String, Integer> e : count(d.getMain()).entrySet()) {
|
for (Entry<String, Integer> e : count(d.getMain()).entrySet()) {
|
||||||
out.write(format("%d %s%n", e.getValue(), e.getKey()));
|
out.write(format("%d %s%n", e.getValue(), e.getKey()));
|
||||||
|
|||||||
Reference in New Issue
Block a user