script_task.py 7.8 KB

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