From 75bc19d4a739a0ba4a2c3108c11871b9f367c1b6 Mon Sep 17 00:00:00 2001 From: emerald000 Date: Tue, 5 May 2020 01:24:34 -0400 Subject: [PATCH] Add a button on the deck editor to change your cards to the oldest versions. --- .../java/mage/client/cards/DragCardGrid.java | 46 +++++++++++++++++++ .../mage/cards/repository/CardRepository.java | 32 +++++++++++++ 2 files changed, 78 insertions(+) diff --git a/Mage.Client/src/main/java/mage/client/cards/DragCardGrid.java b/Mage.Client/src/main/java/mage/client/cards/DragCardGrid.java index 9a7e144f47..8d231eb265 100644 --- a/Mage.Client/src/main/java/mage/client/cards/DragCardGrid.java +++ b/Mage.Client/src/main/java/mage/client/cards/DragCardGrid.java @@ -562,6 +562,7 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg JButton selectByButton; JButton analyseButton; JButton blingButton; + JButton oldVersionButton; // Popup for toolbar final JPopupMenu filterPopup; @@ -716,6 +717,7 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg selectByButton = new JButton("Select By"); analyseButton = new JButton("M"); // "Mana" button blingButton = new JButton("B"); // "Bling" button + oldVersionButton = new JButton("O"); // "Old version" button // Name and count label deckNameAndCountLabel = new JLabel(); @@ -740,6 +742,7 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg toolbarInner.add(visibilityButton); toolbarInner.add(analyseButton); toolbarInner.add(blingButton); + toolbarInner.add(oldVersionButton); toolbar.add(toolbarInner, BorderLayout.WEST); JPanel sliderPanel = new JPanel(new GridBagLayout()); sliderPanel.setOpaque(false); @@ -982,6 +985,11 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg blingButton.addActionListener(evt -> blingDeck()); + // Old version button - Switch cards to the oldest non-promo printing. In case of multiples in a set, take the lowest card number. + oldVersionButton.setToolTipText("Switch cards to the oldest non-promo printing"); + + oldVersionButton.addActionListener(evt -> oldVersionDeck()); + // Filter popup filterPopup = new JPopupMenu(); filterPopup.setPreferredSize(new Dimension(300, 300)); @@ -1567,6 +1575,44 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg } } + private void oldVersionDeck() { + if (this.mode != Constants.DeckEditorMode.FREE_BUILDING) { + return; + } + + if (JOptionPane.showConfirmDialog(null, "Are you sure you want to switch your card versions to the oldest ones?", "WARNING", + JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) { + return; + } + + List>> newCardGrid = new ArrayList<>(); + for (List> gridRow : cardGrid) { + List> newGridRow = new ArrayList<>(); + for (List stack : gridRow) { + List newStack = new ArrayList<>(); + for (CardView card : stack) { + CardInfo oldestCardInfo = CardRepository.instance.findOldestNonPromoVersionCard(card.getName()); + if (oldestCardInfo != null) { + CardView oldestCardView = new CardView(oldestCardInfo.getMockCard()); + this.removeCardView(card); + eventSource.fireEvent(card, ClientEventType.REMOVE_SPECIFIC_CARD); + this.addCardView(oldestCardView, false); + eventSource.fireEvent(oldestCardView, ClientEventType.ADD_SPECIFIC_CARD); + newStack.add(oldestCardView); + } else { + newStack.add(card); + } + } + newGridRow.add(newStack); + } + newCardGrid.add(newGridRow); + } + cardGrid = newCardGrid; + layoutGrid(); + cardScroll.revalidate(); + repaint(); + } + // Update the contents of the card grid public void setCards(CardsView cardsView, DeckCardLayout layout, BigCard bigCard) { if (bigCard != null) { diff --git a/Mage/src/main/java/mage/cards/repository/CardRepository.java b/Mage/src/main/java/mage/cards/repository/CardRepository.java index d77c31bd1a..c43e32dd27 100644 --- a/Mage/src/main/java/mage/cards/repository/CardRepository.java +++ b/Mage/src/main/java/mage/cards/repository/CardRepository.java @@ -12,6 +12,7 @@ import com.j256.ormlite.support.DatabaseConnection; import com.j256.ormlite.table.TableUtils; import mage.cards.CardSetInfo; import mage.constants.CardType; +import mage.constants.SetType; import mage.constants.SuperType; import mage.game.events.Listener; import mage.util.RandomUtil; @@ -485,6 +486,37 @@ public enum CardRepository { return Collections.emptyList(); } + public CardInfo findOldestNonPromoVersionCard(String name) { + List allVersions = this.findCards(name); + if (!allVersions.isEmpty()) { + allVersions.sort(new OldestNonPromoComparator()); + return allVersions.get(0); + } else { + return null; + } + } + + static class OldestNonPromoComparator implements Comparator { + @Override + public int compare(CardInfo a, CardInfo b) { + ExpansionInfo aSet = ExpansionRepository.instance.getSetByCode(a.getSetCode()); + ExpansionInfo bSet = ExpansionRepository.instance.getSetByCode(b.getSetCode()); + if (aSet.getType() == SetType.PROMOTIONAL && bSet.getType() != SetType.PROMOTIONAL) { + return 1; + } + if (bSet.getType() == SetType.PROMOTIONAL && aSet.getType() != SetType.PROMOTIONAL) { + return -1; + } + if (aSet.getReleaseDate().after(bSet.getReleaseDate())) { + return 1; + } + if (aSet.getReleaseDate().before(bSet.getReleaseDate())) { + return -1; + } + return Integer.compare(a.getCardNumberAsInt(), b.getCardNumberAsInt()); + } + } + public long getContentVersionFromDB() { try { ConnectionSource connectionSource = new JdbcConnectionSource(JDBC_URL);