script_task.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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_FRIENDSHIP_UP, interval=1):
  132. continue
  133. if self.appear_then_click(self.I_LUCK_MSG, interval=1):
  134. continue
  135. logger.info('Start luck msg')
  136. check_timer = Timer(2)
  137. check_timer.start()
  138. while 1:
  139. self.screenshot()
  140. if self.appear_then_click(self.I_CLICK_BLESS, interval=1):
  141. continue
  142. if self.appear_then_click(self.I_ONE_CLICK_BLESS, interval=1):
  143. continue
  144. if self.ui_reward_appear_click():
  145. logger.info('Get reward of luck msg')
  146. break
  147. if check_timer.reached():
  148. logger.warning('There is no any luck msg')
  149. break
  150. self.ui_click(self.I_UI_BACK_RED, self.I_CHECK_MAIN)
  151. def run_friend_love(self):
  152. self.ui_get_current_page()
  153. self.ui_goto(page_friends)
  154. while 1:
  155. self.screenshot()
  156. if self.appear(self.I_L_LOVE):
  157. break
  158. if self.appear_then_click(self.I_FRIENDSHIP_UP, interval=1):
  159. continue
  160. if self.appear_then_click(self.I_L_FRIENDS, interval=1):
  161. continue
  162. logger.info('Start friend love')
  163. check_timer = Timer(2)
  164. check_timer.start()
  165. while 1:
  166. self.screenshot()
  167. if self.appear_then_click(self.I_L_COLLECT, interval=1):
  168. continue
  169. if self.ui_reward_appear_click():
  170. logger.info('Get reward of friend love')
  171. break
  172. if check_timer.reached():
  173. logger.warning('There is no any love')
  174. break
  175. self.ui_click(self.I_UI_BACK_RED, self.I_CHECK_MAIN)
  176. def run_store(self):
  177. self.ui_get_current_page()
  178. self.ui_goto(page_mall, confirm_wait=3)
  179. if self.config.daily_trifles.trifles_config.store_sign:
  180. self.run_store_sign()
  181. if self.config.daily_trifles.trifles_config.buy_sushi_count > 0:
  182. self.run_buy_sushi()
  183. self.ui_click(self.I_UI_BACK_YELLOW, self.I_CHECK_MALL)
  184. self.ui_get_current_page()
  185. self.ui_goto(page_main)
  186. def run_store_sign(self):
  187. while 1:
  188. self.screenshot()
  189. if self.appear(self.I_GIFT_RECOMMEND):
  190. break
  191. if self.appear_then_click(self.I_ROOM_GIFT, interval=1):
  192. continue
  193. self.screenshot()
  194. self.appear_then_click(self.I_GIFT_RECOMMEND, interval=1)
  195. logger.info('Enter store sign')
  196. sleep(1) # 等个动画
  197. self.screenshot()
  198. if not self.appear(self.I_GIFT_SIGN):
  199. logger.warning('There is no gift sign')
  200. return
  201. if self.ui_get_reward(self.I_GIFT_SIGN, click_interval=2.5):
  202. logger.info('Get reward of gift sign')
  203. def run_buy_sushi(self):
  204. # 进入Special
  205. while 1:
  206. from tasks.RichMan.assets import RichManAssets
  207. self.screenshot()
  208. if self.appear(RichManAssets.I_SIDE_CHECK_SPECIAL):
  209. break
  210. if self.appear_then_click(RichManAssets.I_MALL_SUNDRY, interval=1):
  211. continue
  212. if self.appear_then_click(RichManAssets.I_SIDE_SURE_SPECIAL, interval=1):
  213. continue
  214. def detect_buy_count(base_element) -> (int, int):
  215. # 返回count,price
  216. MAX_PRICE = 9999
  217. MAX_COUNT = 9999
  218. roi = copy.deepcopy(base_element.roi_front)
  219. roi[0] = roi[0] + roi[2]
  220. roi[1] = roi[1] + roi[3] - 30
  221. roi[2] = 60
  222. roi[3] = 30
  223. self.O_STORE_SUSHI_PRICE.roi = roi
  224. _price = self.O_STORE_SUSHI_PRICE.detect_text(self.device.image)
  225. # 保守策略,避免OCR错误购买
  226. try:
  227. _price = int(_price)
  228. except Exception as e:
  229. _price = MAX_PRICE
  230. if _price < 60:
  231. return 0, MAX_PRICE
  232. _count = (_price - 60) / 20
  233. return _count, _price
  234. roi = None
  235. # 购买体力
  236. while 1:
  237. self.screenshot()
  238. # count, price = detect_buy_count(roi)
  239. # if count >= self.config.model.daily_trifles.trifles_config.buy_sushi_count:
  240. # break
  241. if self.appear(self.I_STORE_COST_TYPE_JADE):
  242. count, price = detect_buy_count(self.I_STORE_COST_TYPE_JADE)
  243. if count >= self.config.daily_trifles.trifles_config.buy_sushi_count:
  244. break
  245. self.ui_click_until_disappear(self.I_STORE_COST_TYPE_JADE, interval=2)
  246. logger.info(f"Buy Sushi With {price} Jade")
  247. continue
  248. if self.appear(self.I_SPECIAL_SUSHI):
  249. # 此处确定当前购买体力所需勾玉数量的位置,用于后续识别
  250. count, price = detect_buy_count(self.I_SPECIAL_SUSHI)
  251. if count >= self.config.daily_trifles.trifles_config.buy_sushi_count:
  252. break
  253. self.ui_click(self.I_SPECIAL_SUSHI, stop=self.I_STORE_COST_TYPE_JADE, interval=2)
  254. continue
  255. return
  256. if __name__ == '__main__':
  257. from module.config.config import Config
  258. from module.device.device import Device
  259. c = Config('oas1')
  260. d = Device(c)
  261. t = ScriptTask(c, d)
  262. t.run_store()