Search in sources :

Example 1 with CardDownloadData

use of org.mage.plugins.card.images.CardDownloadData in project mage by magefree.

the class CardImageUtils method searchForCardImage.

private static String searchForCardImage(CardDownloadData card) {
    TFile file;
    String path;
    CardDownloadData c = new CardDownloadData(card);
    c.setSet(card.getTokenSetCode());
    path = getTokenImagePath(c);
    file = new TFile(path);
    if (file.exists()) {
        pathCache.put(card, path);
        return path;
    }
    return generateTokenDescriptorImagePath(card);
}
Also used : TFile(net.java.truevfs.access.TFile) CardDownloadData(org.mage.plugins.card.images.CardDownloadData)

Example 2 with CardDownloadData

use of org.mage.plugins.card.images.CardDownloadData in project mage by magefree.

the class TokensMtgImageSourceTest method generateTokenUrlTest.

@Test
public void generateTokenUrlTest() throws Exception {
    CardImageSource imageSource = TokensMtgImageSource.instance;
    CardImageUrls url = imageSource.generateTokenUrl(new CardDownloadData("Thopter", "ORI", "0", false, 1, "ORI", ""));
    Assert.assertEquals("https://tokens.mtg.onl/tokens/ORI_010-Thopter.jpg", url.baseUrl);
    url = imageSource.generateTokenUrl(new CardDownloadData("Thopter", "ORI", "0", false, 2, "ORI", ""));
    Assert.assertEquals("https://tokens.mtg.onl/tokens/ORI_011-Thopter.jpg", url.baseUrl);
    url = imageSource.generateTokenUrl(new CardDownloadData("Ashaya, the Awoken World", "ORI", "0", false, 0, "ORI", ""));
    Assert.assertEquals("https://tokens.mtg.onl/tokens/ORI_007-Ashaya,-the-Awoken-World.jpg", url.baseUrl);
    url = imageSource.generateTokenUrl(new CardDownloadData("Emblem Gideon, Ally of Zendikar", "BFZ", "0", false, 0, null, ""));
    Assert.assertEquals("https://tokens.mtg.onl/tokens/BFZ_012-Gideon-Emblem.jpg", url.baseUrl);
}
Also used : CardImageUrls(org.mage.plugins.card.dl.sources.CardImageUrls) CardDownloadData(org.mage.plugins.card.images.CardDownloadData) CardImageSource(org.mage.plugins.card.dl.sources.CardImageSource) Test(org.junit.Test)

Example 3 with CardDownloadData

use of org.mage.plugins.card.images.CardDownloadData in project mage by magefree.

the class VerifyCardDataTest method test_checkMissingTokenData.

