def swipe_handler(self, from_x, from_y, to_x, to_y, millisecond): from_x = int(from_x / self.dpi) if from_x > 0 else 0 from_y = int(from_y / self.dpi) if from_y > 0 else 0 to_x = int(to_x / self.dpi) if to_x > 0 else 0 to_y = int(to_y / self.dpi) if to_y > 0 else 0 duration = millisecond / 1000 # wda accept duration with 'second' timeunit logger.debug('actually swipe from ({0}, {1}) to ({2}, {3})'.format( from_x, from_y, to_x, to_y)) self.session.swipe(from_x, from_y, to_x, to_y, duration)
def program_exit_alert(cause): if cfg.server_chan_enable: secret = cfg.server_chan_secret params = { "title": "复读机复读结束通知", "desp": "游戏:{0}\n\n关卡:{1}\n\n结束原因:{2}\n\n" \ .format(cfg.game_name, cfg.battle_no, cause) } resp = requests.get(url.format(secret, params['title'], params['desp'])) json_string = bytes.decode(resp.content) logger.debug(json_string)
def get_similarity(template, screen, threshold=0.8): res = cv2.matchTemplate(screen, template, cv2.TM_CCOEFF_NORMED) loc = np.where(res >= threshold) possible_targets = [] w, h = template.shape[::-1] for possible_target in zip(*loc[::-1]): x, y = possible_target[0] + w / 2, possible_target[1] + h / 2 if len(possible_targets) > 0: last = possible_targets[len(possible_targets) - 1] if x - last[0] > 30: # 误差在30内视为同一个区域 possible_targets.append((x, y)) else: possible_targets.append((x, y)) logger.debug(possible_targets) return possible_targets
def __init__(self, name, identify_image, action_type="tap", tap_image=None, tap_offset_x=0, tap_offset_y=0, threshold=0.8): self.name = name self.identify_image = identify_image self.type = action_type self.tap_image = tap_image self.tap_offset_x = tap_offset_x self.tap_offset_y = tap_offset_y self.threshold = threshold logger.debug("scene register: <{0}>".format(self.name)) self.__check()
def recognize_and_process_page(self): screen = self.device.screen_capture_handler() if screen is None: return # 场景匹配 matched = None for scene in self.scenes: possible_targets = scene.matched_in(screen) if len(possible_targets) > 0: matched = scene logger.debug('match scene {}'.format(matched.name)) break if matched is None: logger.debug('当前屏幕无法识别出任何已知匹配的场景') return # 前置处理 if matched.before_action is not None: matched.before_action(self.device, screen) if matched.perform_what() == 'tap': # 需要点击 x, y = matched.where_to_tap(screen) logger.debug('calculate tap position: {0}, {1}'.format(x, y)) self.device.tap_handler(x, y) elif matched.perform_what() == 'swipe': # 需要手势滑动 from_x, from_y, to_x, to_y = matched.how_to_swipe() self.device.swipe_handler(from_x, from_y, to_x, to_y, 500) # 后置处理 if matched.after_action is not None: matched.after_action(self.device, screen)
def calculate_move_map(context, config): width, height = context.screen_width, context.screen_height center_x, center_y = width / 2, height / 2 # 屏幕中心坐标 horizontal_unit_distance = width / 3 # 水平方向每次移动距离 vertical_unit_distance = height / 3 # 垂直方向每次移动距离 direction = context.swipe_mode opposite_direction = (config.default_swipe_direction + 2) % 4 # 与初始方向相反的方向值 if direction == 0: logger.debug('↑') # 手势从上往下,查看上侧区域 to_x = center_x to_y = center_y + vertical_unit_distance if opposite_direction == direction or opposite_direction + 1 == direction: to_y += vertical_unit_distance context.swipe_mode = 1 # 下次向右滑动 elif direction == 1: logger.debug('→') # 手势从右往左,查看右侧区域 to_x = center_x - horizontal_unit_distance if opposite_direction == direction or opposite_direction + 1 == direction: to_x -= horizontal_unit_distance to_y = center_y context.swipe_mode = 2 # 下次向下滑动 elif direction == 2: logger.debug('↓') # 手势从下往上,查看下侧区域 to_x = center_x to_y = center_y - vertical_unit_distance if opposite_direction == direction or opposite_direction + 1 == direction: to_y -= vertical_unit_distance context.swipe_mode = 3 # 下次向左滑动 elif direction == 3: logger.debug('←') # 手势从左往右,查看左侧区域 to_x = center_x + horizontal_unit_distance if opposite_direction == direction or opposite_direction + 1 == direction: to_x += horizontal_unit_distance to_y = center_y context.swipe_mode = 0 # 下次向上滑动 else: raise ValueError('移动方向值不存在:%s' % direction) return center_x, center_y, to_x, to_y
def tap_handler(self, pos_x, pos_y): logger.debug('actually tap position: {0}, {1}'.format(pos_x, pos_y)) self.adb.run('shell input tap {} {}'.format(pos_x, pos_y))
def tap_handler(self, pos_x, pos_y): x = pos_x / self.dpi y = pos_y / self.dpi logger.debug('actually tap position: {0}, {1}'.format(x, y)) self.session.tap(x, y)