Search in sources :

Example 36 with MagicDeck

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

the class MTGoldFishDeck method getDeck.

@Override
public MagicDeck getDeck(RetrievableDeck info) throws IOException {
    logger.debug("sniff url : " + info.getUrl());
    MagicDeck deck = new MagicDeck();
    deck.setName(info.getName());
    deck.setDescription(info.getUrl().toString());
    deck.setDateCreation(new Date());
    Document d = Jsoup.connect(info.getUrl().toString()).userAgent(getString("USER_AGENT")).get();
    Elements e = d.select("table.deck-view-deck-table").get(0).select(MTGConstants.HTML_TAG_TR);
    boolean sideboard = false;
    for (Element tr : e) {
        if (tr.select("td.deck-header").text().contains("Sideboard"))
            sideboard = true;
        if ((tr.select("td.deck-col-qty").text() + " " + tr.select("td.deck-col-card").text()).length() > 1) {
            int qte = Integer.parseInt(tr.select("td.deck-col-qty").text());
            String cardName = tr.select("td.deck-col-card").text();
            MagicEdition ed = null;
            if (cardName.trim().equalsIgnoreCase("Plains") || cardName.trim().equalsIgnoreCase("Island") || cardName.trim().equalsIgnoreCase("Swamp") || cardName.trim().equalsIgnoreCase("Mountain") || cardName.trim().equalsIgnoreCase("Forest")) {
                ed = new MagicEdition();
                ed.setId(MTGControler.getInstance().get("default-land-deck"));
            }
            if (cardName.contains("//"))
                cardName = cardName.substring(0, cardName.indexOf("//")).trim();
            MagicCard mc = MTGControler.getInstance().getEnabledProviders().searchCardByCriteria("name", cardName, ed, true).get(0);
            if (!sideboard) {
                deck.getMap().put(mc, qte);
            } else {
                deck.getMapSideBoard().put(mc, qte);
            }
        }
    }
    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)

Example 37 with MagicDeck

use of org.magic.api.beans.MagicDeck 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 38 with MagicDeck

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

the class MkmOnlineExport method export.

@Override
public void export(List<MagicCard> cards, File f) throws IOException {
    MagicDeck d = new MagicDeck();
    for (MagicCard mc : cards) d.getMap().put(mc, Integer.parseInt(getString("DEFAULT_QTE")));
    d.setName(f.getName());
    export(d, f);
}
Also used : MagicCard(org.magic.api.beans.MagicCard) MagicDeck(org.magic.api.beans.MagicDeck)

Example 39 with MagicDeck

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

the class OCTGNDeckExport method exportStock.

@Override
public void exportStock(List<MagicCardStock> stock, File f) throws IOException {
    MagicDeck d = new MagicDeck();
    d.setName(f.getName());
    for (MagicCardStock mcs : stock) {
        d.getMap().put(mcs.getMagicCard(), mcs.getQte());
    }
    export(d, f);
}
Also used : MagicDeck(org.magic.api.beans.MagicDeck) MagicCardStock(org.magic.api.beans.MagicCardStock)

Example 40 with MagicDeck

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

the class OCTGNDeckExport method importDeck.

@Override
public MagicDeck importDeck(File f) throws IOException {
    MagicDeck deck = new MagicDeck();
    deck.setName(f.getName().substring(0, f.getName().indexOf('.')));
    try {
        Document d = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new FileReader(f)));
        XPath xpath = XPathFactory.newInstance().newXPath();
        XPathExpression expr = xpath.compile("//section[@name='Main']/card");
        NodeList result = (NodeList) expr.evaluate(d, XPathConstants.NODESET);
        for (int i = 0; i < result.getLength(); i++) {
            Node it = result.item(i);
            String name = it.getTextContent();
            String qte = it.getAttributes().getNamedItem("qty").getNodeValue();
            MagicCard mc = MTGControler.getInstance().getEnabledProviders().searchCardByCriteria("name", name, null, true).get(0);
            deck.getMap().put(mc, Integer.parseInt(qte));
        }
        expr = xpath.compile("//section[@name='Sideboard']/card");
        result = (NodeList) expr.evaluate(d, XPathConstants.NODESET);
        for (int i = 0; i < result.getLength(); i++) {
            Node it = result.item(i);
            String name = it.getTextContent();
            String qte = it.getAttributes().getNamedItem("qty").getNodeValue();
            MagicCard mc = MTGControler.getInstance().getEnabledProviders().searchCardByCriteria("name", name, null, true).get(0);
            deck.getMapSideBoard().put(mc, Integer.parseInt(qte));
        }
        return deck;
    } catch (Exception e) {
        throw new IOException(e);
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) InputSource(org.xml.sax.InputSource) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) IOException(java.io.IOException) Document(org.w3c.dom.Document) IOException(java.io.IOException) MagicCard(org.magic.api.beans.MagicCard) FileReader(java.io.FileReader) MagicDeck(org.magic.api.beans.MagicDeck)

Aggregations

MagicDeck (org.magic.api.beans.MagicDeck)40 MagicCard (org.magic.api.beans.MagicCard)21 MagicCardStock (org.magic.api.beans.MagicCardStock)14 Date (java.util.Date)12 MagicEdition (org.magic.api.beans.MagicEdition)10 FileReader (java.io.FileReader)9 IOException (java.io.IOException)8 Document (org.jsoup.nodes.Document)7 Element (org.jsoup.nodes.Element)7 BufferedReader (java.io.BufferedReader)6 ArrayList (java.util.ArrayList)6 Elements (org.jsoup.select.Elements)6 BorderLayout (java.awt.BorderLayout)3 File (java.io.File)3 JsonArray (com.google.gson.JsonArray)2 JsonParser (com.google.gson.JsonParser)2 Dimension (java.awt.Dimension)2 SQLException (java.sql.SQLException)2 JButton (javax.swing.JButton)2 JFileChooser (javax.swing.JFileChooser)2