diff options
author | Eduardo Pedroni <ep625@york.ac.uk> | 2015-07-13 03:31:02 +0200 |
---|---|---|
committer | Eduardo Pedroni <ep625@york.ac.uk> | 2015-07-13 03:31:02 +0200 |
commit | 19183986d37c5ca1a31a5b308bace92b87430ffe (patch) | |
tree | fc2bf0fc6bed0fa836a8c1401f16d967b639b491 /src | |
parent | e319b8b0790d442d836958df41302af91d750313 (diff) |
Added some statistics functionality, TDD
Diffstat (limited to 'src')
13 files changed, 141 insertions, 49 deletions
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<String, ReferenceDeck> getDecks() { return Collections.unmodifiableMap(decks); } @@ -205,7 +181,7 @@ public class Cardbase { public List<Card> getMissingCards(StandaloneDeck deckToCheck) { List<Card> missingCards = new ArrayList<Card>(); 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/data/Card.java b/src/eu/equalparts/cardbase/cards/Card.java index 8ac5cd1..6afdff8 100644 --- a/src/eu/equalparts/cardbase/data/Card.java +++ b/src/eu/equalparts/cardbase/cards/Card.java @@ -1,4 +1,4 @@ -package eu.equalparts.cardbase.data; +package eu.equalparts.cardbase.cards; import eu.equalparts.cardbase.comparator.SpecialFields.DirtyNumber; import eu.equalparts.cardbase.comparator.SpecialFields.Rarity; diff --git a/src/eu/equalparts/cardbase/data/CardSetInformation.java b/src/eu/equalparts/cardbase/cards/CardSetInformation.java index d3394b7..587c99a 100644 --- a/src/eu/equalparts/cardbase/data/CardSetInformation.java +++ b/src/eu/equalparts/cardbase/cards/CardSetInformation.java @@ -1,4 +1,4 @@ -package eu.equalparts.cardbase.data; +package eu.equalparts.cardbase.cards; public class CardSetInformation { diff --git a/src/eu/equalparts/cardbase/data/FullCardSet.java b/src/eu/equalparts/cardbase/cards/FullCardSet.java index a7f3ea5..d627111 100644 --- a/src/eu/equalparts/cardbase/data/FullCardSet.java +++ b/src/eu/equalparts/cardbase/cards/FullCardSet.java @@ -1,4 +1,4 @@ -package eu.equalparts.cardbase.data; +package eu.equalparts.cardbase.cards; import java.util.Map; 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/Deck.java b/src/eu/equalparts/cardbase/decks/Deck.java index 6a6658a..1f28d60 100644 --- a/src/eu/equalparts/cardbase/data/Deck.java +++ b/src/eu/equalparts/cardbase/decks/Deck.java @@ -1,4 +1,4 @@ -package eu.equalparts.cardbase.data; +package eu.equalparts.cardbase.decks; public abstract class Deck { diff --git a/src/eu/equalparts/cardbase/data/ReferenceDeck.java b/src/eu/equalparts/cardbase/decks/ReferenceDeck.java index 7cdc15f..9e8695b 100644 --- a/src/eu/equalparts/cardbase/data/ReferenceDeck.java +++ b/src/eu/equalparts/cardbase/decks/ReferenceDeck.java @@ -1,9 +1,10 @@ -package eu.equalparts.cardbase.data; +package eu.equalparts.cardbase.decks; import java.util.HashMap; import java.util.Map; -import eu.equalparts.cardbase.Cardbase; +import eu.equalparts.cardbase.cards.Card; +import eu.equalparts.cardbase.utils.UID; public class ReferenceDeck extends Deck { @@ -22,7 +23,7 @@ public class ReferenceDeck extends Deck { this.forests = deck.forests; for (Card card : deck.cards) { - cardReferences.put(Cardbase.makeHash(card), card.count); + cardReferences.put(UID.makeHash(card), card.count); } } diff --git a/src/eu/equalparts/cardbase/data/StandaloneDeck.java b/src/eu/equalparts/cardbase/decks/StandaloneDeck.java index bfb7b55..4c6d25c 100644 --- a/src/eu/equalparts/cardbase/data/StandaloneDeck.java +++ b/src/eu/equalparts/cardbase/decks/StandaloneDeck.java @@ -1,8 +1,10 @@ -package eu.equalparts.cardbase.data; +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<Card> cards = new HashSet<Card>(); 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; + } + +} |