generate_story.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import configparser
  2. import json
  3. import os
  4. import sys
  5. sys.path.append(os.path.abspath('.'))
  6. from puzzle import generate_derangement, Puzzle
  7. import random
  8. import argparse
  9. random.seed(202503210932)
  10. def parse_ini_to_json(ini_path):
  11. config = configparser.ConfigParser()
  12. config.read(ini_path, encoding="utf-8")
  13. levels = []
  14. level_names = [section for section in config.sections() if section != "Metadata"]
  15. global_source = config["Metadata"]["global_source"]
  16. for h, level_name in enumerate(level_names):
  17. i = h + 1
  18. level_data = config[level_name]
  19. key = generate_derangement()
  20. letter_mappings = {}
  21. for hint in level_data.get("letter_hints", []):
  22. letter_mappings[key[hint]] = hint
  23. source = level_data.get("source", global_source)
  24. if 'quote' in level_data:
  25. puzzle = Puzzle({"quote": level_data.get("quote"), "source": source}, key, letter_mappings)
  26. else:
  27. puzzle = None
  28. levels.append({
  29. "id": str(i),
  30. "index": h + 10,
  31. "name": level_name,
  32. "pre_narrative": level_data.get("pre_narrative").split('\\n') if "pre_narrative" in level_data else None,
  33. "post_narrative": level_data.get("post_narrative").split('\\n') if "post_narrative" in level_data else None,
  34. "next_level": str(i + 1) if (h + 1) < len(level_names) else None,
  35. "ending": "win" if h == len(level_names) - 1 else None,
  36. "choices": None,
  37. "puzzle": None if puzzle is None else {
  38. "encrypted_text": puzzle.encrypted_text,
  39. "decrypted_text": puzzle.decrypted_text,
  40. "quote": puzzle.quote,
  41. "key": puzzle.key,
  42. "letter_mappings": puzzle.letter_mappings
  43. }
  44. })
  45. return {
  46. "name": config["Metadata"]["name"],
  47. "id": config["Metadata"]["id"],
  48. "init_level": "1",
  49. "levels": levels
  50. }
  51. def read_and_combine_files(directory):
  52. combined_data = []
  53. for filename in os.listdir(directory):
  54. filepath = os.path.join(directory, filename)
  55. if os.path.isfile(filepath):
  56. result = parse_ini_to_json(filepath)
  57. combined_data.append(result)
  58. return combined_data
  59. def main():
  60. parser = argparse.ArgumentParser(description="Combine story.ini files from a directory into a combined stories.json file.")
  61. parser.add_argument("input_dir", help="Directory containing the story.ini files to combine")
  62. parser.add_argument("output_file", help="Output stories.json file name")
  63. args = parser.parse_args()
  64. if not os.path.isdir(args.input_dir):
  65. print("Error: Provided directory does not exist.")
  66. return
  67. combined_data = read_and_combine_files(args.input_dir)
  68. with open(args.output_file, "w", encoding="utf-8") as json_file:
  69. json.dump(combined_data, json_file, indent=4)
  70. print(f"Combined data written to {args.output_file}")
  71. if __name__ == "__main__":
  72. main()