﻿local ZhanbuExcel = require("excel.zhanbu")
local Msg = require("core.Msg")
local BagLogic = require("bag.BagLogic")
local HeroGrid = require("hero.HeroGrid")
local Util = require("common.Util")
local ObjHuman = require("core.ObjHuman")
local RoleSystemLogic = require("roleSystem.RoleSystemLogic")
local RoleSystemDefine = require("roleSystem.RoleSystemDefine")
local Lang  = require("common.Lang")
local Broadcast = require("broadcast.Broadcast")
local SysParameter = require("common.SysParameter")
local ChengjiuLogic = require("chengjiu.ChengjiuLogic")
local ChengjiuDefine = require("chengjiu.ChengjiuDefine")
local HeroExcel = require("excel.hero")
local HeroBook = require("hero.HeroBook")
local DropExchangeLogic = require("absAct.DropExchangeLogic")
local HeroGrowUp = require("absAct.HeroGrowUp")
local MengxinLogic = require("present.MengxinLogic")
local AbsZhanbuLunpanLogic = require("absAct.AbsZhanbuLunpanLogic")
local YunYingLogic = require("yunying.YunYingLogic")
local TalismanLogic = require("talisman.TalismanLogic")
local RecommendLineup = require("drawCard.RecommendLineup")
local TriggerDefine = require("trigger.TriggerDefine")
local TriggerLogic = require("trigger.TriggerLogic")

--[[
    pool ����
    hand ����
    handCnt ��������
    money ӵ�е�������
    lv ���صȼ�
    refreshCnt ˢ�´���
    ts ���߻ظ���ʱ
    isGuide �Ƿ���������
    handMaxCnt ���Ƹ���
    heartHero ����Ӣ��
]]

local CARD_POOL_MAX_CNT = 5         -- ����Ӣ������
local AUTO_GET_MONEY_MAX = 99           -- �Զ��ظ�����������
local CARD_POOL_REFRESH = 2             -- ˢ�¿������������
local AUTO_GET_MONEY_TS = 300          -- �Զ��ظ�����ʱ��
CARD_POOL = CARD_POOL or {}

--初始时hand拥有的数量
local INITIALHANDCNT = 4
--hand最大拥有的数量
local MAXHANDCNT = 7


--获取来自秘宝的加成
local function getTalismanAdd(human, addType)
    local addValue = TalismanLogic.getTalismanAdd(human, addType) or 0
    return addValue
end

local function getHandInfo(human)
    local isUnlock = false
    local extraPosList = {}
    local realyHandCnt = INITIALHANDCNT

    for i=INITIALHANDCNT+1, MAXHANDCNT do
        isUnlock = false
        if i == 5 then
            if ChengjiuLogic.checkPrivilege(human, ChengjiuDefine.PRIVILEGE_TYPE_7) then
                isUnlock = true
            end
        elseif i == 6 then
            if getTalismanAdd(human, TalismanLogic.OTHER_EFFECT_TBL.ZHANBU_POS6) > 0 then
                isUnlock = true
            end
        elseif i == 7 then
            if getTalismanAdd(human, TalismanLogic.OTHER_EFFECT_TBL.ZHANBU_POS7) > 0 then
                isUnlock = true
            end
        end

        if isUnlock then
            extraPosList[i] = i
            realyHandCnt = realyHandCnt + 1
        end
    end
    return realyHandCnt, extraPosList
end


