From 5ad58f59c1bcfbe4748d206f7d8e51a9fcc78ba7 Mon Sep 17 00:00:00 2001 From: Neil Gentleman Date: Tue, 27 Oct 2015 20:32:17 -0700 Subject: [PATCH] compare cards with mtgjson --- .gitignore | 5 + Mage.Verify/pom.xml | 76 ++++++++ .../mage/verify/CompareWithMtgjsonTest.java | 182 ++++++++++++++++++ pom.xml | 1 + 4 files changed, 264 insertions(+) create mode 100644 Mage.Verify/pom.xml create mode 100644 Mage.Verify/src/test/java/mage/verify/CompareWithMtgjsonTest.java diff --git a/.gitignore b/.gitignore index 8c1d40ad0d..d9d5853e3f 100644 --- a/.gitignore +++ b/.gitignore @@ -77,6 +77,11 @@ Mage/target Mage.Updater/target mage.updater.client/target +# Mage.Verify +Mage.Verify/target +Mage.Verify/AllCards.json.zip +Mage.Verify/AllSets.json.zip + releases Utils/author.txt .DS_Store diff --git a/Mage.Verify/pom.xml b/Mage.Verify/pom.xml new file mode 100644 index 0000000000..64e0d4c0e9 --- /dev/null +++ b/Mage.Verify/pom.xml @@ -0,0 +1,76 @@ + + + 4.0.0 + + + org.mage + mage-root + 1.4.16 + + + mage-verify + jar + Mage Verify + + + + ${project.groupId} + mage + ${mage-version} + + + ${project.groupId} + mage-sets + ${mage-version} + + + junit + junit + test + + + com.fasterxml.jackson.core + jackson-databind + 2.6.3 + + + + log4j + log4j + jar + + + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + -Dfile.encoding=UTF-8 + + + + org.apache.maven.plugins + maven-compiler-plugin + + + + maven-resources-plugin + + UTF-8 + + + + + mage-verify + + + + + diff --git a/Mage.Verify/src/test/java/mage/verify/CompareWithMtgjsonTest.java b/Mage.Verify/src/test/java/mage/verify/CompareWithMtgjsonTest.java new file mode 100644 index 0000000000..b50370afdb --- /dev/null +++ b/Mage.Verify/src/test/java/mage/verify/CompareWithMtgjsonTest.java @@ -0,0 +1,182 @@ +package mage.verify; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import mage.ObjectColor; +import mage.cards.Card; +import mage.cards.CardImpl; +import mage.cards.CardSetInfo; +import mage.cards.ExpansionSet; +import mage.cards.Sets; +import mage.cards.SplitCard; +import mage.constants.CardType; +import mage.util.ClassScanner; +import org.junit.Test; + +import java.io.IOException; +import java.text.Normalizer; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.ListIterator; +import java.util.Map; +import java.util.Objects; + +public class CompareWithMtgjsonTest { + + @Test + public void testSets() throws IOException { + Collection sets = Sets.getInstance().values(); + + Map> reference = new ObjectMapper().readValue( + CompareWithMtgjsonTest.class.getResourceAsStream("AllCards.json"), + new TypeReference>>() {}); + + Map aliases = new HashMap<>(); + for (String name : reference.keySet()) { + String unaccented = stripAccents(name); + if (!name.equals(unaccented)) { + aliases.put(name, unaccented); + } + } + for (Map.Entry mapping : aliases.entrySet()) { + reference.put(mapping.getValue(), reference.get(mapping.getKey())); + } + + for (ExpansionSet set : sets) { + for (ExpansionSet.SetCardInfo setInfo : set.getSetCardInfo()) { + Card card = CardImpl.createCard(setInfo.getCardClass(), new CardSetInfo(setInfo.getName(), set.getCode(), + setInfo.getCardNumber(), setInfo.getRarity(), setInfo.getGraphicInfo())); + if (card.isSplitCard()) { + check(reference, ((SplitCard) card).getLeftHalfCard()); + check(reference, ((SplitCard) card).getRightHalfCard()); + } else { + check(reference, card); + } + } + } + } + + private String stripAccents(String str) { + String decomposed = Normalizer.normalize(str, Normalizer.Form.NFKD); + return decomposed.replaceAll("[\\p{InCombiningDiacriticalMarks}]", ""); + } + + private void check(Map> reference, Card card) { + String name = card.getName(); + Map 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); + } + if (ref == null) { + System.out.println("Missing card reference for " + card); + return; + } + checkAll(card, ref); + } + + private void checkAll(Card card, Map ref) { + checkCost(card, ref); + checkPT(card, ref); + checkSubtypes(card, ref); + checkSupertypes(card, ref); + checkTypes(card, ref); + checkColors(card, ref); + } + + private void checkColors(Card card, Map ref) { + Collection expected = (Collection) ref.get("colors"); + ObjectColor color = card.getColor(null); + if (expected == null) { + expected = Collections.emptyList(); + } + if (expected.size() != color.getColorCount() || + (color.isBlack() && !expected.contains("Black")) || + (color.isBlue() && !expected.contains("Blue")) || + (color.isGreen() && !expected.contains("Green")) || + (color.isRed() && !expected.contains("Red")) || + (color.isWhite() && !expected.contains("White"))) { + System.out.println(color + " != " + expected + " for " + card); + } + } + + private void checkSubtypes(Card card, Map ref) { + Collection expected = (Collection) ref.get("subtypes"); + if (expected != null && expected.contains("Urza’s")) { + expected = new ArrayList<>(expected); + for (ListIterator it = ((List) expected).listIterator(); it.hasNext();) { + if (it.next().equals("Urza’s")) { + it.set("Urza's"); + } + } + } + if (!eqSet(card.getSubtype(null), expected)) { + System.out.println(card.getSubtype(null) + " != " + expected + " for " + card); + } + } + + private void checkSupertypes(Card card, Map ref) { + Collection expected = (Collection) ref.get("supertypes"); + if (!eqSet(card.getSupertype(), expected)) { + System.out.println(card.getSupertype() + " != " + expected + " for " + card); + } + } + + private void checkTypes(Card card, Map ref) { + Collection expected = (Collection) ref.get("types"); + List type = new ArrayList<>(); + for (CardType cardType : card.getCardType()) { + type.add(cardType.toString()); + } + if (!eqSet(type, expected)) { + System.out.println(type + " != " + expected + " for " + card); + } + } + + private static boolean eqSet(Collection a, Collection b) { + if (a == null || a.isEmpty()) { + return b == null || b.isEmpty(); + } + return b != null && a.size() == b.size() && a.containsAll(b); + } + + private void checkPT(Card card, Map ref) { + String pt = card.getPower() + "/" + card.getToughness(); + String expected = ref.get("power") + "/" + ref.get("toughness"); + if ("0/0".equals(pt) && ("null/null".equals(expected) || "*/*".equals(expected))) { + // ok + } else if (!Objects.equals(pt, expected.replace("*", "0"))) { + System.out.println(pt + " != " + expected + " for " + card); + } + } + + private void checkCost(Card card, Map ref) { + String expected = (String) ref.get("manaCost"); + String cost = join(card.getManaCost().getSymbols()); + if ("".equals(cost)) { + cost = null; + } + if (cost != null) { + cost = cost.replaceAll("P\\}", "/P}"); + } + if (!Objects.equals(cost, expected)) { + System.out.println(cost + " != " + expected + " for " + card); + } + } + + private String join(Iterable items) { + StringBuilder result = new StringBuilder(); + for (Object item : items) { + result.append(item); + } + return result.toString(); + } + +} diff --git a/pom.xml b/pom.xml index 9c20bd429b..65e49f22b2 100644 --- a/pom.xml +++ b/pom.xml @@ -61,6 +61,7 @@ Mage.Tests Mage.Updater Mage.Stats + Mage.Verify