Search in sources :

Example 56 with MagicEdition

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

the class MagicEditionListEditor method getTableCellEditorComponent.

@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
    model.removeAllElements();
    List<MagicEdition> selectedItem = (List<MagicEdition>) table.getValueAt(row, column);
    for (MagicEdition e : selectedItem) model.addElement(e);
    return super.getTableCellEditorComponent(table, model.getSelectedItem(), isSelected, row, column);
}
Also used : MagicEdition(org.magic.api.beans.MagicEdition) List(java.util.List)

Example 57 with MagicEdition

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

the class ScryFallProvider method getSetById.

@Override
public MagicEdition getSetById(String id) throws IOException {
    if (cache.size() > 0) {
        for (MagicEdition ed : cache.values()) if (ed.getId().equalsIgnoreCase(id))
            try {
                return (MagicEdition) BeanUtils.cloneBean(ed);
            } catch (Exception e) {
                throw new IOException(e);
            }
    }
    try {
        JsonReader reader = new JsonReader(new InputStreamReader(getConnection(baseURI + "/sets/" + id.toLowerCase()).getInputStream(), encoding));
        JsonObject root = new JsonParser().parse(reader).getAsJsonObject();
        return generateEdition(root.getAsJsonObject());
    } catch (Exception e) {
        MagicEdition ed = new MagicEdition();
        ed.setId(id);
        ed.setSet(id);
        return ed;
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) MagicEdition(org.magic.api.beans.MagicEdition) JsonReader(com.google.gson.stream.JsonReader) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) JsonParser(com.google.gson.JsonParser)

Example 58 with MagicEdition

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

the class ScryFallProvider method generateCard.

