From d3089c29553fe6536411bed6cc696838a6349291 Mon Sep 17 00:00:00 2001 From: Agetian Date: Thu, 29 Jun 2017 07:51:45 +0000 Subject: [PATCH] - Fixed Add Random Duel crashing if a difficulty tier is not available in the quest world. --- .../forge/quest/QuestEventDuelManager.java | 37 +++++++++++++------ 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/forge-gui/src/main/java/forge/quest/QuestEventDuelManager.java b/forge-gui/src/main/java/forge/quest/QuestEventDuelManager.java index 6f7767888d4..d5053c6222c 100644 --- a/forge-gui/src/main/java/forge/quest/QuestEventDuelManager.java +++ b/forge-gui/src/main/java/forge/quest/QuestEventDuelManager.java @@ -65,16 +65,10 @@ public class QuestEventDuelManager { private static List hardOrder = Arrays.asList(QuestEventDifficulty.HARD, QuestEventDifficulty.MEDIUM, QuestEventDifficulty.EASY, QuestEventDifficulty.EXPERT); private static List expertOrder = Arrays.asList(QuestEventDifficulty.EXPERT, QuestEventDifficulty.HARD, QuestEventDifficulty.MEDIUM, QuestEventDifficulty.EASY); - private void addDuel(List outList, QuestEventDifficulty targetDifficulty, int toAdd) { + private List getOrderForDifficulty(QuestEventDifficulty d) { + final List difficultyOrder; - // if there's no way we can satisfy the request, return now - if (allDuels.size() <= toAdd) { - return; - } - - final List difficultyOrder; - - switch (targetDifficulty) { + switch (d) { case EASY: difficultyOrder = easyOrder; break; @@ -88,9 +82,21 @@ public class QuestEventDuelManager { difficultyOrder = expertOrder; break; default: - throw new RuntimeException("unhandled difficulty: " + targetDifficulty); + throw new RuntimeException("unhandled difficulty: " + d); } + return difficultyOrder; + } + + private void addDuel(List outList, QuestEventDifficulty targetDifficulty, int toAdd) { + + // if there's no way we can satisfy the request, return now + if (allDuels.size() <= toAdd) { + return; + } + + final List 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) { @@ -109,7 +115,16 @@ public class QuestEventDuelManager { QuestEventDuel duel = new QuestEventDuel(); - List possibleDuels = new ArrayList<>(sortedDuels.get(difficulty)); + List difficultyOrder = getOrderForDifficulty(difficulty); + + List possibleDuels = new ArrayList<>(); + for (QuestEventDifficulty diff : difficultyOrder) { + possibleDuels = new ArrayList<>(sortedDuels.get(diff)); + if (!possibleDuels.isEmpty()) { + break; + } + } + QuestEventDuel randomOpponent = possibleDuels.get(((int) (possibleDuels.size() * Math.random()))); duel.setTitle("Random Opponent");