aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu
diff options
context:
space:
mode:
authorEduardo Pedroni <e.pedroni91@gmail.com>2015-06-05 21:50:36 +0200
committerEduardo Pedroni <e.pedroni91@gmail.com>2015-06-05 21:50:36 +0200
commit62bb720ea707765e2a33a03ee5dfa91b753123e8 (patch)
tree868eedcbedb07ba8ef2e70768dbe516ebe1e7ef5 /src/eu
parent0465829c25ef43ba9ba898dff01ebaa5106fd1f8 (diff)
Tidied up README and help, sorted out a neater Ant build script
Diffstat (limited to 'src/eu')
-rw-r--r--src/eu/equalparts/cardbase/data/CardBaseManager.java4
-rw-r--r--src/eu/equalparts/cardbase/data/Cardbase.java (renamed from src/eu/equalparts/cardbase/data/CardBase.java)2
-rw-r--r--src/eu/equalparts/cardbase/query/IO.java8
-rw-r--r--src/eu/equalparts/cardbase/query/Test.java70
-rw-r--r--src/eu/equalparts/cardbase/standalone/CardbaseCLI.java (renamed from src/eu/equalparts/cardbase/standalone/CardBaseCLI.java)152
5 files changed, 116 insertions, 120 deletions
diff --git a/src/eu/equalparts/cardbase/data/CardBaseManager.java b/src/eu/equalparts/cardbase/data/CardBaseManager.java
index 0baf9d7..d6a63dc 100644
--- a/src/eu/equalparts/cardbase/data/CardBaseManager.java
+++ b/src/eu/equalparts/cardbase/data/CardBaseManager.java
@@ -12,7 +12,7 @@ import eu.equalparts.cardbase.query.IO;
public class CardBaseManager {
private ArrayList<MetaCardSet> metaSets;
- public CardBase cardBase;
+ public Cardbase cardBase;
/**
* Parse a cardbase file and create an associated CardBase object.
@@ -33,7 +33,7 @@ public class CardBaseManager {
*/
public CardBaseManager() throws JsonParseException, JsonMappingException, IOException {
metaSets = IO.getAllMetaSets();
- cardBase = new CardBase();
+ cardBase = new Cardbase();
}
public ArrayList<MetaCardSet> getAllMetaSets() {
diff --git a/src/eu/equalparts/cardbase/data/CardBase.java b/src/eu/equalparts/cardbase/data/Cardbase.java
index abdd423..1c2aa12 100644
--- a/src/eu/equalparts/cardbase/data/CardBase.java
+++ b/src/eu/equalparts/cardbase/data/Cardbase.java
@@ -2,7 +2,7 @@ package eu.equalparts.cardbase.data;
import java.util.ArrayList;
-public class CardBase {
+public class Cardbase {
public ArrayList<Card> cards = new ArrayList<>();
public ArrayList<Deck> decks = new ArrayList<>();
diff --git a/src/eu/equalparts/cardbase/query/IO.java b/src/eu/equalparts/cardbase/query/IO.java
index 9b81b20..e913966 100644
--- a/src/eu/equalparts/cardbase/query/IO.java
+++ b/src/eu/equalparts/cardbase/query/IO.java
@@ -13,7 +13,7 @@ import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
-import eu.equalparts.cardbase.data.CardBase;
+import eu.equalparts.cardbase.data.Cardbase;
import eu.equalparts.cardbase.data.CardSet;
import eu.equalparts.cardbase.data.MetaCardSet;
@@ -59,8 +59,8 @@ public class IO {
* @throws JsonMappingException
* @throws IOException
*/
- public static CardBase readCardBase(File file) throws JsonParseException, JsonMappingException, IOException {
- return mapper.readValue(file, CardBase.class);
+ public static Cardbase readCardBase(File file) throws JsonParseException, JsonMappingException, IOException {
+ return mapper.readValue(file, Cardbase.class);
}
/**
@@ -72,7 +72,7 @@ public class IO {
* @throws JsonMappingException
* @throws IOException
*/
- public static void writeCardBase(File file, CardBase cardBase) throws JsonGenerationException, JsonMappingException, IOException {
+ public static void writeCardBase(File file, Cardbase cardBase) throws JsonGenerationException, JsonMappingException, IOException {
mapper.writeValue(file, cardBase);
}
diff --git a/src/eu/equalparts/cardbase/query/Test.java b/src/eu/equalparts/cardbase/query/Test.java
deleted file mode 100644
index e466c55..0000000
--- a/src/eu/equalparts/cardbase/query/Test.java
+++ /dev/null
@@ -1,70 +0,0 @@
-package eu.equalparts.cardbase.query;
-
-import java.io.File;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-
-import com.fasterxml.jackson.core.JsonParseException;
-import com.fasterxml.jackson.databind.DeserializationFeature;
-import com.fasterxml.jackson.databind.JsonMappingException;
-import com.fasterxml.jackson.databind.ObjectMapper;
-
-import eu.equalparts.cardbase.data.Card;
-import eu.equalparts.cardbase.data.CardSet;
-
-public class Test {
-
-// public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
-// ObjectMapper mapper = new ObjectMapper();
-// mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
-//
-// @SuppressWarnings("unchecked")
-// List<Card> allCards = getAllCards((Map<String, CardSet>) mapper
-// .readValue(new File("AllSets.json"), new TypeReference<Map<String, CardSet>>() {}));
-//
-// System.out.println("Number of cards: " + allCards.size());
-// }
-//
-// public static List<Card> getAllCards(Map<String, CardSet> sets) {
-// List<Card> allCards = new ArrayList<Card>();
-//
-// for (CardSet set : sets.values()) {
-// for (Card card : set.getCards()) {
-// card.setSetCode(set.getCode());
-// card.setSetName(set.getName());
-// // System.out.println(set.getName() + ": " + card.getName());
-//
-// allCards.add(card);
-// }
-// }
-//
-// return allCards;
-// }
-
- public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
- ObjectMapper mapper = new ObjectMapper();
- mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
-
-// @SuppressWarnings("unchecked")
- CardSet set = mapper.readValue(new File("M15.json"), CardSet.class);
- System.out.println(set.getCardByNumber("281").name);
-
- }
-
- public static List<Card> getAllCards(Map<String, CardSet> sets) {
- List<Card> allCards = new ArrayList<Card>();
-
- for (CardSet set : sets.values()) {
- for (Card card : set.cards) {
- card.setCode = set.code;
- // System.out.println(set.getName() + ": " + card.getName());
-
- allCards.add(card);
- }
- }
-
- return allCards;
- }
-}
diff --git a/src/eu/equalparts/cardbase/standalone/CardBaseCLI.java b/src/eu/equalparts/cardbase/standalone/CardbaseCLI.java
index d0503ac..c19bc08 100644
--- a/src/eu/equalparts/cardbase/standalone/CardBaseCLI.java
+++ b/src/eu/equalparts/cardbase/standalone/CardbaseCLI.java
@@ -3,6 +3,7 @@ package eu.equalparts.cardbase.standalone;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
+import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.util.HashMap;
@@ -22,14 +23,28 @@ import eu.equalparts.cardbase.query.IO;
* This provides a lightweight CLI for interacting with cardbase files.
*
*/
-public class CardBaseCLI {
+public class CardbaseCLI {
+ private enum LastAction {
+ ADD, REMOVE;
+ public Integer count;
+ public Card card;
+
+ public void set(Card card, Integer count) {
+ this.card = card;
+ this.count = count;
+ }
+
+ }
+ private static LastAction lastAction = null;
+
private static CardSet selectedSet = null;
private static HashMap<String, CardSet> setCache = new HashMap<String, CardSet>();
private static CardBaseManager cbm;
private static boolean exit = false;
private static String help = "No help file was found";
- private static File cardBaseFile;
+ private static File cardBaseFile = null;
+ private static boolean savePrompt = false;
/**
* Execute the interface.
@@ -52,10 +67,8 @@ public class CardBaseCLI {
System.exit(0);
}
} else {
- //System.out.println("No cardbase file was provided, initialising a clean cardbase");
- System.out.println("Loading testbase for debugging purposes");
- cardBaseFile = new File("testbase");
- cbm = new CardBaseManager(cardBaseFile);
+ System.out.println("No cardbase file was provided, initialising a clean cardbase");
+ cbm = new CardBaseManager();
}
// initialise necessary components
@@ -63,9 +76,9 @@ public class CardBaseCLI {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Loading externals");
- File helpFile = new File("help");
- if (helpFile.exists() && helpFile.canRead()) {
- help = new Scanner(helpFile).useDelimiter("\\Z").next();
+ InputStream is = CardbaseCLI.class.getResourceAsStream("/help");
+ if (is != null) {
+ help = new Scanner(is).useDelimiter("\\Z").next();
} else {
System.out.println("Help file is not available, I hope you know how to use the program!");
}
@@ -105,25 +118,24 @@ public class CardBaseCLI {
* Write current CardBase to file
*/
case "write":
+ File output;
+
if (commands.length > 1) {
- File output = new File(commands[1]);
- if (output.exists()) {
- if(output.isFile()) {
- if (output.canWrite()) {
- IO.writeCardBase(output, cbm.cardBase);
- cardBaseFile = output;
- System.out.println("Cardbase saved to " + output.getAbsolutePath());
- } else {
- System.out.println(commands[1] + " cannot be written to, nothing was written");
- }
- } else {
- System.out.println(commands[1] + " is not a file, nothing was written");
- }
- } else {
- System.out.println(commands[1] + " does not exist, nothing was written");
+ output = new File(sanitiseFileName(commands[1]));
+ } else {
+ output = cardBaseFile;
+ }
+
+ if (output != null) {
+ if (output.exists() && (!output.isFile() || !output.canWrite())) {
+ System.out.println("Could not write to " + output.getAbsolutePath());
+ return;
}
- } else if (cardBaseFile != null) {
- System.out.println("Writing to " + cardBaseFile.getAbsolutePath());
+
+ IO.writeCardBase(output, cbm.cardBase);
+ cardBaseFile = output;
+ System.out.println("Cardbase was saved to " + output.getAbsolutePath());
+ savePrompt = false;
} else {
System.out.println("Please provide a file name");
}
@@ -133,7 +145,12 @@ public class CardBaseCLI {
* Exit procedures
*/
case "exit":
- exit = true;
+ if (savePrompt) {
+ System.out.println("Don't forget to save. If you really wish to quit without saving, type exit again.");
+ savePrompt = false;
+ } else {
+ exit = true;
+ }
break;
/*
@@ -159,6 +176,7 @@ public class CardBaseCLI {
setCache.put(mcs.code, selectedSet);
}
System.out.println("Selected set: " + mcs.name);
+ lastAction = null;
return;
}
}
@@ -169,9 +187,14 @@ public class CardBaseCLI {
* Print a brief list of the complete cardbase.
*/
case "glance":
+ Card current;
+ int total = 0;
for (Iterator<Card> i = cbm.cardIterator(); i.hasNext();) {
- printGlance(i.next());
+ current = i.next();
+ printGlance(current);
+ total += current.count;
}
+ System.out.println("Total: " + total);
break;
/*
@@ -197,6 +220,22 @@ public class CardBaseCLI {
break;
/*
+ * Undo previous action.
+ */
+ case "undo":
+ if (lastAction != null) {
+ if (lastAction == LastAction.ADD) {
+ remove(lastAction.card, lastAction.count);
+ } else if (lastAction == LastAction.REMOVE) {
+ add(lastAction.card, lastAction.count);
+ }
+ lastAction = null;
+ } else {
+ System.out.println("Nothing to undo");
+ }
+ break;
+
+ /*
* Remove one or more cards
*/
case "remove":
@@ -212,8 +251,7 @@ public class CardBaseCLI {
return;
}
}
- cbm.removeCard(remove, count);
- System.out.println("Removed " + count + "x " + remove.name);
+ remove(remove, count);
} else {
System.out.println(commands[1] + " does not correspond to a card in " + selectedSet.name);
}
@@ -230,21 +268,24 @@ public class CardBaseCLI {
*/
default:
if (selectedSet != null) {
- Card newCard = selectedSet.getCardByNumber(commands[0]);
- if (newCard != null) {
- Integer count = 1;
- if (commands.length > 1 && commands[1].matches("[0-9]+")) {
- count = Integer.valueOf(commands[1]);
- if (count <= 0) {
- System.out.println("Can't add " + count + " cards");
- return;
+ if (commands.length == 1 && commands[0].isEmpty()) {
+ if (lastAction == LastAction.ADD)
+ add(lastAction.card, lastAction.count);
+ } else {
+ Card newCard = selectedSet.getCardByNumber(commands[0]);
+ if (newCard != null) {
+ Integer count = 1;
+ if (commands.length > 1 && commands[1].matches("[0-9]+")) {
+ count = Integer.valueOf(commands[1]);
+ if (count <= 0) {
+ System.out.println("Can't add " + count + " cards");
+ return;
+ }
}
+ add(newCard, count);
+ } else {
+ System.out.println(commands[0] + " does not correspond to a card in " + selectedSet.name);
}
- newCard.setCode = selectedSet.code;
- cbm.addCard(newCard, count);
- System.out.println("Added " + count + "x " + newCard.name);
- } else {
- System.out.println(commands[0] + " does not correspond to a card in " + selectedSet.name);
}
} else {
System.out.println("Select a set before adding cards.");
@@ -254,6 +295,31 @@ public class CardBaseCLI {
}
}
+ private static void add(Card card, Integer count) {
+ card.setCode = selectedSet.code;
+ cbm.addCard(card, count);
+ System.out.println("Added " + count + "x " + card.name);
+ savePrompt = true;
+ lastAction = LastAction.ADD;
+ lastAction.set(card, count);
+ }
+
+ private static void remove(Card card, Integer count) {
+ cbm.removeCard(card, count);
+ System.out.println("Removed " + count + "x " + card.name);
+ savePrompt = true;
+ lastAction = LastAction.REMOVE;
+ lastAction.set(card, count);
+ }
+
+ private static String sanitiseFileName(String name) {
+ name = name.replaceAll("[^-_.A-Za-z0-9]", "");
+ if (!name.endsWith(".cb")) {
+ name = name.concat(".cb");
+ }
+ return name;
+ }
+
private static void printPerusal(Card card) {
printGlance(card);
if (card.type != null) System.out.println("\t" + card.type);