diff options
author | Eduardo Pedroni <ep625@york.ac.uk> | 2015-07-12 21:00:53 +0200 |
---|---|---|
committer | Eduardo Pedroni <ep625@york.ac.uk> | 2015-07-12 21:00:53 +0200 |
commit | 7d3f8acf5260bf0d5458d258111e05a05859e535 (patch) | |
tree | 5e4f4591db0fbeec0fc55352bc416d60b55f9176 /src/eu/equalparts/cardbase | |
parent | dab875c97790cc482b9a74ad0339b71ff0aab509 (diff) |
Started fiddling with decks
Diffstat (limited to 'src/eu/equalparts/cardbase')
-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 |
4 files changed, 113 insertions, 17 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; } |