From 4495d75d8abc00af9a4c77fb746200db0d44f757 Mon Sep 17 00:00:00 2001 From: Oleg Agafonov Date: Tue, 18 Dec 2018 17:17:10 +0400 Subject: [PATCH] Fixed NPE error on AI calcs --- .../card/dl/sources/ScryfallImageSource.java | 45 +++++--- .../card/dl/sources/TokensMtgImageSource.java | 8 +- .../dl/sources/WizardCardsImageSource.java | 105 +++++++----------- .../src/mage/player/ai/ComputerPlayer7.java | 5 +- .../java/mage/verify/VerifyCardDataTest.java | 4 +- 5 files changed, 79 insertions(+), 88 deletions(-) diff --git a/Mage.Client/src/main/java/org/mage/plugins/card/dl/sources/ScryfallImageSource.java b/Mage.Client/src/main/java/org/mage/plugins/card/dl/sources/ScryfallImageSource.java index fa6dc551c3..6554780bfb 100644 --- a/Mage.Client/src/main/java/org/mage/plugins/card/dl/sources/ScryfallImageSource.java +++ b/Mage.Client/src/main/java/org/mage/plugins/card/dl/sources/ScryfallImageSource.java @@ -1,6 +1,6 @@ package org.mage.plugins.card.dl.sources; -import mage.client.dialog.PreferencesDialog; +import mage.client.util.CardLanguage; import org.mage.plugins.card.images.CardDownloadData; import java.util.*; @@ -14,20 +14,22 @@ public enum ScryfallImageSource implements CardImageSource { private final Set supportedSets; private final Map languageAliases; + private CardLanguage currentLanguage = CardLanguage.ENGLISH; // working language ScryfallImageSource() { // https://scryfall.com/docs/api/languages languageAliases = new HashMap<>(); - languageAliases.put("en", "en"); - languageAliases.put("es", "es"); - languageAliases.put("jp", "ja"); - languageAliases.put("it", "it"); - languageAliases.put("fr", "fr"); - languageAliases.put("cn", "zhs"); // Simplified Chinese - languageAliases.put("de", "de"); - languageAliases.put("ko", "ko"); - languageAliases.put("pt", "pt"); - languageAliases.put("ru", "ru"); + languageAliases.put(CardLanguage.ENGLISH.getCode(), "en"); + languageAliases.put(CardLanguage.SPANISH.getCode(), "es"); + languageAliases.put(CardLanguage.FRENCH.getCode(), "fr"); + languageAliases.put(CardLanguage.GERMAN.getCode(), "de"); + languageAliases.put(CardLanguage.ITALIAN.getCode(), "it"); + languageAliases.put(CardLanguage.PORTUGUESE.getCode(), "pt"); + languageAliases.put(CardLanguage.JAPANESE.getCode(), "ja"); + languageAliases.put(CardLanguage.KOREAN.getCode(), "ko"); + languageAliases.put(CardLanguage.RUSSIAN.getCode(), "ru"); + languageAliases.put(CardLanguage.CHINES_SIMPLE.getCode(), "zhs"); + languageAliases.put(CardLanguage.CHINES_TRADITION.getCode(), "zht"); supportedSets = new LinkedHashSet<>(); // supportedSets.add("PTC"); // @@ -245,9 +247,9 @@ public enum ScryfallImageSource implements CardImageSource { @Override public CardImageUrls generateURL(CardDownloadData card) throws Exception { - String preferredLanguage = PreferencesDialog.getCachedValue(PreferencesDialog.KEY_CARD_IMAGES_PREF_LANGUAGE, "en"); - String defaultCode = "en"; - String localizedCode = languageAliases.getOrDefault(preferredLanguage, defaultCode); + String preferredCode = this.getCurrentLanguage().getCode(); + String defaultCode = CardLanguage.ENGLISH.getCode(); + String localizedCode = languageAliases.getOrDefault(preferredCode, defaultCode); // loc example: https://api.scryfall.com/cards/xln/121/ru?format=image // WARNING, some cards haven't direct images and uses random GUID: @@ -345,6 +347,21 @@ public enum ScryfallImageSource implements CardImageSource { return false; } + @Override + public boolean isLanguagesSupport() { + return true; + } + + @Override + public void setCurrentLanguage(CardLanguage cardLanguage) { + this.currentLanguage = cardLanguage; + } + + @Override + public CardLanguage getCurrentLanguage() { + return currentLanguage; + } + @Override public void doPause(String httpImageUrl) { diff --git a/Mage.Client/src/main/java/org/mage/plugins/card/dl/sources/TokensMtgImageSource.java b/Mage.Client/src/main/java/org/mage/plugins/card/dl/sources/TokensMtgImageSource.java index da76bfc8b6..ae000f70da 100644 --- a/Mage.Client/src/main/java/org/mage/plugins/card/dl/sources/TokensMtgImageSource.java +++ b/Mage.Client/src/main/java/org/mage/plugins/card/dl/sources/TokensMtgImageSource.java @@ -20,7 +20,7 @@ import java.util.logging.Level; import mage.constants.SubType; import org.apache.log4j.Logger; import org.mage.plugins.card.images.CardDownloadData; -import org.mage.plugins.card.images.DownloadPictures; +import org.mage.plugins.card.images.DownloadPicturesService; import org.mage.plugins.card.utils.CardImageUtils; /** @@ -182,7 +182,7 @@ public enum TokensMtgImageSource implements CardImageSource { private HashMap> getTokensData() throws IOException { synchronized (tokensDataSync) { if (tokensData == null) { - DownloadPictures.getInstance().updateAndViewMessage("Creating token data..."); + DownloadPicturesService.getInstance().updateAndViewMessage("Find tokens data..."); tokensData = new HashMap<>(); // get tokens data from resource file @@ -233,10 +233,10 @@ public enum TokensMtgImageSource implements CardImageSource { } } } - DownloadPictures.getInstance().updateAndViewMessage(""); + DownloadPicturesService.getInstance().updateAndViewMessage(""); } catch (Exception ex) { logger.warn("Failed to get tokens description from tokens.mtg.onl", ex); - DownloadPictures.getInstance().updateAndViewMessage(ex.getMessage()); + DownloadPicturesService.getInstance().updateAndViewMessage(ex.getMessage()); } } } diff --git a/Mage.Client/src/main/java/org/mage/plugins/card/dl/sources/WizardCardsImageSource.java b/Mage.Client/src/main/java/org/mage/plugins/card/dl/sources/WizardCardsImageSource.java index 5d39a943f6..7580c000f2 100644 --- a/Mage.Client/src/main/java/org/mage/plugins/card/dl/sources/WizardCardsImageSource.java +++ b/Mage.Client/src/main/java/org/mage/plugins/card/dl/sources/WizardCardsImageSource.java @@ -1,22 +1,10 @@ package org.mage.plugins.card.dl.sources; -import java.io.IOException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; import mage.cards.Sets; import mage.cards.repository.CardCriteria; import mage.cards.repository.CardInfo; import mage.cards.repository.CardRepository; -import mage.client.dialog.PreferencesDialog; +import mage.client.util.CardLanguage; import org.apache.log4j.Logger; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; @@ -24,6 +12,12 @@ import org.jsoup.select.Elements; import org.mage.plugins.card.images.CardDownloadData; import org.mage.plugins.card.utils.CardImageUtils; +import java.io.IOException; +import java.util.*; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + /** * @author North */ @@ -37,6 +31,7 @@ public enum WizardCardsImageSource implements CardImageSource { private final Map languageAliases; private final Map> sets; private final Set supportedSets; + private CardLanguage currentLanguage = CardLanguage.ENGLISH; // working language @Override public String getSourceName() { @@ -44,6 +39,20 @@ public enum WizardCardsImageSource implements CardImageSource { } WizardCardsImageSource() { + + languageAliases = new HashMap<>(); + languageAliases.put(CardLanguage.ENGLISH.getCode(), "English"); + languageAliases.put(CardLanguage.SPANISH.getCode(), "Spanish"); + languageAliases.put(CardLanguage.FRENCH.getCode(), "French"); + languageAliases.put(CardLanguage.GERMAN.getCode(), "German"); + languageAliases.put(CardLanguage.ITALIAN.getCode(), "Italian"); + languageAliases.put(CardLanguage.PORTUGUESE.getCode(), "Portuguese (Brazil)"); + languageAliases.put(CardLanguage.JAPANESE.getCode(), "Japanese"); + languageAliases.put(CardLanguage.KOREAN.getCode(), "Korean"); + languageAliases.put(CardLanguage.RUSSIAN.getCode(), "Russian"); + languageAliases.put(CardLanguage.CHINES_SIMPLE.getCode(), "Chinese Simplified"); + languageAliases.put(CardLanguage.CHINES_TRADITION.getCode(), "Chinese Traditional "); + supportedSets = new LinkedHashSet<>(); // supportedSets.add("PTC"); // Prerelease Events supportedSets.add("LEA"); @@ -430,18 +439,6 @@ public enum WizardCardsImageSource implements CardImageSource { setsAliases.put("WTH", "Weatherlight"); setsAliases.put("WWK", "Worldwake"); setsAliases.put("ZEN", "Zendikar"); - - languageAliases = new HashMap<>(); - languageAliases.put("en", "English"); - languageAliases.put("es", "Spanish"); - languageAliases.put("jp", "Japanese"); - languageAliases.put("it", "Italian"); - languageAliases.put("fr", "French"); - languageAliases.put("cn", "Chinese Simplified"); - languageAliases.put("de", "German"); - languageAliases.put("ko", "Korean"); - languageAliases.put("pt", "Portuguese (Brazil)"); - languageAliases.put("ru", "Russian"); } @Override @@ -517,7 +514,7 @@ public enum WizardCardsImageSource implements CardImageSource { if (setNames == null) { setNames = Sets.getInstance().get(cardSet).getName(); } - String preferredLanguage = PreferencesDialog.getCachedValue(PreferencesDialog.KEY_CARD_IMAGES_PREF_LANGUAGE, "en"); + for (String setName : setNames.split("\\^")) { // String URLSetName = URLEncoder.encode(setName, "UTF-8"); String URLSetName = setName.replaceAll(" ", "%20"); @@ -555,7 +552,7 @@ public enum WizardCardsImageSource implements CardImageSource { cardName = cardName.substring(0, pos1); } } - Integer preferredMultiverseId = getLocalizedMultiverseId(preferredLanguage, multiverseId); + Integer preferredMultiverseId = getLocalizedMultiverseId(getCurrentLanguage().getCode(), multiverseId); setLinks.put(cardName.toLowerCase(Locale.ENGLISH) + numberChar, generateLink(preferredMultiverseId)); } } @@ -618,7 +615,7 @@ public enum WizardCardsImageSource implements CardImageSource { } private int getLocalizedMultiverseId(String preferredLanguage, Integer multiverseId) throws IOException { - if (preferredLanguage.equals("en")) { + if (preferredLanguage.equals(CardLanguage.ENGLISH.getCode())) { return multiverseId; } @@ -682,43 +679,6 @@ public enum WizardCardsImageSource implements CardImageSource { return 60.0f; } - // private final class GetImageLinkTask implements Runnable { -// -// private int multiverseId; -// private String cardName; -// private String preferredLanguage; -// private LinkedHashMap setLinks; -// -// public GetImageLinkTask(int multiverseId, String cardName, String preferredLanguage, LinkedHashMap setLinks) { -// try { -// this.multiverseId = multiverseId; -// this.cardName = cardName; -// this.preferredLanguage = preferredLanguage; -// this.setLinks = setLinks; -// } catch (Exception ex) { -// logger.error(ex.getMessage()); -// logger.error("multiverseId: " + multiverseId); -// logger.error("cardName: " + cardName); -// logger.error("preferredLanguage: " + preferredLanguage); -// logger.error("setLinks: " + setLinks.toString()); -// } -// } -// -// @Override -// public void run() { -// try { -// if (cardName.equals("Forest") || cardName.equals("Swamp") || cardName.equals("Mountain") || cardName.equals("Island") || cardName.equals("Plains")) { -// setLinks.putAll(getLandVariations(multiverseId, cardName)); -// } else { -// Integer preferredMultiverseId = getLocalizedMultiverseId(preferredLanguage, multiverseId); -// setLinks.put(cardName.toLowerCase(Locale.ENGLISH), generateLink(preferredMultiverseId)); -// } -// } catch (IOException | NumberFormatException ex) { -// logger.error("Exception when parsing the wizards page: " + ex.getMessage()); -// } -// } -// -// } @Override public int getTotalImages() { return -1; @@ -729,6 +689,21 @@ public enum WizardCardsImageSource implements CardImageSource { return false; } + @Override + public boolean isLanguagesSupport() { + return true; + } + + @Override + public void setCurrentLanguage(CardLanguage cardLanguage) { + this.currentLanguage = cardLanguage; + } + + @Override + public CardLanguage getCurrentLanguage() { + return currentLanguage; + } + @Override public void doPause(String httpImageUrl) { } diff --git a/Mage.Server.Plugins/Mage.Player.AI.MA/src/mage/player/ai/ComputerPlayer7.java b/Mage.Server.Plugins/Mage.Player.AI.MA/src/mage/player/ai/ComputerPlayer7.java index 628472299c..448105209f 100644 --- a/Mage.Server.Plugins/Mage.Player.AI.MA/src/mage/player/ai/ComputerPlayer7.java +++ b/Mage.Server.Plugins/Mage.Player.AI.MA/src/mage/player/ai/ComputerPlayer7.java @@ -111,9 +111,8 @@ public class ComputerPlayer7 extends ComputerPlayer6 { Game sim = createSimulation(game); SimulationNode2.resetCount(); root = new SimulationNode2(null, sim, maxDepth, playerId); - addActionsTimed(); - if (root.children != null - && !root.children.isEmpty()) { + addActionsTimed(); // TODO: root can be null again after addActionsTimed O_o need to research (it's a CPU AI problem?) + if (root != null && root.children != null && !root.children.isEmpty()) { logger.trace("After add actions timed: root.children.size = " + root.children.size()); root = root.children.get(0); // prevent repeating always the same action with no cost diff --git a/Mage.Verify/src/test/java/mage/verify/VerifyCardDataTest.java b/Mage.Verify/src/test/java/mage/verify/VerifyCardDataTest.java index 0d11f3230a..6dc4d00213 100644 --- a/Mage.Verify/src/test/java/mage/verify/VerifyCardDataTest.java +++ b/Mage.Verify/src/test/java/mage/verify/VerifyCardDataTest.java @@ -14,7 +14,7 @@ import org.junit.Assert; import org.junit.Ignore; import org.junit.Test; import org.mage.plugins.card.images.CardDownloadData; -import org.mage.plugins.card.images.DownloadPictures; +import org.mage.plugins.card.images.DownloadPicturesService; import org.reflections.Reflections; import java.io.IOException; @@ -425,7 +425,7 @@ public class VerifyCardDataTest { } // tok file's data - ArrayList tokFileTokens = DownloadPictures.getTokenCardUrls(); + ArrayList tokFileTokens = DownloadPicturesService.getTokenCardUrls(); LinkedHashMap tokDataClassesIndex = new LinkedHashMap<>(); LinkedHashMap tokDataNamesIndex = new LinkedHashMap<>(); for (CardDownloadData tokData : tokFileTokens) {