diff --git a/src/main/java/forge/card/EditionCollection.java b/src/main/java/forge/card/EditionCollection.java index 6d42834ba47..a8505d9bd17 100644 --- a/src/main/java/forge/card/EditionCollection.java +++ b/src/main/java/forge/card/EditionCollection.java @@ -20,6 +20,8 @@ package forge.card; import java.util.Map; import java.util.TreeMap; +import com.google.common.base.Function; + import forge.util.StorageView; public final class EditionCollection extends StorageView { @@ -78,5 +80,12 @@ public final class EditionCollection extends StorageView { final CardEdition set = this.get(code); return set == null ? "" : set.getCode2(); } + + public final Function FN_EDITION_BY_CODE = new Function() { + @Override + public CardEdition apply(String code) { + return EditionCollection.this.get(code); + }; + }; } diff --git a/src/main/java/forge/gui/home/quest/CSubmenuChallenges.java b/src/main/java/forge/gui/home/quest/CSubmenuChallenges.java index 0c23a053da4..bdf4a25ee9f 100644 --- a/src/main/java/forge/gui/home/quest/CSubmenuChallenges.java +++ b/src/main/java/forge/gui/home/quest/CSubmenuChallenges.java @@ -48,7 +48,7 @@ public enum CSubmenuChallenges implements ICDoc { view.getBtnUnlock().setCommand( new Command() { @Override - public void execute() { SSubmenuQuestUtil.showSetUnlock(); } }); + public void execute() { SSubmenuQuestUtil.chooseAndUnlockEdition(); CSubmenuChallenges.this.update(); } }); view.getBtnStart().addActionListener( new ActionListener() { @Override diff --git a/src/main/java/forge/gui/home/quest/CSubmenuDuels.java b/src/main/java/forge/gui/home/quest/CSubmenuDuels.java index a108bcb7e56..a0e41e29c85 100644 --- a/src/main/java/forge/gui/home/quest/CSubmenuDuels.java +++ b/src/main/java/forge/gui/home/quest/CSubmenuDuels.java @@ -43,7 +43,7 @@ public enum CSubmenuDuels implements ICDoc { view.getBtnUnlock().setCommand( new Command() { @Override - public void execute() { SSubmenuQuestUtil.showSetUnlock(); } }); + public void execute() { SSubmenuQuestUtil.chooseAndUnlockEdition(); CSubmenuDuels.this.update();} }); view.getBtnStart().addActionListener( new ActionListener() { @Override diff --git a/src/main/java/forge/gui/home/quest/CSubmenuQuestData.java b/src/main/java/forge/gui/home/quest/CSubmenuQuestData.java index 52f315f2067..1328b92175d 100644 --- a/src/main/java/forge/gui/home/quest/CSubmenuQuestData.java +++ b/src/main/java/forge/gui/home/quest/CSubmenuQuestData.java @@ -9,6 +9,9 @@ import java.util.Map; import java.util.Map.Entry; import javax.swing.JOptionPane; + +import com.sun.mail.iap.Argument; + import forge.Command; import forge.deck.Deck; import forge.Singletons; @@ -19,6 +22,7 @@ import forge.properties.ForgeProps; import forge.properties.NewConstants; import forge.quest.QuestController; import forge.quest.QuestMode; +import forge.quest.StartingPoolType; import forge.quest.data.GameFormatQuest; import forge.quest.data.QuestData; import forge.quest.data.QuestPreferences.QPref; @@ -181,23 +185,8 @@ public enum CSubmenuQuestData implements ICDoc { } GameFormat fmtPrizes = null; - switch(view.getPrizedPoolType()) { - case Complete: - fmtPrizes = null; - break; - case CustomFormat: - if ( customPrizeFormatCodes.isEmpty() ) - { - int answer = JOptionPane.showConfirmDialog(null, "You have defined custom format as containing no sets.\nThis will choose all editions without restriction as prized.\n\nContinue?"); - if ( JOptionPane.YES_OPTION != answer ) - return; - } - fmtPrizes = customPrizeFormatCodes.isEmpty() ? null : new GameFormat("Custom Prizes", customPrizeFormatCodes, null); // chosen sets and no banend cards - break; - case Rotating: - fmtPrizes = view.getPrizedRotatingFormat(); - break; - default: // same as starting + StartingPoolType prizedPoolType = view.getPrizedPoolType(); + if ( null == prizedPoolType ) { fmtPrizes = fmtStartPool; if ( null == fmtPrizes && dckStartPool != null) { // build it form deck List sets = new ArrayList(); @@ -213,10 +202,27 @@ public enum CSubmenuQuestData implements ICDoc { } fmtPrizes = new GameFormat("From deck", sets, null); } - break; - - } - + } else + switch(prizedPoolType) { + case Complete: + fmtPrizes = null; + break; + case CustomFormat: + if ( customPrizeFormatCodes.isEmpty() ) + { + int answer = JOptionPane.showConfirmDialog(null, "You have defined custom format as containing no sets.\nThis will choose all editions without restriction as prized.\n\nContinue?"); + if ( JOptionPane.YES_OPTION != answer ) + return; + } + fmtPrizes = customPrizeFormatCodes.isEmpty() ? null : new GameFormat("Custom Prizes", customPrizeFormatCodes, null); // chosen sets and no banend cards + break; + case Rotating: + fmtPrizes = view.getPrizedRotatingFormat(); + break; + default: + throw new RuntimeException("Should not get this result"); + } + final Object o = JOptionPane.showInputDialog(null, "Poets will remember your quest as:", "Quest Name", JOptionPane.OK_CANCEL_OPTION); if (o == null) { return; } diff --git a/src/main/java/forge/gui/home/quest/SSubmenuQuestUtil.java b/src/main/java/forge/gui/home/quest/SSubmenuQuestUtil.java index dd3e5d228e1..acdbab2b8c3 100644 --- a/src/main/java/forge/gui/home/quest/SSubmenuQuestUtil.java +++ b/src/main/java/forge/gui/home/quest/SSubmenuQuestUtil.java @@ -8,6 +8,8 @@ import javax.swing.JOptionPane; import javax.swing.SwingUtilities; import javax.swing.SwingWorker; +import org.apache.commons.lang3.tuple.ImmutablePair; + import com.google.common.base.Supplier; import forge.Card; @@ -160,11 +162,8 @@ public class SSubmenuQuestUtil { view0.getLblLosses().setText("Losses: " + qA.getLost()); // Show or hide the set unlocking button - if (qCtrl.getFormatNumberUnlockable() > 0) { - view0.getBtnUnlock().setVisible(true); - } else { - view0.getBtnUnlock().setVisible(false); - } + + view0.getBtnUnlock().setVisible(qCtrl.getUnlocksTokens() > 0); // Challenge in wins final int num = SSubmenuQuestUtil.nextChallengeInWins(); @@ -245,12 +244,20 @@ public class SSubmenuQuestUtil { } /** */ - public static void showSetUnlock() { + public static void chooseAndUnlockEdition() { final QuestController qData = Singletons.getModel().getQuest(); - CardEdition toUnlock = QuestUtilUnlockSets.unlockSet(qData, false, null); - if (toUnlock != null) { - QuestUtilUnlockSets.doUnlock(qData, toUnlock); + ImmutablePair toUnlock = QuestUtilUnlockSets.chooseSetToUnlock(qData, false, null); + if (toUnlock == null) { + return; } + + CardEdition unlocked = toUnlock.left; + qData.getAssets().subtractCredits(toUnlock.right); + JOptionPane.showMessageDialog(null, "You have successfully unlocked " + unlocked.getName() + "!", + unlocked.getName() + " unlocked!", + JOptionPane.PLAIN_MESSAGE); + + QuestUtilUnlockSets.doUnlock(qData, unlocked); } /** */ diff --git a/src/main/java/forge/quest/QuestController.java b/src/main/java/forge/quest/QuestController.java index e16d68e8dcd..f170d69500e 100644 --- a/src/main/java/forge/quest/QuestController.java +++ b/src/main/java/forge/quest/QuestController.java @@ -338,24 +338,18 @@ public class QuestController { * Quest format has unlockable sets available at the moment. * @return int number of unlockable sets. */ - public int getFormatNumberUnlockable() { - if (this.questFormat == null) { + public int getUnlocksTokens() { + if (this.questFormat == null || !this.questFormat.canUnlockSets()) { return 0; } final int wins = this.model.getAchievements().getWin(); - if (wins < 10) { - return 0; - } - int toUnlock = this.questFormat.getExcludedSetCodes().size(); - if (toUnlock > 1 + wins / 50) { - toUnlock = 1 + wins / 50; - } - if (toUnlock > 8) { - toUnlock = 8; - } - return toUnlock; + int cntLocked = this.questFormat.getLockedSets().size(); + int unlocksAvaliable = wins / 20; + int unlocksSpent = this.questFormat.getUnlocksUsed(); + + return unlocksAvaliable > unlocksSpent ? Math.min(unlocksAvaliable - unlocksSpent, cntLocked) : 0; } } diff --git a/src/main/java/forge/quest/QuestUtilUnlockSets.java b/src/main/java/forge/quest/QuestUtilUnlockSets.java index f3409d2b98c..46e1f51e7e0 100644 --- a/src/main/java/forge/quest/QuestUtilUnlockSets.java +++ b/src/main/java/forge/quest/QuestUtilUnlockSets.java @@ -19,12 +19,15 @@ package forge.quest; import java.util.ArrayList; import java.util.Collections; +import java.util.Comparator; import java.util.List; import java.util.Map; -import java.util.TreeMap; - import javax.swing.JOptionPane; +import org.apache.commons.lang3.tuple.ImmutablePair; +import com.google.common.collect.Iterables; +import com.google.common.collect.Lists; + import forge.Singletons; import forge.card.BoosterData; import forge.card.CardEdition; @@ -33,6 +36,7 @@ import forge.gui.CardListViewer; import forge.gui.GuiChoose; import forge.item.CardPrinted; import forge.quest.io.ReadPriceList; +import forge.util.IStorageView; /** * This is a helper class for unlocking new sets during a format-limited @@ -48,78 +52,61 @@ public class QuestUtilUnlockSets { * @param presetChoices List a pregenerated list of options, NOT IMPLEMENTED YET * @return CardEdition, the unlocked edition if any. */ - public static CardEdition unlockSet(final QuestController qData, final boolean freeUnlock, + public static ImmutablePair chooseSetToUnlock(final QuestController qData, final boolean freeUnlock, List presetChoices) { - if (qData.getFormat() == null || qData.getFormat().getExcludedSetCodes().isEmpty()) { - return null; - } - List choices = unlockableSets(qData); - - if (choices == null || choices.size() < 1) { + if (qData.getFormat() == null || !qData.getFormat().canUnlockSets()) { return null; } final ReadPriceList prices = new ReadPriceList(); final Map mapPrices = prices.getPriceList(); + final List> setPrices = new ArrayList>(); - List unlockPrices = new ArrayList(); - for (int i = 0; i < choices.size(); i++) { - if (mapPrices.containsKey(choices.get(i).getName() + " Booster Pack")) { - long newPrice = new Double(60 * Math.pow(Math.sqrt(mapPrices.get(choices.get(i).getName() + " Booster Pack")), 1.65)).longValue(); - if (newPrice < 7500) { newPrice = 7500; } - unlockPrices.add(newPrice); - } - else { - unlockPrices.add((long) 7500); + for (CardEdition ed : getUnlockableEditions(qData)) { + int price = 7500; + if (mapPrices.containsKey(ed.getName() + " Booster Pack")) { + price = Math.max( 50 * mapPrices.get(ed.getName() + " Booster Pack"), 7500 ); } + setPrices.add(ImmutablePair.of(ed, price)); } final String setPrompt = "You have " + qData.getAssets().getCredits() + " credits. Unlock:"; List options = new ArrayList(); - for (int i = 0; i < choices.size(); i++) { - options.add(choices.get(i).getName() + " [PRICE: " + unlockPrices.get(i) + " credits]"); + for (ImmutablePair ee : setPrices) { + options.add(String.format("%s [PRICE: %d credits]", ee.left.getName(), ee.right)); } - final String choice = GuiChoose.oneOrNone(setPrompt, options); - CardEdition chooseEd = null; - long price = 0; - if (choice == null) { + int index = options.indexOf(GuiChoose.oneOrNone(setPrompt, options)); + if (index < 0 || index >= options.size()) { return null; } - /* Examine choice */ - for (int i = 0; i < options.size(); i++) { - if (choice.equals(options.get(i))) { - chooseEd = choices.get(i); - price = unlockPrices.get(i); - break; - } - } + + ImmutablePair toBuy = setPrices.get(index); + + int price = toBuy.right; + CardEdition choosenEdition = toBuy.left; + if (qData.getAssets().getCredits() < price) { JOptionPane.showMessageDialog(null, "Unfortunately, you cannot afford that set yet.\n" - + "To unlock " + chooseEd.getName() + ", you need " + price + " credits.\n" + + "To unlock " + choosenEdition.getName() + ", you need " + price + " credits.\n" + "You have only " + qData.getAssets().getCredits() + " credits.", - "Failed to unlock " + chooseEd.getName(), + "Failed to unlock " + choosenEdition.getName(), JOptionPane.PLAIN_MESSAGE); return null; } final int unlockConfirm = JOptionPane.showConfirmDialog(null, - "Unlocking " + chooseEd.getName() + " will cost you " + price + " credits.\n" + "Unlocking " + choosenEdition.getName() + " will cost you " + price + " credits.\n" + "You have " + qData.getAssets().getCredits() + " credits.\n\n" - + "Are you sure you want to unlock " + chooseEd.getName() + "?", - "Confirm Unlocking " + chooseEd.getName(), JOptionPane.YES_NO_OPTION); + + "Are you sure you want to unlock " + choosenEdition.getName() + "?", + "Confirm Unlocking " + choosenEdition.getName(), JOptionPane.YES_NO_OPTION); if (unlockConfirm == JOptionPane.NO_OPTION) { return null; } - - qData.getAssets().subtractCredits(price); - JOptionPane.showMessageDialog(null, "You have successfully unlocked " + chooseEd.getName() + "!", - chooseEd.getName() + " unlocked!", - JOptionPane.PLAIN_MESSAGE); - return chooseEd; + return toBuy; } /** @@ -127,110 +114,53 @@ public class QuestUtilUnlockSets { * * @return unmodifiable list, assorted sets that are not currently in the format. */ - private static List unlockableSets(final QuestController qData) { - if (qData.getFormat() == null || qData.getFormat().getExcludedSetCodes().isEmpty()) { - return null; + private static final List emptyEditions = Collections.unmodifiableList(new ArrayList()); + private static List getUnlockableEditions(final QuestController qData) { + if (qData.getFormat() == null || !qData.getFormat().canUnlockSets()) { + return emptyEditions; } - final int nrChoices = qData.getFormatNumberUnlockable(); + final int nrChoices = qData.getUnlocksTokens(); if (nrChoices < 1) { // Should never happen if we made it this far but better safe than sorry... throw new RuntimeException("BUG? Could not find unlockable sets even though we should."); } List options = new ArrayList(); // Sort current sets by index - TreeMap sortedFormat = new TreeMap(); - for (String edCode : qData.getFormat().getAllowedSetCodes()) { - sortedFormat.put(new Integer(Singletons.getModel().getEditions().get(edCode).getIndex()), Singletons.getModel().getEditions().get(edCode)); - } - List currentSets = new ArrayList(sortedFormat.values()); - + List allowedSets = Lists.newArrayList(Iterables.transform(qData.getFormat().getAllowedSetCodes(), Singletons.getModel().getEditions().FN_EDITION_BY_CODE)); + Collections.sort(allowedSets); + // Sort unlockable sets by index - TreeMap sortedExcluded = new TreeMap(); - for (String edCode : qData.getFormat().getExcludedSetCodes()) { - sortedExcluded.put(new Integer(Singletons.getModel().getEditions().get(edCode).getIndex()), Singletons.getModel().getEditions().get(edCode)); - } - List excludedSets = new ArrayList(sortedExcluded.values()); - - // Collect 'previous' and 'next' editions - CardEdition first = currentSets.get(0); - CardEdition last = currentSets.get(currentSets.size() - 1); - List fillers = new ArrayList(); - - // Add nearby sets first - for (CardEdition ce : excludedSets) { - if (first.getIndex() == ce.getIndex() + 1 || last.getIndex() + 1 == ce.getIndex()) - { - options.add(ce); - // System.out.println("Added adjacent set: " + ce.getName()); + List excludedSets = Lists.newArrayList(Iterables.transform(qData.getFormat().getLockedSets(), Singletons.getModel().getEditions().FN_EDITION_BY_CODE)); + Collections.sort(excludedSets); + + // get a number of sets between an excluded and any included set + List> excludedWithDistances = new ArrayList>(); + for(CardEdition ex : excludedSets) { + int distance = Integer.MAX_VALUE; + for(CardEdition in : allowedSets) { + int d = Math.abs(ex.getIndex() - in.getIndex()); + if ( d < distance ) + distance = d; } + excludedWithDistances.add(ImmutablePair.of(ex, distance)); } - // Fill in the in-between sets - int j = 0; - // Find the first excluded set between current sets first and current sets last - while (j < excludedSets.size() && excludedSets.get(j).getIndex() < currentSets.get(0).getIndex()) { - j++; - } - // Consider all sets until current sets last - while (j < excludedSets.size() && excludedSets.get(j).getIndex() < currentSets.get(currentSets.size() - 1).getIndex()) { - if (!options.contains(excludedSets.get(j)) && !fillers.contains(excludedSets.get(j))) { - // System.out.println("Added in-between set " + excludedSets.get(j).getCode()); - fillers.add(excludedSets.get(j)); - } - j++; - } - // Add more nearby sets - for (CardEdition ce : excludedSets) { - if (first.getIndex() == ce.getIndex() + 2 || last.getIndex() + 2 == ce.getIndex()) - { - if (!fillers.contains(ce) && !options.contains(ce)) { - fillers.add(ce); - // System.out.println("Added adjacent filler set: " + ce.getName()); - } - } - } + // sort by distance, then by code desc + Collections.sort(excludedWithDistances, new Comparator>() { + @Override + public int compare(ImmutablePair o1, ImmutablePair o2) { + int d1 = o2.right - o1.right; + return d1 != 0 ? d1 : o1.left.getIndex() - o2.left.getIndex(); + } + }); - // Look for nearby core sets or block starting sets... - for (BoosterData bd : Singletons.getModel().getTournamentPacks()) { - if (qData.getFormat().getExcludedSetCodes().contains(bd.getEdition()) - && !(fillers.contains(Singletons.getModel().getEditions().get(bd.getEdition()))) - && !(options.contains(Singletons.getModel().getEditions().get(bd.getEdition())))) { - // Set is not yet on any of the lists, see if it is 'close' to any of the sets we currently have - CardEdition curEd = Singletons.getModel().getEditions().get(bd.getEdition()); - int edIdx = curEd.getIndex(); - for (String cmpCode : qData.getFormat().getAllowedSetCodes()) { - int cmpIdx = Singletons.getModel().getEditions().get(cmpCode).getIndex(); - // Note that we need to check for fillers.contains() again inside this 'for' loop! - if (!fillers.contains(curEd) && (cmpIdx == edIdx + 1 || edIdx == cmpIdx + 1)) { - fillers.add(curEd); - // System.out.println("Added nearby starter/core set " + curEd.getName()); - } - else if (!fillers.contains(curEd) && (cmpIdx == edIdx + 2 || edIdx == cmpIdx + 2)) { - fillers.add(curEd); - //System.out.println("Added nearby2 starter/core set " + curEd.getName()); - } - } - } - } - // Add padding if necessary - if (fillers.size() + options.size() < nrChoices && excludedSets.size() > fillers.size() + options.size()) { - // Pad in order. - for (CardEdition ce : excludedSets) { - if (!fillers.contains(ce) && !options.contains(ce)) { - fillers.add(ce); - if (fillers.size() + options.size() >= nrChoices) { - break; - } - } - } - } - - for (int i = 0; (options.size() < nrChoices) && i < fillers.size(); i++) { - options.add(fillers.get(i)); + for (ImmutablePair set : excludedWithDistances) { + options.add(set.left); // System.out.println("Padded with: " + fillers.get(i).getName()); } + Collections.reverse(options); return Collections.unmodifiableList(options); } @@ -243,30 +173,26 @@ public class QuestUtilUnlockSets { */ public static void doUnlock(QuestController qData, final CardEdition unlockedSet) { + IStorageView starters = Singletons.getModel().getTournamentPacks(); + IStorageView boosters = Singletons.getModel().getBoosters(); qData.getFormat().unlockSet(unlockedSet.getCode()); - List displayCards = new ArrayList(); + List cardsWon = new ArrayList(); - if (Singletons.getModel().getTournamentPacks().contains(unlockedSet.getCode())) { - final List cardsWon = (new UnOpenedProduct(Singletons.getModel().getTournamentPacks().get(unlockedSet.getCode()))).open(); - - qData.getCards().addAllCards(cardsWon); - displayCards.addAll(cardsWon); + if (starters.contains(unlockedSet.getCode())) { + UnOpenedProduct starter = new UnOpenedProduct(starters.get(unlockedSet.getCode())); + cardsWon.addAll(starter.open()); } - else if (Singletons.getModel().getBoosters().contains(unlockedSet.getCode())) { - for (int i = 0; i < 3; i++) { - final List cardsWon = (new UnOpenedProduct(Singletons.getModel().getBoosters().get(unlockedSet.getCode()))).open(); - - qData.getCards().addAllCards(cardsWon); - displayCards.addAll(cardsWon); - } + else if (boosters.contains(unlockedSet.getCode())) { + UnOpenedProduct booster = new UnOpenedProduct(boosters.get(unlockedSet.getCode())); + cardsWon.addAll(booster.open()); + cardsWon.addAll(booster.open()); + cardsWon.addAll(booster.open()); } - final CardListViewer cardView = new CardListViewer(unlockedSet.getName(), - "You get the following bonus cards:", displayCards); + qData.getCards().addAllCards(cardsWon); + final CardListViewer cardView = new CardListViewer(unlockedSet.getName(), "You get the following bonus cards:", cardsWon); cardView.show(); - qData.save(); - } } diff --git a/src/main/java/forge/quest/data/GameFormatQuest.java b/src/main/java/forge/quest/data/GameFormatQuest.java index c27746a4e5d..b4679d92d22 100644 --- a/src/main/java/forge/quest/data/GameFormatQuest.java +++ b/src/main/java/forge/quest/data/GameFormatQuest.java @@ -17,7 +17,6 @@ */ package forge.quest.data; -import java.util.Collections; import java.util.List; import java.util.ArrayList; @@ -36,7 +35,8 @@ import forge.game.GameFormat; */ public final class GameFormatQuest extends GameFormat { - private boolean allowUnlocks = true; + private final boolean allowUnlocks; + private int unlocksUsed = 0; /** * Instantiates a new game format based on two lists. @@ -50,6 +50,7 @@ public final class GameFormatQuest extends GameFormat { */ public GameFormatQuest(final String newName, final List setsToAllow, final List cardsToBan) { super(newName, setsToAllow, cardsToBan); + allowUnlocks = false; } public GameFormatQuest(final String newName, final List setsToAllow, final List cardsToBan, boolean allowSetUnlocks) { @@ -64,50 +65,29 @@ public final class GameFormatQuest extends GameFormat { * @param allowSetUnlocks */ public GameFormatQuest(final GameFormat toCopy, boolean allowSetUnlocks) { - this(toCopy.getName(), toCopy.getAllowedSetCodes(), toCopy.getBannedCardNames()); + super(toCopy.getName(), toCopy.getAllowedSetCodes(), toCopy.getBannedCardNames()); allowUnlocks = allowSetUnlocks; } - /** - * - * Updates the filters based on the current list data. - */ - public void updateFilters() { - // nothing to do here. - // predicates hold references to lists and thus get auto updated. - - // remove this method after reading. - } - - /** - * Empty the whole list. - */ - public void emptyAllowedSets() { - if (allowedSetCodes.isEmpty()) { - return; - } - allowedSetCodes.clear(); - } - /** * Get the list of excluded sets. * * @return unmodifiable list of excluded sets. */ - public List getExcludedSetCodes() { - if (this.allowedSetCodes.isEmpty()) { - return null; - } + public List getLockedSets() { List exSets = new ArrayList(); + if (this.allowedSetCodes.isEmpty()) { + return exSets; + } for (CardEdition ce : Singletons.getModel().getEditions()) { if (!isSetLegal(ce.getCode())) { exSets.add(ce.getCode()); } } - return Collections.unmodifiableList(exSets); + return exSets; } /** @@ -116,13 +96,11 @@ public final class GameFormatQuest extends GameFormat { * @param setCode String, set code. */ public void unlockSet(final String setCode) { - if (this.allowedSetCodes.isEmpty()) { - return; // We are already allowing all sets! - } else if (this.allowedSetCodes.contains(setCode)) { - return; // Already on the list + if (!canUnlockSets() || this.allowedSetCodes.isEmpty() || this.allowedSetCodes.contains(setCode)) { + return; } this.allowedSetCodes.add(setCode); - updateFilters(); + unlocksUsed++; } /** @@ -138,6 +116,11 @@ public final class GameFormatQuest extends GameFormat { return allowUnlocks; } + public int getUnlocksUsed() { + return unlocksUsed; + } + + /** * The Class Predicates. */ diff --git a/src/main/java/forge/quest/io/QuestDataIO.java b/src/main/java/forge/quest/io/QuestDataIO.java index a40dc0ba6a1..d1fb7d1ccf6 100644 --- a/src/main/java/forge/quest/io/QuestDataIO.java +++ b/src/main/java/forge/quest/io/QuestDataIO.java @@ -391,6 +391,8 @@ public class QuestDataIO { writer.startNode("format"); GameFormatQuest format = (GameFormatQuest) source; writer.addAttribute("name", format.getName()); + writer.addAttribute("unlocksUsed", Integer.toString(format.getUnlocksUsed())); + writer.addAttribute("canUnlock", format.canUnlockSets() ? "1" : "0"); writer.endNode(); for (String set : format.getAllowedSetCodes()) { @@ -409,6 +411,8 @@ public class QuestDataIO { public Object unmarshal(final HierarchicalStreamReader reader, final UnmarshallingContext context) { reader.moveDown(); String name = reader.getAttribute("name"); + String unlocksUsed = reader.getAttribute("unlocksUsed"); + boolean canUnlock = !("0".equals(reader.getAttribute("canUnlock"))); List allowedSets = new ArrayList(); List bannedCards = new ArrayList(); reader.moveUp(); @@ -425,7 +429,21 @@ public class QuestDataIO { } reader.moveUp(); } - return new GameFormatQuest(name, allowedSets, bannedCards); + GameFormatQuest res = new GameFormatQuest(name, allowedSets, bannedCards); + try { + if ( StringUtils.isNotEmpty(unlocksUsed)) { + setFinalField(GameFormatQuest.class, "unlocksUsed", res, Integer.parseInt(unlocksUsed)); + } + setFinalField(GameFormatQuest.class, "allowUnlocks", res, canUnlock); + } catch (NumberFormatException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } + + return res; } } @@ -444,12 +462,7 @@ public class QuestDataIO { @Override public Object unmarshal(final HierarchicalStreamReader reader, final UnmarshallingContext context) { final String value = reader.getValue(); - return GameType.smartValueOf(value, GameType.Quest); // does not - // matter - - // this field - // is - // deprecated - // anyway + return GameType.smartValueOf(value, GameType.Quest); } }