From e36558c84c443b2236eb3dacd2e3f66981818232 Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Mon, 14 Mar 2016 21:51:01 +0100 Subject: Transitioning to a better way to store card counts, not done yet though --- src/eu/equalparts/cardbase/Cardbase.java | 160 +++++++++++---------- src/eu/equalparts/cardbase/cards/Card.java | 4 +- src/eu/equalparts/cardbase/cli/CardbaseCLI.java | 6 +- .../equalparts/cardbase/decks/ReferenceDeck.java | 3 +- src/eu/equalparts/cardbase/decks/Statistics.java | 12 +- test/eu/equalparts/cardbase/decks/DeckTest.java | 43 +++--- 6 files changed, 124 insertions(+), 104 deletions(-) diff --git a/src/eu/equalparts/cardbase/Cardbase.java b/src/eu/equalparts/cardbase/Cardbase.java index 15e4860..af8e7d5 100644 --- a/src/eu/equalparts/cardbase/Cardbase.java +++ b/src/eu/equalparts/cardbase/Cardbase.java @@ -32,6 +32,10 @@ public class Cardbase { * generated using {makeHash()}. */ private Map cards; + /** + * TODO comment + */ + private Map collection; /** * The decks which have been saved along with this collection of cards. */ @@ -87,14 +91,18 @@ public class Cardbase { * already exists. *TODO fix comment */ - public void addCard(Card cardToAdd, int count) { - Card card = getCardByHash(cardToAdd.hashCode()); - if (card != null) { - card.count += count; + public void addCard(Card cardToAdd, int addCount) { + Integer hashCode = cardToAdd.hashCode(); + + // ensure that card is in the card map + cards.putIfAbsent(hashCode, cardToAdd); + + // ensure that card is in the collection, with the correct count + Integer currentCount = collection.get(hashCode); + if (currentCount != null) { + collection.replace(hashCode, currentCount + addCount); } else { - // TODO disgusting, refactor - cardToAdd.count = count; - cards.put(cardToAdd.hashCode(), cardToAdd); + collection.put(hashCode, addCount); } } @@ -111,22 +119,23 @@ public class Cardbase { * Shivan Dragon is removed from the cardbase, and the value returned is 5. * * @param cardToRemove the card to be removed. - * @param count the amount of the card to be removed. + * @param removeCount the amount of the card to be removed. * @return the number of cards actually removed. *TODO comment */ - public Integer removeCard(Card cardToRemove, int count) { - Card card = getCardByHash(cardToRemove.hashCode()); - // TODO disgusting, refactor - cardToRemove.count = count; - Integer removed = 0; - if (card != null) { - if (card.count <= cardToRemove.count) { - cards.remove(card.hashCode()); - removed = card.count; + public Integer removeCard(Card cardToRemove, int removeCount) { + Integer hashCode = cardToRemove.hashCode(); + int removed = 0; + + Integer currentCount = collection.get(hashCode); + if (currentCount != null) { + if (removeCount >= currentCount) { + collection.remove(hashCode); + cards.remove(hashCode); + removed = currentCount; } else { - card.count -= cardToRemove.count; - removed = cardToRemove.count; + collection.replace(hashCode, currentCount - removeCount); + removed = removeCount; } } return removed; @@ -182,61 +191,66 @@ public class Cardbase { public Map getDecks() { return Collections.unmodifiableMap(decks); } - - public List getMissingCards(StandaloneDeck deckToCheck) { - List missingCards = new ArrayList(); - for (Card card : deckToCheck.cards) { - Integer hash = card.hashCode(); - 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 int getCount(String setCode, String number) { + Integer count = collection.get(Card.makeHash(setCode, number)); + return count != null ? count : 0; } - public void addStandaloneDeck(StandaloneDeck deckToAdd) { - List 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 List getMissingCards(StandaloneDeck deckToCheck) { +// List missingCards = new ArrayList(); +// for (Card card : deckToCheck.cards) { +// Integer hash = card.hashCode(); +// 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 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 (Integer 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."); - } - } +// public void addStandaloneDeck(StandaloneDeck deckToAdd) { +// List 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 (Integer 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/cards/Card.java b/src/eu/equalparts/cardbase/cards/Card.java index 1d8fbb9..512485a 100644 --- a/src/eu/equalparts/cardbase/cards/Card.java +++ b/src/eu/equalparts/cardbase/cards/Card.java @@ -29,7 +29,7 @@ public class Card { // Not part of upstream JSON public String setCode; public String imageCode; - public Integer count; + //public Integer count; @Override public Card clone() { @@ -53,7 +53,7 @@ public class Card { clone.watermark = this.watermark; clone.setCode = this.setCode; clone.imageCode = this.imageCode; - clone.count = this.count; + //clone.count = this.count; return clone; } diff --git a/src/eu/equalparts/cardbase/cli/CardbaseCLI.java b/src/eu/equalparts/cardbase/cli/CardbaseCLI.java index d526e78..8aece5d 100644 --- a/src/eu/equalparts/cardbase/cli/CardbaseCLI.java +++ b/src/eu/equalparts/cardbase/cli/CardbaseCLI.java @@ -337,7 +337,7 @@ public final class CardbaseCLI { int total = 0; for (Card card : cardbase.getCards()) { printGlance(card); - total += card.count; + total += cardbase.getCount(card.setCode, card.number); } System.out.println("Total: " + total); } @@ -365,7 +365,7 @@ public final class CardbaseCLI { int total = 0; for (Card card : cardbase.getCards()) { printPerusal(card); - total += card.count; + total += cardbase.getCount(card.setCode, card.number); } System.out.println("Total: " + total); } @@ -523,7 +523,7 @@ public final class CardbaseCLI { * @param card the card to glance. */ private void printGlance(Card card) { - System.out.println(String.format("%1$-4d %2$s (%3$s, %4$s)", card.count, card.name, card.setCode, card.number)); + System.out.println(String.format("%1$-4d %2$s (%3$s, %4$s)", cardbase.getCount(card.setCode, card.number), card.name, card.setCode, card.number)); } /** diff --git a/src/eu/equalparts/cardbase/decks/ReferenceDeck.java b/src/eu/equalparts/cardbase/decks/ReferenceDeck.java index 5650d2f..2303f24 100644 --- a/src/eu/equalparts/cardbase/decks/ReferenceDeck.java +++ b/src/eu/equalparts/cardbase/decks/ReferenceDeck.java @@ -21,8 +21,9 @@ public class ReferenceDeck extends Deck { this.mountains = deck.mountains; this.forests = deck.forests; + // TODO sort this out for (Card card : deck.cards) { - cardReferences.put(card.hashCode(), card.count); + cardReferences.put(card.hashCode(), 1); } } diff --git a/src/eu/equalparts/cardbase/decks/Statistics.java b/src/eu/equalparts/cardbase/decks/Statistics.java index 9efc82a..d48dd59 100644 --- a/src/eu/equalparts/cardbase/decks/Statistics.java +++ b/src/eu/equalparts/cardbase/decks/Statistics.java @@ -19,7 +19,8 @@ public final class Statistics { for (Card card : deck.cards) { if (card.type != null && card.type.contains(type)) { - count += card.count; + // TODO sort this out + count += 1; } } return count; @@ -29,7 +30,8 @@ public final class Statistics { int totalCards = countBasicLands(deck); for (Card card : deck.cards) { - totalCards += card.count; + // TODO sort this out + totalCards += 1; } return totalCards; @@ -55,7 +57,8 @@ public final class Statistics { for (Card card : deck.cards) { if (card.type != null && card.type.contains(type)) if (card.cmc != null) - costs[card.cmc] += card.count; + // TODO sort this out + costs[card.cmc] += 1; } return costs; @@ -71,7 +74,8 @@ public final class Statistics { int[] costs = new int[arraySize]; for (Card card : deck.cards) { if (card.cmc != null) - costs[card.cmc] += card.count; + // TODO sort this out + costs[card.cmc] += 1; } return costs; diff --git a/test/eu/equalparts/cardbase/decks/DeckTest.java b/test/eu/equalparts/cardbase/decks/DeckTest.java index a35bc32..1c00e5b 100644 --- a/test/eu/equalparts/cardbase/decks/DeckTest.java +++ b/test/eu/equalparts/cardbase/decks/DeckTest.java @@ -28,25 +28,26 @@ public class DeckTest { public void setUp() throws Exception { } - @Test - public void test_createReferenceDeckFromStandaloneDeck() throws Exception { - ObjectMapper mapper = new ObjectMapper(); - StandaloneDeck standaloneDeck = mapper.readValue(getClass().getResourceAsStream("deck.cbd"), StandaloneDeck.class); - - ReferenceDeck uut = new ReferenceDeck(standaloneDeck); - - boolean condition = uut.name == standaloneDeck.name && - uut.plains == standaloneDeck.plains && - uut.islands == standaloneDeck.islands && - uut.swamps == standaloneDeck.swamps && - uut.mountains == standaloneDeck.mountains && - uut.forests == standaloneDeck.forests; - assertTrue("Metadata was not correctly set.", condition); - assertEquals("Wrong number of cards.", uut.cardReferences.size(), standaloneDeck.cards.size()); - for (Card card : standaloneDeck.cards) { - Integer count = uut.cardReferences.get(card.hashCode()); - assertNotNull("Reference missing in deck.", count); - assertEquals("Card count is wrong.", card.count, count); - } - } + // TODO sort out +// @Test +// public void test_createReferenceDeckFromStandaloneDeck() throws Exception { +// ObjectMapper mapper = new ObjectMapper(); +// StandaloneDeck standaloneDeck = mapper.readValue(getClass().getResourceAsStream("deck.cbd"), StandaloneDeck.class); +// +// ReferenceDeck uut = new ReferenceDeck(standaloneDeck); +// +// boolean condition = uut.name == standaloneDeck.name && +// uut.plains == standaloneDeck.plains && +// uut.islands == standaloneDeck.islands && +// uut.swamps == standaloneDeck.swamps && +// uut.mountains == standaloneDeck.mountains && +// uut.forests == standaloneDeck.forests; +// assertTrue("Metadata was not correctly set.", condition); +// assertEquals("Wrong number of cards.", uut.cardReferences.size(), standaloneDeck.cards.size()); +// for (Card card : standaloneDeck.cards) { +// Integer count = uut.cardReferences.get(card.hashCode()); +// assertNotNull("Reference missing in deck.", count); +// assertEquals("Card count is wrong.", card.count, count); +// } +// } } -- cgit v1.2.3