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;
}
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;
}
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);
}
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);
}
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);
}
}
Aggregations