Example #1
0
    def continuously_find(window, target, similarity=0.8):
        """Method for finding all mathes of an image in the specified window
        with at least the specified similarity continuously. The user must
        manually exit out the script to halt the matching.

        Args:
            window (str): name of window to search in
            target (str): name of image to find in the window
            similarity (float, optional): minimum similarity threshold for the
                match
        """
        target_window = App(window)
        target_region = target_window.focus().focusedWindow()
        while True:
            print("+ {}:".format(strftime("%H:%M:%S")))
            print(
                "+  Sikuli match object for '{}' in window '{}'".format(
                    target, window))
            print("+    with minimum similarity of {}:".format(similarity))

            debug_matches = Util.findAll_wrapper(
                target_region, Pattern(target).similar(similarity))

            for img_match in debug_matches:
                print(img_match)
                target_region.mouseMove(img_match)
            if isinstance(debug_matches, list) and len(debug_matches) == 0:
                print("No matches!")
            print("")
Example #2
0
    def find(window, target, similarity):
        """Method for finding all matches of an image in the specified window
        with at least the specified similarity once.

        Args:
            window (str): name of window to search in
            target (str): name of image to find in the window
            similarity (float, optional): minimum similarity threshold for the
                match
        """
        target_window = App(window)
        target_region = target_window.focus().focusedWindow()
        print("\n")
        print(
            "+  Sikuli match object for '{}' in window '{}'".format(
                target, window))
        print("+    with minimum similarity of {}:".format(similarity))

        debug_matches = Util.findAll_wrapper(
            target_region, Pattern(target).similar(similarity))

        for img_match in debug_matches:
            img_match.highlight()
            print(img_match)
            target_region.mouseMove(img_match)
        if isinstance(debug_matches, list) and len(debug_matches) == 0:
            print("No matches!")
        print("\n")
Example #3
0
    def focus_app(config):
        """Method to focus the specified game window.

        Args:
            config (Config): kcauto-kai Config instance

        Returns:
            Region: region returned by sikuli's focusedWindow method
        """
        kc = App.focus(config.program).focusedWindow()
        kc.setAutoWaitTimeout(1)
        return kc
Example #4
0
    def recover(kcauto_kai, e):
        """Attempts very basic recovery actions on a FindFailed exception. WIP
        and does not integrate with the config.

        Args:
            kcauto_kai (KCAutoKai): KCAutoKai instance
            e (Exception): Exception

        Returns:
            bool: True on successful recovery, otherwise raises an error
        """
        kc_region = kcauto_kai.kc_region
        regions = kcauto_kai.regions
        recovery_method = 'kc3'

        Util.log_warning(
            "FindFailed error occurred; attempting basic recovery.")

        App.focus(kcauto_kai.config.program)
        kc_region.mouseMove(Location(1, 1))

        # basic recovery attempt
        type(Key.ESC)
        sleep(1)
        if kc_region.exists(Pattern('kc_reference_point.png').exact()):
            # reference point exists, so we are in-game
            Util.log_success("Recovery successful.")
            kcauto_kai.stats.increment_recoveries()
            return True
        elif kc_region.exists('next.png'):
            # crashed at some results screen; try to click it away until we see
            # the main game screen
            while (kc_region.exists('next.png') and not kc_region.exists(
                    Pattern('kc_reference_point.png').exact())):
                Util.click_screen(regions, 'center')
                sleep(2)
            if kc_region.exists(Pattern('kc_reference_point.png').exact()):
                # reference point exists, so we are back in-game
                Util.log_success("Recovery successful.")
                kcauto_kai.stats.increment_recoveries()
                return True

        # catbomb recovery
        if kc_region.exists('catbomb.png') and recovery_method != 'None':
            if recovery_method == 'browser':
                Region.type(Key.F5)
            elif recovery_method == 'kc3':
                Region.type(Key.F5)
                sleep(1)
                Region.type(Key.SPACE)
                sleep(1)
                Region.type(Key.TAB)
                sleep(1)
                Region.type(Key.SPACE)
            elif recovery_method == 'kcv':
                Region.type(Key.F5)
            elif recovery_method == 'kct':
                Region.type(Key.ALT)
                sleep(1)
                Region.type(Key.DOWN)
                sleep(1)
                Region.type(Key.DOWN)
                sleep(1)
                Region.type(Key.ENTER)
            elif recovery_method == 'eo':
                Region.type(Key.F5)
                sleep(1)
                Region.type(Key.TAB)
                sleep(1)
                Region.type(Key.SPACE)
            sleep(3)
            kc_region.mouseMove(Location(0, 0))
            sleep(3)
            Util.wait_and_click(kc_region,
                                Pattern('game_start.png').similar(0.999), 60)
            sleep(5)
            Util.log_success("Recovery successful.")
            kcauto_kai.stats.increment_recoveries()
            return True

        # recovery failed
        Util.log_error("Irrecoverable crash")
        print(e)
        raise
