From 1cc069a779e62d7df66f973b177f113b4c73aa18 Mon Sep 17 00:00:00 2001 From: KoboldAI Dev Date: Tue, 11 May 2021 00:27:34 -0400 Subject: [PATCH] Added ability to import AIDungeon games from AIDCAT --- aiserver.py | 79 +++++++++++++++++++++++++++++++++++++++ static/application.js | 65 +++++++++++++++++++++++++++++++- static/custom.css | 87 +++++++++++++++++++++++++++++++++++++++++++ templates/index.html | 19 ++++++++++ 4 files changed, 248 insertions(+), 2 deletions(-) diff --git a/aiserver.py b/aiserver.py index d6b173d9..cb108062 100644 --- a/aiserver.py +++ b/aiserver.py @@ -73,6 +73,8 @@ class vars: usegpu = False # Whether to launch pipeline with GPU support custmodpth = "" # Filesystem location of custom model to run formatoptns = {} # Container for state of formatting options + importnum = -1 # Selection on import popup list + importjs = {} # Temporary storage for import data #==================================================================# # Function to get model selection at startup @@ -319,6 +321,8 @@ def get_message(msg): saveRequest() elif(msg['cmd'] == 'load'): loadRequest() + elif(msg['cmd'] == 'import'): + importRequest() elif(msg['cmd'] == 'newgame'): newGameRequest() elif(msg['cmd'] == 'settemp'): @@ -370,6 +374,14 @@ def get_message(msg): if('frmtadsnsp' in vars.formatoptns): vars.formatoptns["frmtadsnsp"] = msg['data'] settingschanged() + elif(msg['cmd'] == 'importselect'): + vars.importnum = int(msg["data"][-1]) + elif(msg['cmd'] == 'importcancel'): + emit('from_server', {'cmd': 'popupshow', 'data': False}) + vars.importjs = {} + elif(msg['cmd'] == 'importaccept'): + emit('from_server', {'cmd': 'popupshow', 'data': False}) + importgame() #==================================================================# # @@ -941,6 +953,73 @@ def loadRequest(): refresh_story() emit('from_server', {'cmd': 'setgamestate', 'data': 'ready'}) +#==================================================================# +# Import an AIDungon game exported with Mimi's tool +#==================================================================# +def importRequest(): + importpath = fileops.getloadpath(vars.savedir, "Select AID CAT File", [("Json", "*.json")]) + + if(importpath): + # Leave Edit/Memory mode before continuing + exitModes() + + # Read file contents into JSON object + file = open(importpath, "r") + vars.importjs = json.load(file) + + # Clear Popup Contents + emit('from_server', {'cmd': 'clearpopup', 'data': ''}) + + # Initialize vars + num = 0 + vars.importnum = -1 + + # Get list of stories + for story in vars.importjs: + ob = {} + ob["num"] = num + ob["title"] = story["title"] + if(story["description"] != ""): + ob["descr"] = story["description"] + else: + ob["descr"] = "(No Description)" + ob["acts"] = len(story["actions"]) + emit('from_server', {'cmd': 'addimportline', 'data': ob}) + num += 1 + + # Show Popup + emit('from_server', {'cmd': 'popupshow', 'data': True}) + +#==================================================================# +# Import an AIDungon game selected in popup +#==================================================================# +def importgame(): + if(vars.importnum >= 0): + # Cache reference to selected game + ref = vars.importjs[vars.importnum] + + # Copy game contents to vars + vars.gamestarted = True + if(len(ref["actions"]) > 0): + vars.prompt = ref["actions"][0]["text"] + else: + vars.prompt = "" + vars.memory = ref["memory"] + vars.authornote = ref["authorsNote"] + vars.actions = [] + + # Get all actions except for prompt + if(len(ref["actions"]) > 1): + for act in ref["actions"][1:]: + vars.actions.append(act["text"]) + + # Clear import data + vars.importjs = {} + + # Refresh game screen + refresh_story() + emit('from_server', {'cmd': 'setgamestate', 'data': 'ready'}) + #==================================================================# # Starts a new story #==================================================================# diff --git a/static/application.js b/static/application.js index e0a2c9fa..e0d602c5 100644 --- a/static/application.js +++ b/static/application.js @@ -10,6 +10,7 @@ var connect_status; var button_newgame; var button_save; var button_load; +var button_import; var button_settings; var button_format; var button_send; @@ -27,6 +28,11 @@ var anote_menu; var anote_input; var anote_labelcur; var anote_slider; +var popup; +var popup_title; +var popup_content; +var popup_accept; +var popup_close; // Key states var shift_down = false; @@ -102,6 +108,23 @@ function addFormat(ob) { } } +function addImportLine(ob) { + popup_content.append("
\ +
"+ob.title+"
\ +
"+ob.acts+"
\ +
"+ob.descr+"
\ +
"); + $("#import"+ob.num).on("click", function () { + socket.send({'cmd': 'importselect', 'data': $(this).attr('id')}); + highlightImportLine($(this)); + }); +} + +function highlightImportLine(ref) { + $("#popupcontent > div").removeClass("popuplistselected"); + ref.addClass("popuplistselected"); +} + function enableButtons(refs) { for(i=0; iNew Story +
@@ -94,5 +95,23 @@ + \ No newline at end of file