Search in sources :

Example 16 with MagicEdition

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

the class CockatriceTokenProvider method generateTokenFor.

@Override
public MagicCard generateTokenFor(MagicCard mc) {
    String expression = "//card[reverse-related=\"" + mc.getName() + "\"][not(contains(name,'emblem'))]";
    try {
        NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET);
        Element value = (Element) nodeList.item(0);
        MagicCard tok = new MagicCard();
        tok.setLayout(MagicCard.LAYOUT.Token.toString());
        tok.setCmc(0);
        tok.setName(value.getElementsByTagName("name").item(0).getTextContent());
        if (value.getElementsByTagName("color").item(0) != null) {
            tok.getColors().add(ColorParser.getNameByCode(value.getElementsByTagName("color").item(0).getTextContent()));
            tok.getColorIdentity().add("{" + value.getElementsByTagName("color").item(0).getTextContent() + "}");
        }
        String types = value.getElementsByTagName("type").item(0).getTextContent();
        if (types.toLowerCase().contains("legendary"))
            tok.getSupertypes().add("Legendary");
        if (types.toLowerCase().contains("artifact"))
            tok.getTypes().add("Artifact");
        if (types.toLowerCase().contains("creature"))
            tok.getTypes().add("Creature");
        tok.getTypes().add(MagicCard.LAYOUT.Token.toString());
        tok.getSubtypes().add(types.substring(types.indexOf("\u2014") + 1));
        if (value.getElementsByTagName("pt").item(0) != null) {
            tok.setPower(value.getElementsByTagName("pt").item(0).getTextContent().substring(0, value.getElementsByTagName("pt").item(0).getTextContent().indexOf('/')).trim());
            tok.setToughness(value.getElementsByTagName("pt").item(0).getTextContent().substring(value.getElementsByTagName("pt").item(0).getTextContent().indexOf('/') + 1).trim());
        }
        if (value.getElementsByTagName("text").item(0) != null)
            tok.setText(value.getElementsByTagName("text").item(0).getTextContent());
        tok.getEditions().add(mc.getEditions().get(0));
        tok.setNumber("T");
        NodeList sets = value.getElementsByTagName("set");
        for (int s = 0; s < sets.getLength(); s++) {
            String idSet = sets.item(s).getTextContent();
            if (idSet.equals(mc.getEditions().get(0).getId())) {
                MagicEdition ed = MTGControler.getInstance().getEnabledProviders().getSetById(idSet);
                tok.getEditions().add(ed);
            }
        }
        tok.setId(DigestUtils.sha1Hex(tok.getEditions().get(0).getId() + tok.getName()));
        return tok;
    } catch (XPathExpressionException e) {
        logger.error("erreur generate token for" + mc, e);
        return null;
    } catch (IOException e) {
        logger.error("getSetById error " + mc, e);
        return null;
    }
}
Also used : MagicCard(org.magic.api.beans.MagicCard) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) MagicEdition(org.magic.api.beans.MagicEdition) IOException(java.io.IOException)

Example 17 with MagicEdition

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

the class DeckbrewProvider method loadEditions.

public List<MagicEdition> loadEditions() throws IOException {
    String url = urldeckbrewJSON + "/sets";
    JsonReader reader = new JsonReader(new InputStreamReader(new URL(url).openStream()));
    JsonArray root = new JsonParser().parse(reader).getAsJsonArray();
    if (list.isEmpty())
        for (int i = 0; i < root.size(); i++) {
            JsonObject e = root.get(i).getAsJsonObject();
            MagicEdition ed = getSetById(e.get("id").getAsString());
            list.add(ed);
        }
    return list;
}
Also used : JsonArray(com.google.gson.JsonArray) InputStreamReader(java.io.InputStreamReader) MagicEdition(org.magic.api.beans.MagicEdition) JsonReader(com.google.gson.stream.JsonReader) JsonObject(com.google.gson.JsonObject) URL(java.net.URL) JsonParser(com.google.gson.JsonParser)

Example 18 with MagicEdition

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

the class DeckbrewProvider method searchCardByCriteria.

