mirror of
https://github.com/magefree/mage.git
synced 2025-12-20 02:30:08 -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}]", "");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -1,7 +1,5 @@
|
||||||
package mage.verify;
|
package mage.verify;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
||||||
import mage.ObjectColor;
|
import mage.ObjectColor;
|
||||||
import mage.cards.Card;
|
import mage.cards.Card;
|
||||||
import mage.cards.CardImpl;
|
import mage.cards.CardImpl;
|
||||||
|
|
@ -17,43 +15,70 @@ import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.NoSuchFileException;
|
import java.nio.file.NoSuchFileException;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.text.Normalizer;
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public class CompareWithMtgjsonTest {
|
public class VerifyCardDataTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSets() throws IOException {
|
public void verifySets() throws IOException {
|
||||||
Collection<ExpansionSet> sets = Sets.getInstance().values();
|
Map<String, JsonSet> reference = JsonSet.loadAll();
|
||||||
|
|
||||||
Map<String, Map<String, Object>> reference = new ObjectMapper().readValue(
|
for (ExpansionSet set : Sets.getInstance().values()) {
|
||||||
CompareWithMtgjsonTest.class.getResourceAsStream("AllCards.json"),
|
JsonSet ref = reference.get(set.getCode());
|
||||||
new TypeReference<Map<String, Map<String, Object>>>() {});
|
if (ref == null) {
|
||||||
|
for (JsonSet js : reference.values()) {
|
||||||
Map<String, String> aliases = new HashMap<>();
|
if (set.getCode().equals(js.oldCode) || set.getCode().toLowerCase().equals(js.magicCardsInfoCode)) {
|
||||||
for (String name : reference.keySet()) {
|
ref = js;
|
||||||
String unaccented = stripAccents(name);
|
break;
|
||||||
if (!name.equals(unaccented)) {
|
}
|
||||||
aliases.put(name, unaccented);
|
}
|
||||||
|
if (ref == null) {
|
||||||
|
System.out.println("missing reference for " + set);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!String.format("%tF", set.getReleaseDate()).equals(ref.releaseDate)) {
|
||||||
|
System.out.printf("%40s %-20s %20tF %20s%n", set, "release date", set.getReleaseDate(), ref.releaseDate);
|
||||||
|
}
|
||||||
|
if (set.hasBoosters() != (ref.booster != null)) {
|
||||||
|
System.out.printf("%40s %-20s %20s %20s%n", set, "has boosters", set.hasBoosters(), ref.booster != null);
|
||||||
|
}
|
||||||
|
boolean refHasBasicLands = false;
|
||||||
|
for (JsonCard card : ref.cards) {
|
||||||
|
if ("Mountain".equals(card.name)) {
|
||||||
|
refHasBasicLands = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (set.hasBasicLands() != refHasBasicLands) {
|
||||||
|
System.out.printf("%40s %-20s %20s %20s%n", set, "has basic lands", set.hasBasicLands(), refHasBasicLands);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (Map.Entry<String, String> mapping : aliases.entrySet()) {
|
}
|
||||||
reference.put(mapping.getValue(), reference.get(mapping.getKey()));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
public static List<Card> allCards() {
|
||||||
|
Collection<ExpansionSet> sets = Sets.getInstance().values();
|
||||||
|
List<Card> cards = new ArrayList<>();
|
||||||
for (ExpansionSet set : sets) {
|
for (ExpansionSet set : sets) {
|
||||||
for (ExpansionSet.SetCardInfo setInfo : set.getSetCardInfo()) {
|
for (ExpansionSet.SetCardInfo setInfo : set.getSetCardInfo()) {
|
||||||
Set<String> tokens = findSourceTokens(setInfo.getCardClass());
|
cards.add(CardImpl.createCard(setInfo.getCardClass(), new CardSetInfo(setInfo.getName(), set.getCode(),
|
||||||
Card card = CardImpl.createCard(setInfo.getCardClass(), new CardSetInfo(setInfo.getName(), set.getCode(),
|
setInfo.getCardNumber(), setInfo.getRarity(), setInfo.getGraphicInfo())));
|
||||||
setInfo.getCardNumber(), setInfo.getRarity(), setInfo.getGraphicInfo()));
|
}
|
||||||
if (card.isSplitCard()) {
|
}
|
||||||
check(reference, ((SplitCard) card).getLeftHalfCard(), null);
|
return cards;
|
||||||
check(reference, ((SplitCard) card).getRightHalfCard(), null);
|
}
|
||||||
} else {
|
|
||||||
check(reference, card, tokens);
|
@Test
|
||||||
}
|
public void verifyCards() throws IOException {
|
||||||
|
for (Card card : allCards()) {
|
||||||
|
Set<String> tokens = findSourceTokens(card.getClass());
|
||||||
|
if (card.isSplitCard()) {
|
||||||
|
check(((SplitCard) card).getLeftHalfCard(), null);
|
||||||
|
check(((SplitCard) card).getRightHalfCard(), null);
|
||||||
|
} else {
|
||||||
|
check(card, tokens);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -76,22 +101,17 @@ public class CompareWithMtgjsonTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private String stripAccents(String str) {
|
private void check(Card card, Set<String> tokens) {
|
||||||
String decomposed = Normalizer.normalize(str, Normalizer.Form.NFKD);
|
JsonCard ref = MtgJson.find(card.getName());
|
||||||
return decomposed.replaceAll("[\\p{InCombiningDiacriticalMarks}]", "");
|
|
||||||
}
|
|
||||||
|
|
||||||
private void check(Map<String, Map<String, Object>> reference, Card card, Set<String> tokens) {
|
|
||||||
Map<String, Object> ref = findReference(reference, card.getName());
|
|
||||||
if (ref == null) {
|
if (ref == null) {
|
||||||
System.out.println("Missing card reference for " + card);
|
System.out.println("Missing card reference for " + card);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
checkAll(card, ref);
|
checkAll(card, ref);
|
||||||
if (tokens != null) {
|
if (tokens != null) {
|
||||||
Map<String, Object> ref2 = null;
|
JsonCard ref2 = null;
|
||||||
if (card.isFlipCard()) {
|
if (card.isFlipCard()) {
|
||||||
ref2 = findReference(reference, card.getFlipCardName());
|
ref2 = MtgJson.find(card.getFlipCardName());
|
||||||
}
|
}
|
||||||
for (String token : tokens) {
|
for (String token : tokens) {
|
||||||
if (!(token.equals(card.getName())
|
if (!(token.equals(card.getName())
|
||||||
|
|
@ -105,32 +125,18 @@ public class CompareWithMtgjsonTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<String, Object> findReference(Map<String, Map<String, Object>> reference, String name) {
|
private boolean containsInTypesOrText(JsonCard ref, String token) {
|
||||||
Map<String, Object> ref = reference.get(name);
|
return contains(ref.types, token)
|
||||||
if (ref == null) {
|
|| contains(ref.subtypes, token)
|
||||||
name = name.replaceFirst("\\bA[Ee]", "Æ");
|
|| contains(ref.supertypes, token)
|
||||||
ref = reference.get(name);
|
|| ref.text.contains(token);
|
||||||
}
|
|
||||||
if (ref == null) {
|
|
||||||
name = name.replace("'", "\""); // for Kongming, "Sleeping Dragon" & Pang Tong, "Young Phoenix"
|
|
||||||
ref = reference.get(name);
|
|
||||||
}
|
|
||||||
return ref;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean containsInTypesOrText(Map<String, Object> ref, String token) {
|
private boolean contains(Collection<String> options, String value) {
|
||||||
return contains(ref, "types", token)
|
|
||||||
|| contains(ref, "subtypes", token)
|
|
||||||
|| contains(ref, "supertypes", token)
|
|
||||||
|| ((String) ref.get("text")).contains(token);
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean contains(Map<String, Object> ref, String key, String value) {
|
|
||||||
Collection<String> options = (Collection<String>) ref.get(key);
|
|
||||||
return options != null && options.contains(value);
|
return options != null && options.contains(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkAll(Card card, Map<String, Object> ref) {
|
private void checkAll(Card card, JsonCard ref) {
|
||||||
checkCost(card, ref);
|
checkCost(card, ref);
|
||||||
checkPT(card, ref);
|
checkPT(card, ref);
|
||||||
checkSubtypes(card, ref);
|
checkSubtypes(card, ref);
|
||||||
|
|
@ -139,8 +145,8 @@ public class CompareWithMtgjsonTest {
|
||||||
checkColors(card, ref);
|
checkColors(card, ref);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkColors(Card card, Map<String, Object> ref) {
|
private void checkColors(Card card, JsonCard ref) {
|
||||||
Collection<String> expected = (Collection<String>) ref.get("colors");
|
Collection<String> expected = ref.colors;
|
||||||
ObjectColor color = card.getColor(null);
|
ObjectColor color = card.getColor(null);
|
||||||
if (expected == null) {
|
if (expected == null) {
|
||||||
expected = Collections.emptyList();
|
expected = Collections.emptyList();
|
||||||
|
|
@ -155,8 +161,8 @@ public class CompareWithMtgjsonTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkSubtypes(Card card, Map<String, Object> ref) {
|
private void checkSubtypes(Card card, JsonCard ref) {
|
||||||
Collection<String> expected = (Collection<String>) ref.get("subtypes");
|
Collection<String> expected = ref.subtypes;
|
||||||
if (expected != null && expected.contains("Urza’s")) {
|
if (expected != null && expected.contains("Urza’s")) {
|
||||||
expected = new ArrayList<>(expected);
|
expected = new ArrayList<>(expected);
|
||||||
for (ListIterator<String> it = ((List<String>) expected).listIterator(); it.hasNext();) {
|
for (ListIterator<String> it = ((List<String>) expected).listIterator(); it.hasNext();) {
|
||||||
|
|
@ -170,15 +176,15 @@ public class CompareWithMtgjsonTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkSupertypes(Card card, Map<String, Object> ref) {
|
private void checkSupertypes(Card card, JsonCard ref) {
|
||||||
Collection<String> expected = (Collection<String>) ref.get("supertypes");
|
Collection<String> expected = ref.supertypes;
|
||||||
if (!eqSet(card.getSupertype(), expected)) {
|
if (!eqSet(card.getSupertype(), expected)) {
|
||||||
System.out.println(card.getSupertype() + " != " + expected + " for " + card);
|
System.out.println(card.getSupertype() + " != " + expected + " for " + card);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkTypes(Card card, Map<String, Object> ref) {
|
private void checkTypes(Card card, JsonCard ref) {
|
||||||
Collection<String> expected = (Collection<String>) ref.get("types");
|
Collection<String> expected = ref.types;
|
||||||
List<String> type = new ArrayList<>();
|
List<String> type = new ArrayList<>();
|
||||||
for (CardType cardType : card.getCardType()) {
|
for (CardType cardType : card.getCardType()) {
|
||||||
type.add(cardType.toString());
|
type.add(cardType.toString());
|
||||||
|
|
@ -195,9 +201,9 @@ public class CompareWithMtgjsonTest {
|
||||||
return b != null && a.size() == b.size() && a.containsAll(b);
|
return b != null && a.size() == b.size() && a.containsAll(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkPT(Card card, Map<String, Object> ref) {
|
private void checkPT(Card card, JsonCard ref) {
|
||||||
String pt = card.getPower() + "/" + card.getToughness();
|
String pt = card.getPower() + "/" + card.getToughness();
|
||||||
String expected = ref.get("power") + "/" + ref.get("toughness");
|
String expected = ref.power + "/" + ref.toughness;
|
||||||
if ("0/0".equals(pt) && ("null/null".equals(expected) || "*/*".equals(expected))) {
|
if ("0/0".equals(pt) && ("null/null".equals(expected) || "*/*".equals(expected))) {
|
||||||
// ok
|
// ok
|
||||||
} else if (!Objects.equals(pt, expected.replace("*", "0"))) {
|
} else if (!Objects.equals(pt, expected.replace("*", "0"))) {
|
||||||
|
|
@ -205,8 +211,8 @@ public class CompareWithMtgjsonTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkCost(Card card, Map<String, Object> ref) {
|
private void checkCost(Card card, JsonCard ref) {
|
||||||
String expected = (String) ref.get("manaCost");
|
String expected = ref.manaCost;
|
||||||
String cost = join(card.getManaCost().getSymbols());
|
String cost = join(card.getManaCost().getSymbols());
|
||||||
if ("".equals(cost)) {
|
if ("".equals(cost)) {
|
||||||
cost = null;
|
cost = null;
|
||||||
Loading…
Add table
Add a link
Reference in a new issue