From bc0f9c80322034fa958243f9fe57ebd753faf8c9 Mon Sep 17 00:00:00 2001 From: henk717 Date: Fri, 5 Nov 2021 02:30:20 +0100 Subject: [PATCH 1/2] Allow remote mode to load from client-side story files (cherry picked from commit a1345263df42fba3345d1eecd119470a223bc4ee) --- aiserver.py | 25 ++++++++++++++++--------- static/application.js | 19 +++++++++++++++++-- templates/index.html | 1 + 3 files changed, 34 insertions(+), 11 deletions(-) diff --git a/aiserver.py b/aiserver.py index ff426b13..151b35a9 100644 --- a/aiserver.py +++ b/aiserver.py @@ -645,6 +645,8 @@ def get_message(msg): savetofile() elif(not vars.remote and msg['cmd'] == 'loadfromfile'): loadfromfile() + elif(msg['cmd'] == 'loadfromstring'): + loadRequest(json.loads(msg['data']), filename=msg['filename']) elif(not vars.remote and msg['cmd'] == 'import'): importRequest() elif(msg['cmd'] == 'newgame'): @@ -2094,14 +2096,21 @@ def loadfromfile(): #==================================================================# # Load a stored story from a file #==================================================================# -def loadRequest(loadpath): +def loadRequest(loadpath, filename=None): if(loadpath): # Leave Edit/Memory mode before continuing exitModes() # Read file contents into JSON object - file = open(loadpath, "r") - js = json.load(file) + if(isinstance(loadpath, str)): + with open(loadpath, "r") as file: + js = json.load(file) + if(filename is None): + filename = path.basename(loadpath) + else: + js = loadpath + if(filename is None): + filename = "untitled.json" # Copy file contents to vars vars.gamestarted = js["gamestarted"] @@ -2147,8 +2156,6 @@ def loadRequest(loadpath): }) num += 1 - file.close() - # Save path for save button vars.savedir = loadpath @@ -2156,10 +2163,10 @@ def loadRequest(loadpath): vars.loadselect = "" # Refresh game screen - filename = path.basename(loadpath) + _filename = filename if(filename.endswith('.json')): - filename = filename[:-5] - vars.laststory = filename + _filename = filename[:-5] + vars.laststory = _filename emit('from_server', {'cmd': 'setstoryname', 'data': vars.laststory}, broadcast=True) sendwi() emit('from_server', {'cmd': 'setmemory', 'data': vars.memory}, broadcast=True) @@ -2167,7 +2174,7 @@ def loadRequest(loadpath): refresh_story() emit('from_server', {'cmd': 'setgamestate', 'data': 'ready'}, broadcast=True) emit('from_server', {'cmd': 'hidegenseqs', 'data': ''}, broadcast=True) - print("{0}Story loaded from {1}!{2}".format(colors.GREEN, path.basename(loadpath), colors.END)) + print("{0}Story loaded from {1}!{2}".format(colors.GREEN, filename, colors.END)) #==================================================================# # Import an AIDungon game exported with Mimi's tool diff --git a/static/application.js b/static/application.js index 86bee330..1a67db2a 100644 --- a/static/application.js +++ b/static/application.js @@ -77,6 +77,7 @@ var saved_prompt = "..."; var override_focusout = false; var sman_allow_delete = false; var sman_allow_rename = false; +var remote = false; // This is true iff [we're in macOS and the browser is Safari] or [we're in iOS] var using_webkit_patch = true; @@ -1516,7 +1517,8 @@ $(document).ready(function(){ // Update adventure state setadventure(msg.data); } else if(msg.cmd == "runs_remotely") { - hide([button_loadfrfile, button_savetofile, button_import, button_importwi]); + remote = true; + hide([button_savetofile, button_import, button_importwi]); } }); @@ -1583,7 +1585,20 @@ $(document).ready(function(){ }); button_loadfrfile.on("click", function(ev) { - socket.send({'cmd': 'loadfromfile', 'data': ''}); + if(remote) { + $("#remote-save-select").click(); + } else { + socket.send({'cmd': 'loadfromfile', 'data': ''}); + } + }); + + $("#remote-save-select").on("change", function() { + var reader = new FileReader(); + var file = $("#remote-save-select")[0].files[0]; + reader.addEventListener("load", function(response) { + socket.send({'cmd': 'loadfromstring', 'filename': file.name, 'data': response.target.result}); + }, false); + reader.readAsText(file); }); button_import.on("click", function(ev) { diff --git a/templates/index.html b/templates/index.html index 1de19e28..78e1ee75 100644 --- a/templates/index.html +++ b/templates/index.html @@ -17,6 +17,7 @@ +