Example #5
0
    def recover(kcauto_kai, config, e):
        """Attempts very basic recovery actions on a FindFailed exception. WIP
        and does not integrate with the config.

        Args:
            kcauto_kai (KCAutoKai): KCAutoKai instance
            config (Config): Config instance
            e (Exception): Exception

        Returns:
            bool: True on successful recovery, otherwise raises an error
        """
        kc_region = (kcauto_kai.kc_region
                     if kcauto_kai.kc_region else Util.focus_kc(config))
        kc_region = kcauto_kai.kc_region
        regions = kcauto_kai.regions

        Util.log_warning(e)
        Util.log_warning(
            "** FindFailed error occurred; attempting basic recovery. **")

        App.focus(kcauto_kai.config.program)
        kc_region.mouseMove(Location(1, 1))

        # basic recovery attempt
        Region.type(kc_region, Key.ESC)
        sleep(1)
        Region.type(kc_region, Key.SPACE)
        if kc_region.exists(Pattern('kc_reference_point.png').exact()):
            # reference point exists, so we are in-game
            Util.log_success("Recovery successful.")
            kcauto_kai.stats.increment_recoveries()
            return True
        elif kc_region.exists('next.png'):
            # crashed at some results screen; try to click it away until we see
            # the main game screen
            while (kc_region.exists('next.png') and not kc_region.exists(
                    Pattern('kc_reference_point.png').exact())):
                Util.click_preset_region(regions, 'center')
                sleep(2)
            if kc_region.exists(Pattern('kc_reference_point.png').exact()):
                # reference point exists, so we are back in-game
                Util.log_success("Recovery successful.")
                kcauto_kai.stats.increment_recoveries()
                return True

        # catbomb recovery
        if kc_region.exists('catbomb.png', 10):
            Util.log_warning("** Catbomb detected. **")
            catbombed = True
            catbomb_n = 0
            while catbombed and catbomb_n < 7:
                # generic f5-space-tab-space keystrokes to mimick refresh
                # attempt
                Region.type(kc_region, Key.F5)
                sleep(1)
                Region.type(kc_region, Key.SPACE)
                sleep(1)
                Region.type(kc_region, Key.TAB)
                sleep(1)
                Region.type(kc_region, Key.SPACE)
                sleep(3)
                # clear mouse
                kc_region.mouseMove(Location(1, 1))
                if kc_region.exists('catbomb.png'):
                    sleep_len = pow(2, catbomb_n + 4)
                    Util.log_warning(
                        "Catbomb recovery attempt {} failed; trying again in "
                        "{} seconds!".format(catbomb_n + 1, sleep_len))
                    sleep(sleep_len)
                    catbomb_n += 1
                else:
                    catbombed = False
            sleep(3)
            Util.wait_and_click(kc_region,
                                Pattern('game_start.png').similar(0.999), 60)
            sleep(5)
            Util.log_success("Catbomb recovery successful.")
            kcauto_kai.stats.increment_recoveries()
            return True

        # recovery failed
        Util.log_error("** Irrecoverable crash. **")
        print(e)
        raise