Skip to content

Commit

Permalink
add game script matcher and save log text
Browse files Browse the repository at this point in the history
  • Loading branch information
mathewthe2 committed Apr 29, 2021
1 parent b5d19b2 commit 5c3428a
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 13 deletions.
2 changes: 2 additions & 0 deletions gamescript.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def open_game_script():
root = Tk()
root.withdraw()
file = askopenfile(initialdir=str(GAME_SCRIPT_PATH), filetypes = (("TXT files","*.txt"),("all files","*.*")), defaultextension=".txt")
if not file:
return
try:
new_file = str(Path(GAME_SCRIPT_PATH, Path(file.name).stem + '.txt'))
copyfile(file.name, new_file)
Expand Down
26 changes: 17 additions & 9 deletions logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import eel
import glob
import base64
import fileinput
import codecs
import threading
from pathlib import Path
from datetime import datetime
Expand Down Expand Up @@ -181,16 +181,24 @@ def get_latest_log():
def update_log_text(log_id, folder_name, text):
parsed_text = text.replace('\n', '')
if (len(parsed_text) < 1):
return False
return
filename = '{}/{}.txt'.format(TEXT_LOG_PATH, folder_name)
create_directory_if_not_exists(filename)
for line in fileinput.input(filename, inplace=True):
line_id = line[:15]
if (line_id == log_id):
line = '{}, {}'.format(log_id, parsed_text)
return True

return False

temp_filename = '{}/temp.txt'.format(TEXT_LOG_PATH)
with codecs.open(filename, 'r', encoding='utf-8') as fi, \
codecs.open(temp_filename, 'w', encoding='utf-8') as fo:

for line in fi:
line_id = line[:15]
if (line_id == log_id):
fo.write('{}, {}'.format(log_id, parsed_text))
else:
fo.write(line)

os.remove(filename) # remove original
os.rename(temp_filename, filename) # rename temp to original name
return

def insert_newest_log_with_image(base64_image_string, image_type):
log = get_latest_log()
Expand Down
2 changes: 1 addition & 1 deletion web/logs.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
<button hidden class="recordAudioButton mdl-button mdl-js-button mdl-button--icon mdl-button--colored" >
<i class="material-icons">mic</i>
</button>
<span contenteditable class="logText"></span>
<span contenteditable oninput="changeLogText(this)" class="logText"></span>
</span>
<span class="mdl-list__item-secondary-action">
<button hidden class="showMatchingGameScriptButton mdl-button mdl-js-button mdl-button--icon" >
Expand Down
41 changes: 38 additions & 3 deletions web/logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ function createAnkiFormCard() {
},
onHide(instance) {
instance.setProps({trigger: 'mouseenter'});
// Update log element if user changed the log data
const logId = instance.reference.getAttribute('log_id');
if (isLogDataUpdated(logId)) {
refreshLogElement(logId);
}
},
content(reference) {
const logId = reference.getAttribute('log_id');
Expand Down Expand Up @@ -267,7 +272,7 @@ function createCardSectionElement(iconName, field, value, contentEditable=false,
const content = `
<span class="card_${field}_container mdl-list__item-primary-content">
<i class="material-icons mdl-list__item-icon">${iconName}</i>
<span contentEditable=${contentEditable} class="card_${field}">${value}</span>
<span ${contentEditable && `oninput="changeLogText(this)"`} contentEditable=${contentEditable} class="card_${field}">${value}</span>
${footerIcon && `<i class="material-icons" style="padding-left:12px">${footerIcon}</i>`}
</span>`;
cardSection.innerHTML = content;
Expand Down Expand Up @@ -312,6 +317,34 @@ function formatMatchScriptMenu(logId, matchScriptContent) {
return matchScriptContent
}

// Handle user change of log text from log or inside card
function changeLogText(element) {
const newLogText = element.innerText;
let logId = '';
if (element.classList[0] === 'card_card_sentence') {
logId = element.parentNode.parentNode.parentNode.getAttribute('log_id');
currentLogs.find(log=>log.id===logId).text = newLogText;
}
if (element.classList[0] === 'logText') {
const logItemElement = element.parentNode.parentNode;
logId = logItemElement.id.split('logItem-')[1];
currentLogs.find(log=>log.id===logId).text = newLogText;
refreshCardContent(logId);
}
if (logId) {
updateLogFileById(logId);
// TODO: update matching card options after updating log text
}
}

function isLogDataUpdated(logId) {
const log = getLogById(logId);
const logElement = getLogElementById(logId);
const logTextElement = logElement.getElementsByClassName('logText')[0]
const isTextChanged = log.text.trim() !== logTextElement.innerText.trim();
return isTextChanged
}

function replaceLogText(logId, newText) {
updateLogDataById(logId, {
text: newText
Expand Down Expand Up @@ -490,8 +523,10 @@ async function updateCardWithDictionaryEntry(logId, word) {
}
refreshCardContent(logId);
}
function updateLogById(logId) {
// TODO: update logs

function updateLogFileById(logId) {
const log = getLogById(logId);
eel.update_log_text(logId, log.folder, log.text);
}

function fileBaseName(fileName) {
Expand Down

0 comments on commit 5c3428a

Please sign in to comment.