From 98a75846c69f53f17389401096d2a19c697045d3 Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Wed, 3 Jun 2015 20:36:07 +0200 Subject: Have a working example to parse JSON sets and get cards by number --- src/eu/equalparts/cardbase/data/Card.java | 233 +++++++++++++++++++++ src/eu/equalparts/cardbase/data/CardBase.java | 23 ++ src/eu/equalparts/cardbase/data/CardSet.java | 86 ++++++++ src/eu/equalparts/cardbase/query/Test.java | 72 +++++++ .../cardbase/standalone/CardBaseCLI.java | 69 ++++++ 5 files changed, 483 insertions(+) create mode 100644 src/eu/equalparts/cardbase/data/Card.java create mode 100644 src/eu/equalparts/cardbase/data/CardBase.java create mode 100644 src/eu/equalparts/cardbase/data/CardSet.java create mode 100644 src/eu/equalparts/cardbase/query/Test.java create mode 100644 src/eu/equalparts/cardbase/standalone/CardBaseCLI.java (limited to 'src') diff --git a/src/eu/equalparts/cardbase/data/Card.java b/src/eu/equalparts/cardbase/data/Card.java new file mode 100644 index 0000000..d125c54 --- /dev/null +++ b/src/eu/equalparts/cardbase/data/Card.java @@ -0,0 +1,233 @@ +package eu.equalparts.cardbase.data; + +import java.util.ArrayList; + +public class Card { + private String layout; + private String name; + private ArrayList names; + private String manaCost; + private Integer cmc; + private ArrayList colors; + private String type; + private ArrayList supertypes; + private ArrayList types; + private ArrayList 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 variations; + private String imageName; + private String border; + private String watermark; + + // Not part of JSON, will be set later + private String setCode; + private 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 getNames() { + return names; + } + + public void setNames(ArrayList 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 getColors() { + return colors; + } + + public void setColors(ArrayList colors) { + this.colors = colors; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public ArrayList getSupertypes() { + return supertypes; + } + + public void setSupertypes(ArrayList supertypes) { + this.supertypes = supertypes; + } + + public ArrayList getTypes() { + return types; + } + + public void setTypes(ArrayList types) { + this.types = types; + } + + public ArrayList getSubtypes() { + return subtypes; + } + + public void setSubtypes(ArrayList 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 getVariations() { + return variations; + } + + public void setVariations(ArrayList 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 new file mode 100644 index 0000000..14c30a5 --- /dev/null +++ b/src/eu/equalparts/cardbase/data/CardBase.java @@ -0,0 +1,23 @@ +package eu.equalparts.cardbase.data; +import java.io.File; + +public class CardBase { + + public static void main(String... args) { + + } + + /** + * 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/CardSet.java b/src/eu/equalparts/cardbase/data/CardSet.java new file mode 100644 index 0000000..97622db --- /dev/null +++ b/src/eu/equalparts/cardbase/data/CardSet.java @@ -0,0 +1,86 @@ +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 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 getCards() { + return cards; + } + + public Card getCardByNumber(String number) { + for (Card card : cards) { + if (card.getNumber().equals(number)) + return card; + } + return null; + } + + public void setCards(ArrayList 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/query/Test.java b/src/eu/equalparts/cardbase/query/Test.java new file mode 100644 index 0000000..646139e --- /dev/null +++ b/src/eu/equalparts/cardbase/query/Test.java @@ -0,0 +1,72 @@ +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.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.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 allCards = getAllCards((Map) mapper +// .readValue(new File("AllSets.json"), new TypeReference>() {})); +// +// System.out.println("Number of cards: " + allCards.size()); +// } +// +// public static List getAllCards(Map sets) { +// List allCards = new ArrayList(); +// +// 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").getName()); + + } + + public static List getAllCards(Map sets) { + List allCards = new ArrayList(); + + 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; + } +} diff --git a/src/eu/equalparts/cardbase/standalone/CardBaseCLI.java b/src/eu/equalparts/cardbase/standalone/CardBaseCLI.java new file mode 100644 index 0000000..4b746b2 --- /dev/null +++ b/src/eu/equalparts/cardbase/standalone/CardBaseCLI.java @@ -0,0 +1,69 @@ +package eu.equalparts.cardbase.standalone; + +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; + +import eu.equalparts.cardbase.data.CardBase; + +/** + * This provides a lightweight CLI for interacting with cardbase files. + * + */ +public class CardBaseCLI { + + /** + * Execute the interface. + * + * @param args the first argument is the cardbase file. Further arguments are ignored. + */ + 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 { + // 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; + } + } + } + } catch (IOException e) { + e.printStackTrace(); + } + + + } + +} -- cgit v1.2.3