import random
from numpy import random as r


userperf = [0.7, 0.2, 0.03, 0.03, 0.04]
typeNum = 5
type1 = [0.7, 0.2, 0.03, 0.03, 0.04]
type2 = [0.2, 0.7, 0.03, 0.03, 0.04]
type3 = [0.03, 0.2, 0.7, 0.03, 0.04]
type4 = [0.03, 0.2, 0.03, 0.7, 0.04]
type5 = [0.04, 0.2, 0.03, 0.03, 0.7]
class UserForUAV(object):

    def __init__(self, UserId, TotalContentNum, FalvorNum, K):
        self.UserId = UserId                     # 用户索引
        self.TotalContentNum = TotalContentNum   # 文件集合总数
        self.FalvorNum = FalvorNum               # 该用户倾向选择的文件数量

        self.PlaceX = 0                          # 用户水平位置x
        self.PlaceY = 0                          # 用户水平位置y

        self.K = K
        self.RequestIndex = 0

        self.Service = []

        self.eachNum = TotalContentNum / typeNum

        if FalvorNum > TotalContentNum:
            self.FalvorNum = TotalContentNum

        self.FalvorList = random.sample(range(1, 9), FalvorNum)    # 该用户倾向选择的文件

    # 按照zipf分布，模拟用户请求
    def genRequestIndex(self):
        x = r.zipf(a=2, size=1)[0]
        x = x - 1
        if x >= len(self.FalvorList):
            self.RequestIndex = self.FalvorList[len(self.FalvorList)-1]
        else:
            self.RequestIndex = self.FalvorList[x]
        return self.RequestIndex

        # 按照zipf分布，模拟用户请求

    def genRequestIndex(self):
        x = r.zipf(a=2, size=1)[0]
        x = x - 1
        if x >= len(self.FalvorList):
            self.RequestIndex = self.FalvorList[len(self.FalvorList) - 1]
        else:
            self.RequestIndex = self.FalvorList[x]
        return self.RequestIndex

    def getRequestIndex(self):
        return self.RequestIndex

    # 移动用户位置
    def moveTo(self,x, y):
        self.PlaceX = x
        self.PlaceY = y

    def setUAVList(self, UAVList):
        self.UAVList = UAVList

    def tryGetservice(self):

        for i in range(0, len(self.UAVList)):
            self.UAVList[i].tryGetService(self.PlaceX, self.PlaceY, self.RequestIndex, self.UserId)

    def addUAV(self, index):
        self.Service.append(index)

    def clearService(self):
        self.Service.clear()

    def chooseService(self):
        dict = {}
        for i in range(0, len(self.Service)):
            dist = self.UAVList[self.Service[i]].getDist(self.PlaceX, self.PlaceY)
            dict[self.Service[i]] = dist
        a = sorted(dict.items(),key = lambda x:x[1],reverse = False)
        realService = []
        for key in a[:self.K]:
            realService.append(key[0])
        self.Service = realService

    def getService(self):
        return self.Service


    def tryGetCache(self, transSpeedBaseLine, limitedTime):
        vehiclesAbleToTrans = 0
        self.genRequestIndex()
        get = 0
        time = 0
        miss = 0
        time = []
        for i in range(0, len(self.UAVList)):
            canGet, tmp = self.UAVList[i].tryGetContent(self.PlaceX, self.PlaceY, limitedTime, self.UserId, transSpeedBaseLine,  self.RequestIndex)

            if canGet:
                vehiclesAbleToTrans = vehiclesAbleToTrans + 1
            if tmp != -1:
             #   print("uav")
             #   print(tmp)
                time.append(tmp)
                get = get + 1
        if self.K > get:
            miss = self.K - get

        if get == 0:
            return vehiclesAbleToTrans, 0, miss, self.RequestIndex
        time.sort()
        if get >= self.K:
            return vehiclesAbleToTrans, time[self.K - 1], miss, self.RequestIndex

        return vehiclesAbleToTrans, 0, miss, self.RequestIndex



ss = []
for i in range(0,100):
    ss.append(r.zipf(1.5,10)[0])
print(r.zipf(1.6,100))
