config.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. from pydantic import BaseModel, Field, model_validator
  5. from tasks.Component.GeneralBattle.config_general_battle import GreenMarkType
  6. from tasks.Component.config_scheduler import Scheduler
  7. from tasks.Component.config_base import ConfigBase, TimeDelta
  8. from tasks.Component.BaseActivity.config_activity import GeneralClimb
  9. def check_soul_by_number(enable_switch: bool, group_team: str, label: str):
  10. if not enable_switch:
  11. return
  12. if not group_team or group_team == "-1,-1":
  13. raise ValueError(f"[{label}]Switch Soul configuration is enabled, but there is no setting")
  14. if ',' not in group_team:
  15. raise ValueError(f"[{label}]The switch soul configuration must be in English ','")
  16. parts = group_team.split(',')
  17. if len(parts) != 2:
  18. raise ValueError(f"[{label}]The length of the switch soul configuration must be equal to 2")
  19. if not all(p.strip().isdigit() for p in parts):
  20. raise ValueError(f"[{label}]Switching soul configurations must be numeric")
  21. def check_soul_by_ocr(enable_switch: bool, group_team: str, label: str):
  22. if not enable_switch:
  23. return
  24. if not group_team:
  25. raise ValueError(f"[{label}]Switch Soul configuration is enabled, but there is no setting")
  26. if ',' not in group_team:
  27. raise ValueError(f"[{label}]The switch soul configuration must be in English ','")
  28. parts = group_team.split(',')
  29. if len(parts) != 2:
  30. raise ValueError(f"[{label}]The length of the switch soul configuration must be equal to 2")
  31. class SwitchSoulConfig(BaseModel):
  32. enable_switch_pass: bool = Field(default=False, description='enable_switch_pass_help')
  33. pass_group_team: str = Field(default='-1,-1', description='pass_group_team_help')
  34. enable_switch_pass_by_name: bool = Field(default=False, description='enable_switch_pass_by_name_help')
  35. pass_group_team_name: str = Field(default='', description='pass_group_team_name_help')
  36. enable_switch_ap: bool = Field(default=False, description='enable_switch_ap_help')
  37. ap_group_team: str = Field(default='-1,-1', description='ap_group_team_help')
  38. enable_switch_ap_by_name: bool = Field(default=False, description='enable_switch_ap_by_name_help')
  39. ap_group_team_name: str = Field(default='', description='ap_group_team_name_help')
  40. enable_switch_boss: bool = Field(default=False, description='enable_switch_boss_help')
  41. boss_group_team: str = Field(default='-1,-1', description='boss_group_team_help')
  42. enable_switch_boss_by_name: bool = Field(default=False, description='enable_switch_boss_by_name_help')
  43. boss_group_team_name: str = Field(default='', description='boss_group_team_name_help')
  44. enable_switch_ap100: bool = Field(default=False, description='enable_switch_ap100_help')
  45. ap100_group_team: str = Field(default='-1,-1', description='ap100_group_team_help')
  46. enable_switch_ap100_by_name: bool = Field(default=False, description='enable_switch_ap100_by_name_help')
  47. ap100_group_team_name: str = Field(default='', description='ap100_group_team_name_help')
  48. # @model_validator(mode='after')
  49. def validate_switch_soul(self):
  50. label_set = self.get_label_set()
  51. for label in label_set:
  52. enable_num = getattr(self, f"enable_switch_{label}", False)
  53. team = getattr(self, f"{label}_group_team", None)
  54. check_soul_by_number(enable_num, team, label=label.upper())
  55. enable_ocr = getattr(self, f"enable_switch_{label}_by_name", False)
  56. team_name = getattr(self, f"{label}_group_team_name", None)
  57. check_soul_by_ocr(enable_ocr, team_name, label=label.upper())
  58. return self
  59. def get_label_set(self):
  60. return {field.replace("enable_switch_", "") for field in self.model_fields if
  61. field.startswith("enable_switch_") and not field.endswith("by_name")}
  62. class GeneralBattleConfig(BaseModel):
  63. enable_pass_preset: bool = Field(default=False, description='enable_pass_preset_help')
  64. enable_pass_green: bool = Field(default=False, description='enable_pass_green_help')
  65. pass_green_mark: GreenMarkType = Field(default=GreenMarkType.GREEN_LEFT1, description='pass_green_mark_help')
  66. enable_pass_anti_detect: bool = Field(default=True, description='enable_pass_anti_detect_help')
  67. enable_ap_preset: bool = Field(default=False, description='enable_ap_preset_help')
  68. enable_ap_green: bool = Field(default=False, description='enable_ap_green_help')
  69. ap_green_mark: GreenMarkType = Field(default=GreenMarkType.GREEN_LEFT1, description='ap_green_mark_help')
  70. enable_ap_anti_detect: bool = Field(default=True, description='enable_ap_anti_detect_help')
  71. enable_boss_preset: bool = Field(default=False, description='enable_boss_preset_help')
  72. enable_boss_green: bool = Field(default=False, description='enable_boss_green_help')
  73. boss_green_mark: GreenMarkType = Field(default=GreenMarkType.GREEN_LEFT1, description='boss_green_mark_help')
  74. enable_boss_anti_detect: bool = Field(default=True, description='enable_boss_anti_detect_help')
  75. enable_ap100_preset: bool = Field(default=False, description='enable_ap100_preset_help')
  76. enable_ap100_green: bool = Field(default=False, description='enable_ap100_green_help')
  77. ap100_green_mark: GreenMarkType = Field(default=GreenMarkType.GREEN_LEFT1, description='ap100_green_mark_help')
  78. enable_ap100_anti_detect: bool = Field(default=True, description='enable_ap100_anti_detect_help')
  79. class ActivityShikigami(ConfigBase):
  80. scheduler: Scheduler = Field(default_factory=Scheduler)
  81. general_climb: GeneralClimb = Field(default_factory=GeneralClimb)
  82. switch_soul_config: SwitchSoulConfig = Field(default_factory=SwitchSoulConfig)
  83. general_battle: GeneralBattleConfig = Field(default_factory=GeneralBattleConfig)
  84. # @model_validator(mode='after')
  85. def validate_switch_preset(self):
  86. label_set = self.switch_soul_config.get_label_set()
  87. for label in label_set:
  88. enable_preset = getattr(self.general_battle, f"enable_{label}_preset", False)
  89. group_team = getattr(self.switch_soul_config, f"{label}_group_team", None)
  90. try:
  91. check_soul_by_number(enable_preset, group_team, label=label.upper())
  92. except ValueError:
  93. raise ValueError(f'The switch preset is enabled, but the switch soul is configured incorrectly')
  94. return self