Example #1
0
def resetar():
    for i in re.sub('[^0-9]', '', cortar_e_ocr((1350, 380, 50, 20))):
        busca_por_imagem(i)
    ahk = AHK(executable_path='./ahk_folder/AutoHotkeyA32.exe')
    ahk.mouse_move(x=1149, y=649)
    ahk.click(direction='down')
    time.sleep(0.008)
    ahk.click(direction='up')
Example #2
0
def uppar():
    ahk = AHK(executable_path='./ahk_folder/AutoHotkeyA32.exe')
    while True:
        if re.sub('[^0-9]', '', cortar_e_ocr((930, 200, 50, 20))) != '600':
            ahk.mouse_move(1828, 791)
            ahk.click(direction='down')
        else:
            ahk.click(direction='up')
            break
Example #3
0
def busca_por_imagem(name_img):
    pos = imagesearch(f"./data/{name_img}.JPG")
    img = cv2.imread(f"./data/{name_img}.JPG")
    size = img.shape
    print(size)
    if pos[0] != -1:
        ahk = AHK(executable_path='./ahk_folder/AutoHotkeyA32.exe')
        ahk.mouse_move(x=pos[0] + (size[1] / 2), y=pos[1] + (size[0] / 2))
        ahk.click(direction='down')
        time.sleep(0.008)
        ahk.click(direction='up')
    else:
        busca_por_imagem(name_img)
Example #4
0
class Controller:
    def __init__(self):
        self.mouse = AHK()

        self.keyboard = None  #KeyboardController()

    def random_walk(self):
        offset_x = 3
        offset_y = 22

        center_x = (1024 + offset_x) / 2
        center_y = (768 + offset_y) / 2

        range = 150
        x = center_x + randint(-range, range)
        y = center_y + randint(-range, range)
        self.mouse.mouse_move(x=x, y=y, speed=3, blocking=True)
        time.sleep(0.5)
        self.mouse.click(x, y)
        time.sleep(0.5)

    def atacar(self, x, y):
        x += 25
        y += 25

        print("About to  attack")

        self.mouse.mouse_position = (x, y)
        time.sleep(0.2)
        self.mouse.click()
        time.sleep(5)

    def skill_F1(self):

        self.mouse.mouse_position = (235, 41)
        time.sleep(0.2)
        self.mouse.click()
        time.sleep(0.2)
        self.mouse.click()
Example #5
0
    [[2470, 1320], [2495, 1340]]
]

pc_type = input("Desktop or Laptop?")

if pc_type.upper() == "DESKTOP":
    inventory = desktop_inventory
elif pc_type.upper() == "LAPTOP":
    # Need to add pixels for laptop
    inventory = 'laptop inventory'


def get_random_coords(coords):
    random_x = randint(coords[0][0], coords[1][0])
    random_y = randint(coords[0][1], coords[1][1])
    return [random_x, random_y]


sleep(5)

for slot_coords in inventory:
    random_enchant_coords = get_random_coords(enchant_four)
    random_slot_coords = get_random_coords(slot_coords)

    ahk.mouse_move(random_enchant_coords[0], random_enchant_coords[1])
    ahk.click()
    sleep(uniform(0.2, 0.5))
    ahk.mouse_move(random_slot_coords[0], random_slot_coords[1])
    ahk.click()
    sleep(uniform(1.2, 1.5))
