global_langchain.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. from peewee import *
  2. import json
  3. def singleton(cls):
  4. _instance = {}
  5. def inner(id=0, config_flag=True):
  6. if cls not in _instance:
  7. _instance[cls] = cls(id, config_flag)
  8. return _instance[cls]
  9. return inner
  10. @singleton
  11. class global_model():
  12. def __init__(self, id=0, config_flag=True):
  13. '''
  14. Database
  15. '''
  16. print("[Model]Global_model has been created!")
  17. self.db = SqliteDatabase('my_database.db')
  18. self.id = 0
  19. class BaseModel(Model):
  20. class Meta:
  21. database = self.db
  22. class GameData(BaseModel):
  23. id = AutoField(primary_key=True)
  24. intro = TextField()
  25. npc = TextField()
  26. value_system = TextField()
  27. win_condition = TextField()
  28. value_for_now = TextField()
  29. events = TextField()
  30. if_end = BooleanField()
  31. story = TextField()
  32. style = TextField()
  33. intro_declare = TextField()
  34. input_system_template = TextField()
  35. input_human_template = TextField()
  36. event_declare = TextField()
  37. event_system_template = TextField()
  38. event_human_template = TextField()
  39. value_declare = TextField()
  40. value_system_template = TextField()
  41. value_human_template = TextField()
  42. evaluate_declare = TextField()
  43. evaluate_system_template = TextField()
  44. evaluate_human_template = TextField()
  45. format_system_template = TextField()
  46. format_human_template = TextField()
  47. self.db.connect()
  48. self.db.create_tables([GameData])
  49. self.GameData = GameData
  50. print("[Model]Database initialized!")
  51. '''
  52. Input Config
  53. '''
  54. with open('config/input_config.json', 'r', encoding='utf-8') as f:
  55. data = json.load(f)
  56. self.story = data[
  57. "story"] if id == 0 and config_flag else self.GameData.get_by_id(
  58. id).story
  59. self.style = data[
  60. "style"] if id == 0 and config_flag else self.GameData.get_by_id(
  61. id).style
  62. self.intro_declare = data[
  63. "intro_declare"] if id == 0 and config_flag else self.GameData.get_by_id(
  64. id).intro_declare
  65. self.input_system_template = data[
  66. "system_template"] if id == 0 and config_flag else self.GameData.get_by_id(
  67. id).input_system_template
  68. self.input_human_template = data[
  69. "human_template"] if id == 0 and config_flag else self.GameData.get_by_id(
  70. id).input_human_template
  71. '''
  72. Event Config
  73. '''
  74. with open('config/event_config.json', 'r', encoding='utf-8') as f:
  75. data = json.load(f)
  76. self.event_declare = data[
  77. "event_declare"] if id == 0 and config_flag else self.GameData.get_by_id(
  78. id).event_declare
  79. self.event_system_template = data[
  80. "system_template"] if id == 0 and config_flag else self.GameData.get_by_id(
  81. id).event_system_template
  82. self.event_human_template = data[
  83. "human_template"] if id == 0 and config_flag else self.GameData.get_by_id(
  84. id).event_human_template
  85. '''
  86. Value Config
  87. '''
  88. with open('config/value_config.json', 'r', encoding='utf-8') as f:
  89. data = json.load(f)
  90. self.value_declare = data[
  91. "value_declare"] if id == 0 and config_flag else self.GameData.get_by_id(
  92. id).value_declare
  93. self.value_system_template = data[
  94. "system_template"] if id == 0 and config_flag else self.GameData.get_by_id(
  95. id).value_system_template
  96. self.value_human_template = data[
  97. "human_template"] if id == 0 and config_flag else self.GameData.get_by_id(
  98. id).value_human_template
  99. '''
  100. Evaluate Config
  101. '''
  102. with open('config/evaluate_config.json', 'r', encoding='utf-8') as f:
  103. data = json.load(f)
  104. self.evaluate_declare = data[
  105. "evaluate_declare"] if id == 0 and config_flag else self.GameData.get_by_id(
  106. id).evaluate_declare
  107. self.evaluate_system_template = data[
  108. "system_template"] if id == 0 and config_flag else self.GameData.get_by_id(
  109. id).evaluate_system_template
  110. self.evaluate_human_template = data[
  111. "human_template"] if id == 0 and config_flag else self.GameData.get_by_id(
  112. id).evaluate_human_template
  113. '''
  114. Format Config
  115. '''
  116. with open('config/format_config.json', 'r', encoding='utf-8') as f:
  117. data = json.load(f)
  118. self.format_system_template = data[
  119. "system_template"] if id == 0 and config_flag else self.GameData.get_by_id(
  120. id).format_system_template
  121. self.format_human_template = data[
  122. "human_template"] if id == 0 and config_flag else self.GameData.get_by_id(
  123. id).format_human_template
  124. if id != 0:
  125. self.id = id
  126. def get_intro_str(self):
  127. intro = ""
  128. intro += "故事简介:" + self.get_intro() + ","
  129. intro += "角色设定:" + str(self.get_npc()) + ","
  130. intro += "数值系统:" + str(self.get_value_system()) + ","
  131. intro += "游戏通关所需条件:" + self.get_win_condition() + ","
  132. intro += "玩家当前数值:" + str(self.get_value_for_now()) + ","
  133. intro += "已经发生的事件:" + str(self.get_events())
  134. return intro
  135. def input_init(self, intro, npc, value_system, win_condition):
  136. new_model = self.GameData.create(
  137. intro=intro,
  138. npc=str(npc),
  139. value_system=str(value_system),
  140. win_condition=win_condition,
  141. value_for_now=str([]),
  142. events=str([]),
  143. if_end=False,
  144. story=self.story,
  145. style=self.style,
  146. intro_declare=str(self.intro_declare),
  147. input_system_template=self.input_system_template,
  148. input_human_template=self.input_human_template,
  149. event_declare=str(self.event_declare),
  150. event_system_template=self.event_system_template,
  151. event_human_template=self.event_human_template,
  152. value_declare=str(self.value_declare),
  153. value_system_template=self.value_system_template,
  154. value_human_template=self.value_human_template,
  155. evaluate_declare=str(self.evaluate_declare),
  156. evaluate_system_template=self.evaluate_system_template,
  157. evaluate_human_template=self.evaluate_human_template,
  158. format_system_template=self.format_system_template,
  159. format_human_template=self.format_human_template)
  160. self.id = new_model.id
  161. def get_intro(self):
  162. try:
  163. intro = self.GameData.get(self.GameData.id == self.id).intro
  164. except:
  165. intro = ""
  166. return intro
  167. def get_npc(self):
  168. try:
  169. npc = eval(self.GameData.get(self.GameData.id == self.id).npc)
  170. except:
  171. npc = []
  172. return npc
  173. def get_value_system(self):
  174. try:
  175. value_system = eval(
  176. self.GameData.get(self.GameData.id == self.id).value_system)
  177. except:
  178. value_system = []
  179. return value_system
  180. def get_win_condition(self):
  181. try:
  182. win_condition = self.GameData.get(
  183. self.GameData.id == self.id).win_condition
  184. except:
  185. win_condition = ""
  186. return win_condition
  187. def get_value_for_now(self):
  188. try:
  189. value_for_now = eval(
  190. self.GameData.get(self.GameData.id == self.id).value_for_now)
  191. except:
  192. value_for_now = []
  193. return value_for_now
  194. def get_events(self):
  195. try:
  196. events = eval(
  197. self.GameData.get(self.GameData.id == self.id).events)
  198. except:
  199. events = []
  200. return events
  201. def add_event(self, event):
  202. try:
  203. events = eval(
  204. self.GameData.get(self.GameData.id == self.id).events)
  205. except:
  206. events = []
  207. events.append(event)
  208. self.GameData.update(events=str(events)).where(
  209. self.GameData.id == self.id).execute()
  210. def update_events(self, events):
  211. self.GameData.update(events=str(events)).where(
  212. self.GameData.id == self.id).execute()
  213. def update_value_for_now(self, value_for_now):
  214. self.GameData.update(value_for_now=str(value_for_now)).where(
  215. self.GameData.id == self.id).execute()
  216. def update_if_end(self, if_end):
  217. self.GameData.update(if_end=if_end).where(
  218. self.GameData.id == self.id).execute()
  219. def get_if_end(self):
  220. try:
  221. if_end = self.GameData.get(self.GameData.id == self.id).if_end
  222. except:
  223. if_end = False
  224. return if_end