Search in sources :

Example 11 with MagicEdition

use of org.magic.api.beans.MagicEdition in project MtgDesktopCompanion by nicho92.

the class XMageDeckExport method importDeck.

@Override
public MagicDeck importDeck(File f) throws IOException {
    try (BufferedReader read = new BufferedReader(new FileReader(f))) {
        MagicDeck deck = new MagicDeck();
        deck.setName(f.getName().substring(0, f.getName().indexOf('.')));
        String line = read.readLine();
        while (line != null) {
            if (!line.startsWith("NAME:")) {
                if (!line.startsWith("SB:")) {
                    MagicEdition ed = new MagicEdition();
                    ed.setId(line.substring(line.indexOf('[') + 1, line.indexOf(':')));
                    String cardName = line.substring(line.indexOf(']') + 1, line.length()).trim();
                    int qte = Integer.parseInt(line.substring(0, line.indexOf('[')).trim());
                    MagicCard mc = MTGControler.getInstance().getEnabledProviders().searchCardByCriteria("name", cardName, ed, true).get(0);
                    deck.getMap().put(mc, qte);
                } else {
                    line = line.replace("SB:", "").trim();
                    MagicEdition ed = new MagicEdition();
                    ed.setId(line.substring(line.indexOf('[') + 1, line.indexOf(':')));
                    String cardName = line.substring(line.indexOf(']') + 1, line.length()).trim();
                    int qte = Integer.parseInt(line.substring(0, line.indexOf('[')).trim());
                    MagicCard mc = MTGControler.getInstance().getEnabledProviders().searchCardByCriteria("name", cardName, ed, true).get(0);
                    deck.getMap().put(mc, qte);
                }
            } else {
                deck.setName(line.replaceAll("NAME: ", ""));
            }
            line = read.readLine();
        }
        return deck;
    }
}
Also used : MagicCard(org.magic.api.beans.MagicCard) BufferedReader(java.io.BufferedReader) MagicEdition(org.magic.api.beans.MagicEdition) FileReader(java.io.FileReader) MagicDeck(org.magic.api.beans.MagicDeck)

Example 12 with MagicEdition

use of org.magic.api.beans.MagicEdition in project MtgDesktopCompanion by nicho92.

the class MtgjsonProvider method getSetById.

public MagicEdition getSetById(String id) {
    MagicEdition me = new MagicEdition();
    if (!id.substring(0, 1).equals("p"))
        id = id.toUpperCase();
    me.setId(id);
    try {
        me.setSet(ctx.read("$." + id + ".name", String.class));
        me.setReleaseDate(ctx.read("$." + id + ".releaseDate", String.class));
        me.setBorder(ctx.read("$." + id + ".border", String.class));
        me.setType(ctx.read("$." + id + ".type", String.class));
        if (me.getCardCount() == 0)
            // long !
            me.setCardCount(ctx.read("$." + id + ".cards", List.class).size());
    } catch (PathNotFoundException pnfe) {
        me.setSet(id);
        me.setReleaseDate("");
        me.setBorder("");
        me.setType("unknow");
        me.setCardCount(0);
    }
    try {
        me.setGathererCode(ctx.read("$." + id + ".gathererCode", String.class));
    } catch (Exception e) {
        MTGLogger.printStackTrace(e);
    }
    try {
        me.setMagicCardsInfoCode(ctx.read("$." + id + ".magicCardsInfoCode", String.class));
    } catch (Exception e) {
        MTGLogger.printStackTrace(e);
    }
    try {
        me.setMkm_id(ctx.read("$." + id + ".mkm_id", Integer.class));
        me.setMkm_name(ctx.read("$." + id + ".mkm_name", String.class));
    } catch (Exception e) {
        MTGLogger.printStackTrace(e);
    }
    try {
        me.setOnlineOnly(ctx.read("$." + id + ".onlineOnly", Boolean.class));
    } catch (Exception e) {
        me.setOnlineOnly(false);
    }
    try {
        me.setBooster(ctx.read("$." + id + ".booster", List.class));
    } catch (PathNotFoundException e) {
        MTGLogger.printStackTrace(e);
    }
    try {
        me.setBlock(ctx.read("$." + id + ".block", String.class));
    } catch (PathNotFoundException e) {
        MTGLogger.printStackTrace(e);
    }
    try {
        me.setTranslations(ctx.read("$." + id + ".translations", Map.class));
    } catch (PathNotFoundException e) {
        MTGLogger.printStackTrace(e);
    }
    return me;
}
Also used : MagicEdition(org.magic.api.beans.MagicEdition) ArrayList(java.util.ArrayList) List(java.util.List) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException) HashMap(java.util.HashMap) Map(java.util.Map) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException)

