diff --git a/src/main/java/forge/gui/deckeditor/controllers/CEditorQuestCardShop.java b/src/main/java/forge/gui/deckeditor/controllers/CEditorQuestCardShop.java index c509a4c09e6..0e9f2a21c07 100644 --- a/src/main/java/forge/gui/deckeditor/controllers/CEditorQuestCardShop.java +++ b/src/main/java/forge/gui/deckeditor/controllers/CEditorQuestCardShop.java @@ -20,6 +20,7 @@ package forge.gui.deckeditor.controllers; import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.HashMap; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -27,6 +28,8 @@ import java.util.Map.Entry; import javax.swing.JLabel; import javax.swing.JOptionPane; +import org.apache.commons.lang3.tuple.Pair; + import com.google.common.base.Function; import forge.Command; @@ -103,6 +106,9 @@ public final class CEditorQuestCardShop extends ACEditorBase newCards = booster.getCards(); - for (final CardPrinted card : newCards) { - this.getTableDeck().addCard(card); - } + final List newInventory = new LinkedList(newCards); + getTableDeck().addCards(newInventory); final CardListViewer c = new CardListViewer(booster.getName(), "You have found the following cards inside:", newCards); c.show(); @@ -334,11 +340,8 @@ public final class CEditorQuestCardShop extends ACEditorBase newInventory = ItemPool.createFrom(deck.getDeck().getMain(), InventoryItem.class, false); + getTableDeck().addCards(newInventory); JOptionPane.showMessageDialog(null, String.format( "Deck '%s' was added to your decklist.%n%nCards from it were also added to your pool.", deck.getName()), "Thanks for purchasing!", JOptionPane.INFORMATION_MESSAGE); @@ -352,11 +355,6 @@ public final class CEditorQuestCardShop extends ACEditorBase> cardsToRemove) { + this.getTableDeck().removeCards(cardsToRemove); + this.getTableCatalog().addCards(cardsToRemove); + for (Map.Entry item : cardsToRemove) { + if (!(item.getKey() instanceof CardPrinted)) { + continue; + } + CardPrinted card = (CardPrinted)item.getKey(); + final int price = Math.min((int) (this.multiplier * this.getCardValue(card)), + this.questData.getCards().getSellPriceLimit()); + this.questData.getCards().sellCard(card, item.getValue(), price); + } + + this.creditsLabel.setText("Credits: " + this.questData.getAssets().getCredits()); + } /* * (non-Javadoc) @@ -395,6 +409,7 @@ public final class CEditorQuestCardShop extends ACEditorBase> cardsToRemove = new LinkedList>(); + for (Map.Entry item : getTableDeck().getCards()) { + if (4 < item.getValue() && !"Basic Land - ".startsWith(item.getKey().getItemType())) { + cardsToRemove.add(Pair.of(item.getKey(), item.getValue() - 4)); + } + } + removeCards(cardsToRemove); + } + }); + VCurrentDeck.SINGLETON_INSTANCE.getPnlRemButtons().add(creditsLabel); this.creditsLabel.setText("Credits: " + this.questData.getAssets().getCredits()); @@ -446,14 +475,16 @@ public final class CEditorQuestCardShop extends ACEditorBase extends AbstractTab * * @param card0   {@link forge.Card} object */ - public void removeCard(final T card0) { + public void removeCard(final T card0, int qty) { final boolean wasThere = this.data.count(card0) > 0; if (wasThere) { - this.data.remove(card0); + this.data.remove(card0, qty); this.fireTableDataChanged(); } } @@ -152,8 +152,8 @@ public final class EditorTableModel extends AbstractTab * * @param card0   {@link forge.Card} object. */ - public void addCard(final T card0) { - this.data.add(card0); + public void addCard(final T card0, int qty) { + this.data.add(card0, qty); this.fireTableDataChanged(); } diff --git a/src/main/java/forge/gui/deckeditor/tables/EditorTableView.java b/src/main/java/forge/gui/deckeditor/tables/EditorTableView.java index baea4f2d9f0..a9b69b10bcf 100644 --- a/src/main/java/forge/gui/deckeditor/tables/EditorTableView.java +++ b/src/main/java/forge/gui/deckeditor/tables/EditorTableView.java @@ -22,11 +22,14 @@ import java.awt.Component; import java.awt.Point; import java.awt.event.MouseEvent; import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import java.util.Map; import java.util.Map.Entry; import javax.swing.JComponent; import javax.swing.JTable; +import javax.swing.SwingUtilities; import javax.swing.event.TableModelEvent; import javax.swing.event.TableModelListener; import javax.swing.table.DefaultTableColumnModel; @@ -243,13 +246,21 @@ public final class EditorTableView { */ public void fixSelection(final int rowLastSelected) { // 3 cases: 0 cards left, select the same row, select prev row - int newRow = rowLastSelected; final int cntRowsAbove = this.model.getRowCount(); - if (cntRowsAbove != 0) { + if (0 <= rowLastSelected && cntRowsAbove != 0) { + int newRow = rowLastSelected; if (cntRowsAbove == newRow) { + // move selection away from the last, already missing, option newRow--; - } // move selection away from the last, already missing, option - this.table.setRowSelectionInterval(newRow, newRow); + } + final int selectRow = newRow; + + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + table.setRowSelectionInterval(selectRow, selectRow); + } + }); } } @@ -346,12 +357,37 @@ public final class EditorTableView { * an InventoryItem */ public void addCard(final T card) { - // int n = table.getSelectedRow(); + final int n = this.table.getSelectedRow(); this.pool.add(card); if (this.isUnfiltered()) { - this.model.addCard(card); + this.model.addCard(card, 1); } this.updateView(false); + this.fixSelection(n); + } + + public void addCards(Iterable> cardsToAdd) { + final int n = this.table.getSelectedRow(); + for (Map.Entry item : cardsToAdd) { + this.pool.add(item.getKey(), item.getValue()); + if (this.isUnfiltered()) { + this.model.addCard(item.getKey(), item.getValue()); + } + } + this.updateView(false); + this.fixSelection(n); + } + + public void addCards(Collection cardsToAdd) { + final int n = this.table.getSelectedRow(); + for (T item : cardsToAdd) { + this.pool.add(item, 1); + if (this.isUnfiltered()) { + this.model.addCard(item, 1); + } + } + this.updateView(false); + this.fixSelection(n); } /** @@ -365,7 +401,19 @@ public final class EditorTableView { final int n = this.table.getSelectedRow(); this.pool.remove(card); if (this.isUnfiltered()) { - this.model.removeCard(card); + this.model.removeCard(card, 1); + } + this.updateView(false); + this.fixSelection(n); + } + + public void removeCards(List> cardsToRemove) { + final int n = this.table.getSelectedRow(); + for (Map.Entry item : cardsToRemove) { + this.pool.remove(item.getKey(), item.getValue()); + if (this.isUnfiltered()) { + this.model.removeCard(item.getKey(), item.getValue()); + } } this.updateView(false); this.fixSelection(n); diff --git a/src/main/java/forge/gui/deckeditor/views/VCurrentDeck.java b/src/main/java/forge/gui/deckeditor/views/VCurrentDeck.java index 7334516d9c2..b907dabb2a3 100644 --- a/src/main/java/forge/gui/deckeditor/views/VCurrentDeck.java +++ b/src/main/java/forge/gui/deckeditor/views/VCurrentDeck.java @@ -80,14 +80,14 @@ public enum VCurrentDeck implements IVDoc, ITableContainer { private final JPanel pnlRemoveButtons = new JPanel(new MigLayout("insets 0, gap 0, ax center, hidemode 3")); - private final JLabel btnRemove = new FLabel.Builder() + private final FLabel btnRemove = new FLabel.Builder() .fontSize(14) .text("Remove card") .tooltip("Remove selected card to current deck (or double click the row)") .icon(FSkin.getIcon(FSkin.InterfaceIcons.ICO_MINUS)) .iconScaleAuto(false).hoverable(true).build(); - private final JLabel btnRemove4 = new FLabel.Builder() + private final FLabel btnRemove4 = new FLabel.Builder() .fontSize(14) .text("Remove 4 of card") .tooltip("Remove up to 4 of selected card to current deck") @@ -236,12 +236,12 @@ public enum VCurrentDeck implements IVDoc, ITableContainer { //========== Retrieval /** @return {@link javax.swing.JLabel} */ - public JLabel getBtnRemove() { + public FLabel getBtnRemove() { return btnRemove; } /** @return {@link javax.swing.JLabel} */ - public JLabel getBtnRemove4() { + public FLabel getBtnRemove4() { return btnRemove4; } diff --git a/src/main/java/forge/gui/match/QuestWinLose.java b/src/main/java/forge/gui/match/QuestWinLose.java index 4da70e81a5b..8e1a6ef3ed7 100644 --- a/src/main/java/forge/gui/match/QuestWinLose.java +++ b/src/main/java/forge/gui/match/QuestWinLose.java @@ -144,7 +144,7 @@ public class QuestWinLose extends ControlWinLose { this.anteWon(anteCards); } else { for (CardPrinted c : anteCards) { - qc.getCards().loseCard(c); + qc.getCards().loseCard(c, 1); } this.anteLost(anteCards); } diff --git a/src/main/java/forge/item/OpenablePack.java b/src/main/java/forge/item/OpenablePack.java index 2eccafbf15c..53b13ec1f40 100644 --- a/src/main/java/forge/item/OpenablePack.java +++ b/src/main/java/forge/item/OpenablePack.java @@ -26,6 +26,7 @@ import com.google.common.collect.Iterables; import forge.card.BoosterData; import forge.card.BoosterGenerator; import forge.card.CardRulesPredicates; +import forge.deck.Deck; import forge.util.Aggregates; /** @@ -80,7 +81,7 @@ public abstract class OpenablePack implements InventoryItemFromSet { } return this.cards; } - + /** * Gets the total cards. * diff --git a/src/main/java/forge/quest/QuestUtilCards.java b/src/main/java/forge/quest/QuestUtilCards.java index ebded9fca9b..05d50708c18 100644 --- a/src/main/java/forge/quest/QuestUtilCards.java +++ b/src/main/java/forge/quest/QuestUtilCards.java @@ -17,19 +17,6 @@ */ package forge.quest; -import forge.Singletons; -import forge.card.BoosterGenerator; -import forge.card.CardEdition; -import forge.card.FormatCollection; -import forge.deck.Deck; -import forge.quest.data.GameFormatQuest; -import forge.item.*; -import forge.quest.bazaar.QuestItemType; -import forge.quest.data.QuestAssets; -import forge.quest.data.QuestPreferences; -import forge.quest.data.QuestPreferences.QPref; -import forge.util.Aggregates; -import forge.util.MyRandom; import java.util.ArrayList; import java.util.List; import java.util.Map.Entry; @@ -39,6 +26,29 @@ import com.google.common.base.Predicate; import com.google.common.base.Predicates; import com.google.common.collect.Iterables; +import forge.Singletons; +import forge.card.BoosterGenerator; +import forge.card.CardEdition; +import forge.card.FormatCollection; +import forge.deck.Deck; +import forge.item.BoosterPack; +import forge.item.CardDb; +import forge.item.CardPrinted; +import forge.item.FatPack; +import forge.item.InventoryItem; +import forge.item.ItemPool; +import forge.item.ItemPoolView; +import forge.item.OpenablePack; +import forge.item.PreconDeck; +import forge.item.TournamentPack; +import forge.quest.bazaar.QuestItemType; +import forge.quest.data.GameFormatQuest; +import forge.quest.data.QuestAssets; +import forge.quest.data.QuestPreferences; +import forge.quest.data.QuestPreferences.QPref; +import forge.util.Aggregates; +import forge.util.MyRandom; + /** * This is a helper class to execute operations on QuestData. It has been * created to decrease complexity of questData class @@ -305,12 +315,12 @@ public final class QuestUtilCards { * @param price * the price */ - public void sellCard(final CardPrinted card, final int price) { - this.sellCard(card, price, true); + public void sellCard(final CardPrinted card, int qty, final int pricePerCard) { + this.sellCard(card, qty, pricePerCard, true); } - public void loseCard(final CardPrinted card) { - this.sellCard(card, 0, false); + public void loseCard(final CardPrinted card, int qty) { + this.sellCard(card, qty, 0, false); } /** @@ -323,13 +333,13 @@ public final class QuestUtilCards { * @param addToShop * true if this card should be added to the shop, false otherwise */ - private void sellCard(final CardPrinted card, final int price, final boolean addToShop) { - if (price > 0) { - this.qa.setCredits(this.qa.getCredits() + price); + private void sellCard(final CardPrinted card, int qty, final int pricePerCard, final boolean addToShop) { + if (pricePerCard > 0) { + this.qa.setCredits(this.qa.getCredits() + (qty * pricePerCard)); } - this.qa.getCardPool().remove(card); + this.qa.getCardPool().remove(card, qty); if (addToShop) { - this.qa.getShopList().add(card); + this.qa.getShopList().add(card, qty); } // remove card being sold from all decks @@ -623,7 +633,13 @@ public final class QuestUtilCards { @Override public Comparable apply(final Entry from) { InventoryItem i = from.getKey(); - return i instanceof CardPrinted ? QuestUtilCards.this.qa.getCardPool().count((CardPrinted)i) : null; + if (i instanceof CardPrinted) { + return QuestUtilCards.this.qa.getCardPool().count((CardPrinted)i); + } else if (i instanceof PreconDeck) { + PreconDeck pDeck = (PreconDeck)i; + return Singletons.getModel().getQuest().getMyDecks().contains(pDeck.getName()) ? 1 : 0; + } + return null; } }; @@ -632,7 +648,13 @@ public final class QuestUtilCards { @Override public Object apply(final Entry from) { InventoryItem i = from.getKey(); - return i instanceof CardPrinted ? QuestUtilCards.this.qa.getCardPool().count((CardPrinted)i) : null; + if (i instanceof CardPrinted) { + return QuestUtilCards.this.qa.getCardPool().count((CardPrinted)i); + } else if (i instanceof PreconDeck) { + PreconDeck pDeck = (PreconDeck)i; + return Singletons.getModel().getQuest().getMyDecks().contains(pDeck.getName()) ? "YES" : "NO"; + } + return null; } }; }