diff options
-rw-r--r-- | src/eu/equalparts/cardbase/Cardbase.java | 86 | ||||
-rw-r--r-- | src/eu/equalparts/cardbase/cli/CardbaseCLI.java | 2 | ||||
-rw-r--r-- | src/eu/equalparts/cardbase/data/Card.java | 28 | ||||
-rw-r--r-- | src/eu/equalparts/cardbase/data/Deck.java | 14 | ||||
-rw-r--r-- | test/Testest.java | 50 |
5 files changed, 130 insertions, 50 deletions
diff --git a/src/eu/equalparts/cardbase/Cardbase.java b/src/eu/equalparts/cardbase/Cardbase.java index 9245870..d69dee3 100644 --- a/src/eu/equalparts/cardbase/Cardbase.java +++ b/src/eu/equalparts/cardbase/Cardbase.java @@ -16,6 +16,8 @@ import com.fasterxml.jackson.databind.JsonMappingException; import eu.equalparts.cardbase.comparator.CardComparator; import eu.equalparts.cardbase.data.Card; +import eu.equalparts.cardbase.data.ReferenceDeck; +import eu.equalparts.cardbase.data.StandaloneDeck; import eu.equalparts.cardbase.utils.JSON; /** @@ -31,6 +33,10 @@ public class Cardbase { */ private Map<String, Card> cards; /** + * The decks which have been saved along with this collection of cards. + */ + private Map<String, ReferenceDeck> decks; + /** * Debug flag is raised when the DEBUG environment variable is set. This causes additional * information to be printed to the console. */ @@ -41,13 +47,6 @@ public class Cardbase { private static final String HASH_DIVIDER = "~"; /** - * Creates an empty cardbase. - */ - public Cardbase() { - cards = new HashMap<String, Card>(); - } - - /** * Initialises the cardbase with the contents of a file. * * @param cardbaseFile the cardbase JSON to load. @@ -59,6 +58,14 @@ public class Cardbase { public Cardbase(File cardbaseFile) throws JsonParseException, JsonMappingException, IOException { cards = JSON.mapper.readValue(cardbaseFile, new TypeReference<Map<String, Card>>() {}); } + + /** + * Initialises a clean cardbase. + */ + public Cardbase() { + cards = new HashMap<String, Card>(); + decks = new HashMap<String, ReferenceDeck>(); + } /** * Writes the provided {@code Cardbase} to the provided file in JSON format. @@ -70,7 +77,7 @@ public class Cardbase { * @throws JsonMappingException if the data structure given does not generate valid JSON as well? * @throws IOException if a low-level I/O problem (unexpected end-of-input, network error) occurs. */ - public void writeCardbase(File outputFile) throws JsonGenerationException, JsonMappingException, IOException { + public void writeCollection(File outputFile) throws JsonGenerationException, JsonMappingException, IOException { JSON.mapper.writeValue(outputFile, cards); } @@ -166,7 +173,7 @@ public class Cardbase { * @param hash the Cardbase hash of the requested card. * @return the requested {@code Card} or null if no card is found. */ - public Card getCardFromHash(String hash) { + public Card getCardByHash(String hash) { return cards.get(hash); } @@ -190,4 +197,65 @@ public class Cardbase { public static String makeHash(Card card) { return card.setCode + HASH_DIVIDER + card.number; } + + public Map<String, ReferenceDeck> getDecks() { + return Collections.unmodifiableMap(decks); + } + + public List<Card> getMissingCards(StandaloneDeck deckToCheck) { + List<Card> missingCards = new ArrayList<Card>(); + for (Card card : deckToCheck.cards) { + String hash = makeHash(card); + if (cards.containsKey(hash)) { + if (cards.get(hash).count < card.count) { + Card missingCard = card.clone(); + missingCard.count = card.count - cards.get(hash).count; + missingCards.add(missingCard); + } + } else { + missingCards.add(card); + } + } + return missingCards; + } + + public void addStandaloneDeck(StandaloneDeck deckToAdd) { + List<Card> missingCards = getMissingCards(deckToAdd); + if (missingCards.size() <= 0) { + decks.put(deckToAdd.name, new ReferenceDeck(deckToAdd)); + } else { + throw new IllegalArgumentException("The cardbase is missing cards to add this deck."); + } + } + + public StandaloneDeck exportDeck(String deckName) { + ReferenceDeck referenceDeck = decks.get(deckName); + + if (referenceDeck != null) { + StandaloneDeck standaloneDeck = new StandaloneDeck(); + + standaloneDeck.name = referenceDeck.name; + standaloneDeck.plains = referenceDeck.plains; + standaloneDeck.islands = referenceDeck.islands; + standaloneDeck.swamps = referenceDeck.swamps; + standaloneDeck.mountains = referenceDeck.mountains; + standaloneDeck.forests = referenceDeck.forests; + + for (String cardHash : referenceDeck.cardReferences.keySet()) { + Card card = getCardByHash(cardHash); + if (card != null) { + // must clone otherwise the original count is affected too + card = card.clone(); + card.count = referenceDeck.cardReferences.get(cardHash); + standaloneDeck.cards.add(card); + } else { + throw new IllegalArgumentException("Deck refers to card not in cardbase: " + cardHash); + } + } + + return standaloneDeck; + } else { + throw new IllegalArgumentException("The specified deck does not exist."); + } + } } diff --git a/src/eu/equalparts/cardbase/cli/CardbaseCLI.java b/src/eu/equalparts/cardbase/cli/CardbaseCLI.java index 866314a..d67a198 100644 --- a/src/eu/equalparts/cardbase/cli/CardbaseCLI.java +++ b/src/eu/equalparts/cardbase/cli/CardbaseCLI.java @@ -220,7 +220,7 @@ public class CardbaseCLI { } else { // handle these exceptions locally - they don't necessarily mean the program should exit try { - cardbase.writeCardbase(outputFile); + cardbase.writeCollection(outputFile); // we are now working off outputFile, which may or may not be the same as cardbaseFile at this point cardbaseFile = outputFile; System.out.println("Cardbase was saved to \"" + outputFile.getAbsolutePath() + "\". " diff --git a/src/eu/equalparts/cardbase/data/Card.java b/src/eu/equalparts/cardbase/data/Card.java index e2ad3f0..8ac5cd1 100644 --- a/src/eu/equalparts/cardbase/data/Card.java +++ b/src/eu/equalparts/cardbase/data/Card.java @@ -32,4 +32,32 @@ public class Card { public String imageCode; public Integer count; + @Override + public Card clone() { + Card clone = new Card(); + + clone.name = this.name; + clone.layout = this.layout; + clone.manaCost = this.manaCost; + clone.cmc = this.cmc; + clone.type = this.type; + clone.rarity = this.rarity; + clone.text = this.text; + clone.flavor = this.flavor; + clone.artist = this.artist; + clone.number = this.number; + clone.power = this.power; + clone.toughness = this.toughness; + clone.loyalty = this.loyalty; + clone.multiverseid = this.multiverseid; + clone.imageName = this.imageName; + clone.border = this.border; + clone.watermark = this.watermark; + clone.setCode = this.setCode; + clone.imageCode = this.imageCode; + clone.count = this.count; + + return clone; + } + }
\ No newline at end of file diff --git a/src/eu/equalparts/cardbase/data/Deck.java b/src/eu/equalparts/cardbase/data/Deck.java index 8a9ed12..6a6658a 100644 --- a/src/eu/equalparts/cardbase/data/Deck.java +++ b/src/eu/equalparts/cardbase/data/Deck.java @@ -1,12 +1,12 @@ package eu.equalparts.cardbase.data; -import java.util.HashMap; +public abstract class Deck { -public class Deck { - - /** - * Cards stored in key-value pairs of [multiverse ID, amount]. - */ - public HashMap<Integer, Integer> cards; + public String name = "Unnamed Deck"; + public int plains = 0, + islands = 0, + swamps = 0, + mountains = 0, + forests = 0; } diff --git a/test/Testest.java b/test/Testest.java index 016cbf4..7128b75 100644 --- a/test/Testest.java +++ b/test/Testest.java @@ -1,54 +1,38 @@ import java.io.File; import java.io.IOException; import java.net.MalformedURLException; -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.JsonMappingException; +import eu.equalparts.cardbase.Cardbase; import eu.equalparts.cardbase.data.Card; +import eu.equalparts.cardbase.data.StandaloneDeck; import eu.equalparts.cardbase.utils.JSON; public class Testest { - - - public static class FullCardSet { - - public String name; - public String code; - public String magicCardsInfoCode; - public String releaseDate; - public String border; - public String type; - public String block; - public String gathererCode; - public List<Card> cards; - - public FullCardSet() {} - } - public static void main(String... args) throws JsonParseException, JsonMappingException, MalformedURLException, IOException { - Map<String, FullCardSet> setMap = JSON.mapper.readValue(new File("./AllSets.json"), new TypeReference<Map<String, FullCardSet>> () {}); - - List<String> rarities = new ArrayList<String>(); + StandaloneDeck deck = JSON.mapper.readValue(new File("control.cbd"), StandaloneDeck.class); - int count = 0; + System.out.println(deck.name); + System.out.println("plains: " + deck.plains); + System.out.println("islands: " + deck.islands); + System.out.println("swamps: " + deck.swamps); + System.out.println("mountains: " + deck.mountains); + System.out.println("forests: " + deck.forests); - for (FullCardSet cardSet : setMap.values()) { - for (Card card : cardSet.cards) { - count++; - if (!rarities.contains(card.rarity)) { - rarities.add(card.rarity); - System.out.println(card.rarity); - } - } + for (Card card : deck.cards) { + System.out.println(card.count + "x " + card.name); } - System.out.println("Done, " + count + " cards looked at."); + Cardbase cards = new Cardbase(new File("cards.cb")); + List<Card> missingCards = cards.getMissingCards(deck); + + for (Card card : missingCards) { + System.out.println(card.count + "x " + card.name); + } } } |