ArgieAnon

No play.py edits online to local save

Dec 11th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.41 KB | None | 0 0
  1. from story.utils import *
  2. import json
  3. import uuid
  4. from subprocess import Popen
  5. import subprocess
  6. import os
  7.  
  8. class Story():
  9.  
  10. def __init__(self, story_start, context ="", seed=None, game_state=None, upload_story=False):
  11. self.story_start = story_start
  12. self.context = context
  13. self.rating = -1
  14. self.upload_story = upload_story
  15.  
  16. # list of actions. First action is the prompt length should always equal that of story blocks
  17. self.actions = []
  18.  
  19. # list of story blocks first story block follows prompt and is intro story
  20. self.results = []
  21.  
  22. # Only needed in constrained/cached version
  23. self.seed = seed
  24. self.choices = []
  25. self.possible_action_results = None
  26. self.uuid = None
  27.  
  28. if game_state is None:
  29. game_state = dict()
  30. self.game_state = game_state
  31. self.memory = 20
  32.  
  33. def __del__(self):
  34. if self.upload_story:
  35. self.save_to_storage()
  36. console_print("Game saved.")
  37. console_print("To load the game, type 'load' and enter the following ID: " + self.uuid)
  38.  
  39.  
  40. def init_from_dict(self, story_dict):
  41. self.story_start = story_dict["story_start"]
  42. self.seed = story_dict["seed"]
  43. self.actions = story_dict["actions"]
  44. self.results = story_dict["results"]
  45. self.choices = story_dict["choices"]
  46. self.possible_action_results = story_dict["possible_action_results"]
  47. self.game_state = story_dict["game_state"]
  48. self.context = story_dict["context"]
  49. self.uuid = story_dict["uuid"]
  50.  
  51. if "rating" in story_dict.keys():
  52. self.rating = story_dict["rating"]
  53. else:
  54. self.rating = -1
  55.  
  56.  
  57. def initialize_from_json(self, json_string):
  58. story_dict = json.loads(json_string)
  59. self.init_from_dict(story_dict)
  60.  
  61. def add_to_story(self, action, story_block):
  62. self.actions.append(action)
  63. self.results.append(story_block)
  64.  
  65. def latest_result(self):
  66.  
  67. mem_ind = self.memory
  68. if len(self.results) < 2:
  69. latest_result = self.story_start
  70. else:
  71. latest_result = self.context
  72. while mem_ind > 0:
  73.  
  74. if len(self.results) >= mem_ind:
  75. latest_result += (self.actions[-mem_ind] + self.results[-mem_ind])
  76.  
  77. mem_ind -= 1
  78.  
  79. return latest_result
  80.  
  81. def __str__(self):
  82. story_list = [self.story_start]
  83. for i in range(len(self.results)):
  84. story_list.append("\n" + self.actions[i] + "\n")
  85. story_list.append("\n" + self.results[i])
  86.  
  87. return "".join(story_list)
  88.  
  89. def to_json(self):
  90. story_dict = {}
  91. story_dict["story_start"] = self.story_start
  92. story_dict["seed"] = self.seed
  93. story_dict["actions"] = self.actions
  94. story_dict["results"] = self.results
  95. story_dict["choices"] = self.choices
  96. story_dict["possible_action_results"] = self.possible_action_results
  97. story_dict["game_state"] = self.game_state
  98. story_dict["context"] = self.context
  99. story_dict["uuid"] = self.uuid
  100. story_dict["rating"] = self.rating
  101.  
  102. return json.dumps(story_dict)
  103.  
  104. def save_to_local(self, save_name):
  105. self.uuid = str(uuid.uuid1())
  106. story_json = self.to_json()
  107. file_name = "AIDungeonSave_" + save_name + ".json"
  108. f = open(file_name, "w")
  109. f.write(story_json)
  110. f.close()
  111.  
  112. def load_from_local(self, save_name):
  113. file_name = "AIDungeonSave_" + save_name + ".json"
  114. print("Save ID that can be used to load game is: ", self.uuid)
  115.  
  116. with open(file_name, 'r') as fp:
  117. game = json.load(fp)
  118. self.init_from_dict(game)
  119.  
  120. def save_to_storage(self):
  121. self.uuid = str(uuid.uuid1())
  122.  
  123.  
  124. story_json = self.to_json()
  125. file_name = "stories\story" + str(self.uuid) + ".json"
  126. f = open(file_name, "w")
  127. f.write(story_json)
  128. f.close()
  129.  
  130. return self.uuid
  131.  
  132.  
  133. def load_from_storage(self, story_id):
  134.  
  135. file_name = "stories\story" + story_id + ".json"
  136. with open(file_name, 'r') as fp:
  137. game = json.load(fp)
  138. self.init_from_dict(game)
  139. return str(self)
  140.  
  141.  
  142.  
  143. class StoryManager():
  144.  
  145. def __init__(self, generator):
  146. self.generator = generator
  147. self.story = None
  148.  
  149. def start_new_story(self, story_prompt, context="", game_state=None, upload_story=False):
  150. block = self.generator.generate(context + story_prompt)
  151. block = cut_trailing_sentence(block)
  152. self.story = Story(context + story_prompt + block, context=context, game_state=game_state, upload_story=upload_story)
  153. return self.story
  154.  
  155. def load_story(self, story, from_json=False):
  156. if from_json:
  157. self.story = Story("")
  158. self.story.initialize_from_json(story)
  159. else:
  160. self.story = story
  161. return str(story)
  162.  
  163. def json_story(self):
  164. return self.story.to_json()
  165.  
  166. def story_context(self):
  167. return self.story.latest_result()
  168.  
  169.  
  170. class UnconstrainedStoryManager(StoryManager):
  171.  
  172. def act(self, action_choice):
  173.  
  174. result = self.generate_result(action_choice)
  175. self.story.add_to_story(action_choice, result)
  176. return result
  177.  
  178. def generate_result(self, action):
  179. block = self.generator.generate(self.story_context() + action)
  180. return block
  181.  
  182.  
  183. class ConstrainedStoryManager(StoryManager):
  184.  
  185. def __init__(self, generator, action_verbs_key="classic"):
  186. super().__init__(generator)
  187. self.action_phrases = get_action_verbs(action_verbs_key)
  188. self.cache = False
  189. self.cacher = None
  190. self.seed = None
  191.  
  192. def enable_caching(self, credentials_file=None, seed=0, bucket_name="dungeon-cache"):
  193. self.cache = True
  194. self.cacher = Cacher(credentials_file, bucket_name)
  195. self.seed = seed
  196.  
  197. def start_new_story(self, story_prompt, context="", game_state=None):
  198. if self.cache:
  199. return self.start_new_story_cache(story_prompt, game_state=game_state)
  200. else:
  201. return super().start_new_story(story_prompt, context=context, game_state=game_state)
  202.  
  203. def start_new_story_generate(self, story_prompt, game_state=None):
  204. super().start_new_story(story_prompt, game_state=game_state)
  205. self.story.possible_action_results = self.get_action_results()
  206. return self.story.story_start
  207.  
  208. def start_new_story_cache(self, story_prompt, game_state=None):
  209.  
  210. response = self.cacher.retrieve_from_cache(self.seed, [], "story")
  211. if response is not None:
  212. story_start = story_prompt + response
  213. self.story = Story(story_start, seed=self.seed)
  214. self.story.possible_action_results = self.get_action_results()
  215. else:
  216. story_start = self.start_new_story_generate(story_prompt, game_state=game_state)
  217. self.story.seed = self.seed
  218. self.cacher.cache_file(self.seed, [], story_start, "story")
  219.  
  220. return story_start
  221.  
  222. def load_story(self, story, from_json=False):
  223. story_string = super().load_story(story, from_json=from_json)
  224. return story_string
  225.  
  226. def get_possible_actions(self):
  227. if self.story.possible_action_results is None:
  228. self.story.possible_action_results = self.get_action_results()
  229.  
  230. return [action_result[0] for action_result in self.story.possible_action_results]
  231.  
  232. def act(self, action_choice_str):
  233.  
  234. try:
  235. action_choice = int(action_choice_str)
  236. except:
  237. print("Error invalid choice.")
  238. return None, None
  239.  
  240. if action_choice < 0 or action_choice >= len(self.action_phrases):
  241. print("Error invalid choice.")
  242. return None, None
  243.  
  244. self.story.choices.append(action_choice)
  245. action, result = self.story.possible_action_results[action_choice]
  246. self.story.add_to_story(action, result)
  247. self.story.possible_action_results = self.get_action_results()
  248. return result, self.get_possible_actions()
  249.  
  250. def get_action_results(self):
  251. if self.cache:
  252. return self.get_action_results_cache()
  253. else:
  254. return self.get_action_results_generate()
  255.  
  256. def get_action_results_generate(self):
  257. action_results = [self.generate_action_result(self.story_context(), phrase) for phrase in self.action_phrases]
  258. return action_results
  259.  
  260. def get_action_results_cache(self):
  261. response = self.cacher.retrieve_from_cache(self.story.seed, self.story.choices, "choices")
  262.  
  263. if response is not None:
  264. print("Retrieved from cache")
  265. return json.loads(response)
  266. else:
  267. print("Didn't receive from cache")
  268. action_results = self.get_action_results_generate()
  269. response = json.dumps(action_results)
  270. self.cacher.cache_file(self.story.seed, self.story.choices, response, "choices")
  271. return action_results
  272.  
  273. def generate_action_result(self, prompt, phrase, options=None):
  274.  
  275. action_result = phrase + " " + self.generator.generate(prompt + " " + phrase, options)
  276. action, result = split_first_sentence(action_result)
  277. return action, result
Add Comment
Please, Sign In to add comment