142 lines
6.5 KiB
Python
142 lines
6.5 KiB
Python
"""Convert the pinned Ravenous monster SQLite database to Roll20 import JSON.
|
|
|
|
The sheet uses hunter formulas, so monster values without a faithful sheet field
|
|
remain in GM notes alongside the complete source rows.
|
|
"""
|
|
import argparse
|
|
import json
|
|
import re
|
|
import sqlite3
|
|
from pathlib import Path
|
|
|
|
if __package__:
|
|
from tools.build import read_glossary_labels, translate_typst
|
|
else: # Support the documented ``python tools/export_monsters.py`` invocation.
|
|
from build import read_glossary_labels, translate_typst
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
DEFAULT_DB = ROOT / "ravenous_typst/data/src/db/monsters.db"
|
|
|
|
|
|
def rows(db: sqlite3.Connection, table: str, creature_id: int) -> list[dict]:
|
|
order = {"creature_stats": "stats_id", "creature_body_parts": "body_part_id",
|
|
"creature_abilities": "attack_id", "creature_rewards": "reward_id"}[table]
|
|
return [dict(row) for row in db.execute(
|
|
f"SELECT * FROM {table} WHERE creature_id = ? ORDER BY {order}", (creature_id,))]
|
|
|
|
|
|
def text(value: object) -> str:
|
|
return "" if value is None else str(value)
|
|
|
|
|
|
def fixed_successes(value: object) -> int:
|
|
"""Read the monster database's fixed-success notation (for example ``4r``)."""
|
|
match = re.fullmatch(r"\s*(\d+)\s*[rR]?\s*", text(value))
|
|
return int(match.group(1)) if match else 0
|
|
|
|
|
|
def convert(db: sqlite3.Connection, creature_id: int) -> dict:
|
|
core_row = db.execute("SELECT * FROM creature WHERE id = ?", (creature_id,)).fetchone()
|
|
if core_row is None:
|
|
raise ValueError(f"No creature with ID {creature_id}")
|
|
core = dict(core_row)
|
|
stats_rows = rows(db, "creature_stats", creature_id)
|
|
if len(stats_rows) != 1:
|
|
raise ValueError(f"Expected one stats row for creature {creature_id}")
|
|
stats = stats_rows[0]
|
|
parts = rows(db, "creature_body_parts", creature_id)
|
|
abilities = rows(db, "creature_abilities", creature_id)
|
|
rewards = rows(db, "creature_rewards", creature_id)
|
|
glossary = read_glossary_labels()
|
|
attrs: dict[str, dict] = {}
|
|
|
|
def add(name: str, value: object, maximum: object = "") -> None:
|
|
attrs[name] = {"name": name, "current": text(value), "max": text(maximum)}
|
|
|
|
add("character_name", core["name"])
|
|
add("force", stats["strength"])
|
|
add("perception", stats["perception"])
|
|
add("volonte", stats["willpower"])
|
|
add("fatigue", stats["fatigue"])
|
|
add("sang", stats["blood_base"])
|
|
add("sang_max", stats["blood_base"])
|
|
add("resist_transcendental", stats["resistance_transcendentale"])
|
|
add("resist_alterations", stats["alteration_resistance"])
|
|
|
|
# Every body part is repeating: fixed hunter HP maxima would be recalculated.
|
|
for part in parts:
|
|
prefix = f"repeating_locations_dbpart{part['body_part_id']}_"
|
|
add(prefix + "name", part["body_part_name"])
|
|
add(prefix + "hp", part["life_points_p1"])
|
|
add(prefix + "hp_max", part["life_points_p1"])
|
|
add(prefix + "protection", part["protection_p1"])
|
|
add(prefix + "armor", part["integrity_p1"])
|
|
add(prefix + "armor_max", part["integrity_p1"])
|
|
|
|
for ability in abilities:
|
|
ident = f"dbability{ability['attack_id']}"
|
|
description = translate_typst("\n".join(filter(None, [
|
|
f"Phase: {text(ability['phase'])}",
|
|
f"Type: {text(ability['attack_type'])}",
|
|
f"Dégâts: {text(ability['damage_regular'])}",
|
|
f"Succès: {text(ability['success'])}",
|
|
text(ability["description_all"]), text(ability["effects"]),
|
|
])), glossary)
|
|
if ability["damage_regular"]:
|
|
prefix = f"repeating_attacks_{ident}_"
|
|
successes = fixed_successes(ability["success"])
|
|
add(prefix + "name", ability["name"])
|
|
add(prefix + "dice", 0) # The database does not specify the hunter roll pool.
|
|
add(prefix + "auto_successes", successes)
|
|
add(prefix + "roll_dice", 0)
|
|
add(prefix + "base_damage", 0) # Damage may be phase dependent or Typst markup.
|
|
add(prefix + "break_damage", ability["armor_break"] or 0)
|
|
add(prefix + "rmax", successes)
|
|
add(prefix + "parry_threshold", ability["threshold_parry"] or 0)
|
|
add(prefix + "dodge_threshold", ability["threshold_dodge"] or 0)
|
|
add(prefix + "reposition_threshold", ability["threshold_repositionning"] or 0)
|
|
add(prefix + "notes", description)
|
|
else:
|
|
prefix = f"repeating_powers_{ident}_"
|
|
add(prefix + "name", ability["name"])
|
|
add(prefix + "origin", core["name"])
|
|
add(prefix + "stage", 0)
|
|
add(prefix + "type", ability["attack_type"])
|
|
add(prefix + "description", description)
|
|
|
|
source = {"database_creature_id": creature_id, "creature": core,
|
|
"stats": stats, "body_parts": parts, "abilities": abilities,
|
|
"rewards": rewards}
|
|
return {"format": "ravenous-roll20-character", "version": 1,
|
|
"name": core["name"], "bio": translate_typst(text(core["description"]), glossary),
|
|
"gmnotes": json.dumps(source, ensure_ascii=False),
|
|
"attributes": list(attrs.values())}
|
|
|
|
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
group = parser.add_mutually_exclusive_group(required=True)
|
|
group.add_argument("--id", type=int, help="database creature ID")
|
|
group.add_argument("--all", action="store_true", help="export all master-visible creatures")
|
|
parser.add_argument("--output", type=Path, required=True,
|
|
help="JSON file for --id, directory for --all")
|
|
parser.add_argument("--database", type=Path, default=DEFAULT_DB)
|
|
args = parser.parse_args()
|
|
if not args.database.is_file():
|
|
parser.error("monster database missing; run git -C ravenous_typst submodule update --init data/src/db")
|
|
with sqlite3.connect(f"file:{args.database}?mode=ro", uri=True) as db:
|
|
db.row_factory = sqlite3.Row
|
|
ids = ([args.id] if args.id is not None else [row[0] for row in db.execute(
|
|
"SELECT id FROM creature WHERE show_to_master = 1 ORDER BY sort_id, id")])
|
|
if args.all:
|
|
args.output.mkdir(parents=True, exist_ok=True)
|
|
for creature_id in ids:
|
|
data = convert(db, creature_id)
|
|
path = args.output / f"monster-{creature_id}.json" if args.all else args.output
|
|
path.write_text(json.dumps(data, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
|
|
print(f"{creature_id}: {data['name']} -> {path}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|