Merge pull request #78 from VE-FORBRYDERNE/patch

Allow remote mode to load from client-side story files
This commit is contained in:
henk717 2021-11-06 16:58:05 +01:00 committed by GitHub
commit 4af0d9dabd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 14 deletions

View File

@ -1,6 +1,6 @@
#==================================================================# #==================================================================#
# KoboldAI # KoboldAI
# Version: 1.16.3 # Version: 1.16.4
# By: KoboldAIDev and the KoboldAI Community # By: KoboldAIDev and the KoboldAI Community
#==================================================================# #==================================================================#
@ -647,6 +647,8 @@ def get_message(msg):
savetofile() savetofile()
elif(not vars.remote and msg['cmd'] == 'loadfromfile'): elif(not vars.remote and msg['cmd'] == 'loadfromfile'):
loadfromfile() loadfromfile()
elif(msg['cmd'] == 'loadfromstring'):
loadRequest(json.loads(msg['data']), filename=msg['filename'])
elif(not vars.remote and msg['cmd'] == 'import'): elif(not vars.remote and msg['cmd'] == 'import'):
importRequest() importRequest()
elif(msg['cmd'] == 'newgame'): elif(msg['cmd'] == 'newgame'):
@ -2096,14 +2098,21 @@ def loadfromfile():
#==================================================================# #==================================================================#
# Load a stored story from a file # Load a stored story from a file
#==================================================================# #==================================================================#
def loadRequest(loadpath): def loadRequest(loadpath, filename=None):
if(loadpath): if(loadpath):
# Leave Edit/Memory mode before continuing # Leave Edit/Memory mode before continuing
exitModes() exitModes()
# Read file contents into JSON object # Read file contents into JSON object
file = open(loadpath, "r") if(isinstance(loadpath, str)):
js = json.load(file) 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 # Copy file contents to vars
vars.gamestarted = js["gamestarted"] vars.gamestarted = js["gamestarted"]
@ -2149,8 +2158,6 @@ def loadRequest(loadpath):
}) })
num += 1 num += 1
file.close()
# Save path for save button # Save path for save button
vars.savedir = loadpath vars.savedir = loadpath
@ -2158,10 +2165,10 @@ def loadRequest(loadpath):
vars.loadselect = "" vars.loadselect = ""
# Refresh game screen # Refresh game screen
filename = path.basename(loadpath) _filename = filename
if(filename.endswith('.json')): if(filename.endswith('.json')):
filename = filename[:-5] _filename = filename[:-5]
vars.laststory = filename vars.laststory = _filename
emit('from_server', {'cmd': 'setstoryname', 'data': vars.laststory}, broadcast=True) 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': 'setmemory', 'data': vars.memory}, broadcast=True)
@ -2169,7 +2176,7 @@ def loadRequest(loadpath):
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)
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 # Import an AIDungon game exported with Mimi's tool

View File

@ -77,6 +77,7 @@ var saved_prompt = "...";
var override_focusout = false; var override_focusout = false;
var sman_allow_delete = false; var sman_allow_delete = false;
var sman_allow_rename = 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] // This is true iff [we're in macOS and the browser is Safari] or [we're in iOS]
var using_webkit_patch = true; var using_webkit_patch = true;
@ -1516,7 +1517,8 @@ $(document).ready(function(){
// Update adventure state // Update adventure state
setadventure(msg.data); setadventure(msg.data);
} else if(msg.cmd == "runs_remotely") { } 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) { 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) { button_import.on("click", function(ev) {

View File

@ -6,17 +6,18 @@
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<script src="static/jquery-3.6.0.min.js"></script> <script src="static/jquery-3.6.0.min.js"></script>
<script src="static/socket.io.min.js"></script> <script src="static/socket.io.min.js"></script>
<script src="static/application.js?ver=1.16.2j"></script> <script src="static/application.js?ver=1.16.4a"></script>
<script src="static/bootstrap.min.js"></script> <script src="static/bootstrap.min.js"></script>
<script src="static/bootstrap-toggle.min.js"></script> <script src="static/bootstrap-toggle.min.js"></script>
<script src="static/rangy-core.min.js"></script> <script src="static/rangy-core.min.js"></script>
<link rel="stylesheet" href="static/bootstrap.min.css"> <link rel="stylesheet" href="static/bootstrap.min.css">
<link rel="stylesheet" href="static/bootstrap-toggle.min.css"> <link rel="stylesheet" href="static/bootstrap-toggle.min.css">
<link rel="stylesheet" href="static/custom.css?ver=1.16.2a"> <link rel="stylesheet" href="static/custom.css?ver=1.16.4a">
<link rel="stylesheet" href="static/open-iconic-bootstrap.min.css"> <link rel="stylesheet" href="static/open-iconic-bootstrap.min.css">
</head> </head>
<body> <body>
<input type="file" id="remote-save-select" accept="application/json" style="display:none">
<div class="container"> <div class="container">
<div class="row" id="topmenu"> <div class="row" id="topmenu">
<div id="menuitems"> <div id="menuitems">