diff --git a/play.py b/play.py index 211efcd..aa4c79e 100644 --- a/play.py +++ b/play.py @@ -5,6 +5,15 @@ from termios import tcflush, TCIFLUSH import time, sys, os os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3" +def splash(): + print("0) New Game\n1) Load Game\n") + choice = get_num_options(2) + + if choice == 1: + return "load" + else: + return "new" + def select_game(): with open(YAML_FILE, 'r') as stream: data = yaml.safe_load(stream) @@ -84,14 +93,25 @@ def play_aidungeon_2(): del story_manager.story print("\n\n") - context, prompt = select_game() - console_print(instructions()) - print("\nGenerating story...") - story_manager.start_new_story(prompt, context=context, upload_story=upload_story) + splash_choice = splash() + + if splash_choice == "new": + print("\n\n") + context, prompt = select_game() + console_print(instructions()) + print("\nGenerating story...") + + story_manager.start_new_story(prompt, context=context, upload_story=upload_story) + print("\n") + console_print(str(story_manager.story)) + + else: + load_ID = input("What is the ID of the saved game? ") + result = story_manager.load_new_story(load_ID) + print("\nLoading Game...\n") + print(result) - print("\n") - console_print(str(story_manager.story)) while True: tcflush(sys.stdin, TCIFLUSH) action = input("> ") @@ -209,4 +229,3 @@ def play_aidungeon_2(): if __name__ == '__main__': play_aidungeon_2() - diff --git a/story/story_manager.py b/story/story_manager.py index 5cbca84..0f35b15 100644 --- a/story/story_manager.py +++ b/story/story_manager.py @@ -152,13 +152,28 @@ class StoryManager(): def __init__(self, generator): self.generator = generator self.story = None - + def start_new_story(self, story_prompt, context="", game_state=None, upload_story=False): block = self.generator.generate(context + story_prompt) block = cut_trailing_sentence(block) self.story = Story(context + story_prompt + block, context=context, game_state=game_state, upload_story=upload_story) return self.story - + + def load_new_story(self, story_id): + file_name = "story" + story_id + ".json" + cmd = "gsutil cp gs://aidungeonstories/" + file_name + " ." + os.system(cmd) + exists = os.path.isfile(file_name) + + if exists: + with open(file_name, 'r') as fp: + game = json.load(fp) + self.story = Story("") + self.story.init_from_dict(game) + return str(self.story) + else: + return "Error: save not found." + def load_story(self, story, from_json=False): if from_json: self.story = Story("") @@ -281,4 +296,4 @@ class ConstrainedStoryManager(StoryManager): action_result = phrase + " " + self.generator.generate(prompt + " " + phrase, options) action, result = split_first_sentence(action_result) - return action, result \ No newline at end of file + return action, result