From 19183986d37c5ca1a31a5b308bace92b87430ffe Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Mon, 13 Jul 2015 03:31:02 +0200 Subject: Added some statistics functionality, TDD --- src/eu/equalparts/cardbase/Cardbase.java | 42 +++--------- src/eu/equalparts/cardbase/cards/Card.java | 63 +++++++++++++++++ .../cardbase/cards/CardSetInformation.java | 34 +++++++++ src/eu/equalparts/cardbase/cards/FullCardSet.java | 26 +++++++ src/eu/equalparts/cardbase/cli/CardbaseCLI.java | 6 +- .../cardbase/comparator/CardComparator.java | 2 +- src/eu/equalparts/cardbase/data/Card.java | 63 ----------------- .../cardbase/data/CardSetInformation.java | 34 --------- src/eu/equalparts/cardbase/data/Deck.java | 12 ---- src/eu/equalparts/cardbase/data/FullCardSet.java | 26 ------- src/eu/equalparts/cardbase/data/ReferenceDeck.java | 29 -------- .../equalparts/cardbase/data/StandaloneDeck.java | 10 --- src/eu/equalparts/cardbase/decks/Deck.java | 12 ++++ .../equalparts/cardbase/decks/ReferenceDeck.java | 30 ++++++++ .../equalparts/cardbase/decks/StandaloneDeck.java | 12 ++++ src/eu/equalparts/cardbase/decks/Statistics.java | 80 ++++++++++++++++++++++ src/eu/equalparts/cardbase/gui/CardView.java | 2 +- src/eu/equalparts/cardbase/utils/MTGUniverse.java | 6 +- src/eu/equalparts/cardbase/utils/UID.java | 33 +++++++++ 19 files changed, 307 insertions(+), 215 deletions(-) create mode 100644 src/eu/equalparts/cardbase/cards/Card.java create mode 100644 src/eu/equalparts/cardbase/cards/CardSetInformation.java create mode 100644 src/eu/equalparts/cardbase/cards/FullCardSet.java delete mode 100644 src/eu/equalparts/cardbase/data/Card.java delete mode 100644 src/eu/equalparts/cardbase/data/CardSetInformation.java delete mode 100644 src/eu/equalparts/cardbase/data/Deck.java delete mode 100644 src/eu/equalparts/cardbase/data/FullCardSet.java delete mode 100644 src/eu/equalparts/cardbase/data/ReferenceDeck.java delete mode 100644 src/eu/equalparts/cardbase/data/StandaloneDeck.java create mode 100644 src/eu/equalparts/cardbase/decks/Deck.java create mode 100644 src/eu/equalparts/cardbase/decks/ReferenceDeck.java create mode 100644 src/eu/equalparts/cardbase/decks/StandaloneDeck.java create mode 100644 src/eu/equalparts/cardbase/decks/Statistics.java create mode 100644 src/eu/equalparts/cardbase/utils/UID.java (limited to 'src/eu') diff --git a/src/eu/equalparts/cardbase/Cardbase.java b/src/eu/equalparts/cardbase/Cardbase.java index d69dee3..04a2760 100644 --- a/src/eu/equalparts/cardbase/Cardbase.java +++ b/src/eu/equalparts/cardbase/Cardbase.java @@ -14,11 +14,12 @@ import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JsonMappingException; +import eu.equalparts.cardbase.cards.Card; 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.decks.ReferenceDeck; +import eu.equalparts.cardbase.decks.StandaloneDeck; import eu.equalparts.cardbase.utils.JSON; +import eu.equalparts.cardbase.utils.UID; /** * Provides a variety of utility methods to interact with an optionally loaded cardbase. @@ -40,11 +41,7 @@ public class Cardbase { * Debug flag is raised when the DEBUG environment variable is set. This causes additional * information to be printed to the console. */ - public static final boolean DEBUG = System.getenv("CB_DEBUG") != null; - /** - * Used in the hash generation. - */ - private static final String HASH_DIVIDER = "~"; + public static final boolean DEBUG = System.getenv("CB_DEBUG") != null; /** * Initialises the cardbase with the contents of a file. @@ -95,7 +92,7 @@ public class Cardbase { card.count += count; } else { cardToAdd.count = count; - cards.put(makeHash(cardToAdd), cardToAdd); + cards.put(UID.makeHash(cardToAdd), cardToAdd); } } @@ -120,7 +117,7 @@ public class Cardbase { Integer removed = 0; if (card != null) { if (card.count <= count) { - cards.remove(makeHash(card)); + cards.remove(UID.makeHash(card)); removed = card.count; } else { card.count -= count; @@ -162,7 +159,7 @@ public class Cardbase { * @return the requested {@code Card} or null if no card is found. */ public Card getCard(String setCode, String number) { - return cards.get(makeHash(setCode, number)); + return cards.get(UID.makeHash(setCode, number)); } /** @@ -177,27 +174,6 @@ public class Cardbase { return cards.get(hash); } - /** - * Generate the hash used as a key in the storage map. - * - * @param setCode the card's set code. - * @param number the card's set number. - * @return the generated hash. - */ - public static String makeHash(String setCode, String number) { - return setCode + HASH_DIVIDER + number; - } - - /** - * Generate the hash used as a key in the storage map. - * - * @param the {@code Card} whose hash is desired. - * @return the generated hash. - */ - public static String makeHash(Card card) { - return card.setCode + HASH_DIVIDER + card.number; - } - public Map getDecks() { return Collections.unmodifiableMap(decks); } @@ -205,7 +181,7 @@ public class Cardbase { public List getMissingCards(StandaloneDeck deckToCheck) { List missingCards = new ArrayList(); for (Card card : deckToCheck.cards) { - String hash = makeHash(card); + String hash = UID.makeHash(card); if (cards.containsKey(hash)) { if (cards.get(hash).count < card.count) { Card missingCard = card.clone(); diff --git a/src/eu/equalparts/cardbase/cards/Card.java b/src/eu/equalparts/cardbase/cards/Card.java new file mode 100644 index 0000000..6afdff8 --- /dev/null +++ b/src/eu/equalparts/cardbase/cards/Card.java @@ -0,0 +1,63 @@ +package eu.equalparts.cardbase.cards; + +import eu.equalparts.cardbase.comparator.SpecialFields.DirtyNumber; +import eu.equalparts.cardbase.comparator.SpecialFields.Rarity; + +public class Card { + + public String name; + public String layout; + public String manaCost; + public Integer cmc; + public String type; + @Rarity + public String rarity; + public String text; + public String flavor; + public String artist; + @DirtyNumber + public String number; + @DirtyNumber + public String power; + @DirtyNumber + public String toughness; + public Integer loyalty; + public Integer multiverseid; + public String imageName; + public String border; + public String watermark; + + // Not part of upstream JSON + public String setCode; + 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/cards/CardSetInformation.java b/src/eu/equalparts/cardbase/cards/CardSetInformation.java new file mode 100644 index 0000000..587c99a --- /dev/null +++ b/src/eu/equalparts/cardbase/cards/CardSetInformation.java @@ -0,0 +1,34 @@ +package eu.equalparts.cardbase.cards; + +public class CardSetInformation { + + private String name; + private String code; + private String releaseDate; + + /** + * @return the set's name. + */ + public String getName() { + return name; + } + + /** + * @return the set code. + */ + public String getCode() { + return code; + } + + /** + * @return the set's release date. + */ + public String getReleaseDate() { + return releaseDate; + } + + @Override + public String toString() { + return String.format("%1$-12s : %2$s", code, name, releaseDate); + } +} diff --git a/src/eu/equalparts/cardbase/cards/FullCardSet.java b/src/eu/equalparts/cardbase/cards/FullCardSet.java new file mode 100644 index 0000000..d627111 --- /dev/null +++ b/src/eu/equalparts/cardbase/cards/FullCardSet.java @@ -0,0 +1,26 @@ +package eu.equalparts.cardbase.cards; + +import java.util.Map; + +public 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 Map 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 requested {@code Card}, or null if no card is found with that number. + */ + public Card getCardByNumber(String number) { + return cards.get(number); + } +} \ No newline at end of file diff --git a/src/eu/equalparts/cardbase/cli/CardbaseCLI.java b/src/eu/equalparts/cardbase/cli/CardbaseCLI.java index d67a198..4d72d5b 100644 --- a/src/eu/equalparts/cardbase/cli/CardbaseCLI.java +++ b/src/eu/equalparts/cardbase/cli/CardbaseCLI.java @@ -13,9 +13,9 @@ import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.JsonMappingException; import eu.equalparts.cardbase.Cardbase; -import eu.equalparts.cardbase.data.Card; -import eu.equalparts.cardbase.data.FullCardSet; -import eu.equalparts.cardbase.data.CardSetInformation; +import eu.equalparts.cardbase.cards.Card; +import eu.equalparts.cardbase.cards.CardSetInformation; +import eu.equalparts.cardbase.cards.FullCardSet; import eu.equalparts.cardbase.utils.MTGUniverse; /** diff --git a/src/eu/equalparts/cardbase/comparator/CardComparator.java b/src/eu/equalparts/cardbase/comparator/CardComparator.java index fe440f3..9cb72b4 100644 --- a/src/eu/equalparts/cardbase/comparator/CardComparator.java +++ b/src/eu/equalparts/cardbase/comparator/CardComparator.java @@ -8,9 +8,9 @@ import java.util.Comparator; import java.util.function.BiFunction; import eu.equalparts.cardbase.Cardbase; +import eu.equalparts.cardbase.cards.Card; import eu.equalparts.cardbase.comparator.SpecialFields.DirtyNumber; import eu.equalparts.cardbase.comparator.SpecialFields.Rarity; -import eu.equalparts.cardbase.data.Card; /** * I'm new to this reflection business, so bear with me. diff --git a/src/eu/equalparts/cardbase/data/Card.java b/src/eu/equalparts/cardbase/data/Card.java deleted file mode 100644 index 8ac5cd1..0000000 --- a/src/eu/equalparts/cardbase/data/Card.java +++ /dev/null @@ -1,63 +0,0 @@ -package eu.equalparts.cardbase.data; - -import eu.equalparts.cardbase.comparator.SpecialFields.DirtyNumber; -import eu.equalparts.cardbase.comparator.SpecialFields.Rarity; - -public class Card { - - public String name; - public String layout; - public String manaCost; - public Integer cmc; - public String type; - @Rarity - public String rarity; - public String text; - public String flavor; - public String artist; - @DirtyNumber - public String number; - @DirtyNumber - public String power; - @DirtyNumber - public String toughness; - public Integer loyalty; - public Integer multiverseid; - public String imageName; - public String border; - public String watermark; - - // Not part of upstream JSON - public String setCode; - 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/CardSetInformation.java b/src/eu/equalparts/cardbase/data/CardSetInformation.java deleted file mode 100644 index d3394b7..0000000 --- a/src/eu/equalparts/cardbase/data/CardSetInformation.java +++ /dev/null @@ -1,34 +0,0 @@ -package eu.equalparts.cardbase.data; - -public class CardSetInformation { - - private String name; - private String code; - private String releaseDate; - - /** - * @return the set's name. - */ - public String getName() { - return name; - } - - /** - * @return the set code. - */ - public String getCode() { - return code; - } - - /** - * @return the set's release date. - */ - public String getReleaseDate() { - return releaseDate; - } - - @Override - public String toString() { - return String.format("%1$-12s : %2$s", code, name, releaseDate); - } -} diff --git a/src/eu/equalparts/cardbase/data/Deck.java b/src/eu/equalparts/cardbase/data/Deck.java deleted file mode 100644 index 6a6658a..0000000 --- a/src/eu/equalparts/cardbase/data/Deck.java +++ /dev/null @@ -1,12 +0,0 @@ -package eu.equalparts.cardbase.data; - -public abstract class Deck { - - public String name = "Unnamed Deck"; - public int plains = 0, - islands = 0, - swamps = 0, - mountains = 0, - forests = 0; - -} diff --git a/src/eu/equalparts/cardbase/data/FullCardSet.java b/src/eu/equalparts/cardbase/data/FullCardSet.java deleted file mode 100644 index a7f3ea5..0000000 --- a/src/eu/equalparts/cardbase/data/FullCardSet.java +++ /dev/null @@ -1,26 +0,0 @@ -package eu.equalparts.cardbase.data; - -import java.util.Map; - -public 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 Map 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 requested {@code Card}, or null if no card is found with that number. - */ - public Card getCardByNumber(String number) { - return cards.get(number); - } -} \ No newline at end of file diff --git a/src/eu/equalparts/cardbase/data/ReferenceDeck.java b/src/eu/equalparts/cardbase/data/ReferenceDeck.java deleted file mode 100644 index 7cdc15f..0000000 --- a/src/eu/equalparts/cardbase/data/ReferenceDeck.java +++ /dev/null @@ -1,29 +0,0 @@ -package eu.equalparts.cardbase.data; - -import java.util.HashMap; -import java.util.Map; - -import eu.equalparts.cardbase.Cardbase; - -public class ReferenceDeck extends Deck { - - public Map cardReferences = new HashMap(); - - public ReferenceDeck() { - - } - - public ReferenceDeck(StandaloneDeck deck) { - this.name = deck.name; - this.plains = deck.plains; - this.islands = deck.islands; - this.swamps = deck.swamps; - this.mountains = deck.mountains; - this.forests = deck.forests; - - for (Card card : deck.cards) { - cardReferences.put(Cardbase.makeHash(card), card.count); - } - } - -} diff --git a/src/eu/equalparts/cardbase/data/StandaloneDeck.java b/src/eu/equalparts/cardbase/data/StandaloneDeck.java deleted file mode 100644 index bfb7b55..0000000 --- a/src/eu/equalparts/cardbase/data/StandaloneDeck.java +++ /dev/null @@ -1,10 +0,0 @@ -package eu.equalparts.cardbase.data; - -import java.util.HashSet; -import java.util.Set; - -public class StandaloneDeck extends Deck { - - public Set cards = new HashSet(); - -} diff --git a/src/eu/equalparts/cardbase/decks/Deck.java b/src/eu/equalparts/cardbase/decks/Deck.java new file mode 100644 index 0000000..1f28d60 --- /dev/null +++ b/src/eu/equalparts/cardbase/decks/Deck.java @@ -0,0 +1,12 @@ +package eu.equalparts.cardbase.decks; + +public abstract class Deck { + + public String name = "Unnamed Deck"; + public int plains = 0, + islands = 0, + swamps = 0, + mountains = 0, + forests = 0; + +} diff --git a/src/eu/equalparts/cardbase/decks/ReferenceDeck.java b/src/eu/equalparts/cardbase/decks/ReferenceDeck.java new file mode 100644 index 0000000..9e8695b --- /dev/null +++ b/src/eu/equalparts/cardbase/decks/ReferenceDeck.java @@ -0,0 +1,30 @@ +package eu.equalparts.cardbase.decks; + +import java.util.HashMap; +import java.util.Map; + +import eu.equalparts.cardbase.cards.Card; +import eu.equalparts.cardbase.utils.UID; + +public class ReferenceDeck extends Deck { + + public Map cardReferences = new HashMap(); + + public ReferenceDeck() { + + } + + public ReferenceDeck(StandaloneDeck deck) { + this.name = deck.name; + this.plains = deck.plains; + this.islands = deck.islands; + this.swamps = deck.swamps; + this.mountains = deck.mountains; + this.forests = deck.forests; + + for (Card card : deck.cards) { + cardReferences.put(UID.makeHash(card), card.count); + } + } + +} diff --git a/src/eu/equalparts/cardbase/decks/StandaloneDeck.java b/src/eu/equalparts/cardbase/decks/StandaloneDeck.java new file mode 100644 index 0000000..4c6d25c --- /dev/null +++ b/src/eu/equalparts/cardbase/decks/StandaloneDeck.java @@ -0,0 +1,12 @@ +package eu.equalparts.cardbase.decks; + +import java.util.HashSet; +import java.util.Set; + +import eu.equalparts.cardbase.cards.Card; + +public class StandaloneDeck extends Deck { + + public Set cards = new HashSet(); + +} diff --git a/src/eu/equalparts/cardbase/decks/Statistics.java b/src/eu/equalparts/cardbase/decks/Statistics.java new file mode 100644 index 0000000..9efc82a --- /dev/null +++ b/src/eu/equalparts/cardbase/decks/Statistics.java @@ -0,0 +1,80 @@ +package eu.equalparts.cardbase.decks; + +import eu.equalparts.cardbase.cards.Card; + +public final class Statistics { + + private Statistics() {} + + + + public static double calculatePercentage(StandaloneDeck deck, String type) { + double allCardsByType = count(deck, type); + double allCards = count(deck); + return allCardsByType / allCards; + } + + public static int count(StandaloneDeck deck, String type) { + int count = type.contains("Land") ? countBasicLands(deck) : 0; + for (Card card : deck.cards) { + if (card.type != null && + card.type.contains(type)) { + count += card.count; + } + } + return count; + } + + public static int count(StandaloneDeck deck) { + int totalCards = countBasicLands(deck); + + for (Card card : deck.cards) { + totalCards += card.count; + } + + return totalCards; + } + + private static int countBasicLands(StandaloneDeck deck) { + return deck.plains + + deck.islands + + deck.swamps + + deck.mountains + + deck.forests; + } + + public static int[] computeDistribution(StandaloneDeck deck, String type) { + int arraySize = 0; + for (Card card : deck.cards) { + if (card.type != null && card.type.contains(type)) + if (card.cmc != null && card.cmc >= arraySize) + arraySize = card.cmc + 1; + } + + int[] costs = new int[arraySize]; + for (Card card : deck.cards) { + if (card.type != null && card.type.contains(type)) + if (card.cmc != null) + costs[card.cmc] += card.count; + } + + return costs; + } + + public static int[] computeDistribution(StandaloneDeck deck) { + int arraySize = 0; + for (Card card : deck.cards) { + if (card.cmc != null && card.cmc >= arraySize) + arraySize = card.cmc + 1; + } + + int[] costs = new int[arraySize]; + for (Card card : deck.cards) { + if (card.cmc != null) + costs[card.cmc] += card.count; + } + + return costs; + } + +} diff --git a/src/eu/equalparts/cardbase/gui/CardView.java b/src/eu/equalparts/cardbase/gui/CardView.java index 61504ee..2ec7576 100644 --- a/src/eu/equalparts/cardbase/gui/CardView.java +++ b/src/eu/equalparts/cardbase/gui/CardView.java @@ -1,6 +1,6 @@ package eu.equalparts.cardbase.gui; -import eu.equalparts.cardbase.data.Card; +import eu.equalparts.cardbase.cards.Card; import javafx.event.EventHandler; import javafx.scene.image.ImageView; import javafx.scene.input.MouseEvent; diff --git a/src/eu/equalparts/cardbase/utils/MTGUniverse.java b/src/eu/equalparts/cardbase/utils/MTGUniverse.java index b52c3e6..1e8efc2 100644 --- a/src/eu/equalparts/cardbase/utils/MTGUniverse.java +++ b/src/eu/equalparts/cardbase/utils/MTGUniverse.java @@ -12,9 +12,9 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.JsonNode; -import eu.equalparts.cardbase.data.Card; -import eu.equalparts.cardbase.data.CardSetInformation; -import eu.equalparts.cardbase.data.FullCardSet; +import eu.equalparts.cardbase.cards.Card; +import eu.equalparts.cardbase.cards.CardSetInformation; +import eu.equalparts.cardbase.cards.FullCardSet; /** * Access point to the complete set of cards that exist in the diff --git a/src/eu/equalparts/cardbase/utils/UID.java b/src/eu/equalparts/cardbase/utils/UID.java new file mode 100644 index 0000000..ae4a11d --- /dev/null +++ b/src/eu/equalparts/cardbase/utils/UID.java @@ -0,0 +1,33 @@ +package eu.equalparts.cardbase.utils; + +import eu.equalparts.cardbase.cards.Card; + +public class UID { + + /** + * Used in the hash generation. + */ + private static final String HASH_DIVIDER = "~"; + + /** + * Generate the hash used as a key in the storage map. + * + * @param setCode the card's set code. + * @param number the card's set number. + * @return the generated hash. + */ + public static String makeHash(String setCode, String number) { + return setCode + HASH_DIVIDER + number; + } + + /** + * Generate the hash used as a key in the storage map. + * + * @param the {@code Card} whose hash is desired. + * @return the generated hash. + */ + public static String makeHash(Card card) { + return card.setCode + HASH_DIVIDER + card.number; + } + +} -- cgit v1.2.3