mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-20 04:38:00 +00:00
Merge branch 'questMode_wildOpponents' into 'master'
Quest mode wild opponents See merge request core-developers/forge!3202
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package forge.screens.home.quest;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
import com.google.common.primitives.Ints;
|
||||
@@ -7,6 +9,7 @@ import com.google.common.primitives.Ints;
|
||||
import forge.gui.framework.ICDoc;
|
||||
import forge.model.FModel;
|
||||
import forge.quest.data.QuestPreferences;
|
||||
import forge.quest.data.QuestPreferences.QPref;
|
||||
import forge.screens.home.quest.VSubmenuQuestPrefs.PrefInput;
|
||||
import forge.util.Localizer;
|
||||
|
||||
@@ -50,16 +53,29 @@ public enum CSubmenuQuestPrefs implements ICDoc {
|
||||
public static void validateAndSave(final PrefInput i0) {
|
||||
if (i0.getText().equals(i0.getPreviousText())) { return; }
|
||||
final QuestPreferences prefs = FModel.getQuestPreferences();
|
||||
|
||||
final Integer val = Ints.tryParse(i0.getText());
|
||||
resetErrors();
|
||||
|
||||
String validationError = null;
|
||||
final Localizer localizer = Localizer.getInstance();
|
||||
final String validationError = val == null ? localizer.getMessage("lblEnteraNumber") : prefs.validatePreference(i0.getQPref(), val.intValue());
|
||||
resetErrors();
|
||||
|
||||
if(QPref.UNLOCK_DISTANCE_MULTIPLIER.equals(i0.getQPref())
|
||||
|| QPref.WILD_OPPONENTS_MULTIPLIER.equals(i0.getQPref())) {
|
||||
Double val = null;
|
||||
try {
|
||||
val = new Double(i0.getText());
|
||||
} catch (Exception e) {
|
||||
}
|
||||
validationError = val == null ? localizer.getMessage("lblEnteraDecimal") : null;
|
||||
} else {
|
||||
final Integer val = Ints.tryParse(i0.getText());
|
||||
validationError = val == null ? localizer.getMessage("lblEnteraNumber") : prefs.validatePreference(i0.getQPref(), val.intValue());
|
||||
}
|
||||
|
||||
if (validationError != null) {
|
||||
showError(i0, validationError);
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
prefs.setPref(i0.getQPref(), i0.getText());
|
||||
prefs.save();
|
||||
i0.setPreviousText(i0.getText());
|
||||
|
||||
@@ -292,6 +292,10 @@ public enum VSubmenuQuestPrefs implements IVSubmenu<CSubmenuQuestPrefs> {
|
||||
pnlDifficulty.add(new PrefInput(QPref.PENALTY_LOSS, QuestPreferencesErrType.DIFFICULTY), fieldConstraints + ", wrap");
|
||||
pnlDifficulty.add(new FLabel.Builder().text(localizer.getMessage("lblMoreDuelChoices")).fontAlign(SwingConstants.RIGHT).build(), labelConstraints);
|
||||
pnlDifficulty.add(new PrefInput(QPref.MORE_DUEL_CHOICES, QuestPreferencesErrType.DIFFICULTY), fieldConstraints + ", wrap");
|
||||
pnlDifficulty.add(new FLabel.Builder().text(localizer.getMessage("lblWildOpponentMultiplier")).fontAlign(SwingConstants.RIGHT).build(), labelConstraints);
|
||||
pnlDifficulty.add(new PrefInput(QPref.WILD_OPPONENTS_MULTIPLIER, QuestPreferencesErrType.DIFFICULTY), fieldConstraints + ", wrap");
|
||||
pnlDifficulty.add(new FLabel.Builder().text(localizer.getMessage("lblWildOpponentNumber")).fontAlign(SwingConstants.RIGHT).build(), labelConstraints);
|
||||
pnlDifficulty.add(new PrefInput(QPref.WILD_OPPONENTS_NUMBER, QuestPreferencesErrType.DIFFICULTY), fieldConstraints + ", wrap");
|
||||
}
|
||||
private void populateBooster() {
|
||||
pnlBooster.setOpaque(false);
|
||||
|
||||
@@ -2587,3 +2587,7 @@ lblEnterMessageToSend=Enter message to send
|
||||
lblDetectedInvalidHostAddress=Invalid host address ({0}) was detected.
|
||||
#Player.java
|
||||
lblChooseACompanion=Choose a companion
|
||||
lblWildOpponentMultiplier=Wild Multiplier
|
||||
lblEnteraDecimal=Enter a decimal
|
||||
lblWildOpponentNumber=Number of Wild Opponents
|
||||
lblWildOpponentNumberError=Wild Opponents can only be 0 to 3
|
||||
|
||||
@@ -119,4 +119,8 @@ public abstract class PreferencesStore<T extends Enum<T>> {
|
||||
public final boolean getPrefBoolean(final T fp0) {
|
||||
return Boolean.parseBoolean(getPref(fp0));
|
||||
}
|
||||
|
||||
public final double getPrefDouble(final T fp0) {
|
||||
return Double.parseDouble(getPref(fp0));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,227 @@
|
||||
package forge.quest;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import forge.model.FModel;
|
||||
import forge.quest.data.QuestPreferences;
|
||||
import forge.quest.data.QuestPreferences.DifficultyPrefs;
|
||||
import forge.quest.data.QuestPreferences.QPref;
|
||||
import forge.quest.io.MainWorldDuelReader;
|
||||
import forge.util.CollectionSuppliers;
|
||||
import forge.util.MyRandom;
|
||||
import forge.util.maps.EnumMapOfLists;
|
||||
import forge.util.maps.MapOfLists;
|
||||
import forge.util.storage.IStorage;
|
||||
import forge.util.storage.StorageBase;
|
||||
|
||||
public class MainWorldEventDuelManager implements QuestEventDuelManagerInterface {
|
||||
|
||||
protected final MapOfLists<QuestEventDifficulty, QuestEventDuel> sortedDuels = new EnumMapOfLists<>(QuestEventDifficulty.class, CollectionSuppliers.arrayLists());
|
||||
protected final IStorage<QuestEventDuel> allDuels;
|
||||
|
||||
/**
|
||||
* Instantiate all events and difficulty lists.
|
||||
*
|
||||
* @param dir   File object
|
||||
*/
|
||||
public MainWorldEventDuelManager(final File dir) {
|
||||
allDuels = new StorageBase<>("Quest duels", new MainWorldDuelReader(dir));
|
||||
assembleDuelDifficultyLists();
|
||||
}
|
||||
|
||||
public Iterable<QuestEventDuel> getAllDuels() {
|
||||
return allDuels;
|
||||
}
|
||||
|
||||
public Iterable<QuestEventDuel> getDuels(QuestEventDifficulty difficulty) {
|
||||
return sortedDuels.get(difficulty);
|
||||
}
|
||||
|
||||
// define fallback orders if there aren't enough opponents defined for a particular difficultly level
|
||||
private static List<QuestEventDifficulty> easyOrder = Arrays.asList(QuestEventDifficulty.EASY, QuestEventDifficulty.MEDIUM, QuestEventDifficulty.HARD, QuestEventDifficulty.EXPERT);
|
||||
private static List<QuestEventDifficulty> mediumOrder = Arrays.asList(QuestEventDifficulty.MEDIUM, QuestEventDifficulty.HARD, QuestEventDifficulty.EASY, QuestEventDifficulty.EXPERT);
|
||||
private static List<QuestEventDifficulty> hardOrder = Arrays.asList(QuestEventDifficulty.HARD, QuestEventDifficulty.MEDIUM, QuestEventDifficulty.EASY, QuestEventDifficulty.EXPERT);
|
||||
private static List<QuestEventDifficulty> expertOrder = Arrays.asList(QuestEventDifficulty.EXPERT, QuestEventDifficulty.HARD, QuestEventDifficulty.MEDIUM, QuestEventDifficulty.EASY);
|
||||
private static List<QuestEventDifficulty> wildOrder = Arrays.asList(QuestEventDifficulty.WILD);
|
||||
|
||||
private List<QuestEventDifficulty> getOrderForDifficulty(QuestEventDifficulty d) {
|
||||
final List<QuestEventDifficulty> difficultyOrder;
|
||||
|
||||
switch (d) {
|
||||
case EASY:
|
||||
difficultyOrder = easyOrder;
|
||||
break;
|
||||
case MEDIUM:
|
||||
difficultyOrder = mediumOrder;
|
||||
break;
|
||||
case HARD:
|
||||
difficultyOrder = hardOrder;
|
||||
break;
|
||||
case EXPERT:
|
||||
difficultyOrder = expertOrder;
|
||||
break;
|
||||
case WILD:
|
||||
difficultyOrder = wildOrder;
|
||||
break;
|
||||
default:
|
||||
throw new RuntimeException("unhandled difficulty: " + d);
|
||||
}
|
||||
|
||||
return difficultyOrder;
|
||||
}
|
||||
|
||||
private void addDuel(List<QuestEventDuel> outList, QuestEventDifficulty targetDifficulty, int toAdd) {
|
||||
|
||||
// if there's no way we can satisfy the request, return now
|
||||
if (allDuels.size() <= toAdd) {
|
||||
return;
|
||||
}
|
||||
|
||||
final List<QuestEventDifficulty> difficultyOrder = getOrderForDifficulty(targetDifficulty);
|
||||
|
||||
for (QuestEventDifficulty d : difficultyOrder) { // will add duels from preferred difficulty, will use others if the former has too few options.
|
||||
for (QuestEventDuel duel : sortedDuels.get(d)) {
|
||||
if (toAdd <= 0) {
|
||||
return;
|
||||
}
|
||||
if (!outList.contains(duel)) {
|
||||
outList.add(duel);
|
||||
toAdd--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void addRandomDuel(final List<QuestEventDuel> listOfDuels, final QuestEventDifficulty difficulty) {
|
||||
|
||||
QuestEventDuel duel = new QuestEventDuel();
|
||||
|
||||
List<QuestEventDifficulty> difficultyOrder = getOrderForDifficulty(difficulty);
|
||||
List<QuestEventDuel> possibleDuels = new ArrayList<>();
|
||||
for (QuestEventDifficulty diff : difficultyOrder) {
|
||||
possibleDuels = new ArrayList<>(sortedDuels.get(diff));
|
||||
if (!possibleDuels.isEmpty()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
QuestEventDuel randomOpponent = possibleDuels.get(((int) (possibleDuels.size() * MyRandom.getRandom().nextDouble())));
|
||||
|
||||
duel.setTitle("Random Opponent");
|
||||
duel.setIconImageKey(randomOpponent.getIconImageKey());
|
||||
duel.setOpponentName(randomOpponent.getTitle());
|
||||
duel.setDifficulty(difficulty);
|
||||
duel.setProfile(randomOpponent.getProfile());
|
||||
duel.setShowDifficulty(false);
|
||||
duel.setDescription("Fight a random opponent");
|
||||
duel.setEventDeck(randomOpponent.getEventDeck());
|
||||
|
||||
listOfDuels.add(duel);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates an array of new duel opponents based on current win conditions.
|
||||
*
|
||||
* @return an array of {@link java.lang.String} objects.
|
||||
*/
|
||||
public final List<QuestEventDuel> generateDuels() {
|
||||
|
||||
final QuestPreferences questPreferences = FModel.getQuestPreferences();
|
||||
boolean moreDuelChoices = questPreferences.getPrefInt(QPref.MORE_DUEL_CHOICES) > 0;
|
||||
|
||||
if (FModel.getQuest().getAchievements() == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final QuestController qCtrl = FModel.getQuest();
|
||||
final int numberOfWins = qCtrl.getAchievements().getWin();
|
||||
|
||||
final int index = qCtrl.getAchievements().getDifficulty();
|
||||
final List<QuestEventDuel> duelOpponents = new ArrayList<>();
|
||||
|
||||
QuestEventDifficulty randomDuelDifficulty = QuestEventDifficulty.EASY;
|
||||
|
||||
if (numberOfWins < questPreferences.getPrefInt(DifficultyPrefs.WINS_MEDIUMAI, index)) {
|
||||
addDuel(duelOpponents, QuestEventDifficulty.EASY, 3);
|
||||
randomDuelDifficulty = QuestEventDifficulty.EASY;
|
||||
} else if (numberOfWins == questPreferences.getPrefInt(DifficultyPrefs.WINS_MEDIUMAI, index)) {
|
||||
addDuel(duelOpponents, QuestEventDifficulty.EASY, 1);
|
||||
addDuel(duelOpponents, QuestEventDifficulty.MEDIUM, 2);
|
||||
randomDuelDifficulty = QuestEventDifficulty.MEDIUM;
|
||||
} else if (numberOfWins < questPreferences.getPrefInt(DifficultyPrefs.WINS_HARDAI, index)) {
|
||||
addDuel(duelOpponents, QuestEventDifficulty.MEDIUM, 3);
|
||||
randomDuelDifficulty = QuestEventDifficulty.MEDIUM;
|
||||
} else if (numberOfWins == questPreferences.getPrefInt(DifficultyPrefs.WINS_HARDAI, index)) {
|
||||
addDuel(duelOpponents, QuestEventDifficulty.MEDIUM, 1);
|
||||
addDuel(duelOpponents, QuestEventDifficulty.HARD, 2);
|
||||
randomDuelDifficulty = QuestEventDifficulty.HARD;
|
||||
} else if (numberOfWins < questPreferences.getPrefInt(DifficultyPrefs.WINS_EXPERTAI, index)) {
|
||||
addDuel(duelOpponents, QuestEventDifficulty.HARD, 3);
|
||||
randomDuelDifficulty = QuestEventDifficulty.HARD;
|
||||
} else {
|
||||
addDuel(duelOpponents, QuestEventDifficulty.HARD, 2);
|
||||
addDuel(duelOpponents, QuestEventDifficulty.EXPERT, 1);
|
||||
if (MyRandom.getRandom().nextDouble() * 3 < 2) {
|
||||
randomDuelDifficulty = QuestEventDifficulty.HARD;
|
||||
} else {
|
||||
randomDuelDifficulty = QuestEventDifficulty.EXPERT;
|
||||
}
|
||||
}
|
||||
|
||||
if (moreDuelChoices) {
|
||||
if (numberOfWins == questPreferences.getPrefInt(DifficultyPrefs.WINS_MEDIUMAI, index)) {
|
||||
addDuel(duelOpponents, QuestEventDifficulty.EASY, 1);
|
||||
} else if (numberOfWins < questPreferences.getPrefInt(DifficultyPrefs.WINS_HARDAI, index)) {
|
||||
addDuel(duelOpponents, QuestEventDifficulty.EASY, 1);
|
||||
} else if (numberOfWins == questPreferences.getPrefInt(DifficultyPrefs.WINS_HARDAI, index)) {
|
||||
addDuel(duelOpponents, QuestEventDifficulty.MEDIUM, 1);
|
||||
} else {
|
||||
addDuel(duelOpponents, QuestEventDifficulty.MEDIUM, 1);
|
||||
addDuel(duelOpponents, QuestEventDifficulty.EASY, 1);
|
||||
}
|
||||
}
|
||||
|
||||
//TODO put in preferences the number of wild duels to add
|
||||
addDuel(duelOpponents, QuestEventDifficulty.WILD, FModel.getQuestPreferences().getPrefInt(QPref.WILD_OPPONENTS_NUMBER));
|
||||
addRandomDuel(duelOpponents, randomDuelDifficulty);
|
||||
|
||||
return duelOpponents;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* assembleDuelDifficultyLists.
|
||||
* </p>
|
||||
* Assemble duel deck difficulty lists
|
||||
*/
|
||||
protected void assembleDuelDifficultyLists() {
|
||||
|
||||
sortedDuels.clear();
|
||||
sortedDuels.put(QuestEventDifficulty.EASY, new ArrayList<>());
|
||||
sortedDuels.put(QuestEventDifficulty.MEDIUM, new ArrayList<>());
|
||||
sortedDuels.put(QuestEventDifficulty.HARD, new ArrayList<>());
|
||||
sortedDuels.put(QuestEventDifficulty.EXPERT, new ArrayList<>());
|
||||
sortedDuels.put(QuestEventDifficulty.WILD, new ArrayList<>());
|
||||
|
||||
for (final QuestEventDuel qd : allDuels) {
|
||||
sortedDuels.add(qd.getDifficulty(), qd);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** */
|
||||
public void randomizeOpponents() {
|
||||
for (QuestEventDifficulty qd : sortedDuels.keySet()) {
|
||||
List<QuestEventDuel> list = (List<QuestEventDuel>) sortedDuels.get(qd);
|
||||
Collections.shuffle(list, MyRandom.getRandom());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -17,8 +17,18 @@
|
||||
*/
|
||||
package forge.quest;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.eventbus.Subscribe;
|
||||
|
||||
import forge.card.CardEdition;
|
||||
import forge.deck.Deck;
|
||||
import forge.deck.DeckGroup;
|
||||
@@ -32,16 +42,18 @@ import forge.properties.ForgeConstants;
|
||||
import forge.quest.bazaar.QuestBazaarManager;
|
||||
import forge.quest.bazaar.QuestItemType;
|
||||
import forge.quest.bazaar.QuestPetStorage;
|
||||
import forge.quest.data.*;
|
||||
import forge.quest.data.DeckConstructionRules;
|
||||
import forge.quest.data.GameFormatQuest;
|
||||
import forge.quest.data.QuestAchievements;
|
||||
import forge.quest.data.QuestAssets;
|
||||
import forge.quest.data.QuestData;
|
||||
import forge.quest.data.QuestPreferences.DifficultyPrefs;
|
||||
import forge.quest.data.QuestPreferences.QPref;
|
||||
import forge.quest.data.StarRating;
|
||||
import forge.quest.io.QuestChallengeReader;
|
||||
import forge.util.storage.IStorage;
|
||||
import forge.util.storage.StorageBase;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* TODO: Write javadoc for this type.
|
||||
*
|
||||
@@ -456,13 +468,19 @@ public class QuestController {
|
||||
return;
|
||||
}else if (world.isCustom()) {
|
||||
path = world.getDuelsDir() == null ? ForgeConstants.DEFAULT_DUELS_DIR : ForgeConstants.USER_QUEST_WORLD_DIR + world.getDuelsDir();
|
||||
this.duelManager = new QuestEventDuelManager(new File(path));
|
||||
} else {
|
||||
path = world.getDuelsDir() == null ? ForgeConstants.DEFAULT_DUELS_DIR : ForgeConstants.QUEST_WORLD_DIR + world.getDuelsDir();
|
||||
if(QuestWorld.MAINWORLDNAME.equals(world.getName())) {
|
||||
this.duelManager = new MainWorldEventDuelManager(new File(path));
|
||||
} else {
|
||||
this.duelManager = new QuestEventDuelManager(new File(path));
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
this.duelManager = new QuestEventDuelManager(new File(path));
|
||||
}
|
||||
|
||||
this.duelManager = new QuestEventDuelManager(new File(path));
|
||||
}
|
||||
|
||||
public HashSet<StarRating> GetRating() {
|
||||
|
||||
@@ -2,6 +2,9 @@ package forge.quest;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import forge.model.FModel;
|
||||
import forge.quest.data.QuestPreferences.QPref;
|
||||
|
||||
/**
|
||||
* TODO: Write javadoc for this type.
|
||||
*
|
||||
@@ -10,7 +13,8 @@ public enum QuestEventDifficulty {
|
||||
EASY ("easy", 1. ),
|
||||
MEDIUM("medium", 1.5),
|
||||
HARD ("hard", 2. ),
|
||||
EXPERT("very hard", 3. );
|
||||
EXPERT("very hard", 3. ),
|
||||
WILD("wild", FModel.getQuestPreferences().getPrefDouble(QPref.WILD_OPPONENTS_MULTIPLIER) );
|
||||
|
||||
private final String inFile;
|
||||
private final double multiplier;
|
||||
|
||||
@@ -43,6 +43,7 @@ public class QuestWorld implements Comparable<QuestWorld>{
|
||||
public static final String PIONEERWORLDNAME = "Random Pioneer";
|
||||
public static final String MODERNWORLDNAME = "Random Modern";
|
||||
public static final String RANDOMCOMMANDERWORLDNAME = "Random Commander";
|
||||
public static final String MAINWORLDNAME = "Main world";
|
||||
|
||||
private boolean isCustom;
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ package forge.quest.data;
|
||||
|
||||
import forge.properties.ForgeConstants;
|
||||
import forge.properties.PreferencesStore;
|
||||
import forge.util.Localizer;
|
||||
import forge.util.TextUtil;
|
||||
|
||||
import java.io.Serializable;
|
||||
@@ -173,6 +174,8 @@ public class QuestPreferences extends PreferencesStore<QuestPreferences.QPref> i
|
||||
SHOP_WINS_FOR_NO_SELL_LIMIT("50"),
|
||||
// Duels of the current difficulty only, or that and all difficulties below it?
|
||||
MORE_DUEL_CHOICES("0"),
|
||||
WILD_OPPONENTS_MULTIPLIER("2.0"),
|
||||
WILD_OPPONENTS_NUMBER("0"),
|
||||
|
||||
//The number of cards to keep before selling
|
||||
PLAYSET_SIZE("4"),
|
||||
@@ -313,6 +316,11 @@ public class QuestPreferences extends PreferencesStore<QuestPreferences.QPref> i
|
||||
return "Value too small (minimum 1).";
|
||||
}
|
||||
break;
|
||||
case WILD_OPPONENTS_NUMBER:
|
||||
if(val < 0 || val > 3) {
|
||||
return Localizer.getInstance().getMessage("lblWildOpponentNumberError");
|
||||
}
|
||||
break;
|
||||
case BOOSTER_COMMONS:
|
||||
case BOOSTER_UNCOMMONS:
|
||||
case BOOSTER_RARES:
|
||||
|
||||
144
forge-gui/src/main/java/forge/quest/io/MainWorldDuelReader.java
Normal file
144
forge-gui/src/main/java/forge/quest/io/MainWorldDuelReader.java
Normal file
@@ -0,0 +1,144 @@
|
||||
package forge.quest.io;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FilenameFilter;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.FileVisitResult;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.SimpleFileVisitor;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import forge.ImageKeys;
|
||||
import forge.deck.Deck;
|
||||
import forge.deck.io.DeckSerializer;
|
||||
import forge.deck.io.DeckStorage;
|
||||
import forge.model.FModel;
|
||||
import forge.quest.QuestEvent;
|
||||
import forge.quest.QuestEventDifficulty;
|
||||
import forge.quest.QuestEventDuel;
|
||||
import forge.util.FileSection;
|
||||
import forge.util.FileUtil;
|
||||
import forge.util.TextUtil;
|
||||
import forge.util.storage.IStorage;
|
||||
import forge.util.storage.StorageReaderFolder;
|
||||
|
||||
public class MainWorldDuelReader extends StorageReaderFolder<QuestEventDuel> {
|
||||
|
||||
private static final String WILD_DEFAULT_ICON_NAME = "Wild.jpg";
|
||||
private static final String WILD_DIR_NAME = "wild";
|
||||
|
||||
public MainWorldDuelReader(File deckDir0) {
|
||||
super(deckDir0, QuestEvent.FN_GET_NAME);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, QuestEventDuel> readAll() {
|
||||
|
||||
final Map<String, QuestEventDuel> result = new TreeMap<>();
|
||||
|
||||
// First I add wild decks in quest directory
|
||||
try {
|
||||
Files.walkFileTree(directory.toPath(), new SimpleFileVisitor<Path>() {
|
||||
@Override
|
||||
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
|
||||
File actualFile = new File(path.toString());
|
||||
try {
|
||||
final QuestEventDuel newDeck = read(actualFile);
|
||||
if (null == newDeck) {
|
||||
final String msg = "An object stored in " + actualFile.getPath() + " failed to load.\nPlease submit this as a bug with the mentioned file/directory attached.";
|
||||
throw new RuntimeException(msg);
|
||||
}
|
||||
|
||||
String newKey = keySelector.apply(newDeck);
|
||||
if (result.containsKey(newKey)) {
|
||||
System.err.println("StorageReaderFolder: an object with key " + newKey + " is already present - skipping new entry");
|
||||
} else {
|
||||
result.put(newKey, newDeck);
|
||||
}
|
||||
} catch (final NoSuchElementException ex) {
|
||||
final String message = TextUtil.concatWithSpace( actualFile.getName(),"failed to load because ----", ex.getMessage());
|
||||
objectsThatFailedToLoad.add(message);
|
||||
}
|
||||
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
});
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// then I add wild decks in constructed directory
|
||||
IStorage<Deck> constructedDecks = FModel.getDecks().getConstructed();
|
||||
Iterator it = constructedDecks.iterator();
|
||||
while(it.hasNext()) {
|
||||
Deck currDeck = (Deck) it.next();
|
||||
final QuestEventDuel newDeck = read(currDeck);
|
||||
String newKey = keySelector.apply(newDeck);
|
||||
if (result.containsKey(newKey)) {
|
||||
System.err.println("StorageReaderFolder: an object with key " + newKey + " is already present - skipping new entry");
|
||||
} else {
|
||||
result.put(newKey, newDeck);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected QuestEventDuel read(File file) {
|
||||
final Map<String, List<String>> contents = FileSection.parseSections(FileUtil.readFile(file));
|
||||
final QuestEventDuel qc = new QuestEventDuel();
|
||||
|
||||
// Common properties
|
||||
FileSection sectionMeta = FileSection.parse(contents.get("metadata"), FileSection.EQUALS_KV_SEPARATOR);
|
||||
qc.setName(sectionMeta.get("Name")); // Challenges have unique titles
|
||||
|
||||
boolean difficultySpecified = !StringUtils.isEmpty(sectionMeta.get("Difficulty"));
|
||||
if(difficultySpecified) {
|
||||
qc.setTitle(sectionMeta.get("Title"));
|
||||
qc.setDifficulty(QuestEventDifficulty.fromString(sectionMeta.get("Difficulty")));
|
||||
qc.setDescription(sectionMeta.get("Description", "").replace("\\n", "\n"));
|
||||
qc.setCardReward(sectionMeta.get("Card Reward"));
|
||||
qc.setIconImageKey(ImageKeys.ICON_PREFIX + sectionMeta.get("Icon"));
|
||||
if (sectionMeta.contains("Profile")) {
|
||||
qc.setProfile(sectionMeta.get("Profile"));
|
||||
}
|
||||
} else {
|
||||
qc.setDifficulty(QuestEventDifficulty.WILD);
|
||||
qc.setTitle(sectionMeta.get("Title") != null ? sectionMeta.get("Title") : qc.getName());
|
||||
qc.setDescription(sectionMeta.get("Description") != null ? sectionMeta.get("Description") : "Wild opponent");
|
||||
qc.setIconImageKey(ImageKeys.ICON_PREFIX + (sectionMeta.get("Icon") != null ? sectionMeta.get("Icon") : WILD_DEFAULT_ICON_NAME));
|
||||
}
|
||||
|
||||
// Deck
|
||||
qc.setEventDeck(DeckSerializer.fromSections(contents));
|
||||
return qc;
|
||||
}
|
||||
|
||||
protected QuestEventDuel read(Deck deck) {
|
||||
final QuestEventDuel qc = new QuestEventDuel();
|
||||
qc.setName(deck.getName());
|
||||
qc.setTitle(deck.getName());
|
||||
qc.setDifficulty(QuestEventDifficulty.WILD);
|
||||
qc.setDescription("Wild opponent");
|
||||
qc.setIconImageKey(ImageKeys.ICON_PREFIX + WILD_DEFAULT_ICON_NAME);
|
||||
qc.setEventDeck(deck);
|
||||
|
||||
return qc;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected FilenameFilter getFileFilter() {
|
||||
return DeckStorage.DCK_FILE_FILTER;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -35,7 +35,7 @@ public class QuestDuelReader extends StorageReaderFolder<QuestEventDuel> {
|
||||
qc.setCardReward(sectionMeta.get("Card Reward"));
|
||||
qc.setIconImageKey(ImageKeys.ICON_PREFIX + sectionMeta.get("Icon"));
|
||||
if (sectionMeta.contains("Profile")) {
|
||||
qc.setProfile(sectionMeta.get("Profile"));
|
||||
qc.setProfile(sectionMeta.get("Profile"));
|
||||
}
|
||||
|
||||
// Deck
|
||||
|
||||
Reference in New Issue
Block a user