Example 13 with MagicEdition

use of org.magic.api.beans.MagicEdition in project MtgDesktopCompanion by nicho92.

the class MtgjsonProvider method search.

private List<MagicCard> search(String jsquery, String att, String crit) {
    currentSet = new ArrayList<>();
    List<MagicCard> listCards = new ArrayList<>();
    logger.debug("searchCardByCriteria : " + jsquery);
    List<Map<String, Object>> cardsElement = ctx.withListeners(fr -> {
        if (fr.path().startsWith("$")) {
            currentSet.add(fr.path().substring(fr.path().indexOf("$[") + 3, fr.path().indexOf("]") - 1));
        }
        return null;
    }).read(jsquery, List.class);
    int indexSet = 0;
    for (Map<String, Object> map : cardsElement) {
        MagicCard mc = new MagicCard();
        if (map.get("name") != null)
            mc.setName(map.get("name").toString());
        mc.setFlippable(false);
        mc.setTranformable(false);
        if (map.get("multiverseid") != null)
            mc.setMultiverseid((int) (double) map.get("multiverseid"));
        mc.setId(String.valueOf(map.get("id")));
        mc.setText(String.valueOf(map.get("text")));
        if (map.get("cmc") != null)
            mc.setCmc((int) Double.parseDouble(String.valueOf(map.get("cmc"))));
        mc.setPower(String.valueOf(map.get("power")));
        mc.setToughness(String.valueOf(map.get("toughness")));
        mc.setFlavor(String.valueOf(map.get("flavor")));
        mc.setArtist(String.valueOf(map.get("artist")));
        mc.setLayout(String.valueOf(map.get("layout")));
        if (map.get("mciNumber") != null)
            mc.setMciNumber(String.valueOf(map.get("mciNumber")));
        if (map.get("originalText") != null)
            mc.setOriginalText(String.valueOf(map.get("originalText")));
        if (map.get("originalType") != null)
            mc.setOriginalType(String.valueOf(map.get("originalType")));
        if (map.get("supertypes") != null)
            mc.getSupertypes().addAll((List<String>) map.get("supertypes"));
        if (map.get("types") != null)
            mc.getTypes().addAll((List<String>) map.get("types"));
        if (map.get("subtypes") != null)
            mc.getSubtypes().addAll((List<String>) map.get("subtypes"));
        if (map.get("variations") != null)
            mc.getVariations().addAll((List<Integer>) map.get("variations"));
        if (map.get("colors") != null)
            mc.getColors().addAll((List<String>) map.get("colors"));
        if (map.get("colorIdentity") != null)
            mc.getColorIdentity().addAll((List<String>) map.get("colorIdentity"));
        if (map.get("watermark") != null)
            mc.setWatermarks(String.valueOf(map.get("watermark")));
        if (map.get("number") != null)
            mc.setNumber(String.valueOf(map.get("number")));
        if (map.get("gathererCode") != null)
            mc.setGathererCode(String.valueOf(map.get("gathererCode")));
        if (map.get("reserved") != null)
            mc.setReserved(Boolean.valueOf(String.valueOf(map.get("reserved"))));
        if (map.get("loyalty") != null) {
            try {
                mc.setLoyalty((int) Double.parseDouble(map.get("loyalty").toString()));
            } catch (Exception e) {
                mc.setLoyalty(0);
            }
        }
        if (map.get("manaCost") != null)
            mc.setCost(String.valueOf(map.get("manaCost")));
        else
            mc.setCost("");
        if (map.get("legalities") != null) {
            for (Map<String, Object> mapFormats : (List<Map>) map.get("legalities")) {
                MagicFormat mf = new MagicFormat();
                mf.setFormat(String.valueOf(mapFormats.get("format")));
                mf.setLegality(String.valueOf(mapFormats.get("legality")));
                mc.getLegalities().add(mf);
            }
        }
        if (map.get("rulings") != null) {
            for (Map<String, Object> mapRules : (List<Map>) map.get("rulings")) {
                MagicRuling mr = new MagicRuling();
                mr.setDate(String.valueOf(mapRules.get("date")));
                mr.setText(String.valueOf(mapRules.get("text")));
                mc.getRulings().add(mr);
            }
        }
        String codeEd;
        if (currentSet.size() == 1)
            codeEd = currentSet.get(0);
        else
            codeEd = currentSet.get(indexSet++);
        MagicEdition me = getSetById(codeEd);
        me.setRarity(String.valueOf(map.get("rarity")));
        me.setNumber(mc.getNumber());
        if (mc.getMultiverseid() != null)
            me.setMultiverse_id(String.valueOf(mc.getMultiverseid()));
        mc.getEditions().add(me);
        if (// mtgjson give null text for basic Land. Modify it for adding mana color
        me.getRarity().equals("Basic Land")) {
            mc.setText(mc.getOriginalText());
            switch(mc.getName()) {
                case "Plains":
                    mc.setText("{T} : Add {W} to your mana pool");
                    break;
                case "Island":
                    mc.setText("{T} : Add {U} to your mana pool");
                    break;
                case "Swamp":
                    mc.setText("{T} : Add {B} to your mana pool");
                    break;
                case "Mountain":
                    mc.setText("{T} : Add {R} to your mana pool");
                    break;
                case "Forest":
                    mc.setText("{T} : Add {G} to your mana pool");
                    break;
                default:
                    break;
            }
        }
        /*get other sets*/
        if (// too much elements, so, remove all re-printings information for basic lands
        !me.getRarity().equals("Basic Land") && map.get("printings") != null) {
            for (String print : (List<String>) map.get("printings")) {
                if (!print.equalsIgnoreCase(codeEd)) {
                    MagicEdition meO = getSetById(print);
                    if (mc.getMultiverseid() == null)
                        meO.setMultiverse_id(String.valueOf(0));
                    initOtherEditionCardsVar(mc, meO);
                    mc.getEditions().add(meO);
                }
            }
        }
        MagicCardNames defnames = new MagicCardNames();
        defnames.setLanguage("English");
        defnames.setName(mc.getName());
        if (mc.getMultiverseid() != null)
            defnames.setGathererId(mc.getMultiverseid());
        mc.getForeignNames().add(defnames);
        if (map.get("foreignNames") != null) {
            for (Map<String, Object> mapNames : (List<Map>) map.get("foreignNames")) {
                MagicCardNames fnames = new MagicCardNames();
                fnames.setLanguage(String.valueOf(mapNames.get("language")));
                fnames.setName(String.valueOf(mapNames.get("name")));
                if (mapNames.get("multiverseid") != null)
                    fnames.setGathererId((int) (double) mapNames.get("multiverseid"));
                mc.getForeignNames().add(fnames);
            }
        }
        if (map.get("names") != null) {
            ((List) map.get("names")).remove(mc.getName());
            String rotateName = ((List) map.get("names")).get(((List) map.get("names")).size() - 1).toString();
            mc.setRotatedCardName(rotateName);
            if (mc.getLayout().equals("flip"))
                mc.setFlippable(true);
        }
        if (mc.getLayout().equals("double-faced") || mc.getLayout().equals("meld"))
            mc.setTranformable(true);
        setChanged();
        notifyObservers(mc);
        listCards.add(mc);
    }
    currentSet.clear();
    if (att.equalsIgnoreCase("set"))
        cachedCardEds.put(crit, listCards);
    return listCards;
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) URL(java.net.URL) HashMap(java.util.HashMap) MagicCardNames(org.magic.api.beans.MagicCardNames) JsonProvider(com.jayway.jsonpath.spi.json.JsonProvider) ArrayList(java.util.ArrayList) ReadContext(com.jayway.jsonpath.ReadContext) AbstractCardsProvider(org.magic.api.interfaces.abstracts.AbstractCardsProvider) URLConnection(java.net.URLConnection) Configuration(com.jayway.jsonpath.Configuration) Map(java.util.Map) CacheProvider(com.jayway.jsonpath.spi.cache.CacheProvider) ZipEntry(java.util.zip.ZipEntry) Booster(org.magic.api.beans.Booster) EnumSet(java.util.EnumSet) MTGLogger(org.magic.services.MTGLogger) GsonJsonProvider(com.jayway.jsonpath.spi.json.GsonJsonProvider) MalformedURLException(java.net.MalformedURLException) GsonMappingProvider(com.jayway.jsonpath.spi.mapper.GsonMappingProvider) FileOutputStream(java.io.FileOutputStream) Set(java.util.Set) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) FileInputStream(java.io.FileInputStream) Option(com.jayway.jsonpath.Option) JsonPath(com.jayway.jsonpath.JsonPath) InputStreamReader(java.io.InputStreamReader) File(java.io.File) List(java.util.List) MagicEdition(org.magic.api.beans.MagicEdition) MagicRuling(org.magic.api.beans.MagicRuling) LRUCache(com.jayway.jsonpath.spi.cache.LRUCache) MappingProvider(com.jayway.jsonpath.spi.mapper.MappingProvider) MagicFormat(org.magic.api.beans.MagicFormat) MagicCard(org.magic.api.beans.MagicCard) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException) Collections(java.util.Collections) InputStream(java.io.InputStream) MTGConstants(org.magic.services.MTGConstants) ArrayList(java.util.ArrayList) MagicEdition(org.magic.api.beans.MagicEdition) MagicFormat(org.magic.api.beans.MagicFormat) MagicCardNames(org.magic.api.beans.MagicCardNames) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException) MagicCard(org.magic.api.beans.MagicCard) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) MagicRuling(org.magic.api.beans.MagicRuling)

