diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2021-10-10 17:51:24 +0200 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2021-10-10 17:51:24 +0200 |
| commit | 1887f62868e92e9cd5e87f7cf6f44e70a75add44 (patch) | |
| tree | ddd00ef1e4a5daefb1d4d09e154c3353bba47769 /scripts | |
| parent | 8d13bf9760161498f53adbd3ede45450c725d07c (diff) | |
| download | muhqs-game-1887f62868e92e9cd5e87f7cf6f44e70a75add44.tar.gz muhqs-game-1887f62868e92e9cd5e87f7cf6f44e70a75add44.zip | |
[generate_card] generalize list filed handling, add babel
* Use lists wehere appropriate
* Fix some typos
* Rename thieve to thief
* units add \n after upkeep cost
Diffstat (limited to 'scripts')
| -rwxr-xr-x | scripts/generate_card.py | 79 |
1 files changed, 50 insertions, 29 deletions
diff --git a/scripts/generate_card.py b/scripts/generate_card.py index d403eac8..2b77f45b 100755 --- a/scripts/generate_card.py +++ b/scripts/generate_card.py @@ -2,16 +2,19 @@ import argparse import pathlib from pathlib import Path -from typing import MutableMapping, Tuple, Union +from typing import MutableMapping, Tuple import yaml +BABEL_LANGS = {'en': 'english', 'de': 'ngerman'} + CARD_TEMPLATE = \ """\\documentclass{{standalone}} +\\usepackage[{lang}]{{babel}} \\input{{common.tex}} \\begin{{document}} \\begin{{tikzpicture}} -{0} +{content} \\cardborder \\end{{tikzpicture}} \\end{{document}}""" @@ -34,7 +37,7 @@ ASSETS_DIR = GAME_ROOT / 'assets' # all keys known by the rules KEYS = [ 'type', 'token', 'buy', 'upkeep', 'play', 'ai', 'health', 'movement', - 'attack', 'full_action', 'effect' + 'attack', 'full_action', 'effect', 'use' ] FORMATTED_KEYS = { @@ -79,16 +82,46 @@ def get_formatted_key(key: str, language='en') -> str: def get_latex_field(card: MutableMapping, field: str, - language='en') -> Tuple[str, bool]: + language='en', + fmt_prefix='') -> Tuple[str, bool]: """Return the latex_ prefixed field from card or the field itself""" + normal_value = card[field] + latex_field = f'latex_{field}' - value = card.get(latex_field, card[field]) + has_latex = latex_field in card + if has_latex: + latex_value = card[latex_field] # internationalize - if isinstance(value, dict): - value = value.get(language, value['en']) + if isinstance(normal_value, dict): + normal_value = normal_value.get(language, normal_value['en']) + if has_latex: + latex_value = latex_value.get(language, normal_value) + + # NOTE: normal and latex_ fields must have the same data type + # (both string or both list of strings). + if has_latex: + assert type(latex_value) == type(normal_value) - return value, latex_field in card + # If the fetched value is not a list we are done here + if not isinstance(latex_value or normal_value, list): + return latex_value or normal_value, has_latex + + # Transform a list of values into a single string. + # normal fields are joined using '\\ ' + if not has_latex: + return '\\\\ '.join(normal_value), False + + # latex_* values are just concatenated. + + # NOTE: it is allowed for a latex_ field to be sparse. + # Missing entries in a latex_ field list are populated from the corrsponding + # entry of the normal field + for i in range(0, len(latex_value)): + if latex_value[i] is None: + latex_value[i] = f'{fmt_prefix}{normal_value[i]}// ' + + return ''.join(latex_value), True def generate_markdown(card: MutableMapping, language='en', indentation=3): @@ -133,7 +166,8 @@ def generate_latex(card: MutableMapping, language='en'): card_content += f'\\cardbackground{{{image_file.name}}}\n' break - card_content += f'\\cardtype{card["type"].capitalize()}{"Token" if "token" in card else ""}{{{name}}}\n' + card_content += f'\\cardtype{card["type"].capitalize()}' + card_content += f'{"Token" if "token" in card else ""}{{{name}}}\n' if 'buy' in card: card_content += f'\\cardbuycost{{{card["buy"]}}}\n' @@ -152,24 +186,9 @@ def generate_latex(card: MutableMapping, language='en'): abilities.append(effect) if 'full_action' in card: - full_actions: Union[str, list[str]] = '' - full_action_txt = '' - full_actions, is_latex = get_latex_field(card, 'full_action', - language) - if not isinstance(full_actions, list): - full_actions = [full_actions] - - for full_action in full_actions: - assert full_action - if not is_latex: - full_action_txt += f'\\faRedo: {full_action}\\\\ ' - else: - full_action_txt += f'{full_action}' - - # strip last '\\ ' - if full_action_txt.endswith('\\\\ '): - full_action_txt = full_action_txt[:-3] - abilities.append(full_action_txt) + full_action, _ = get_latex_field(card, 'full_action', language, + '\\faRedo: ') + abilities.append(full_action) unit_stats_str = '\\\\'.join(unit_stats) ability_block = '\\\\ \\vspace{0.2cm} '.join(abilities) @@ -181,7 +200,7 @@ def generate_latex(card: MutableMapping, language='en'): card_content += '\n' if 'upkeep' in card: - card_content += f'\\cardplaycost{{{card["upkeep"]}}}' + card_content += f'\\cardplaycost{{{card["upkeep"]}}}\n' elif card['type'] in ['spell', 'artifact']: effect, _ = get_latex_field(card, 'effect', language) @@ -211,7 +230,9 @@ def generate_latex(card: MutableMapping, language='en'): else: print(f'WARNING unknown set: {card["set"]}') - print(CARD_TEMPLATE.format(card_content)) + print( + CARD_TEMPLATE.format(lang=BABEL_LANGS.get(language, language), + content=card_content)) def main(): |
