mirror of
https://github.com/LmeSzinc/StarRailCopilot.git
synced 2024-11-25 10:01:10 +00:00
Refactor: Config update callback and add dungeon settings sync
This commit is contained in:
parent
ab8e54628e
commit
4a3af51c80
@ -1,3 +1,4 @@
|
|||||||
|
import typing as t
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
|
|
||||||
from cached_property import cached_property
|
from cached_property import cached_property
|
||||||
@ -715,6 +716,45 @@ class ConfigUpdater:
|
|||||||
set_daily('Use_Consumables_1_time', 'achievable')
|
set_daily('Use_Consumables_1_time', 'achievable')
|
||||||
return data
|
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):
|
def read_file(self, config_name, is_template=False):
|
||||||
"""
|
"""
|
||||||
Read and update config file.
|
Read and update config file.
|
||||||
|
@ -456,8 +456,7 @@ class AlasGUI(Frame):
|
|||||||
try:
|
try:
|
||||||
d = self.modified_config_queue.get(timeout=10)
|
d = self.modified_config_queue.get(timeout=10)
|
||||||
config_name = self.alas_name
|
config_name = self.alas_name
|
||||||
read = self.alas_config.read_file
|
config_updater = self.alas_config
|
||||||
write = self.alas_config.write_file
|
|
||||||
except queue.Empty:
|
except queue.Empty:
|
||||||
continue
|
continue
|
||||||
modified[d["name"]] = d["value"]
|
modified[d["name"]] = d["value"]
|
||||||
@ -466,7 +465,7 @@ class AlasGUI(Frame):
|
|||||||
d = self.modified_config_queue.get(timeout=1)
|
d = self.modified_config_queue.get(timeout=1)
|
||||||
modified[d["name"]] = d["value"]
|
modified[d["name"]] = d["value"]
|
||||||
except queue.Empty:
|
except queue.Empty:
|
||||||
self._save_config(modified, config_name, read, write)
|
self._save_config(modified, config_name, config_updater)
|
||||||
modified.clear()
|
modified.clear()
|
||||||
break
|
break
|
||||||
|
|
||||||
@ -474,13 +473,12 @@ class AlasGUI(Frame):
|
|||||||
self,
|
self,
|
||||||
modified: Dict[str, str],
|
modified: Dict[str, str],
|
||||||
config_name: str,
|
config_name: str,
|
||||||
read=State.config_updater.read_file,
|
config_updater: AzurLaneConfig = State.config_updater,
|
||||||
write=State.config_updater.write_file,
|
|
||||||
) -> None:
|
) -> None:
|
||||||
try:
|
try:
|
||||||
valid = []
|
valid = []
|
||||||
invalid = []
|
invalid = []
|
||||||
config = read(config_name)
|
config = config_updater.read_file(config_name)
|
||||||
for k, v in modified.copy().items():
|
for k, v in modified.copy().items():
|
||||||
valuetype = deep_get(self.ALAS_ARGS, k + ".valuetype")
|
valuetype = deep_get(self.ALAS_ARGS, k + ".valuetype")
|
||||||
v = parse_pin_value(v, valuetype)
|
v = parse_pin_value(v, valuetype)
|
||||||
@ -497,16 +495,12 @@ class AlasGUI(Frame):
|
|||||||
modified[k] = v
|
modified[k] = v
|
||||||
valid.append(k)
|
valid.append(k)
|
||||||
|
|
||||||
# update Emotion Record if Emotion Value is changed
|
for set_key, set_value in config_updater.save_callback(k, v):
|
||||||
if "Emotion" in k and "Value" in k:
|
logger.info([set_key, set_value, pin["_".join(set_key.split("."))]])
|
||||||
k = k.split(".")
|
modified[set_key] = set_value
|
||||||
k[-1] = k[-1].replace("Value", "Record")
|
deep_set(config, set_key, set_value)
|
||||||
k = ".".join(k)
|
valid.append(set_key)
|
||||||
v = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
pin["_".join(set_key.split("."))] = set_value
|
||||||
modified[k] = v
|
|
||||||
deep_set(config, k, v)
|
|
||||||
valid.append(k)
|
|
||||||
pin["_".join(k.split("."))] = v
|
|
||||||
else:
|
else:
|
||||||
modified.pop(k)
|
modified.pop(k)
|
||||||
invalid.append(k)
|
invalid.append(k)
|
||||||
@ -523,7 +517,7 @@ class AlasGUI(Frame):
|
|||||||
logger.info(
|
logger.info(
|
||||||
f"Save config {filepath_config(config_name)}, {dict_to_kv(modified)}"
|
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:
|
except Exception as e:
|
||||||
logger.exception(e)
|
logger.exception(e)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user