script_task.py 5.2 KB

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