Merge pull request #16 from VE-FORBRYDERNE/failsafe
Download Story as JSON/Plaintext no longer requires server
This commit is contained in:
commit
12264bbdd8
33
aiserver.py
33
aiserver.py
|
@ -112,6 +112,7 @@ class vars:
|
||||||
bmsupported = False # Whether the breakmodel option is supported (GPT-Neo/GPT-J only, currently)
|
bmsupported = False # Whether the breakmodel option is supported (GPT-Neo/GPT-J only, currently)
|
||||||
smandelete = False # Whether stories can be deleted from inside the browser
|
smandelete = False # Whether stories can be deleted from inside the browser
|
||||||
smanrename = False # Whether stories can be renamed from inside the browser
|
smanrename = False # Whether stories can be renamed from inside the browser
|
||||||
|
laststory = None # Filename (without extension) of most recent story JSON file we loaded
|
||||||
acregex_ai = re.compile(r'\n* *>(.|\n)*') # Pattern for matching adventure actions from the AI so we can remove them
|
acregex_ai = re.compile(r'\n* *>(.|\n)*') # Pattern for matching adventure actions from the AI so we can remove them
|
||||||
acregex_ui = re.compile(r'^ *(>.*)$', re.MULTILINE) # Pattern for matching actions in the HTML-escaped story so we can apply colouring, etc (make sure to encase part to format in parentheses)
|
acregex_ui = re.compile(r'^ *(>.*)$', re.MULTILINE) # Pattern for matching actions in the HTML-escaped story so we can apply colouring, etc (make sure to encase part to format in parentheses)
|
||||||
actionmode = 1
|
actionmode = 1
|
||||||
|
@ -572,14 +573,21 @@ def do_connect():
|
||||||
setStartState()
|
setStartState()
|
||||||
sendsettings()
|
sendsettings()
|
||||||
refresh_settings()
|
refresh_settings()
|
||||||
|
vars.laststory = None
|
||||||
|
emit('from_server', {'cmd': 'setstoryname', 'data': vars.laststory}, broadcast=True)
|
||||||
sendwi()
|
sendwi()
|
||||||
|
emit('from_server', {'cmd': 'setmemory', 'data': vars.memory}, broadcast=True)
|
||||||
|
emit('from_server', {'cmd': 'setanote', 'data': vars.authornote}, broadcast=True)
|
||||||
vars.mode = "play"
|
vars.mode = "play"
|
||||||
else:
|
else:
|
||||||
# Game in session, send current game data and ready state to browser
|
# Game in session, send current game data and ready state to browser
|
||||||
refresh_story()
|
refresh_story()
|
||||||
sendsettings()
|
sendsettings()
|
||||||
refresh_settings()
|
refresh_settings()
|
||||||
|
emit('from_server', {'cmd': 'setstoryname', 'data': vars.laststory}, broadcast=True)
|
||||||
sendwi()
|
sendwi()
|
||||||
|
emit('from_server', {'cmd': 'setmemory', 'data': vars.memory}, broadcast=True)
|
||||||
|
emit('from_server', {'cmd': 'setanote', 'data': vars.authornote}, broadcast=True)
|
||||||
if(vars.mode == "play"):
|
if(vars.mode == "play"):
|
||||||
if(not vars.aibusy):
|
if(not vars.aibusy):
|
||||||
emit('from_server', {'cmd': 'setgamestate', 'data': 'ready'}, broadcast=True)
|
emit('from_server', {'cmd': 'setgamestate', 'data': 'ready'}, broadcast=True)
|
||||||
|
@ -1600,7 +1608,7 @@ def togglememorymode():
|
||||||
vars.mode = "memory"
|
vars.mode = "memory"
|
||||||
emit('from_server', {'cmd': 'memmode', 'data': 'true'}, broadcast=True)
|
emit('from_server', {'cmd': 'memmode', 'data': 'true'}, broadcast=True)
|
||||||
emit('from_server', {'cmd': 'setinputtext', 'data': vars.memory}, broadcast=True)
|
emit('from_server', {'cmd': 'setinputtext', 'data': vars.memory}, broadcast=True)
|
||||||
emit('from_server', {'cmd': 'setanote', 'data': vars.authornote})
|
emit('from_server', {'cmd': 'setanote', 'data': vars.authornote}, broadcast=True)
|
||||||
elif(vars.mode == "memory"):
|
elif(vars.mode == "memory"):
|
||||||
vars.mode = "play"
|
vars.mode = "play"
|
||||||
emit('from_server', {'cmd': 'memmode', 'data': 'false'}, broadcast=True)
|
emit('from_server', {'cmd': 'memmode', 'data': 'false'}, broadcast=True)
|
||||||
|
@ -1657,7 +1665,7 @@ def requestwi():
|
||||||
list = []
|
list = []
|
||||||
for wi in vars.worldinfo:
|
for wi in vars.worldinfo:
|
||||||
list.append(wi["num"])
|
list.append(wi["num"])
|
||||||
emit('from_server', {'cmd': 'requestwiitem', 'data': list}, broadcast=True)
|
emit('from_server', {'cmd': 'requestwiitem', 'data': list})
|
||||||
|
|
||||||
#==================================================================#
|
#==================================================================#
|
||||||
# Renumber WI items consecutively
|
# Renumber WI items consecutively
|
||||||
|
@ -1783,7 +1791,7 @@ def memsubmit(data):
|
||||||
emit('from_server', {'cmd': 'memmode', 'data': 'false'}, broadcast=True)
|
emit('from_server', {'cmd': 'memmode', 'data': 'false'}, broadcast=True)
|
||||||
|
|
||||||
# Ask for contents of Author's Note field
|
# Ask for contents of Author's Note field
|
||||||
emit('from_server', {'cmd': 'getanote', 'data': ''}, broadcast=True)
|
emit('from_server', {'cmd': 'getanote', 'data': ''})
|
||||||
|
|
||||||
#==================================================================#
|
#==================================================================#
|
||||||
# Commit changes to Author's Note
|
# Commit changes to Author's Note
|
||||||
|
@ -2111,7 +2119,14 @@ def loadRequest(loadpath):
|
||||||
vars.loadselect = ""
|
vars.loadselect = ""
|
||||||
|
|
||||||
# Refresh game screen
|
# Refresh game screen
|
||||||
|
filename = path.basename(loadpath)
|
||||||
|
if(filename.endswith('.json')):
|
||||||
|
filename = filename[:-5]
|
||||||
|
vars.laststory = filename
|
||||||
|
emit('from_server', {'cmd': 'setstoryname', 'data': vars.laststory}, broadcast=True)
|
||||||
sendwi()
|
sendwi()
|
||||||
|
emit('from_server', {'cmd': 'setmemory', 'data': vars.memory}, broadcast=True)
|
||||||
|
emit('from_server', {'cmd': 'setanote', 'data': vars.authornote}, broadcast=True)
|
||||||
refresh_story()
|
refresh_story()
|
||||||
emit('from_server', {'cmd': 'setgamestate', 'data': 'ready'}, broadcast=True)
|
emit('from_server', {'cmd': 'setgamestate', 'data': 'ready'}, broadcast=True)
|
||||||
emit('from_server', {'cmd': 'hidegenseqs', 'data': ''}, broadcast=True)
|
emit('from_server', {'cmd': 'hidegenseqs', 'data': ''}, broadcast=True)
|
||||||
|
@ -2228,7 +2243,11 @@ def importgame():
|
||||||
vars.savedir = getcwd()+"\stories"
|
vars.savedir = getcwd()+"\stories"
|
||||||
|
|
||||||
# Refresh game screen
|
# Refresh game screen
|
||||||
|
vars.laststory = None
|
||||||
|
emit('from_server', {'cmd': 'setstoryname', 'data': vars.laststory}, broadcast=True)
|
||||||
sendwi()
|
sendwi()
|
||||||
|
emit('from_server', {'cmd': 'setmemory', 'data': vars.memory}, broadcast=True)
|
||||||
|
emit('from_server', {'cmd': 'setanote', 'data': vars.authornote}, broadcast=True)
|
||||||
refresh_story()
|
refresh_story()
|
||||||
emit('from_server', {'cmd': 'setgamestate', 'data': 'ready'}, broadcast=True)
|
emit('from_server', {'cmd': 'setgamestate', 'data': 'ready'}, broadcast=True)
|
||||||
emit('from_server', {'cmd': 'hidegenseqs', 'data': ''}, broadcast=True)
|
emit('from_server', {'cmd': 'hidegenseqs', 'data': ''}, broadcast=True)
|
||||||
|
@ -2272,7 +2291,11 @@ def importAidgRequest(id):
|
||||||
vars.savedir = getcwd()+"\stories"
|
vars.savedir = getcwd()+"\stories"
|
||||||
|
|
||||||
# Refresh game screen
|
# Refresh game screen
|
||||||
|
vars.laststory = None
|
||||||
|
emit('from_server', {'cmd': 'setstoryname', 'data': vars.laststory}, broadcast=True)
|
||||||
sendwi()
|
sendwi()
|
||||||
|
emit('from_server', {'cmd': 'setmemory', 'data': vars.memory}, broadcast=True)
|
||||||
|
emit('from_server', {'cmd': 'setanote', 'data': vars.authornote}, broadcast=True)
|
||||||
refresh_story()
|
refresh_story()
|
||||||
emit('from_server', {'cmd': 'setgamestate', 'data': 'ready'}, broadcast=True)
|
emit('from_server', {'cmd': 'setgamestate', 'data': 'ready'}, broadcast=True)
|
||||||
|
|
||||||
|
@ -2329,7 +2352,11 @@ def newGameRequest():
|
||||||
vars.savedir = getcwd()+"\stories"
|
vars.savedir = getcwd()+"\stories"
|
||||||
|
|
||||||
# Refresh game screen
|
# Refresh game screen
|
||||||
|
vars.laststory = None
|
||||||
|
emit('from_server', {'cmd': 'setstoryname', 'data': vars.laststory}, broadcast=True)
|
||||||
sendwi()
|
sendwi()
|
||||||
|
emit('from_server', {'cmd': 'setmemory', 'data': vars.memory}, broadcast=True)
|
||||||
|
emit('from_server', {'cmd': 'setanote', 'data': vars.authornote}, broadcast=True)
|
||||||
setStartState()
|
setStartState()
|
||||||
|
|
||||||
def randomGameRequest(topic):
|
def randomGameRequest(topic):
|
||||||
|
|
|
@ -62,7 +62,9 @@ var rs_close;
|
||||||
var seqselmenu;
|
var seqselmenu;
|
||||||
var seqselcontents;
|
var seqselcontents;
|
||||||
|
|
||||||
|
var storyname = null;
|
||||||
var memorymode = false;
|
var memorymode = false;
|
||||||
|
var memorytext = "";
|
||||||
var gamestarted = false;
|
var gamestarted = false;
|
||||||
var editmode = false;
|
var editmode = false;
|
||||||
var connected = false;
|
var connected = false;
|
||||||
|
@ -184,7 +186,7 @@ function addImportLine(ob) {
|
||||||
function addWiLine(ob) {
|
function addWiLine(ob) {
|
||||||
if(ob.init) {
|
if(ob.init) {
|
||||||
if(ob.selective){
|
if(ob.selective){
|
||||||
wi_menu.append("<div class=\"wilistitem\">\
|
wi_menu.append("<div class=\"wilistitem wilistitem-selective "+(ob.constant ? "wilistitem-constant" : "")+"\">\
|
||||||
<div class=\"wiremove\">\
|
<div class=\"wiremove\">\
|
||||||
<button type=\"button\" class=\"btn btn-primary heightfull\" id=\"btn_wi"+ob.num+"\">X</button>\
|
<button type=\"button\" class=\"btn btn-primary heightfull\" id=\"btn_wi"+ob.num+"\">X</button>\
|
||||||
<button type=\"button\" class=\"btn btn-success heighthalf hidden\" id=\"btn_widel"+ob.num+"\">✓</button>\
|
<button type=\"button\" class=\"btn btn-success heighthalf hidden\" id=\"btn_widel"+ob.num+"\">✓</button>\
|
||||||
|
@ -205,7 +207,7 @@ function addWiLine(ob) {
|
||||||
</div>\
|
</div>\
|
||||||
</div>");
|
</div>");
|
||||||
} else {
|
} else {
|
||||||
wi_menu.append("<div class=\"wilistitem\">\
|
wi_menu.append("<div class=\"wilistitem "+(ob.constant ? "wilistitem-constant" : "")+"\">\
|
||||||
<div class=\"wiremove\">\
|
<div class=\"wiremove\">\
|
||||||
<button type=\"button\" class=\"btn btn-primary heightfull\" id=\"btn_wi"+ob.num+"\">X</button>\
|
<button type=\"button\" class=\"btn btn-primary heightfull\" id=\"btn_wi"+ob.num+"\">X</button>\
|
||||||
<button type=\"button\" class=\"btn btn-success heighthalf hidden\" id=\"btn_widel"+ob.num+"\">✓</button>\
|
<button type=\"button\" class=\"btn btn-success heighthalf hidden\" id=\"btn_widel"+ob.num+"\">✓</button>\
|
||||||
|
@ -282,18 +284,22 @@ function addWiLine(ob) {
|
||||||
});
|
});
|
||||||
$("#btn_wiselon"+ob.num).on("click", function () {
|
$("#btn_wiselon"+ob.num).on("click", function () {
|
||||||
enableWiSelective(ob.num);
|
enableWiSelective(ob.num);
|
||||||
|
$("#wikey"+ob.num).addClass("wilistitem-selective");
|
||||||
});
|
});
|
||||||
$("#btn_wiseloff"+ob.num).on("click", function () {
|
$("#btn_wiseloff"+ob.num).on("click", function () {
|
||||||
disableWiSelective(ob.num);
|
disableWiSelective(ob.num);
|
||||||
|
$("#wikey"+ob.num).removeClass("wilistitem-selective");
|
||||||
});
|
});
|
||||||
$("#constant-key-"+ob.num).on("click", function () {
|
$("#constant-key-"+ob.num).on("click", function () {
|
||||||
var element = $("#constant-key-"+ob.num);
|
var element = $("#constant-key-"+ob.num);
|
||||||
if(element.hasClass("constant-key-icon-enabled")) {
|
if(element.hasClass("constant-key-icon-enabled")) {
|
||||||
socket.send({'cmd': 'wiconstantoff', 'data': ob.num});
|
socket.send({'cmd': 'wiconstantoff', 'data': ob.num});
|
||||||
element.removeClass("constant-key-icon-enabled")
|
element.removeClass("constant-key-icon-enabled");
|
||||||
|
$("#wikey"+ob.num).removeClass("wilistitem-constant");
|
||||||
} else {
|
} else {
|
||||||
socket.send({'cmd': 'wiconstanton', 'data': ob.num});
|
socket.send({'cmd': 'wiconstanton', 'data': ob.num});
|
||||||
element.addClass("constant-key-icon-enabled");
|
element.addClass("constant-key-icon-enabled");
|
||||||
|
$("#wikey"+ob.num).addClass("wilistitem-constant");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -485,6 +491,9 @@ function returnWiList(ar) {
|
||||||
function dosubmit() {
|
function dosubmit() {
|
||||||
var txt = input_text.val();
|
var txt = input_text.val();
|
||||||
socket.send({'cmd': 'submit', 'actionmode': adventure ? action_mode : 0, 'data': txt});
|
socket.send({'cmd': 'submit', 'actionmode': adventure ? action_mode : 0, 'data': txt});
|
||||||
|
if(memorymode) {
|
||||||
|
memorytext = input_text.val();
|
||||||
|
}
|
||||||
input_text.val("");
|
input_text.val("");
|
||||||
hideMessage();
|
hideMessage();
|
||||||
hidegenseqs();
|
hidegenseqs();
|
||||||
|
@ -830,6 +839,57 @@ function submitEditedChunk(event) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function downloadStory(format) {
|
||||||
|
var filename_without_extension = storyname !== null ? storyname : "untitled";
|
||||||
|
|
||||||
|
var anchor = document.createElement('a');
|
||||||
|
|
||||||
|
var actionlist = $("chunk");
|
||||||
|
var actionlist_compiled = [];
|
||||||
|
for(var i = 0; i < actionlist.length; i++) {
|
||||||
|
actionlist_compiled.push(actionlist[i].innerText.replace(/\u00a0/g, " "));
|
||||||
|
}
|
||||||
|
|
||||||
|
if(format == "plaintext") {
|
||||||
|
var objectURL = URL.createObjectURL(new Blob(actionlist_compiled));
|
||||||
|
anchor.setAttribute('href', objectURL);
|
||||||
|
anchor.setAttribute('download', filename_without_extension + ".txt");
|
||||||
|
anchor.click();
|
||||||
|
URL.revokeObjectURL(objectURL);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var wilist = $(".wilistitem");
|
||||||
|
var wilist_compiled = [];
|
||||||
|
for(var i = 0; i < wilist.length-1; i++) {
|
||||||
|
var selective = wilist[i].classList.contains("wilistitem-selective");
|
||||||
|
wilist_compiled.push({
|
||||||
|
key: selective ? $("#wikeyprimary"+i).val() : $("#wikey"+i).val(),
|
||||||
|
keysecondary: $("#wikeysecondary"+i).val(),
|
||||||
|
content: $("#wientry"+i).val(),
|
||||||
|
selective: selective,
|
||||||
|
constant: wilist[i].classList.contains("wilistitem-constant"),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
var prompt = actionlist_compiled.shift();
|
||||||
|
if(prompt === undefined) {
|
||||||
|
prompt = "";
|
||||||
|
}
|
||||||
|
var objectURL = URL.createObjectURL(new Blob([JSON.stringify({
|
||||||
|
gamestarted: gamestarted,
|
||||||
|
prompt: prompt,
|
||||||
|
memory: memorytext,
|
||||||
|
authorsnote: $("#anoteinput").val(),
|
||||||
|
actions: actionlist_compiled,
|
||||||
|
worldinfo: wilist_compiled,
|
||||||
|
}, null, 4)]));
|
||||||
|
anchor.setAttribute('href', objectURL);
|
||||||
|
anchor.setAttribute('download', filename_without_extension + ".json");
|
||||||
|
anchor.click();
|
||||||
|
URL.revokeObjectURL(objectURL);
|
||||||
|
}
|
||||||
|
|
||||||
//=================================================================//
|
//=================================================================//
|
||||||
// READY/RUNTIME
|
// READY/RUNTIME
|
||||||
//=================================================================//
|
//=================================================================//
|
||||||
|
@ -980,6 +1040,8 @@ $(document).ready(function(){
|
||||||
} else if(msg.data == "start") {
|
} else if(msg.data == "start") {
|
||||||
setStartState();
|
setStartState();
|
||||||
}
|
}
|
||||||
|
} else if(msg.cmd == "setstoryname") {
|
||||||
|
storyname = msg.data;
|
||||||
} else if(msg.cmd == "editmode") {
|
} else if(msg.cmd == "editmode") {
|
||||||
// Enable or Disable edit mode
|
// Enable or Disable edit mode
|
||||||
if(msg.data == "true") {
|
if(msg.data == "true") {
|
||||||
|
@ -989,6 +1051,12 @@ $(document).ready(function(){
|
||||||
}
|
}
|
||||||
} else if(msg.cmd == "setinputtext") {
|
} else if(msg.cmd == "setinputtext") {
|
||||||
// Set input box text for memory mode
|
// Set input box text for memory mode
|
||||||
|
if(memorymode) {
|
||||||
|
memorytext = msg.data;
|
||||||
|
input_text.val(msg.data);
|
||||||
|
}
|
||||||
|
} else if(msg.cmd == "setmemory") {
|
||||||
|
memorytext = msg.data;
|
||||||
if(memorymode) {
|
if(memorymode) {
|
||||||
input_text.val(msg.data);
|
input_text.val(msg.data);
|
||||||
}
|
}
|
||||||
|
@ -1276,11 +1344,11 @@ $(document).ready(function(){
|
||||||
});
|
});
|
||||||
|
|
||||||
button_download.on("click", function(ev) {
|
button_download.on("click", function(ev) {
|
||||||
window.open("/download", "_blank");
|
downloadStory('json');
|
||||||
});
|
});
|
||||||
|
|
||||||
button_downloadtxt.on("click", function(ev) {
|
button_downloadtxt.on("click", function(ev) {
|
||||||
window.open("/download?format=plaintext", "_blank");
|
downloadStory('plaintext');
|
||||||
});
|
});
|
||||||
|
|
||||||
button_load.on("click", function(ev) {
|
button_load.on("click", function(ev) {
|
||||||
|
|
Loading…
Reference in New Issue