diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2022-01-04 14:37:27 +0100 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2022-01-04 14:37:27 +0100 |
| commit | 791a83da34ff35f7dc159e7f8d5f060f02c6bc5a (patch) | |
| tree | 769af1751e3bdf72c35d36790ddda9cf46a35cae /scripts/data.py | |
| parent | a62fde90aa946b0ea5516b1ae4591fc9f67ea22e (diff) | |
| download | muhqs-game-791a83da34ff35f7dc159e7f8d5f060f02c6bc5a.tar.gz muhqs-game-791a83da34ff35f7dc159e7f8d5f060f02c6bc5a.zip | |
improve scripts
* move more generic code handling data to data.py
* fix markdown generation in generate_deck.py
* make gen_hoverable_link more flexible by introducing callbacks
to generate the relevant link components.
* fix the link targets for hoverable links in the generated card markdown
Diffstat (limited to 'scripts/data.py')
| -rwxr-xr-x | scripts/data.py | 71 |
1 files changed, 64 insertions, 7 deletions
diff --git a/scripts/data.py b/scripts/data.py index b72fc094..fadf9613 100755 --- a/scripts/data.py +++ b/scripts/data.py @@ -7,10 +7,67 @@ CARDS_DATA_DIR = GAME_ROOT / 'data' / 'cards' CARDS_TO_SETS = {} SETS_TO_CARDS = {} -# build up the card 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 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('!', '') |