@Test
// TODO: enable test after massive token fixes
@Ignore
public void test_checkMissingTokenData() {
    Collection<String> errorsList = new ArrayList<>();
    Collection<String> warningsList = new ArrayList<>();
    // all tokens must be stores in card-pictures-tok.txt (if not then viewer and image downloader are missing token images)
    // https://github.com/ronmamo/reflections
    Reflections reflections = new Reflections("mage.");
    Set<Class<? extends TokenImpl>> tokenClassesList = reflections.getSubTypesOf(TokenImpl.class);
    // xmage tokens
    Set<Class<? extends TokenImpl>> privateTokens = new HashSet<>();
    Set<Class<? extends TokenImpl>> publicTokens = new HashSet<>();
    for (Class<? extends TokenImpl> tokenClass : tokenClassesList) {
        if (Modifier.isPublic(tokenClass.getModifiers())) {
            publicTokens.add(tokenClass);
        } else {
            privateTokens.add(tokenClass);
        }
    }
    // tok file's data
    List<CardDownloadData> tokFileTokens = DownloadPicturesService.getTokenCardUrls();
    LinkedHashMap<String, String> tokDataClassesIndex = new LinkedHashMap<>();
    LinkedHashMap<String, String> tokDataNamesIndex = new LinkedHashMap<>();
    for (CardDownloadData tokData : tokFileTokens) {
        String searchName;
        String setsList;
        // by class
        searchName = tokData.getTokenClassName();
        setsList = tokDataClassesIndex.getOrDefault(searchName, "");
        if (!setsList.isEmpty()) {
            setsList += ",";
        }
        setsList += tokData.getSet();
        tokDataClassesIndex.put(searchName, setsList);
        // by name
        searchName = tokData.getName();
        setsList = tokDataNamesIndex.getOrDefault(searchName, "");
        if (!setsList.isEmpty()) {
            setsList += ",";
        }
        setsList += tokData.getSet();
        tokDataNamesIndex.put(searchName, setsList);
    }
    // 1. check token name convention
    for (Class<? extends TokenImpl> tokenClass : tokenClassesList) {
        if (!tokenClass.getName().endsWith("Token")) {
            String className = extractShortClass(tokenClass);
            warningsList.add("warning, token class must ends with Token: " + className + " from " + tokenClass.getName());
        }
    }
    // 2. check store file for public
    for (Class<? extends TokenImpl> tokenClass : publicTokens) {
        String fullClass = tokenClass.getName();
        if (!fullClass.startsWith("mage.game.permanent.token.")) {
            String className = extractShortClass(tokenClass);
            errorsList.add("Error: public token must stores in mage.game.permanent.token package: " + className + " from " + tokenClass.getName());
        }
    }
    // 3. check private tokens (they aren't need at all)
    for (Class<? extends TokenImpl> tokenClass : privateTokens) {
        String className = extractShortClass(tokenClass);
        errorsList.add("Error: no needs in private tokens, replace it with CreatureToken: " + className + " from " + tokenClass.getName());
    }
    // 4. all public tokens must have tok-data (private tokens uses for innner abilities -- no need images for it)
    for (Class<? extends TokenImpl> tokenClass : publicTokens) {
        String className = extractShortClass(tokenClass);
        Token token = (Token) createNewObject(tokenClass);
        if (token == null) {
            errorsList.add("Error: token must have default constructor with zero params: " + tokenClass.getName());
        } else if (tokDataNamesIndex.getOrDefault(token.getName(), "").isEmpty()) {
            errorsList.add("Error: can't find data in card-pictures-tok.txt for token: " + tokenClass.getName() + " -> " + token.getName());
        }
    }
    // TODO: all sets must have full tokens data in tok file (token in every set)
    // 1. Download scryfall tokens list: https://api.scryfall.com/cards/search?q=t:token
    // 2. Proccess each token with all prints: read "prints_search_uri" field from token data and go to link like
    // https://api.scryfall.com/cards/search?order=set&q=%21%E2%80%9CAngel%E2%80%9D&unique=prints
    // 3. Collect all strings in "set@name"
    // 4. Proccess tokens data and find missing strings from "set@name" list
    printMessages(warningsList);
    printMessages(errorsList);
    if (errorsList.size() > 0) {
        Assert.fail("Found token errors: " + errorsList.size());
    }
// TODO: all token must have correct availableImageSetCodes (all sets with that token)
// Some sets have original card, but don't have token card at all. So you must use scryfall tokens list above to find
// all token's sets and compare with xmage
}
Also used : CardDownloadData(org.mage.plugins.card.images.CardDownloadData) TokenImpl(mage.game.permanent.token.TokenImpl) Token(mage.game.permanent.token.Token) Reflections(org.reflections.Reflections) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with CardDownloadData

use of org.mage.plugins.card.images.CardDownloadData in project mage by magefree.

the class MageBook method loadTokens.

