config.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. from typing import Any, Dict
  5. from pydantic import (Field,
  6. model_validator,
  7. ValidationError,
  8. model_serializer,
  9. WithJsonSchema,
  10. SerializerFunctionWrapHandler,
  11. GetJsonSchemaHandler)
  12. from tasks.Component.config_scheduler import Scheduler
  13. from tasks.Component.config_base import ConfigBase, Time
  14. from tasks.Component.GeneralBattle.config_general_battle import GreenMarkType
  15. class DuelConfig(ConfigBase):
  16. # 一键切换斗技御魂
  17. switch_all_soul: bool = Field(default=False, description='switch_all_soul_help')
  18. # 限制时间
  19. limit_time: Time = Field(default=Time(minute=30), description='limit_time_help')
  20. # 目标分数
  21. target_score: int = Field(default=2000, description='target_score_help')
  22. # 刷满荣誉就退出
  23. honor_full_exit: bool = Field(default=False, description='honor_full_exit_help')
  24. # 是否开启绿标
  25. green_enable: bool = Field(default=False, description='green_enable_help')
  26. # 选哪一个绿标
  27. green_mark: GreenMarkType = Field(default=GreenMarkType.GREEN_LEFT1, description='green_mark_help')
  28. class Duel(ConfigBase):
  29. scheduler: Scheduler = Field(default_factory=Scheduler)
  30. duel_config: DuelConfig = Field(default_factory=DuelConfig)
  31. test_list: list[DuelConfig]
  32. @model_validator(mode='before')
  33. @classmethod
  34. def check_list(cls, data: dict) -> Any:
  35. if 'test_list' not in data:
  36. data['test_list'] = []
  37. for key, value in data.items():
  38. if isinstance(value, list):
  39. continue
  40. if 'test_list' not in key:
  41. continue
  42. try:
  43. DuelConfig(**value)
  44. data['test_list'].append(value)
  45. except ValidationError as e:
  46. pass
  47. # 补全list
  48. while len(data['test_list']) < data['scheduler']['priority']:
  49. data['test_list'].append(DuelConfig())
  50. return data
  51. @model_serializer()
  52. def serializer_model(self, value: Any) -> Dict[str, Any]:
  53. properties = self.__dict__
  54. data = {}
  55. for key,value in properties.items():
  56. if isinstance(value, list):
  57. for index, v in enumerate(value):
  58. data[f'{key}_{index+1}'] = v.model_dump()
  59. else:
  60. data[key] = value.model_dump()
  61. return data
  62. if __name__ == "__main__":
  63. test_dict = {
  64. 'scheduler': {'enable': False,
  65. 'next_run': '2023-01-01T00:00:00',
  66. 'priority': 5,
  67. 'success_interval': '01 00:00:00',
  68. 'failure_interval': '01 00:00:00',
  69. 'server_update': '09:00:00',
  70. 'float_time': '00:00:00'
  71. },
  72. 'duel_config': {'switch_all_soul': False,
  73. 'limit_time': '00:30:00',
  74. 'target_score': 2000,
  75. 'honor_full_exit': False,
  76. 'green_enable': False,
  77. 'green_mark': 'green_left1'},
  78. # 'test_list': [
  79. # {'switch_all_soul': True,
  80. # 'limit_time': '00:30:00',
  81. # 'target_score': 2000,
  82. # 'honor_full_exit': False,
  83. # 'green_enable': False,
  84. # 'green_mark': 'green_left1'},
  85. # ],
  86. # 'duel_config1': {'switch_all_soul': False,
  87. # 'limit_time': '00:30:00',
  88. # 'target_score': 2000,
  89. # 'honor_full_exit': False,
  90. # 'green_enable': False,
  91. # 'green_mark': 'green_left1'},
  92. # 'duel_config2': {'switch_all_soul': False,
  93. # 'limit_time': '00:30:00',
  94. # 'target_score': 2000,
  95. # 'honor_full_exit': False,
  96. # 'green_enable': False,
  97. # 'green_mark': 'green_left1'},
  98. }
  99. import json
  100. c = Duel(**test_dict)
  101. # print(c.model_dump_json(indent=2))
  102. print(json.dumps(c.model_json_schema(), indent=2))