mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-19 20:28:00 +00:00
Deck sections can be arranged in any order
This commit is contained in:
@@ -141,6 +141,7 @@ public final class Deck implements Comparable<Deck>, Serializable {
|
||||
public void addMain(final String cardName) { addMain(CardDb.instance().getCard(cardName)); }
|
||||
public void addMain(final CardPrinted card) { main.add(card); }
|
||||
public void addMain(final ItemPoolView<CardPrinted> list) { main.addAll(list); }
|
||||
public void setMain(final Iterable<String> cards) { main = new ItemPool<CardPrinted>(cards, CardPrinted.class); }
|
||||
public void removeMain(final CardPrinted card) { main.remove(card); }
|
||||
public void removeMain(final CardPrinted card, final int amount) { main.remove(card, amount); }
|
||||
public int countMain() { return main.countAll(); }
|
||||
@@ -154,6 +155,7 @@ public final class Deck implements Comparable<Deck>, Serializable {
|
||||
public final void addSideboard(final CardPrinted card) { sideboard.add(card); }
|
||||
public final void addSideboard(final CardPrinted card, final int amount) { sideboard.add(card, amount); }
|
||||
public final void addSideboard(final ItemPoolView<CardPrinted> cards) { sideboard.addAll(cards); }
|
||||
public final void setSideboard(final Iterable<String> cards) { sideboard = new ItemPool<CardPrinted>(cards, CardPrinted.class); }
|
||||
|
||||
/**
|
||||
* <p>countSideboard.</p>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package forge.deck;
|
||||
|
||||
|
||||
import forge.FileUtil;
|
||||
import forge.PlayerType;
|
||||
import forge.error.ErrorViewer;
|
||||
import forge.game.GameType;
|
||||
@@ -291,36 +292,42 @@ public class DeckManager {
|
||||
* @return a {@link forge.deck.Deck} object.
|
||||
*/
|
||||
public static Deck readDeck(File deckFile) {
|
||||
List<String> lines = new LinkedList<String>();
|
||||
|
||||
try {
|
||||
BufferedReader r = new BufferedReader(new FileReader(deckFile));
|
||||
|
||||
String line;
|
||||
while ((line = r.readLine()) != null) {
|
||||
lines.add(line);
|
||||
}
|
||||
|
||||
r.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
ListIterator<String> lineIterator = lines.listIterator();
|
||||
if (!lineIterator.hasNext()) { return null; }
|
||||
|
||||
String line = lineIterator.next();
|
||||
List<String> lines = FileUtil.readFile(deckFile);
|
||||
if (lines.isEmpty()) { return null; }
|
||||
|
||||
//Old text-based format
|
||||
if (!line.equals("[metadata]")) {
|
||||
lineIterator.previous();
|
||||
return readDeckOld(lineIterator);
|
||||
}
|
||||
|
||||
Deck d = new Deck();
|
||||
|
||||
//read metadata
|
||||
while (!(line = lineIterator.next()).equals("[main]")) {
|
||||
String firstLine = lines.get(0);
|
||||
if (!firstLine.startsWith("[") || firstLine.equalsIgnoreCase("[general]")) {
|
||||
readDeckOldMetadata(lines.iterator(), d);
|
||||
} else {
|
||||
readDeckMetadata(findSection(lines, "metadata"), d);
|
||||
}
|
||||
d.setMain(readCardList(findSection(lines, "main")));
|
||||
d.setSideboard(readCardList(findSection(lines, "sideboard")));
|
||||
|
||||
return d;
|
||||
}
|
||||
|
||||
private static Iterator<String> findSection(final Iterable<String> lines, final String sectionName)
|
||||
{
|
||||
Iterator<String> lineIterator = lines.iterator();
|
||||
String toSearch = String.format("[%s]", sectionName);
|
||||
while(lineIterator.hasNext()) {
|
||||
if (toSearch.equalsIgnoreCase(lineIterator.next())) { break; }
|
||||
}
|
||||
|
||||
return lineIterator;
|
||||
}
|
||||
|
||||
private static void readDeckMetadata(final Iterator<String> lineIterator, final Deck d)
|
||||
{
|
||||
while (lineIterator.hasNext()) {
|
||||
String line = lineIterator.next();
|
||||
if( line.startsWith("[") ) { break; }
|
||||
|
||||
String[] linedata = line.split("=", 2);
|
||||
String field = linedata[0].toLowerCase();
|
||||
if (NAME.equalsIgnoreCase(field)) {
|
||||
@@ -337,11 +344,6 @@ public class DeckManager {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
addCardList(lineIterator, d);
|
||||
|
||||
return d;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -350,7 +352,7 @@ public class DeckManager {
|
||||
* @param iterator a {@link java.util.ListIterator} object.
|
||||
* @return a {@link forge.deck.Deck} object.
|
||||
*/
|
||||
private static Deck readDeckOld(final ListIterator<String> iterator) {
|
||||
private static void readDeckOldMetadata(final Iterator<String> iterator, Deck d) {
|
||||
|
||||
String line;
|
||||
//readDeck name
|
||||
@@ -369,19 +371,9 @@ public class DeckManager {
|
||||
//readDeck deck type
|
||||
GameType deckType = GameType.smartValueOf(iterator.next());
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -405,7 +397,7 @@ public class DeckManager {
|
||||
}
|
||||
|
||||
// Precondition: iterator should point at the first line of cards list
|
||||
private static List<String> readCardList(final ListIterator<String> lineIterator) {
|
||||
private static List<String> readCardList(final Iterator<String> lineIterator) {
|
||||
List<String> result = new ArrayList<String>();
|
||||
Pattern p = Pattern.compile("((\\d+)\\s+)?(.*?)");
|
||||
|
||||
|
||||
@@ -153,7 +153,7 @@ public final class CardDb {
|
||||
}
|
||||
|
||||
// Multiple fetch
|
||||
public List<CardPrinted> getCards(final List<String> names) {
|
||||
public List<CardPrinted> getCards(final Iterable<String> names) {
|
||||
List<CardPrinted> result = new ArrayList<CardPrinted>();
|
||||
for (String name : names) { result.add(getCard(name)); }
|
||||
return result;
|
||||
|
||||
@@ -15,7 +15,7 @@ public final class ItemPool<T extends InventoryItem> extends ItemPoolView<T> {
|
||||
public ItemPool(final Class<T> cls) { super(cls); }
|
||||
|
||||
@SuppressWarnings("unchecked") // conversion here must be safe
|
||||
public ItemPool(final List<String> names, final Class<T> cls) { super(cls); addAllCards((Iterable<T>) CardDb.instance().getCards(names)); }
|
||||
public ItemPool(final Iterable<String> names, final Class<T> cls) { super(cls); addAllCards((Iterable<T>) CardDb.instance().getCards(names)); }
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <Tin extends InventoryItem, Tout extends InventoryItem> ItemPool<Tout>
|
||||
|
||||
Reference in New Issue
Block a user