Search in sources :

Example 11 with MagicDeck

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

the class CSVExport method importDeck.

@Override
public MagicDeck importDeck(File f) throws IOException {
    try (BufferedReader read = new BufferedReader(new FileReader(f))) {
        MagicDeck deck = new MagicDeck();
        deck.setName(f.getName().substring(0, f.getName().indexOf('.')));
        String line = read.readLine();
        line = read.readLine();
        while (line != null) {
            String[] part = line.split(getString("importDeckCharSeparator"));
            String name = part[0];
            String qte = part[1];
            String set = part[2];
            MagicEdition ed = new MagicEdition();
            ed.setId(set);
            List<MagicCard> list = MTGControler.getInstance().getEnabledProviders().searchCardByCriteria("name", name, ed, true);
            deck.getMap().put(list.get(0), Integer.parseInt(qte));
            line = read.readLine();
        }
        return deck;
    }
}
Also used : MagicCard(org.magic.api.beans.MagicCard) BufferedReader(java.io.BufferedReader) MagicEdition(org.magic.api.beans.MagicEdition) FileReader(java.io.FileReader) MagicDeck(org.magic.api.beans.MagicDeck)

Example 12 with MagicDeck

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

the class CocatriceDeckExport method importDeck.

@Override
public MagicDeck importDeck(File f) throws IOException {
    MagicDeck deck = new MagicDeck();
    try {
        Document d = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new FileReader(f)));
        XPath xpath = XPathFactory.newInstance().newXPath();
        XPathExpression expr = xpath.compile("//cockatrice_deck/deckname");
        NodeList result = (NodeList) expr.evaluate(d, XPathConstants.NODESET);
        deck.setName(result.item(0).getTextContent());
        expr = xpath.compile("//cockatrice_deck/comments");
        result = (NodeList) expr.evaluate(d, XPathConstants.NODESET);
        deck.setDescription(result.item(0).getTextContent());
        expr = xpath.compile("//cockatrice_deck/zone[contains(@name,'main')]/card");
        result = ((NodeList) expr.evaluate(d, XPathConstants.NODESET));
        int c = 0;
        for (int i = 0; i < result.getLength(); i++) {
            String name = result.item(i).getAttributes().getNamedItem("name").getTextContent();
            Integer qte = Integer.parseInt(result.item(i).getAttributes().getNamedItem("number").getTextContent());
            deck.getMap().put(MTGControler.getInstance().getEnabledProviders().searchCardByCriteria("name", name, null, true).get(0), qte);
            setChanged();
            notifyObservers(c++);
        }
        expr = xpath.compile("//cockatrice_deck/zone[contains(@name,'side')]/card");
        result = ((NodeList) expr.evaluate(d, XPathConstants.NODESET));
        for (int i = 0; i < result.getLength(); i++) {
            String name = result.item(i).getAttributes().getNamedItem("name").getTextContent();
            Integer qte = Integer.parseInt(result.item(i).getAttributes().getNamedItem("number").getTextContent());
            deck.getMapSideBoard().put(MTGControler.getInstance().getEnabledProviders().searchCardByCriteria("name", name, null, true).get(0), qte);
            setChanged();
            notifyObservers(c++);
        }
    } catch (Exception e) {
        throw new IOException(e);
    }
    return deck;
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) InputSource(org.xml.sax.InputSource) NodeList(org.w3c.dom.NodeList) FileReader(java.io.FileReader) IOException(java.io.IOException) Document(org.w3c.dom.Document) IOException(java.io.IOException) MagicDeck(org.magic.api.beans.MagicDeck)

Example 13 with MagicDeck

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

the class CocatriceDeckExport 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 14 with MagicDeck

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

the class DCIDeckSheetExport 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 15 with MagicDeck

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

the class JsonExport method importDeck.

@Override
public MagicDeck importDeck(File f) throws IOException {
    JsonReader reader = new JsonReader(new FileReader(f));
    JsonObject root = new JsonParser().parse(reader).getAsJsonObject();
    MagicDeck deck = new MagicDeck();
    deck.setDateCreation(new Date());
    if (!root.get("name").isJsonNull())
        deck.setName(root.get("name").getAsString());
    if (!root.get("description").isJsonNull())
        deck.setDescription(root.get("description").getAsString());
    if (!root.get("tags").isJsonNull()) {
        JsonArray arr = root.get("tags").getAsJsonArray();
        for (int i = 0; i < arr.size(); i++) deck.getTags().add(arr.get(i).getAsString());
    }
    JsonArray main = root.get("main").getAsJsonArray();
    for (int i = 0; i < main.size(); i++) {
        JsonObject line = main.get(i).getAsJsonObject();
        int qte = line.get("qty").getAsInt();
        MagicCard mc = new Gson().fromJson(line.get("card"), MagicCard.class);
        deck.getMap().put(mc, qte);
    }
    JsonArray side = root.get("side").getAsJsonArray();
    for (int i = 0; i < side.size(); i++) {
        JsonObject line = side.get(i).getAsJsonObject();
        int qte = line.get("qty").getAsInt();
        MagicCard mc = new Gson().fromJson(line.get("card"), MagicCard.class);
        deck.getMapSideBoard().put(mc, qte);
    }
    return deck;
}
Also used : JsonArray(com.google.gson.JsonArray) MagicCard(org.magic.api.beans.MagicCard) JsonReader(com.google.gson.stream.JsonReader) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) FileReader(java.io.FileReader) Date(java.util.Date) JsonParser(com.google.gson.JsonParser) 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