script_task.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import random
  5. from cached_property import cached_property
  6. from tasks.GameUi.game_ui import GameUi
  7. from tasks.GameUi.page import page_realm_raid, page_main
  8. from tasks.Quiz.assets import QuizAssets
  9. from tasks.ActivityShikigami.assets import ActivityShikigamiAssets
  10. from tasks.DemonEncounter.data.answer import Answer
  11. from tasks.Quiz.debug import Debugger, remove_symbols
  12. from module.logger import logger
  13. from module.exception import TaskEnd
  14. from module.base.timer import Timer
  15. from module.atom.image_grid import ImageGrid
  16. from module.atom.image import RuleImage
  17. from module.atom.click import RuleClick
  18. class NoTicket(Exception):
  19. pass
  20. class ScriptTask(GameUi, QuizAssets, ActivityShikigamiAssets, Debugger):
  21. answer_cnt = 0
  22. last_select_1 = ''
  23. last_select_2 = ''
  24. last_select_3 = ''
  25. last_select_4 = ''
  26. @cached_property
  27. def anwser(self) -> Answer:
  28. # Misspelling
  29. return Answer()
  30. @cached_property
  31. def click_options(self) -> list:
  32. return [self.O_ANSWER1, self.O_ANSWER2, self.O_ANSWER3, self.O_ANSWER4]
  33. @cached_property
  34. def _config(self):
  35. return self.config.model.quiz.quiz_config
  36. def run(self):
  37. self.ui_get_current_page()
  38. self.ui_goto(page_main)
  39. _config = self.config.model.quiz.quiz_config
  40. self.enter()
  41. quiz_cnt = 0
  42. while 1:
  43. if quiz_cnt >= _config.quiz_cnt:
  44. break
  45. try:
  46. self.once()
  47. quiz_cnt += 1
  48. except NoTicket:
  49. break
  50. self.ui_click(self.I_UI_BACK_YELLOW, self.I_CHECK_MAIN, interval=2)
  51. self.set_next_run(task='Quiz', success=True, finish=True)
  52. raise TaskEnd('Quiz')
  53. def enter(self):
  54. while 1:
  55. self.screenshot()
  56. if self.appear(self.I_START):
  57. break
  58. if self.appear_then_click(self.I_SHI, interval=1):
  59. continue
  60. if self.appear_then_click(self.I_ENTRY, interval=1):
  61. continue
  62. logger.info('Quiz start')
  63. def once(self) -> bool:
  64. logger.hr('Quiz', 3)
  65. start_cnt = 0
  66. self.answer_cnt = 0
  67. while 1:
  68. self.screenshot()
  69. if self.appear(self.I_MESSAGE):
  70. break
  71. if start_cnt >= 4:
  72. logger.error('No ticket')
  73. raise NoTicket('No ticket')
  74. if self.appear_then_click(self.I_START, interval=1.5):
  75. start_cnt += 1
  76. continue
  77. self.last_select_1, self.last_select_2, self.last_select_3, self.last_select_4 = '', '', '', ''
  78. quiz_timer = Timer(1.4)
  79. quiz_timer.start()
  80. while 1:
  81. self.screenshot()
  82. if self.ui_reward_appear_click():
  83. continue
  84. if self.appear(self.I_FAIL_QUIT):
  85. # 失败
  86. logger.info('Quiz Fail and exit')
  87. self.ui_click(self.I_FAIL_QUIT, self.I_START)
  88. break
  89. if self.appear(self.I_SHARE):
  90. # 结算
  91. logger.info('Quiz Victory and exit')
  92. self.ui_click(self.I_UI_BACK_RED, self.I_START)
  93. break
  94. if quiz_timer.reached():
  95. quiz_timer.reset()
  96. self._deal_quiz()
  97. continue
  98. self.close_fn()
  99. def detect_new(self, select_1, select_2, select_3, select_4) -> bool:
  100. new = True if self.last_select_1 != select_1 or \
  101. self.last_select_2 != select_2 or \
  102. self.last_select_3 != select_3 or \
  103. self.last_select_4 != select_4 else False
  104. self.last_select_1, self.last_select_2, self.last_select_3, self.last_select_4 = \
  105. select_1, select_2, select_3, select_4
  106. return new
  107. def _deal_quiz(self):
  108. countdown = self.O_COUNTDOWN.ocr(self.device.image)
  109. if countdown < 2 or countdown > 5:
  110. # 最后两秒钟的时候 进行选择
  111. return False
  112. question, answer_1, answer_2, answer_3, answer_4 = self.detect_question_and_answers()
  113. if answer_1 == '' and answer_2 == '' and answer_3 == '' and answer_4 == '':
  114. return False
  115. question = remove_symbols(question)
  116. new_question = self.detect_new(answer_1, answer_2, answer_3, answer_4)
  117. if new_question:
  118. self.answer_cnt += 1
  119. logger.info(f'Question count: {self.answer_cnt}')
  120. # questions = self.O_QUESTION.detect_and_ocr(self.device.image)
  121. # question = ''.join([q.ocr_text for q in questions])
  122. # question = question.replace('?', '').replace('?', '').replace(' ', '').replace(',', ',')
  123. # question = remove_symbols(question)
  124. index = self.anwser.answer_one(question=question, options=[answer_1, answer_2, answer_3, answer_4])
  125. if index is None:
  126. logger.error('Now question has no answer, please check')
  127. self.append_one(question=question, options=[answer_1, answer_2, answer_3, answer_4])
  128. self.config.notifier.push(title='Quiz',
  129. content=f"New question: \n{question} \n{[answer_1, answer_2, answer_3, answer_4]}")
  130. index = 1
  131. if self._config.quiz_per_round < 150 and self.answer_cnt > self._config.quiz_per_round:
  132. index_options = {1, 2, 3, 4}
  133. index_options.remove(index)
  134. index = random.choice(list(index_options))
  135. logger.info(f'Question: {question}, Answer: {index}')
  136. self.click(self.click_options[index-1], interval=1)
  137. self.device.click_record_clear()
  138. return True
  139. def detect_question_and_answers(self) -> tuple:
  140. results = self.O_QUESTION_AND_ANSWER.detect_and_ocr(self.device.image)
  141. question = ''
  142. answer_1 = ''
  143. answer_2 = ''
  144. answer_3 = ''
  145. answer_4 = ''
  146. for result in results:
  147. # box 是四个点坐标 左上, 右上, 右下, 左下
  148. # x1, y1, x2, y2 = result.box[0][0], result.box[0][1], result.box[2][0], result.box[2][1]
  149. # w, h = x2 - x1, y2 - y1
  150. y_start = result.box[0][1]
  151. y_end = result.box[2][1]
  152. text = result.ocr_text
  153. if y_start >= 0 and y_end <= 150:
  154. question += text
  155. elif y_start > 150 and y_end <= 240:
  156. answer_1 += text
  157. elif y_start > 240 and y_end <= 370:
  158. answer_2 += text
  159. elif y_start > 370 and y_end <= 470:
  160. answer_3 += text
  161. elif y_start > 470 and y_end <= 600:
  162. answer_4 += text
  163. return question, answer_1, answer_2, answer_3, answer_4
  164. if __name__ == '__main__':
  165. from module.config.config import Config
  166. from module.device.device import Device
  167. c = Config('oas1')
  168. d = Device(c)
  169. t = ScriptTask(c, d)
  170. t.screenshot()
  171. t.run()