This commit is contained in:
Gnome Ann
2021-08-24 17:27:50 -04:00
3 changed files with 86 additions and 14 deletions

8
.gitignore vendored
View File

@ -8,4 +8,10 @@ stories/*
*.bak *.bak
miniconda3/* miniconda3/*
*.settings *.settings
__pycache__ __pycache__
# Ignore PyCharm project files.
.idea
# Ignore compiled Python files.
*.pyc

View File

@ -10,6 +10,8 @@ import re
import tkinter as tk import tkinter as tk
from tkinter import messagebox from tkinter import messagebox
import json import json
from typing import Literal, Union
import requests import requests
import html import html
import argparse import argparse
@ -859,13 +861,13 @@ def actionsubmit(data, actionmode=0):
data = applyinputformatting(data) data = applyinputformatting(data)
# Store the result in the Action log # Store the result in the Action log
vars.actions.append(data) vars.actions.append(data)
update_story_chunk('last')
if(not vars.noai): if(not vars.noai):
# Off to the tokenizer! # Off to the tokenizer!
calcsubmit(data) calcsubmit(data)
emit('from_server', {'cmd': 'scrolldown', 'data': ''}, broadcast=True) emit('from_server', {'cmd': 'scrolldown', 'data': ''}, broadcast=True)
else: else:
refresh_story()
set_aibusy(0) set_aibusy(0)
emit('from_server', {'cmd': 'scrolldown', 'data': ''}, broadcast=True) emit('from_server', {'cmd': 'scrolldown', 'data': ''}, broadcast=True)
@ -882,7 +884,7 @@ def actionretry(data):
# Remove last action if possible and resubmit # Remove last action if possible and resubmit
if(len(vars.actions) > 0): if(len(vars.actions) > 0):
vars.actions.pop() vars.actions.pop()
refresh_story() remove_story_chunk(len(vars.actions) + 1)
calcsubmit('') calcsubmit('')
#==================================================================# #==================================================================#
@ -893,8 +895,9 @@ def actionback():
return return
# Remove last index of actions and refresh game screen # Remove last index of actions and refresh game screen
if(len(vars.actions) > 0): if(len(vars.actions) > 0):
action_index = len(vars.actions)
vars.actions.pop() vars.actions.pop()
refresh_story() remove_story_chunk(len(vars.actions) + 1)
#==================================================================# #==================================================================#
# Take submitted text and build the text to be given to generator # Take submitted text and build the text to be given to generator
@ -1153,7 +1156,7 @@ def genresult(genout):
# Add formatted text to Actions array and refresh the game screen # Add formatted text to Actions array and refresh the game screen
vars.actions.append(genout) vars.actions.append(genout)
refresh_story() update_story_chunk('last')
emit('from_server', {'cmd': 'texteffect', 'data': len(vars.actions)}, broadcast=True) emit('from_server', {'cmd': 'texteffect', 'data': len(vars.actions)}, broadcast=True)
#==================================================================# #==================================================================#
@ -1172,9 +1175,6 @@ def genselect(genout):
# Send sequences to UI for selection # Send sequences to UI for selection
emit('from_server', {'cmd': 'genseqs', 'data': genout}, broadcast=True) emit('from_server', {'cmd': 'genseqs', 'data': genout}, broadcast=True)
# Refresh story for any input text
refresh_story()
#==================================================================# #==================================================================#
# Send selected sequence to action log and refresh UI # Send selected sequence to action log and refresh UI
@ -1183,7 +1183,7 @@ def selectsequence(n):
if(len(vars.genseqs) == 0): if(len(vars.genseqs) == 0):
return return
vars.actions.append(vars.genseqs[int(n)]["generated_text"]) vars.actions.append(vars.genseqs[int(n)]["generated_text"])
refresh_story() update_story_chunk('last')
emit('from_server', {'cmd': 'texteffect', 'data': len(vars.actions)}, broadcast=True) emit('from_server', {'cmd': 'texteffect', 'data': len(vars.actions)}, broadcast=True)
emit('from_server', {'cmd': 'hidegenseqs', 'data': ''}, broadcast=True) emit('from_server', {'cmd': 'hidegenseqs', 'data': ''}, broadcast=True)
vars.genseqs = [] vars.genseqs = []
@ -1321,6 +1321,39 @@ def refresh_story():
text_parts.extend(('<chunk n="', str(idx), '" id="n', str(idx), '">', item, '</chunk>')) text_parts.extend(('<chunk n="', str(idx), '" id="n', str(idx), '">', item, '</chunk>'))
emit('from_server', {'cmd': 'updatescreen', 'gamestarted': vars.gamestarted, 'data': formatforhtml(''.join(text_parts))}, broadcast=True) emit('from_server', {'cmd': 'updatescreen', 'gamestarted': vars.gamestarted, 'data': formatforhtml(''.join(text_parts))}, broadcast=True)
#==================================================================#
# Signals the Game Screen to update one of the chunks
#==================================================================#
def update_story_chunk(idx: Union[int, Literal['last']]):
if idx == 'last':
if len(vars.actions) <= 1:
# In this case, we are better off just refreshing the whole thing as the
# prompt might not have been shown yet (with a "Generating story..."
# message instead).
refresh_story()
return
idx = len(vars.actions)
if idx == 0:
text = vars.prompt
else:
# Actions are 0 based, but in chunks 0 is the prompt.
# So the chunk index is one more than the corresponding action index.
text = vars.actions[idx - 1]
chunk_text = f'<chunk n="{idx}" id="n{idx}">{formatforhtml(html.escape(text))}</chunk>'
emit('from_server', {'cmd': 'updatechunk', 'data': {'index': idx, 'html': chunk_text, 'last': (idx == len(vars.actions))}})
#==================================================================#
# Signals the Game Screen to remove one of the chunks
#==================================================================#
def remove_story_chunk(idx: int):
emit('from_server', {'cmd': 'removechunk', 'data': idx})
#==================================================================# #==================================================================#
# Sends the current generator settings to the Game Menu # Sends the current generator settings to the Game Menu
#==================================================================# #==================================================================#
@ -1389,7 +1422,7 @@ def editsubmit(data):
vars.actions[vars.editln-1] = data vars.actions[vars.editln-1] = data
vars.mode = "play" vars.mode = "play"
refresh_story() update_story_chunk(vars.editln)
emit('from_server', {'cmd': 'texteffect', 'data': vars.editln}, broadcast=True) emit('from_server', {'cmd': 'texteffect', 'data': vars.editln}, broadcast=True)
emit('from_server', {'cmd': 'editmode', 'data': 'false'}) emit('from_server', {'cmd': 'editmode', 'data': 'false'})
@ -1404,7 +1437,7 @@ def deleterequest():
else: else:
del vars.actions[vars.editln-1] del vars.actions[vars.editln-1]
vars.mode = "play" vars.mode = "play"
refresh_story() remove_story_chunk(vars.editln)
emit('from_server', {'cmd': 'editmode', 'data': 'false'}) emit('from_server', {'cmd': 'editmode', 'data': 'false'})
#==================================================================# #==================================================================#
@ -1658,7 +1691,7 @@ def ikrequest(txt):
genout = req.json()["data"]["text"] genout = req.json()["data"]["text"]
print("{0}{1}{2}".format(colors.CYAN, genout, colors.END)) print("{0}{1}{2}".format(colors.CYAN, genout, colors.END))
vars.actions.append(genout) vars.actions.append(genout)
refresh_story() update_story_chunk('last')
emit('from_server', {'cmd': 'texteffect', 'data': len(vars.actions)}, broadcast=True) emit('from_server', {'cmd': 'texteffect', 'data': len(vars.actions)}, broadcast=True)
set_aibusy(0) set_aibusy(0)
@ -1708,7 +1741,7 @@ def oairequest(txt, min, max):
genout = req.json()["choices"][0]["text"] genout = req.json()["choices"][0]["text"]
print("{0}{1}{2}".format(colors.CYAN, genout, colors.END)) print("{0}{1}{2}".format(colors.CYAN, genout, colors.END))
vars.actions.append(genout) vars.actions.append(genout)
refresh_story() update_story_chunk('last')
emit('from_server', {'cmd': 'texteffect', 'data': len(vars.actions)}, broadcast=True) emit('from_server', {'cmd': 'texteffect', 'data': len(vars.actions)}, broadcast=True)
set_aibusy(0) set_aibusy(0)

View File

@ -863,6 +863,39 @@ $(document).ready(function(){
setTimeout(function () { setTimeout(function () {
$('#gamescreen').animate({scrollTop: $('#gamescreen').prop('scrollHeight')}, 1000); $('#gamescreen').animate({scrollTop: $('#gamescreen').prop('scrollHeight')}, 1000);
}, 5); }, 5);
} else if(msg.cmd == "updatechunk") {
const {index, html, last} = msg.data;
const existingChunk = game_text.children(`#n${index}`)
const newChunk = $(html);
if (existingChunk.length > 0) {
// Update existing chunk
existingChunk.before(newChunk);
existingChunk.remove();
} else {
// Append at the end
game_text.append(newChunk);
}
if(last) {
// Scroll to bottom of text if it's the last element
setTimeout(function () {
$('#gamescreen').animate({scrollTop: $('#gamescreen').prop('scrollHeight')}, 1000);
}, 5);
}
} else if(msg.cmd == "removechunk") {
let index = msg.data;
// Remove the chunk
game_text.children(`#n${index}`).remove()
// Shift all existing chunks by 1
index++;
while (true) {
const chunk = game_text.children(`#n${index}`)
if(chunk.length === 0) {
break;
}
const newIndex = index - 1;
chunk.attr('n', newIndex.toString()).attr('id', `n${newIndex}`);
index++;
}
} else if(msg.cmd == "setgamestate") { } else if(msg.cmd == "setgamestate") {
// Enable or Disable buttons // Enable or Disable buttons
if(msg.data == "ready") { if(msg.data == "ready") {