import opencv_compare
import opencvjietu
import mousedianji
import similarity
import cv2
import time
import random
import queue


# 读取图标
# 读取御魂图标
yuhun_pic = cv2.imread('pic/yuhun.png', 0)
# 读取御魂中八岐大蛇图标
yuhundashe_pic = cv2.imread('pic/yuhundashe.png', 0)
# 读取开始图标
yuhuntiaozhan_pic = cv2.imread('pic/tiaozhan.png', 0)
# 读取结束图标
yuhun_jieshu_pic = cv2.imread('pic/jieshu.png', 0)
# 读取结束时的图标 判断是否结束
yuhun_jieshu_pic_s = cv2.imread('pic/jieshu.png')
# 读取御魂失败时的图片
yuhun_shibai_pic = cv2.imread('pic/yuhunshibai.png')

#循环队列
def judge_queue(s):
    #队列长度：
    size = 3
    i=0
    a=[]
    q = queue.Queue(size)
    if q.qsize()<=size:
        q.put(s)
    elif q.full():
        while not q.empty():
            a[i]=q.get()
            i = i+1
        if a[0]==a[1]==a[2]:
            print("图片相同")
            return True
        else:
            for j in range(0,i):
                q.put(a[j+1])
                if j==i-1:
                    break
# 御魂失败
# def yuhundefeat(image_shibai,image, s):
#     if judge_queue(s)==1:
        #连续三次返回的都是相同的数字可以认为是失败了





# 验证是否正确找到目标
def checking(jietufun,imga):
    #作为验证函数 其参数为截图的方法，如截取当前阴阳师客户端全部，或者截取部分区域，另外参数imga是作为对照的图片
    #验证方法在sinularity.py中
    #返回两张图片的汉明距离
    image = imga
    s = 10 #汉明距离的初始值，小于5基本相似
    while s >= 5:
        yysclient_jieshu = jietufun()
        yysclient_jieshu = opencv_compare.PIL_opencv(yysclient_jieshu)
        s = similarity.classify_aHash(image, yysclient_jieshu)

    return s

# 截取当前图片，输入要标出的图片位置并返回坐标点
def feature_matching(imga):
    #参数是需要找出图片，原图片直接在函数中截取
    image = imga
    yysclient = opencvjietu.yys_client()
    yysclient = opencv_compare.PIL_opencv(yysclient)#截取图片
    result = opencv_compare.template_matching(yysclient, image)
    return result

# 进行比对并标点
# 选择御魂图标并点击
def yuhun_tubiao():
    yuhun_xy = feature_matching(yuhun_pic)
    mousedianji.mouse_click(yuhun_xy[0], yuhun_xy[1], 3)

#选择八岐大蛇图标、
def yuhun_dashe_tubiao():
    # 选择御魂-八岐大蛇
    dasheyyh_xy = feature_matching(yuhundashe_pic)
    mousedianji.mouse_click(dasheyyh_xy[0], dasheyyh_xy[1], 0)

# 选择挑战图标
def tiaozhan():
    yuhuntiaozhan_xy = feature_matching(yuhuntiaozhan_pic)
    mousedianji.mouse_click(yuhuntiaozhan_xy[0], yuhuntiaozhan_xy[1], 0)


def yuhun(t):
    #开始时间：
    print(time.strftime( '%Y-%m-%d %H:%M:%S' ,time.localtime(time.time())),'开始：')
    yuhun_tubiao()
    yuhun_dashe_tubiao()
    t=int(t)

    while t>0:
        tiaozhan()
        #************这里要做一下验证，有时候会出现bug导致无法进入御魂。**********************
        s = 10
        j = 0
        #s= checking(opencvjietu.jieshu(),yuhun_jieshu_pic_s)
        while s >= 5:
            i = 10
            jieshu = opencvjietu.jieshu()
            jieshu = opencv_compare.PIL_opencv(jieshu)  # 截取图片
            s = similarity.classify_aHash(yuhun_jieshu_pic_s, jieshu)
            #print(s)
            yysclient = opencvjietu.yys_client()
            yysclient = opencv_compare.PIL_opencv(yysclient)
            i = similarity.classify_aHash(yuhun_shibai_pic, yysclient)
            #print('i=',i)
            if i< 5:
                j = j + 1  # 统计失败次数
                t = t + 1
                print('这是第', j, '次失败！')
                break

            #设置一个队列，把s存入队列中当队列中的数值都一样时证明停留在了同一画面
            time.sleep(2)
        #******御魂结束之后的等待时间有点长应该在中间加一个点击，另外一个问题就是如果结束是正好有好友来了协作就GG结束不了**************
        a = feature_matching(yuhun_jieshu_pic)
        mousedianji.mouse_click(a[0], a[1], 0)
        t=t-1
        time.sleep(random.randint(0, 1))
        #打印基本信息
        print(time.strftime( '%Y-%m-%d %H:%M:%S' ,time.localtime(time.time())),'阴阳师大人，小助手已经帮您刷了', 500 - t, '次御魂了！总共失败', j, '次，继续加油哦！')



#注意桌面客户端每天只能刷500次御魂，300次御灵
yuhun(500)