public List<MagicCard> searchCardByCriteria(String att, String crit, MagicEdition me, boolean exact) throws IOException {
    crit = att + "=" + URLEncoder.encode(crit, encoding);
    String url = urldeckbrewJSON + "/cards?" + crit;
    Reader reader = new InputStreamReader(new URL(url).openStream(), encoding);
    List<MagicCard> retour = new ArrayList<>();
    JsonArray root = new JsonParser().parse(reader).getAsJsonArray();
    int page = 1;
    boolean gonextpage = true;
    while (gonextpage) {
        String pagination;
        if (crit == null)
            pagination = "?";
        else
            pagination = "&";
        URL u = null;
        if (root.size() == 100) {
            u = new URL(url + pagination + "page=" + page++);
            gonextpage = true;
        } else {
            u = new URL(url);
            gonextpage = false;
        }
        logger.info("Connexion to " + u);
        reader = new InputStreamReader(u.openStream(), encoding);
        root = new JsonParser().parse(reader).getAsJsonArray();
        for (int i = 0; i < root.size(); i++) {
            JsonObject e = root.get(i).getAsJsonObject();
            MagicCard mc = new MagicCard();
            if (e.get("name") != null)
                mc.setName(e.get("name").getAsString());
            if (e.get("id") != null)
                mc.setId(e.get("id").getAsString());
            if (e.get("cmc") != null)
                mc.setCmc(e.get("cmc").getAsInt());
            if (e.get("cost") != null)
                mc.setCost(e.get("cost").getAsString());
            if (e.get("text") != null)
                mc.setText(e.get("text").getAsString());
            if (e.get("power") != null)
                mc.setPower(e.get("power").getAsString());
            if (e.get("toughness") != null)
                mc.setToughness(e.get("toughness").getAsString());
            if (e.get("types") != null) {
                Iterator<JsonElement> it = e.get("types").getAsJsonArray().iterator();
                while (it.hasNext()) {
                    mc.getTypes().add(it.next().getAsString());
                }
            }
            if (e.get("subtypes") != null) {
                Iterator<JsonElement> it2 = e.get("subtypes").getAsJsonArray().iterator();
                while (it2.hasNext()) {
                    mc.getSubtypes().add(it2.next().getAsString());
                }
            }
            if (e.get("colors") != null) {
                Iterator<JsonElement> it3 = e.get("colors").getAsJsonArray().iterator();
                while (it3.hasNext()) {
                    mc.getColors().add(it3.next().getAsString());
                }
            }
            JsonArray editions = e.getAsJsonArray("editions");
            for (int j = 0; j < editions.size(); j++) {
                JsonObject obj = editions.get(j).getAsJsonObject();
                MagicEdition ed = getSetById(obj.get("set_id").getAsString());
                ed.setArtist(obj.get("artist").getAsString());
                ed.setMultiverse_id(obj.get("multiverse_id").getAsString());
                ed.setRarity(obj.get("rarity").getAsString());
                ed.setNumber(obj.get("number").getAsString());
                MagicCardNames name = new MagicCardNames();
                name.setName(mc.getName());
                name.setLanguage("English");
                name.setGathererId(Integer.parseInt(ed.getMultiverse_id()));
                mc.getForeignNames().add(name);
                mc.setLayout(obj.get("layout").getAsString());
                mc.getEditions().add(ed);
                mc.setNumber(ed.getNumber());
            }
            if (e.get("formats") != null) {
                JsonObject obj = e.get("formats").getAsJsonObject();
                for (Entry<String, JsonElement> k : obj.entrySet()) {
                    MagicFormat format = new MagicFormat();
                    format.setFormat(k.getKey());
                    format.setLegality(k.getValue().getAsString());
                    mc.getLegalities().add(format);
                }
            }
            setChanged();
            notifyObservers(mc);
            retour.add(mc);
        }
    }
    return retour;
}
Also used : InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) MagicEdition(org.magic.api.beans.MagicEdition) JsonReader(com.google.gson.stream.JsonReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) JsonObject(com.google.gson.JsonObject) MagicFormat(org.magic.api.beans.MagicFormat) MagicCardNames(org.magic.api.beans.MagicCardNames) URL(java.net.URL) JsonArray(com.google.gson.JsonArray) MagicCard(org.magic.api.beans.MagicCard) JsonElement(com.google.gson.JsonElement) JsonParser(com.google.gson.JsonParser)

Example 19 with MagicEdition

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

the class MagicTheGatheringIOProvider method generateCard.

