From dea73ebabae0c3c27b65e5e02117dd2f6b3ee6db Mon Sep 17 00:00:00 2001 From: Nick Date: Tue, 24 Sep 2019 06:56:17 -0600 Subject: [PATCH] reworked javascript now need to edit server and test --- main.py | 11 -- static/old_script.js | 285 +++++++++++++++++++++++++++++++++++++++++++ static/script.js | 268 +++++++++------------------------------- 3 files changed, 343 insertions(+), 221 deletions(-) create mode 100644 static/old_script.js diff --git a/main.py b/main.py index 2eff616..0d1be21 100644 --- a/main.py +++ b/main.py @@ -23,17 +23,6 @@ def root(): data = {'seed': seed} return render_template('index.html', data=data) -# Starts an adventure with a specific seed -@app.route('/') -def rootseed(seed): - if seed == "": - seed = -1 - else: - seed = int(seed) - data = {'seed': seed} - session["seed"] = seed - return render_template('index.html', data=data) - # Starts an adventure @app.route('/index.html') def index(): diff --git a/static/old_script.js b/static/old_script.js new file mode 100644 index 0000000..069d8f9 --- /dev/null +++ b/static/old_script.js @@ -0,0 +1,285 @@ +start_text = "Adventurer@AIDungeon:~$ ./EnterDungeon \n
" + +var acceptInput=false +var action_waiting = false +var inputStr = "" +var typing = false +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; +} + + +//var seed = 999 +console.log("Seed is ", seed) + +function isMobileDevice() { + return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) +}; + + +function buttonCheck(){ + if(typing == true){ + setTimeout(buttonCheck, 500); + } + else{ + document.getElementById('buttons').style.visibility='visible'; + } +} + +var StoryTracker = { + lastActionResult: "", + actions: [], + results: [], + choices: [], + + // Requests the first story + getFirstStory:function(){ + console.log("Requesting first story") + Typer.appendToText(prompts[prompt_num]) + StoryTracker.requestFirstStory() + + }, + + addFirstStory:function(story){ + StoryTracker.lastActionResult = story + StoryTracker.makeActionRequests() + Typer.appendToText(story) + }, + + // Called after requesting options, prints generating msg if waits too lng + actionWait:function(){ + + if(action_waiting == true){ + if(typing == true || acceptInput == true){ + setTimeout(StoryTracker.actionWait, 10000); + } + else{ + Typer.appendToText("\n\n Generating options... (~20s)") + } + } + + }, + + // Callback for action request + addNextAction:function(action_result){ + + // 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] + + action = action_result[0] + result = action_result[1] + + StoryTracker.results.push(result) + var print_action = "\n" + String(i) + ") " + action + Typer.appendToText(print_action) + + if (i == 3){ + Typer.appendToText("\nWhich action do you choose? ") + + // Now we wait for the user to give input to us. + acceptInput = true + + if(isMobileDevice()){ + setTimeout(buttonCheck, 500); + } + } + } + }, + + // 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(){ + $.post("/generate", {actions: false, seed, prompt_num}, + StoryTracker.addFirstStory) + }, + + 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.lastActionResult = StoryTracker.results[choice_int] + StoryTracker.makeActionRequests(StoryTracker.firstStory + StoryTracker.lastStory) + action_waiting = true + Typer.appendToText("\n") + Typer.appendToText(StoryTracker.lastActionResult) + } + else{ + + Typer.appendToText("Invalid choice. Must be a number from 0 to 3. \n") + Typer.appendToText("\nWhich action do you choose? ") + acceptInput = true + + + if(isMobileDevice()){ + setTimeout(buttonCheck, 500); + } + + + } + + + inputStr = "" + } +} + +// Used to control the terminal like screen typing +var Typer={ + text: null, + index:0, + speed:2, + + content:function(){ + return $("#console").html() + }, + + appendToText:function(str){ + str = str.replace(".", "." + "") + typing = true + Typer.text = Typer.text + str; + }, + + removeChar:function(){ + var cont=Typer.content() + $("#console").html($("#console").html().substring(0,cont.length-1)) + Typer.text = Typer.text.substring(0, Typer.text.length-1) + Typer.index = Typer.index - 1 + }, + + addText:function(){ + + if (Typer.index <= Typer.text.length) { + var cont=Typer.content() + if(cont.substring(cont.length-1,cont.length)=="|") + $("#console").html($("#console").html().substring(0,cont.length-1)) + + if (Typer.text.substring(Typer.index, Typer.index + Typer.speed).includes(".")){ + Typer.index += 1 + } + else{ + Typer.index+=Typer.speed + } + var text=Typer.text.substring(0,Typer.index) + var rtn= new RegExp("\n", "g") + + $("#console").html(text.replace(rtn,"
")) + } + else{ + typing = false + } + + }, +} + + +function writeAppend(str){ + $("#console").append(str) + +} + + +function startTyping(){ + addTextTimer = setInterval("typeWords()", 20) +} + + +function typeWords() { + Typer.addText() +} + +document.addEventListener("keydown", KeyCheck); + +function KeyCheck(evt) { + evt = evt || window.event + var charCode = evt.keyCode || evt.which + if(charCode == 8){ + if(inputStr.length > 0){ + console.log(inputStr) + inputStr = inputStr.substring(0, inputStr.length-1) + console.log(inputStr) + Typer.removeChar() + } + } +} + + +document.onkeypress = function(evt) { + + if(acceptInput && !isMobileDevice()){ + evt = evt || window.event + var charCode = evt.keyCode || evt.which + + if(charCode == 13){ + acceptInput = false + Typer.appendToText("\n") + StoryTracker.processInput(inputStr) + } + else{ + var charStr = String.fromCharCode(charCode) + Typer.appendToText(charStr) + inputStr = inputStr + charStr + } + + } +} + +function onButtonClick(num){ + + document.getElementById('buttons').style.visibility='hidden'; + + if (acceptInput == true){ + acceptInput = false + num = String(num) + Typer.appendToText(num) + Typer.appendToText("\n") + inputStr = num + StoryTracker.processInput() + } +} + +function start(){ + + Typer.speed=1 + Typer.text = "" + Typer.appendToText(start_text) + + StoryTracker.getFirstStory() + + startTyping() + + console.log("Not mobile device"); + document.getElementById('buttons').style.visibility='hidden'; +} + + +$(document).ready(function() { + start() +}) + diff --git a/static/script.js b/static/script.js index 069d8f9..6cec0bf 100644 --- a/static/script.js +++ b/static/script.js @@ -1,160 +1,13 @@ start_text = "Adventurer@AIDungeon:~$ ./EnterDungeon \n
" -var acceptInput=false -var action_waiting = false -var inputStr = "" -var typing = false -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; -} - - -//var seed = 999 -console.log("Seed is ", seed) - -function isMobileDevice() { - return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) -}; - - -function buttonCheck(){ - if(typing == true){ - setTimeout(buttonCheck, 500); - } - else{ - document.getElementById('buttons').style.visibility='visible'; - } -} - -var StoryTracker = { - lastActionResult: "", - actions: [], - results: [], - choices: [], - - // Requests the first story - getFirstStory:function(){ - console.log("Requesting first story") - Typer.appendToText(prompts[prompt_num]) - StoryTracker.requestFirstStory() - - }, - - addFirstStory:function(story){ - StoryTracker.lastActionResult = story - StoryTracker.makeActionRequests() - Typer.appendToText(story) - }, - - // Called after requesting options, prints generating msg if waits too lng - actionWait:function(){ - - if(action_waiting == true){ - if(typing == true || acceptInput == true){ - setTimeout(StoryTracker.actionWait, 10000); - } - else{ - Typer.appendToText("\n\n Generating options... (~20s)") - } - } - - }, - - // Callback for action request - addNextAction:function(action_result){ - - // 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] - - action = action_result[0] - result = action_result[1] - - StoryTracker.results.push(result) - var print_action = "\n" + String(i) + ") " + action - Typer.appendToText(print_action) - - if (i == 3){ - Typer.appendToText("\nWhich action do you choose? ") - - // Now we wait for the user to give input to us. - acceptInput = true - - if(isMobileDevice()){ - setTimeout(buttonCheck, 500); - } - } - } - }, - - // 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(){ - $.post("/generate", {actions: false, seed, prompt_num}, - StoryTracker.addFirstStory) - }, - - 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.lastActionResult = StoryTracker.results[choice_int] - StoryTracker.makeActionRequests(StoryTracker.firstStory + StoryTracker.lastStory) - action_waiting = true - Typer.appendToText("\n") - Typer.appendToText(StoryTracker.lastActionResult) - } - else{ - - Typer.appendToText("Invalid choice. Must be a number from 0 to 3. \n") - Typer.appendToText("\nWhich action do you choose? ") - acceptInput = true - - - if(isMobileDevice()){ - setTimeout(buttonCheck, 500); - } - - - } - - - inputStr = "" - } -} - // Used to control the terminal like screen typing var Typer={ text: null, + inputStr: "", index:0, speed:2, + acceptInput: false, + inputReady: false, content:function(){ return $("#console").html() @@ -196,89 +49,84 @@ var Typer={ } }, -} -function writeAppend(str){ - $("#console").append(str) + sendInput:function(){ + $.post("/generate", {Typer.inputStr}, receiveResponse) + Typer.inputStr = "" + }, -} + startTyping:function(){ + addTextTimer = setInterval("Typer.addText()", 20) + }, -function startTyping(){ - addTextTimer = setInterval("typeWords()", 20) -} - - -function typeWords() { - Typer.addText() -} - -document.addEventListener("keydown", KeyCheck); - -function KeyCheck(evt) { - evt = evt || window.event - var charCode = evt.keyCode || evt.which - if(charCode == 8){ - if(inputStr.length > 0){ - console.log(inputStr) - inputStr = inputStr.substring(0, inputStr.length-1) - console.log(inputStr) - Typer.removeChar() - } - } -} - - -document.onkeypress = function(evt) { - - if(acceptInput && !isMobileDevice()){ + KeyCheck:function(evt) { evt = evt || window.event var charCode = evt.keyCode || evt.which + if(charCode == 8){ + if(inputStr.length > 0){ + console.log(inputStr) + inputStr = inputStr.substring(0, inputStr.length-1) + console.log(inputStr) + Typer.removeChar() + } + } + }, - if(charCode == 13){ + onKeyPressFunc:function(evt) { + + if(acceptInput && !isMobileDevice()){ + evt = evt || window.event + var charCode = evt.keyCode || evt.which + + if(charCode == 13){ + acceptInput = false + Typer.sendInput() + } + else{ + var charStr = String.fromCharCode(charCode) + Typer.appendToText(charStr) + Typer.inputStr = Typer.inputStr + charStr + } + + } + }, + + onButtonClick:function(num){ + + document.getElementById('buttons').style.visibility='hidden'; + + if (acceptInput == true){ acceptInput = false - Typer.appendToText("\n") - StoryTracker.processInput(inputStr) + num = String(num) + Typer.appendToText(num) + Typer.inputStr = num + Typer.sendInput } - else{ - var charStr = String.fromCharCode(charCode) - Typer.appendToText(charStr) - inputStr = inputStr + charStr - } - - } + }, } -function onButtonClick(num){ +function receiveResponse(text){ - document.getElementById('buttons').style.visibility='hidden'; - - if (acceptInput == true){ - acceptInput = false - num = String(num) - Typer.appendToText(num) - Typer.appendToText("\n") - inputStr = num - StoryTracker.processInput() - } + Typer.appendToText(text) + Typer.acceptInput = true } function start(){ - + Typer.speed=1 Typer.text = "" Typer.appendToText(start_text) - - StoryTracker.getFirstStory() - - startTyping() + Typer.startTyping() + $.post("/generate", {""}, receiveResponse) console.log("Not mobile device"); - document.getElementById('buttons').style.visibility='hidden'; + document.getElementById('buttons').style.visibility='hidden'; } - +document.onkeypress = Typer.onKeyPressFunc +document.addEventListener("keydown", Typer.KeyCheck); $(document).ready(function() { start() })