def run(): print('每日任务开始。。。') print('目前仅支持 ”商船护卫“、”海域突进“、“斩首行动” 三个类别。') print('“战术研修”和“破交作战”由于战力要求和操控要求比较高,因此暂不能自动化。') print() adb = AutoAdb() task_list = [{ 'loc': Location(adb, None, 160, 400, '商船护卫'), 'day': [1, 4, 7] }, { 'loc': Location(adb, None, 370, 400, '海域突进'), 'day': [2, 5, 7] }, { 'loc': Location(adb, None, 900, 400, '斩首行动'), 'day': [3, 6, 7] }] week_day = datetime.datetime.now().weekday() + 1 for task in task_list: task_loc = task['loc'] if week_day not in task['day']: continue PageUtils.to_stage_page() adb.wait( 'temp_images/daily-task/daily-task/goto-daily-task.png').click() task_loc.click() Location(adb, None, 640, 400).click() # 执行方法 print('今天是周%d,%s开放' % (week_day, task_loc.remark)) TaskHelper(task_loc.remark).run() PageUtils.to_main_page()
def wait(self, temp_rel_path, threshold=threshold, cycle_interval=0, max_wait_time=None, episode=None): timer = Timer() while True: duration = timer.get_duration() print('\r > wait %s ... %ds ' % (temp_rel_path, duration), end='') if max_wait_time is not None and 0 < max_wait_time < duration: print(' ×', flush=True) return Location(self, None, None, None) if episode is not None: try: episode() except Exception as e: print('过程方法执行异常') print(e) loc = self.get_location(temp_rel_path, threshold=threshold) if loc is not None: print(' √', flush=True) return loc if cycle_interval > 0: time.sleep(cycle_interval)
def run(self): # 点击出击 Location(self.adb, None, self.x_pos, self.y_pos + self.y_step * self.flag).click() # 观察是否已经耗尽机会 while True: # 出现出击按钮,说明已经进入关卡了 if self.adb.check('temp_images/fight/fight.png'): break # 如果出现没有机会提示,则说明没机会了 if self.adb.check( 'temp_images/daily-task/daily-task/no-chance.png', 'temp_images/daily-task/daily-task/no-chance-2.png', threshold=0.95): print('机会耗尽,%s结束' % self.task_name) return # 切换到最右边的队伍 while True: have_right = self.adb.click( 'temp_images/daily-task/daily-task/right-team.png') if not have_right: break # 出击 print('出击第 %d 关' % (self.flag + 1)) result = StageFight.fight() if not result: print('挑战失败,目标下移。。。') self.step_add() # 调用自己,继续战斗 self.run()
def wait(self, temp_rel_path, threshold=threshold, max_wait_time=None, episode=None): timer = Timer() none_loc = Location(self, None, None, None) while True: duration = timer.get_duration() print('\r > wait %s ... %ds ' % (temp_rel_path, duration), end='') if max_wait_time is not None and 0 < max_wait_time < duration: print(' ×', flush=True) return none_loc if episode is not None: try: res = episode() if res is not None and not res: return none_loc except Exception as e: print('过程方法执行异常') print(e) loc = self.get_location(temp_rel_path, threshold=threshold) if loc is not None: print(' √', flush=True) return loc
def run(): adb = AutoAdb() blank_loc = Location(adb, None, 600, 30) PageUtils.to_main_page() # 点击生活区 Location(adb, None, 580, 680).click() # 点击指挥喵 Location(adb, None, 980, 460).click() # 判断有无免费订购。 have_free = adb.click("temp_images/daily-task/meow/free-meow.png", threshold=0.95) if have_free: # 第二次判断 have_free = adb.check("temp_images/daily-task/meow/free-meow-1.png", threshold=0.95) if have_free: print('领取每日免费喵箱...') # 结算 adb.wait('temp_images/daily-task/meow/buy.png').click() # 点击确定 adb.wait('temp_images/daily-task/meow/confirm.png').click() # 二次确定 adb.wait("temp_images/click-to-continue.png").click() # 关闭领取对话框 blank_loc.click() # 点击训练 Location(adb, None, 1200, 680).click() # 等待对话框打开 adb.wait('temp_images/daily-task/meow/in-lesson-page.png') while adb.click('temp_images/daily-task/meow/lesson-finish.png', wait_time=2): print('领取训练完成的喵...') meow_full = adb.check('temp_images/daily-task/meow/meow-full.png') if meow_full: print('喵窝已满!!!请先处理!!!') return while True: if adb.click('temp_images/daily-task/meow/confirm.png'): continue if adb.click('temp_images/daily-task/meow/new-meow-btn.png'): continue if adb.check('temp_images/daily-task/meow/in-lesson-page.png'): break print('训练新的喵...') # 点击“开始训练” adb.wait('temp_images/daily-task/meow/start-lesson.png').click() # 点击“一键选择” adb.wait("temp_images/daily-task/meow/pick-all.png").click() # 点击“开始训练” adb.wait('temp_images/daily-task/meow/start-lesson.png').click() # 点击确认 adb.wait("temp_images/daily-task/meow/confirm.png", max_wait_time=3).click() # 回到主页 PageUtils.to_main_page() print('指挥喵处理完毕')
def run(): print('日常巡检开始。。。') adb = AutoAdb() notice_loc = adb.get_location('temp_images/daily-task/notice.png') if not notice_loc: print('没有待处理的任务,巡检结束') return # 菜单位置 menu_pos = [ Location(adb, None, 628, 674, 'life-area'), # 生活区 Location(adb, None, 946, 674, 'missions'), # 任务 ] nearest_loc = notice_loc.get_nearest(menu_pos) nearest_loc.click() nearest_remark = nearest_loc.remark if nearest_remark == 'life-area': check_life_area(adb) elif nearest_remark == 'missions': deal_missions(adb) else: print('不能分辨出处理任务')
def fight(): adb = AutoAdb() # 战斗前各种按钮的点击 while True: if PageUtils.in_fight_page(): break click = adb.click('temp_images/fight/fight.png') if click: continue retired = PortUtils.check_port_full() if retired: # 如果发生了退役操作,则再次点击确认按钮 AutoAdb().wait('temp_images/fight/fight.png').click() continue low_mood = adb.check('temp_images/fight/low-mood.png') if low_mood: print('低心情状态,脚本终止') exit() print('战斗开始 >>>') adb.wait('temp_images/fight/fight-finish.png', cycle_interval=5) # 战斗结束 fight_result = None ending_loc = Location(adb, None, 1160, 690) while True: if fight_result is None: # 处理新船 new_ship = adb.check('temp_images/fight/new-ship.png') if new_ship: print('发现新船!!') fight_result = True ending_loc.click() adb.click('temp_images/confirm-btn.png') continue # 处理失败 fail_confirm = adb.click('temp_images/fight/fail-confirm.png') if fail_confirm: fight_result = False # 战队难以成型时点击确定 adb.wait('temp_images/confirm-btn.png', max_wait_time=3).click() break # 持续点击右下角 ending_loc.click() # 回到 stage列表页面 或 敌人列表页面 也说明战斗已经结束 if adb.check( 'temp_images/page/in-stage.png', # stage列表 'temp_images/page/in-enemy.png', # enemy列表 'temp_images/page/main-page-button.png'): # 日常任务结束后的界面 fight_result = True break print('战斗胜利~(~ ̄▽ ̄)~' if fight_result else '战斗失败 >_<') # 战斗结束后可能出现紧急任务提示 # 由于是透明遮罩, 所以无法根据其他元素是否显示而做出反应, 只能等一定的时间 adb.wait('temp_images/confirm-btn.png', max_wait_time=2).click() return fight_result
def get_location(self, *temp_rel_path_list, threshold=threshold): self.screen_cap() sp_gray = cv2.imread(self.screen_pic_path, cv2.COLOR_BGR2BGRA) for temp_rel_path in temp_rel_path_list: temp_abs_path = PathUtils.get_abs_path(temp_rel_path) temp_gray = cv2.imread(temp_abs_path, cv2.COLOR_BGR2BGRA) res = cv2.matchTemplate(sp_gray, temp_gray, cv2.TM_CCOEFF_NORMED) _, max_val, _, max_loc = cv2.minMaxLoc(res) if max_val < threshold: continue h, w, _ = cv2.imread(temp_abs_path).shape x = max_loc[0] + w / 2 y = max_loc[1] + h / 2 return Location(self, temp_rel_path, x, y) return None
def fight_finish(): adb = AutoAdb() # 等待战斗结束 timer = Timer() while True: print('\r等待战斗结束 %ds ...' % timer.get_duration(), end='') skip_dialog() time.sleep(3) # 每次循环加一个等待时间,降低cpu占用 if adb.check('temp_images/fight/fight-finish.png'): print(' √ 总耗时: %ds' % timer.get_duration()) break # 战斗结束 fight_result = None ending_loc = Location(adb, None, 1160, 690) while True: if fight_result is None: # 处理新船 new_ship = adb.check('temp_images/fight/new-ship.png') if new_ship: print('发现新船!!') fight_result = True ending_loc.click() adb.click('temp_images/confirm-btn.png') continue # 处理失败 fail_confirm = adb.click('temp_images/fight/fail-confirm.png') if fail_confirm: fight_result = False # 战队难以成型时点击确定 adb.wait('temp_images/confirm-btn.png', max_wait_time=3).click() break # 持续点击右下角 ending_loc.click() # 回到 stage列表页面 或 敌人列表页面 也说明战斗已经结束 if adb.check('temp_images/page/in-stage.png', # stage列表 'temp_images/page/in-enemy.png', # enemy列表 'temp_images/page/in-daily.png'): # 日常任务结束后的界面 fight_result = True break if adb.check('temp_images/page/in-operation.png'): print('误入演习界面,退出。。。') PageUtils.back() continue print('战斗胜利~(~ ̄▽ ̄)~' if fight_result else '战斗失败 >_<') # 战斗结束后可能出现紧急任务提示 # 由于是透明遮罩, 所以无法根据其他元素是否显示而做出反应, 只能等一定的时间 adb.wait('temp_images/confirm-btn.png', max_wait_time=2).click() return fight_result
def to_main_page(): adb = AutoAdb() temp_list = [ 'temp_images/page/main-page-button.png', # 主页按钮 'temp_images/page/close.png', # 关闭按钮 'temp_images/page/close-1.png', # 关闭按钮 'temp_images/page/back.png', # 返回按钮 'temp_images/page/back-2.png' # 返回按钮 ] while True: # 如果已经在主页则直接返回 if in_main_page(): return True # 尝试点击返回按钮 loc = adb.get_location(*temp_list) if loc is None: # 如果没找到合理的返回按钮,则点击左上角尝试 Location(adb, None, 10, 10).click() else: loc.click()
def fight_in_stage(): auto_adb = AutoAdb() while True: # 寻找敌人 res = provoke_enemy() if not res: break print('战斗开始 >>>') auto_adb.wait('temp_images/fight/fight-finish.png', cycle_interval=5) print(' 战斗结束 !') ending_loc = Location(auto_adb, None, 1040, 660) while True: in_stage = auto_adb.check('temp_images/stage/in-stage.png') in_unit = auto_adb.check('temp_images/stage/in-unit.png') if in_stage or in_unit: break new_ship = auto_adb.check('temp_images/fight/new-ship.png') if new_ship: print('发现新船!!') ending_loc.click() auto_adb.click('temp_images/fight/new-ship-confirm.png') continue fail_confirm = auto_adb.click('temp_images/fight/fail-confirm.png') if fail_confirm: input('战斗失败!! 请整理后按下任何按键以继续 ...') continue ending_loc.click() # 可能出现紧急任务提示 # 由于是透明遮罩, 所以无法根据其他元素是否显示而做出反应, 只能等一定的时间 auto_adb.wait('temp_images/fight/urgent-task.png', max_wait_time=3).click()