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