From 90fd0d1b6a6713710d7eb9a2f0083c3d3e443f06 Mon Sep 17 00:00:00 2001 From: Nick Walton Date: Fri, 12 Apr 2019 18:44:09 -0600 Subject: [PATCH] refactor --- generate.sh | 4 +-- main.py | 33 +++++++++++++------ static/script.js | 84 +++++++++++++++++++++--------------------------- 3 files changed, 61 insertions(+), 60 deletions(-) diff --git a/generate.sh b/generate.sh index 9ccce8b..2a3713e 100755 --- a/generate.sh +++ b/generate.sh @@ -1,8 +1,8 @@ #!/bin/bash START=0 -END=15 +END=25 for ((I=START;I 1): diff --git a/static/script.js b/static/script.js index 0a455aa..d1a6e05 100644 --- a/static/script.js +++ b/static/script.js @@ -1,10 +1,5 @@ -var prompts = ["You enter a dungeon with your trusty sword and shield. You are searching for the evil necromancer who killed your family and have heard that he resides at the bottom of the dungeon, guarded by legions of the undead. You enter the first door and see"] - start_text = "Adventurer@AIDungeon:~$ ./EnterDungeon \n
" -input_form = '
' - - var acceptInput=false var action_waiting = false var inputStr = "" @@ -15,6 +10,7 @@ var action_list = ["You attack", "You tell", "You use", "You go"] var prompt_num = 0 var seed_max = 100 var seed_min = 0; +prompts = ["You enter a dungeon with your trusty sword and shield. You are searching for the evil necromancer who killed your family. You've heard that he resides at the bottom of the dungeon, guarded by legions of the undead. You enter the first door and see"] if(seed == -1){ var seed = Math.floor(Math.random() * (+seed_max - +seed_min)) + +seed_min; @@ -39,34 +35,26 @@ function buttonCheck(){ } var StoryTracker = { - firstStory: null, - lastStory: null, - lastAction: null, + lastActionResult: "", actions: [], results: [], choices: [], - action_int: 0, - startPrompt: prompts[prompt_num], + // Requests the first story getFirstStory:function(){ console.log("Requesting first story") - Typer.appendToText(StoryTracker.startPrompt) - StoryTracker.requestFirstStory(StoryTracker.startPrompt) + Typer.appendToText(prompts[prompt_num]) + StoryTracker.requestFirstStory() }, - addNextStory:function(story){ - StoryTracker.lastStory = story - if (StoryTracker.firstStory == null){ - StoryTracker.firstStory = StoryTracker.startPrompt + story - } - - StoryTracker.makeActionRequests(StoryTracker.firstStory + StoryTracker.lastStory) + addFirstStory:function(story){ + StoryTracker.lastActionResult = story + StoryTracker.makeActionRequests() Typer.appendToText(story) - action_waiting = true - setTimeout(StoryTracker.actionWait, 10000); }, + // Called after requesting options, prints generating msg if waits too lng actionWait:function(){ if(action_waiting == true){ @@ -80,12 +68,17 @@ var StoryTracker = { }, + // Callback for action request addNextAction:function(action_result){ - action_waiting = false - + // Response receieved no longer waiting var action_results = JSON.parse(action_result) Typer.appendToText("\n\nOptions:") + + action_waiting = false + StoryTracker.actions = [] + StoryTracker.results = [] + for (i = 0; i < 4; i++){ action_result = action_results[i] @@ -93,57 +86,52 @@ var StoryTracker = { action = action_result[0] result = action_result[1] - StoryTracker.actions.push(action) StoryTracker.results.push(result) - var print_action = "\n" + String(StoryTracker.action_int) + ") " + action - StoryTracker.action_int += 1 + var print_action = "\n" + String(i) + ") " + action Typer.appendToText(print_action) - if (StoryTracker.action_int > 3){ + if (i == 3){ Typer.appendToText("\nWhich action do you choose? ") - StoryTracker.action_int = 0 + + // Now we wait for the user to give input to us. acceptInput = true if(isMobileDevice()){ setTimeout(buttonCheck, 500); } } - } - + } }, - makeActionRequests:function(prompt){ - - StoryTracker.actions = [] - StoryTracker.results = [] - - StoryTracker.requestActions(prompt, JSON.stringify(StoryTracker.choices)) + // Make a request to the server for result actions + makeActionRequests:function(){ + action_waiting = true + setTimeout(StoryTracker.actionWait, 10000); + StoryTracker.requestActions(StoryTracker.lastActionResult, JSON.stringify(StoryTracker.choices)) }, - - requestFirstStory:function(prompt){ + requestFirstStory:function(){ $.post("/generate", {actions: false, seed, prompt_num}, - StoryTracker.addNextStory) + StoryTracker.addFirstStory) }, - requestActions:function(prompt, choices){ - $.post("/generate", {actions: true, seed, prompt_num, prompt, choices}, + requestActions:function(last_action_result, choices){ + $.post("/generate", {actions: true, seed, prompt_num, last_action_result, choices}, StoryTracker.addNextAction) }, + // Called once a choice has been made by button or entering. processInput:function(){ var choice_int = parseInt(inputStr, 10) if(choice_int >= 0 && choice_int <= 3){ console.log("choice_int is %d", choice_int) StoryTracker.choices.push(choice_int) - StoryTracker.lastAction = StoryTracker.actions[choice_int] - StoryTracker.lastStory = StoryTracker.results[choice_int] + StoryTracker.lastActionResult = StoryTracker.results[choice_int] StoryTracker.makeActionRequests(StoryTracker.firstStory + StoryTracker.lastStory) action_waiting = true - setTimeout(StoryTracker.actionWait, 10000); Typer.appendToText("\n") - Typer.appendToText(StoryTracker.lastStory) + Typer.appendToText(StoryTracker.lastActionResult) } else{ @@ -164,13 +152,14 @@ var StoryTracker = { } } +// Used to control the terminal like screen typing var Typer={ text: null, - accessCountimer:null, + accessBlinktimer:null, index:0, speed:2, startBlinker: function(){ - accessCountimer=setInterval(function(){Typer.blinkCursor()},500) + accessBlinktimer=setInterval(function(){Typer.blinkCursor()},500) }, content:function(){ @@ -237,7 +226,6 @@ function writeAppend(str){ function startTyping(){ addTextTimer = setInterval("typeWords()", 25) - }