sqlalchemy ORM models for quest databases and json parsing scripts

also minor bugfixes
next is testing and implementation
This commit is contained in:
louis 2019-05-15 12:32:33 -04:00
parent 6c455fe30b
commit 8b7815500e
10 changed files with 267 additions and 15 deletions

1
diva/__init__.py Normal file
View file

@ -0,0 +1 @@
from . import quest

126
diva/quest.py Normal file
View file

@ -0,0 +1,126 @@
import db
import io
import json
import os
from sqlalchemy.orm import contains_eager
def update_missions(dir_in, old_path, languages):
db.init(dir_in)
quest_mst(old_path, languages)
scene_mst(old_path, languages)
def scene_mst(old_path, languages):
result = (db.session.query(db.QuestScene)
.join(db.QuestScene.parts)
.options(contains_eager(db.QuestScene.parts))
.filter(db.QuestPart.beforeTalkName != "" or db.QuestPart.afterTalkName != "")
.filter(db.QuestScene.parts.any())
.all())
scenes = {}
for x in result:
scenes[str(x.questSceneMstId)] = { "Name": x.name,
"SummaryText": x.summaryText,
"Parts": [],
"Enabled": False,
"Folder": "" }
id_num = ""
# this is slow as balls but i'm way too tired to figure out how to optimize it
for y in x.parts:
if y.beforeTalkName:
id_num = y.beforeTalkName
scenes[str(x.questSceneMstId)]["Parts"].append(y.beforeTalkName)
if y.afterTalkName:
id_num = y.afterTalkName
scenes[str(x.questSceneMstId)]["Parts"].append(y.afterTalkName)
# i checked every entry, moving it to do once for speed
if not scenes[str(x.questSceneMstId)]["Folder"] and id_num:
path = (db.session.query(db.ResourceEntry)
.filter(db.ResourceEntry.path.like("%/Scenario/{}.tsv.utage".format(id_num)))
.filter(db.ResourceEntry.path.notlike("%Utage/side03/%.tsv.utage")) # dead folder with dupes too lazy to parse settings tsv
.one())
folder = path.path.split("/")[2]
if scenes[str(x.questSceneMstId)]["Folder"] and scenes[str(x.questSceneMstId)]["Folder"] != folder:
raise KeyError("Internal path inconsitency, bailing out {}".format(y.afterTalkName))
scenes[str(x.questSceneMstId)]["Folder"] = folder
if os.path.isfile(os.path.join(old_path, "XduScene.json")):
with open(os.path.join(old_path, "XduScene.json"), "r") as old_json_file:
old_json = json.load(old_json_file)
for key in old_json:
if old_json[key]['Enabled'] == True and key in scenes:
scenes[key]['Enabled'] = True
with io.open(os.path.join(old_path, "XduScene.json"), "w", newline='\n') as json_file:
json.dump(scenes, json_file, ensure_ascii=False, indent='\t', sort_keys=True)
for l in languages:
out_dict = {}
if l == "jpn":
for x in result:
out_dict[str(x.questSceneMstId)] = { "Name": x.name,
"SummaryText": x.summaryText }
else:
for x in result:
out_dict[str(x.questSceneMstId)] = { "Name": "",
"SummaryText": "" }
langfile = os.path.join(old_path, "XduSceneNames_{}.json".format(l))
if os.path.isfile(langfile):
with open(langfile, "r") as lang_file:
lang_dict = json.load(lang_file)
out_dict.update(lang_dict)
with io.open(langfile, "w", newline='\n') as lang_file: # you're using git right
json.dump(out_dict, lang_file, ensure_ascii=False, indent='\t', sort_keys=True)
def quest_mst(old_path, languages):
# i regret my life choices
result = (db.session.query(db.Quest)
.filter(db.Quest.baseQuestMstId == 0)
.join(db.Quest.scenes)
.join(db.QuestScene.parts)
.options(contains_eager(db.Quest.scenes).
contains_eager(db.QuestScene.parts))
.filter(db.QuestPart.beforeTalkName != "" or db.QuestPart.afterTalkName != "")
.filter(db.QuestScene.parts.any())
.filter(db.Quest.scenes.any())
.all())
quests = {}
for x in result:
quests[str(x.questMstId)] = { "Name": x.name, "Scenes": [d.questSceneMstId for d in x.scenes], "Enabled": False }
if os.path.isfile(os.path.join(old_path, "XduQuest.json")):
with open(os.path.join(old_path, "XduQuest.json"), "r") as old_json_file:
old_json = json.load(old_json_file)
for key in old_json:
if old_json[key]['Enabled'] == True and key in quests:
quests[key]['Enabled'] = True
with io.open(os.path.join(old_path, "XduQuest.json"), "w", newline='\n') as json_file:
json.dump(quests, json_file, ensure_ascii=False, indent='\t', sort_keys=True)
for l in languages:
out_dict = {}
if l == "jpn":
for x in result:
out_dict[str(x.questMstId)] = { "Name": x.name }
else:
for x in result:
out_dict[str(x.questMstId)] = { "Name": "" }
langfile = os.path.join(old_path, "XduQuestNames_{}.json".format(l))
if os.path.isfile(langfile):
with open(langfile, "r") as lang_file:
lang_dict = json.load(lang_file)
out_dict.update(lang_dict)
with io.open(langfile, "w", newline='\n') as lang_file: # you're using git right
json.dump(out_dict, lang_file, ensure_ascii=False, indent='\t', sort_keys=True)
return quests