Skip to content

Commit

Permalink
Add sanitization for reversed hybrid mana symbols and reverse phyrexi…
Browse files Browse the repository at this point in the history
…an mana symbols
  • Loading branch information
ldeluigi committed Nov 27, 2024
1 parent a6308d9 commit 7cd61f6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
20 changes: 17 additions & 3 deletions backend/spellbook/models/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import unicodedata
from typing import Iterable

from ..regexs import MANA_SYMBOL, ORACLE_SYMBOL
from ..regexs import MANA_SYMBOL, ORACLE_SYMBOL, ORACLE_SYMBOL_EXTENDED
from ..parsers.scryfall_query_grammar import COMPARISON_OPERATORS, MANA_COMPARABLE_VARIABLES
from django.utils.text import normalize_newlines
from django.db.models import Expression, F, Value, TextChoices
Expand Down Expand Up @@ -116,14 +116,28 @@ def upper_oracle_symbols(text: str):
return re.sub(r'\{' + ORACLE_SYMBOL + r'\}', lambda m: m.group(0).upper(), text, flags=re.IGNORECASE)


def auto_fix_reverse_hybrid_mana(text: str):
def replace_reversed(text: str, color_combo: str):
return re.sub(r'\{' + '/'.join(reversed(color_combo)) + r'\}', r'{' + '/'.join(color_combo) + r'}', text, flags=re.IGNORECASE)
for color_combo in SORTED_COLORS.values():
match len(color_combo):
case 1:
text = replace_reversed(text, f'2{color_combo}')
text = replace_reversed(text, f'{color_combo}P')
case 2:
text = replace_reversed(text, color_combo)
return text


def auto_fix_missing_braces_to_oracle_symbols(text: str):
if re.compile(r'^' + ORACLE_SYMBOL + r'+$', flags=re.IGNORECASE).match(text):
return re.sub(r'\{?(' + ORACLE_SYMBOL + r')\}?', r'{\1}', text, flags=re.IGNORECASE)
if re.compile(r'^' + ORACLE_SYMBOL_EXTENDED + r'+$', flags=re.IGNORECASE).match(text):
return re.sub(r'\{?(' + ORACLE_SYMBOL_EXTENDED + r')\}?', r'{\1}', text, flags=re.IGNORECASE)
return text


def sanitize_mana(mana: str) -> str:
mana = auto_fix_missing_braces_to_oracle_symbols(mana)
mana = auto_fix_reverse_hybrid_mana(mana)
mana = re.sub(r'\{([WUBRG])P\}', r'{\1/P}', mana, flags=re.IGNORECASE)
mana = upper_oracle_symbols(mana)
return mana
Expand Down
1 change: 1 addition & 0 deletions backend/spellbook/regexs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
MANA_REGEX = r'^(?:(?:\{' + MANA_SYMBOL + r'\})[^\{\}\[\]]*)*$'
DOUBLE_SQUARE_BRACKET_TEXT_REGEX = r'^(?:[^\[]*(?:\[(?!\[)|\[{2}[^\[]+\]{2}|\[{3,}))*[^\[]*$'
ORACLE_SYMBOL = r'(?:(?:2\/[WUBRG]|W\/U|W\/B|B\/R|B\/G|U\/B|U\/R|R\/G|R\/W|G\/W|G\/U)(?:\/P)?|CHAOS|PW|TK|[WUBRG](?:\/P)?|[1-9][0-9]{1,2}|H[WUBRG]|[0-9CPXYZSTQEA½∞])'
ORACLE_SYMBOL_EXTENDED = r'(?:(?:(?:2|P)\/[WUBRG]|W\/U|W\/B|B\/R|B\/G|U\/B|U\/R|R\/G|R\/W|G\/W|G\/U|U\/W|B\/W|R\/B|G\/B|B\/U|R\/U|G\/R|W\/R|W\/G|U\/G)(?:\/(?:2|P))?|CHAOS|PW|TK|[WUBRG](?:\/(?:2|P))?|[1-9][0-9]{1,2}|H[WUBRG]|[0-9CPXYZSTQEA½∞])'
SYMBOLS_TEXT_REGEX = r'^(?:[^\{]*\{' + ORACLE_SYMBOL + r'\})*[^\{]*$'
ORDINARY_CHARACTERS_REGEX = r'^[\x0A\x0D\x20-\x7E\x80\x95\x99\xA1\xA9\xAE\xB0\xB1-\xB3\xBC-\xFF]*$'
RESERVED_CHARACTERS_REGEX = r'\$|\|'
12 changes: 12 additions & 0 deletions backend/spellbook/tests/test_models/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,18 @@ def test_fix_phyrexian(self):
self.assertEqual(sanitize_mana('{WP}'), '{W/P}')
self.assertEqual(sanitize_mana('{wp}'), '{W/P}')

def test_fix_hybrid(self):
self.assertEqual(sanitize_mana('2/W'), '{2/W}')
self.assertEqual(sanitize_mana('2/W/PU12BR/PG'), '{2/W/P}{U}{12}{B}{R/P}{G}')
self.assertEqual(sanitize_mana('{W/G}'), '{G/W}')
self.assertEqual(sanitize_mana('{W/G}{U}'), '{G/W}{U}')
self.assertEqual(sanitize_mana('{R/B}'), '{B/R}')
self.assertEqual(sanitize_mana('W/G'), '{G/W}')
self.assertEqual(sanitize_mana('W/GU'), '{G/W}{U}')
self.assertEqual(sanitize_mana('{W/2}'), '{2/W}')
self.assertEqual(sanitize_mana('W/2'), '{2/W}')
self.assertEqual(sanitize_mana('P/R'), '{R/P}')


class TestSanitizeScryfallQuery(TestCase):
def test_empty_string(self):
Expand Down

0 comments on commit 7cd61f6

Please sign in to comment.