script_task.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. from cached_property import cached_property
  5. from tasks.GameUi.game_ui import GameUi
  6. from tasks.GameUi.page import page_realm_raid, page_main
  7. from tasks.Quiz.assets import QuizAssets
  8. from tasks.ActivityShikigami.assets import ActivityShikigamiAssets
  9. from tasks.DemonEncounter.data.answer import Answer
  10. from tasks.Quiz.debug import Debugger, remove_symbols
  11. from module.logger import logger
  12. from module.exception import TaskEnd
  13. from module.atom.image_grid import ImageGrid
  14. from module.atom.image import RuleImage
  15. from module.atom.click import RuleClick
  16. class ScriptTask(GameUi, QuizAssets, ActivityShikigamiAssets, Debugger):
  17. last_select_1 = ''
  18. last_select_2 = ''
  19. last_select_3 = ''
  20. last_select_4 = ''
  21. @cached_property
  22. def anwser(self) -> Answer:
  23. # Misspelling
  24. return Answer()
  25. @cached_property
  26. def click_options(self) -> list:
  27. return [self.O_ANSWER1, self.O_ANSWER2, self.O_ANSWER3, self.O_ANSWER4]
  28. def run(self):
  29. self.ui_get_current_page()
  30. self.ui_goto(page_main)
  31. _config = self.config.model.quiz.quiz_config
  32. self.enter()
  33. quiz_cnt = 0
  34. while 1:
  35. if quiz_cnt >= _config.quiz_cnt:
  36. break
  37. self.once()
  38. quiz_cnt += 1
  39. self.ui_click(self.I_UI_BACK_YELLOW, self.I_CHECK_MAIN, interval=2)
  40. self.set_next_run(task='Quiz', success=True, finish=True)
  41. raise TaskEnd('Quiz')
  42. def enter(self):
  43. while 1:
  44. self.screenshot()
  45. if self.appear(self.I_START):
  46. break
  47. if self.appear_then_click(self.I_SHI, interval=1):
  48. continue
  49. if self.appear_then_click(self.I_ENTRY, interval=1):
  50. continue
  51. logger.info('Quiz start')
  52. def once(self) -> bool:
  53. logger.hr('Quiz', 3)
  54. self.ui_click(self.I_START, self.I_MESSAGE)
  55. quests_cnt = 0
  56. selected_flag = False
  57. self.last_select_1, self.last_select_2, self.last_select_3, self.last_select_4 = '', '', '', ''
  58. while 1:
  59. self.screenshot()
  60. if self.appear(self.I_FAIL_QUIT):
  61. # 失败
  62. logger.info('Quiz Fail and exit')
  63. self.ui_click(self.I_FAIL_QUIT, self.I_START)
  64. break
  65. if self.appear(self.I_SHARE):
  66. # 结算
  67. logger.info('Quiz Victory and exit')
  68. self.ui_click(self.I_UI_BACK_RED, self.I_START)
  69. break
  70. self._deal_quiz()
  71. self.close_fn()
  72. def detect_new(self, select_1, select_2, select_3, select_4) -> bool:
  73. if self.last_select_1 != select_1 or \
  74. self.last_select_2 != select_2 or \
  75. self.last_select_3 != select_3 or \
  76. self.last_select_4 != select_4:
  77. return True
  78. return False
  79. def _deal_quiz(self):
  80. answer_1 = self.O_ANSWER1.detect_text(self.device.image)
  81. answer_2 = self.O_ANSWER2.detect_text(self.device.image)
  82. answer_3 = self.O_ANSWER3.detect_text(self.device.image)
  83. answer_4 = self.O_ANSWER4.detect_text(self.device.image)
  84. if answer_1 == '' and answer_2 == '' and answer_3 == '' and answer_4 == '':
  85. return False
  86. if not self.detect_new(answer_1, answer_2, answer_3, answer_4) and\
  87. not self.ocr_appear(self.O_COUNTDOWN) and\
  88. not self.ocr_appear(self.O_COUNTDOWN3):
  89. # 第一次换题目的时候 和 最后两秒钟的时候 进行选择
  90. return False
  91. self.last_select_1, self.last_select_2, self.last_select_3, self.last_select_4 = \
  92. answer_1, answer_2, answer_3, answer_4
  93. questions = self.O_QUESTION.detect_and_ocr(self.device.image)
  94. question = ''.join([q.ocr_text for q in questions])
  95. question = question.replace('?', '').replace('?', '').replace(' ', '').replace(',', ',')
  96. question = remove_symbols(question)
  97. index = self.anwser.answer_one(question=question, options=[answer_1, answer_2, answer_3, answer_4])
  98. if index is None:
  99. logger.error('Now question has no answer, please check')
  100. self.append_one(question=question, options=[answer_1, answer_2, answer_3, answer_4])
  101. index = 1
  102. logger.info(f'Question: {question}, Answer: {index}')
  103. self.click(self.click_options[index-1])
  104. return True
  105. if __name__ == '__main__':
  106. from module.config.config import Config
  107. from module.device.device import Device
  108. c = Config('oas1')
  109. d = Device(c)
  110. t = ScriptTask(c, d)
  111. t.screenshot()
  112. t.run()