private MagicCard generateCard(JsonObject obj) throws IOException {
    MagicCard mc = new MagicCard();
    if (obj.get("name") != null)
        mc.setName(obj.get("name").getAsString());
    if (obj.get("manaCost") != null)
        mc.setCost(obj.get("manaCost").getAsString());
    if (obj.get("text") != null)
        mc.setText(obj.get("text").getAsString());
    if (obj.get("originalText") != null)
        mc.setOriginalText(obj.get("originalText").getAsString());
    if (obj.get("id") != null)
        mc.setId(obj.get("id").getAsString());
    if (obj.get("artist") != null)
        mc.setArtist(obj.get("artist").getAsString());
    if (obj.get("cmc") != null)
        mc.setCmc(obj.get("cmc").getAsInt());
    if (obj.get("layout") != null)
        mc.setLayout(obj.get("layout").getAsString());
    if (obj.get("number") != null)
        mc.setNumber(obj.get("number").getAsString());
    if (obj.get("power") != null)
        mc.setPower(obj.get("power").getAsString());
    if (obj.get("toughness") != null)
        mc.setToughness(obj.get("toughness").getAsString());
    if (obj.get("loyalty") != null)
        mc.setLoyalty(obj.get("loyalty").getAsInt());
    if (obj.get("mciNumber") != null)
        mc.setMciNumber(String.valueOf(obj.get("mciNumber")));
    if (obj.get("colors") != null) {
        Iterator<JsonElement> it = obj.get("colors").getAsJsonArray().iterator();
        while (it.hasNext()) mc.getColors().add(it.next().getAsString());
    }
    if (obj.get("types") != null) {
        Iterator<JsonElement> it = obj.get("types").getAsJsonArray().iterator();
        while (it.hasNext()) mc.getTypes().add(it.next().getAsString());
    }
    if (obj.get("supertypes") != null) {
        Iterator<JsonElement> it = obj.get("supertypes").getAsJsonArray().iterator();
        while (it.hasNext()) mc.getSupertypes().add(it.next().getAsString());
    }
    if (obj.get("subtypes") != null) {
        Iterator<JsonElement> it = obj.get("subtypes").getAsJsonArray().iterator();
        while (it.hasNext()) mc.getSubtypes().add(it.next().getAsString());
    }
    if (obj.get("legalities") != null) {
        JsonArray arr = obj.get("legalities").getAsJsonArray();
        for (int i = 0; i < arr.size(); i++) {
            JsonObject k = arr.get(i).getAsJsonObject();
            MagicFormat format = new MagicFormat();
            format.setFormat(k.get("format").getAsString());
            format.setLegality(k.get("legality").getAsString());
            mc.getLegalities().add(format);
        }
    }
    if (obj.get("rulings") != null) {
        JsonArray arr = obj.get("rulings").getAsJsonArray();
        for (int i = 0; i < arr.size(); i++) {
            JsonObject k = arr.get(i).getAsJsonObject();
            MagicRuling rule = new MagicRuling();
            rule.setDate(k.get("date").getAsString());
            rule.setText(k.get("text").getAsString());
            mc.getRulings().add(rule);
        }
    }
    if (obj.get("names") != null) {
        JsonArray arr = obj.get("names").getAsJsonArray();
        List<String> list = new ArrayList<>();
        for (int i = 0; i < arr.size(); list.add(arr.get(i++).getAsString())) {
        // TODO complete this function
        }
        list.remove(mc.getName());
        String rotateName = (list.get(list.size() - 1));
        mc.setRotatedCardName(rotateName);
        if (mc.getLayout().equals("flip"))
            mc.setFlippable(true);
        if (mc.getLayout().equals("double-faced") || mc.getLayout().equals("meld"))
            mc.setTranformable(true);
    }
    String currentSet = obj.get("set").getAsString();
    MagicEdition currentEd = getSetById(currentSet);
    if (obj.get("multiverseid") != null)
        currentEd.setMultiverse_id(obj.get("multiverseid").getAsString());
    if (obj.get("rarity") != null)
        currentEd.setRarity(obj.get("rarity").getAsString());
    currentEd.setNumber(mc.getNumber());
    mc.getEditions().add(0, currentEd);
    if (obj.get("printings") != null) {
        JsonArray arr = obj.get("printings").getAsJsonArray();
        for (int i = 0; i < arr.size(); i++) {
            String k = arr.get(i).getAsString();
            if (!k.equals(currentSet)) {
                MagicEdition ed = getSetById(k);
                initOtherEdVariable(mc, ed);
                mc.getEditions().add(ed);
            }
        }
    }
    MagicCardNames defaultMcn = new MagicCardNames();
    defaultMcn.setName(mc.getName());
    defaultMcn.setLanguage("English");
    try {
        defaultMcn.setGathererId(Integer.parseInt(currentEd.getMultiverse_id()));
    } catch (Exception e) {
        defaultMcn.setGathererId(0);
    }
    mc.getForeignNames().add(defaultMcn);
    if (obj.get("foreignNames") != null) {
        JsonArray arr = obj.get("foreignNames").getAsJsonArray();
        for (int i = 0; i < arr.size(); i++) {
            JsonObject lang = arr.get(i).getAsJsonObject();
            MagicCardNames mcn = new MagicCardNames();
            mcn.setName(lang.get("name").getAsString());
            mcn.setLanguage(lang.get("language").getAsString());
            if (lang.get("multiverseid") != null)
                mcn.setGathererId(lang.get("multiverseid").getAsInt());
            mc.getForeignNames().add(mcn);
        }
    }
    setChanged();
    notifyObservers(mc);
    return mc;
}
Also used : ArrayList(java.util.ArrayList) MagicEdition(org.magic.api.beans.MagicEdition) JsonObject(com.google.gson.JsonObject) MagicFormat(org.magic.api.beans.MagicFormat) MagicCardNames(org.magic.api.beans.MagicCardNames) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) JsonArray(com.google.gson.JsonArray) MagicCard(org.magic.api.beans.MagicCard) JsonElement(com.google.gson.JsonElement) MagicRuling(org.magic.api.beans.MagicRuling)

Example 20 with MagicEdition

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

the class MagicTheGatheringIOProvider method loadEditions.

@Override
public List<MagicEdition> loadEditions() throws IOException {
    if (cache.size() == 0) {
        String url = jsonUrl + "/sets";
        String rootKey = "sets";
        logger.info("connect to " + url);
        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(rootKey).getAsJsonArray().size(); i++) {
            JsonObject e = root.get(rootKey).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) 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