From d91147f01aedb78dd0ccd8b0d025201f0af4ac3c Mon Sep 17 00:00:00 2001 From: Oleg Agafonov Date: Wed, 14 Jun 2023 03:16:24 +0400 Subject: [PATCH] decks: improved deck import to support unicode card names from LTR, UGL and other sets, added verify tests for name converters (closes #10465) --- .../java/mage/verify/mtgjson/MtgJsonCard.java | 14 ++++++- .../java/mage/verify/VerifyCardDataTest.java | 38 +++++++++++++++++ .../java/mage/cards/decks/CardNameUtil.java | 42 ++++++++++++------- 3 files changed, 78 insertions(+), 16 deletions(-) diff --git a/Mage.Verify/src/main/java/mage/verify/mtgjson/MtgJsonCard.java b/Mage.Verify/src/main/java/mage/verify/mtgjson/MtgJsonCard.java index ec173ea9aa..39953671a6 100644 --- a/Mage.Verify/src/main/java/mage/verify/mtgjson/MtgJsonCard.java +++ b/Mage.Verify/src/main/java/mage/verify/mtgjson/MtgJsonCard.java @@ -65,6 +65,18 @@ public final class MtgJsonCard { return getNameAsFace(); } - return asciiName != null ? asciiName : name; + return getNameAsASCII(); + } + + public String getNameAsUnicode() { + return this.name; + } + + public String getNameAsASCII() { + return this.asciiName != null ? this.asciiName : this.name; + } + + public boolean isUseUnicodeName() { + return this.asciiName != null && this.name != null && !this.asciiName.equals(this.name); } } diff --git a/Mage.Verify/src/test/java/mage/verify/VerifyCardDataTest.java b/Mage.Verify/src/test/java/mage/verify/VerifyCardDataTest.java index ddae637c40..3ba53b105e 100644 --- a/Mage.Verify/src/test/java/mage/verify/VerifyCardDataTest.java +++ b/Mage.Verify/src/test/java/mage/verify/VerifyCardDataTest.java @@ -15,6 +15,7 @@ import mage.abilities.effects.common.counter.ProliferateEffect; import mage.abilities.effects.keyword.ScryEffect; import mage.abilities.keyword.*; import mage.cards.*; +import mage.cards.decks.CardNameUtil; import mage.cards.decks.DeckCardLists; import mage.cards.decks.importer.DeckImporter; import mage.cards.repository.*; @@ -2358,4 +2359,41 @@ public class VerifyCardDataTest { Assert.fail("Found " + errorsList.size() + " errors in the cubes, look at logs above for more details"); } } + + @Test + public void test_checkUnicodeCardNamesForImport() { + // deck import can catch real card names with non-ascii symbols like Arwen Undómiel, so it must be able to process it + + // check unicode card + MtgJsonCard card = MtgJsonService.cardFromSet("LTR", "Arwen Undomiel", "194"); + Assert.assertNotNull("test card must exists", card); + Assert.assertTrue(card.isUseUnicodeName()); + Assert.assertEquals("Arwen Undomiel", card.getNameAsASCII()); + Assert.assertEquals("Arwen Undómiel", card.getNameAsUnicode()); + Assert.assertEquals("Arwen Undomiel", card.getNameAsFull()); + + // mtga format can contain /// in the names, so check it too + // see https://github.com/magefree/mage/pull/9855 + Assert.assertEquals("Dusk // Dawn", CardNameUtil.normalizeCardName("Dusk /// Dawn")); + + // check all converters + Collection errorsList = new ArrayList<>(); + MtgJsonService.sets().values().forEach(jsonSet -> { + jsonSet.cards.forEach(jsonCard -> { + if (jsonCard.isUseUnicodeName()) { + String inName = jsonCard.getNameAsUnicode(); + String outName = CardNameUtil.normalizeCardName(inName); + String needOutName = jsonCard.getNameAsFace(); + if (!outName.equals(needOutName)) { + // how-to fix: add new unicode symbol in CardNameUtil.normalizeCardName + errorsList.add(String.format("error, found unsupported unicode symbol in %s - %s", inName, jsonSet.code)); + } + } + }); + }); + printMessages(errorsList); + if (errorsList.size() > 0) { + Assert.fail(String.format("Card name converters contains unsupported unicode symbols in %d cards, see logs above", errorsList.size())); + } + } } diff --git a/Mage/src/main/java/mage/cards/decks/CardNameUtil.java b/Mage/src/main/java/mage/cards/decks/CardNameUtil.java index 223b1105d0..89aed3c095 100644 --- a/Mage/src/main/java/mage/cards/decks/CardNameUtil.java +++ b/Mage/src/main/java/mage/cards/decks/CardNameUtil.java @@ -6,22 +6,34 @@ public class CardNameUtil { public static final Pattern CARD_NAME_PATTERN = Pattern.compile("[ !\"&',\\-./0-9:A-Za-z]+"); + /** + * Convert card names with unicode symbols to ascii, uses to deck import from a third party services + * + * @param name + * @return + */ public static String normalizeCardName(String name) { + // new symbols checks in verify test, no need to manually search it return name - .replace("&", "//") - .replace("///", "//") - .replace("Æ", "Ae") - .replace("ö", "A") - .replace("ö", "o") - .replace("û", "u") - .replace("í", "i") - .replace("â", "a") - .replace("á", "a") - .replace("à", "a") - .replace("é", "e") - .replace("ú", "u") - .replace("†", "+") - .replace("★", "*"); + .replace("&", "//") + .replace("///", "//") + .replace("Æ", "Ae") + .replace("ö", "A") + .replace("ö", "o") + .replace("û", "u") + .replace("í", "i") + .replace("â", "a") + .replace("á", "a") + .replace("à", "a") + .replace("é", "e") + .replace("ú", "u") + .replace("†", "+") + .replace("★", "*") + .replace("ó", "o") + .replace("ä", "a") + .replace("ü", "u") + .replace("É", "E") + .replace("ñ", "n") + .replace("®", ""); } - }