script_task.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import copy
  5. from time import sleep
  6. from datetime import time, datetime, timedelta
  7. from exceptiongroup import catch
  8. from tasks.DailyTrifles.page import page_store_gift_room
  9. from winerror import NOERROR
  10. from tasks.GameUi.game_ui import GameUi
  11. from tasks.GameUi.page import page_main, page_summon, page_guild, page_mall, page_friends
  12. from tasks.DailyTrifles.config import DailyTriflesConfig
  13. from tasks.DailyTrifles.assets import DailyTriflesAssets
  14. from tasks.Component.Summon.summon import Summon
  15. from module.logger import logger
  16. from module.exception import TaskEnd
  17. from module.base.timer import Timer
  18. from tasks.DailyTrifles.config import SummonType
  19. import re
  20. class ScriptTask(GameUi, Summon, DailyTriflesAssets):
  21. def run(self):
  22. con = self.config.daily_trifles.trifles_config
  23. # 每日召唤
  24. if con.one_summon:
  25. self.run_one_summon()
  26. if con.guild_wish:
  27. pass
  28. # 友情点
  29. if con.friend_love:
  30. self.run_friend_love()
  31. # 吉闻
  32. if con.luck_msg:
  33. self.run_luck_msg()
  34. # 商店签到 or 购买寿司
  35. if con.store_sign or con.buy_sushi_count > 0:
  36. self.run_store()
  37. self.set_next_run('DailyTrifles', success=True, finish=False)
  38. raise TaskEnd('DailyTrifles')
  39. def run_one_summon(self):
  40. self.goto_page(page_summon)
  41. config=self.config.daily_trifles.trifles_config
  42. if config.summon_type == SummonType.default:
  43. self.summon_one(draw_mystery_pattern=config.draw_mystery_pattern)
  44. self.check_time()
  45. elif config.summon_type == SummonType.recall:
  46. self.summon_recall()
  47. self.back_summon_main()
  48. def check_time(self):
  49. config = self.config.daily_trifles.trifles_config
  50. now = datetime.now()
  51. next_run = now + self.config.daily_trifles.scheduler.success_interval
  52. # 检查是否跨月(next_run的月份与当前月份不同)
  53. if next_run.month != now.month:
  54. # 跨月重置神秘图案触发状态
  55. if not config.draw_mystery_pattern:
  56. config.draw_mystery_pattern = True
  57. logger.info(
  58. f"reset draw_mystery_pattern to True, next_run: {next_run}")
  59. else:
  60. # 如果还是在同一月份,则没必要再绘制神秘图案
  61. config.draw_mystery_pattern = False
  62. self.config.save()
  63. def summon_recall(self):
  64. """
  65. 确保在召唤界面,每日召唤一次
  66. 召唤结束后回到 召唤主界面
  67. :return:
  68. """
  69. list = [self.O_SELECT_SM2, self.O_SELECT_SM3, self.O_SELECT_SM4]
  70. count = 0
  71. while True:
  72. count += 1
  73. for i in range(len(list)):
  74. sleep(1)
  75. self.goto_page(page_main)
  76. self.goto_page(page_summon)
  77. self.appear_then_click(self.I_UI_BACK_RED, interval=1)
  78. x, y = list[i].coord()
  79. self.device.click(x, y)
  80. sleep(1)
  81. self.screenshot()
  82. if self.appear(self.I_RECALL_TICKET):
  83. break
  84. logger.info("Select preset group RECALL")
  85. self.screenshot()
  86. if self.appear(self.I_RECALL_TICKET):
  87. break
  88. if count >= 3:
  89. self.config.notifier.push(title='今忆召唤抽卡失败', content='每日任务,今忆召唤抽卡失败!!!')
  90. return
  91. logger.info('Summon one RECALL')
  92. self.wait_until_appear(self.I_RECALL_TICKET)
  93. while True:
  94. ticket_info = self.O_RECALL_TICKET_AREA.ocr(self.device.image)
  95. # 处理 None 和空字符串
  96. if ticket_info is None or ticket_info == '':
  97. ticket_info = 0
  98. else:
  99. # 使用正则表达式提取字符串中的数字
  100. match = re.search(r'\d+', ticket_info)
  101. if match:
  102. ticket_info = int(match.group())
  103. else:
  104. logger.warning(f'Invalid ticket_info value: {ticket_info}, expected a numeric string')
  105. ticket_info = 0 # 将无效值设置为默认值 0
  106. if ticket_info <= 0:
  107. logger.warning('There is no any one RECALL ticket')
  108. return
  109. # 某些情况下滑动异常
  110. self.S_RANDOM_SWIPE_1.name = 'S_RANDOM_SWIPE'
  111. self.S_RANDOM_SWIPE_2.name = 'S_RANDOM_SWIPE'
  112. self.S_RANDOM_SWIPE_3.name = 'S_RANDOM_SWIPE'
  113. self.S_RANDOM_SWIPE_4.name = 'S_RANDOM_SWIPE'
  114. while 1:
  115. self.screenshot()
  116. if self.appear(self.I_RECALL_ONE_TICKET):
  117. break
  118. if self.appear_then_click(self.I_RECALL_TICKET, interval=1):
  119. continue
  120. # 画一张票
  121. sleep(1)
  122. while 1:
  123. self.screenshot()
  124. if self.appear(self.I_RECALL_SM_CONFIRM, interval=0.6):
  125. self.ui_click_until_disappear(self.I_RECALL_SM_CONFIRM)
  126. break
  127. if self.appear(self.I_SM_CONFIRM_2, interval=0.6):
  128. self.ui_click_until_disappear(self.I_SM_CONFIRM_2)
  129. break
  130. if self.appear(self.I_RECALL_ONE_TICKET, interval=1):
  131. # 某些时候会点击到 “语言召唤”
  132. if self.appear_then_click(self.I_UI_CANCEL, interval=0.8):
  133. continue
  134. self.summon()
  135. continue
  136. logger.info('Summon one success')
  137. def run_guild_wish(self):
  138. pass
  139. def run_luck_msg(self):
  140. self.goto_page(page_friends)
  141. while 1:
  142. self.screenshot()
  143. if self.appear(self.I_LUCK_TITLE):
  144. break
  145. if self.appear_then_click(self.I_FRIENDSHIP_UP, interval=1):
  146. continue
  147. if self.ocr_appear_click(self.O_LUCK_MSG, interval=1):
  148. continue
  149. logger.info('Start luck msg')
  150. check_timer = Timer(2)
  151. check_timer.start()
  152. while 1:
  153. self.screenshot()
  154. if self.appear_then_click(self.I_CLICK_BLESS, interval=1):
  155. continue
  156. if self.appear_then_click(self.I_ONE_CLICK_BLESS, interval=1):
  157. continue
  158. if self.ui_reward_appear_click():
  159. logger.info('Get reward of luck msg')
  160. break
  161. if check_timer.reached():
  162. logger.warning('There is no any luck msg')
  163. break
  164. self.ui_click(self.I_UI_BACK_RED, self.I_CHECK_MAIN)
  165. def run_friend_love(self):
  166. self.goto_page(page_friends)
  167. while 1:
  168. self.screenshot()
  169. if self.appear(self.I_L_LOVE):
  170. break
  171. if self.appear_then_click(self.I_FRIENDSHIP_UP, interval=1):
  172. continue
  173. if self.appear_then_click(self.I_L_FRIENDS, interval=1):
  174. continue
  175. if self.appear_then_click(self.I_FRIEND_TAB, interval=3):
  176. continue
  177. logger.info('Start friend love')
  178. check_timer = Timer(2)
  179. check_timer.start()
  180. while 1:
  181. self.screenshot()
  182. if self.appear_then_click(self.I_L_COLLECT, interval=1):
  183. continue
  184. if self.ui_reward_appear_click():
  185. logger.info('Get reward of friend love')
  186. break
  187. if check_timer.reached():
  188. logger.warning('There is no any love')
  189. break
  190. self.ui_click(self.I_UI_BACK_RED, self.I_CHECK_MAIN)
  191. def run_store(self):
  192. self.goto_page(page_mall, confirm_wait=3)
  193. if self.config.daily_trifles.trifles_config.store_sign:
  194. self.run_store_sign()
  195. if self.config.daily_trifles.trifles_config.buy_sushi_count > 0:
  196. self.run_buy_sushi()
  197. self.goto_page(page_mall, skip_first_screenshot=False, timeout=8)
  198. self.goto_page(page_main)
  199. def run_store_sign(self):
  200. self.goto_page(page_store_gift_room)
  201. cnt = 0
  202. while 1:
  203. self.screenshot()
  204. if self.appear(self.I_GIFT_SIGN) or cnt >=4:
  205. break
  206. if self.appear_then_click(self.I_GIFT_RECOMMEND, interval=1):
  207. cnt += 1
  208. continue
  209. logger.info('Enter store sign')
  210. sleep(1) # 等个动画
  211. self.screenshot()
  212. if not self.appear(self.I_GIFT_SIGN):
  213. logger.warning('There is no gift sign')
  214. return
  215. if self.ui_get_reward(self.I_GIFT_SIGN, click_interval=2.5):
  216. logger.info('Get reward of gift sign')
  217. def run_buy_sushi(self):
  218. # 进入Special
  219. while 1:
  220. from tasks.RichMan.assets import RichManAssets
  221. self.screenshot()
  222. if self.appear(RichManAssets.I_SIDE_CHECK_SPECIAL):
  223. break
  224. if self.appear_then_click(RichManAssets.I_MALL_SUNDRY, interval=1):
  225. continue
  226. if self.appear_then_click(RichManAssets.I_SIDE_SURE_SPECIAL, interval=1):
  227. continue
  228. def detect_buy_count(base_element) -> (int, int):
  229. # 返回count,price
  230. MAX_PRICE = 9999
  231. MAX_COUNT = 9999
  232. roi = copy.deepcopy(base_element.roi_front)
  233. roi[0] = roi[0] + roi[2]
  234. roi[1] = roi[1] + roi[3] - 30
  235. roi[2] = 60
  236. roi[3] = 30
  237. self.O_STORE_SUSHI_PRICE.roi = roi
  238. _price = self.O_STORE_SUSHI_PRICE.detect_text(self.device.image)
  239. # 保守策略,避免OCR错误购买
  240. try:
  241. _price = int(_price)
  242. except Exception as e:
  243. _price = MAX_PRICE
  244. if _price < 60:
  245. return 0, MAX_PRICE
  246. _count = (_price - 60) / 20
  247. return _count, _price
  248. roi = None
  249. # 购买体力
  250. while 1:
  251. self.screenshot()
  252. # count, price = detect_buy_count(roi)
  253. # if count >= self.config.model.daily_trifles.trifles_config.buy_sushi_count:
  254. # break
  255. if self.appear(self.I_STORE_COST_TYPE_JADE):
  256. count, price = detect_buy_count(self.I_STORE_COST_TYPE_JADE)
  257. if count >= self.config.daily_trifles.trifles_config.buy_sushi_count:
  258. break
  259. self.ui_click_until_disappear(self.I_STORE_COST_TYPE_JADE, interval=2)
  260. logger.info(f"Buy Sushi With {price} Jade")
  261. continue
  262. if self.appear(self.I_SPECIAL_SUSHI):
  263. # 此处确定当前购买体力所需勾玉数量的位置,用于后续识别
  264. count, price = detect_buy_count(self.I_SPECIAL_SUSHI)
  265. if count >= self.config.daily_trifles.trifles_config.buy_sushi_count:
  266. break
  267. self.ui_click(self.I_SPECIAL_SUSHI, stop=self.I_STORE_COST_TYPE_JADE, interval=2)
  268. continue
  269. return
  270. if __name__ == '__main__':
  271. from module.config.config import Config
  272. from module.device.device import Device
  273. c = Config('oas1')
  274. d = Device(c)
  275. t = ScriptTask(c, d)
  276. t.run()