generate_story.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. import configparser
  2. import json
  3. def parse_ini_to_json(ini_path):
  4. config = configparser.ConfigParser()
  5. config.read(ini_path, encoding="utf-8")
  6. levels = []
  7. level_names = [section for section in config.sections() if section != "Metadata"]
  8. for h, level_name in enumerate(level_names):
  9. i = h + 1
  10. level_data = config[level_name]
  11. levels.append({
  12. "id": str(i),
  13. "index": h + 10,
  14. "name": level_name,
  15. "pre_narrative": level_data.get("pre_narrative", ""),
  16. "post_narrative": level_data.get("post_narrative", ""),
  17. "next_level": str(i + 1) if i + 1 < len(level_names) else None,
  18. "decisions": None,
  19. "ending": "win" if h == len(level_names) - 1 else None
  20. })
  21. return json.dumps([{
  22. "name": config["Metadata"]["name"],
  23. "id": config["Metadata"]["id"],
  24. "init_level": "1",
  25. "levels": levels}], indent=4)
  26. ini_path = "story.ini"
  27. json_output = parse_ini_to_json(ini_path)
  28. print(json_output)