Example 14 with MagicEdition

use of org.magic.api.beans.MagicEdition in project MtgDesktopCompanion by nicho92.

the class ScryFallProvider method generateEdition.

private MagicEdition generateEdition(JsonObject obj) {
    MagicEdition ed = new MagicEdition();
    ed.setId(obj.get("code").getAsString());
    ed.setSet(obj.get("name").getAsString());
    ed.setType(obj.get("set_type").getAsString());
    if (obj.get("digital") != null)
        ed.setOnlineOnly(obj.get("digital").getAsBoolean());
    if (obj.get("border") != null)
        ed.setBorder(obj.get("border").getAsString());
    ed.setCardCount(obj.get("card_count").getAsInt());
    if (obj.get("block") != null)
        ed.setBlock(obj.get("block").getAsString());
    if (obj.get("released_at") != null)
        ed.setReleaseDate(obj.get("released_at").getAsString());
    return ed;
}
Also used : MagicEdition(org.magic.api.beans.MagicEdition)

Example 15 with MagicEdition

use of org.magic.api.beans.MagicEdition in project MtgDesktopCompanion by nicho92.

the class ScryFallProvider method loadEditions.

@Override
public List<MagicEdition> loadEditions() throws IOException {
    if (cache.size() <= 0) {
        String url = baseURI + "/sets";
        URLConnection con = getConnection(url);
        JsonReader reader = new JsonReader(new InputStreamReader(con.getInputStream(), encoding));
        JsonObject root = new JsonParser().parse(reader).getAsJsonObject();
        for (int i = 0; i < root.get("data").getAsJsonArray().size(); i++) {
            JsonObject e = root.get("data").getAsJsonArray().get(i).getAsJsonObject();
            MagicEdition ed = generateEdition(e.getAsJsonObject());
            cache.put(ed.getId(), ed);
        }
    }
    return new ArrayList<>(cache.values());
}
Also used : InputStreamReader(java.io.InputStreamReader) MagicEdition(org.magic.api.beans.MagicEdition) ArrayList(java.util.ArrayList) JsonReader(com.google.gson.stream.JsonReader) JsonObject(com.google.gson.JsonObject) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) JsonParser(com.google.gson.JsonParser)

Aggregations

MagicEdition (org.magic.api.beans.MagicEdition)67 MagicCard (org.magic.api.beans.MagicCard)38 IOException (java.io.IOException)24 ArrayList (java.util.ArrayList)18 MagicCollection (org.magic.api.beans.MagicCollection)11 MagicDeck (org.magic.api.beans.MagicDeck)10 JsonObject (com.google.gson.JsonObject)9 InputStreamReader (java.io.InputStreamReader)9 JsonReader (com.google.gson.stream.JsonReader)8 List (java.util.List)8 JsonParser (com.google.gson.JsonParser)7 MalformedURLException (java.net.MalformedURLException)7 SQLException (java.sql.SQLException)7 Date (java.util.Date)7 JLabel (javax.swing.JLabel)7 Before (org.junit.Before)7 JsonArray (com.google.gson.JsonArray)6 BufferedImage (java.awt.image.BufferedImage)6 URL (java.net.URL)6 Map (java.util.Map)6