Various minor improvements to ease usage

This commit is contained in:
Maxopoly 2021-11-10 19:55:52 +01:00
parent 79692ec3ad
commit 82aaf7cb01
6 changed files with 39 additions and 10 deletions

View File

@ -31,7 +31,7 @@ public class Attack {
private final String damage; private final String damage;
Attack(JSONObject json) { Attack(JSONObject json) {
this(Types.parse(json.getJSONArray("cost")), json.getString("name"), json.optString("effect"), this(Types.parse(json.optJSONArray("cost")), json.getString("name"), json.optString("effect"),
json.optString("damage")); json.optString("damage"));
} }

View File

@ -36,7 +36,7 @@ public class CardInfo extends CardResume {
CardInfo(JSONObject json) { CardInfo(JSONObject json) {
super(json); super(json);
this.illustrator = json.getString("illustrator"); this.illustrator = json.optString("illustrator");
this.rarity = Rarities.parse(json.getString("rarity")); this.rarity = Rarities.parse(json.getString("rarity"));
this.category = Categories.parse(json.getString("category")); this.category = Categories.parse(json.getString("category"));
JSONObject variantSection = json.getJSONObject("variants"); JSONObject variantSection = json.getJSONObject("variants");
@ -152,7 +152,7 @@ public class CardInfo extends CardResume {
/** /**
* *
* @return Card illustrator * @return Card illustrator, may be null
*/ */
public String getIllustrator() { public String getIllustrator() {
return illustrator; return illustrator;

View File

@ -11,7 +11,7 @@ import org.json.JSONObject;
* Core information to describe a single card * Core information to describe a single card
* *
*/ */
public class CardResume { public class CardResume implements Comparable<CardResume> {
static List<CardResume> parse(JSONArray array) { static List<CardResume> parse(JSONArray array) {
if (array == null) { if (array == null) {
@ -40,6 +40,10 @@ public class CardResume {
this.image = image; this.image = image;
} }
public String toString() {
return String.format("%s (%s): %s,%s", name, id, localId, image);
}
/** /**
* @return Globally unique card ID based on the set ID and the cards ID within the set * @return Globally unique card ID based on the set ID and the cards ID within the set
*/ */
@ -69,4 +73,23 @@ public class CardResume {
public String getName() { public String getName() {
return name; return name;
} }
@Override
public boolean equals(Object o) {
if (!(o instanceof CardResume)) {
return false;
}
CardResume other = (CardResume) o;
return this.id.equals(other.id);
}
@Override
public int hashCode() {
return this.id.hashCode();
}
@Override
public int compareTo(CardResume arg0) {
return this.id.compareTo(arg0.id);
}
} }

View File

@ -142,7 +142,11 @@ public class TCGDexAPI {
*/ */
public CardInfo getCardInfo(String globalCardID) throws IOException { public CardInfo getCardInfo(String globalCardID) throws IOException {
String data = Utils.doGet(buildURL("cards", globalCardID)); String data = Utils.doGet(buildURL("cards", globalCardID));
return new CardInfo(new JSONObject(data)); JSONObject json = new JSONObject(data);
if (json.has("error")) {
return null;
}
return new CardInfo(json);
} }
/** /**
@ -158,7 +162,11 @@ public class TCGDexAPI {
*/ */
public CardInfo getCardInfo(String setID, String cardID) throws IOException { public CardInfo getCardInfo(String setID, String cardID) throws IOException {
String data = Utils.doGet(buildURL("sets", setID, cardID)); String data = Utils.doGet(buildURL("sets", setID, cardID));
return new CardInfo(new JSONObject(data)); JSONObject json = new JSONObject(data);
if (json.has("error")) {
return null;
}
return new CardInfo(json);
} }
/** /**

View File

@ -2,9 +2,7 @@ package net.tcgdex;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.URL; import java.net.URL;

View File

@ -30,7 +30,7 @@ public class Weakness {
Weakness(JSONObject json) { Weakness(JSONObject json) {
this(Types.parse(json.getString("type")), json.getString("value")); this(Types.parse(json.getString("type")), json.optString("value"));
} }
Weakness(Types type, String value) { Weakness(Types type, String value) {
@ -56,7 +56,7 @@ public class Weakness {
} }
/** /**
* @return Descriptor of the weakness multiplier, including a leading x, for example 'x2' * @return Descriptor of the weakness multiplier, including a leading x, for example 'x2'. May be null
*/ */
public String getValue() { public String getValue() {
return value; return value;