def mouse_routine(): mouse = Mouse(sleep=1) mouse.get_position() mouse.move(10, 10) mouse.double_click() mouse.set_position(200, 400) mouse.click()
class Bot: def __init__(self, ratio=1): self.RUN = True self.TOGGLE = False self.vision = Vision(ratio=ratio) self.keyboard = Keyboard(sleep=1) self.KEYCODES = {1: 18, 2: 19, 3: 20, 4: 21, 5: 23} self.potions_box = {"top": 850, "left": 1920, "width": 500, "height": 200} self.menu_box = {"top": 300, "left": 1920 + 680, "width": 320, "height": 260} self.mouse = Mouse() def drink_defense_potion(self, x, y, xb, yb, channel, slot): print("Starting drink_defense_potion") while self.RUN: if not self.TOGGLE: sleep(0.1) continue self.vision.capture_window(self.potions_box) dp = self.vision.pcat(x=x, y=y, channel=channel, threshold=60) dp_bar = self.vision.pcat(x=xb, y=yb, channel="green", threshold=200) if dp and not dp_bar: self.keyboard.key(KeyCode.from_vk(self.KEYCODES[slot])) print(f"Drinking from Defense Potion {slot}") sleep(1) def run_fast(self): print("Starting run_fast") while self.RUN: if not self.TOGGLE: sleep(0.1) continue self.vision.capture_window(self.potions_box) qs4 = self.vision.pcat(x=425, y=175, channel="green", threshold=70) qs4_bar = self.vision.pcat(x=418, y=194, channel="green", threshold=200) qs5 = self.vision.pcat(x=465, y=175, channel="green", threshold=60) qs5_bar = self.vision.pcat(x=459, y=194, channel="green", threshold=200) if not qs5_bar and not qs4_bar: if qs4: self.keyboard.key(KeyCode.from_vk(self.KEYCODES[4])) print("Drinking from QS4") sleep(0.5) elif qs5: self.keyboard.key(KeyCode.from_vk(self.KEYCODES[5])) print("Drinking from QS5") sleep(0.5) def drink_life_potions(self): print("Starting drink_life_potions") while self.RUN: if not self.TOGGLE: sleep(0.1) continue self.vision.capture_window(self.potions_box) life_10 = self.vision.pcat(x=105, y=15, channel="red", threshold=70) life_20 = self.vision.pcat(x=105, y=25, channel="red", threshold=80) life_30 = self.vision.pcat(x=105, y=35, channel="red", threshold=80) life_40 = self.vision.pcat(x=105, y=45, channel="red", threshold=80) life_50 = self.vision.pcat(x=102, y=55, channel="red", threshold=80) life_60 = self.vision.pcat(x=102, y=65, channel="red", threshold=80) life_70 = self.vision.pcat(x=105, y=75, channel="red", threshold=80) life_80 = self.vision.pcat(x=105, y=85, channel="red", threshold=80) life_90 = self.vision.pcat(x=105, y=95, channel="red", threshold=80) life_100 = self.vision.pcat(x=105, y=105, channel="red", threshold=80) life_110 = self.vision.pcat(x=105, y=115, channel="red", threshold=80) life_120 = self.vision.pcat(x=105, y=125, channel="red", threshold=80) life_130 = self.vision.pcat(x=105, y=135, channel="red", threshold=80) life_140 = self.vision.pcat(x=105, y=145, channel="red", threshold=70) life_150 = self.vision.pcat(x=105, y=155, channel="red", threshold=70) life_160 = self.vision.pcat(x=105, y=165, channel="red", threshold=60) life_170 = self.vision.pcat(x=105, y=175, channel="red", threshold=60) life_1 = self.vision.pcat(x=298, y=175, channel="red", threshold=60) life_2 = self.vision.pcat(x=338, y=175, channel="red", threshold=60) in_game = self.vision.pcat(238, 126, channel="green", threshold=70) if not life_120 and in_game: self.keyboard.quit() print("Emergency exit (life too low)") if not life_30: if life_1: self.keyboard.key(KeyCode.from_vk(self.KEYCODES[1])) print("Drinking from LP1") sleep(0.1) elif life_2: self.keyboard.key(KeyCode.from_vk(self.KEYCODES[2])) print("Drinking from LP2") sleep(0.1) elif in_game: self.keyboard.quit() print("Emergency exit (No more potions)") def listen(self): print("Starting listen") while self.RUN: listener = Listener() listener.listen() if listener.last_key == "home": if self.TOGGLE: print("Toggling OFF") self.TOGGLE = False else: print("Toggling ON") self.TOGGLE = True if listener.last_key == "end": print("Quitting") self.RUN = False def logout(self): while self.RUN: self.vision.capture_window(self.menu_box) found = self.vision.template_match("exit.png") if found: self.mouse.set_position( x=int(1920 + 680 + 256 / 2 + found[0][0]), y=int(300 + 24 / 2 + found[0][1]), ) self.mouse.double_click()
class Location(object): def __init__(self, x, y, title="New Location"): self.title = title self.mouse = Mouse() self.keyboard = Keyboard() try: self.x = int(x) self.y = int(y) self._is_mouse_down = False logger.debug( 'New Location with name "{name}" created ({x}, {y})'.format( name=self.title, x=self.x, y=self.y)) except: raise FailExit('Incorect Location class constructor call:' '\n\tx = {x}\n\ty = {y}\n\ttitle= %{title}'.format( x=x, y=y, title=title)) def __str__(self): return 'Location ({x}, {y})'.format(x=self.x, y=self.y) @property def coordinates(self): return self.x, self.y def mouse_move(self, delay=0): self.mouse.move(self.x, self.y, delay) logger.debug('Mouse moved to ({x}, {y})'.format(x=self.x, y=self.y)) def offset(self, dx, dy): if isinstance(dx, int) and isinstance(dy, int): return Location(self.x + dx, self.y + dy) else: raise FailExit('Location.offset: incorrect offset values') def above(self, dy): if isinstance(dy, int) and dy >= 0: return Location(self.x, self.y - dy) else: raise FailExit('Location.above: incorrect value') def below(self, dy): if isinstance(dy, int) and dy >= 0: return Location(self.x, self.y + dy) else: raise FailExit('Location.below: incorrect value') def left(self, dx): if isinstance(dx, int) and dx >= 0: return Location(self.x - dx, self.y) else: raise FailExit('Location.left: incorrect value') def right(self, dx): if isinstance(dx, int) and dx >= 0: return Location(self.x + dx, self.y) else: raise FailExit('Location.right: incorrect value') def click(self, after_click_delay=0): self.mouse.click(self.x, self.y, after_click_delay) logger.debug('mouse left click in ({x}, {y})'.format(x=self.x, y=self.y)) def mouse_down(self): self.mouse.key_down(self.x, self.y) logger.debug('mouse down in ({x}, {y})'.format(x=self.x, y=self.y)) def mouse_up(self): self.mouse.key_up(self.x, self.y) logger.debug('mouse up in ({x}, {y})'.format(x=self.x, y=self.y)) def right_click(self, after_click_delay=0): self.mouse.right_click(self.x, self.y, after_click_delay) logger.debug('mouse right click in ({x}, {y})'.format(x=self.x, y=self.y)) def double_click(self, after_click_delay=0): self.mouse.double_click(self.x, self.y, after_click_delay) logger.debug('mouse double click in ({x}, {y})'.format(x=self.x, y=self.y)) def scroll(self, direction=1, count=1, click=True): # direction: # 1 - forward # -1 - backward for _ in range(0, int(count)): self.mouse.scroll(self.x, self.y, direction, click) logger.debug( 'scroll in ({x}, {y}) {count} times, {dir_} direction'.format( x=self.x, y=self.y, count=count, dir_='forward' if direction == 1 else 'backward')) def drag_to(self, *dest_location): delay = DRAGnDROP_MOVE_DELAY if len(dest_location) == 1 and isinstance(dest_location[0], Location): (dest_x, dest_y) = (dest_location[0].x, dest_location[0].y) elif len(dest_location) == 2: try: (dest_x, dest_y) = (int(dest_location[0]), int(dest_location[1])) except ValueError: raise FailExit('Location.drag_to: incorrect parameters') elif len(dest_location) == 3: try: (dest_x, dest_y) = (int(dest_location[0]), int(dest_location[1])) except ValueError: raise FailExit('Location.drag_to: incorrect parameters') delay = float(dest_location[2]) else: raise FailExit('') self.mouse.drag_to(self.x, self.y, dest_x, dest_y, delay) logger.debug('Mouse drag from (%i, %i) to (%i, %i)' % (self.x, self.y, dest_x, dest_y)) return self def drop(self): self.mouse.drop() logger.debug('Mouse drop') def dragndrop(self, *dest_location): self.drag_to(*dest_location) self.drop() return self def type(self, text, modifiers=None, click=True, click_type_delay=DELAY_BETWEEN_CLICK_AND_TYPE): log = 'Typed "{}"'.format(text) log += ' with modifiers "{}"'.format( modifiers) if modifiers is not None else '' if click: self.click(after_click_delay=click_type_delay) self.keyboard.type_text(str(text), modifiers) logger.info(log) def enter_text(self, text, modifiers=None, click=True, click_type_delay=DELAY_BETWEEN_CLICK_AND_TYPE): if click: self.click(after_click_delay=click_type_delay) self.keyboard.type_text('a', 'CTRL') time.sleep(click_type_delay) self.keyboard.type_text(str(text), modifiers)