script_task.py 10 KB

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