class WindowControl:
    def __init__(self, window_title: str) -> "WindowControl":
        self.autohotkey = AHK()
        self.window = self.autohotkey.find_window(title=window_title.encode("utf-8"))

        if not self.window:
            settings.LOGGER.error(f"Window {window_title} not found")

    def activate(self) -> None:
        """
        Activate window i.e. bring it to front.
        """
        if self.window:
            self.window.activate()

    def send_click(
        self,
        click_position_x_percentage_from_origin: Union[int, float],
        click_position_y_percentage_from_origin: Union[int, float],
    ) -> bool:
        """
        :param click_position_x_percentage_from_origin: X position as percentage from window origin that should be clicked
        :param click_position_y_percentage_from_origin: Y position as percentage from window origin that should be clicked
        :return: True if click was sent to the window
        """
        if self.window:
            self.activate()
            if (
                click_position_x_percentage_from_origin is not None
                and click_position_y_percentage_from_origin is not None
            ):
                click_x, click_y = self._click_window_position(
                    click_position_x_percentage_from_origin,
                    click_position_y_percentage_from_origin,
                )
            settings.LOGGER.warning(
                f"Clicked {click_x}, {click_y} of window {self.window.title}"
            )
            return True
        return False

    def send_drag(
        self,
        from_position_x_percentage_from_origin: Union[int, float],
        from_position_y_percentage_from_origin: Union[int, float],
        to_position_x_percentage_from_origin: Union[int, float],
        to_position_y_percentage_from_origin: Union[int, float],
    ) -> bool:
        """
        Drag mouse over window.

        :param from_position_x_percentage_from_origin: X position as percentage from window origin from where the mouse drag should start
        :param from_position_y_percentage_from_origin: Y position as percentage from window origin from where the mouse drag should start
        :param from_position_x_percentage_from_origin: X position as percentage from window origin to where the mouse drag should end
        :param from_position_y_percentage_from_origin: Y position as percentage from window origin to where the mouse drag should end
        :return: True if mouse drag was sent to the window
        """
        if self.window:
            self.activate()
            if (
                from_position_x_percentage_from_origin is not None
                and from_position_y_percentage_from_origin is not None
                and to_position_x_percentage_from_origin is not None
                and to_position_y_percentage_from_origin is not None
            ):
                from_x, from_y, to_x, to_y = self._drag_mouse_inside_window(
                    from_position_x_percentage_from_origin,
                    from_position_y_percentage_from_origin,
                    to_position_x_percentage_from_origin,
                    to_position_y_percentage_from_origin,
                )
                settings.LOGGER.warning(
                    f"Dragged mouse from {from_x}, {from_y} to {to_x}, {to_y} inside window {self.window.title}"
                )
            return True
        return False

    def send_key(
        self,
        command: str,
        click_position_x_percentage_from_origin: int,
        click_position_y_percentage_from_origin: int,
    ) -> bool:
        """
        Send given command (keys)

        :param command: keys to send
        :param click_position_x_percentage_from_origin: X position as percentage from window origin that should be clicked
        :param click_position_y_percentage_from_origin: Y position as percentage from window origin that should be clicked
        :return: True if keys were sent to the window
        """
        if self.window:
            self.activate()
            if (
                click_position_x_percentage_from_origin is not None
                and click_position_y_percentage_from_origin is not None
            ):
                self._click_window_position(
                    click_position_x_percentage_from_origin,
                    click_position_y_percentage_from_origin,
                )
            self._send_key(command)
            settings.LOGGER.warning(f"Sent {command} to window {self.window.title}")
            return True
        return False

    def _calculate_click_position(
        self,
        x: int,
        y: int,
        width: int,
        height: int,
        click_position_x_percentage_from_origin: Union[int, float] = 0,
        click_position_y_percentage_from_origin: Union[int, float] = 0,
    ) -> Tuple[Union[int, float], Union[int, float]]:
        """
        Calculate a screen position (x, y) that is going to be clicked.

        :param x: X position of window
        :param y: Y position of window
        :param width: Window width
        :param height: Window height
        :param click_position_x_percentage_from_origin: X position as percentage from window XY origin that should be clicked, defaults to 0
        :param click_position_y_percentage_from_origin: Y position as percentage from window XY origin that should be clicked, defaults to 0
        :return: x, y position in screen to click as tuple
        :raises ValueError: x or y position can't be converted to float or is nan
        """
        return (
            float_or_raise(x + width * click_position_x_percentage_from_origin / 100),
            float_or_raise(
                y + (height - height * click_position_y_percentage_from_origin / 100)
            ),
        )

    def _click_window_position(
        self,
        click_position_x_percentage_from_origin: Union[int, float] = 0,
        click_position_y_percentage_from_origin: Union[int, float] = 0,
    ) -> Tuple[Union[int, float], Union[int, float]]:
        """
        Click a position (x, y from origin) in the window.

        :param click_position_x_percentage_from_origin: X position as percentage from window XY origin that should be clicked, defaults to 0
        :param click_position_y_percentage_from_origin: Y position as percentage from window XY origin that should be clicked, defaults to 0
        :return: x, y position in screen that was clicked as tuple
        """
        click_x, click_y = self._calculate_click_position(
            *self.window.rect,
            click_position_x_percentage_from_origin,
            click_position_y_percentage_from_origin,
        )
        settings.LOGGER.warning(
            f"Moving mouse and clicking to {click_x}, {click_y} inside window {self.window.title}"
        )
        self.autohotkey.click(click_x, click_y, blocking=True)
        return (click_x, click_y)

    def _drag_mouse_inside_window(
        self,
        from_position_x_percentage_from_origin: Union[int, float],
        from_position_y_percentage_from_origin: Union[int, float],
        to_position_x_percentage_from_origin: Union[int, float],
        to_position_y_percentage_from_origin: Union[int, float],
    ) -> Tuple[
        Union[int, float], Union[int, float], Union[int, float], Union[int, float]
    ]:
        """
        Drag mouse over window.

        :param from_position_x_percentage_from_origin: X position as percentage from window origin from where the mouse drag should start
        :param from_position_y_percentage_from_origin: Y position as percentage from window origin from where the mouse drag should start
        :param from_position_x_percentage_from_origin: X position as percentage from window origin to where the mouse drag should end
        :param from_position_y_percentage_from_origin: Y position as percentage from window origin to where the mouse drag should end
        :return: drag position from x, y to x, y as tuple
        """
        rect = self.window.rect
        from_x, from_y = self._calculate_click_position(
            *rect,
            from_position_x_percentage_from_origin,
            from_position_y_percentage_from_origin,
        )
        to_x, to_y = self._calculate_click_position(
            *rect,
            to_position_x_percentage_from_origin,
            to_position_y_percentage_from_origin,
        )

        settings.LOGGER.warning(
            f"Dragging mouse from {from_x}, {from_y} to {to_x}, {to_y} inside window {self.window.title}"
        )
        self.autohotkey.mouse_drag(
            to_x, to_y, from_position=(from_x, from_y), blocking=True
        )
        return (from_x, from_y, to_x, to_y)

    def _send_key(self, command) -> None:
        """
        Send given keys to window.
        """
        self.window.send(command, blocking=True)
