download the json at runtime

This commit is contained in:
Neil Gentleman 2016-02-14 14:33:32 -08:00
parent 26b8b88963
commit 510f7a86b6
4 changed files with 73 additions and 33 deletions

View file

@ -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;

View file

@ -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;
}

View file

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