mirror of
https://github.com/correl/mage.git
synced 2024-11-14 19:19:32 +00:00
download the json at runtime
This commit is contained in:
parent
26b8b88963
commit
510f7a86b6
4 changed files with 73 additions and 33 deletions
|
@ -1,26 +1,15 @@
|
|||
package mage.verify;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
class JsonCard {
|
||||
|
||||
static Map<String, JsonCard> loadAll() throws IOException {
|
||||
return new ObjectMapper().readValue(
|
||||
JsonCard.class.getResourceAsStream("AllCards.json"),
|
||||
new TypeReference<Map<String, JsonCard>>() {});
|
||||
}
|
||||
|
||||
public String layout;
|
||||
public String name;
|
||||
public List<String> names; // flip cards
|
||||
public String manaCost;
|
||||
public int cmc;
|
||||
public List<String> colors;
|
||||
public List<String> colorIdentity;
|
||||
public String type;
|
||||
public List<String> supertypes;
|
||||
public List<String> types;
|
||||
|
@ -33,6 +22,7 @@ class JsonCard {
|
|||
public boolean starter; // only available in boxed sets and not in boosters
|
||||
public int hand; // vanguard
|
||||
public int life; // vanguard
|
||||
public String mciNumber;
|
||||
|
||||
// only available in AllSets.json
|
||||
public String artist;
|
||||
|
|
|
@ -1,20 +1,9 @@
|
|||
package mage.verify;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
class JsonSet {
|
||||
|
||||
static Map<String, JsonSet> loadAll() throws IOException {
|
||||
return new ObjectMapper().readValue(
|
||||
JsonSet.class.getResourceAsStream("AllSets.json"),
|
||||
new TypeReference<Map<String, JsonSet>>() {});
|
||||
}
|
||||
|
||||
public String name;
|
||||
public String code;
|
||||
public String oldCode;
|
||||
|
@ -28,4 +17,7 @@ class JsonSet {
|
|||
public List<JsonCard> cards;
|
||||
public String block;
|
||||
public boolean onlineOnly;
|
||||
public String mkm_id;
|
||||
public String mkm_name;
|
||||
public Map<String, String> translations;
|
||||
}
|
||||
|
|
|
@ -1,17 +1,28 @@
|
|||
package mage.verify;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.text.Normalizer;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.zip.ZipInputStream;
|
||||
|
||||
public class MtgJson {
|
||||
private MtgJson() {}
|
||||
|
||||
private static class CardHolder {
|
||||
private static final Map<String, JsonCard> cards;
|
||||
static {
|
||||
try {
|
||||
cards = JsonCard.loadAll();
|
||||
cards = loadAllCards();
|
||||
addAliases(cards);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
|
@ -19,7 +30,48 @@ public class MtgJson {
|
|||
}
|
||||
}
|
||||
|
||||
public static JsonCard find(String name) {
|
||||
private static class SetHolder {
|
||||
private static final Map<String, JsonSet> sets;
|
||||
static {
|
||||
try {
|
||||
sets = loadAllSets();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static Map<String, JsonCard> loadAllCards() throws IOException {
|
||||
return readFromZip("AllCards.json.zip", new TypeReference<Map<String, JsonCard>>() {});
|
||||
}
|
||||
|
||||
private static Map<String, JsonSet> loadAllSets() throws IOException {
|
||||
return readFromZip("AllSets.json.zip", new TypeReference<Map<String, JsonSet>>() {});
|
||||
}
|
||||
|
||||
private static <T> T readFromZip(String filename, TypeReference<T> ref) throws IOException {
|
||||
InputStream stream = MtgJson.class.getResourceAsStream(filename);
|
||||
if (stream == null) {
|
||||
File file = new File(filename);
|
||||
if (!file.exists()) {
|
||||
InputStream download = new URL("http://mtgjson.com/json/" + filename).openStream();
|
||||
Files.copy(download, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
||||
System.out.println("Downloaded " + filename + " to " + file.getAbsolutePath());
|
||||
} else {
|
||||
System.out.println("Using " + filename + " from " + file.getAbsolutePath());
|
||||
}
|
||||
stream = new FileInputStream(file);
|
||||
}
|
||||
ZipInputStream zipInputStream = new ZipInputStream(stream);
|
||||
zipInputStream.getNextEntry();
|
||||
return new ObjectMapper().readValue(zipInputStream, ref);
|
||||
}
|
||||
|
||||
public static Map<String, JsonSet> sets() {
|
||||
return SetHolder.sets;
|
||||
}
|
||||
|
||||
public static JsonCard card(String name) {
|
||||
return findReference(CardHolder.cards, name);
|
||||
}
|
||||
|
||||
|
|
|
@ -23,7 +23,7 @@ public class VerifyCardDataTest {
|
|||
|
||||
@Test
|
||||
public void verifySets() throws IOException {
|
||||
Map<String, JsonSet> reference = JsonSet.loadAll();
|
||||
Map<String, JsonSet> reference = MtgJson.sets();
|
||||
|
||||
for (ExpansionSet set : Sets.getInstance().values()) {
|
||||
JsonSet ref = reference.get(set.getCode());
|
||||
|
@ -102,7 +102,7 @@ public class VerifyCardDataTest {
|
|||
}
|
||||
|
||||
private void check(Card card, Set<String> tokens) {
|
||||
JsonCard ref = MtgJson.find(card.getName());
|
||||
JsonCard ref = MtgJson.card(card.getName());
|
||||
if (ref == null) {
|
||||
System.out.println("Missing card reference for " + card);
|
||||
return;
|
||||
|
@ -111,7 +111,7 @@ public class VerifyCardDataTest {
|
|||
if (tokens != null) {
|
||||
JsonCard ref2 = null;
|
||||
if (card.isFlipCard()) {
|
||||
ref2 = MtgJson.find(card.getFlipCardName());
|
||||
ref2 = MtgJson.card(card.getFlipCardName());
|
||||
}
|
||||
for (String token : tokens) {
|
||||
if (!(token.equals(card.getName())
|
||||
|
@ -202,15 +202,21 @@ public class VerifyCardDataTest {
|
|||
}
|
||||
|
||||
private void checkPT(Card card, JsonCard ref) {
|
||||
String pt = card.getPower() + "/" + card.getToughness();
|
||||
String expected = ref.power + "/" + ref.toughness;
|
||||
if ("0/0".equals(pt) && ("null/null".equals(expected) || "*/*".equals(expected))) {
|
||||
// ok
|
||||
} else if (!Objects.equals(pt, expected.replace("*", "0"))) {
|
||||
if (!eqPT(card.getPower().toString(), ref.power) || !eqPT(card.getToughness().toString(), ref.toughness)) {
|
||||
String pt = card.getPower() + "/" + card.getToughness();
|
||||
String expected = ref.power + "/" + ref.toughness;
|
||||
System.out.println(pt + " != " + expected + " for " + card);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean eqPT(String found, String expected) {
|
||||
if (expected == null) {
|
||||
return "0".equals(found);
|
||||
} else {
|
||||
return found.equals(expected) || expected.contains("*");
|
||||
}
|
||||
}
|
||||
|
||||
private void checkCost(Card card, JsonCard ref) {
|
||||
String expected = ref.manaCost;
|
||||
String cost = join(card.getManaCost().getSymbols());
|
||||
|
|
Loading…
Reference in a new issue