private MagicCard generateCard(JsonObject obj, boolean exact, String search) throws IOException {
    final MagicCard mc = new MagicCard();
    mc.setId(obj.get("id").getAsString());
    mc.setName(obj.get("name").getAsString());
    mc.setCmc(obj.get("cmc").getAsInt());
    mc.setLayout(obj.get("layout").getAsString());
    try {
        mc.setMultiverseid(obj.get("multiverse_ids").getAsJsonArray().get(0).getAsInt());
    } catch (Exception e) {
        logger.error("could not find multiverse_ids " + mc.getName());
    }
    try {
        mc.setText(obj.get("oracle_text").getAsString());
    } catch (NullPointerException e) {
        mc.setText("");
    }
    try {
        mc.setCost(obj.get("mana_cost").getAsString());
    } catch (NullPointerException e) {
        mc.setCost("");
    }
    try {
        mc.setFlavor(obj.get("flavor_text").getAsString());
    } catch (NullPointerException e) {
        mc.setFlavor("");
    }
    if (obj.get("type_line") != null)
        generateTypes(mc, String.valueOf(obj.get("type_line")));
    MagicCardNames n = new MagicCardNames();
    n.setLanguage("English");
    n.setName(mc.getName());
    try {
        n.setGathererId(obj.get("multiverse_id").getAsInt());
    } catch (NullPointerException e) {
        n.setGathererId(0);
    }
    mc.getForeignNames().add(n);
    mc.setNumber(obj.get("collector_number").getAsString());
    try {
        mc.setArtist(obj.get("artist").getAsString());
    } catch (NullPointerException e) {
        logger.trace("artist not found");
    }
    try {
        mc.setReserved(obj.get("reserved").getAsBoolean());
    } catch (NullPointerException e) {
        logger.trace("reserved not found");
    }
    try {
        mc.setPower(obj.get("power").getAsString());
    } catch (NullPointerException e) {
        logger.trace("power not found");
    }
    try {
        mc.setToughness(obj.get("toughness").getAsString());
    } catch (NullPointerException e) {
        logger.trace("toughness not found");
    }
    try {
        mc.setLoyalty(obj.get("loyalty").getAsInt());
    } catch (Exception e) {
        logger.trace("loyalty not found");
    }
    try {
        mc.setWatermarks(obj.get("watermark").getAsString());
    } catch (NullPointerException e) {
        logger.trace("watermark not found");
    }
    try {
        mc.setImageName(obj.get("illustration_id").getAsString());
    } catch (NullPointerException e) {
        logger.trace("illustration_id not found");
    }
    if (obj.get("colors") != null) {
        Iterator<JsonElement> it = obj.get("colors").getAsJsonArray().iterator();
        while (it.hasNext()) mc.getColors().add(ColorParser.getNameByCode(it.next().getAsString()));
    }
    if (obj.get("color_identity") != null) {
        Iterator<JsonElement> it = obj.get("color_identity").getAsJsonArray().iterator();
        while (it.hasNext()) mc.getColorIdentity().add("{" + it.next().getAsString() + "}");
    }
    if (obj.get("legalities") != null) {
        JsonObject legs = obj.get("legalities").getAsJsonObject();
        Iterator<Entry<String, JsonElement>> it = legs.entrySet().iterator();
        while (it.hasNext()) {
            Entry<String, JsonElement> ent = it.next();
            MagicFormat format = new MagicFormat();
            format.setFormat(ent.getKey());
            format.setLegality(ent.getValue().getAsString());
            mc.getLegalities().add(format);
        }
    }
    mc.setTranformable(mc.getLayout().equalsIgnoreCase("transform") || mc.getLayout().equalsIgnoreCase("meld"));
    mc.setFlippable(mc.getLayout().equals("flip"));
    int idface = 0;
    if (mc.getName().contains("//")) {
        String[] names = mc.getName().split(" // ");
        if (exact)
            if (names[0].equals(search)) {
                idface = 0;
            } else {
                idface = 1;
            }
    }
    if (obj.get("card_faces") != null) {
        mc.setText(obj.get("card_faces").getAsJsonArray().get(idface).getAsJsonObject().get("oracle_text").getAsString());
        mc.setCost(obj.get("card_faces").getAsJsonArray().get(idface).getAsJsonObject().get("mana_cost").getAsString());
        mc.setRotatedCardName(obj.get("card_faces").getAsJsonArray().get(1).getAsJsonObject().get("name").getAsString());
        mc.setImageName(obj.get("card_faces").getAsJsonArray().get(idface).getAsJsonObject().get("illustration_id").getAsString());
        generateTypes(mc, obj.get("card_faces").getAsJsonArray().get(idface).getAsJsonObject().get("type_line").getAsString());
        try {
            mc.setMultiverseid(obj.get("multiverse_ids").getAsJsonArray().get(idface).getAsInt());
        } catch (Exception e) {
            logger.error(mc.getName() + " has no multiverseid :" + e);
        }
        try {
            mc.setLoyalty(obj.get("card_faces").getAsJsonArray().get(idface).getAsJsonObject().get("loyalty").getAsInt());
        } catch (Exception e) {
            logger.error(mc.getName() + " has no loyalty: " + e);
        }
        try {
            Iterator<JsonElement> it = obj.get("card_faces").getAsJsonArray().get(idface).getAsJsonObject().get("colors").getAsJsonArray().iterator();
            while (it.hasNext()) mc.getColors().add(ColorParser.getNameByCode(it.next().getAsString()));
        } catch (Exception e) {
            logger.error(mc.getName() + " has no colors: " + e);
        }
        try {
            mc.setPower(obj.get("card_faces").getAsJsonArray().get(idface).getAsJsonObject().get("power").getAsString());
            mc.setToughness(obj.get("card_faces").getAsJsonArray().get(idface).getAsJsonObject().get("toughness").getAsString());
        } catch (Exception e) {
            logger.error(mc.getName() + " has no power/toughness: " + e);
        }
    }
    // meld
    if (obj.get("all_parts") != null) {
        JsonArray arr = obj.get("all_parts").getAsJsonArray();
        int index = -1;
        for (int i = 0; i < arr.size(); i++) {
            if (arr.get(i).getAsJsonObject().get("name").getAsString().equals(mc.getName())) {
                index = i;
                break;
            }
        }
        arr.remove(index);
        if (arr.size() == 1)
            mc.setRotatedCardName(arr.get(0).getAsJsonObject().get("name").getAsString());
    }
    MagicEdition ed;
    try {
        ed = (MagicEdition) BeanUtils.cloneBean(getSetById(obj.get("set").getAsString()));
        ed.setArtist(mc.getArtist());
        if (mc.getMultiverseid() != null)
            ed.setMultiverse_id(String.valueOf(mc.getMultiverseid()));
        ed.setRarity(obj.get("rarity").getAsString());
        ed.setOnlineOnly(obj.get("digital").getAsBoolean());
        ed.setNumber(mc.getNumber());
        mc.getEditions().add(ed);
    } catch (Exception e1) {
        throw new IOException(e1);
    }
    new Thread(() -> {
        try {
            initOtherEdition(mc);
        // generateRules(mc);
        } catch (Exception e) {
            logger.error("error in initOtherEdition :" + e.getMessage());
        }
    }, "other editions").start();
    setChanged();
    notifyObservers(mc);
    return mc;
}
Also used : MagicEdition(org.magic.api.beans.MagicEdition) JsonObject(com.google.gson.JsonObject) MagicFormat(org.magic.api.beans.MagicFormat) IOException(java.io.IOException) MagicCardNames(org.magic.api.beans.MagicCardNames) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) JsonArray(com.google.gson.JsonArray) Entry(java.util.Map.Entry) MagicCard(org.magic.api.beans.MagicCard) JsonElement(com.google.gson.JsonElement)

