script_task.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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.atom.image_grid import ImageGrid
  15. from module.atom.image import RuleImage
  16. from module.atom.click import RuleClick
  17. class NoTicket(Exception):
  18. pass
  19. class ScriptTask(GameUi, QuizAssets, ActivityShikigamiAssets, Debugger):
  20. answer_cnt = 0
  21. last_select_1 = ''
  22. last_select_2 = ''
  23. last_select_3 = ''
  24. last_select_4 = ''
  25. @cached_property
  26. def anwser(self) -> Answer:
  27. # Misspelling
  28. return Answer()
  29. @cached_property
  30. def click_options(self) -> list:
  31. return [self.O_ANSWER1, self.O_ANSWER2, self.O_ANSWER3, self.O_ANSWER4]
  32. @cached_property
  33. def _config(self):
  34. return self.config.model.quiz.quiz_config
  35. def run(self):
  36. self.ui_get_current_page()
  37. self.ui_goto(page_main)
  38. _config = self.config.model.quiz.quiz_config
  39. self.enter()
  40. quiz_cnt = 0
  41. while 1:
  42. if quiz_cnt >= _config.quiz_cnt:
  43. break
  44. try:
  45. self.once()
  46. quiz_cnt += 1
  47. except NoTicket:
  48. break
  49. self.ui_click(self.I_UI_BACK_YELLOW, self.I_CHECK_MAIN, interval=2)
  50. self.set_next_run(task='Quiz', success=True, finish=True)
  51. raise TaskEnd('Quiz')
  52. def enter(self):
  53. while 1:
  54. self.screenshot()
  55. if self.appear(self.I_START):
  56. break
  57. if self.appear_then_click(self.I_SHI, interval=1):
  58. continue
  59. if self.appear_then_click(self.I_ENTRY, interval=1):
  60. continue
  61. logger.info('Quiz start')
  62. def once(self) -> bool:
  63. logger.hr('Quiz', 3)
  64. start_cnt = 0
  65. self.answer_cnt = 0
  66. while 1:
  67. self.screenshot()
  68. if self.appear(self.I_MESSAGE):
  69. break
  70. if start_cnt >= 4:
  71. logger.error('No ticket')
  72. raise NoTicket('No ticket')
  73. if self.appear_then_click(self.I_START, interval=1.5):
  74. start_cnt += 1
  75. continue
  76. self.last_select_1, self.last_select_2, self.last_select_3, self.last_select_4 = '', '', '', ''
  77. while 1:
  78. self.screenshot()
  79. if self.ui_reward_appear_click():
  80. continue
  81. if self.appear(self.I_FAIL_QUIT):
  82. # 失败
  83. logger.info('Quiz Fail and exit')
  84. self.ui_click(self.I_FAIL_QUIT, self.I_START)
  85. break
  86. if self.appear(self.I_SHARE):
  87. # 结算
  88. logger.info('Quiz Victory and exit')
  89. self.ui_click(self.I_UI_BACK_RED, self.I_START)
  90. break
  91. self._deal_quiz()
  92. self.close_fn()
  93. def detect_new(self, select_1, select_2, select_3, select_4) -> bool:
  94. if self.last_select_1 != select_1 or \
  95. self.last_select_2 != select_2 or \
  96. self.last_select_3 != select_3 or \
  97. self.last_select_4 != select_4:
  98. return True
  99. return False
  100. def _deal_quiz(self):
  101. answer_1 = self.O_ANSWER1.detect_text(self.device.image)
  102. answer_2 = self.O_ANSWER2.detect_text(self.device.image)
  103. answer_3 = self.O_ANSWER3.detect_text(self.device.image)
  104. answer_4 = self.O_ANSWER4.detect_text(self.device.image)
  105. if answer_1 == '' and answer_2 == '' and answer_3 == '' and answer_4 == '':
  106. return False
  107. new_question = self.detect_new(answer_1, answer_2, answer_3, answer_4)
  108. if not new_question and\
  109. not self.ocr_appear(self.O_COUNTDOWN) and\
  110. not self.ocr_appear(self.O_COUNTDOWN3):
  111. # 第一次换题目的时候 和 最后两秒钟的时候 进行选择
  112. return False
  113. if new_question:
  114. self.answer_cnt += 1
  115. logger.info(f'Question count: {self.answer_cnt}')
  116. self.last_select_1, self.last_select_2, self.last_select_3, self.last_select_4 = \
  117. answer_1, answer_2, answer_3, answer_4
  118. questions = self.O_QUESTION.detect_and_ocr(self.device.image)
  119. question = ''.join([q.ocr_text for q in questions])
  120. question = question.replace('?', '').replace('?', '').replace(' ', '').replace(',', ',')
  121. question = remove_symbols(question)
  122. index = self.anwser.answer_one(question=question, options=[answer_1, answer_2, answer_3, answer_4])
  123. if index is None:
  124. logger.error('Now question has no answer, please check')
  125. self.append_one(question=question, options=[answer_1, answer_2, answer_3, answer_4])
  126. self.config.notifier.push(title='Quiz',
  127. content=f"New question: \n{question} \n{[answer_1, answer_2, answer_3, answer_4]}")
  128. index = 1
  129. if self._config.quiz_per_round < 150 and self.answer_cnt > self._config.quiz_per_round:
  130. index_options = {1, 2, 3, 4}
  131. index_options.remove(index)
  132. index = random.choice(list(index_options))
  133. logger.info(f'Question: {question}, Answer: {index}')
  134. self.click(self.click_options[index-1])
  135. self.device.click_record_clear()
  136. return True
  137. if __name__ == '__main__':
  138. from module.config.config import Config
  139. from module.device.device import Device
  140. c = Config('oas1')
  141. d = Device(c)
  142. t = ScriptTask(c, d)
  143. t.screenshot()
  144. t.run()