Example #7
0
class BoardAPI:
    """
    def __init__(self, point_top_right, point_top_left, point_bottom_right, point_bottom_left):
        # point format (x, y) tuple type
        top = point_top_right[1]
        bottom = point_bottom_right[1]
        left = point_top_left[0]
        right = point_top_right[0]

        self.point_top_right = point_top_right

        if (point_top_left[1] != top):
            point_top_left = (left, top)
        self.point_top_left = point_top_left

        if (point_bottom_right[0] != right ):
            point_bottom_right = (right, bottom)
        self.point_bottom_right = point_bottom_right

        if (point_bottom_left[0] != left or point_bottom_left[1] != bottom):
            point_bottom_left = (left, bottom)

        self.point_bottom_left = point_bottom_left
    """
    def __init__(self, point_top_right, length, playing_color):
        self.point_top_right = point_top_right
        self.point_top_left = (point_top_right[0] + length, point_top_right[1])
        self.point_bottom_right = (point_top_right[0],
                                   point_top_right[1] + length)
        self.point_bottom_left = (point_top_right[0] + length,
                                  point_top_right[1] + length)
        self.board = chess.Board()
        # to detect rival move
        self.previous_board_state = {
            'a8': ('black', 'R'),
            'b8': ('black', 'N'),
            'c8': ('black', 'B'),
            'd8': ('black', 'Q'),
            'e8': ('black', 'K'),
            'f8': ('black', 'B'),
            'g8': ('black', 'N'),
            'h8': ('black', 'R'),
            'a7': ('black', 'P'),
            'b7': ('black', 'P'),
            'c7': ('black', 'P'),
            'd7': ('black', 'P'),
            'e7': ('black', 'P'),
            'f7': ('black', 'P'),
            'g7': ('black', 'P'),
            'h7': ('black', 'P'),
            'a6': None,
            'b6': None,
            'c6': None,
            'd6': None,
            'e6': None,
            'f6': None,
            'g6': None,
            'h6': None,
            'a5': None,
            'b5': None,
            'c5': None,
            'd5': None,
            'e5': None,
            'f5': None,
            'g5': None,
            'h5': None,
            'a4': None,
            'b4': None,
            'c4': None,
            'd4': None,
            'e4': None,
            'f4': None,
            'g4': None,
            'h4': None,
            'a3': None,
            'b3': None,
            'c3': None,
            'd3': None,
            'e3': None,
            'f3': None,
            'g3': None,
            'h3': None,
            'a2': ('white', 'P'),
            'b2': ('white', 'P'),
            'c2': ('white', 'P'),
            'd2': ('white', 'P'),
            'e2': ('white', 'P'),
            'f2': ('white', 'P'),
            'g2': ('white', 'P'),
            'h2': ('white', 'P'),
            'a1': ('white', 'R'),
            'b1': ('white', 'N'),
            'c1': ('white', 'B'),
            'd1': ('white', 'Q'),
            'e1': ('white', 'K'),
            'f1': ('white', 'B'),
            'g1': ('white', 'N'),
            'h1': ('white', 'R')
        }

        self.playing_color = playing_color
        self.ahk = AHK()

    def init(self):
        self.previous_board_state = self.get_figures_pos()

    def get_rival_color(self):
        map_color = {"white": "black", "black": "white"}
        return map_color[self.playing_color]

    def get_A(self):
        return abs(self.point_bottom_right[1]) - abs(self.point_top_right[1])

    def get_B(self):
        return abs(self.point_top_left[0]) - abs(self.point_top_right[0])

    def get_C(self):
        return abs(self.point_bottom_left[1]) - abs(self.point_top_left[1])

    def get_D(self):
        return abs(self.point_bottom_left[0]) - abs(self.point_bottom_right[0])

    def save_image_of_board(self, filepath):
        # part of the screen
        im = ImageGrab.grab(bbox=(self.point_top_right[0],
                                  self.point_top_right[1],
                                  self.point_bottom_left[0],
                                  self.point_bottom_left[1]))  # X1,Y1,X2,Y2

        # save image file
        im.save(filepath)

    def _slice_images(self, filepath, dirpath):
        # white case

        board_coord_map_col = {
            1: "8",
            2: "7",
            3: "6",
            4: "5",
            5: "4",
            6: "3",
            7: "2",
            8: "1"
        }

        board_coord_map_row = {
            1: "a",
            2: "b",
            3: "c",
            4: "d",
            5: "e",
            6: "f",
            7: "g",
            8: "h"
        }

        # black case
        if (self.playing_color == "black"):
            board_coord_map_col = {
                1: "1",
                2: "2",
                3: "3",
                4: "4",
                5: "5",
                6: "6",
                7: "7",
                8: "8"
            }
            board_coord_map_row = {
                1: "h",
                2: "g",
                3: "f",
                4: "e",
                5: "d",
                6: "c",
                7: "b",
                8: "a"
            }

        tiles = slice(filepath, 64, 8, 8, False)
        result = []
        for tile in tiles:
            filepath = dirpath
            filename = f"{board_coord_map_row[tile.row]}{board_coord_map_col[tile.column]}.png"
            fullpath = os.path.join(filepath, filename)
            tile.save(fullpath)
            result.append(fullpath)
        return result

    def get_figures_pos(self, debug=False):
        """[summary]
            returns dict with figures and possitions
            possitions as keys and figures as value
        """
        result = {}
        with tempfile.TemporaryDirectory() as tmpdirname:
            filepath = os.path.join(tmpdirname, "board.png")
            self.save_image_of_board(filepath)
            tiles_pathes = self._slice_images(filepath, tmpdirname)
            for tile in tiles_pathes:
                figure = self.recognize_figure(tile)
                tile_location = Path(tile).stem
                result[tile_location] = figure
            if (debug):
                print(tmpdirname)
                input(f"press any button {__name__}")

        return result

    def _get_figures_pos(self, debug=True):
        result = {}
        tmpdirname = tempfile.mkdtemp()
        if True:
            filepath = os.path.join(tmpdirname, "board.png")
            self.save_image_of_board(filepath)
            tiles_pathes = self._slice_images(filepath, tmpdirname)
            for tile in tiles_pathes:
                figure = self.recognize_figure(tile)
                tile_location = Path(tile).stem
                result[tile_location] = figure
            if (debug):
                print(tmpdirname)
                input(f"press any button {__name__}")

        return result

    def get_board_state_fen(self, debug=False):
        return self.board.fen()

    def recognize_figure(self, filepath):
        # output format (1 or 2, "letter")
        result = None
        which_color = {
            "(248, 248, 248)": "white",  # whie color
            "(86, 83, 82)": "black"  # black color
        }
        approx_pixel_count_map = {
            "K": 2171,
            "Q": 1865,
            "R": 1507,
            "B": 1365,
            "N": 1677,
            "P": 978
        }

        coef_map = {
            "K": 4.15707,
            "Q": 4.91825,
            "R": 5.98871,
            "B": 6.61172,
            "N": 5.18163,
            "P": 9.22801
        }

        im = Image.open(filepath)

        pixels = list(im.getdata())
        width, height = im.size
        pixels = [pixels[i] for i in range(height * width)]

        unique_pixels = list(set(pixels))
        pixel_count = {}
        for pixel in unique_pixels:
            if (pixels.count(pixel) > 20):
                pixel_count[str(pixel)] = pixels.count(pixel)
        # create difference dict
        difference_dict = {}
        # find main color
        figure_color = None
        main_color_pixel = None
        for pixel in pixel_count:
            if pixel in which_color:
                figure_color = which_color[pixel]
                main_color_pixel = pixel

        if figure_color is not None:
            # get subtraction of main color
            for figure_letter in approx_pixel_count_map:
                # write into difference dict as differ as key and letter as value
                # amount_of_pixels = width * height
                # coefficient = amount_of_pixels / pixel_count[main_color_pixel]
                # difference = abs(coefficient - coef_map[figure_letter])
                # difference_dict[difference] = figure_letter
                # working
                difference = abs(pixel_count[main_color_pixel] -
                                 approx_pixel_count_map[figure_letter])
                difference_dict[difference] = figure_letter

            diff_list = list(difference_dict.keys())
            diff_list.sort()
            print(
                f"recognize_figure: filepath = {filepath} diffrenece dict = ")
            print(difference_dict)
            print(f"sorted values: {diff_list}")
            print()
            figure_letter = difference_dict[diff_list[0]]
            result = (figure_color, figure_letter)

        return result

    def _click_on_tile(self, tile_location: str):
        # white case
        letter_map = {
            "a": 0,
            "b": 1,
            "c": 2,
            "d": 3,
            "e": 4,
            "f": 5,
            "g": 6,
            "h": 7
        }
        number_map = {
            "8": 0,
            "7": 1,
            "6": 2,
            "5": 3,
            "4": 4,
            "3": 5,
            "2": 6,
            "1": 7
        }
        # black case
        if (self.playing_color == "black"):
            letter_map = {
                "a": 7,
                "b": 6,
                "c": 5,
                "d": 4,
                "e": 3,
                "f": 2,
                "g": 1,
                "h": 0
            }
            number_map = {
                "8": 7,
                "7": 6,
                "6": 5,
                "5": 4,
                "4": 3,
                "3": 2,
                "2": 1,
                "1": 0
            }

        length = self.get_A()

        length_of_tile = length / 8
        # init X Y values which will be added to point top right
        # set X to letter_sequence * width + (width/8/2)
        X = letter_map[tile_location[0]] * length_of_tile + (length_of_tile /
                                                             2)

        # set Y to number * height + (height/8/2)
        Y = number_map[tile_location[1]] * length_of_tile + (length_of_tile /
                                                             2)

        # add to point top rigth X and Y
        coords = (self.point_top_right[0] + X, self.point_top_right[1] + Y)
        print(X)
        print(Y)
        print(coords)
        # click with this coords on screen

        self.ahk.click(coords[0], coords[1])

        # get rid of cursor
        self.ahk.mouse_move(0, 0)

    def move_figure_from_to(self, coords: str):
        """[summary]

        Args:
            coords (str): e5e1 this is an example of coords 
        """
        print(f" move_figure_from_to coords is {coords}")
        print(f"before:\n{self.board}")

        self._click_on_tile(coords[:2])
        self._click_on_tile(coords[2:])

        self.board.push_uci(coords)
        # TODO optimize this shit, if it s needed due to optimazation problem with this method
        self.previous_board_state = self.get_figures_pos()

        print(f"after:\n{self.board}")

    def update_board_after_rival_move(self):
        print("update_board_after_rival_move")
        print(f"before:\n{self.board}")
        moveuci = self.get_rivals_move()
        self.board.push_uci(moveuci)
        self.previous_board_state = self.get_figures_pos()
        print(f"after:\n{self.board}")

    def is_my_turn(self):
        turn_dict = {chess.BLACK: "black", chess.WHITE: "white"}

        return turn_dict[self.board.turn] == self.playing_color

    def get_rivals_move(self):
        # get new board state
        new_board_state = self.get_figures_pos()

        # check if it s catstling
        castling_tiles_map = {
            "white": {
                'K': ['e1', 'g1'],
                'Q': ['c1', 'd1']
            },
            "black": {
                'K': ['e8', 'g8'],
                'Q': ['c8', 'd8']
            }
        }

        # check castling rivals tiles from King side
        print("get_rival_move: check for castling k side")

        k_side_castle = castling_tiles_map[self.get_rival_color()]['K']
        if new_board_state[
                k_side_castle[1]] != None and self.previous_board_state[
                    k_side_castle[0]] != None:
            first_check = new_board_state[k_side_castle[1]][
                1] == 'K' and new_board_state[k_side_castle[0]] == None
            second_check = self.previous_board_state[
                k_side_castle[1]] == None and self.previous_board_state[
                    k_side_castle[0]][1] == 'K'
            if first_check and second_check:
                return ''.join(k_side_castle)
        print("nope this is not the case")
        # check castling rivals tiles from Queen side
        print("get_rival_move: check for castling q side")
        q_side_castle = castling_tiles_map[self.get_rival_color()]['Q']
        if new_board_state[q_side_castle[0]] != None and new_board_state[
                q_side_castle[1]] != None:
            first_check = new_board_state[q_side_castle[0]][
                1] == 'K' and new_board_state[q_side_castle[1]][1] == 'R'
            second_check = self.previous_board_state[q_side_castle[
                0]] == None and self.previous_board_state[q_side_castle[1]]
            if first_check and second_check:
                return ''.join(q_side_castle)
        print("this is also not the case")

        # else go ahead

        # check for en passant
        # get en passant fen str from board
        en_passant = self.board.fen().split(" ")[3]
        if en_passant != '-':
            # if on that tile is a pawn, then check where is was
            positions = list(new_board_state.keys())
            prev_pos = None
            # get position, comparing new and old states, of empty tile
            for position in positions:
                # where no figure there is coords for first part
                existing_check = new_board_state[position] == None
                generic_check = new_board_state[
                    position] != self.previous_board_state[position]
                if generic_check and existing_check and en_passant[
                        0] not in position:
                    prev_pos = position
                    logging.debug(
                        f"Comparing En passant new board state[pos] = {new_board_state[position]} prev pos = {self.previous_board_state[position]}, pos appended = {position} "
                    )
            return position + prev_pos
        # concate en passant str with pawn prev pos
        # return this
        # else go ahead

        # compare every single position on board with new board state
        positions = list(new_board_state.keys())
        changed_pos = []

        # get position, comparing new and old states, of empty tile
        for position in positions:
            # where no figure there is coords for first part
            existing_check = new_board_state[position] == None
            generic_check = new_board_state[
                position] != self.previous_board_state[position]
            if generic_check and existing_check:
                changed_pos.append(position)
                logging.debug(
                    f"Comparing None new board state[pos] = {new_board_state[position]} prev pos = {self.previous_board_state[position]}, pos appended = {position} "
                )
        # get figure that moved and new pos of this figure
        for position in positions:
            # where figure appeared there is coords for second part
            if (new_board_state[position] != None):
                color_check = new_board_state[position][
                    0] == self.get_rival_color()
                generic_check = new_board_state[
                    position] != self.previous_board_state[position]
                if generic_check and color_check:
                    changed_pos.append(position)
                    logging.debug(
                        f"Comparing figure new board state[pos] = {new_board_state[position]} prev pos = {self.previous_board_state[position]}, pos appended = {position} "
                    )

        # get two difference positions of rivals figures
        move = ''.join(changed_pos)
        if (move == ''):
            move = None
        # promotion in chess, if pawn manage to get to the other side of a board
        # TODO there some bug where Knight recognized as Queen in some cases
        # have no idea how to fix that so I jsut bodge this
        if self.previous_board_state[changed_pos[0]][1] != new_board_state[
                changed_pos[1]][1] and self.previous_board_state[
                    changed_pos[0]][1] == 'P':
            print(
                f"promotion check: prev on '{changed_pos[0]}' = {self.previous_board_state[changed_pos[0]]}, new on '{changed_pos[1]}' = {new_board_state[changed_pos[1]]}"
            )
            print(self.previous_board_state)
            print(new_board_state)
            move += new_board_state[changed_pos[1]][1].lower()
        return move