public List<Object> loadTokens() {
    List<Object> res = new ArrayList<>();
    // tokens
    List<CardDownloadData> allTokens = getTokenCardUrls();
    for (CardDownloadData token : allTokens) {
        if (token.getSet().equals(currentSet)) {
            try {
                String className = token.getName();
                className = className.replaceAll("[^a-zA-Z0-9]", "");
                className = "mage.game.permanent.token." + className + "Token";
                if (token.getTokenClassName() != null && token.getTokenClassName().length() > 0) {
                    if (token.getTokenClassName().toLowerCase(Locale.ENGLISH).matches(".*token.*")) {
                        className = token.getTokenClassName();
                        className = "mage.game.permanent.token." + className;
                    } else if (token.getTokenClassName().toLowerCase(Locale.ENGLISH).matches(".*emblem.*")) {
                        continue;
                    }
                }
                Class<?> c = Class.forName(className);
                Constructor<?> cons = c.getConstructor();
                Object newToken = cons.newInstance();
                if (newToken instanceof Token) {
                    ((Token) newToken).setOriginalExpansionSetCode(currentSet);
                    ((Token) newToken).setExpansionSetCodeForImage(currentSet);
                    ((Token) newToken).setTokenType(token.getType());
                    res.add(newToken);
                }
            } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
            // Swallow exception
            }
        }
    }
    // emblems
    List<CardDownloadData> allEmblems = getTokenCardUrls();
    for (CardDownloadData emblem : allEmblems) {
        if (emblem.getSet().equals(currentSet)) {
            try {
                String className = emblem.getName();
                if (emblem.getTokenClassName() != null && emblem.getTokenClassName().length() > 0) {
                    if (emblem.getTokenClassName().toLowerCase(Locale.ENGLISH).matches(".*emblem.*")) {
                        className = emblem.getTokenClassName();
                        className = "mage.game.command.emblems." + className;
                    }
                } else {
                    continue;
                }
                Class<?> c = Class.forName(className);
                Constructor<?> cons = c.getConstructor();
                Object newEmblem = cons.newInstance();
                if (newEmblem instanceof Emblem) {
                    ((Emblem) newEmblem).setExpansionSetCodeForImage(currentSet);
                    res.add(newEmblem);
                }
            } catch (ClassNotFoundException | InvocationTargetException | IllegalArgumentException | IllegalAccessException | InstantiationException | SecurityException | NoSuchMethodException ex) {
            // Swallow exception
            }
        }
    }
    // planes
    List<CardDownloadData> allPlanes = getTokenCardUrls();
    for (CardDownloadData plane : allPlanes) {
        if (plane.getSet().equals(currentSet)) {
            try {
                String className = plane.getName();
                if (plane.getTokenClassName() != null && plane.getTokenClassName().length() > 0) {
                    if (plane.getTokenClassName().toLowerCase(Locale.ENGLISH).matches(".*plane.*")) {
                        className = plane.getTokenClassName();
                        className = "mage.game.command.planes." + className;
                    }
                } else {
                    continue;
                }
                Class<?> c = Class.forName(className);
                Constructor<?> cons = c.getConstructor();
                Object newPlane = cons.newInstance();
                if (newPlane instanceof Plane) {
                    ((Plane) newPlane).setExpansionSetCodeForImage(currentSet);
                    res.add(newPlane);
                }
            } catch (ClassNotFoundException | InvocationTargetException | IllegalArgumentException | IllegalAccessException | InstantiationException | SecurityException | NoSuchMethodException ex) {
            // Swallow exception
            }
        }
    }
    // dungeons
    List<CardDownloadData> allDungeons = getTokenCardUrls();
    for (CardDownloadData dungeon : allDungeons) {
        if (dungeon.getSet().equals(currentSet)) {
            try {
                String className = dungeon.getName();
                if (dungeon.getTokenClassName() != null && dungeon.getTokenClassName().length() > 0) {
                    if (dungeon.getTokenClassName().toLowerCase(Locale.ENGLISH).matches(".*dungeon.*")) {
                        className = dungeon.getTokenClassName();
                        className = "mage.game.command.dungeons." + className;
                    }
                } else {
                    continue;
                }
                Class<?> c = Class.forName(className);
                Constructor<?> cons = c.getConstructor();
                Object newDungeon = cons.newInstance();
                if (newDungeon instanceof Dungeon) {
                    ((Dungeon) newDungeon).setExpansionSetCodeForImage(currentSet);
                    res.add(newDungeon);
                }
            } catch (ClassNotFoundException | InvocationTargetException | IllegalArgumentException | IllegalAccessException | InstantiationException | SecurityException | NoSuchMethodException ex) {
            // Swallow exception
            }
        }
    }
    return res;
}
Also used : CardDownloadData(org.mage.plugins.card.images.CardDownloadData) Plane(mage.game.command.Plane) ArrayList(java.util.ArrayList) PermanentToken(mage.game.permanent.PermanentToken) Token(mage.game.permanent.token.Token) InvocationTargetException(java.lang.reflect.InvocationTargetException) Emblem(mage.game.command.Emblem) Dungeon(mage.game.command.Dungeon)

Aggregations

CardDownloadData (org.mage.plugins.card.images.CardDownloadData)4 Token (mage.game.permanent.token.Token)2 Test (org.junit.Test)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 Dungeon (mage.game.command.Dungeon)1 Emblem (mage.game.command.Emblem)1 Plane (mage.game.command.Plane)1 PermanentToken (mage.game.permanent.PermanentToken)1 TokenImpl (mage.game.permanent.token.TokenImpl)1 TFile (net.java.truevfs.access.TFile)1 Ignore (org.junit.Ignore)1 CardImageSource (org.mage.plugins.card.dl.sources.CardImageSource)1 CardImageUrls (org.mage.plugins.card.dl.sources.CardImageUrls)1 Reflections (org.reflections.Reflections)1