configuration.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import json
  2. import os
  3. def singleton(cls):
  4. instances = {}
  5. def wrapper(*args, **kwargs):
  6. if cls not in instances:
  7. instances[cls] = cls(*args, **kwargs)
  8. return instances[cls]
  9. return wrapper
  10. @singleton
  11. class Configuration:
  12. def __init__(self, game_name):
  13. self.game_name = game_name
  14. def save_2_json(self,file_path,output_type,data):
  15. if output_type=="{class_output_data}":
  16. file_path='config/'+self.game_name+'/module_output/'+file_path
  17. os.makedirs(os.path.dirname(file_path), exist_ok=True)
  18. with open(file_path, 'w', encoding='utf-8') as f:
  19. json.dump(data, f, ensure_ascii=False, indent=4)
  20. elif output_type=="{class_output_data_append}":
  21. file_path='config/'+self.game_name+'/module_output/'+file_path
  22. os.makedirs(os.path.dirname(file_path), exist_ok=True)
  23. if os.path.exists(file_path):
  24. with open(file_path, 'r', encoding='utf-8') as f:
  25. existing_data = json.load(f)
  26. else:
  27. existing_data = {"class_output_data":[]}
  28. existing_data["class_output_data"].append(data)
  29. with open(file_path, 'w', encoding='utf-8') as f:
  30. json.dump(existing_data, f, ensure_ascii=False, indent=4)
  31. else:
  32. file_path='config/'+self.game_name+'/module_output/'+file_path
  33. os.makedirs(os.path.dirname(file_path), exist_ok=True)
  34. if os.path.exists(file_path):
  35. with open(file_path, 'r', encoding='utf-8') as f:
  36. existing_data = json.load(f)
  37. else:
  38. existing_data = {"class_output_data":[]}
  39. existing_data["class_output_data"].append(data)
  40. with open(file_path, 'w', encoding='utf-8') as f:
  41. json.dump(data, f, ensure_ascii=False, indent=4)
  42. def input_process(self,input,prompt_input):
  43. for input_key, input_value in input.items():
  44. with open('config/'+self.game_name+'/'+input_value,encoding='utf-8') as f:
  45. globals()[input_key] = json.load(f)
  46. prompt_input_values = {}
  47. for prompt_input_key, prompt_input_value in prompt_input.items():
  48. prompt_input_values[prompt_input_key] = eval(prompt_input_value.format(**locals()))
  49. return prompt_input_values
  50. def read_prompt(self,prompt_path):
  51. with open('config/'+self.game_name+'/'+prompt_path,encoding='utf-8') as f:
  52. prompt = json.load(f)
  53. return prompt
  54. def read_chat_config(self,classname,id):
  55. with open('config/'+self.game_name+'/modules_config.json',encoding='utf-8') as f:
  56. data = json.load(f)
  57. chats=data["Chat"]
  58. for chat in chats:
  59. if chat["id"]==id and chat["class"]==classname:
  60. return chat["input"],chat["prompt_input"],chat["prompt"],chat["output"],chat["next_module"]
  61. def read_judgement_config(self,classname,id):
  62. with open('config/'+self.game_name+'/modules_config.json',encoding='utf-8') as f:
  63. data = json.load(f)
  64. judgements=data["Judgement"]
  65. for judgement in judgements:
  66. if judgement["id"]==id and judgement["class"]==classname:
  67. return judgement["input"],judgement["judgement_input"],judgement["value"],judgement
  68. def read_interact_config(self,classname,id):
  69. with open('config/'+self.game_name+'/modules_config.json',encoding='utf-8') as f:
  70. data = json.load(f)
  71. interacts=data["Interact"]
  72. for interact in interacts:
  73. if interact["id"]==id and interact["class"]==classname:
  74. return interact["input"],interact["choices"],interact["output"],interact["next_module"]
  75. def interact_input_process(self,input,value):
  76. for input_key, input_value in input.items():
  77. with open('config/'+self.game_name+'/'+input_value,encoding='utf-8') as f:
  78. globals()[input_key] = json.load(f)
  79. input_value = eval(value.format(**locals()))
  80. return input_value
  81. def read_json(self,path):
  82. with open('config/'+path,encoding='utf-8') as f:
  83. data = json.load(f)
  84. return data