def wait_for_leaving_color(color, max_waiting_time=15, max_click_time=5, clicking=False, clicking_gap=0.2, location=None, rand_offset=3): logging.info(f'trying to leave color table {color}') color_location = myFindColor(color) count = 0 cur_time = datetime.datetime.now() recheck = 0 while recheck < 3: while color_location: click_location = location if location else color_location time.sleep(0.2) if clicking: utilities.random_sleep(clicking_gap, 0.2) click(click_location, rand_offset, tired_check=False, need_convert=True) count += 1 if count > max_click_time: logging.info( f'failed to leave state {img} for {retry_time}.') raise Exception time.sleep(0.5) color_location = myFindColor(color) if (datetime.datetime.now() - cur_time).seconds > max_waiting_time: logging.info(f'time out finding color table {color}.') raise Exception recheck += 1 time.sleep(0.5)
def get_ready(self): for i in range(2): # 消抖 while self.get_state() == "st_getting_ready": random_sleep(0.5, 0.5) click(self.coord_entry, tired_check=False) random_sleep(0.5, 0.5)
def tansuo_to_dungeon(): logging.info('尝试进入副本。') enter_dugeon_loc = wait_for_color(TansuoColor.EnterDungeon, max_time=5) if enter_dugeon_loc: click(enter_dugeon_loc) in_dugeon = wait_for_color(TansuoColor.InDungeon) if not in_dugeon: return tansuo_to_dungeon() else: loc = map_locator() if loc == LocatorColor.Main: time.sleep(1) constants.INPUT_CONTROLLER.move(1570, 720) constants.INPUT_CONTROLLER.drag(x0=1570 + random.randrange(30), y0=720 - random.randrange(10), x1=173 + random.randrange(30), y1=720 - random.randrange(10), duration=0.5) utilities.random_sleep(0.2, 0.5) click((681, 263), random_range=10, need_convert=True) wait_for_color(LocatorColor.Map) elif loc == LocatorColor.Map: pass else: raise KeyError constants.INPUT_CONTROLLER.move(1564 + random.randrange(-50, 50), 648 + random.randrange(-50, 50)) time.sleep(0.2) for i in range(random.randrange(50, 100)): constants.INPUT_CONTROLLER.scroll() time.sleep(0.01) click((1555, 870), random_range=15, need_convert=True) return tansuo_to_dungeon()
def repeat_tansuo(times=999, liao_sep=8): t = 0 while t < times: tansuo_to_dungeon() result = one_tansuo() t += 1 if not t % liao_sep: enter_tupo() click((1648, 505), random_range=3, need_convert=True) liao_status = main_liaotupo() click((1648, 333), need_convert=True) personal_status = main_personaltupo() click((1648, 505), need_convert=True) liao_status = main_liaotupo() escape() wait_for_color(LocatorColor.Main) time.sleep(1) constants.INPUT_CONTROLLER.move(1570, 720) constants.INPUT_CONTROLLER.drag(x0=1570 + random.randrange(30), y0=720 - random.randrange(10), x1=173 + random.randrange(30), y1=720 - random.randrange(10), duration=0.5) utilities.random_sleep(0.2, 0.5) click((681, 263), random_range=10, need_convert=True) wait_for_color(LocatorColor.Map)
def get_bonus(self): for i in range(2): # 消抖 while self.get_state() == "st_getting_bonus": random_sleep(0.5, 0.1) click(self.coord_leaving, tired_check=False) random_sleep(0.5, 0.1)
def one_tansuo(): wait_for_color(TansuoColor.InDungeon) fight_count = 0 search_for_exp(fight_count) for find_time in range(4): print(f'找怪第{find_time+1}次') next_scene() result = search_for_exp(fight_count) if result == TansuoResult.FinishedWithBoss: print('finished with boss') # while 1: # result = find_color(ChaoGuiColor.FindChaoGui) # if result: # pyautogui.click((510, 392)) # speaker = win32com.client.Dispatch("SAPI.SpVoice") # speaker.Speak(u"Found Guiwang") # pyautogui.click((510, 392)) # time.sleep(99999) # time.sleep(0.1) return result wait_for_color(TansuoColor.InDungeon) accept_invite() click((77, 124), random_range=3, need_convert=True) utilities.random_sleep(0.5, 1) accept_invite() click((1030, 557), random_range=3, tired_check=False, need_convert=True) wait_for_color(TansuoColor.EnterDungeon) return TansuoResult.FinishedWithoutBoss
def as_leader(batter_name): while 1: battle = Combat(batter_name) battle.start(auto_ready=True) time.sleep(2) start_loc = wait_for_color(PartyColor.StartFight) utilities.random_sleep(0.5, 2) click(start_loc, random_range=3, tired_check=True)
def tap_to_main(self): count = 0 while not myFindColor(LocatorColor.Jiejie): # print(pyautogui.pixel(self.x, self.y)) utilities.random_sleep(0.1, 0.2) click((491, 113), random_range=20, tired_check=False, need_convert=True) count += 1 if count > self.retry_time: logging.info(f'failed to enter jiejie for {self.retry_time}.') raise EnterJiejieDashboardFailed
def main_personaltupo(refresh_time=3, desc=True): ''' 1. 锁定阵容 2,进入寮突破 3. 开始挂机 :return: ''' tupo_main = MainJiejie() tupo_main.tap_to_main() personal_tupo = PersonalTuPo(desc) remain = personal_tupo.get_remain_chance() while remain: win_count, avaliable_targets = personal_tupo.get_tupo_page_status() if win_count < refresh_time: next_target = avaliable_targets[0] logging.debug(f'this target have {next_target.metals} metal.') click((int((next_target.region[2] + next_target.region[0]) / 2), int((next_target.region[3] + next_target.region[1]) / 2))) this_color = JiejieColor.PersonalAttack x_offset, y_offset = 423 - 185, 547 - 171 x_offset, y_offset = cordinates_scale((x_offset, y_offset), constants.WINDOW_ATTRIBUTES) this_color.region = [ next_target.region[0], next_target.region[1], next_target.region[2] + x_offset, next_target.region[1] + y_offset ] this_color.screen_shot_region = this_color.get_region_to_screenshot( ) time.sleep(1) attack_cords = wait_for_color(this_color) accept_invite() click(attack_cords) this_fight = combat.Combat('结界突破', combat_time_limit=60 * 2 + random.randint(40, 80)) try: combat_result = this_fight.start(auto_ready=True) except TimeoutError: logging.warning('Failed to finish tupo combat!') accept_invite() click((986, 629), tired_check=False, need_convert=True) escape() utilities.random_sleep(1, 0.5) accept_invite() click((986, 629), tired_check=False, need_convert=True) # combat_result = this_fight.start(auto_ready=True) logging.debug('finished one tupo') tupo_main.tap_to_main() remain = personal_tupo.get_remain_chance() else: personal_tupo.refresh() wait_for_state(img.jiejietupo_img.MAIN_TUPO) return 0
def choose_boss(self): for i in range(2): # 消抖 while self.get_state() == "st_choosing_boss": # 选择鬼王 ind = np.random.randint(0, 3) click(self.coord_bosses[ind], tired_check=False) random_sleep(0.5, 0.5) # 点击“开始” click(self.coord_ok, tired_check=False) random_sleep(0.5, 0.5)
def click_to_leaving_state(img, retry_time=10, location=None, rand_offset=10): if not location: location = findimg(img) count = 0 while location: utilities.random_sleep(0.2, 0.4) click(location, rand_offset, tired_check=False, need_convert=True) count += 1 location = findimg(img) if count > retry_time: logging.info(f'failed to leave state {img} for {retry_time}.') break
def main_liaotupo(): ''' 1. 锁定阵容 2,进入寮突破 3. 开始挂机 :return: ''' tupo_main = MainJiejie() tupo_main.tap_to_main() liao_tupo = LiaoTuPo() remain = liao_tupo.get_remain_chance() while remain: try: next_target = liao_tupo.get_next_avaliable_target() click((int((next_target.region[2] + next_target.region[0]) / 2), int((next_target.region[3] + next_target.region[1]) / 2))) this_color = JiejieColor.LiaoAttack y_offset_top, y_offset_bottom = 593 - 729, 920 - 549 y_offset_top, y_offset_bottom = cordinates_scale( (y_offset_top, y_offset_bottom), constants.WINDOW_ATTRIBUTES) this_color.region = [ next_target.region[0], next_target.region[1] + y_offset_top, next_target.region[2], next_target.region[1] + y_offset_bottom ] this_color.screen_shot_region = this_color.get_region_to_screenshot( ) time.sleep(1) attack_cords = wait_for_color(this_color) accept_invite() click(attack_cords) this_fight = combat.Combat('阴阳寮突破', combat_time_limit=60 * 5 + random.randint(40, 80)) try: combat_result = this_fight.start(auto_ready=True) except TimeoutError: logging.warning('Failed to finish tupo combat!') accept_invite() click((986, 629), tired_check=False, need_convert=True) escape() utilities.random_sleep(1, 0.5) accept_invite() click((986, 629), tired_check=False, need_convert=True) # combat_result = this_fight.start(auto_ready=True) tupo_main.tap_to_main() liao_tupo.current_page = 0 logging.debug('finished one tupo') remain = liao_tupo.get_remain_chance() except LiaotupoFinishedException: # no need to continue return 0 return 1
def swap_full_exp_mons(): wait_for_color(CombatColor.Ready) need_change_mons = detect_full_exp_mons() if need_change_mons: click((100, 743), tired_check=True, random_range=5) logging.debug('更换狗粮中。。。') utilities.random_sleep(2, 1) click((113, 928), tired_check=False, random_range=5) utilities.random_sleep(0.2, 0.2) click((228, 525), tired_check=False, random_range=5) utilities.random_sleep(0.5, 0.5) for num, change in enumerate(need_change_mons): this_mons_loc_x = mons_loc[num][0] + random.randrange(-10, 10) this_mons_loc_y = mons_loc[num][1] + random.randrange(-10, 10) constants.INPUT_CONTROLLER.move(x=this_mons_loc_x, y=this_mons_loc_y, duration=0.2) constants.INPUT_CONTROLLER.click_down() constants.INPUT_CONTROLLER.move(x=this_mons_loc_x, y=this_mons_loc_y - 240 - random.randrange(20), duration=0.2) constants.INPUT_CONTROLLER.move( x=slot_loc[change][0] + random.randrange(-10, 10), y=slot_loc[change][1] + +random.randrange(-10, 10), duration=0.2) constants.INPUT_CONTROLLER.click_up() utilities.random_sleep(0.5, 0.5)
def as_leader(batter_name, chaoguiwang=False): while 1: if chaoguiwang: battle = Combat(batter_name, combat_time_limit=99999) else: battle = Combat(batter_name) battle.start(auto_ready=True) if chaoguiwang: time.sleep(5) else: time.sleep(2) logging.debug("waiting for start combat") start_loc = wait_for_color(PartyColor.StartFight, max_time=99999) utilities.random_sleep(0.5, 2) click(start_loc, random_range=3, tired_check=True)
def one_tansuo(): wait_for_color(TansuoColor.InDungeon) fight_count = 0 search_for_exp(fight_count) for find_time in range(4): print(f'找怪第{find_time+1}次') next_scene() result = search_for_exp(fight_count) if result == TansuoResult.FinishedWithBoss: print('finished with boss') return result wait_for_color(TansuoColor.InDungeon) click((77, 124), random_range=3, need_convert=True) utilities.random_sleep(0.5, 1) click((1030, 557), random_range=3, tired_check=False, need_convert=True) wait_for_color(TansuoColor.EnterDungeon) return TansuoResult.FinishedWithoutBoss
def start(self): enter_yeyuanhuo() while self.get_remain_chance(): fight_loc = wait_for_color(YeyuanhuoColor.ReadyFightYeyuanhuoChi, max_time=10) click(fight_loc, random_range=5, tired_check=True) utilities.random_sleep(90, 60 * 3) wait_for_color(CombatColor.Damo, max_time=60 * 5) leaving_test = 0 while leaving_test < 3: utilities.random_sleep(0.2, 0.5) wait_for_leaving_color(CombatColor.Damo, max_waiting_time=15, max_click_time=8, clicking=True, clicking_gap=0.2, location=(57, 940), rand_offset=20) leaving_test += 1
def do_mamemaki(self): self.logger.info("Entered pseudo-state st_doing_mamemaki") random_sleep(5) # 等待先头部队出场 # 背景剪除 # there are quite many background removing functions provided by OpenCV # try some fast ones? bg_substractor = cv2.createBackgroundSubtractorMOG2( detectShadows=False) while self.get_state() not in BaiguiyexingStateDecision.states: # 只要没到其他状态就认为砸豆子还在继续 img_grabbed = grab_screen(region=self.win_region, use_channel_rgb=False) fg_mask = bg_substractor.apply(img_grabbed) # 消除孤立的细小轮廓 kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)) fg_mask = cv2.morphologyEx(fg_mask, cv2.MORPH_OPEN, kernel) # cv2.imwrite("fg_mask.jpg", fg_mask) # 调试用 contours, hierarchy = cv2.findContours(fg_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) hit_coord = [] # 保存候选点击坐标 for c in contours: # 求轮廓的中心点 m = cv2.moments(c) if abs(m['m00']) < 10000: # 面积太小则跳过 continue x = int(m['m10'] / m['m00']) + constants.WINDOW_ATTRIBUTES["x_offset"] y = int(m['m01'] / m['m00']) + constants.WINDOW_ATTRIBUTES["y_offset"] hit_coord.append((x, y)) # print(f"hit_coord is {hit_coord}") if len(hit_coord) > 0: # 随机选取hit_coord中的一个点进行点击 ind = np.random.randint(0, len(hit_coord)) click(hit_coord[ind], tired_check=False) random_sleep(0.5, 0.1)
def main_all_tupo(refrehs_time=3, desc=True): accept_invite() liao_status = main_liaotupo() click((1648, 333), need_convert=True) personal_status = main_personaltupo(refrehs_time, desc) click((1648, 505), need_convert=True) while personal_status or liao_status: liao_status = main_liaotupo() escape() time.sleep((10 + random.randrange(10, 20)) * 60) wait_for_color(LocatorColor.Main) time.sleep(1) constants.INPUT_CONTROLLER.move(1570, 720) constants.INPUT_CONTROLLER.drag(x0=1570 + random.randrange(30), y0=720 - random.randrange(10), x1=173 + random.randrange(30), y1=720 - random.randrange(10), duration=0.5) utilities.random_sleep(0.2, 0.5) click((681, 263), random_range=10, need_convert=True) wait_for_color(LocatorColor.Map) click((420, 943), random_range=3, need_convert=True) utilities.random_sleep(1, 2) click((1648, 505), random_range=3, need_convert=True) utilities.random_sleep(1, 1.5)
def refresh(self): self.wait_for_next_refresh() click((1404, 772), need_convert=True) utilities.random_sleep(1, 1) click((1040, 633), need_convert=True)
def enter_tupo(): click((429, 940), tired_check=True, need_convert=True) utilities.random_sleep(1, 1.5)
def get_result(self): # wait_for_color(CombatColor.InCombat) win_loc = myFindColor(CombatColor.Win) lose_loc = myFindColor(CombatColor.Lose) cur_time = datetime.datetime.now() while not win_loc and not lose_loc: win_loc = myFindColor(CombatColor.Win) lose_loc = myFindColor(CombatColor.Lose) # self.logger.debug(f'win_loc: {win_loc}; lose_loc: {lose_loc}') if (datetime.datetime.now() - cur_time).seconds > self.combat_time_limit: self.logger.debug('time out ending combat.') if myFindColor(CombatColor.Damo): leaving_test = 0 while leaving_test < 3: utilities.random_sleep(0.2, 0.5) wait_for_leaving_color(CombatColor.Damo, max_waiting_time=15, max_click_time=8, clicking=True, clicking_gap=0.2, location=(57, 940), rand_offset=20) leaving_test += 1 return CombatResult.WIN else: raise TimeoutError if win_loc: click((798, 337), random_range=30, tired_check=False, need_convert=True) wait_for_leaving_color(CombatColor.Win, max_waiting_time=15, max_click_time=8, clicking=False, clicking_gap=0.3, location=(798, 337), rand_offset=30) result = None while not result: click((57, 940), random_range=3, tired_check=False, need_convert=True) utilities.random_sleep(0.2, 0.5) result = myFindColor(CombatColor.Damo) leaving_test = 0 while leaving_test < 3: utilities.random_sleep(0.2, 0.5) wait_for_leaving_color(CombatColor.Damo, max_waiting_time=15, max_click_time=8, clicking=True, clicking_gap=0.2, location=(57, 940), rand_offset=20) leaving_test += 1 return CombatResult.WIN elif lose_loc: click((798, 337), random_range=30, tired_check=False, need_convert=True) wait_for_leaving_color(CombatColor.Lose, max_waiting_time=15, max_click_time=3, clicking=True, clicking_gap=0.2, location=(798, 337), rand_offset=30) utilities.random_sleep(1, 0.5) return CombatResult.LOSE else: self.exist() return CombatResult.LOSE
if __name__ == '__main__': user32 = windll.user32 user32.SetProcessDPIAware() constants.init_constants(move_window=1) logging.basicConfig( level=0, format= '%(asctime)s.%(msecs)03d %(levelname)s %(module)s - %(funcName)s: %(message)s', datefmt="%Y-%m-%d %H:%M:%S") _CAPTAINSLOT = 3 enter_tupo() click((1648, 505), random_range=3, need_convert=True) liao_status = main_liaotupo() click((1648, 333), need_convert=True) personal_status = main_personaltupo() click((1648, 505), need_convert=True) liao_status = main_liaotupo() escape() wait_for_color(LocatorColor.Main) time.sleep(1) constants.INPUT_CONTROLLER.move(1570, 720) constants.INPUT_CONTROLLER.drag(x0=1570 + random.randrange(30), y0=720 - random.randrange(10), x1=173 + random.randrange(30), y1=720 - random.randrange(10), duration=0.5) utilities.random_sleep(0.2, 0.5) click((681, 263), random_range=10, need_convert=True) wait_for_color(LocatorColor.Map) repeat_tansuo()
def search_for_exp(fight_count): count = 0 print('寻找经验怪, 同屏找怪5秒') search_t = datetime.datetime.now() while (datetime.datetime.now() - search_t).total_seconds() <= 5: # print(f'找怪第{count}次') exp_loc = myFindColor(TansuoColor.ExpIcon) if exp_loc: print('找到经验怪') # click(exp_loc) accept_invite() combat_loc = myFindColor( ColorToMatch((max(exp_loc[0] - 200, 0), max( exp_loc[1] - 500, 0), exp_loc[0] + 200, exp_loc[1] - 30), [[(0, 0), (229, 230, 248)], [(-14, 31), (237, 163, 172)], [(-23, -13), (66, 77, 132)]], 10)) if combat_loc: logging.debug('找到战斗') click(combat_loc) time.sleep(0.5) if_outof_sushi() combat_loc = myFindColor(TansuoColor.CombatIcon) if combat_loc: logging.debug('进入战斗失败') # check_current_state() return search_for_exp(fight_count) swap_full_exp_mons() this_fight = combat.Combat('探索', combat_time_limit=60 * 1 + random.randint(40, 80)) combat_result = this_fight.start(auto_ready=False) time.sleep(1) wait_for_color(TansuoColor.InDungeon) boss_loc = myFindColor(TansuoColor.CombatBoss) if boss_loc: click(boss_loc) fight_count = fight_count + 1 this_fight = combat.Combat('探索BOSS', combat_time_limit=60 * 1 + random.randint(40, 80)) combat_result = this_fight.start(auto_ready=False) time.sleep(2) loc = wait_for_color(TansuoColor.InDungeon, max_time=3) if loc: logging.info('loot found!') loot_loc = myFindColor(TansuoColor.BossLoot) while loot_loc: logging.info('picking up loot') click(loot_loc, tired_check=True) utilities.random_sleep(0.5, 1) click((90, 943), tired_check=True, random_range=10, need_convert=True) utilities.random_sleep(1.5, 1) loot_loc = myFindColor(TansuoColor.BossLoot) else: logging.info('no loot, enter dungeon') print('完成boss战斗') return TansuoResult.FinishedWithBoss return search_for_exp(fight_count) else: print('未找到战斗') return search_for_exp(fight_count) else: print('未找到经验怪') count = count + 1