Search in sources :

Example 91 with MagicCard

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

the class MagicTheGatheringIOProvider method searchCardByCriteria.

@Override
public List<MagicCard> searchCardByCriteria(String att, String crit, MagicEdition me, boolean exact) throws IOException {
    List<MagicCard> lists = new ArrayList<>();
    URLConnection con = null;
    int page = 1;
    String url = jsonUrl + "/cards?" + att + "=" + URLEncoder.encode(crit, encoding);
    logger.debug(url);
    con = getConnection(url);
    JsonReader reader;
    int count = 0;
    int totalcount = con.getHeaderFieldInt("Total-Count", 0);
    while (count < totalcount) {
        url = jsonUrl + "/cards?" + att + "=" + URLEncoder.encode(crit, encoding) + "&page=" + page++;
        logger.debug(url);
        con = getConnection(url);
        reader = new JsonReader(new InputStreamReader(con.getInputStream(), encoding));
        JsonArray jsonList = new JsonParser().parse(reader).getAsJsonObject().getAsJsonArray("cards");
        for (int i = 0; i < jsonList.size(); i++) {
            lists.add(generateCard(jsonList.get(i).getAsJsonObject()));
        }
        count += con.getHeaderFieldInt("Count", 0);
    }
    return lists;
}
Also used : JsonArray(com.google.gson.JsonArray) MagicCard(org.magic.api.beans.MagicCard) InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) JsonReader(com.google.gson.stream.JsonReader) URLConnection(java.net.URLConnection) JsonParser(com.google.gson.JsonParser)

Example 92 with MagicCard

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

the class TappedOutDeckSniffer method getDeck.

@Override
public MagicDeck getDeck(RetrievableDeck info) throws IOException {
    HttpGet get = new HttpGet(info.getUrl());
    logger.debug("sniff deck : " + info.getName() + " at " + info.getUrl());
    String responseBody = EntityUtils.toString(httpclient.execute(get, httpContext).getEntity());
    MagicDeck deck = new MagicDeck();
    deck.setDateCreation(new Date());
    JsonElement root = new JsonParser().parse(responseBody);
    deck.setName(root.getAsJsonObject().get("name").getAsString());
    deck.setDescription(root.getAsJsonObject().get("url").getAsString());
    for (int i = 0; i < root.getAsJsonObject().get("inventory").getAsJsonArray().size(); i++) {
        JsonArray inv = root.getAsJsonObject().get("inventory").getAsJsonArray().get(i).getAsJsonArray();
        String cardName = inv.get(0).getAsString();
        String position = inv.get(1).getAsJsonObject().get("b").getAsString();
        int qte = inv.get(1).getAsJsonObject().get("qty").getAsInt();
        // remove foil if present
        cardName = StringUtils.replaceAll(cardName, "\\*.+?\\*", "").trim();
        // ged ed if present
        String idSet = null;
        Matcher m = Pattern.compile("\\(([^)]+)\\)").matcher(cardName);
        while (m.find()) {
            idSet = (m.group(1));
        }
        cardName = StringUtils.replaceAll(cardName, "\\(([^)]+)\\)", "").trim();
        // remove behavior if present
        if (cardName.contains("#"))
            cardName = cardName.substring(0, cardName.indexOf('#')).trim();
        if (cardName.contains("//"))
            cardName = cardName.substring(0, cardName.indexOf("//")).trim();
        List<MagicCard> ret;
        if (idSet == 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"));
                ret = MTGControler.getInstance().getEnabledProviders().searchCardByCriteria("name", cardName, ed, true);
            } else {
                ret = MTGControler.getInstance().getEnabledProviders().searchCardByCriteria("name", cardName, null, true);
            }
        } else {
            MagicEdition ed = new MagicEdition();
            ed.setId(idSet);
            ret = MTGControler.getInstance().getEnabledProviders().searchCardByCriteria("name", cardName, ed, true);
        }
        if (!ret.isEmpty()) {
            setChanged();
            notifyObservers(deck.getMap());
            if (position.equalsIgnoreCase("main"))
                deck.getMap().put(ret.get(0), qte);
            else
                deck.getMapSideBoard().put(ret.get(0), qte);
        }
    }
    return deck;
}
Also used : JsonArray(com.google.gson.JsonArray) MagicCard(org.magic.api.beans.MagicCard) Matcher(java.util.regex.Matcher) JsonElement(com.google.gson.JsonElement) HttpGet(org.apache.http.client.methods.HttpGet) MagicEdition(org.magic.api.beans.MagicEdition) Date(java.util.Date) MagicDeck(org.magic.api.beans.MagicDeck) JsonParser(com.google.gson.JsonParser)

