Search in sources :

Example 41 with MagicCard

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

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

the class Search method run.

@Override
public void run(String[] args, IoSession session, MTGConsoleHandler mtgConsoleHandler) throws ParseException, ClassNotFoundException, InstantiationException, IllegalAccessException, IOException {
    CommandLine cl = parser.parse(opts, args);
    this.session = session;
    if (cl.hasOption("c")) {
        String att = cl.getOptionValue("c").split("=")[0];
        String val = cl.getOptionValue("c").split("=")[1];
        List<MagicCard> list = MTGControler.getInstance().getEnabledProviders().searchCardByCriteria(att, val, null, false);
        session.write(showList(list, Arrays.asList(MTGConsoleHandler.getAttCards())));
    }
    if (cl.hasOption("s")) {
        List<MagicEdition> list = MTGControler.getInstance().getEnabledProviders().loadEditions();
        session.write(showList(list, Arrays.asList(MTGConsoleHandler.getAttSet())));
    }
    if (cl.hasOption("col")) {
        List<MagicCollection> list;
        try {
            list = MTGControler.getInstance().getEnabledDAO().getCollections();
        } catch (SQLException e) {
            throw new IOException(e);
        }
        session.write(showList(list, Arrays.asList(MTGConsoleHandler.getAttCols())));
    }
    if (cl.hasOption("?")) {
        usage();
    }
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) MagicCard(org.magic.api.beans.MagicCard) SQLException(java.sql.SQLException) MagicEdition(org.magic.api.beans.MagicEdition) IOException(java.io.IOException) MagicCollection(org.magic.api.beans.MagicCollection)

Example 43 with MagicCard

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

the class CopyFromActions method actionPerformed.

@Override
public void actionPerformed(ActionEvent e) {
    CardChooseDialog diag = new CardChooseDialog();
    diag.setVisible(true);
    if (diag.getSelected() != null)
        try {
            mc.setMagicCard((MagicCard) BeanUtils.cloneBean(diag.getSelected().getMagicCard()));
        } catch (Exception e1) {
            logger.error(e1);
        }
    mc.validate();
    mc.repaint();
}
Also used : MagicCard(org.magic.api.beans.MagicCard) CardChooseDialog(org.magic.game.gui.components.dialog.CardChooseDialog)

Example 44 with MagicCard

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

the class EmblemActions method actionPerformed.

@Override
public void actionPerformed(ActionEvent e) {
    try {
        MagicCard tok = GamePanelGUI.getInstance().getTokenGenerator().generateEmblemFor(card.getMagicCard());
        DisplayableCard dc = new DisplayableCard(tok, MTGControler.getInstance().getCardsDimension(), true);
        dc.setMagicCard(tok);
        GamePanelGUI.getInstance().getPanelBattleField().addComponent(dc);
        GamePanelGUI.getInstance().getPanelBattleField().revalidate();
        GamePanelGUI.getInstance().getPanelBattleField().repaint();
        GamePanelGUI.getInstance().getPlayer().logAction("generate " + tok + " emblem");
    } catch (Exception ex) {
        logger.error(ex);
    }
}
Also used : MagicCard(org.magic.api.beans.MagicCard) DisplayableCard(org.magic.game.gui.components.DisplayableCard)

Example 45 with MagicCard

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

the class TransformActions method actionPerformed.

@Override
public void actionPerformed(ActionEvent e) {
    GamePanelGUI.getInstance().getPlayer().logAction("Transform " + card.getMagicCard());
    try {
        card.removeAllCounters();
        MagicCard mc = MTGControler.getInstance().getEnabledProviders().searchCardByCriteria("name", card.getMagicCard().getRotatedCardName(), card.getMagicCard().getEditions().get(0), true).get(0);
        mc.setRulings(card.getMagicCard().getRulings());
        card.setMagicCard(mc);
        card.revalidate();
        card.repaint();
        card.initActions();
    } catch (Exception ex) {
        logger.error(ex);
    }
}
Also used : MagicCard(org.magic.api.beans.MagicCard)

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