#!/usr/bin/env python3 """Build the two D10 Roll20 sheets from explicit models and skill YAML.""" import argparse import hashlib import html import json from pathlib import Path import re import yaml ROOT = Path(__file__).resolve().parents[1] DATA = ROOT / 'data' IDENT = re.compile(r'^[A-Za-z][A-Za-z0-9_-]*$') COLOR = re.compile(r'^#[0-9a-fA-F]{6}$') TABS = (('core', 'Personnage'), ('skills', 'Compétences'), ('combat', 'Combat'), ('other', 'Autres')) def read_yaml(path): return yaml.safe_load(path.read_text(encoding='utf-8')) def ident(value): if not isinstance(value, str) or not IDENT.fullmatch(value): raise ValueError(f'invalid Roll20 identifier: {value!r}') return value def esc(value): return html.escape(str(value), quote=True) def validate(model, skills, theme): if model.get('version') != 1: raise ValueError('unsupported model version') for color in theme.values(): if not isinstance(color, str) or not COLOR.fullmatch(color): raise ValueError(f'invalid theme color: {color!r}') for required in ('background', 'panel', 'ink', 'accent', 'border'): if required not in theme: raise ValueError(f'missing theme color: {required}') fields = set() for item in model['attributes'] + model['resources']: key = ident(item['id']) if key in fields: raise ValueError(f'duplicate field: {key}') fields.add(key) for item in model.get('combat', []): key = ident(item['id']) if key in fields: raise ValueError(f'duplicate field: {key}') fields.add(key) for location in model.get('health_locations', []): ident(location) if not isinstance(model.get('weapons', 0), int) or not 0 <= model.get('weapons', 0) <= 10: raise ValueError('weapons must be an integer from 0 to 10') sections = skills['sections'] if not isinstance(sections, list) or not sections: raise ValueError('skills need sections') generated_fields = set() for section in sections: for group in section['groups']: prefix = ''.join(c for c in group['name'].replace(' ', '') if c.isascii() and c.isalpha()).lower() if not prefix: raise ValueError(f'group has no usable identifier: {group["name"]!r}') seen = set() for skill in group.get('skills', []): key = ident(skill['id']) if key in seen: raise ValueError(f'duplicate skill in {group["name"]}: {key}') seen.add(key) stem = f'{prefix}_skilllevel_{key}' if stem in generated_fields: raise ValueError(f'duplicate generated skill field: {stem}') generated_fields.add(stem) if skill.get('parent') and skill['parent'] not in seen: raise ValueError(f'unknown or later parent for {key}') for block in model['repeating']: ident(block['id']) for field in block['fields']: ident(field) def roll(label, threshold): expression = f'{threshold}-1d@{{dice}}cs1cf@{{dice}}' value = ('&{template:d10skillcheck} ' '{{name=@{character_name}}} ' f'{{{{roll_name={label}}}}} ' '{{dice_name=@{dice}}} ' f'{{{{threshold=[[{threshold}]]}}}} ' f'{{{{result=[[{expression}]]}}}}') return f'' def render_skill(group, skill, variant): # Cyberpunk names follow the original generator's subgroup prefix and ID. prefix = ''.join(c for c in group['name'].replace(' ', '') if c.isascii() and c.isalpha()).lower() if variant == 'base': stem = f'general_skilllevel_{skill["id"]}' name_field = skill['name'] else: stem = f'{prefix}_skilllevel_{skill["id"]}' name_field = skill['name'] threshold = f'@{{{stem}}}+@{{skill_{prefix}_attribute_select_{skill["id"]}}}' if skill.get('parent'): threshold += f'+@{{{prefix}_skilllevel_{skill["parent"]}}}' options = [('Force', 'For'), ('Agilite', 'Agi'), ('Perception', 'Per'), ('Charisme', 'Cha'), ('Intelligence', 'Int'), ('Volonte', 'Vol'), (('Psyche' if variant == 'base' else 'Accred'), ('Psy' if variant == 'base' else 'Acc')), ('Chance', 'Chn')] selector = f'skill_{prefix}_attribute_select_{skill["id"]}' choices = ''.join(f'' for key, short in options) depth = ' sheet-subskill' if skill.get('parent') else '' return (f'