Example #8
0
resetClickLoc = ()

while True:
    resetClickLoc = ahk.image_search(
        image_path='ReferenceImages\\reference.png',
        color_variation=70,
        upper_bound=[winX, winY],
        lower_bound=[winX2, winY2])
    if resetClickLoc is not None:
        break

resetClickLoc = ahk.image_search(image_path='ReferenceImages\\reference.png',
                                 color_variation=70,
                                 upper_bound=[winX, winY],
                                 lower_bound=[winX2, winY2])
ahk.click(resetClickLoc[0], resetClickLoc[1])

# Pause to make sure the computer has time to rescan the login screen after mouse is moved off the username button
time.sleep(0.1)

userLoc = ahk.image_search(image_path='ReferenceImages\\usernameBox.jpg',
                           color_variation=70,
                           upper_bound=[winX, winY],
                           lower_bound=[winX2, winY2])
ahk.click(userLoc[0] + 150, userLoc[1] + 30)
# ahk.type() doesn't work for equal signs
ag.write(userName)

passLoc = ahk.image_search(image_path='ReferenceImages\\passwordBox.jpg',
                           color_variation=70,
                           upper_bound=[winX, winY],
Example #9
0
state = np.array([None] * 22)

#dqn_agent = DQN()
with open('model_pickle', 'rb') as f:
    dqn_agent = pickle.load(f)
new_state = np.reshape([None] * 22, [1, 22])
empty_state = np.reshape([None] * 22, [1, 22])
#win.close()
time.sleep(1)
#print(ahk.mouse_position)
#(1058, 153)#select game.
#(977, 94) play button
#time.sleep(1000)
while (True):
    #ahk.key_press('.') #advance frame
    ahk.click(357, 749)  #open dolphin
    time.sleep(3)

    #win = ahk.find_window(title=b'Dolphin 5.0-10222')

    time.sleep(5)
    #win.move(x=865, y=0, width=500, height=500)
    time.sleep(1)
    #(1058, 153)#select game.
    #(1015, 94) play button
    ahk.click(1058, 153)
    time.sleep(1)

    ahk.click(1015, 94)
    time.sleep(15)
    ahk.key_press('.')
Example #10
0
# Keys

coord0 = (907, 401)
coord1 = (1016, 399)
coord2 = (1098, 488)
coord3 = (1082, 594)
coord4 = (1016, 675)
coord5 = (895, 686)
coord6 = (837, 590)
coord7 = (838, 482)

center = (960, 540)

coords = [coord0, coord1, coord2, coord3, coord4, coord5, coord6, coord7]

index = -1

## Experiment ##

# Calibration
time.sleep(10)
ahk.key_press('space')  # Press and release a key

while index < 345:
    index = index + 1
    resp = random.randint(0, 7)
    time.sleep(1.2)
    ahk.click(coords[resp][0], coords[resp][1])
    time.sleep(0.5)
    ahk.click(center[0], center[1])
Example #11
0
			os.makedirs(newpath)
		else:
			continue
		'''
        cur_dir = 18
        region_dbg.clear()

        screenshot = ImageGrab.grab(bbox=(517, 110, 1418, 814))
        # to file
        #screenshot.save('test_images/log/{0}/000.png'.format(cur_dir))

        # step-0: AHK执行,获取屏幕
        #ahk.double_click(1205, 423)  #+价输入框
        #输入自定义数值+400
        #ahk.send_input('400')
        ahk.click(1327, 421)
        #点击按钮弹出对话框
        ahk.click(1325, 534)

        #time.sleep(0.4)

        p0_x = 950
        p0_y = 358
        p1_x = 1370
        p1_y = 640
        screenshot = ImageGrab.grab(bbox=(p0_x, p0_y, p1_x, p1_y))
        # to file
        #screenshot.save('test_images/log/{0}/001.png'.format(cur_dir))

        started = time.time()
class InputAutomator(webdriver.Ie):

    options = webdriver.IeOptions()
    user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko'
    options.add_argument(f'user-agent={user_agent}')
    options.add_argument("IgnoreZoomLevel=true")

    def __init__(self, *args, **kwargs):
        kwargs['executable_path'] = 'IEDriverServer.exe'
        kwargs['ie_options'] = self.options
        super().__init__(*args, **kwargs)
        self.maximize_window()
        self.ahk = AHK(executable_path="AutoHotkeyU64.exe")

    def adjust_coordinates(self, x, y):
        """
        Adjusts document coordinates given by selenium to screen coordinates used by AHK.
        :param x: x coordinate (pixels) in document
        :param y: y coordinate (pixels) in document
        :return: (x_adj, y_adj)
        """
        window_pos = self.get_window_position()
        browser_navigation_panel_height = self.execute_script(
            'return window.outerHeight - window.innerHeight;')
        return x + window_pos['x'], y + window_pos[
            'y'] + browser_navigation_panel_height

    def move_rand(self, elem_type, elem_text, x_offset=0, y_offset=0):
        """
        Moves mouse to a given element using a random walk path. Does not seem to make a difference.
        :param elem_type: one of "class", "css" or "id"
        :param elem_text: value of "class", "css" or "id"
        :param x_offset: x offset from element coordinates for final mouse position
        :param y_offset: y offset from element coordinates for final mouse position
        """
        try:
            if elem_type == "class":
                out = self.find_element_by_class_name(elem_text).location
            elif elem_type == "css":
                out = self.find_element_by_css_selector(elem_text).location
            elif elem_type == "id":
                out = self.find_element_by_class_name(elem_text).location
            else:
                raise ValueError("Unknown elem_type: must be class, css or id")
        except (NoSuchElementException, TimeoutException, JavascriptException):
            return False
        x_final, y_final = self.adjust_coordinates(out['x'] + x_offset,
                                                   out['y'] + y_offset)
        x0, y0 = self.ahk.mouse_position
        x_path, y_path = constrained_walk_2d((x0, y0), (x_final, y_final))
        # Reduce points
        x_path = x_path[:1] + x_path[::max([len(x_path) //
                                            50, 1])] + x_path[-1:]
        y_path = y_path[:1] + y_path[::max([len(y_path) //
                                            50, 1])] + y_path[-1:]
        for x, y in zip(x_path, y_path):
            self.ahk.run_script(f"SetMouseDelay, -1\nMouseMove, {x}, {y} , 0")
            # self.ahk.mouse_move(x=x, y=y, blocking=True, speed=0)

    def move_to(self, elem_type, elem_text, x_offset=0, y_offset=0):
        self.maximize_window()
        """
        Moves mouse to a given element directly. Passes reCAPTCHA.
        :param elem_type: one of "class", "css" or "id"
        :param elem_text: value of "class", "css" or "id"
        :param x_offset: x offset from element coordinates for final mouse position
        :param y_offset: y offset from element coordinates for final mouse position
        """
        try:
            if elem_type == "class":
                out = self.find_element_by_class_name(elem_text).location
            elif elem_type == "css":
                out = self.find_element_by_css_selector(elem_text).location
            elif elem_type == "id":
                out = self.find_element_by_class_name(elem_text).location
            else:
                raise ValueError("Unknown elem_type: must be class, css or id")
            self.ahk.mouse_move(*self.adjust_coordinates(
                out['x'] + x_offset, out['y'] + y_offset),
                                speed=10,
                                blocking=True)
            return True
        except (NoSuchElementException, TimeoutException, JavascriptException):
            return False

    def click(self):
        self.ahk.click()

    def type(self, text):
        self.ahk.send_input(text)

    def scroll(self, direction, times):
        [self.ahk.mouse_wheel(direction) for i in range(times)]

    def wait_for(self, elem_type, elem_text, timeout=15):
        result = False
        start = time()
        while not result and time() - start < timeout:
            try:
                if elem_type == "class":
                    result = self.find_element_by_class_name(
                        elem_text).is_displayed()
                elif elem_type == "css":
                    result = self.find_element_by_css_selector(
                        elem_text).is_displayed()
                elif elem_type == "id":
                    result = self.find_element_by_class_name(
                        elem_text).is_displayed()
                return result
            except (NoSuchElementException, TimeoutException,
                    JavascriptException):
                result = False
            sleep(0.1)
        if not result:
            print(f"Warn: Element {elem_text} not found after {timeout} sec")
            return result
Example #13
0
# Keys

coord0 = (907, 401)
coord1 = (1016, 399)
coord2 = (1098, 488)
coord3 = (1082, 594)
coord4 = (1016, 675)
coord5 = (895, 686)
coord6 = (837, 590)
coord7 = (838, 482)

center = (960, 540)

coords = [coord0, coord1, coord2, coord3, coord4, coord5, coord6, coord7]

ahk.click(907, 401)  # Blocks until mouse finishes moving (the default)

index = -1

## Experiment ##

# Calibration
time.sleep(10)
ahk.key_press('space')  # Press and release a key

while index < 10:
    index = index + 1
    time.sleep(1)
    resp = random.randint(0, 7)
    ahk.click(coords[resp][0], coords[resp][1])
    time.sleep(5)