mirror of
https://github.com/magefree/mage.git
synced 2025-12-25 13:02:06 -08:00
update verification
now using java types instead of Map<String, Object>
This commit is contained in:
parent
584b532598
commit
26b8b88963
4 changed files with 213 additions and 69 deletions
50
Mage.Verify/src/main/java/mage/verify/JsonCard.java
Normal file
50
Mage.Verify/src/main/java/mage/verify/JsonCard.java
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
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 String type;
|
||||
public List<String> supertypes;
|
||||
public List<String> types;
|
||||
public List<String> subtypes;
|
||||
public String text;
|
||||
public String power;
|
||||
public String toughness;
|
||||
public int loyalty;
|
||||
public String imageName;
|
||||
public boolean starter; // only available in boxed sets and not in boosters
|
||||
public int hand; // vanguard
|
||||
public int life; // vanguard
|
||||
|
||||
// only available in AllSets.json
|
||||
public String artist;
|
||||
public String flavor;
|
||||
public String id;
|
||||
public int multiverseid;
|
||||
public String rarity;
|
||||
public boolean reserved;
|
||||
public int[] variations;
|
||||
public String number;
|
||||
public String releaseDate; // promos
|
||||
public String border;
|
||||
public String watermark;
|
||||
public boolean timeshifted;
|
||||
}
|
||||
31
Mage.Verify/src/main/java/mage/verify/JsonSet.java
Normal file
31
Mage.Verify/src/main/java/mage/verify/JsonSet.java
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
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;
|
||||
public String gathererCode;
|
||||
public String magicCardsInfoCode;
|
||||
public String[] magicRaritiesCodes;
|
||||
public String releaseDate;
|
||||
public String border;
|
||||
public String type;
|
||||
public List<Object> booster; // [String|[String]]
|
||||
public List<JsonCard> cards;
|
||||
public String block;
|
||||
public boolean onlineOnly;
|
||||
}
|
||||
57
Mage.Verify/src/main/java/mage/verify/MtgJson.java
Normal file
57
Mage.Verify/src/main/java/mage/verify/MtgJson.java
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
package mage.verify;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.Normalizer;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class MtgJson {
|
||||
|
||||
private static class CardHolder {
|
||||
private static final Map<String, JsonCard> cards;
|
||||
static {
|
||||
try {
|
||||
cards = JsonCard.loadAll();
|
||||
addAliases(cards);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static JsonCard find(String name) {
|
||||
return findReference(CardHolder.cards, name);
|
||||
}
|
||||
|
||||
private static <T> T findReference(Map<String, T> reference, String name) {
|
||||
T ref = reference.get(name);
|
||||
if (ref == null) {
|
||||
name = name.replaceFirst("\\bA[Ee]", "Æ");
|
||||
ref = reference.get(name);
|
||||
}
|
||||
if (ref == null) {
|
||||
name = name.replace("'", "\""); // for Kongming, "Sleeping Dragon" & Pang Tong, "Young Phoenix"
|
||||
ref = reference.get(name);
|
||||
}
|
||||
return ref;
|
||||
}
|
||||
|
||||
private static <T> void addAliases(Map<String, T> reference) {
|
||||
Map<String, String> aliases = new HashMap<>();
|
||||
for (String name : reference.keySet()) {
|
||||
String unaccented = stripAccents(name);
|
||||
if (!name.equals(unaccented)) {
|
||||
aliases.put(name, unaccented);
|
||||
}
|
||||
}
|
||||
for (Map.Entry<String, String> mapping : aliases.entrySet()) {
|
||||
reference.put(mapping.getValue(), reference.get(mapping.getKey()));
|
||||
}
|
||||
}
|
||||
|
||||
private static String stripAccents(String str) {
|
||||
String decomposed = Normalizer.normalize(str, Normalizer.Form.NFKD);
|
||||
return decomposed.replaceAll("[\\p{InCombiningDiacriticalMarks}]", "");
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue