Refactor: Config update callback and add dungeon settings sync

This commit is contained in:
LmeSzinc 2023-11-03 01:33:46 +08:00
parent ab8e54628e
commit 4a3af51c80
2 changed files with 51 additions and 17 deletions

View File

@ -1,3 +1,4 @@
import typing as t
from copy import deepcopy
from cached_property import cached_property
@ -715,6 +716,45 @@ class ConfigUpdater:
set_daily('Use_Consumables_1_time', 'achievable')
return data
def save_callback(self, key: str, value: t.Any) -> t.Iterable[t.Tuple[str, t.Any]]:
"""
Args:
key: Key path in config json, such as "Main.Emotion.Fleet1Value"
value: Value set by user, such as "98"
Yields:
str: Key path to set config json, such as "Main.Emotion.Fleet1Record"
any: Value to set, such as "2020-01-01 00:00:00"
"""
if key.startswith('Dungeon.Dungeon') or key.startswith('Dungeon.DungeonDaily'):
from tasks.dungeon.keywords.dungeon import DungeonList
from module.exception import ScriptError
try:
dungeon = DungeonList.find(value)
except ScriptError:
return
if key.endswith('Name'):
if dungeon.is_Calyx_Golden:
yield 'Dungeon.Dungeon.NameAtDoubleCalyx', value
yield 'Dungeon.DungeonDaily.CalyxGolden', value
elif dungeon.is_Calyx_Crimson:
yield 'Dungeon.Dungeon.NameAtDoubleCalyx', value
yield 'Dungeon.DungeonDaily.CalyxCrimson', value
elif dungeon.is_Stagnant_Shadow:
yield 'Dungeon.DungeonDaily.StagnantShadow', value
elif dungeon.is_Cavern_of_Corrosion:
yield 'Dungeon.Dungeon.NameAtDoubleRelic', value
yield 'Dungeon.DungeonDaily.CavernOfCorrosion', value
elif key.endswith('NameAtDoubleCalyx'):
if dungeon.is_Calyx_Golden:
yield 'Dungeon.DungeonDaily.CalyxGolden', value
elif dungeon.is_Calyx_Crimson:
yield 'Dungeon.DungeonDaily.CalyxCrimson', value
elif key.endswith('NameAtDoubleRelic'):
yield 'Dungeon.DungeonDaily.CavernOfCorrosion', value
elif key.endswith('CavernOfCorrosion'):
yield 'Dungeon.Dungeon.NameAtDoubleRelic', value
def read_file(self, config_name, is_template=False):
"""
Read and update config file.

View File

@ -456,8 +456,7 @@ class AlasGUI(Frame):
try:
d = self.modified_config_queue.get(timeout=10)
config_name = self.alas_name
read = self.alas_config.read_file
write = self.alas_config.write_file
config_updater = self.alas_config
except queue.Empty:
continue
modified[d["name"]] = d["value"]
@ -466,7 +465,7 @@ class AlasGUI(Frame):
d = self.modified_config_queue.get(timeout=1)
modified[d["name"]] = d["value"]
except queue.Empty:
self._save_config(modified, config_name, read, write)
self._save_config(modified, config_name, config_updater)
modified.clear()
break
@ -474,13 +473,12 @@ class AlasGUI(Frame):
self,
modified: Dict[str, str],
config_name: str,
read=State.config_updater.read_file,
write=State.config_updater.write_file,
config_updater: AzurLaneConfig = State.config_updater,
) -> None:
try:
valid = []
invalid = []
config = read(config_name)
config = config_updater.read_file(config_name)
for k, v in modified.copy().items():
valuetype = deep_get(self.ALAS_ARGS, k + ".valuetype")
v = parse_pin_value(v, valuetype)
@ -497,16 +495,12 @@ class AlasGUI(Frame):
modified[k] = v
valid.append(k)
# update Emotion Record if Emotion Value is changed
if "Emotion" in k and "Value" in k:
k = k.split(".")
k[-1] = k[-1].replace("Value", "Record")
k = ".".join(k)
v = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
modified[k] = v
deep_set(config, k, v)
valid.append(k)
pin["_".join(k.split("."))] = v
for set_key, set_value in config_updater.save_callback(k, v):
logger.info([set_key, set_value, pin["_".join(set_key.split("."))]])
modified[set_key] = set_value
deep_set(config, set_key, set_value)
valid.append(set_key)
pin["_".join(set_key.split("."))] = set_value
else:
modified.pop(k)
invalid.append(k)
@ -523,7 +517,7 @@ class AlasGUI(Frame):
logger.info(
f"Save config {filepath_config(config_name)}, {dict_to_kv(modified)}"
)
write(config_name, config)
config_updater.write_file(config_name, config)
except Exception as e:
logger.exception(e)