aboutsummaryrefslogtreecommitdiff
path: root/scripts/data.py
blob: fadf9613b0f8123f7910afabbe32b00f6aaf4101 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""Helper code for the data subdirectory"""
from pathlib import Path

GAME_ROOT = Path(__file__).parent.parent
CARDS_DATA_DIR = GAME_ROOT / 'data' / 'cards'

CARDS_TO_SETS = {}
SETS_TO_CARDS = {}


def populate_lookup_dicts():
    """Populate the global lookup dictionaries"""
    for set_dir in CARDS_DATA_DIR.iterdir():
        set_name = set_dir.name
        SETS_TO_CARDS[set_name] = []
        for card in set_dir.iterdir():
            SETS_TO_CARDS[set_name].append(card.stem)
            CARDS_TO_SETS[card.stem] = set_name


def name2set(card: str) -> str:
    """Return the set containing the card

    :param card: the english card name
    :returns: the set containing the card
    """

    if not CARDS_TO_SETS:
        populate_lookup_dicts()

    return CARDS_TO_SETS[card]


def set2names(set_name: str) -> list[str]:
    """Return a list of all card in a set

    :param set_name: the set name
    :returns: a list of all cards in the set
    """

    if not SETS_TO_CARDS:
        populate_lookup_dicts()

    return SETS_TO_CARDS[set_name]


def name2file(card: str) -> str:
    """Return the file name of a card

    File names are lower case card names with '_' instead of whitespace.

    :param card: the english name of a card
    :returns: the file name of the card
    """

    return card.lower().replace(' ', '_')


def name2cardlisting(card: str) -> str:
    """Return the card listing target

    The returned target can be used to directly navigate the
    card listing to the specific card.

    Card listing targets are all lower case and use '-' instead of
    whitespace.
    Ending '!' are stripped from the card names.

    :param card: the english name of a card
    :returns: the card listing target
    """

    return card.lower().replace(' ', '-').replace('!', '')