diff options
author | Eduardo Pedroni <e.pedroni91@gmail.com> | 2015-06-04 20:57:10 +0200 |
---|---|---|
committer | Eduardo Pedroni <e.pedroni91@gmail.com> | 2015-06-04 20:57:10 +0200 |
commit | 1f0159bcf903c0f422ab47b07cd296da1e816f87 (patch) | |
tree | 237ea7d5a71313cc0bb4db850174a76462de35cb /src/eu/equalparts | |
parent | 129cfbf8d5afec5725a88835869203a8825de22a (diff) |
Made a plan, now i'm following it. CLI is well under way
Diffstat (limited to 'src/eu/equalparts')
-rw-r--r-- | src/eu/equalparts/cardbase/data/Card.java | 250 | ||||
-rw-r--r-- | src/eu/equalparts/cardbase/data/CardBase.java | 23 | ||||
-rw-r--r-- | src/eu/equalparts/cardbase/data/CardBaseManager.java | 43 | ||||
-rw-r--r-- | src/eu/equalparts/cardbase/data/CardSet.java | 90 | ||||
-rw-r--r-- | src/eu/equalparts/cardbase/data/Deck.java | 5 | ||||
-rw-r--r-- | src/eu/equalparts/cardbase/data/MetaCardSet.java | 39 | ||||
-rw-r--r-- | src/eu/equalparts/cardbase/query/IO.java | 43 | ||||
-rw-r--r-- | src/eu/equalparts/cardbase/query/Test.java | 9 | ||||
-rw-r--r-- | src/eu/equalparts/cardbase/standalone/CardBaseCLI.java | 112 |
9 files changed, 262 insertions, 352 deletions
diff --git a/src/eu/equalparts/cardbase/data/Card.java b/src/eu/equalparts/cardbase/data/Card.java index d125c54..8e25d56 100644 --- a/src/eu/equalparts/cardbase/data/Card.java +++ b/src/eu/equalparts/cardbase/data/Card.java @@ -3,231 +3,33 @@ package eu.equalparts.cardbase.data; import java.util.ArrayList; public class Card { - private String layout; - private String name; - private ArrayList<String> names; - private String manaCost; - private Integer cmc; - private ArrayList<String> colors; - private String type; - private ArrayList<String> supertypes; - private ArrayList<String> types; - private ArrayList<String> subtypes; - private String rarity; - private String text; - private String flavor; - private String artist; - private String number; - private String power; - private String toughness; - private Integer loyalty; - private Integer multiverseid; - private ArrayList<String> variations; - private String imageName; - private String border; - private String watermark; + + public String layout; + public String name; + public ArrayList<String> names; + public String manaCost; + public Integer cmc; + public ArrayList<String> colors; + public String type; + public ArrayList<String> supertypes; + public ArrayList<String> types; + public ArrayList<String> subtypes; + public String rarity; + public String text; + public String flavor; + public String artist; + public String number; + public String power; + public String toughness; + public Integer loyalty; + public Integer multiverseid; + public ArrayList<String> variations; + public String imageName; + public String border; + public String watermark; // Not part of JSON, will be set later - private String setCode; - private String setName; + public String setCode; + public String setName; - public String getLayout() { - return layout; - } - - public void setLayout(String layout) { - this.layout = layout; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public ArrayList<String> getNames() { - return names; - } - - public void setNames(ArrayList<String> names) { - this.names = names; - } - - public String getManaCost() { - return manaCost; - } - - public void setManaCost(String manaCost) { - this.manaCost = manaCost; - } - - public Integer getCmc() { - return cmc; - } - - public void setCmc(Integer cmc) { - this.cmc = cmc; - } - - public ArrayList<String> getColors() { - return colors; - } - - public void setColors(ArrayList<String> colors) { - this.colors = colors; - } - - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } - - public ArrayList<String> getSupertypes() { - return supertypes; - } - - public void setSupertypes(ArrayList<String> supertypes) { - this.supertypes = supertypes; - } - - public ArrayList<String> getTypes() { - return types; - } - - public void setTypes(ArrayList<String> types) { - this.types = types; - } - - public ArrayList<String> getSubtypes() { - return subtypes; - } - - public void setSubtypes(ArrayList<String> subtypes) { - this.subtypes = subtypes; - } - - public String getRarity() { - return rarity; - } - - public void setRarity(String rarity) { - this.rarity = rarity; - } - - public String getText() { - return text; - } - - public void setText(String text) { - this.text = text; - } - - public String getFlavor() { - return flavor; - } - - public void setFlavor(String flavor) { - this.flavor = flavor; - } - - public String getArtist() { - return artist; - } - - public void setArtist(String artist) { - this.artist = artist; - } - - public String getNumber() { - return number; - } - - public void setNumber(String number) { - this.number = number; - } - - public String getPower() { - return power; - } - - public void setPower(String power) { - this.power = power; - } - - public String getToughness() { - return toughness; - } - - public void setToughness(String toughness) { - this.toughness = toughness; - } - - public Integer getLoyalty() { - return loyalty; - } - - public void setLoyalty(Integer loyalty) { - this.loyalty = loyalty; - } - - public Integer getMultiverseid() { - return multiverseid; - } - - public void setMultiverseid(Integer multiverseid) { - this.multiverseid = multiverseid; - } - - public ArrayList<String> getVariations() { - return variations; - } - - public void setVariations(ArrayList<String> variations) { - this.variations = variations; - } - - public String getImageName() { - return imageName; - } - - public void setImageName(String imageName) { - this.imageName = imageName; - } - - public String getBorder() { - return border; - } - - public void setBorder(String border) { - this.border = border; - } - - public String getWatermark() { - return watermark; - } - - public void setWatermark(String watermark) { - this.watermark = watermark; - } - - public String getSetName() { - return setName; - } - - public void setSetName(String setName) { - this.setName = setName; - } - - public String getSetCode() { - return setCode; - } - - public void setSetCode(String setCode) { - this.setCode = setCode; - } }
\ No newline at end of file diff --git a/src/eu/equalparts/cardbase/data/CardBase.java b/src/eu/equalparts/cardbase/data/CardBase.java index 14c30a5..70ae0ea 100644 --- a/src/eu/equalparts/cardbase/data/CardBase.java +++ b/src/eu/equalparts/cardbase/data/CardBase.java @@ -1,23 +1,12 @@ package eu.equalparts.cardbase.data; -import java.io.File; + +import java.util.ArrayList; public class CardBase { - public static void main(String... args) { - - } + private ArrayList<Card> cards; + private ArrayList<Deck> decks; + + - /** - * Parse a cardbase file and create an associated CardBase object. - */ - public CardBase(File cardbase) { - - } - - /** - * Create an empty CardBase. - */ - public CardBase() { - - } } diff --git a/src/eu/equalparts/cardbase/data/CardBaseManager.java b/src/eu/equalparts/cardbase/data/CardBaseManager.java new file mode 100644 index 0000000..acf5a3a --- /dev/null +++ b/src/eu/equalparts/cardbase/data/CardBaseManager.java @@ -0,0 +1,43 @@ +package eu.equalparts.cardbase.data; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; + +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.JsonMappingException; + +import eu.equalparts.cardbase.query.IO; + +public class CardBaseManager { + + private ArrayList<MetaCardSet> metaSets; + + public static void main(String... args) { + + } + + /** + * Parse a cardbase file and create an associated CardBase object. + * @throws IOException + * @throws JsonMappingException + * @throws JsonParseException + */ + public CardBaseManager(File cardbase) throws JsonParseException, JsonMappingException, IOException { + metaSets = IO.getAllMetaSets(); + } + + /** + * Create an empty CardBase. + * @throws IOException + * @throws JsonMappingException + * @throws JsonParseException + */ + public CardBaseManager() throws JsonParseException, JsonMappingException, IOException { + metaSets = IO.getAllMetaSets(); + } + + public ArrayList<MetaCardSet> getAllMetaSets() { + return metaSets; + } + +} diff --git a/src/eu/equalparts/cardbase/data/CardSet.java b/src/eu/equalparts/cardbase/data/CardSet.java index 97622db..2c8f950 100644 --- a/src/eu/equalparts/cardbase/data/CardSet.java +++ b/src/eu/equalparts/cardbase/data/CardSet.java @@ -3,84 +3,26 @@ package eu.equalparts.cardbase.data; import java.util.ArrayList; public class CardSet { - private String name; - private String code; - private String releaseDate; - private String border; - private String type; - private String block; - private String gathererCode; - private ArrayList<Card> cards; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getCode() { - return code; - } - - public void setCode(String code) { - this.code = code; - } - - public String getReleaseDate() { - return releaseDate; - } - - public void setReleaseDate(String releaseDate) { - this.releaseDate = releaseDate; - } - - public String getBorder() { - return border; - } - - public void setBorder(String border) { - this.border = border; - } - - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } - - public String getBlock() { - return block; - } - - public void setBlock(String block) { - this.block = block; - } - - public ArrayList<Card> getCards() { - return cards; - } - + public String name; + public String code; + public String releaseDate; + public String border; + public String type; + public String block; + public String gathererCode; + public ArrayList<Card> cards; + + /** + * Searches for a card by number (the one shown on the card itself). + * + * @param number the number of the card to search. + * @return the card, or null if no card is found with that number. + */ public Card getCardByNumber(String number) { for (Card card : cards) { - if (card.getNumber().equals(number)) + if (card.number.equals(number)) return card; } return null; } - - public void setCards(ArrayList<Card> cards) { - this.cards = cards; - } - - public String getGathererCode() { - return gathererCode; - } - - public void setGathererCode(String gathererCode) { - this.gathererCode = gathererCode; - } }
\ No newline at end of file diff --git a/src/eu/equalparts/cardbase/data/Deck.java b/src/eu/equalparts/cardbase/data/Deck.java new file mode 100644 index 0000000..50f1129 --- /dev/null +++ b/src/eu/equalparts/cardbase/data/Deck.java @@ -0,0 +1,5 @@ +package eu.equalparts.cardbase.data; + +public class Deck { + +} diff --git a/src/eu/equalparts/cardbase/data/MetaCardSet.java b/src/eu/equalparts/cardbase/data/MetaCardSet.java new file mode 100644 index 0000000..4ddf228 --- /dev/null +++ b/src/eu/equalparts/cardbase/data/MetaCardSet.java @@ -0,0 +1,39 @@ +package eu.equalparts.cardbase.data; + +public class MetaCardSet { + + private String name = ""; + private String code = ""; + private String releaseDate = ""; + + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public String getReleaseDate() { + return releaseDate; + } + + public void setReleaseDate(String releaseDate) { + this.releaseDate = releaseDate; + } + + @Override + public String toString() { + return String.format("%1$-12s : %2$s", code, name, releaseDate); + } + +} diff --git a/src/eu/equalparts/cardbase/query/IO.java b/src/eu/equalparts/cardbase/query/IO.java new file mode 100644 index 0000000..65a3a34 --- /dev/null +++ b/src/eu/equalparts/cardbase/query/IO.java @@ -0,0 +1,43 @@ +package eu.equalparts.cardbase.query; + +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.util.ArrayList; + +import com.fasterxml.jackson.core.JsonGenerationException; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.type.TypeReference; +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.CardSet; +import eu.equalparts.cardbase.data.MetaCardSet; + +public class IO { + + public static final String BASE_URL = "http://mtgjson.com/json/"; + public static final String SETS_URL = BASE_URL + "SetList.json"; + + private static final ObjectMapper mapper = new ObjectMapper(); + + public static CardSet getCardSet(String setId) { + return new CardSet(); + } + + public static ArrayList<MetaCardSet> getAllMetaSets() throws JsonParseException, JsonMappingException, IOException { + //mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + return mapper.readValue(new URL(SETS_URL), new TypeReference<ArrayList<MetaCardSet>>() {}); + } + + public static CardBase readCardBase(File file) throws JsonParseException, JsonMappingException, IOException { + return mapper.readValue(file, CardBase.class); + } + + 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 index 646139e..2050c76 100644 --- a/src/eu/equalparts/cardbase/query/Test.java +++ b/src/eu/equalparts/cardbase/query/Test.java @@ -7,7 +7,6 @@ import java.util.List; import java.util.Map; import com.fasterxml.jackson.core.JsonParseException; -import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; @@ -50,7 +49,7 @@ public class Test { // @SuppressWarnings("unchecked") CardSet set = mapper.readValue(new File("M15.json"), CardSet.class); - System.out.println(set.getCardByNumber("281").getName()); + System.out.println(set.getCardByNumber("281").name); } @@ -58,9 +57,9 @@ public class Test { List<Card> allCards = new ArrayList<Card>(); for (CardSet set : sets.values()) { - for (Card card : set.getCards()) { - card.setSetCode(set.getCode()); - card.setSetName(set.getName()); + for (Card card : set.cards) { + card.setCode = set.code; + card.setName = set.name; // System.out.println(set.getName() + ": " + card.getName()); allCards.add(card); diff --git a/src/eu/equalparts/cardbase/standalone/CardBaseCLI.java b/src/eu/equalparts/cardbase/standalone/CardBaseCLI.java index 4b746b2..2806a9e 100644 --- a/src/eu/equalparts/cardbase/standalone/CardBaseCLI.java +++ b/src/eu/equalparts/cardbase/standalone/CardBaseCLI.java @@ -5,7 +5,11 @@ import java.io.File; import java.io.IOException; import java.io.InputStreamReader; -import eu.equalparts.cardbase.data.CardBase; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.JsonMappingException; + +import eu.equalparts.cardbase.data.CardBaseManager; +import eu.equalparts.cardbase.data.MetaCardSet; /** * This provides a lightweight CLI for interacting with cardbase files. @@ -13,6 +17,10 @@ import eu.equalparts.cardbase.data.CardBase; */ public class CardBaseCLI { + private static String selectedSet = ""; + private static CardBaseManager cbm; + private static boolean exit = false; + /** * Execute the interface. * @@ -21,49 +29,89 @@ public class CardBaseCLI { public static void main(String... args) { System.out.println("Welcome to cardbase"); - CardBase cb; - // construct the cardbase - if (args.length > 0) { - System.out.println("Building cardbase from " + args[0]); - cb = new CardBase(new File(args[0])); - } else { - System.out.println("No cardbase file was provided, initialising a clean cardbase"); - cb = new CardBase(); - } - - // main UI loop try { + // construct the cardbase + if (args.length > 0) { + System.out.println("Building cardbase from " + args[0]); + cbm = new CardBaseManager(new File(args[0])); + } else { + System.out.println("No cardbase file was provided, initialising a clean cardbase"); + cbm = new CardBaseManager(); + } + // initialise necessary components System.out.println("Fetching card sets from upstream"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); - boolean exit = false; - + while (!exit) { String rawInput = br.readLine().trim().toLowerCase(); String[] commands = rawInput.split("[ \t]+"); - - if (commands.length > 0) { - switch (commands[0]) { - case "exit": - exit = true; - break; - case "sets": - break; - case "set": - break; - case "write": - break; - default: - break; - } - } + + parse(commands); } + + } catch (JsonParseException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (JsonMappingException e) { + // TODO Auto-generated catch block + e.printStackTrace(); } catch (IOException e) { + // TODO Auto-generated catch block e.printStackTrace(); } - } - + + private static void parse(String[] commands) { + if (commands.length > 0) { + switch (commands[0]) { + /* + * Exit procedures + */ + case "exit": + exit = true; + break; + /* + * Show help (externalised?) + */ + case "help": + System.out.println("google it"); + break; + /* + * Print a list of valid set codes + */ + case "sets": + for (MetaCardSet mcs : cbm.getAllMetaSets()) { + System.out.println(mcs); + } + break; + /* + * Select a set, any card numbers provided will be fetched from that set + */ + case "set": + for (MetaCardSet mcs : cbm.getAllMetaSets()) { + if (mcs.getCode().equalsIgnoreCase(commands[1])) { + System.out.println("Selected set: " + mcs.getName()); + selectedSet = mcs.getCode(); + return; + } + } + System.out.println(commands[1] + " does not correspond to any set (use \"sets\" to see all valid set codes)"); + break; + /* + * Write current CardBase to file + */ + case "write": + break; + /* + * Try to parse a card number and get it from the set + */ + default: + break; + } + } + } + } |