Search in sources :

Example 81 with MagicCard

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

the class JDeckChooserDialog method initTree.

private void initTree() {
    creatureNode.removeAllChildren();
    artifactsNode.removeAllChildren();
    planeswalkerNode.removeAllChildren();
    landsNode.removeAllChildren();
    spellsNode.removeAllChildren();
    sideNode.removeAllChildren();
    if (selectedDeck != null) {
        for (MagicCard mc : selectedDeck.getMap().keySet()) {
            if (mc.getTypes().contains("Creature") && !mc.getTypes().contains("Artifact"))
                creatureNode.add(new DefaultMutableTreeNode(selectedDeck.getMap().get(mc) + " " + mc));
            else if (mc.getTypes().contains("Artifact"))
                artifactsNode.add(new DefaultMutableTreeNode(selectedDeck.getMap().get(mc) + " " + mc));
            else if (mc.getTypes().contains("Land"))
                landsNode.add(new DefaultMutableTreeNode(selectedDeck.getMap().get(mc) + " " + mc));
            else if (mc.getTypes().contains("Planeswalker"))
                planeswalkerNode.add(new DefaultMutableTreeNode(selectedDeck.getMap().get(mc) + " " + mc));
            else
                spellsNode.add(new DefaultMutableTreeNode(selectedDeck.getMap().get(mc) + " " + mc));
        }
        for (MagicCard mc : selectedDeck.getMapSideBoard().keySet()) sideNode.add(new DefaultMutableTreeNode(selectedDeck.getMapSideBoard().get(mc) + " " + mc));
        model.reload();
        expandAll(new TreePath(root));
    }
}
Also used : MagicCard(org.magic.api.beans.MagicCard) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) TreePath(javax.swing.tree.TreePath)

Example 82 with MagicCard

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

the class ManualImportDialog method getAsDeck.

public MagicDeck getAsDeck() {
    if (editorPane.getText().length() == 0)
        return deck;
    String[] line = editorPane.getText().split("\n");
    for (String l : line) {
        int nb = Integer.parseInt(l.substring(0, l.indexOf(' ')));
        String name = l.substring(l.indexOf(' '), l.length());
        try {
            MagicCard mc;
            if (name.trim().equalsIgnoreCase("Plains") || name.trim().equalsIgnoreCase("Island") || name.trim().equalsIgnoreCase("Swamp") || name.trim().equalsIgnoreCase("Mountain") || name.trim().equalsIgnoreCase("Forest")) {
                MagicEdition ed = new MagicEdition();
                ed.setId(MTGControler.getInstance().get("default-land-deck"));
                mc = MTGControler.getInstance().getEnabledProviders().searchCardByCriteria("name", name.trim(), ed, true).get(0);
            } else {
                mc = MTGControler.getInstance().getEnabledProviders().searchCardByCriteria("name", name.trim(), null, true).get(0);
            }
            if (mc != null) {
                deck.getMap().put(mc, nb);
            }
        } catch (Exception e) {
            logger.error(e);
        }
    }
    return deck;
}
Also used : MagicCard(org.magic.api.beans.MagicCard) MagicEdition(org.magic.api.beans.MagicEdition)

Example 83 with MagicCard

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

the class MtgjsonProvider method getCardByNumber.

public MagicCard getCardByNumber(String num, MagicEdition me) throws IOException {
    String jsquery = "$." + me.getId().toUpperCase() + ".cards[?(@.number == '" + num + "')]";
    logger.debug("search " + jsquery);
    try {
        MagicCard mc = search(jsquery, "number", num).get(0);
        mc.getEditions().add(me);
        return mc;
    } catch (Exception e) {
        logger.error(e);
        return null;
    }
}
Also used : MagicCard(org.magic.api.beans.MagicCard) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException)

Example 84 with MagicCard

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

the class ScryFallProvider method generateBooster.

@Override
public Booster generateBooster(MagicEdition me) throws IOException {
    List<MagicCard> ret = new ArrayList<>();
    List<MagicCard> common = new ArrayList<>();
    List<MagicCard> uncommon = new ArrayList<>();
    List<MagicCard> rare = new ArrayList<>();
    if (cachedCardEds.get(me.getId()) == null)
        cachedCardEds.put(me.getId(), searchCardByCriteria("set", me.getId(), null, true));
    for (MagicCard mc : cachedCardEds.get(me.getId())) {
        if (mc.getEditions().get(0).getRarity().equalsIgnoreCase("common"))
            common.add(mc);
        if (mc.getEditions().get(0).getRarity().equalsIgnoreCase("uncommon"))
            uncommon.add(mc);
        if (mc.getEditions().get(0).getRarity().toLowerCase().contains("rare"))
            rare.add(mc);
    }
    Collections.shuffle(common);
    Collections.shuffle(uncommon);
    Collections.shuffle(rare);
    ret.addAll(common.subList(0, 11));
    ret.addAll(uncommon.subList(0, 3));
    ret.add(rare.get(0));
    Booster b = new Booster();
    b.setEdition(me);
    b.setCards(ret);
    return b;
}
Also used : MagicCard(org.magic.api.beans.MagicCard) Booster(org.magic.api.beans.Booster) ArrayList(java.util.ArrayList)

Example 85 with MagicCard

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

MagicCard (org.magic.api.beans.MagicCard)106 MagicEdition (org.magic.api.beans.MagicEdition)40 ArrayList (java.util.ArrayList)31 IOException (java.io.IOException)28 MagicDeck (org.magic.api.beans.MagicDeck)21 MagicCollection (org.magic.api.beans.MagicCollection)14 SQLException (java.sql.SQLException)12 Date (java.util.Date)11 MagicCardStock (org.magic.api.beans.MagicCardStock)10 DisplayableCard (org.magic.game.gui.components.DisplayableCard)10 JsonArray (com.google.gson.JsonArray)8 List (java.util.List)8 FileReader (java.io.FileReader)7 Before (org.junit.Before)7 BufferedReader (java.io.BufferedReader)6 File (java.io.File)6 FileWriter (java.io.FileWriter)6 MalformedURLException (java.net.MalformedURLException)6 PreparedStatement (java.sql.PreparedStatement)6 ResultSet (java.sql.ResultSet)6