function initAfterStart()
    for i = 1,#ZhanbuExcel.pool do
        local grade = ZhanbuExcel.pool[i].grade
        local heroID = ZhanbuExcel.pool[i].heroID
        local weight = ZhanbuExcel.pool[i].weight
        CARD_POOL[grade] = CARD_POOL[grade] or {}
        CARD_POOL[grade].weight = CARD_POOL[grade].weight or 0
        CARD_POOL[grade].weight = CARD_POOL[grade].weight + ZhanbuExcel.pool[i].weight
        CARD_POOL[grade].pool = CARD_POOL[grade].pool or {}
        CARD_POOL[grade].pool[#CARD_POOL[grade].pool + 1] = {heroID,weight,i}
    end
end


local function initDB(human)
    if human.db.zhanbu == nil then
        local maxPointsAdd = getTalismanAdd(human, TalismanLogic.OTHER_EFFECT_TBL.ZHANBU_MAX_POINTS)
        human.db.zhanbu = {}
        local zhanbu = human.db.zhanbu
        zhanbu.hand = {}
        zhanbu.handCnt = 0
        --zhanbu.handMaxCnt = 4   -- ���������Ϊ4��
        zhanbu.handMaxCnt = MAXHANDCNT  --直接改成最大值方便一点
        zhanbu.money = AUTO_GET_MONEY_MAX + maxPointsAdd
        zhanbu.lv = 1
        zhanbu.isGuide = 1
        zhanbu.refreshCnt = 0
        zhanbu.heartHero = {}
        zhanbu.pool = {}
        refreshPool(human) 
    end

    --处理老号
    if human.db.zhanbu and human.db.zhanbu.handMaxCnt < MAXHANDCNT then
        human.db.zhanbu.handMaxCnt = MAXHANDCNT
    end

    -- ���û�û��heartHero
    if human.db.zhanbu.heartHero == nil then
        human.db.zhanbu.heartHero = {}
    end

    --屏蔽这里的判断, 改为通过 getHandInfo() 获取数据后进行判断
    -- local cjPrivilege = ChengjiuLogic.checkPrivilege(human, ChengjiuDefine.PRIVILEGE_TYPE_7)
    -- if cjPrivilege then
    --     -- �Ѽ������Ȩ����Ϊ5��
    --     human.db.zhanbu.handMaxCnt = 5
    -- else
    --     if human.db.zhanbu.hand[5] ~= nil then
    --         -- ������ҵ��������������ߣ�Ĭ�ϼ���
    --         human.db.zhanbu.handMaxCnt = 5
    --     else
    --         human.db.zhanbu.handMaxCnt = 4
    --     end
    -- end
end

function refreshPool(human)
    -- ��ʼ������
    initDB(human)

    local totalWeight = 0
    local weightConfig = ZhanbuExcel.poolLv[human.db.zhanbu.lv]
    for i = 1,#weightConfig.weight do
        totalWeight = totalWeight + weightConfig.weight[i]
    end
    -- ����һ��
    local cardPoolTb = Util.copyTable(CARD_POOL)
    for i = 1,#cardPoolTb[3].pool do
        if cardPoolTb[3].pool[i][1] == human.db.zhanbu.heartHero[1] or
           cardPoolTb[3].pool[i][1] == human.db.zhanbu.heartHero[2] or
           cardPoolTb[3].pool[i][1] == human.db.zhanbu.heartHero[3] then
            local newWeight = math.floor(cardPoolTb[3].pool[i][2] * 20 / 100)
            cardPoolTb[3].weight = cardPoolTb[3].weight + newWeight
            cardPoolTb[3].pool[i][2] = cardPoolTb[3].pool[i][2] + newWeight
        end
    end 
    -- �������ſ�
    for i = 1,CARD_POOL_MAX_CNT do
        local randomGrade = math.random(1,totalWeight)
        -- ��ȡ���� R or SR or SSR
        for j = 1,#weightConfig.weight do
            if randomGrade <= weightConfig.weight[j] then
                local randomCard = math.random(1,cardPoolTb[j].weight)
                -- ��ȡ��
                for k = 1,#cardPoolTb[j].pool do
                    if randomCard <= cardPoolTb[j].pool[k][2] then
                        human.db.zhanbu.pool[i] = cardPoolTb[j].pool[k][3]
                        break
                    end
                    randomCard = randomCard - cardPoolTb[j].pool[k][2]
                end
                break
            end
            randomGrade = randomGrade - weightConfig.weight[j]
        end
    end
end

function zhanbuQuery(human)
    -- ��ʼ������
    initDB(human)

    local msgRet = Msg.gc.GC_ZHANBU_QUERY
    local len = 0
    for i = 1,CARD_POOL_MAX_CNT do
        if human.db.zhanbu.pool[i] ~= nil then
            len = len + 1
            local index = human.db.zhanbu.pool[i]
            local config = ZhanbuExcel.pool[index]
            HeroGrid.makeHeroSimpleByID(msgRet.heroPool[len].heroInfo, config.heroID)
            msgRet.heroPool[len].price = config.price
            msgRet.heroPool[len].index = i
        end
    end
    msgRet.isGuide = human.db.zhanbu.isGuide or 0
    -- ��������������������ݷŵ�������
    if human.db.zhanbu.isGuide == 1 then
        len = 0
        sysHeroList = SysParameter.getSysParameterTb(SysParameter.PARAMETER_16)
        for i = 1,#sysHeroList do
            len = len + 1
            local index = sysHeroList[i]
            local config = ZhanbuExcel.pool[index]
            human.db.zhanbu.pool[i] = index
            HeroGrid.makeHeroSimpleByID(msgRet.heroPool[len].heroInfo, config.heroID)
            msgRet.heroPool[len].price = config.price
            msgRet.heroPool[len].index = i
        end
        human.db.zhanbu.isGuide = 0
    end
    msgRet.heroPool[0] = len

    -- msgRet.isLock = 1
    -- if human.db.zhanbu.handMaxCnt == 5 then
    --     msgRet.isLock = 0
    -- end

    len = MAXHANDCNT - INITIALHANDCNT
    msgRet.isLock[0] = len
    local _, extraPosList = getHandInfo(human)

    for i=MAXHANDCNT, INITIALHANDCNT+1, -1 do
        msgRet.isLock[len] = 1
        if extraPosList[i] then
            msgRet.isLock[len] = 0
        end
        len = len - 1
    end


    len = 0
    for i = 1,human.db.zhanbu.handMaxCnt do
        if human.db.zhanbu.hand[i] ~= nil then
            len = len + 1
            local index = human.db.zhanbu.hand[i]
            local config = ZhanbuExcel.pool[index]
            HeroGrid.makeHeroSimpleByID(msgRet.hand[len].heroInfo, config.heroID)
            msgRet.hand[len].price = config.price
            msgRet.hand[len].index = i
        end
    end
    msgRet.hand[0] = len

    local maxPointsAdd = getTalismanAdd(human, TalismanLogic.OTHER_EFFECT_TBL.ZHANBU_MAX_POINTS)
    msgRet.cardRate = Util.copyTable(ZhanbuExcel.poolLv[human.db.zhanbu.lv].weight)
    msgRet.cardRate[0] = 3

    msgRet.lv = human.db.zhanbu.lv

    msgRet.money = human.db.zhanbu.money
    msgRet.moneyMax = AUTO_GET_MONEY_MAX + maxPointsAdd
    msgRet.moneyCost = 1
    msgRet.nextRecover = AUTO_GET_MONEY_TS
    msgRet.recoverTs = AUTO_GET_MONEY_TS
    if human.db.zhanbu.ts then
        local now = os.time()
        msgRet.nextRecover = human.db.zhanbu.ts + AUTO_GET_MONEY_TS - now
        if msgRet.nextRecover < 0 then
            msgRet.nextRecover = 0
        end
    end

    msgRet.hasRed = xinyiDot(human) and 1 or 0
    Msg.send(msgRet,human.fd)
end

function buyCard(human,index,heroID)
    -- �������ݶ�û�У��Ҹ�
    if human.db.zhanbu == nil or human.db.zhanbu.pool == nil then
        return
    end

    local heroIndex = human.db.zhanbu.pool[index]
    -- �������ݶԲ���
    local config = ZhanbuExcel.pool[heroIndex]
    if not config or config.heroID ~= heroID then
        return
    end
    
    -- �ж��Ƿ�ɺϳ���Ƭ
    local sameCnt = 0
    for i = 1,human.db.zhanbu.handMaxCnt do
        if human.db.zhanbu.hand[i] ~= nil and 
           human.db.zhanbu.hand[i] == heroIndex then
            sameCnt = sameCnt + 1
        end
    end
    
    -- �ж������Ƿ�
    if human.db.zhanbu.money < config.price then
        return Broadcast.sendErr(human,Lang.ABS_ITEM_ERR)
    end
    
    local msgRet = Msg.gc.GC_ZHANBU_BUY_CARD

    -- �ɺϳ���Ƭ
    local maxPointsAdd = getTalismanAdd(human, TalismanLogic.OTHER_EFFECT_TBL.ZHANBU_MAX_POINTS)
    if sameCnt >= 2 then
        -- �۳�����
        human.db.zhanbu.money = human.db.zhanbu.money - config.price
        RoleSystemLogic.onDot(human,RoleSystemDefine.ROLE_SYS_ID_502)
        
        -- �Զ��ظ���ʱ
        if human.db.zhanbu.money < AUTO_GET_MONEY_MAX + maxPointsAdd then
            human.db.zhanbu.ts = human.db.zhanbu.ts or os.time()
        end

        -- �۳�����
        for i = 1,human.db.zhanbu.handMaxCnt do
            if human.db.zhanbu.hand[i] ~= nil and 
               human.db.zhanbu.hand[i] == heroIndex then
                human.db.zhanbu.hand[i] = nil
                human.db.zhanbu.handCnt = human.db.zhanbu.handCnt - 1
            end
        end
        -- �ӿ�����ɾ��
        human.db.zhanbu.pool[index] = nil

        -- ������Ƭ
        local randomCnt = math.random(config.chipCnt[1],config.chipCnt[2])
        randomCnt = randomCnt + getTalismanAdd(human, TalismanLogic.OTHER_EFFECT_TBL.ZHANBU_FRAGMENT)

        local item = {}
        item[config.heroID] = randomCnt
        BagLogic.addItem(human, config.heroID, randomCnt, "zhanbu")
        if config.grade == 3 then
           DropExchangeLogic.drawSSR(human, item, 1)
        end

        local zhanbuActItemID,zhanbuActCnt = AbsZhanbuLunpanLogic.getZhanbuComposeCnt(human, config.grade)
        if zhanbuActCnt and zhanbuActCnt > 0 then
            item[zhanbuActItemID] = zhanbuActCnt
            BagLogic.addItem(human, zhanbuActItemID, zhanbuActCnt, "zhanbu")
        end
        BagLogic.sendItemGetList(human, item, "zhanbu")
        
        msgRet.index = 0
        
        HeroGrowUp.onCallback(human, HeroGrowUp.TASKTYPE3, 1)
        MengxinLogic.onCallBack(human,MengxinLogic.MX_TASK_TYPE_6,1)

        YunYingLogic.onCallBack(human, "ZhanBuHecheng",1)
        -- print("ZhanbuExcel....当前进入了占卜合成！！！！！！！！！！！！！！")
        TriggerLogic.PublishEvent(TriggerDefine.ZHANBU_SYNTHESIS, human.db._id, 1)
    else
        -- �ж������Ƿ�����
        -- if human.db.zhanbu.handCnt >= human.db.zhanbu.handMaxCnt then
        --     return Broadcast.sendErr(human,Lang.ZHANBU_HAND_CARD_FULL)
        -- end

        local realyHandCnt, extraPosList = getHandInfo(human)
        if human.db.zhanbu.handCnt >= realyHandCnt then
            return Broadcast.sendErr(human,Lang.ZHANBU_HAND_CARD_FULL)
        end

        -- �۳�����
        human.db.zhanbu.money = human.db.zhanbu.money - config.price
        RoleSystemLogic.onDot(human,RoleSystemDefine.ROLE_SYS_ID_502)
        
        -- �Զ��ظ���ʱ
        if human.db.zhanbu.money < AUTO_GET_MONEY_MAX + maxPointsAdd then
            human.db.zhanbu.ts = human.db.zhanbu.ts or os.time()
        end

        -- �ӿ�����ɾ��
        human.db.zhanbu.pool[index] = nil

        local bl = true
        for i = 1,human.db.zhanbu.handMaxCnt do
            if human.db.zhanbu.hand[i] == nil then
                bl = true
                if i >= INITIALHANDCNT + 1 and not extraPosList[i] then
                    bl = false
                end
                
                if bl then
                    human.db.zhanbu.handCnt = human.db.zhanbu.handCnt + 1
                    human.db.zhanbu.hand[i] = heroIndex
                    msgRet.index = i
                    break
                end
            end
        end
    end

    -- ֪ͨ�ͻ��˹���ɹ�
    Msg.send(msgRet,human.fd)

    -- ˢ�½���
    zhanbuQuery(human)

    YunYingLogic.onCallBack(human, "onZhanBuBuyCard", config.price)

end

function sellCard(human,index,heroID)
    -- �������ݶ�û�У��Ҹ�
    if human.db.zhanbu == nil or human.db.zhanbu.hand == nil then
        return
    end

    -- У�鿨�ĺϷ���
    local heroIndex = human.db.zhanbu.hand[index]
    local config = ZhanbuExcel.pool[heroIndex]
    if heroIndex == nil or config == nil then
        return
    end
    
    -- ɾ������
    human.db.zhanbu.hand[index] = nil
    human.db.zhanbu.handCnt = human.db.zhanbu.handCnt - 1

    -- ���ӵ���
    human.db.zhanbu.money = human.db.zhanbu.money + config.price

    RoleSystemLogic.onDot(human,RoleSystemDefine.ROLE_SYS_ID_502)

    -- �Զ��ظ���ʱ
    local maxPointsAdd = getTalismanAdd(human, TalismanLogic.OTHER_EFFECT_TBL.ZHANBU_MAX_POINTS)
    if human.db.zhanbu.money >= AUTO_GET_MONEY_MAX + maxPointsAdd then
        human.db.zhanbu.ts = nil
    end

    -- ֪ͨ�ͻ����۳��ɹ�
    Msg.send(Msg.gc.GC_ZHANBU_SELL_CARD,human.fd)

    -- ˢ�½���
    zhanbuQuery(human)
    YunYingLogic.onCallBack(human, "onZhanBuBuyCard", -config.price)
end

function changePos(human,posList)
    -- �������ݶ�û�У��Ҹ�
    if human.db.zhanbu == nil or human.db.zhanbu.hand == nil then
        return
    end

    -- ��������λ���Ƿ�Ϸ�
    local tb = Util.split(posList, "|")
    -- ���Ȳ��ԣ����Ϸ�
    if #tb > human.db.zhanbu.handMaxCnt then
        return
    end

    local _, extraPosList = getHandInfo(human)

    for i = 1,#tb do
        -- ���ܽ���ɾ��
        tb[i] = tonumber(tb[i])
        local pos = tb[i]

        if pos == 0 and human.db.zhanbu.hand[i] ~= nil then
            return
        end
        -- ��λ�ò��Ϸ�
        if pos > human.db.zhanbu.handMaxCnt then
            return
        end

        if pos >= INITIALHANDCNT + 1 and not extraPosList[pos] then
            return
        end
    end

    -- ����λ��
    local dataList = Util.copyTable(human.db.zhanbu.hand)
    human.db.zhanbu.hand = {}
    for i = 1,human.db.zhanbu.handMaxCnt do
        if tb[i] and tb[i] ~= 0 then
            human.db.zhanbu.hand[tb[i]] = dataList[i]
        end
    end
    
    zhanbuQuery(human)
end

function refreshCardPool(human)
    -- �������ݶ�û�У��Ҹ�
    if human.db.zhanbu == nil or human.db.zhanbu.hand == nil then
        return
    end

    -- У����ϲ���
    if human.db.zhanbu.money < CARD_POOL_REFRESH then
        return Broadcast.sendErr(human,Lang.ABS_ITEM_ERR)
    end

    -- �۳�����
    human.db.zhanbu.money = human.db.zhanbu.money - CARD_POOL_REFRESH
    
    RoleSystemLogic.onDot(human,RoleSystemDefine.ROLE_SYS_ID_502)
    
    -- �Զ��ظ���ʱ
    local maxPointsAdd = getTalismanAdd(human, TalismanLogic.OTHER_EFFECT_TBL.ZHANBU_MAX_POINTS)
    if human.db.zhanbu.money < AUTO_GET_MONEY_MAX + maxPointsAdd then
        human.db.zhanbu.ts = human.db.zhanbu.ts or os.time()
    end

    -- ˢ��
    refreshPool(human)
    
    -- ˢ�³ɹ�������+1
    onLv(human)

    -- ˢ�½���
    zhanbuQuery(human)

    --ռ�����̻ ˢ�º�ص�
    AbsZhanbuLunpanLogic.onZhanbuRefresh(human)
    YunYingLogic.onCallBack(human, "onZhanBuBuyCard", CARD_POOL_REFRESH)
end


local function populateHeroMsg(net, heroId)
    net.heroId = heroId or 0
    net.heroCamp = 0
    net.heroName = ""
    local heroConfig = HeroExcel.hero[heroId]
    if heroConfig then
        net.heroCamp = heroConfig.camp
        net.heroName = heroConfig.name
    end
end


-- ����Ӣ��
function zhanbuHeartHeroQuery(human)
    -- �������ݶ�û�У��Ҹ�
    if human.db.zhanbu == nil or human.db.zhanbu.hand == nil then
        return
    end
    
    local msgRet = Msg.gc.GC_ZHANBU_HEART_HERO_QUERY
    -- for i = 1,3 do
    --     local heroID = human.db.zhanbu.heartHero[i]
    --     msgRet.heroInfo[i] = heroID or 0
    -- end
    -- msgRet.heroInfo[0] = 3
    -- local len = 0
    -- for heroID,v in pairs(human.db.heroBook) do
    --     local heroConfig = HeroExcel.hero[heroID]
    --     if heroConfig and heroConfig.grade >= 4 then
    --         len = len + 1
    --         msgRet.heroList[len] = heroID
    --     end
    -- end
    -- msgRet.heroList[0] = len
    -- Msg.send(msgRet,human.fd)



    msgRet.isEnd = 0
    msgRet.isStart = 1
    msgRet.heroInfo[0] = 3
    msgRet.heroList[0] = 0

    for i = 1,3 do
        local heroID = human.db.zhanbu.heartHero[i]
        populateHeroMsg(msgRet.heroInfo[i], heroID)
    end

    local heroIdArr = {}
    for heroID in pairs(human.db.heroBook) do
        local heroConfig = HeroExcel.hero[heroID]
        if heroConfig and heroConfig.grade >= 4 then
            heroIdArr[#heroIdArr+1] = heroID
        end
    end

    local heroNum = #heroIdArr
    local len, msgOnceLen = 0, 20

    for _, heroId in ipairs(heroIdArr) do
        len = len + 1
        msgRet.heroList[0] = len
        populateHeroMsg(msgRet.heroList[len], heroId)

        if len >= msgOnceLen then
            heroNum = heroNum - len
            if heroNum <= 0 then
                msgRet.isEnd = 1
                return Msg.send(msgRet,human.fd)
            end

            Msg.send(msgRet,human.fd)
            len = 0
            msgRet.isStart = 0
        end
    end

    if len > 0 then
        msgRet.isEnd = 1
        Msg.send(msgRet,human.fd)
    end

end

-- ��������Ӣ��
function setHeartHero(human,heroList)
    -- �������ݶ�û�У��Ҹ�
    if human.db.zhanbu == nil or human.db.zhanbu.hand == nil then
        return
    end

    -- ��������λ���Ƿ�Ϸ�
    local tb = Util.split(heroList, "|")

    -- ���Ȳ��ԣ����Ϸ�
    local len = #tb
    if len > 3 then
        return
    end

    -- У��Ӣ���Ƿ���ͼ���У��Ƿ���SSR
    for i  = 1,len do
        tb[i] = tonumber(tb[i])
        local heroID = tb[i]
        if heroID ~= 0 then
            local heroConfig = HeroExcel.hero[heroID]
            -- �����Ѽ���ͼ���У�����
            if not HeroBook.isGet(human, heroID,heroConfig.star) then
                return
            end

            -- ����SSR������
            if heroConfig.grade < 4 then
                return
            end
        end
    end

    -- �������������
    human.db.zhanbu.heartHero = {}
    for i  = 1,len do
        if tb[i] ~= 0 then
            human.db.zhanbu.heartHero[i] = tb[i]
        end
    end

    -- ˢ������Ӣ������
    zhanbuHeartHeroQuery(human)
end


function onLv(human)
    human.db.zhanbu.refreshCnt = human.db.zhanbu.refreshCnt + 1
    if human.db.zhanbu.refreshCnt >= ZhanbuExcel.poolLv[human.db.zhanbu.lv].cnt and 
       human.db.zhanbu.lv < #ZhanbuExcel.poolLv then
        human.db.zhanbu.lv = human.db.zhanbu.lv + 1
        local msgRet = Msg.gc.GC_ZHANBU_LVUP
        msgRet.cardRate = Util.copyTable(ZhanbuExcel.poolLv[human.db.zhanbu.lv].weight)
        msgRet.cardRate[0] = 3
        msgRet.lv = human.db.zhanbu.lv
        Msg.send(msgRet,human.fd)
    end
end

function checkRecover()
    local now = os.time()
    for _,human in pairs(ObjHuman.onlineUuid) do
        if human.db.zhanbu and human.db.zhanbu.ts then
            local maxPointsAdd = getTalismanAdd(human, TalismanLogic.OTHER_EFFECT_TBL.ZHANBU_MAX_POINTS)
            local finalMaxPoints = maxPointsAdd + AUTO_GET_MONEY_MAX
            local ts = now - human.db.zhanbu.ts
            local cnt = math.floor(ts/AUTO_GET_MONEY_TS)
            if cnt >= 1 and human.db.zhanbu.money < finalMaxPoints  then
                human.db.zhanbu.money = human.db.zhanbu.money + cnt
                human.db.zhanbu.ts = human.db.zhanbu.ts + AUTO_GET_MONEY_TS * cnt
                if human.db.zhanbu.money >= finalMaxPoints then
                    human.db.zhanbu.ts = nil
                    human.db.zhanbu.money = finalMaxPoints
                end
                RoleSystemLogic.onDot(human,RoleSystemDefine.ROLE_SYS_ID_502)
            elseif human.db.zhanbu.money >= finalMaxPoints then
                human.db.zhanbu.ts = nil
            end
        end
    end
end

function isDot(human)
    if human.db.lv < 14 then
        return
    end
    initDB(human)
    if human.db.zhanbu and human.db.zhanbu.money >= 30 then
        return true
    end

    if xinyiDot(human) then
        return true
    end

    return 
end

function updateDaily(human)
    if human.db.zhanbu == nil then
        return
    end
    human.db.zhanbu.lv = 1
    human.db.zhanbu.refreshCnt = 0

    if human.db.zhanbu and human.db.zhanbu.money then
        local ts = human.db.zhanbu.ts
        local maxPointsAdd = getTalismanAdd(human, TalismanLogic.OTHER_EFFECT_TBL.ZHANBU_MAX_POINTS)
        if not ts and human.db.zhanbu.money < AUTO_GET_MONEY_MAX + maxPointsAdd then
            human.db.zhanbu.ts = os.time()
        end
    end
end

function onLogin(human)
    --处理mone达到老的上限值后, human.db.zhanbu.ts为nil,导致后面获得秘宝时,无法获得秘宝加成情况
    if human.db.zhanbu and human.db.zhanbu.money then
        local ts = human.db.zhanbu.ts
        local maxPointsAdd = getTalismanAdd(human, TalismanLogic.OTHER_EFFECT_TBL.ZHANBU_MAX_POINTS)
        if not ts and human.db.zhanbu.money < AUTO_GET_MONEY_MAX + maxPointsAdd then
            human.db.zhanbu.ts = os.time()
        end
    end

    RecommendLineup.RecommendLineup_UpDate(human)
end



function addZhanbuItem(human,cnt)
    -- ��ʼ������
    initDB(human)

    human.db.zhanbu.money = human.db.zhanbu.money + cnt
end

function xinyiDot(human)
    if human.db.heroBook == nil then return end
    local count = 0
    for k ,v in pairs(human.db.heroBook) do
        if type(k) == "number" then
            if HeroExcel.hero[k].grade >= 4 then
                count = count + 1
            end 
        end
    end
    local xinyiCount = 0
    for i = 1, 3 do
        if human.db.zhanbu.heartHero[i] ~= nil then
            xinyiCount = xinyiCount + 1 
        end
    end
    if xinyiCount < count and xinyiCount < 3 then
        return true
    end
end