def get_ports(emulator): ps = get_processes(emulator) pcr_log('admin').write_log(level='debug', message=_("{name}({type}) processes:").format( name=emulator.name, type=emulator.type)) pcr_log('admin').write_log(level='debug', message=ps) # print( # _("{name}({type}) processes:").format( # name=emulator.name, type=emulator.type)) print(ps) ports = [] if 1 <= len(ps) <= len(emulator.default_ports): for default_port in emulator.default_ports: result = check_adb_connectable_by_port( default_port, auto_disconnect=False) if result: ports.append(default_port) if ports: return ports for p in ps: connections = [ x.laddr for x in p.connections() if x.status == psutil.CONN_LISTEN ] # print(connections) if emulator.re_port: ports += [ int(x.port) for x in connections if re.match(emulator.re_port, str(x.port)) and x.port > 2000 ] else: ports += [int(x.port) for x in connections if x.port > 2000] ports = check_adb_connectable_by_ports(ports) return ports
def img_similar(cls, screen_short, threshold=0.84, at=None, method=cv2.TM_CCOEFF_NORMED): """ 和上次截图匹配相似度 :param threshold: :param screen_short: :param at: 缩小查找范围 :return: """ if screen_short.shape[0] > screen_short.shape[1]: screen_short = UIMatcher.RotateClockWise90(screen_short) if at is not None: try: x1, y1, x2, y2 = at screen_short = screen_short[y1:y2, x1:x2] except: pcr_log('admin').write_log(level='debug', message="检测区域填写错误") exit(-1) else: x1, y1 = 0, 0 if cls.screen_short_befor is None: cls.screen_short_befor = screen_short # print('填空') th, tw = screen_short.shape[:2] # rows->h, cols->w res = UIMatcher.matchTemplate(cls.screen_short_befor, screen_short, method) min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) # print(max_val) if max_val >= threshold: x = x1 + max_loc[0] + tw // 2 y = y1 + max_loc[1] + th // 2 cls.screen_short_befor = screen_short # print(round(max_val, 3)) return round(max_val, 3)
def get(self, key: str, default: Optional[dict] = None) -> dict: """ 获取某一个分区对应的记录 :param key: 分区名称 :param default: 默认值,如果获取的记录不存在,则以default创建该记录 :return: 该分区的dict """ target_name = "%s/%s/%s.json" % (user_addr, key, self.account) dir = os.path.dirname(target_name) if default is not None and (not os.path.isdir(dir) or not os.path.exists(target_name)): self.json_save(target_name, default) now = self.load(target_name) if now is None: self.json_save(target_name, default) return default flag = False # 检查缺失值,用默认值填充 for k, v in default.items(): if k not in now: now[k] = v flag = True if flag: self.json_save(target_name, now) if debug: pcr_log("AR").write_log("debug", f"Key:{key},AR Get:{now}") return now
def rename(self, name, auto_id): # 重命名 # 2021/1/4 CyiceK对代码进行了维护 name = name.split(' ') name_len = len(name) if auto_id: name = name[random.randint(0, name_len - 1)] + str( random.randint(0, 1000)) else: name = name[random.randint(0, name_len - 1)] self.click(871, 513) # 主菜单 self.lock_img('img/zhucaidan/bangzhu.bmp', ifclick=[(370, 250)]) # 锁定帮助 点击简介 self.lock_img('img/bianji.bmp', ifclick=[(900, 155)]) # 锁定 点击铅笔修改按钮 self.lock_img('img/biangeng.bmp', ifclick=[(480, 270)]) # 锁定 玩家名 点击游戏渲染编辑框 time.sleep(1) self.click(290, 425) # 点击编辑框 self.d.clear_text() self.d.send_keys(name) self.click(880, 425) # 点击确定 time.sleep(0.5) self.click(590, 370) # 变更按钮 time.sleep(1) self.lock_img('img/zhucaidan/bangzhu.bmp', elseclick=[(32, 32)]) # 锁定帮助 pcr_log(self.account).write_log(level='info', message='账号:%s已修改名字' % name)
def ocr_local(self, x1, y1, x2, y2, screen_shot=None, size=1.0): if screen_shot is None: screen_shot = self.getscreen() try: if screen_shot.shape[0] > screen_shot.shape[1]: if anticlockwise_rotation_times >= 1: for _ in range(anticlockwise_rotation_times): screen_shot = UIMatcher.AutoRotateClockWise90( screen_shot) screen_shot = UIMatcher.AutoRotateClockWise90(screen_shot) part = screen_shot[y1:y2, x1:x2] # 对角线点坐标 part = cv2.resize(part, None, fx=size, fy=size, interpolation=cv2.INTER_LINEAR) # 利用resize调整图片大小 img_binary = cv2.imencode('.png', part)[1].tobytes() files = {'file': ('tmp.png', img_binary, 'image/png')} local_ocr_text = requests.post( url="http://127.0.0.1:5000/ocr/local_ocr/", files=files) pcr_log(self.account).write_log(level='info', message='本地OCR识别结果:%s' % local_ocr_text.text) return local_ocr_text.text except Exception as ocr_error: pcr_log(self.account).write_log(level='error', message='本地OCR识别失败,原因:%s' % ocr_error) return -1
def execute(account_filename, tasks, acc_filter=None): """ 执行脚本 """ # 连接adb与uiautomator devices = connect() # 读取账号 account_dic, opcode_dic = read_account(account_filename) # 过滤账号 if acc_filter is not None: account_dic = acc_filter(account_dic, opcode_dic) # 这个队列用来保存设备, 初始化的时候先把所有的模拟器设备放入队列 queue = Manager().Queue() # 进程池参数列表 params = list() for account, password in account_dic.items(): opcode = opcode_dic[account] params.append((account, password, queue, tasks, opcode)) # 初始化队列, 先把所有的模拟器设备放入队列 for device in devices: queue.put(device) # 进程池大小为模拟器数量, 保证同一时间最多有模拟器数量个进程在运行 with Pool(len(devices)) as mp: mp.map(runmain, params) # 退出adb os.system('cd adb & adb kill-server') pcr_log('admin').write_log(level='info', message='任务全部完成') pcr_log('admin').server_bot('', message='任务全部完成')
def img_prob(cls, screen, template_path, at=None, method=cv2.TM_CCOEFF_NORMED) -> float: """ 在screen里寻找template,若找到多个,则返回第一个的匹配度。 :param screen: 截图图片 :param template_path: 文件路径 :param at: 缩小查找范围 :return: 匹配度 """ if screen.shape[0] > screen.shape[1]: screen = UIMatcher.RotateClockWise90(screen) if at is not None: try: x1, y1, x2, y2 = at screen = screen[y1:y2 + 1, x1:x2 + 1] except: pcr_log('admin').write_log(level='debug', message="检测区域填写错误") exit(-1) if screen.mean() < 1: # 纯黑色与任何图相关度为1 return 0 template = cls._get_template(template_path) prob = UIMatcher.matchTemplate(screen, template, method).max() return prob
def shuatu12(self): # 进入冒险 self.click(480, 505, pre_delay=2, post_delay=2) while True: screen_shot_ = self.d.screenshot(format="opencv") if UIMatcher.img_where(screen_shot_, 'img/dixiacheng.jpg'): break self.click( 562, 253, ) self.click(701, 83, pre_delay=4, post_delay=2) self.duanyazuobiao() if self.tag < 22: # 暂时先按各11次来判定 for _ in range(5): # 以7图为基向右移动5图 self.click(925, 275, post_delay=3) while True: screen_shot_ = self.d.screenshot(format="opencv") if UIMatcher.img_where(screen_shot_, 'img/normal.jpg', at=(660, 72, 743, 94)): break if UIMatcher.img_where(screen_shot_, 'img/hard.jpg'): self.click(701, 83, pre_delay=0, post_delay=2) break self.switch = 0 self.d.drag(600, 270, 200, 270, 0.1) # 拖拽到最右 time.sleep(2) self.shuatuzuobiao(760, 255, self.times) # 12-17 self.shuatuzuobiao(610, 245, self.times) # 12-16 self.shuatuzuobiao(450, 270, self.times) # 12-15 self.shuatuzuobiao(565, 415, self.times) # 12-14 self.shuatuzuobiao(400, 425, self.times) # 12-13 self.shuatuzuobiao(280, 365, self.times) # 12-12 self.shuatuzuobiao(265, 245, self.times) # 12-11 self.shuatuzuobiao(130, 265, self.times) # 12-10 self.d.drag(200, 270, 600, 270, 0.1) # 拖拽到最左 time.sleep(2) self.shuatuzuobiao(675, 380, self.times) # 12-9 self.shuatuzuobiao(550, 440, self.times) # 12-8 self.shuatuzuobiao(445, 365, self.times) # 12-7 self.shuatuzuobiao(575, 245, self.times) # 12-6 self.shuatuzuobiao(435, 250, self.times) # 12-5 self.shuatuzuobiao(310, 285, self.times) # 12-4 self.shuatuzuobiao(265, 395, self.times) # 12-3 self.shuatuzuobiao(155, 315, self.times) # 12-2 self.shuatuzuobiao(185, 210, self.times) # 12-1 self.lockimg('img/liwu.bmp', elseclick=[(131, 533)], elsedelay=1, at=(891, 413, 930, 452)) # 回首页 else: pcr_log(self.account).write_log(level='error', message='>>>高延迟模式刷图失败,放弃刷图<<<\r\n') self.lockimg('img/liwu.bmp', elseclick=[(131, 533)], elsedelay=1, at=(891, 413, 930, 452)) # 回首页
def shuatu11(self): # 进入冒险 self.click(480, 505, pre_delay=2, post_delay=2) while True: screen_shot_ = self.d.screenshot(format="opencv") if UIMatcher.img_where(screen_shot_, 'img/dixiacheng.jpg'): break self.click( 562, 253, ) self.click(701, 83, pre_delay=4, post_delay=2) self.duanyazuobiao() if self.tag < 22: # 暂时先按各11次来判定 for _ in range(4): # 以7图为基向右移动4图 self.click(925, 275, post_delay=3) while True: screen_shot_ = self.d.screenshot(format="opencv") if UIMatcher.img_where(screen_shot_, 'img/normal.jpg', at=(660, 72, 743, 94)): break if UIMatcher.img_where(screen_shot_, 'img/hard.jpg'): self.click(701, 83, pre_delay=0, post_delay=2) break self.switch = 0 self.d.drag(600, 270, 200, 270, 0.1) # 拖拽到最右 time.sleep(2) self.shuatuzuobiao(663, 408, self.times) # 11-17 self.shuatuzuobiao(542, 338, self.times) # 11-16 self.shuatuzuobiao(468, 429, self.times) # 11-15 self.shuatuzuobiao(398, 312, self.times) # 11-14 self.shuatuzuobiao(302, 428, self.times) # 11-13 self.shuatuzuobiao(182, 362, self.times) # 11-12 self.shuatuzuobiao(253, 237, self.times) # 11-11 self.shuatuzuobiao(107, 247, self.times) # 11-10 self.d.drag(200, 270, 600, 270, 0.1) # 拖拽到最左 time.sleep(2) self.shuatuzuobiao(648, 316, self.times) # 11-9 self.shuatuzuobiao(594, 420, self.times) # 11-8 self.shuatuzuobiao(400, 432, self.times) # 11-7 self.shuatuzuobiao(497, 337, self.times) # 11-6 self.shuatuzuobiao(558, 240, self.times) # 11-5 self.shuatuzuobiao(424, 242, self.times) # 11-4 self.shuatuzuobiao(290, 285, self.times) # 11-3 self.shuatuzuobiao(244, 412, self.times) # 11-2 self.shuatuzuobiao(161, 325, self.times) # 11-1 self.lockimg('img/liwu.bmp', elseclick=[(131, 533)], elsedelay=1, at=(891, 413, 930, 452)) # 回首页 else: pcr_log(self.account).write_log(level='error', message='>>>高延迟模式刷图失败,放弃刷图<<<\r\n') self.lockimg('img/liwu.bmp', elseclick=[(131, 533)], elsedelay=1, at=(891, 413, 930, 452)) # 回首页
def shuatu10(self): # 进入冒险 self.click(480, 505, pre_delay=2, post_delay=2) while True: screen_shot_ = self.d.screenshot(format="opencv") if UIMatcher.img_where(screen_shot_, 'img/dixiacheng.jpg'): break self.click( 562, 253, ) self.click(701, 83, pre_delay=4, post_delay=2) self.duanyazuobiao() if self.tag < 22: # 暂时先按各11次来判定 for _ in range(3): # 以7图为基向右移动3图 self.click(925, 275, post_delay=3) while True: screen_shot_ = self.d.screenshot(format="opencv") if UIMatcher.img_where(screen_shot_, 'img/normal.jpg', at=(660, 72, 743, 94)): break if UIMatcher.img_where(screen_shot_, 'img/hard.jpg'): self.click(701, 83, pre_delay=0, post_delay=2) break self.switch = 0 self.d.drag(600, 270, 200, 270, 0.1) time.sleep(2) self.shuatuzuobiao(821, 299, self.times) # 10-17 self.shuatuzuobiao(703, 328, self.times) # 10-16 self.shuatuzuobiao(608, 391, self.times) # 10-15 self.shuatuzuobiao(485, 373, self.times) # 10-14 self.shuatuzuobiao(372, 281, self.times) # 10-13 self.shuatuzuobiao(320, 421, self.times) # 10-12 self.shuatuzuobiao(172, 378, self.times) # 10-11 self.shuatuzuobiao(251, 235, self.times) # 10-10 self.shuatuzuobiao(111, 274, self.times) # 10-9 self.d.drag(200, 270, 600, 270, 0.1) # 拖拽到最左 time.sleep(2) self.shuatuzuobiao(690, 362, self.times) # 10-8 self.shuatuzuobiao(594, 429, self.times) # 10-7 self.shuatuzuobiao(411, 408, self.times) # 10-6 self.shuatuzuobiao(518, 332, self.times) # 10-5 self.shuatuzuobiao(603, 238, self.times) # 10-4 self.shuatuzuobiao(430, 239, self.times) # 10-3 self.shuatuzuobiao(287, 206, self.times) # 10-2 self.shuatuzuobiao(146, 197, self.times) # 10-1 self.lockimg('img/liwu.bmp', elseclick=[(131, 533)], elsedelay=1, at=(891, 413, 930, 452)) # 回首页 else: pcr_log(self.account).write_log(level='error', message='>>>高延迟模式刷图失败,放弃刷图<<<\r\n') self.lockimg('img/liwu.bmp', elseclick=[(131, 533)], elsedelay=1, at=(891, 413, 930, 452)) # 回首页
def img_cut(screen, at): try: x1, y1, x2, y2 = at screen = screen[y1:y2 + 1, x1:x2 + 1] except: pcr_log('admin').write_log(level='debug', message="检测区域填写错误") exit(-1) return screen
def ziduan00(self): pcr_log(self.account).write_log( level='warning', message='>>>识别到00参数该字段不刷图,要刷图请更改zhanghao.txt!结束刷图任务!<<<\r\n') self.lockimg('img/liwu.bmp', elseclick=[(131, 533)], elsedelay=1, at=(891, 413, 930, 452)) # 回首页
async def bad_connecting(self): # 异步判断异常 By:CyiceK # 测试 _time = 0 cumulative_time = 0.1 while Multithreading({}).is_stopped(): try: screenshot = self.last_screen if screenshot is None: continue if time.time() - self.last_screen_time > async_screenshot_freq: continue time_start = time.time() if self.is_exists(screen=screenshot, img='img/connecting.bmp', at=(748, 20, 931, 53)): cumulative_time = 0.1 # 卡连接 time.sleep(1) time_end = time.time() _time = time_end - time_start _time = _time + _time if _time > bad_connecting_time: _time = 0 # LOG().Account_bad_connecting(self.account) raise Exception("connecting时间过长") elif self.is_exists(screen=screenshot, img='img/loading.bmp', threshold=0.8): # 卡加载 # 不知道为什么,at 无法在这里使用 cumulative_time = 0.1 time.sleep(1) time_end = time.time() _time = time_end - time_start _time = _time + _time if _time > bad_connecting_time: pcr_log(self.account).write_log(level='error', message='%s卡connecting/loading了,qwq' % self.account) _time = 0 raise Exception("loading时间过长") elif self.is_exists(screen=screenshot, img='img/fanhuibiaoti.bmp', at=(377, 346, 581, 395)): cumulative_time = 0.1 # 返回标题 raise Exception("reboot", "网络错误,重启。") elif self.is_exists(screen=screenshot, img='img/shujucuowu.bmp', at=(407, 132, 559, 297)): cumulative_time = 0.1 # 数据错误 raise Exception("数据错误,重启。") elif cumulative_time < 10: cumulative_time = cumulative_time + 1 time.sleep(bad_connecting_time + cumulative_time) # 过快可能会卡 except Exception as e: self.send_move_method("restart", f"bad_connecting-{e}") time.sleep(1) time.sleep(0.8 + self.change_time)
def shuatu8(self): # 进入冒险 self.click(480, 505, pre_delay=2, post_delay=2) while True: screen_shot_ = self.d.screenshot(format="opencv") if UIMatcher.img_where(screen_shot_, 'img/dixiacheng.jpg'): break self.click( 562, 253, ) self.click(701, 83, pre_delay=4, post_delay=2) self.duanyazuobiao() if self.tag < 22: # 暂时先按各11次来判定 for _ in range(1): # 以7图为基向右移动3图 self.click(925, 275, post_delay=3) while True: screen_shot_ = self.d.screenshot(format="opencv") if UIMatcher.img_where(screen_shot_, 'img/normal.jpg', at=(660, 72, 743, 94)): break if UIMatcher.img_where(screen_shot_, 'img/hard.jpg'): self.click(701, 83, pre_delay=0, post_delay=2) break self.switch = 0 self.d.drag(600, 270, 200, 270, 0.1) time.sleep(2) self.shuatuzuobiao(584, 260, self.times) # 8-14 self.shuatuzuobiao(715, 319, self.times) # 8-13 self.shuatuzuobiao(605, 398, self.times) # 8-12 self.shuatuzuobiao(478, 374, self.times) # 8-11 self.shuatuzuobiao(357, 405, self.times) # 8-10 self.shuatuzuobiao(263, 324, self.times) # 8-9 self.shuatuzuobiao(130, 352, self.times) # 8-8 self.d.drag(200, 270, 600, 270, 0.1) # 拖拽到最左 time.sleep(2) self.shuatuzuobiao(580, 401, self.times) # 8-7 self.shuatuzuobiao(546, 263, self.times) # 8-6 self.shuatuzuobiao(457, 334, self.times) # 8-5 self.shuatuzuobiao(388, 240, self.times) # 8-4 self.shuatuzuobiao(336, 314, self.times) # 8-3 self.shuatuzuobiao(230, 371, self.times) # 8-2 self.shuatuzuobiao(193, 255, self.times) # 8-1 self.lockimg('img/liwu.bmp', elseclick=[(131, 533)], elsedelay=1, at=(891, 413, 930, 452)) # 回首页 else: pcr_log(self.account).write_log(level='error', message='>>>高延迟模式刷图失败,放弃刷图<<<\r\n') self.lockimg('img/liwu.bmp', elseclick=[(131, 533)], elsedelay=1, at=(891, 413, 930, 452)) # 回首页
def shuatuNN(self, tu_dict: list, use_ocr=False, var={}): """ 刷指定N图 tu_dict: 其实应该叫tu_list,来不及改了 ["A-B-Times",...,] :return: """ # 进入冒险 L = ShuatuToTuple(tu_dict) if use_ocr or force_as_ocr_as_possible: # L: List[Tuple[A,B,T]] new_L = [] for l in L: A, B, T = l new_L += [f"{A}-{B}-{T}"] self.shuatu_daily_ocr(new_L, 0, False, "do", "do", "skip", "exit", False, "zhanli", False, var) return # 按照 A-B的顺序排序:A为主要依据,B为次要依据。 self.enter_normal() self.switch = 0 cur_map = self.check_normal_id() mv = movevar(var) if "curNN" in var: cur = var["curNN"] A, B, Times = L[cur] self.log.write_log("info", f"断点恢复:上次刷到了{A}-{B},继续执行。") else: cur = 0 var["curNN"] = 0 for cur in range(cur, len(L)): var["curNN"] = cur mv.save() A, B, Times = L[cur] if A not in NORMAL_COORD: pcr_log(self.account).write_log("error", f"坐标库中没有图号{A}-{B}的信息!跳过此图。") continue while cur_map != A: self.select_normal_id(A) cur_map = A now_dict = NORMAL_COORD[A] if B in now_dict["left"]: if A != 1: self.Drag_Left() xy = now_dict["left"][B] self.shuatuzuobiao(*xy, Times) elif B in now_dict["right"]: if A != 1: self.Drag_Right() xy = now_dict["right"][B] self.shuatuzuobiao(*xy, Times) else: pcr_log(self.account).write_log("error", f"坐标库中没有图号{A}-{B}的信息!跳过此图。") continue del var["curNN"] mv.save() self.lock_home()
def shuatu7(self): # 进入冒险 self.click(480, 505, pre_delay=2, post_delay=2) while True: screen_shot_ = self.d.screenshot(format="opencv") if UIMatcher.img_where(screen_shot_, 'img/dixiacheng.jpg'): break self.d.click(562, 253) time.sleep(3) self.d.click(701, 83) time.sleep(2) self.duanyazuobiao() if self.tag < 22: # 暂时先按各11次来判定 while True: screen_shot_ = self.d.screenshot(format="opencv") if UIMatcher.img_where(screen_shot_, 'img/normal.jpg', at=(660, 72, 743, 94)): break if UIMatcher.img_where(screen_shot_, 'img/hard.jpg'): self.d.click(701, 83) break self.switch = 0 self.d.drag(600, 270, 200, 270, 0.1) # 拖拽到最右 time.sleep(2) self.shuatuzuobiao(760, 240, self.times) # 7-14 self.shuatuzuobiao(630, 257, self.times) # 7-13 self.shuatuzuobiao(755, 350, self.times) # 7-12 self.shuatuzuobiao(664, 410, self.times) # 7-11 self.shuatuzuobiao(544, 400, self.times) # 7-10 self.shuatuzuobiao(505, 300, self.times) # 7-9 self.shuatuzuobiao(410, 240, self.times) # 7-8 self.d.drag(200, 270, 600, 270, 0.1) # 拖拽到最左 time.sleep(2) self.shuatuzuobiao(625, 230, self.times) # 7-7 self.shuatuzuobiao(680, 365, self.times) # 7-6 self.shuatuzuobiao(585, 425, self.times) # 7-5 self.shuatuzuobiao(500, 330, self.times) # 7-4 self.shuatuzuobiao(450, 240, self.times) # 7-3 self.shuatuzuobiao(353, 285, self.times) # 7-2 self.shuatuzuobiao(275, 200, self.times) # 7-1 self.lockimg('img/liwu.bmp', elseclick=[(131, 533)], elsedelay=1, at=(891, 413, 930, 452)) # 回首页 else: pcr_log(self.account).write_log(level='error', message='>>>高延迟模式刷图失败,放弃刷图<<<\r\n') self.lockimg('img/liwu.bmp', elseclick=[(131, 533)], elsedelay=1, at=(891, 413, 930, 452)) # 回首页
def do_login(self, ac, pwd): # 执行登陆逻辑 """ :param ac: :param pwd: :return: """ for retry in range(30): if not self.d( resourceId= "com.bilibili.priconne:id/bsgamesdk_edit_username_login" ).exists(): time.sleep(2) else: break else: raise Exception("进入登陆页面失败!") self.d( resourceId="com.bilibili.priconne:id/bsgamesdk_edit_username_login" ).click() self.d.clear_text() self.d.send_keys(str(ac)) self.d( resourceId="com.bilibili.priconne:id/bsgamesdk_edit_password_login" ).click() self.d.clear_text() self.d.send_keys(str(pwd)) self.d(resourceId="com.bilibili.priconne:id/bsgamesdk_buttonLogin" ).click() time.sleep(20) if debug: print("等待认证") while self.d(text="请滑动阅读协议内容").exists(): if debug: print("发现协议") self.d.touch.down(814, 367).sleep(1).up(814, 367) self.d(text="同意").click() time.sleep(10) while self.d(text="Geetest").exists(): self.phone_privacy() pcr_log(self.account).server_bot('', message='%s账号出现了验证码' % self.account) return -1 if debug: print("认证结束") if self.d(resourceId= "com.bilibili.priconne:id/bsgamesdk_edit_authentication_name" ).exists(timeout=0.1): return 1 # 说明要进行认证 else: return 0 # 正常
async def juqingtiaoguo(self): # 异步跳过教程 By:CyiceK # 测试 global th_sw global screenshot while th_sw == 0: cpu_occupy = psutil.cpu_percent(interval=5, percpu=False) if cpu_occupy >= 80: # print('ka') time.sleep(0.8) try: # await asyncio.sleep(10) # time.sleep(10) # 过快可能会卡 if UIMatcher.img_where(screenshot, 'img/caidan_yuan.jpg', at=(860, 0, 960, 100)): self.d.click(917, 39) # 菜单 time.sleep(1) self.d.click(807, 44) # 跳过 time.sleep(1) self.d.click(589, 367) # 跳过ok time.sleep(5) if UIMatcher.img_where(screenshot, 'img/kekeluo.bmp', at=(181, 388, 384, 451)): # 防妈骑脸 self.d.click(1, 1) time.sleep(3) self.d.click(1, 1) if UIMatcher.img_where(screenshot, 'img/dxc_tb_1.bmp', at=(0, 390, 147, 537)): self.lockimg('img/liwu.bmp', elseclick=[(131, 533)], elsedelay=1) # 回首页 if UIMatcher.img_where(screenshot, 'img/dxc_tb_2.bmp', at=(580, 320, 649, 468)): time.sleep(4) self.d.click(610, 431) self.lockimg('img/liwu.bmp', elseclick=[(131, 533)], elsedelay=1) # 回首页 except Exception as e: pcr_log(self.account).write_log( level='error', message='异步线程终止并检测出异常{}'.format(e)) th_sw = 1 # sys.exit() break
async def c_img_server_bot(self): """ 此函数会定时发送截图给sever_bot :return: """ while Multithreading({}).is_stopped(): if sentstate != 0: account = self.account address = self.address await asyncio.sleep(sentstate * 60 + 1) sent_img = self.getscreen() pcr_log(account).server_bot('STATE', '', '', img=sent_img, img_title=f"server_bot运行截图播报\n" f"账号:{account}\n" f"所运行的设备:{address}")
def ocr_center(self, x1, y1, x2, y2, screen_shot=None, size=1.0): """ :param size: 放大的大小 :param x1: 左上坐标 :param y1: 左上坐标 :param x2: 右下坐标 :param y2: 右下坐标 :param screen_shot: 截图 :return: """ global ocr_text try: requests.get(url="http://127.0.0.1:5000/ocr/") except: pcr_log(self.account).write_log(level='error', message='无法连接到OCR,请尝试重新开启app.py') return -1 if len(ocr_mode) == 0: return -1 # OCR识别任务分配 if ocr_mode == "智能": baidu_ocr_ping = requests.get(url="https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic") code = baidu_ocr_ping.status_code if code == 200: ocr_text = self.baidu_ocr(x1, y1, x2, y2, screen_shot=screen_shot, size=size) if ocr_text == -1: ocr_text = self.ocr_local(x1, y1, x2, y2, screen_shot=screen_shot, size=size) else: ocr_text = self.ocr_local(x1, y1, x2, y2, screen_shot=screen_shot, size=size) elif ocr_mode == "网络": ocr_text = self.baidu_ocr(x1, y1, x2, y2, screen_shot=screen_shot, size=size) elif ocr_mode == "本地": ocr_text = self.ocr_local(x1, y1, x2, y2, screen_shot=screen_shot, size=size) elif ocr_mode == "混合": # 机器伪随机 ocr_way = random.randint(1, 2) if ocr_way == 1: ocr_text = self.baidu_ocr(x1, y1, x2, y2, screen_shot=screen_shot, size=size) elif ocr_way == 2: ocr_text = self.ocr_local(x1, y1, x2, y2, screen_shot=screen_shot, size=size) # OCR返回的数据 纠错 try: if ocr_text: return str(ocr_text) else: return -1 except: raise Exception("ocr-error", "OCR识别错误。")
def shuatuNN(self, tu_dict: list, var={}): """ 刷指定N图 tu_dict: 其实应该叫tu_list,来不及改了 ["A-B-Times",...,] :return: """ # 进入冒险 L = ShuatuToTuple(tu_dict) # 按照 A-B的顺序排序:A为主要依据,B为次要依据。 self.enter_normal() self.switch = 0 cur_map = self.check_normal_id() mv = movevar(var) if "curNN" in var: cur = var["curNN"] A, B, Times = L[cur] self.log.write_log("info", f"断点恢复:上次刷到了{A}-{B},继续执行。") else: cur = 0 var["curNN"] = 0 for cur in range(cur, len(L)): var["curNN"] = cur mv.save() A, B, Times = L[cur] if A not in NORMAL_COORD: pcr_log(self.account).write_log("error", f"坐标库中没有图号{A}-{B}的信息!跳过此图。") continue while cur_map != A: self.select_normal_id(A) cur_map = A now_dict = NORMAL_COORD[A] if B in now_dict["left"]: if A != 1: self.Drag_Left() xy = now_dict["left"][B] self.shuatuzuobiao(*xy, Times) elif B in now_dict["right"]: if A != 1: self.Drag_Right() xy = now_dict["right"][B] self.shuatuzuobiao(*xy, Times) else: pcr_log(self.account).write_log("error", f"坐标库中没有图号{A}-{B}的信息!跳过此图。") continue del var["curNN"] mv.save() self.lock_home()
def rename(self, name): # 重命名 self.d.click(871, 513) # 主菜单 self.lockimg('img/bangzhu.bmp', ifclick=[(370, 270)]) # 锁定帮助 点击简介 self.lockimg('img/bianji.bmp', ifclick=[(900, 140)]) # 锁定 点击铅笔修改按钮 self.lockimg('img/biangeng.bmp', ifclick=[(480, 270)]) # 锁定 玩家名 点击游戏渲染编辑框 time.sleep(1) self.d.click(290, 425) # 点击编辑框 self.d.clear_text() self.d.send_keys(name) self.d.click(880, 425) # 点击确定 time.sleep(0.5) self.d.click(590, 370) # 变更按钮 time.sleep(1) self.lockimg('img/bangzhu.bmp', elseclick=[(32, 32)]) # 锁定帮助 pcr_log(self.account).write_log(level='info', message='账号:%s已修改名字' % name)
def img_where(cls, screen, template_path, threshold=0.84, at=None): """ 在screen里寻找template,若找到则返回坐标,若没找到则返回False 注:可以使用if img_where(): 来判断图片是否存在 :param threshold: :param screen: :param template_path: :param at: 缩小查找范围 :return: """ if screen.shape[0] > screen.shape[1]: screen = UIMatcher.RotateClockWise90(screen) if at is not None: try: x1, y1, x2, y2 = at screen = screen[y1:y2, x1:x2] except: pcr_log('admin').write_log(level='debug', message="检测区域填写错误") exit(-1) else: x1, y1 = 0, 0 # 缓存未命中时从源文件读取 if template_path not in cls.template_cache: template = cv2.imread(template_path) cls.template_cache[template_path] = template else: template = cls.template_cache[template_path] th, tw = template.shape[:2] # rows->h, cols->w res = cv2.matchTemplate(screen, template, cv2.TM_CCOEFF_NORMED) min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) if max_val >= threshold: x = x1 + max_loc[0] + tw // 2 y = y1 + max_loc[1] + th // 2 if debug: print("{}--{}--({},{})".format(template_path, round(max_val, 3), x, y)) pcr_log('admin').write_log(level='debug', message="{}--{}--({},{})".format(template_path, round(max_val, 3) , x, y)) pass if at is None: pass print("{} at=({}, {}, {}, {})".format(template_path, x1 + max_loc[0], y1 + max_loc[1], x1 + max_loc[0] + tw, y1 + max_loc[1] + th)) pcr_log('admin').write_log(level='debug', message="{} at=({}, {}, {}, {})".format(template_path, x1 + max_loc[0], y1 + max_loc[1], x1 + max_loc[0] + tw, y1 + max_loc[1] + th)) return x, y else: if debug: print("{}--{}".format(template_path, round(max_val, 3))) pcr_log('admin').write_log(level='debug', message="{}--{}".format(template_path, round(max_val, 3))) return False
def shuatuHH(self, tu_dict: list, use_ocr: bool = False, var={}): """ 刷指定H图 :param tu_dict: 刷图列表 tu_dict: 其实应该叫tu_list,来不及改了 ["A-B-Times",...,] :return: """ L = ShuatuToTuple(tu_dict) if use_ocr or force_as_ocr_as_possible: # L: List[Tuple[A,B,T]] new_L = [] for l in L: A, B, T = l new_L += [f"H{A}-{B}-{T}"] self.shuatu_daily_ocr(new_L, 0, False, "do", "do", "skip", "exit", False, "zhanli", False, var) return self.enter_hard() self.switch = 0 cur_map = self.check_hard_id(self.last_screen) mv = movevar(var) if "curHH" in var: cur = var["curHH"] A, B, Times = L[cur] self.log.write_log("info", f"断点恢复:上次刷到了H{A}-{B},继续执行。") else: cur = 0 var["curHH"] = 0 for cur in range(cur, len(L)): var["curHH"] = cur mv.save() A, B, Times = L[cur] if A not in HARD_COORD: pcr_log(self.account).write_log("error", f"坐标库中没有图号H{A}-{B}的信息!跳过此图。") continue while cur_map != A: self.select_hard_id(A) cur_map = A now_dict = HARD_COORD[A] if B in now_dict: xy = now_dict[B] self.shuatuzuobiao(*xy, Times) else: pcr_log(self.account).write_log("error", f"坐标库中没有图号H{A}-{B}的信息!跳过此图。") continue del var["curHH"] mv.save() self.lock_home()
def __init__(self, _log=None): self.host_result = '' self._count_times = 0 self.img = None self.question_type = 0 self.conversation = requests.Session() self.conversation.keep_alive = False self.conversation.mount('http://', HTTPAdapter(max_retries=2)) self.conversation.mount('https://', HTTPAdapter(max_retries=2)) self.img_post_url = 'http://' + self.host_result + '/UploadBase64.aspx' self.img_answer = 'http://' + self.host_result + '/GetAnswer.aspx' self.img_send_error = 'http://' + self.host_result + '/SendError.aspx' self.img_getpoint = 'http://' + self.host_result + '/GetPoint.aspx' self.error_feature = ['#', '', ' '] self.no_result = [ "#答案不确定", "#超时", "不扣分", "#", '#编号不存在', '#Tid不正确(#题分不足)' ] self.img_hear_dict = { 'Content-Type': 'application/x-www-form-urlencoded', } if _log: self.log = _log else: from core import log_handler self.log = log_handler.pcr_log("CaptionSkip_admin")
async def juqingtiaoguo(self): # 异步跳过教程 By:CyiceK # 废弃 cumulative_time = 0.1 while Multithreading({}).is_stopped(): if not self.async_juqingtiaoguo_switch: time.sleep(1) continue # print('juqing', self.change_time) try: # await asyncio.sleep(10) # time.sleep(10) # 过快可能会卡 time.sleep(cumulative_time) screenshot = self.last_screen if self.is_exists(screen=screenshot, img='img/juqing/caidanyuan.bmp', at=(860, 0, 960, 100)): self.lock_img('img/juqing/caidanyuan.bmp', ifclick=[(917, 39)], ifdelay=self.change_time, retry=15) # 菜单 self.lock_img('img/juqing/tiaoguo_1.bmp', ifclick=[(807, 44)], ifdelay=self.change_time, retry=15) # 跳过 self.lock_img('img/juqing/tiaoguo_2.bmp', ifclick=[(589, 367)], ifdelay=self.change_time, retry=15) # 跳过 cumulative_time = 0.1 elif self.is_exists(screen=screenshot, img='img/kekeluo.bmp', at=(181, 388, 384, 451)): # 防妈骑脸 self.lock_no_img('img/kekeluo.bmp', elseclick=[(1, 1)], at=(181, 388, 384, 451)) cumulative_time = 0.1 elif self.is_exists(screen=screenshot, img='img/dxc_tb_1.bmp', at=(0, 390, 147, 537)): self.lock_no_img('img/dxc_tb_1.bmp', ifclick=[(131, 533)], elsedelay=self.change_time) # 回首页 cumulative_time = 0.1 elif self.is_exists(screen=screenshot, img='img/dxc_tb_2.bmp', at=(580, 320, 649, 468)): self.lock_no_img('img/dxc_tb_2.bmp', ifclick=[(610, 431)], elsedelay=self.change_time) self.lock_img('img/liwu.bmp', elseclick=[(131, 533)], elsedelay=self.change_time) # 回首页 self.click(480, 505, pre_delay=0.5, post_delay=self.change_time) cumulative_time = 0.1 if self.is_exists('img/dixiacheng.jpg', at=(837, 92, 915, 140)): self.lock_no_img('img/dixiacheng.jpg', elseclick=(900, 138), elsedelay=self.change_time, retry=10) raise Exception("地下城吃塔币跳过完成,重启") elif cumulative_time < 20: cumulative_time = cumulative_time + 1 except Exception as e: pcr_log(self.account).write_log(level='error', message='juqingtiaoguo-异步线程终止并检测出异常{}'.format(e)) # sys.exit() break time.sleep(0.8 + self.change_time)
def tasks(a: Automator, account, opcode): # 主功能体函数,可以在本函数中自定义需要的功能 try: a.c_async(a, account, a.screenshot(), sync=False) # 异步眨眼截图,开异步必须有这个 a.init_home() # 初始化,确保进入首页 a.c_async(a, account, a.juqingtiaoguo(), sync=False) # 异步剧情跳过 a.c_async(a, account, a.bad_connecting(), sync=False) # 异步异常处理 a.gonghuizhijia() # 家园一键领取 # a.goumaimana(1) # 购买mana 10次 a.mianfeiniudan() # 免费扭蛋 # a.mianfeishilian() # 免费十连 a.shouqu() # 收取所有礼物 a.dianzan(sortflag=1) # 公会点赞,sortflag=1表示按战力排序 a.dixiacheng_ocr(skip=False) # 地下城 skip是否开启战斗跳过 # a.goumaitili(3) # 购买3次体力 # a.buyExp() # 买药 # a.doActivityHard() # 刷活动hard # a.do1to3Hard() # 刷hard 4-1图, 需已开Hard 4-1 # a.do11to3Hard() # 刷hard 11-3图,需已开Hard 11图 a.shouqurenwu() # 收取任务 # a.tansuo() # 刷探索,注意mana号没开探索可能会卡死 if can_shuatu( opcode): # 仅当刷图被激活(即注明了刷图图号)的账号执行行会捐赠,不刷图的认为是mana号不执行行会捐赠。 ''' 目前支持刷图图号,(请将需要的图号填入zhanghao.txt) 'h00': # h00为不刷任何hard图 'h01': # 刷hard 1-11图 'tsk': # 探索开,注意mana号没开探索可能会卡死 'n07': # 刷7图 'n08': # 刷8图 'n10': # 刷10图 'n11': # 刷11图 'n12': # 刷12图 ''' a.shuatu(opcode) # 刷normal和探索图,需要再zhanghao.txt里注明,不然不会刷 a.shuatu_hard(opcode) # 刷hard图,需要再zhanghao.txt里注明,不然不会刷 a.hanghui() # 刷图后进行行会捐赠 else: # 刷图没有被激活的可以去刷经验 # a.goumaitili(times=3) # 购买times次体力 a.shuajingyan(map=3) # 刷1-1经验,map为主图 pass a.shouqurenwu() # 二次收取任务 except Ellipsis as e: pcr_log(account).write_log(level='error', message='main-检测出异常{}'.format(e))
def baidu_ocr(self, x1, y1, x2, y2, size=1.0, screen_shot=None): # size表示相对原图的放大/缩小倍率,1.0为原图大小,2.0表示放大两倍,0.5表示缩小两倍 # 默认原图大小(1.0) if len(baidu_apiKey) == 0 or len(baidu_secretKey) == 0: pcr_log(self.account).write_log(level='error', message='读取SecretKey或apiKey失败!') return -1 # 强制size为1.0,避免百度无法识图 size = 1.0 if screen_shot is None: screen_shot = self.getscreen() # from numpy import rot90 # screen_shot_ = rot90(screen_shot_) # 旋转90° if baidu_ocr_img: cv2.imwrite('baidu_ocr.bmp', screen_shot) if screen_shot.shape[0] > screen_shot.shape[1]: if anticlockwise_rotation_times >= 1: for _ in range(anticlockwise_rotation_times): screen_shot = UIMatcher.AutoRotateClockWise90(screen_shot) screen_shot = UIMatcher.AutoRotateClockWise90(screen_shot) # cv2.imwrite('fuck_rot90_test.bmp', screen_shot_) # screen_shot_ = rot90(screen_shot_) # 旋转90° pass part = screen_shot[y1:y2, x1:x2] # 对角线点坐标 part = cv2.resize(part, None, fx=size, fy=size, interpolation=cv2.INTER_LINEAR) # 利用resize调整图片大小 partbin = cv2.imencode('.jpg', part)[1] # 转成base64编码(误) try: files = {'file': ('tmp.png', partbin, 'image/png')} result = requests.post(url="http://127.0.0.1:5000/ocr/baidu_ocr/", files=files) # 原生输出有助于开发者 result = result.json().get('words_result')[0].get('words') return result except: pcr_log(self.account).write_log( level='error', message='百度云识别失败!请检查apikey和secretkey以及截图范围返回结果' '是否有误!') return -1
def tiaozhan() -> bool: # 非主流写法,内部方法 while True: self.lock_img(TUANDUIZHAN_BTN["tiaozhan"], ifclick=[(833, 462)], side_check=self.juqing_kkr) self.lock_img(DXC_ELEMENT["sheding"], ifclick=(478, 443), retry=3) if self.is_exists(TUANDUIZHAN_BTN["guanbi"]): self.click(TUANDUIZHAN_BTN["guanbi"]) if self.is_exists(TUANDUIZHAN_BTN["qianwangguanqia"]): # self.lock_no_img(TUANDUIZHAN_BTN["qianwangguanqia"], elseclick=(592, 436)) # 刷 1-1获取次数? pcr_log(self.account).write_log("info", f"没有挑战次数") self.lock_home() return False if self.is_exists('img/notzhandoukaishi.bmp', at=(758, 423, 915, 473), is_black=True, black_threshold=1400): # 全部 self.click_btn(DXC_ELEMENT["quanbu_white"], until_appear=DXC_ELEMENT["quanbu_blue"], elsedelay=0.1) if not self.is_exists(DXC_ELEMENT["zhiyuan_gouxuan"]): for i in range(1, 9): self.click(DXC_ELEMENT["zhiyuan_dianren"][i]) break else: # 点完人后确认一遍 if self.is_exists('img/notzhandoukaishi.bmp', at=(758, 423, 915, 473), is_black=True, black_threshold=1400): pcr_log(self.account).write_log( level='info', message="%s没有合适的人物打公会战!" % self.account) self.lock_home() return False break elif self.is_exists('img/dxc/zhandoukaishi.bmp', at=(758, 423, 915, 473)): return True
def init(self, address, account): """ device: 如果是 USB 连接,则为 adb devices 的返回结果;如果是模拟器,则为模拟器的控制 URL 。 """ self.appRunning = False self.account = account self.d = u2.connect(address) self.dWidth, self.dHeight = self.d.window_size() self.log = log_handler.pcr_log(account) # 初始化日志 self.AR = AutomatorRecorder(account)