| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- # This Python file uses the following encoding: utf-8
- # @author runhey
- # github https://github.com/runhey
- import re
- from pathlib import Path
- from pydantic import BaseModel, ValidationError, Field
- from module.config.utils import *
- from module.logger import logger
- # 导入配置的Python文件
- from tasks.Component.config_base import ConfigBase
- from tasks.Script.config import Script
- from tasks.Restart.config import Restart
- from tasks.GlobalGame.config import GlobalGame
- from tasks.AreaBoss.config import AreaBoss
- from tasks.ExperienceYoukai.config import ExperienceYoukai
- from tasks.GoldYoukai.config import GoldYoukai
- from tasks.Nian.config import Nian
- from tasks.Orochi.config import Orochi
- from tasks.OrochiMoans.config import OrochiMoans
- from tasks.Sougenbi.config import Sougenbi
- from tasks.FallenSun.config import FallenSun
- from tasks.EternitySea.config import EternitySea
- from tasks.RealmRaid.config import RealmRaid
- # 这一部分是活动的配置-----------------------------------------------------------------------------------------------------
- from tasks.ActivityShikigami.config import ActivityShikigami
- # ----------------------------------------------------------------------------------------------------------------------
- class ConfigModel(ConfigBase):
- config_name: str = "oas"
- script: Script = Field(default_factory=Script)
- restart: Restart = Field(default_factory=Restart)
- global_game: GlobalGame = Field(default_factory=GlobalGame)
- # 这些是每日任务的
- area_boss: AreaBoss = Field(default_factory=AreaBoss)
- experience_youkai: ExperienceYoukai = Field(default_factory=ExperienceYoukai)
- gold_youkai: GoldYoukai = Field(default_factory=GoldYoukai)
- nian: Nian = Field(default_factory=Nian)
- realm_raid: RealmRaid = Field(default_factory=RealmRaid)
- # 这些是刷御魂的
- orochi: Orochi = Field(default_factory=Orochi)
- orochi_moans: OrochiMoans = Field(default_factory=OrochiMoans)
- sougenbi: Sougenbi = Field(default_factory=Sougenbi)
- fallen_sun: FallenSun = Field(default_factory=FallenSun)
- eternity_sea: EternitySea = Field(default_factory=EternitySea)
- # 这些是活动的
- activity_shikigami: ActivityShikigami = Field(default_factory=ActivityShikigami)
- # @validator('script')
- # def script_validator(cls, v):
- # if v is None:
- # return Script()
- def __init__(self, config_name: str=None) -> None:
- """
- :param config_name:
- """
- if not config_name:
- super().__init__()
- return
- data = self.read_json(config_name)
- data["config_name"] = config_name
- super().__init__(**data)
- def __setattr__(self, key, value):
- """
- 只要修改属性就会触发这个函数 自动保存
- :param key:
- :param value:
- :return:
- """
- super().__setattr__(key, value)
- logger.info("auto save config")
- self.save()
- @staticmethod
- def read_json(config_name: str) -> dict:
- """
- 读文件 没有额外操作
- :param config_name: 不带后缀
- :return:
- """
- filepath = Path.cwd() / "config" / f"{config_name}.json"
- return read_file(filepath)
- @staticmethod
- def write_json(config_name: str, data) -> None:
- """
- :param config_name: 不带后缀
- :param data: 字典而不是字符串
- :return:
- """
- filepath = Path.cwd() / "config" / f"{config_name}.json"
- write_file(filepath, data)
- def gui_args(self, task: str) -> str:
- """
- 返回提供给gui显示的参数
- :param task: 输入的是任务的名称英文 如'Script' 或者是'script'都是可以的
- :return: 返回的是pydantic给我们结构化的输出的信息, 如果不能获取就返回空的str
- """
- task = convert_to_underscore(task)
- task_gui = getattr(self, task, None)
- if task_gui is None:
- logger.warning(f'{task} is no inexistence')
- return ''
- schema2 = task_gui.schema()
- # https://github.com/pydantic/pydantic/discussions/5687
- if 'definitions' in schema2:
- if 'Scheduler' in schema2['definitions']:
- if 'properties' in schema2['definitions']['Scheduler']:
- properties = schema2['definitions']['Scheduler']['properties']
- if 'success_interval' in properties:
- properties['success_interval']['type'] = 'string'
- if 'failure_interval' in properties:
- properties['failure_interval']['type'] = 'string'
- return json.dumps(schema2)
- def gui_task(self, task: str) -> str:
- """
- 返回提供给gui显示的参数
- :param task:
- :return:
- """
- task_name = convert_to_underscore(task)
- task = getattr(self, task_name, None)
- if task is None:
- logger.warning(f'{task_name} is no inexistence')
- return ''
- return task.json()
- def save(self) -> None:
- """
- :return:
- """
- self.write_json(self.config_name, self.dict())
- @staticmethod
- def type(key: str) -> str:
- """
- 输入模型的键值,获取这个字段对象的类型 比如输入是orochi输出是Orochi
- :param key:
- :return:
- """
- field_type: str = str(ConfigModel.__annotations__[key])
- # return field_type
- if '.' in field_type:
- classname = field_type.split('.')[-1][:-2]
- return classname
- else:
- classname = re.findall(r"'([^']*)'", field_type)[0]
- return classname
- # @root_validator
- # def on_on_property_change(cls, values):
- # """
- # 当属性改变时保存
- # :param values:
- # :return:
- # """
- # logger.info(f'property change auto save')
- # cls.save()
- if __name__ == "__main__":
- try:
- c = ConfigModel("oas1")
- except ValidationError as e:
- print(e)
- c = ConfigModel()
- c.gui_args('Nian')
- print(c.gui_task('Nian'))
- print(c.gui_task('AreaBoss'))
- print(c.dict()['nian']['scheduler']['success_interval'])
- c.save()
|