script_task.py 7.8 KB

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