Example 93 with MagicCard

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

the class Apprentice2DeckExport method export.

@Override
public void export(MagicDeck deck, File dest) throws IOException {
    StringBuilder temp = new StringBuilder();
    int c = 0;
    for (MagicCard mc : deck.getMap().keySet()) {
        temp.append("MD,");
        temp.append(deck.getMap().get(mc) + ",");
        temp.append("\"" + mc.getName() + "\",");
        temp.append(mc.getEditions().get(0).getId());
        temp.append("\n");
        setChanged();
        notifyObservers(c++);
    }
    for (MagicCard mc : deck.getMapSideBoard().keySet()) {
        temp.append("SB,");
        temp.append(deck.getMapSideBoard().get(mc) + ",");
        temp.append("\"" + mc.getName() + "\",");
        temp.append(mc.getEditions().get(0).getId());
        temp.append("\n");
        setChanged();
        notifyObservers(c++);
    }
    try (FileWriter out = new FileWriter(dest)) {
        out.write(temp.toString());
    }
}
Also used : MagicCard(org.magic.api.beans.MagicCard) FileWriter(java.io.FileWriter)

Example 94 with MagicCard

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

the class CSVExport method exportPriceCatalog.

public void exportPriceCatalog(List<MagicCard> cards, File f, MTGPricesProvider prov) throws IOException {
    try (BufferedWriter bw = new BufferedWriter(new FileWriter(f))) {
        exportedProperties = getString("exportedProperties").split(",");
        exportedPricesProperties = getString("exportedPricesProperties").split(",");
        for (String k : exportedProperties) bw.write(k + ";");
        for (String k : exportedPricesProperties) bw.write(k + ";");
        bw.write("\n");
        int i = 0;
        for (MagicCard mc : cards) {
            for (MagicPrice prices : prov.getPrice(mc.getEditions().get(0), mc)) {
                for (String k : exportedProperties) {
                    String val;
                    try {
                        val = BeanUtils.getProperty(mc, k);
                        if (val == null)
                            val = "";
                        bw.write(val.replaceAll("\n", "") + ";");
                    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                        throw new IOException(e);
                    }
                }
                for (String p : exportedPricesProperties) {
                    String val;
                    try {
                        val = BeanUtils.getProperty(prices, p);
                        if (val == null)
                            val = "";
                        bw.write(val.replaceAll("\n", "") + ";");
                    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                        throw new IOException(e);
                    }
                }
                bw.write("\n");
            }
            setChanged();
            notifyObservers(i++);
        }
    }
}
Also used : MagicCard(org.magic.api.beans.MagicCard) MagicPrice(org.magic.api.beans.MagicPrice) FileWriter(java.io.FileWriter) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) BufferedWriter(java.io.BufferedWriter)

Example 95 with MagicCard

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

the class CSVExport method importStock.

@Override
public List<MagicCardStock> importStock(File f) throws IOException {
    try (BufferedReader read = new BufferedReader(new FileReader(f))) {
        List<MagicCardStock> stock = new ArrayList<>();
        String line = read.readLine();
        line = read.readLine();
        while (line != null) {
            String[] part = line.split(";");
            MagicCardStock mcs = new MagicCardStock();
            MagicCard mc = MTGControler.getInstance().getEnabledProviders().searchCardByCriteria("name", part[1], null, true).get(0);
            for (MagicEdition ed : mc.getEditions()) if (ed.getSet().equals(part[2])) {
                mc.getEditions().add(0, ed);
                break;
            }
            mcs.setMagicCard(mc);
            mcs.setLanguage(part[3]);
            mcs.setQte(Integer.parseInt(part[4]));
            mcs.setCondition(EnumCondition.valueOf(part[5]));
            mcs.setFoil(Boolean.valueOf(part[6]));
            mcs.setAltered(Boolean.valueOf(part[7]));
            mcs.setSigned(Boolean.valueOf(part[8]));
            mcs.setMagicCollection(new MagicCollection(part[9]));
            mcs.setPrice(Double.valueOf(part[10]));
            try {
                mcs.setComment(part[11]);
            } catch (ArrayIndexOutOfBoundsException aioob) {
                mcs.setComment("");
            }
            mcs.setIdstock(-1);
            mcs.setUpdate(true);
            stock.add(mcs);
            line = read.readLine();
        }
        return stock;
    }
}
Also used : MagicCard(org.magic.api.beans.MagicCard) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) MagicEdition(org.magic.api.beans.MagicEdition) FileReader(java.io.FileReader) MagicCollection(org.magic.api.beans.MagicCollection) MagicCardStock(org.magic.api.beans.MagicCardStock)

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