Fixed NPE error on AI calcs

This commit is contained in:
Oleg Agafonov 2018-12-18 17:17:10 +04:00
parent d0e4ecaba1
commit 4495d75d8a
5 changed files with 79 additions and 88 deletions

View file

@ -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<String> supportedSets;
private final Map<String, String> 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) {

View file

@ -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<String, ArrayList<TokenData>> 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());
}
}
}

View file

@ -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<String, String> languageAliases;
private final Map<String, Map<String, String>> sets;
private final Set<String> 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) {
}

View file

@ -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

View file

@ -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<CardDownloadData> tokFileTokens = DownloadPictures.getTokenCardUrls();
ArrayList<CardDownloadData> tokFileTokens = DownloadPicturesService.getTokenCardUrls();
LinkedHashMap<String, String> tokDataClassesIndex = new LinkedHashMap<>();
LinkedHashMap<String, String> tokDataNamesIndex = new LinkedHashMap<>();
for (CardDownloadData tokData : tokFileTokens) {