Example 59 with MagicEdition

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

the class ScryFallProvider method initOtherEdition.

private void initOtherEdition(MagicCard mc) throws IOException {
    String url = baseURI + "/cards/search?q=+" + URLEncoder.encode("++!\"" + mc.getName() + "\"", encoding) + "%20include:extras" + "%20-s:" + mc.getEditions().get(0).getId();
    logger.trace("initOtherEdition " + URLDecoder.decode(url, encoding));
    HttpURLConnection con;
    JsonReader reader;
    boolean hasMore = true;
    while (hasMore) {
        con = (HttpURLConnection) getConnection(url);
        try {
            reader = new JsonReader(new InputStreamReader(con.getInputStream(), encoding));
            JsonElement el = parser.parse(reader);
            JsonArray jsonList = el.getAsJsonObject().getAsJsonArray("data");
            for (int i = 0; i < jsonList.size(); i++) {
                JsonObject obj = jsonList.get(i).getAsJsonObject();
                MagicEdition ed = getSetById(obj.get("set").getAsString());
                if (obj.get("artist") != null)
                    ed.setArtist(obj.get("artist").getAsString());
                if (obj.get("multiverse_id") != null)
                    ed.setMultiverse_id(obj.get("multiverse_id").getAsString());
                if (obj.get("rarity") != null)
                    ed.setRarity(obj.get("rarity").getAsString());
                if (obj.get("collector_number") != null)
                    ed.setNumber(obj.get("collector_number").getAsString());
                mc.getEditions().add(ed);
            }
            hasMore = el.getAsJsonObject().get("has_more").getAsBoolean();
            if (hasMore)
                url = el.getAsJsonObject().get("next_page").getAsString();
            Thread.sleep(50);
        } catch (Exception e) {
            logger.trace(e);
            hasMore = false;
        }
    }
}
Also used : JsonArray(com.google.gson.JsonArray) HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) JsonElement(com.google.gson.JsonElement) MagicEdition(org.magic.api.beans.MagicEdition) JsonReader(com.google.gson.stream.JsonReader) JsonObject(com.google.gson.JsonObject) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 60 with MagicEdition

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

the class DeckstatsDeckSniffer method getDeck.

@Override
public MagicDeck getDeck(RetrievableDeck info) throws IOException {
    // 
    MagicDeck deck = new MagicDeck();
    logger.debug("get deck " + info.getUrl());
    Document d = Jsoup.connect(info.getUrl().toString()).userAgent(getString("USER_AGENT")).timeout(getInt("TIMEOUT")).get();
    deck.setDescription(info.getUrl().toString());
    deck.setName(info.getName());
    deck.setDateCreation(new Date());
    for (Element a : d.select("a.deck_tags_list_tag")) deck.getTags().add(a.text());
    boolean side = false;
    Elements e = d.select("div#deck_overview_cards").select("div.deck_overview_section_chunk");
    for (Element block : e) {
        if (block.hasClass("deck_overview_section_chunk__sideboard"))
            side = true;
        else
            side = false;
        for (Element cont : block.select("div.deck_overview_card_header")) {
            Integer qte = Integer.parseInt(cont.getElementsByClass("deck_overview_card_amount").get(0).text());
            String cardName = cont.getElementsByClass("deck_overview_card_name").get(0).text().trim();
            if (cardName.contains("//"))
                cardName = cardName.substring(0, cardName.indexOf("//")).trim();
            MagicCard mc = null;
            if (cardName.trim().equalsIgnoreCase("Plains") || cardName.trim().equalsIgnoreCase("Island") || cardName.trim().equalsIgnoreCase("Swamp") || cardName.trim().equalsIgnoreCase("Mountain") || cardName.trim().equalsIgnoreCase("Forest")) {
                MagicEdition ed = new MagicEdition();
                ed.setId(MTGControler.getInstance().get("default-land-deck"));
                mc = MTGControler.getInstance().getEnabledProviders().searchCardByCriteria("name", cardName, ed, true).get(0);
            } else {
                mc = MTGControler.getInstance().getEnabledProviders().searchCardByCriteria("name", cardName, null, true).get(0);
            }
            if (side)
                deck.getMapSideBoard().put(mc, qte);
            else
                deck.getMap().put(mc, qte);
        }
    }
    if (d.select("div#deck_overview_info") != null)
        deck.setDescription(d.select("div#deck_overview_info").select("div.deck_text_editable_container").text());
    return deck;
}
Also used : MagicCard(org.magic.api.beans.MagicCard) Element(org.jsoup.nodes.Element) MagicEdition(org.magic.api.beans.MagicEdition) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) Date(java.util.Date) MagicDeck(org.magic.api.beans.MagicDeck)

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