Skip to content

Commit

Permalink
Fix sorting of cards in PLST
Browse files Browse the repository at this point in the history
  • Loading branch information
gwax committed Aug 27, 2024
1 parent ff34054 commit 0ce69ba
Show file tree
Hide file tree
Showing 14 changed files with 1,395 additions and 746 deletions.
5 changes: 5 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ Development

- ...

2.6.9
-----

- Fix sorting of cards with collector number prefixes (primarily PLST).

2.6.1
-----

Expand Down
10 changes: 4 additions & 6 deletions mtg_ssm/containers/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@
from mtg_ssm.scryfall.models import ScryCard, ScryMigrationStrategy, ScrySet


def name_card_sort_key(card: ScryCard) -> Tuple[str, int, str]:
def name_card_sort_key(card: ScryCard) -> Tuple[str, str, int, str]:
"""Key function for sorting cards in a by-name list."""
card_num, card_var = util.collector_int_var(card)
return (card.set, card_num or 0, card_var or "") # TODO: sort by set release date
return (card.set, *util.collector_number_parts(card.collector_number))


def set_card_sort_key(card: ScryCard) -> Tuple[int, str]:
def set_card_sort_key(card: ScryCard) -> Tuple[str, int, str]:
"""Key function for sorting cards in a by-set list."""
card_num, card_var = util.collector_int_var(card)
return (card_num or 0, card_var or "")
return util.collector_number_parts(card.collector_number)


def build_snnmas(
Expand Down
34 changes: 13 additions & 21 deletions mtg_ssm/mtg/util.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
"""Utility functions for working with card data."""

import functools
import string
from typing import Optional, Tuple

from mtg_ssm.scryfall.models import ScryCard
import re
from typing import Tuple

STRICT_BASICS = frozenset({"Plains", "Island", "Swamp", "Mountain", "Forest"})


@functools.lru_cache(maxsize=None)
def dig_str(collector_number: str) -> Tuple[Optional[int], Optional[str]]:
"""Split a collector number into integer portion and non-digit portion."""
digpart = []
strpart = []
for char in collector_number:
if char in string.digits:
digpart.append(char)
else:
strpart.append(char)
if not digpart:
return (None, "".join(strpart))
return (int("".join(digpart)), "".join(strpart) or None)
COLLECTOR_NUMBER_RE = re.compile(r"(?P<prefix>.*-|\D+)?(?P<number>\d+)?(?P<suffix>\D.*)?")


def collector_int_var(card: ScryCard) -> Tuple[Optional[int], Optional[str]]:
"""Get the integer and variant portions of a card's collector number."""
return dig_str(card.collector_number)
@functools.lru_cache(maxsize=None)
def collector_number_parts(collector_number: str) -> Tuple[str, int, str]:
"""Split a collector number into its parts."""
match = COLLECTOR_NUMBER_RE.match(collector_number)
if not match:
return ("", 0, "")
prefix: str = match.group("prefix") or ""
number: int = int(match.group("number")) if match.group("number") else 0
suffix: str = match.group("suffix") or ""
return (prefix, number, suffix)


def is_strict_basic(card_name: str) -> bool:
Expand Down
9 changes: 9 additions & 0 deletions tests/containers/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ def test_setcode_to_cards(scryfall_data: ScryfallDataSet) -> None:
"74d",
]

# The List Sorting
assert [(c.collector_number, c.name) for c in index.setcode_to_cards["plst"]] == [
("10E-20", "High Ground"),
("10E-182", "Terror"),
("AKH-2", "Anointed Procession"),
("AKH-3", "Anointer Priest"),
("ARB-1", "Ardent Plea"),
]


def test_id_to_setindex(scryfall_data: ScryfallDataSet) -> None:
index = ScryfallDataIndex()
Expand Down
4 changes: 2 additions & 2 deletions tests/data/bulk_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"type": "default_cards",
"name": "Default Cards",
"description": "A JSON file containing every card object on Scryfall in English or the printed language if the card is only available in one language.",
"download_uri": "https://data.scryfall.io/default-cards/default-cards-20240510210636.json",
"updated_at": "2024-05-10T21:06:36.150000Z",
"download_uri": "https://data.scryfall.io/default-cards/default-cards-20240826212052.json",
"updated_at": "2024-08-26T21:20:52.469000Z",
"content_type": "application/json",
"content_encoding": "gzip"
}
Expand Down
Loading

0 comments on commit 0ce69ba

Please sign in to comment.