# -*- encoding=utf8 -*-
__author__ = "GAME"
from airtest.utils.logger import get_logger
import logging
logger=get_logger("airtest")
logger.setLevel(logging.ERROR)
import sys
import json
from airtest.core.api import *
from airtest.cli.parser import cli_setup
from airtest.aircv import *
from PIL import Image
from ocrmode import *
# 初始化连接adb
auto_setup(__file__, logdir=False, project_root="D:/hzw_haidao",
devices=["Android:///"])
#dev = connect_device("Android:///127.0.0.1:5554?cap_method=javacap&touch_method=adb")
dev = connect_device("Android:///")
# 棋盘
num_xy = {"1": [815, 322], "3": [815, 522], "5": [683, 422], "7": [551, 322], "9": [551, 522], "10": [538, 226]}
# 待选区
'''
daixuan_xy = {"1": {"zuobiao": [175, 835], "name": "null"}, "2": {"zuobiao": [392, 835], "name": "null"},
"3": {"zuobiao": [623, 835], "name": "null"}, "4": {"zuobiao": [850, 835], "name": "null"},
"5": {"zuobiao": [1075, 835], "name": "null"}, "6": {"zuobiao": [1300, 835], "name": "null"},
"7": {"zuobiao": [1527, 835], "name": "null"}, "8": {"zuobiao": [1751, 835], "name": "null"}}
'''
#备战待选区
'''
daixuan_xy = {"1": {"zuobiao": [354, 835], "name": "null"}, "2": {"zuobiao": [528, 796], "name": "null"},
"3": {"zuobiao": [699, 835], "name": "null"}, "4": {"zuobiao": [878, 835], "name": "null"},
"5": {"zuobiao": [1046, 835], "name": "null"}, "6": {"zuobiao": [1222, 835], "name": "null"},
"7": {"zuobiao": [1392, 835], "name": "null"}, "8": {"zuobiao": [1569, 835], "name": "null"}}
'''
daixuan_xy = {"1": {"zuobiao": [353, 808], "name": "null"}, "2": {"zuobiao": [528, 808], "name": "null"},
"3": {"zuobiao": [703, 808], "name": "null"}, "4": {"zuobiao": [878, 808], "name": "null"},
"5": {"zuobiao": [1053, 808], "name": "null"}, "6": {"zuobiao": [1228, 808], "name": "null"},
"7": {"zuobiao": [1403, 808], "name": "null"}, "8": {"zuobiao": [1578, 808], "name": "null"}}
#待选区有人数据
dxyr_data=[]
#待选区无人数据
daixuan_data=["1","2","3","4","5","6","7","8"]
#dev.shell("input swipe %s %s %s %s 1000" % (1727,587, daixuan_xy["8"]["zuobiao"][0],835))
# 人物信息
rwinfo = {'阿金': {"weizhi": "1", "zuobiao": 0, "level": 2,"num":0,"maxnum":3}, '克里克': {"weizhi": "5", "zuobiao": 0, "level": 2,"num":0,"maxnum":3},
'强高': {"weizhi": "7", "zuobiao": 0, "level": 2,"num":0,"maxnum":3}, '帕鲁': {"weizhi": "3", "zuobiao": 0, "level": 2,"num":0,"maxnum":3}}
for key in list(rwinfo.keys()):
rwinfo[key]["zuobiao"] = num_xy.get(str(rwinfo[key]["weizhi"]))
# 现在的阵容情况
xianzaizr = {"1": "null", "3": "null", "5": "null", "7": "null", "9": "null"}
# 待选区信息
#daixuanlist = {"1": "null", "2": "null", "3": "null", "4": "null", "5": "null", "6": "null", "7": "null", "8": "null"}
#拖动
def tdxy(x_0,y_0,x_1,y_1,yanchi=300):
dev.shell("input swipe %s %s %s %s %s"%(x_0,y_0,x_1,y_1,yanchi))
# 区域截图类
def core_jt(x_0, y_0, x_1, y_1, filepath):
screen = G.DEVICE.snapshot()
screen = aircv.crop_image(screen, (x_0, y_0, x_1, y_1))
pil_image = cv2_2_pil(screen)
pil_image.save(filepath, quality=99, optimize=True)
return screen
# pytesseract识别数字
def tess_num(picture):
# 二值化后识别会更精确
img = Image.open(picture)
Img = img.convert('L')
threshold = 110
table = []
for i in range(256):
if i < threshold:
table.append(0)
else:
table.append(1)
photo = Img.point(table, '1')
photo.save(picture)
textImage = Image.open("test2.jpg")
text = pytesseract.image_to_string(textImage, lang='wnum',
config='--psm 10 --oem 2 -c tessedit_char_whitelist=0123456789')
return text
# 排队检查
def paidui():
while True:
if exists(Template(r"./picture/paidui/tpl1617260644200.png", record_pos=(0.022, -0.122),
resolution=(1920, 1080))):
print("wait....")
sleep(5.0)
else:
print("gameserver ok")
return 0
break
# 日常任务
def daily_task():
touch(Template(r"./picture/richang/tpl1617256338204.png", record_pos=(0.46, 0.239), resolution=(1920, 1080)))
# 查找日常任务图标
while True:
if exists(Template(r'./picture/richang/richang.png', record_pos=(0.125, 0.06), resolution=(1920, 1080))):
touch(Template(r'./picture/richang/richang.png', record_pos=(0.125, 0.06), resolution=(1920, 1080)))
while True:
try:
touch(Template(r"./picture/richang/tpl1617258247226.png", record_pos=(0.01, 0.183),
resolution=(1920, 1080)))
break
except:
touch(Template(r"./picture/richang/richang.png", record_pos=(0.125, 0.06), resolution=(1920, 1080)))
touch(Template(r"./picture/richang/tpl1617259072269.png", record_pos=(0.21, 0.12), resolution=(1920, 1080)))
break
else:
touch((1542, 518))
# 海岛购买人物类
def hd_hero(gm_num):
if exists(Template(r"tpl1617636327570.png", record_pos=(0.004, 0.248), resolution=(1920, 1080))):
print("开始购买人物")
heroname = ["克里克", "强高", "阿金", "帕鲁"]
name_x = [167, 627]
name_x1 = [355, 675]
# name_x=[174,628]
# name_x1=[381,670]
goumaicishu=1
break_flag = False
#循环购买两次,根据break_flag判断
while break_flag == False:
#if goumaicishu == 1:
#touch((1807, 997))
if goumaicishu>=gm_num:
break_flag = True
if goumaicishu >= 2:
touch((90, 983))
name_x = [167, 627]
name_x1 = [355, 675]
print("第%s次购买"%goumaicishu)
for i in range(0, 5):
core_jt(name_x[0], name_x[1], name_x1[0], name_x1[1], "score%s.png" % i)
# 识别人物名字
name_ocr = cnocr_str("score%s.png" % i)
# 判断是否有人物在商店
if name_ocr != 1:
if name_ocr in heroname:
for dx_id in daixuan_data:
if daixuan_xy[dx_id]["name"] != u"null":
#有人就从无人数据列表删除
daixuan_data.remove(dx_id)
print(dx_id, u",这个位置有人了")
else:
#如果已经买了三次这个英雄,就不进行操作
if rwinfo[name_ocr]["num"]>=rwinfo[name_ocr]["maxnum"]:
print("买过三次了,等待升星")
else:
zba = daixuan_xy[dx_id]["zuobiao"]
print("拖动%s到待选区第%s格"%(name_ocr,dx_id))
#print("move %s,%s to %s,%s" % (name_x[0] + 80, name_x[1] - 40, zba[0], zba[1]))
xy_list = rwinfo[name_ocr]["zuobiao"]
tdxy(name_x[0] + 97, name_x[1] - 40, zba[0], zba[1])
sleep(0.3)
rwinfo[name_ocr]["num"]=rwinfo[name_ocr]["num"]+1 #购买英雄后计数,为升星做准备
dxyr_data.append(dx_id)#更新数据到有人列表
daixuan_xy[dx_id]["name"] = name_ocr # 更新阵容信息
print(daixuan_xy)
touch([1814, 1007])
daixuan_data.append(dx_id)
break
print("待选有人列表,%s"%dxyr_data)
else:
print("******不购买", name_ocr)
else:
print("******识别失败")
name_x[0] = name_x[0] + 370
name_x1[0] = name_x1[0] + 345
goumaicishu=goumaicishu+1
else:
print("不在商店")
# 拖动上阵
def hd_tuodong():
print("start")
touch((917, 913))
print("得到有人待选,%s"%dxyr_data)
for i in dxyr_data:
hero_name=daixuan_xy[i]["name"]
if hero_name!= "null":
hero_xy=daixuan_xy[i]["zuobiao"]# 人物待选坐标
weizhi = rwinfo[hero_name]["weizhi"] # 人物棋盘摆放位置
xy_list = rwinfo[hero_name]["zuobiao"] #棋盘对应坐标
zr_info = xianzaizr[str(weizhi)] #获取棋盘当前人物名字
if rwinfo[hero_name]["num"]==3:
touch((hero_xy[0],hero_xy[1]+30))
print("将%s合成二星"%hero_name)
for name_id in daixuan_xy:
if daixuan_xy[name_id]["name"]==hero_name:
print(daixuan_xy[name_id]["name"])
daixuan_xy[i]["name"] = "null"#更新待选位置人物名
dxyr_data.remove(i)#从有人列表删除
daixuan_data.append(i)#位置空了,添加到无人列表
else:
print("[获取待选栏人物]:%s"%hero_name)
# 如果棋盘位置是空的,就直接上阵
if zr_info == "null":
print("%s上阵到棋盘第%s个位置"%(hero_name,weizhi))
tdxy(hero_xy[0], hero_xy[1], xy_list[0], xy_list[1]) # 开始拖动
xianzaizr[weizhi] = hero_name # 更新新人物格子列表信息
daixuan_xy[i]["name"] = "null"#更新待选位置人物名
dxyr_data.remove(i)#从有人列表删除
daixuan_data.append(i)#位置空了,添加到无人列表
print("当前阵容%s"%xianzaizr)
else:
#优先级判断
level_1 = rwinfo[hero_name]["level"] # 待选栏优先级
level_2 = rwinfo[zr_info]["level"] # 当前阵容位置优先级
if level_1 < level_2: # 如果当前选择英雄优先级比阵上现有的小,开始拖动
print("%s的优先级比%s小,开始替换"%(hero_name,zr_info))
xy_list = rwinfo[name_ocr]["zuobiao"] # 坐标
tdxy(hero_xy[0], hero_xy[1], xy_list[0], xy_list[1]) # 开始拖动
daixuan_xy[i]["name"] = zr_info#更新待选位置人物名
dxyr_data.remove(i)#从有人列表删除
daixuan_data.append(i)#位置空了,添加到无人列表
#如果优先级相同
if level_1==level_2:
print("优先级相同,继续对比人物数量")
#如果待选栏人物数量比在阵上人物的数量多,就下阵棋盘人物
if rwinfo[hero_name]["num"]>=rwinfo[zr_info]["num"]:
print("把%s替换为%s"%(rwinfo[zr_info]["num"],rwinfo[hero_name]["num"]))
tdxy(hero_xy[0], hero_xy[1], xy_list[0], xy_list[1]) # 开始拖动
daixuan_xy[i]["name"] = zr_info#更新待选位置人物名
dxyr_data.remove(i)#从有人列表删除
daixuan_data.append(i)#位置空了,添加到无人列表
else:
# 如果优先级低,就卖掉人物
#如果还没买够升星需要的人物数量
touch((hero_xy[0], hero_xy[1]))#点击原待选位置获取人物信息界面
touch((1467, 928))#点击出售
daixuan_xy[i]["name"] = "null"#更新待选位置人物名
dxyr_data.remove(i)#从有人列表删除
daixuan_data.append(i)#位置空了,添加到无人列表
print("mai")
else:
# 如果优先级低,就卖掉人物
#如果还没买够升星需要的人物数量
touch((hero_xy[0], hero_xy[1]))#点击原待选位置获取人物信息界面
touch((1467, 928))#点击出售
daixuan_xy[i]["name"] = "null"#更新待选位置人物名
dxyr_data.remove(i)#从有人列表删除
daixuan_data.append(i)#位置空了,添加到无人列表
print("mai")
else:
print("******识别失败".encode("GB18030"))
sleep(10)
def haidao():
while True:
core_jt(931,10,995,55,"huihe.png")
huihe_list=cnocr_num("huihe.png")
if huihe_list:
print(huihe_list)
print("开始购买并上阵")
hd_hero(2)
hd_tuodong()
'''
if exists():
hd_hero()
# 如果不在战斗期间,获取回合数,然后升级
else:
# 如果回合数到了4回合以上就开始升级
if hd_huihe() == 4:
touch((1000, 968))
sleep(0.5)
touch((100, 1000), times=1)
if hd_huihe() == 6:
touch((1000, 968))
sleep(0.5)
touch((100, 1000), times=3)
hd_huihe()
'''
# like_num = find_template(screen_01,"duibi.png",-1)['confidence']
# print like_num
# hd_tuodong()
haidao()
#dev.shell("input swipe %s %s %s %s 1000" % (1727,587, 570,835))
# hd_hero()
# tdxy(724,379,698,652)#开始拖动
# tdxy(353,799,531,781)#开始拖动
# hd_rs()
# daily_task()
#haidao()
#获取金币和回合
#core_jt(328,45,398,80,"shangzhen.png")
#core_jt(1528,35,1596,75,"jinbi.png")
#print(cnocr_num("shangzhen.png"))
#print(cnocr_num("jinbi.png"))
文字识别模块
# -*- encoding=utf8 -*-
import mxnet as mx
from cnocr import CnOcr
from PIL import Image
def is_number(s):
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
def cnocr_str(picture):
ocr = CnOcr(name=picture)
img_fp = picture
img = mx.image.imread(img_fp, 1)
res = ocr.ocr(img)
name=""
try:
for i in res[0]:
name=name+i
return name
except:
return 1
def cnocr_num(picture):
img = Image.open(picture)
Img = img.convert('L')
threshold = 110
table = []
for i in range(256):
if i < threshold:
table.append(0)
else:
table.append(1)
photo = Img.point(table, '1')
photo.save(picture)
ocr = CnOcr(name=picture,cand_alphabet="0,1,2,3,4,5,6,7,8,9")
img_fp = picture
img = mx.image.imread(img_fp, 1)
res = ocr.ocr(img)
num_list=[]
try:
for i in res[0]:
if is_number(i):
num_list.append(i)
return num_list
except:
return []