Basic game finished!

This commit is contained in:
Nick
2019-04-06 13:58:29 -06:00
parent a6ecf2e855
commit d1ddb4ff9e
9 changed files with 167 additions and 44 deletions
Binary file not shown.
Binary file not shown.
+3 -1
View File
@@ -8,6 +8,8 @@ import gpt2.src.sample as sample
import gpt2.src.encoder as encoder
from utils import *
pos_action_starts = ["You attack", "You tell", "You use", "You go"]
class StoryGenerator():
@@ -53,7 +55,7 @@ class StoryGenerator():
return block
def generate_action_options(self, prompt, action_starts):
def generate_action_options(self, prompt, action_starts=pos_action_starts):
possible_actions = []
for phrase in action_starts:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+11 -6
View File
@@ -17,7 +17,9 @@ import datetime
from generator import StoryGenerator
import tensorflow as tf
from flask import g
import pdb
import json
from flask import Flask, render_template, request
app = Flask(__name__)
@@ -34,16 +36,19 @@ def root():
@app.route('/generate', methods=['POST'])
def story_request():
print("****Beginning Generation****")
is_story_block = request.form["story_block"] # is it a full story block or just an action?
print("****Generating Story****")
prompt = request.form["prompt"] # given prompt
gen_actions = request.form["actions"] # given prompt
print("\n Given prompt is: \n",prompt,"\n")
generator = get_generator()
if is_story_block:
response = generator.generate_story_block(prompt)
if gen_actions == "true":
response = generator.generate_action_options(prompt)
response = json.dumps(response)
else:
response = generator.generate_action_block(prompt)
response = generator.generate_story_block(prompt)
print("\nGenerated response is: \n", response)
print("")
+153 -37
View File
@@ -1,75 +1,191 @@
initial_prompt = "You enter a dungeon with your trusty sword and shield. You are searching for the evil necromancer who killed your family. You believe he is at the lowest level of the dungeon. You know you will encounter undead zombies and skeletons. You enter the first door and see"
start_text = "<span id='a'>Adventurer@AIDungeon</span>:<span id='b'>~</span><span id='c'>$</span> ./EnterDungeon \n <br/>"
var acceptInput=false
var inputStr = ""
var blinkCounter = 0
var StoryTracker = {
firstStory: null,
lastStory: null,
lastAction: null,
actions: null,
startPrompt: initial_prompt,
getFirstStory:function(){
console.log("Requesting first story")
Typer.appendToText(initial_prompt)
StoryTracker.requestStory(initial_prompt)
},
addNextStory:function(story){
StoryTracker.lastStory = story
if (StoryTracker.firstStory == null){
StoryTracker.firstStory = StoryTracker.startPrompt + story
}
StoryTracker.requestActions(StoryTracker.firstStory + StoryTracker.lastStory)
Typer.appendToText(story)
},
addNextActions:function(actions){
var actions = JSON.parse(actions)
StoryTracker.actions = actions
Typer.appendToText("\n\nOptions:")
Typer.appendToText("\n0) " + actions[0])
Typer.appendToText("\n1) " + actions[1])
Typer.appendToText("\n2) " + actions[2])
Typer.appendToText("\n3) " + actions[3])
Typer.appendToText("\nWhich action do you choose? ")
acceptInput = true
},
requestStory:function(prompt){
$.post("/generate", {actions: false, prompt},
StoryTracker.addNextStory)
},
requestActions:function(prompt){
$.post("/generate", {actions: true, prompt},
StoryTracker.addNextActions)
},
processInput:function(){
var choice_int = parseInt(inputStr, 10)
if(choice_int >= 0 && choice_int <= 3){
console.log("choice_int is %d", choice_int)
StoryTracker.lastAction = StoryTracker.actions[choice_int]
StoryTracker.requestStory(StoryTracker.firstStory + StoryTracker.lastStory + StoryTracker.lastAction)
}
else{
Typer.appendToText("Invalid choice. Must be a number from 0 to 3. \n")
Typer.appendToText("\nWhich action do you choose? ")
acceptInput=true
}
inputStr = ""
}
}
var Typer={
text: null,
accessCountimer:null,
index:0,
speed:2,
init: function(){
accessCountimer=setInterval(function(){Typer.blinkCursor();},500);
startBlinker: function(){
accessCountimer=setInterval(function(){Typer.blinkCursor()},500)
},
content:function(){
return $("#console").html();
return $("#console").html()
},
appendToText:function(str){
str = str.replace(".", "." + "<!-- laglaglaglaglaglaglag -->")
Typer.text = Typer.text + str;
},
addText:function(){
var cont=Typer.content();
if(cont.substring(cont.length-1,cont.length)=="|")
$("#console").html($("#console").html().substring(0,cont.length-1));
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))
Typer.index+=Typer.speed;
var text=Typer.text.substring(0,Typer.index)
var rtn= new RegExp("\n", "g");
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,"<br/>"));
window.scrollBy(0,50);
$("#console").html(text.replace(rtn,"<br/>"))
window.scrollBy(0,50)
}
},
blinkCursor:function(){
var cont=this.content();
if(blinkCounter > 10){
var cont=this.content()
if(cont.substring(cont.length-1,cont.length)=="|")
$("#console").html($("#console").html().substring(0,cont.length-1));
if(cont.substring(cont.length-1,cont.length)=="|")
$("#console").html($("#console").html().substring(0,cont.length-1))
else
$("#console").append("|");
else
$("#console").append("|")
}
else{
blinkCounter += 1
}
}
}
// request_story("Hello")
function requestStory(prompt){
$.post("/generate", { story_block: true, prompt},
startTyping);
function writeAppend(str){
$("#console").append(str)
}
function startTyping(story){
Typer.text=story
addTextTimer = setInterval("typeWords();", 40);
function startTyping(){
addTextTimer = setInterval("typeWords()", 35)
}
function typeWords() {
Typer.addText();
if (Typer.index > Typer.text.length) {
clearInterval(addTextTimer);
var choice = prompt("What's your choice?");
Typer.addText()
}
document.onkeypress = function(evt) {
if(acceptInput){
evt = evt || window.event
var charCode = evt.keyCode || evt.which
if(charCode == 13){
acceptInput = false
Typer.appendToText("\n\n")
StoryTracker.processInput(inputStr)
}
else{
requestStory(prompt);
// Here we probably want to call the request story function with the choice they made
var charStr = String.fromCharCode(charCode)
Typer.appendToText(charStr)
inputStr = inputStr + charStr
}
}
}
}
function start(){
Typer.speed=2;
Typer.speed=1
Typer.text = ""
Typer.appendToText(start_text)
StoryTracker.getFirstStory()
Typer.init();
startTyping("<span id='a'>Adventurer@DungeonDream</span>:<span id='b'>~</span><span id='c'>$</span> Hello Adventurer and welcome to my dungeon!");
startTyping()
Typer.startBlinker()
}
addTextTimer = null;
start();
$(document).ready(function() {
start()
})