Search in sources :

Example 6 with MagicCardNames

use of org.magic.api.beans.MagicCardNames 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)

Aggregations

MagicCardNames (org.magic.api.beans.MagicCardNames)6 MagicEdition (org.magic.api.beans.MagicEdition)6 MagicCard (org.magic.api.beans.MagicCard)5 ArrayList (java.util.ArrayList)4 MagicFormat (org.magic.api.beans.MagicFormat)4 JsonArray (com.google.gson.JsonArray)3 JsonElement (com.google.gson.JsonElement)3 JsonObject (com.google.gson.JsonObject)3 IOException (java.io.IOException)3 MalformedURLException (java.net.MalformedURLException)3 MagicRuling (org.magic.api.beans.MagicRuling)3 File (java.io.File)2 InputStreamReader (java.io.InputStreamReader)2 URL (java.net.URL)2 List (java.util.List)2 JsonParser (com.google.gson.JsonParser)1 JsonReader (com.google.gson.stream.JsonReader)1 Configuration (com.jayway.jsonpath.Configuration)1 JsonPath (com.jayway.jsonpath.JsonPath)1 Option (com.jayway.jsonpath.Option)1