diff options
Diffstat (limited to 'scripts')
| -rwxr-xr-x | scripts/generate_card.py | 165 | ||||
| -rwxr-xr-x | scripts/generate_latex.py | 194 |
2 files changed, 165 insertions, 194 deletions
diff --git a/scripts/generate_card.py b/scripts/generate_card.py new file mode 100755 index 00000000..c11c039b --- /dev/null +++ b/scripts/generate_card.py @@ -0,0 +1,165 @@ +#!/usr/bin/env python3 +import argparse +import pathlib +import os +from typing import MutableMapping +import yaml + +CARD_TEMPLATE = \ +"""\\documentclass{{standalone}} +\\input{{common.tex}} + +\\begin{{document}} +\\begin{{tikzpicture}} +{0} +\\cardborder +\\end{{tikzpicture}} +\\end{{document}}""" + +SET_SYMBOLS = { + 'base': 'base', + 'magic': 'magic', + 'equipments': 'equipments', + 'nautics': 'nautics', + 'misc': '', + 'kraken': 'kraken' +} + +LATEX_ROOT = pathlib.Path(os.getcwd()) +ASSETS_DIR = LATEX_ROOT / '../assets' +CARDS_DIR = LATEX_ROOT / 'cards' + +def file_for_card(card_name: str) -> pathlib.Path: + """Return the corresponding latex file name for a card""" + return pathlib.Path(f'{card_name.lower().replace(" ", "_")}.tex') + +def get_latex_field(card: MutableMapping, field: str) -> str: + """Return the latex_ prefixed field from card or the field itself""" + latex_field = f'latex_{field}' + if latex_field in card: + return card[latex_field] + else: + return card[field] + +def generate_card(card: MutableMapping): + """Generate a standalone tikz picture with the card data""" + card_content = "" + + card_file = file_for_card(card['name']) + + for ext in ['.jpg', '.png']: + image_file = ASSETS_DIR / card_file.with_suffix(ext) + if image_file.exists(): + card_content += f'\\cardbackground{{{image_file.name}}}\n' + break + + card_content += f'\cardtype{card["type"].capitalize()}{"Token" if "token" in card else ""}{{{card["name"]}}}\n' + + if 'buy' in card: + card_content += f'\cardbuycost{{{card["buy"]}}}\n' + + if card['type'] == 'unit': + stats = [] + abilities = [] + if 'ai' in card: + stats.append(f'AI: {card["ai"]}') + + if 'health' in card: + stats.append(f'Health: {card["health"]}') + + if 'movement' in card: + stats.append(f'Movement: {card["movement"]}') + + attack = card.get('attack', None) + if attack: + # attack_sym = '\\faFistRaised' + # if "Range" in attack: + # attack_sym = '\\ding{246}' + stats.append(f'Attack: {attack}') + + if 'effect' in card: + abilities.append(get_latex_field(card, 'effect')) + + if 'full_action' in card: + if 'latex_full_action' in card: + abilities.append(f'{card["latex_full_action"]}') + else: + abilities.append(f'\\faRedo: {card["full_action"]}') + + stats_str = '\\\\'.join(stats) + ability_block = '\\\\ \\vspace{0.2cm} '.join(abilities) + if stats and abilities: + card_content += f'\cardsplitcontent{{{stats_str}}}{{{ability_block}}}' + elif stats: + card_content += f'\cardcontent{{{stats_str}}}' + + card_content += '\n' + + if 'upkeep' in card: + card_content += f'\cardplaycost{{{card["upkeep"]}}}' + + elif card['type'] in ['spell', 'artifact']: + effect = get_latex_field(card, 'effect') + print(card['effect']) + assert effect is not None + card_content += f'\cardcontent{{{effect}}}\n' + + elif card['type'] == 'equipment': + stats = f'Durability: {card["durability"]}' + effect = get_latex_field(card, 'effect') + card_content += f'\cardsplitcontent{{{stats}}}{{{effect}}}\n' + else: + print(f'WARNING: unknown card type {card["type"]}!') + + if 'play' in card: + card_content += f'\cardplaycost{{{card["play"]}}}\n' + + if card['set'] in SET_SYMBOLS: + card_content += f'\cardmodule{{{SET_SYMBOLS[card["set"]]}}}' + else: + print(f'WARNING unknown set: {card["set"]}') + + set_dir = CARDS_DIR / card['set'] + if not set_dir.exists(): + set_dir.mkdir(parents=True) + + with open(set_dir / card_file, 'w') as tex_file: + print(CARD_TEMPLATE.format(card_content), file=tex_file) + + +def main(): + """Generate one standalone tikz picture for each card in the input data""" + + parser = argparse.ArgumentParser(description='generate latex standalone cards') + parser.add_argument('data', help='yaml file or directory containing yaml files defining the cards to generate') + parser.add_argument('--latex-root', help='path to the latex root directory') + + args = parser.parse_args() + + if args.latex_root: + global LATEX_ROOT + LATEX_ROOT = pathlib.Path(args.latex_root) + global CARDS_DIR + CARDS_DIR = LATEX_ROOT / 'cards' + global ASSETS_DIR + ASSETS_DIR = LATEX_ROOT / '../assets' + + + data_files = [args.data] + data_path = pathlib.Path(args.data) + if data_path.is_dir(): + data_files = data_path.glob('*.yml') + card_set = data_path.stem + else: + card_set = data_path.parent.stem + + for f in data_files: + with open(f, 'r') as yaml_file: + card = yaml.full_load(yaml_file) + + card['set'] = card_set + generate_card(card) + + +if __name__ == '__main__': + main() diff --git a/scripts/generate_latex.py b/scripts/generate_latex.py deleted file mode 100755 index 71a99915..00000000 --- a/scripts/generate_latex.py +++ /dev/null @@ -1,194 +0,0 @@ -#!/usr/bin/env python3 -import argparse -import configparser -import pathlib -import os -from typing import MutableMapping - -COLLECTION_TEMPLATE = \ -"""\\documentclass[a4paper,landscape]{{article}} - -% Ränder des Dokuments anpassen -\\usepackage[margin=6mm,top=5mm]{{geometry}} - -% Pakete dazuladen -\\input{{libs.tex}} -% Farbdefinitionen laden -\\input{{colors.tex}} -% \card-Commands laden -\\input{{tikzcards.tex}} - -\\usepackage{{standalone}} - -\\begin{{document}} -\\begin{{center}} -\\pagestyle{{empty}} -{0} -\\end{{center}} -\\end{{document}} -""" - -CARD_TEMPLATE = \ -"""\\documentclass[]{{standalone}} - -% Pakete dazuladen -\\input{{libs.tex}} -% Farbdefinitionen laden -\\input{{colors.tex}} -% \card-Commands laden -\\input{{tikzcards.tex}} - -\\begin{{document}} -\\begin{{tikzpicture}} -{0} -\\cardborder -\\end{{tikzpicture}} -\\end{{document}} -""" - -SET_SYMBOLS = {'base': 'base', 'magic': 'magic', 'equipments': 'equipments', 'nautics': 'nautics', 'misc': '', 'kraken': 'kraken'} - -LATEX_ROOT = pathlib.Path(os.getcwd()) -ASSETS_DIR = LATEX_ROOT / '../assets' -CARDS_DIR = LATEX_ROOT / 'cards' - -def file_for_card(card_name: str) -> pathlib.Path: - """Return the corresponding latex file name for a card""" - return pathlib.Path(f'{card_name.lower().replace(" ", "_")}.tex') - -def generate_card(card_name: str, card: MutableMapping): - """Generate a standalone tikz picture with the card data""" - card_content = "" - - card_file = file_for_card(card_name) - - for ext in ['.jpg', '.png']: - image_file = ASSETS_DIR / card_file.with_suffix(ext) - if image_file.exists(): - card_content += f'\\cardbackground{{{image_file.name}}}\n' - break - - card_content += f'\cardtype{card["type"].capitalize()}{"Token" if "token" in card else ""}{{{card_name}}}\n' - # card_content += f'\cardtitle{{{card_name}}}\n' - - if 'buy' in card: - card_content += f'\cardbuycost{{{card["buy"]}}}\n' - - if card['type'] == 'unit': - stats = '' - if 'ai' in card: - stats += f'AI: {card["ai"]}' - - if 'health' in card: - if stats: - stats += '\\\\ ' - stats += f'Health: {card["health"]}' - - if 'movement' in card: - if stats: - stats += '\\\\ ' - stats += f'Movement: {card["movement"]}' - - attack = card.get('attack', None) - if attack: - attack_sym = '\\faFistRaised' - if "Range" in attack: - attack_sym = '\\ding{246}' - # stats += f'\\\\ {attack_sym}: {attack}' - if stats: - stats += '\\\\ ' - stats += f'Attack: {attack}' - - if 'effect' in card or 'full_action' in card: - ability_block = "" - - effect = card.get('effect', None) - if effect: - ability_block += f'{effect}' - - full_action = card.get('full_action', None) - if full_action: - if ability_block: - ability_block += '\\\\ \\vspace{0.2cm} ' - ability_block += f'\\faRedo: {full_action}' - - card_content += f'\cardsplitcontent{{{stats}}}{{{ability_block}}}' - elif stats: - card_content += f'\cardcontent{{{stats}}}' - - - card_content += '\n' - - if 'upkeep' in card: - card_content += f'\cardplaycost{{{card["upkeep"]}}}' - - elif card['type'] == 'spell': - card_content += f'\cardcontent{{{card["effect"]}}}\n' - - elif card['type'] == 'artifact': - card_content += f'\cardcontent{{{card["effect"]}}}\n' - - elif card['type'] == 'equipment': - # card_content += f'\cardsplitcontent{{\\faRecycle: {card["durability"]}}}{{{card["effect"]}}}' - card_content += f'\cardsplitcontent{{Durability: {card["durability"]}}}{{{card["effect"]}}}\n' - else: - print(f'WARNING: unknown card type {card["type"]}!') - - if 'play' in card: - card_content += f'\cardplaycost{{{card["play"]}}}\n' - - if card['set'] in SET_SYMBOLS: - card_content += f'\cardmodule{{{SET_SYMBOLS[card["set"]]}}}' - else: - print(f'WARNING unknown set: {card["set"]}') - - with open(CARDS_DIR / card_file, 'w') as tex_file: - print(CARD_TEMPLATE.format(card_content), file=tex_file) - - -def main(): - """Generate one standalone tikz picture for each card in the input data""" - - parser = argparse.ArgumentParser(description='generate latex standalone cards') - parser.add_argument('data', help='ini file or directory containing ini files defining the cards to generate') - parser.add_argument('--latex-root', help='path to the latex root directory') - - args = parser.parse_args() - - if args.latex_root: - global LATEX_ROOT - LATEX_ROOT = pathlib.Path(args.latex_root) - global CARDS_DIR - CARDS_DIR = LATEX_ROOT / 'cards' - global ASSETS_DIR - ASSETS_DIR = LATEX_ROOT / '../assets' - - - data_files = [args.data] - data_path = pathlib.Path(args.data) - if data_path.is_dir(): - data_files = data_path.glob('*.ini') - - for f in data_files: - data = configparser.ConfigParser() - data.read(f) - - collection_inputs = "" - cards = [card for card in data if card != 'DEFAULT'] - for i, card in enumerate(cards): - card_content = data[card] - card_content = dict(card_content) - card_content['set'] = pathlib.Path(f).stem - generate_card(card, card_content) - collection_inputs += f'\\input{{cards/{file_for_card(card)}}}\n' - if i == 0 or i % 4 < 3: - collection_inputs += f'\\hspace{{5mm}}\n' - else: - collection_inputs += f'\\vspace{{5mm}}\n\n' - - collection_path = pathlib.Path(f) - with open(LATEX_ROOT / 'sets' / f'{collection_path.stem}.tex', 'w') as collection_file: - print(COLLECTION_TEMPLATE.format(collection_inputs), file=collection_file) - -if __name__ == '__main__': - main() |
