--------------------------------
-- 文件名       :   jjcNewLadderLogic.lua
-- 文件说明     :   天梯赛-管理器
-- 创建时间     :   2025/05/07
-- 创建人       :   FC
--------------------------------

local Util = require("common.Util")
local Lang = require("common.Lang")
local CommonDB = require("common.CommonDB")
local JjcExcel = require("excel.jjc")
local MonsterExcel = require("excel.monster")
local MailExcel = require("excel.mail")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local RoleDBLogic = require("role.RoleDBLogic")
local Config = require("Config")
local ItemDefine = require("bag.ItemDefine")
local Grid = require("bag.Grid")
local CombatVideo = require("combat.CombatVideo")
local RoleLogic = require("role.RoleLogic")
local JjcNewLadderDefine = require("jjcnewladder.jjcNewLadderDefine")
local JjcNewLadderMiddle = require("jjcnewladder.jjcNewLadderMiddle")
local JjcNewLadderDB = require("jjcnewladder.jjcNewLadderDB")
local BagLogic = require("bag.BagLogic")
local MiddleCommonLogic = require("middle.MiddleCommonLogic")
local CombatDefine = require("combat.CombatDefine")
local Broadcast = require("broadcast.Broadcast")
local CombatLogic = require("combat.CombatLogic")
local WarReportLogic = require("warReport.WarReportLogic")
local MailManager = require("mail.MailManager")
local Timer = require("core.Timer")
local JjcActLogic = require("jjc.JjcActLogic")
local CommonDefine = require("common.CommonDefine")
local Log = require("common.Log")
local RoleSystemLogic = require("roleSystem.RoleSystemLogic")
local RoleSystemDefine = require("roleSystem.RoleSystemDefine")
local WarOrder               = require("shop.WarOrder")


-- 请求加入天梯赛的玩家
local tQueryJoinPlayer = {}

-- 请求天梯赛前三排名的数据的玩家
local tQueryLast3Rank = {}
local bQueryLast = false

-- 上一轮天梯赛前三的排名玩家
local tLast3RankPlayerInfo = nil

-- 排行榜数据
local tCacheRankInfo = {
    -- nNextUpdateTime -- 更新时间
    -- tRankList = {}
}
local bQueryRank = nil

-- 缓存请求排行榜的玩家
local tCacheQueryRankPlaeyer = {}

------------------------------------- 内部判断 ----------------------------------------------------
-- 判断是否开启
local function JjcNewLadderLogic_CheckIsOpen()
    local nNowTime = os.time()
    local nEndTime = CommonDB.GetJjcLadderEndTime()
    print("[JjcNewLadderLogic_CheckIsOpen] nNowTime = "..nNowTime.." nEndTime = "..nEndTime)
    local tDate = os.date("*t",nNowTime)
    if 0 >= nEndTime then
        if tDate.wday >= JjcNewLadderDefine.JJC_NEWLADDER_OPENDAY and JjcNewLadderDefine.JJC_NEWLADDER_ENDDAY >= tDate.wday then
            if JjcNewLadderDefine.JJC_NEWLADDER_OPENDAY == tDate.wday then
                if tDate.hour >= JjcNewLadderDefine.JJC_NEWLADDER_ENDTIME then
                    return false
                end
            end

            if JjcNewLadderDefine.JJC_NEWLADDER_OPENDAY == tDate.wday and
                    tDate.hour == JjcNewLadderDefine.JJC_NEWLADDER_OPENTIME and tDate.min < 11 then
                return false
            end

            return true
        end

        return false
    else
        if JjcNewLadderDefine.JJC_NEWLADDER_OPENDAY == tDate.wday and JjcNewLadderDefine.JJC_NEWLADDER_OPENTIME == tDate.hour
            and tDate.min < 10 then
            return false
        end

        return nEndTime > nNowTime
    end
end

local function JjcNewLadderLogic_BaseCheck(human)
    return RoleSystemLogic.isOpen(human, RoleSystemDefine.ROLE_SYS_ID_1304)
end


-- 写日志
local function JjcNewLadderLogic_WriteLog(szText)
    Log.write(Log.LOGID_OSS_JJCNEWLADDER, szText)
end

-- 获取剩余时间
local function JjcNewLadder_GetLeftTime()
    local nNowTime = os.time()
    local nEndTime = CommonDB.GetJjcLadderEndTime()
    if not nEndTime or nEndTime < nNowTime then
        return 0
    end

    return nEndTime - nNowTime
end

-- 初始化玩家DB
local function JjcNewLadderLogic_CreatDB(human)
    human.db.JjcNewLadder = 
    {
        nRefresh = os.time(),   -- 免费刷新时间
        nFreeNum = 5,        -- 免费挑战次数
        nPayNum = 0,         -- 购买挑战次数
        bJoin = false,
        nRank = -1,
        nPoint = 0,     -- 积分
        tEnemy = nil,   -- 对战敌方数据
        recordList = nil,   -- 对战记录
        tWorship = nil,     -- 点赞记录
        nLastEndTime = CommonDB.GetJjcLadderEndTime(), -- 上一次活动结束时间
    }
end

-- 获取玩家上一次活动结束时间
local function JjcNewLadder_GetHumanLastEndTime(human)
    return human.db.JjcNewLadder.nLastEndTime
end

-- 设置玩家上一次活动结束时间
local function JjcNewLadder_SetHumanLastEndTime(human, nValue)
    human.db.JjcNewLadder.nLastEndTime = nValue
end

-- 获取玩家是否参加了天梯赛
local function JjcNewLadder_GetHumanJoin(human)
    return human.db.JjcNewLadder.bJoin
end

-- 设置玩家是否参加了天梯赛
local function JjcNewLadder_SetHumanJoin(human, nValue)
    human.db.JjcNewLadder.bJoin = nValue
end

-- 获取玩家敌方数据
local function JjcNewLadder_GetHumanEnemy(human)
    return human.db.JjcNewLadder.tEnemy
end

-- 设置玩家敌方数据
local function JjcNewLadder_SetHumanEnemy(human, tEnemy)
    human.db.JjcNewLadder.tEnemy = tEnemy
end

-- 获取玩家排名数据
local function JjcNewLadder_GetHumanRank(human)
    return human.db.JjcNewLadder.nRank
end

-- 设置玩家排名数据
local function JjcNewLadder_SetHumanRank(human, nValue)
    human.db.JjcNewLadder.nRank = nValue
end

-- 获取玩家积分数据
local function JjcNewLadder_GetHumanPoint(human)
    return human.db.JjcNewLadder.nPoint
end

-- 设置玩家积分数据
local function JjcNewLadder_SetHumanPoint(human, nValue)
    human.db.JjcNewLadder.nPoint = nValue
end

-- 获取玩家免费刷新时间
local function JjcNewLadder_GetHumanRefreshTime(human)
    return human.db.JjcNewLadder.nRefresh
end

-- 设置玩家免费刷新时间
local function JjcNewLadder_SetHumanRefreshTime(human, nValue)
    human.db.JjcNewLadder.nRefresh = nValue
end

-- 获取玩家免费挑战次数
local function JjcNewLadder_GetHumanFreeNum(human)
    return human.db.JjcNewLadder.nFreeNum
end

-- 设置玩家免费挑战次数
local function JjcNewLadder_SetHumanFreeNum(human, nValue)
    human.db.JjcNewLadder.nFreeNum = human.db.JjcNewLadder.nFreeNum + nValue
end

-- 获取玩家付费挑战次数
local function JjcNewLadder_GetHumanPayNum(human)
    return human.db.JjcNewLadder.nPayNum
end

-- 设置玩家付费挑战次数
local function JjcNewLadder_SetHumanPayNumNum(human, nValue)
    human.db.JjcNewLadder.nPayNum = human.db.JjcNewLadder.nPayNum + nValue
end

-- 获取玩家所有挑战次数
local function JjcNewLadder_GetAllFightNum(human)
    return JjcNewLadder_GetHumanFreeNum(human) + JjcNewLadder_GetHumanPayNum(human)
end

-- 获取对战记录
local function JjcNewLadder_GetRecordList(human)
    return human.db.JjcNewLadder.recordList
end

-- 设置对战记录
local function JjcNewLadder_SetRecordList(human, videoUuid)
    if not human.db.JjcNewLadder.recordList then
        human.db.JjcNewLadder.recordList = {}
    end

    table.insert(human.db.JjcNewLadder.recordList, {videoUuid = videoUuid, time = os.time()})   
end

-- 尝试删除对战记录的视频
local function JjcNewLadder_TryDelRecordList(human)
    local tRecordList = JjcNewLadder_GetRecordList(human)
    if nil == tRecordList then
        return
    end

    local nLen = #tRecordList
    if JjcNewLadderDefine.JJC_NEWLADDER_SAVE_RECORDLIST_LEN >= nLen then
        return
    end

    local nDelLen = nLen - JjcNewLadderDefine.JJC_NEWLADDER_SAVE_RECORDLIST_LEN
    local nAllLen = #tRecordList
    for i = nDelLen, 1, -1 do
        local tData = tRecordList[nAllLen]
        CombatVideo.removeCombatVideo(tData.videoUuid)
        table.remove(tRecordList, nAllLen)
        nAllLen = nAllLen - 1
    end
end

-- 获取是否加入天梯赛
local function JjcNewLadderLogic_GetJoin(human)
    return human.db.JjcNewLadder.bJoin
end

-- 设置是否加入天梯赛
local function JjcNewLadderLogic_SetJoin(human, nValue)
    human.db.JjcNewLadder.bJoin = nValue
end

-- 获取是否点赞
local function JjcNewLadderLogic_GetWorShip(human, uuid)
    if not human.db.JjcNewLadder.tWorship or not human.db.JjcNewLadder.tWorship[uuid] then
        return JjcNewLadderDefine.JJC_NEWLADDER_NOWORSHIP
    end

    return human.db.JjcNewLadder.tWorship[uuid]
end

-- 设置是否点赞
local function JjcNewLadderLogic_SetWorShip(human, uuid, nValue)
    if not human.db.JjcNewLadder.tWorship  then
        human.db.JjcNewLadder.tWorship = {}
    end

    human.db.JjcNewLadder.tWorship[uuid] = nValue
end

-- 获取对应uuid的一些数据
local function JjcNewLadderLogic_GetInfoByUUID(human, uuid, nType)
    local tPlayInfo = nil
    if nType == JjcNewLadderDefine.JJC_NEWLADDER_QUERYUID_ENEMY_TYPE then
        local tEnemy = JjcNewLadder_GetHumanEnemy(human)
        if not tEnemy or nil == _G.next(tEnemy) then
            print("[JjcNewLadderLogic_GetInfoByUUID] 不存在对应的对战列表")
            return tPlayInfo
        end

        for _, v in pairs(tEnemy) do
            print("[JjcNewLadderLogic_GetInfoByUUID] 进行判断的 uuid = "..v.uuid.." nType = "..type(v.uuid))
            if tostring(v.uuid) == uuid then
                tPlayInfo = v
                print("[JjcNewLadderLogic_GetInfoByUUID] 寻找到了对应的假人数据")
                return tPlayInfo
            end
        end

        print("[JjcNewLadderLogic_GetInfoByUUID] 不存在对应的数据")
    else
        for i = 1, JjcNewLadderDefine.JJC_NEWLADDER_RANK_SEND_LEN, 1 do
            local tRankData = tCacheRankInfo.tRankList[i]
            if not tRankData then
                return tPlayInfo
            end

            if tRankData.uuid == uuid then
                tPlayInfo = tRankData
                return tPlayInfo
            end
        end
    end

    return tPlayInfo
end

-- 请求加入天梯赛
local function JjcNewLadderLogic_JoinLadder(human)
    JjcNewLadderMiddle.JjcNewLadderMiddle_JoinLadder(human)
end

-- 进行匹配对手
local function JjcNewLadderLogic_MatchRival(uuid)
    return JjcNewLadderDB.JjcNewLadderDB_MatchRival(uuid)
end

-- 获取对应uid的DB数据
local function JjcNewLadderLogic_GetDataByuid(uuid)
    return JjcNewLadderDB.JjcNewLadderDB_GetDBDataByuid(uuid)
end

-- 组装下发的主界面数据
local function JjcNewLadder_GetInterfaceInfo(human)
    local tMsgData = Msg.gc.GC_JJC_NEW_LADDER_QUERY
    tMsgData.ownRank = JjcNewLadder_GetHumanRank(human)
    tMsgData.ownJifen = JjcNewLadder_GetHumanPoint(human)
    tMsgData.atkZDL = human.db.zhandouli
    tMsgData.leftSec = JjcNewLadder_GetLeftTime()
    tMsgData.challengeCnt = JjcNewLadder_GetAllFightNum(human)
    tMsgData.isQuick = human.db.combatQuick[CombatDefine.COMBAT_TYPE15] or 0

    Grid.makeItem(tMsgData.challengeCostItem,ItemDefine.ITEM_JJC_TICKET_ID, JjcNewLadderDefine.JJC_NEWLADDER_COST_ITEM_NUM)

    -- 红点
    tMsgData.dotList[0] = 3
    tMsgData.dotList[1] = tMsgData.challengeCnt > 0 and 1 or 0
    tMsgData.dotList[2] = 0
    tMsgData.dotList[3] = 0

    -- 敌人
    local tEnemy = JjcNewLadder_GetHumanEnemy(human)
    if not tEnemy then
        tMsgData.tEnemy[0] = 0
    else
        tMsgData.tEnemy[0] = 5
        local nIndex = 0
        for _, v in pairs(tEnemy) do
            nIndex = nIndex + 1
            local tData = tMsgData.tEnemy[nIndex]
            tData.head = v.head
            tData.headFrame = v.headFrame or 0
            tData.nPower = v.zhandouli
            tData.name = v.name
            tData.nPoint = v.point
            tData.uuid = tostring(v.uuid)
            tData.szServerName = v.szServerName or Config.NEW_SVR_INDEX.."区"
            tData.nRank = v.nRank
        end
        -- table.print_lua_table(tMsgData.tEnemy)
        -- print("[JjcNewLadder_GetInterfaceInfo] 打印封装的数据完成")
    end

    Msg.send(tMsgData, human.fd)
    print("[JjcNewLadder_GetInterfaceInfo] 发送协议完成")
end

-- 发送对手数据
local function JjcNewLadder_SendEnemy(human)
    local tMsgData = Msg.gc.GC_JJC_NEW_LADDER_TARGET_FIND
    tMsgData.leftFreeCnt = JjcNewLadder_GetHumanFreeNum(human)
    local tEnemy = JjcNewLadder_GetHumanEnemy(human)
    if not tEnemy then
        tMsgData.targetList[0] = 0
    else
        tMsgData.targetList[0] = 5
        local nIndex = 0
        for _, v in pairs(tEnemy) do
            nIndex = nIndex + 1
            local tData = tMsgData.targetList[nIndex]
            tData.head = v.head
            tData.headFrame = v.headFrame or 0
            tData.nPower = v.zhandouli
            tData.name = v.name
            tData.nPoint = v.point
            tData.uuid = tostring(v.uuid)
            tData.szServerName = v.szServerName or Config.NEW_SVR_INDEX.."区"
            tData.nRank = v.nRank
        end
    end

    Msg.send(tMsgData, human.fd)
end

-- 发送上一轮前3名数据
local function JjcNewLadder_SendLast3Rank(human)
    local tMsgData = Msg.gc.GC_JJC_NEW_LADDER_QUERY_LAST_RANK
    tMsgData.tList[0] = 0
    table.print_lua_table(tLast3RankPlayerInfo)
    if tLast3RankPlayerInfo and tLast3RankPlayerInfo.tLastRankInfo and 
        nil ~= _G.next(tLast3RankPlayerInfo.tLastRankInfo) then
        local nIndex = 0
        for _, v in pairs(tLast3RankPlayerInfo.tLastRankInfo) do
            nIndex = nIndex + 1
            local tData = tMsgData.tList[nIndex]
            tData.head = v.head
            tData.headFrame = v.headFrame or 0
            tData.name = v.name
            tData.szServerName = v.szServerName
            tData.uuid = v.uuid
            tData.worshipCnt = v.worshipCnt
            tData.worshipState = JjcNewLadderLogic_GetWorShip(human, v.uuid)
        end
        --tMsgData.tList[0] = nIndex
    end

    Msg.send(tMsgData, human.fd)
    print("[JjcNewLadder_SendLast3Rank] 发送上一轮数据完成")
end

-- 发送排行榜数据
local function JjcNewLadder_SendRankInfo(human)
    local tMsgData = Msg.gc.GC_JJC_NEW_LADDER_RANK_QUERY
    tMsgData.ownRank.nRank = JjcNewLadder_GetHumanRank(human)
    tMsgData.ownRank.head = human.db.head
    tMsgData.ownRank.headFrame = human.db.headFrame or 0
    tMsgData.ownRank.nPower = human.db.zhandouli
    tMsgData.ownRank.name = human.db.name
    tMsgData.ownRank.szServerName = Config.NEW_SVR_INDEX.."区"
    tMsgData.ownRank.uuid = human.db._id

    tMsgData.topList[0] = JjcNewLadderDefine.JJC_NEWLADDER_RANK_SEND_LEN
    for i = 1, JjcNewLadderDefine.JJC_NEWLADDER_RANK_SEND_LEN, 1 do
        local tRankData = tCacheRankInfo.tRankList[i]
        if tRankData then
            local tData = tMsgData.topList[i]
            tData.nRank = i
            tData.head = tRankData.head
            tData.headFrame = tRankData.headFrame or 0
            tData.nPower = tRankData.zhandouli
            tData.name = tRankData.name
            tData.uuid = tostring(tRankData.uuid)
            -- print("[JjcNewLadder_SendRankInfo] nRank = "..i.." uuid = "..tRankData.uuid.." type1 = "..type(tRankData.uuid).." type2 = "..type(tData.uuid))
            tData.szServerName = tRankData.szServerName
        else
            print("[JjcNewLadder_SendRankInfo] 不存在对应排名对应的排行榜数据 i = "..i)
            return
        end
    end

    print("[JjcNewLadder_SendRankInfo] 开始下发数据")
    Msg.send(tMsgData, human.fd)
    print("[JjcNewLadder_SendRankInfo] 结束下发数据")
end

-- 保存对战录像和数据
local function JjcNewLadder_SaveRecord(human, combatInfo)
    -- 添加录像
    local combatVideo = CombatVideo.createCombatVideo(CombatVideo.VIDEOTYPE_JJCLODDER, combatInfo, human.db._id)
    local videoUuid = combatVideo._id

    JjcNewLadder_TryDelRecordList(human)

    JjcNewLadder_SetRecordList(human, videoUuid)
end

-- 发送战败邮件(真人)
local function JjcNewLadder_SendFailMail(uuidDes, nServerID, SrcServerName, SrcName, nNewRank)
    JjcNewLadderMiddle.JjcNewLadderMiddle_SendFailMail(uuidDes, nServerID, SrcServerName, SrcName, nNewRank)
end

-- 获取当前玩家对战列表缓存的uuid
local function JjcNewLaader_GetAllEnemyUID(human)
    local tEnemy = JjcNewLadder_GetHumanEnemy(human)
    if not tEnemy then
        return nil
    end

    local tEnemyUid = {}
    for _, v in pairs(tEnemy) do
        table.insert(tEnemyUid, v.uuid)
    end

    return tEnemyUid
end

-- 获取 对战显示基础数据(JJC_NEW_LADDER_BASE_ENEMY) 敌方或排行榜
local function JjcNewLaader_GetBaseInfo(tData, tBaseData)
    tData.head = tBaseData.head
    tData.headFrame = tBaseData.headFrame or 0
    tData.nPower = tBaseData.zhandouli
    tData.name = tBaseData.name
    tData.nPoint = tBaseData.point
    tData.uuid = tostring(tBaseData.uuid)
    tData.szServerName = tBaseData.szServerName or Config.NEW_SVR_INDEX.."区"
    tData.nRank = tBaseData.nRank
end

local function JjcNewLaader_GetHumanBaseInfo(tData, human)
    tData.head = human.db.head
    tData.headFrame = human.db.headFrame or 0
    tData.nPower = human.db.zhandouli
    tData.name = human.db.name
    tData.nPoint = JjcNewLadder_GetHumanPoint(human)
    tData.uuid = tostring(human.db._id)
    tData.szServerName = Config.NEW_SVR_INDEX.."区"
    tData.nRank = JjcNewLadder_GetHumanRank(human)
end

-- 弹不在活动提示
local function JjcNewLadder_PopNoOpenTips(human)
    Broadcast.sendErr(human, Lang.JJCNEWLADDERNOOPEN)
end

---------------------------------- 内部调用 ----------------------------------------------
-- 请求加入天梯赛(数据服)
function JjcNewLadderLogic_JoinHuman(tData)
    local bRet = JjcNewLadderDB.JjcNewLadderDB_AddHuman(tData)
    if false == bRet then
        print("[JjcNewLadderLogic_JoinHuman] 加入DB数据失败")
        return
    end

    print("[JjcNewLadderLogic_JoinHuman] 请求加入天梯赛 ")
    local nNowRank = JjcNewLadderDB.JjcNewLadderDB_GetRankByuid(tData.uuid)
    local nPoint = JjcNewLadderDB.JjcNewLadderDB_GetPointByuid(tData.uuid)
    if not nNowRank or 0 >= nPoint then
        print("[JjcNewLadderLogic_JoinHuman] 加入DB后还是不存在对应的排名值 uuid = "..tData.uuid)
        return
    end

    print("[JjcNewLadderLogic_JoinHuman] 请求加入天梯赛 name = "..tData.name.." nNowRank = "..nNowRank)

    local tRival = JjcNewLadderLogic_MatchRival(tData.uuid)
    if nil == tRival then
        print("[JjcNewLadderLogic_JoinHuman] 获取匹配对手的uid失败")
        return
    end

    print("[JjcNewLadderLogic_JoinHuman] 匹配对战对象成功 name = "..tData.name.." nNowRank = "..nNowRank)

    local tRivalData = {}
    for nRank, v in pairs(tRival) do
        local tJjcData = JjcNewLadderDB.JjcNewLadderDB_GetDBDataByRank(nRank)
        if nil == tJjcData then
            print("[JjcNewLadderLogic_JoinHuman] 获取不到对应的DB数据 nRank = "..nRank)
            return
        end

        local tSendData = {
            name = tJjcData.name,
            head = tJjcData.head,
            headFrame = tJjcData.headFrame or 0,
            uuid = tJjcData.uuid,
            nIsHuman = tJjcData.nIsHuman,
            nRank = nRank,
            zhandouli = tJjcData.zhandouli,
            point = tJjcData.point,
            nServerID = tJjcData.nServerID or Config.SVR_INDEX,
            monsterOutID = tJjcData.monsterOutID or 0,
            szServerName = tJjcData.szServerName
        }

        table.insert(tRivalData, tSendData)
    end

    print("[JjcNewLadderLogic_JoinHuman] 开始发送回包 name = "..tData.name.." nNowRank = "..nNowRank)
    JjcNewLadderMiddle.JjcNewLadderMiddle_JoinLadder_D2C(tData.uuid, tData.nSrcServerID, nNowRank, tRivalData, nPoint)
end

-- 加入天梯赛回包(普通服)
function JjcNewLadderLogic_JoinHumanOk(tData)
    print("[JjcNewLadderLogic_JoinHumanOk] 加入天梯赛回包(普通服) 收到回包")
    table.print_lua_table(tData)
    local uuid, nRank, nPoint, tEnemy = tData.uuid, tData.nRank, tData.nPoint, tData.tEnemy
    local human = ObjHuman.onlineUuid[uuid]
    if not human then
        human = {}
        human.db = RoleDBLogic.getDb(uuid)
        human.db.JjcNewLadder.bJoin = true
        human.db.JjcNewLadder.nRank = nRank
        human.db.JjcNewLadder.tEnemy = tEnemy
        human.db.JjcNewLadder.nPoint = nPoint
        RoleDBLogic.saveRole(human.db)
    else
        human.db.JjcNewLadder.bJoin = true
        human.db.JjcNewLadder.nRank = nRank
        human.db.JjcNewLadder.tEnemy = tEnemy
        human.db.JjcNewLadder.nPoint = nPoint
        JjcNewLadder_GetInterfaceInfo(human)
        -- local tMsgData = JjcNewLadder_GetInterfaceInfo(human)
        -- Msg.send(tMsgData, human.fd)
        -- print("[JjcNewLadderLogic_JoinHumanOk] 下发数据结束")
    end
end

-- 数据服收到刷新对战列表
function JjcNewLadderLogic_RefeshEnemy(tData)
    print("[JjcNewLadderLogic_RefeshEnemy] 开始刷新数据")
    local tRival = JjcNewLadderLogic_MatchRival(tData.uuid)
    if nil == tRival then
        print("[JjcNewLadderLogic_RefeshEnemy] 获取匹配对手的uid失败")
        return
    end

    print("[JjcNewLadderLogic_RefeshEnemy] 数据服收到刷新对战列表")

    local tRivalData = {}
    for nRank, v in pairs(tRival) do
        local tJjcData = JjcNewLadderDB.JjcNewLadderDB_GetDBDataByRank(nRank)
        if nil == tJjcData then
            print("[JjcNewLadderLogic_JoinHuman] 获取不到对应的DB数据 nRank = "..nRank)
            return
        end

        local tSendData = {
            name = tJjcData.name,
            head = tJjcData.head,
            headFrame = tJjcData.headFrame or 0,
            uuid = tJjcData.uuid,
            nIsHuman = tJjcData.nIsHuman,
            nRank = nRank,
            zhandouli = tJjcData.zhandouli,
            point = tJjcData.point,
            nServerID = tJjcData.nServerID or Config.SVR_INDEX,
            monsterOutID = tJjcData.monsterOutID or 0,
            lv = tJjcData.lv or 0,
            identity = tJjcData.identity or 0,
            szServerName = tJjcData.szServerName or Config.SVR_INDEX.."区"
        }

        table.insert(tRivalData, tSendData)
    end

    JjcNewLadderMiddle.JjcNewLadderMiddle_RefreshEnemy_D2C(tData.uuid, tData.nSrcServerID, tRivalData)
    print("[JjcNewLadderLogic_RefeshEnemy] 回复数据完成")
end

-- 刷新对战列表回包(普通服)
function JjcNewLadderLogic_RefeshEnemyOK(tData)
    print("[JjcNewLadderLogic_RefeshEnemyOK] 刷新对战列表回包(普通服)")
    local uuid, tEnemy = tData.uuid, tData.tEnemy
    local human = ObjHuman.onlineUuid[uuid]
    if not human then
        human = {}
        human.db = RoleDBLogic.getDb(uuid)
        human.db.JjcNewLadder.tEnemy = tEnemy
        RoleDBLogic.saveRole(human.db)
    else
        JjcNewLadder_SetHumanEnemy(human, tEnemy)
        JjcNewLadder_SendEnemy(human)

        local isQuick = human.db.combatQuick[CombatDefine.COMBAT_TYPE15] == 1 -- 是否自动战斗
        if isQuick then
            JjcNewLadderLogic_Query(human)
        end
        print("[JjcNewLadderLogic_RefeshEnemyOK] 发送完成")
    end
end

-- 请求上一轮前3玩家信息(回包)
function JjcNewLadderLogic_GetLast3RankOK(tData)
    print("[JjcNewLadderLogic_GetLast3RankOK] 普通服获取到对应的数据了")
    table.print_lua_table(tData)
    print("\n")

    tLast3RankPlayerInfo = tData
    for uuid, _ in pairs(tQueryLast3Rank) do
        local human = ObjHuman.onlineUuid[uuid]
        if human then
            JjcNewLadder_SendLast3Rank(human)
        end
    end

    tQueryLast3Rank = {}
    bQueryLast = false
end

-- 进行点赞(中心->数据(被点赞玩家所在服))
function JjcNewLadderLogic_AddWorShip(tData)
    local uuidDes = tData.uuidDes
    local human = ObjHuman.onlineUuid[uuidDes]
    local bSave = false
    if not human then
        human = {}
        human.db = RoleDBLogic.getDb(uuidDes)
        bSave = true
    end

    if not human.db.jjcBeWorship then
        human.db.jjcBeWorship = 0
    end

    human.db.jjcBeWorship = human.db.jjcBeWorship + 1

    if true == bSave then
        RoleDBLogic.saveRole(human.db)
    end

    JjcNewLadderMiddle.JjcNewLadderMiddle_AddWorShip_D2C(tData, human.db.jjcBeWorship)
end

-- 点赞成功
function JjcNewLadderLogic_AddWorShipOK(tData)
    if not tLast3RankPlayerInfo or  not tLast3RankPlayerInfo.tLastRankInfo then
        print("[JjcNewLadderLogic_AddWorShipOK] 为什么点赞完不存在数据了")
        return
    end

    local uuidDes, nNowWorShip = tData.uuidDes, tData.nNowWorShip
    for _, v in pairs(tLast3RankPlayerInfo.tLastRankInfo) do
        if v.uuid == uuidDes then
            v.worshipCnt = nNowWorShip
            break
        end
    end

    local human = ObjHuman.onlineUuid[tData.uuidSrc]
    local bSave = false
    if not human then
        human = {}
        human.db = RoleDBLogic.getDb(uuidDes)
        bSave = true
    end

    -- 设置已点赞
    JjcNewLadderLogic_SetWorShip(human, uuidDes, JjcNewLadderDefine.JJC_NEWLADDER_WORSHIP)
    if true == bSave then
        RoleDBLogic.saveRole(human.db)
    end

    if false == bSave then
        local tMsgData = Msg.gc.GC_JJC_NEW_LADDER_CHAMPION_WORSHIP
        tMsgData.worshipCnt = nNowWorShip
        tMsgData.dot = 0

        Msg.send(tMsgData, human.fd)
    end
end

-- 更新缓存的点赞数据
function JjcNewLadderLogic_UpdateWorShip(tData)
    if not tLast3RankPlayerInfo or not tLast3RankPlayerInfo.tLastRankInfo then
        print("[JjcNewLadderLogic_UpdateWorShip] 更新缓存的点赞数据 为什么不存在数据了")
        return
    end

    for _, v in pairs(tLast3RankPlayerInfo.tLastRankInfo) do
        if v.uuid == tData.uuidDes then
            v.worshipCnt = tData.nNowWorShip
            break
        end
    end
end

-- 请求天梯赛排行榜数据(中心->战区数据服)
function JjcNewLadderLogic_GetRankInfo(tData)
    local tSendRankData = {}
    local nFirst = 1
    for i = 1, JjcNewLadderDefine.JJC_NEWLADDER_RANK_SEND_LEN, 1 do
        local tRank = JjcNewLadderDB.JjcNewLadderDB_GetDBDataByRank(i)
        if not tRank then
            
        end
        table.insert(tSendRankData, {tRankData = tRank, nRank = i})

        local nLen = #tSendRankData
        if JjcNewLadderDefine.JJC_NEWLADDER_RANK_ONE_SEND_LEN  <= nLen then
            local nFinish = i == JjcNewLadderDefine.JJC_NEWLADDER_RANK_SEND_LEN and 1 or 0
            JjcNewLadderMiddle.JjcNewLadderMiddle_QueryRankInfo_D2C(tSendRankData, nFinish, tData.nSrcServerID, nFirst)
            print("[JjcNewLadderLogic_GetRankInfo] 发送一次数据  nFirst = "..nFirst.." nFinish = "..nFinish)
            nFirst = 0
            tSendRankData = {}
        end
    end

    if nil ~= _G.next(tSendRankData) then
        JjcNewLadderMiddle.JjcNewLadderMiddle_QueryRankInfo_D2C(tSendRankData, 1, tData.nSrcServerID, nFirst)
        print("[JjcNewLadderLogic_GetRankInfo] 发送完成数据  nFirst = "..nFirst)
    end
end

-- 获取到天梯赛排行榜数据(中心->普通)
function JjcNewLadderLogic_GetRankInfoOK(tData)
    local nFirst, nIsEnd = tData.nFirst, tData.nIsEnd
    print("[JjcNewLadderLogic_GetRankInfoOK] nFirst = "..nFirst.." nIsEnd = "..nIsEnd)
    local tRankInfo = tData.tRankInfo
    if nFirst == 1 then
        print("[JjcNewLadderLogic_GetRankInfoOK] 获取到天梯赛排行榜数据(中心->普通) 进行重置")
        tCacheRankInfo = {
            nNextUpdateTime = os.time() + JjcNewLadderDefine.JJC_NEWLADDER_RANK_NEXT_UPDATE_TIME + math.random(1, 30),
            tRankList = {}
        }
    end

    for _, v in ipairs(tRankInfo) do
        local nRank = v.nRank
        local tRankData = v.tRankData
        tCacheRankInfo.tRankList[nRank] = tRankData
    end

    if nIsEnd == 1 then
        for uuid, v in pairs(tCacheQueryRankPlaeyer) do
            local human = ObjHuman.onlineUuid[uuid]
            if human then
                JjcNewLadder_SendRankInfo(human)
            end
        end

        -- 清空缓存请求排行榜的玩家
        tCacheQueryRankPlaeyer = {}
        bQueryRank = nil
    end
end

-- 查询是否能够战斗(中心->战区数据服)
function JjcNewLadderLogic_CheckCanFight(tData)
    print("[JjcNewLadderLogic_CheckCanFight] 查询是否能够战斗(中心->战区数据服) 开始")
    local uuid, uuidDes = tData.uuid, tData.uuidDes
    local nCanFight = JjcNewLadderDefine.JJC_NEWLADDER_NO_CAN_FIGHT

    -- 检查挑战者
    local tRankData = JjcNewLadderDB.JjcNewLadderDB_GetDBDataByuid(uuid)
    if not tRankData then
        print("[JjcNewLadderLogic_CheckCanFight] 不存在数据无法挑战返回")
        JjcNewLadderMiddle.JjcNewLadderMiddle_ReplyCheckFight(tData, nCanFight)
        return
    end

    -- if tRankData.nInFight == JjcNewLadderDefine.JJC_NEWLADDER_IN_FIGHT then
    --     print("[JjcNewLadderLogic_CheckCanFight] 当前玩家记录的是在挑战中")
    --     -- 还在持续时间内不能战斗
    --     if tRankData.nLastFightTime + JjcNewLadderDefine.JJC_NEWLADDER_FIGHT_DURATION > os.time() then
    --         print("[JjcNewLadderLogic_CheckCanFight] 当前玩家记录的是在挑战中,并且在持续战斗时间内")
    --         JjcNewLadderMiddle.JjcNewLadderMiddle_ReplyCheckFight(tData, nCanFight)
    --         return
    --     end
    -- end

    -- 检查被挑战者
    local tRankEnemyData = JjcNewLadderDB.JjcNewLadderDB_GetDBDataByuid(uuidDes)
    if not tRankEnemyData then
        print("[JjcNewLadderLogic_CheckCanFight] 不存在 被挑战者 数据无法挑战返回")
        JjcNewLadderMiddle.JjcNewLadderMiddle_ReplyCheckFight(tData, nCanFight)
        return
    end

    -- if tRankEnemyData.nInFight == JjcNewLadderDefine.JJC_NEWLADDER_IN_FIGHT then
    --     print("[JjcNewLadderLogic_CheckCanFight]  被挑战者 当前在战斗中")
    --     -- 还在持续时间内不能战斗
    --     if tRankEnemyData.nLastFightTime + JjcNewLadderDefine.JJC_NEWLADDER_FIGHT_DURATION > os.time() then
    --         print("[JjcNewLadderLogic_CheckCanFight]  被挑战者 当前在战斗中， 并且在持续战斗时间内")
    --         JjcNewLadderMiddle.JjcNewLadderMiddle_ReplyCheckFight(tData, nCanFight)
    --         return
    --     end
    -- end

    -- 可以战斗
    print("[JjcNewLadderLogic_CheckCanFight]  双方可以战斗进行设置操作")

    -- tRankData.nInFight = JjcNewLadderDefine.JJC_NEWLADDER_IN_FIGHT
    -- tRankData.nLastFightTime = os.time()
    -- tRankEnemyData.nInFight = JjcNewLadderDefine.JJC_NEWLADDER_IN_FIGHT
    -- tRankEnemyData.nLastFightTime = os.time()

    nCanFight = JjcNewLadderDefine.JJC_NEWLADDER_CAN_FIGHT
    JjcNewLadderMiddle.JjcNewLadderMiddle_ReplyCheckFight(tData, nCanFight)
end

-- 查询是否能够战斗(中心->普通)
function JjcNewLadderLogic_CheckCanFightOK(tData)
    print("[JjcNewLadderLogic_CheckCanFightOK] 收到查询能否战斗的回包")
    table.print_lua_table(tData)
    local uuid, uuidDes, nIsFight = tData.uuid, tData.uuidDes, tData.nIsFight
    local human = ObjHuman.onlineUuid[uuid]
    -- 已经不在线了，直接返回
    if not human then
        return
    end

    if JjcNewLadderDefine.JJC_NEWLADDER_NO_CAN_FIGHT == nIsFight then
        return Broadcast.sendErr(human, Lang.JJCNEWLADDERNOFIGHT)
    else
        -- 开始正在的战斗流程
        print("[JjcNewLadderLogic_CheckCanFightOK] 开始真正的战斗流程")
        JjcLadderLogic_TrueFight(human, uuidDes)
    end
end

-- 战斗结束(中心->战区数据服)
function JjcNewLadderLogic_FightEnd(tData)
    print("[JjcNewLadderLogic_FightEnd] 进入 战斗结束 (中心->战区数据服)")

    local uuid, uuidDes, nResult = tData.uuid, tData.uuidDes, tData.nResult
    local tEnemyInfo = JjcNewLadderDB.JjcNewLadderDB_GetDBDataByuid(uuidDes)
    if not tEnemyInfo then
        print("[JjcNewLadderLogic_FightEnd] 不存在对战人物的DB数据 uuidDes = "..uuidDes.." nResult = "..nResult)
        return
    end

    local tPlayerInfo = JjcNewLadderDB.JjcNewLadderDB_GetDBDataByuid(uuid)
    if not tPlayerInfo then
        print("[JjcNewLadderLogic_FightEnd] 不存在玩家的DB数据 uuidDes = "..uuid.." nResult = "..nResult)
        return
    end

    local nOldRank = JjcNewLadderDB.JjcNewLadderDB_GetRankByuid(uuid)
    local nOldRankEnemy = JjcNewLadderDB.JjcNewLadderDB_GetRankByuid(uuidDes)
    print("[JjcNewLadderLogic_FightEnd] 当前老玩家的排名 nOldRank = "..nOldRank)

    -- 修改为可以继续战斗
    tPlayerInfo.nInFight = JjcNewLadderDefine.JJC_NEWLADDER_NOIN_FIGHT
    tEnemyInfo.nInFight = JjcNewLadderDefine.JJC_NEWLADDER_NOIN_FIGHT

    -- 修改战斗力
    tPlayerInfo.zhandouli = tData.nZhanDouLi

    --table.print_lua_table(tData)
    print("[JjcNewLadderLogic_FightEnd] 战斗结束 (中心->战区数据服)\n")

    -- 增加积分(会重新排序)
    if nResult == CombatDefine.RESULT_WIN then
        print("[JjcNewLadderLogic_FightEnd] 交换排名开始 ")
        -- JjcNewLadderDB.JjcNewLadderDB_AddPoint(uuid, 1)
        JjcNewLadderDB.JjcNewLadderDB_ExChangeRank(uuid, uuidDes)
        print("[JjcNewLadderLogic_FightEnd] 交换排名完成")
    end
    
    local nNewPlayerRank = JjcNewLadderDB.JjcNewLadderDB_GetRankByuid(uuid)
    local nNewEnemyRank = JjcNewLadderDB.JjcNewLadderDB_GetRankByuid(uuidDes)
    print("[JjcNewLadderLogic_FightEnd] 玩家新的排名 nNewPlayerRank = "..nNewPlayerRank)
    if nResult == CombatDefine.RESULT_WIN then
        -- 真人要发邮件
        if tEnemyInfo.nIsHuman == 1 and nOldRankEnemy ~= nNewEnemyRank then
            print("[JjcNewLadderLogic_FightEnd] 敌方是真人, 需要发送邮件")
            JjcNewLadder_SendFailMail(uuidDes, tEnemyInfo.nServerID, tPlayerInfo.szServerName, tPlayerInfo.name, nNewEnemyRank)
        end
    end

    -- 操作对战列表
    local tOldEnemyData, tChoseRank = {}, {}

    -- 防止选到已对战的角色
    tChoseRank[nNewEnemyRank] = 1

    for _, uuidEnemy in ipairs(tData.tEnemyUid) do
        if uuidEnemy ~= uuidDes then
            local nRank = JjcNewLadderDB.JjcNewLadderDB_GetRankByuid(uuidEnemy)
            if nRank then
                local tRankData = JjcNewLadderDB.JjcNewLadderDB_GetDBDataByRank(nRank)
                table.insert(tOldEnemyData, {uuid = uuidEnemy, nRank = nRank, nPoint = tRankData.point})
                tChoseRank[nRank] = 1
            end
        end
    end

    print("[JjcNewLadderLogic_FightEnd] 组装数据旧的敌方数据完成")

    print("[JjcNewLadderLogic_FightEnd] 匹配一名新的对手开始")
    local nNewOneRank = JjcNewLadderDB.JjcNewLadderDB_MatchOneRival(uuid, tChoseRank)
    if -1 >= nNewOneRank then
        nNewOneRank = nNewEnemyRank
    end

    -- 组装新的对战人物数据
    local tJjcData = JjcNewLadderDB.JjcNewLadderDB_GetDBDataByRank(nNewOneRank)
    local tSendData = {
        name = tJjcData.name,
        head = tJjcData.head,
        headFrame = tJjcData.headFrame or 0,
        uuid = tJjcData.uuid,
        nIsHuman = tJjcData.nIsHuman,
        nRank = nNewOneRank,
        zhandouli = tJjcData.zhandouli,
        point = tJjcData.point,
        nServerID = tJjcData.nServerID or Config.SVR_INDEX,
        monsterOutID = tJjcData.monsterOutID or 0
    }

    print("[JjcNewLadderLogic_FightEnd] 组装新的对手数据完成")

    print("[JjcNewLadderLogic_FightEnd]")
    JjcNewLadderMiddle.JjcNewLadderMiddle_ReplyFightD2C(uuid, uuidDes, tData.nSrcServerID, nNewPlayerRank, tPlayerInfo.point, tOldEnemyData, tSendData)
end

function JjcNewLadderLogic_FightEndOK(tData)
    -- 战斗结束(中心 -> 普通)
    local uuid, nNewRank, nNewPoint = tData.uuid, tData.nNewRank, tData.nNewPoint
    local uuidDes, tNewOneEnemy, tOldEnemyData = tData.uuidDes, tData.tNewOneEnemy, tData.tOldEnemyData
    local human = ObjHuman.onlineUuid[uuid]
    local bSave = false
    if not human then
        human = {}
        human.db = RoleDBLogic.getDb(uuid)
        bSave = true
    end

    print("[JjcNewLadderLogic_FightEndOK] 战斗结束(中心 -> 普通) ")
    JjcNewLadder_SetHumanRank(human, nNewRank)
    JjcNewLadder_SetHumanPoint(human, nNewPoint)

    -- 更新对战列表数据
    print("[JjcNewLadderLogic_FightEndOK] 更新对战列表数据开始 ")
    local tEnemy = JjcNewLadder_GetHumanEnemy(human)
    local nIndex = nil
    for i, v in ipairs(tEnemy) do
        if v.uuid == uuidDes then
            nIndex = i
        else
            for _, tUpdateData in ipairs(tOldEnemyData) do
                if tUpdateData.uuid == v.uuid then
                    v.point = tUpdateData.nPoint
                    v.nRank = tUpdateData.nRank
                end
            end
        end
    end

    if nIndex then
        tEnemy[nIndex] = tNewOneEnemy
    else
        print("[JjcNewLadderLogic_FightEndOK] 居然找不到对应对战角色数据记录的下标")
    end

    print("[JjcNewLadderLogic_FightEndOK] 更新对战列表数据结束")

    if true == bSave then
        print("[JjcNewLadderLogic_FightEndOK] 存一次DB")
        RoleDBLogic.saveRole(human.db)
    else
        JjcNewLadderLogic_TargetFind(human)
        RoleSystemLogic.onDot(human,RoleSystemDefine.ROLE_SYS_ID_1300)
    end
end

-- 发送战败邮件
function JjcNewLadderLogic_SendFailMailOK(tData)
    print("[JjcNewLadderLogic_SendFailMailOK] 获取到战败邮件\n")
    table.print_lua_table(tData)
    local uuid, szServerName, szName, nNewRank = tData.uuidDes, tData.szServerName, tData.szName, tData.nNewRank
    local human = ObjHuman.onlineUuid[uuid]
    local bSave = false
    if not human then
        human = {}
        human.db = RoleDBLogic.getDb(uuid)
        bSave = true
    end

    local mailConfig = MailExcel.mail[JjcNewLadderDefine.JJC_NEWLADDER_FAIL_MAIL_ID]
    local title = mailConfig.title
    local senderName = mailConfig.senderName
    local content = Util.format(mailConfig.content, szServerName, szName, nNewRank)
    print("[JjcNewLadderLogic_SendFailMailOK] 打印邮件信息 title = "..title.." content = "..content)
    MailManager.add(MailManager.SYSTEM, human.db._id, title, content, nil, senderName)
    print("[JjcNewLadderLogic_SendFailMailOK] 发送邮件结束")

    -- 改变排名
    JjcNewLadder_SetHumanRank(human, nNewRank)
    if bSave == true then
        RoleDBLogic.saveRole(human.db)
    end
end

-- 发送排名奖励(中心->普通)
function JjcNewLadderLogic_SendPrizeMail(tData)
    local tConfig = JjcExcel.skyladderRank
    local uuid, nRank = tData.uuid, tData.nRank
    print("[JjcNewLadderLogic_SendPrizeMail] 获取到奖励邮件，进行发奖励操作 uuid = "..uuid.." nRank = "..nRank)

    local tPrize = nil
    for _, v in ipairs(tConfig) do
        if v.nMinRank <= nRank and v.nMaxRank >= nRank then
            tPrize = v.tPrize
            break
        end
    end

    -- 发送奖励
    if tPrize then
        local mailConfig = MailExcel.mail[JjcNewLadderDefine.JJC_NEWLADDER_RANKPRIZE_MAIL_ID]
        local title = mailConfig.title
        local senderName = mailConfig.senderName
        local content = mailConfig.content
        MailManager.add(MailManager.SYSTEM, uuid,
                                    title, Util.format(content, nRank), tPrize, senderName)
        print("[JjcNewLadderLogic_SendPrizeMail] 发送邮件奖励结束")
    end
end

function JjcNewLadderLogic_QueryWorship(tData)
    local human = ObjHuman.onlineUuid[tData.uuid]
    if not human then
        human = {}
        human.db = RoleDBLogic.getDb(tData.uuid)
    end

    local nNowWorship = human.db.jjcBeWorship

    JjcNewLadderMiddle.JjcNewLadderMiddle_QueryWorShipO2C(tData.uuid, tData.nFromServerID, nNowWorship)
end

---------------------------------- 客户端请求 ----------------------------------------------
-- 请求天梯赛信息
function JjcNewLadderLogic_Query(human)
    if not human.db.JjcNewLadder then
        JjcNewLadderLogic_CreatDB(human)
    end

    print("[JjcNewLadderLogic_Query] 请求天梯赛信息")
    if false == JjcNewLadderLogic_GetJoin(human) then
        if true == JjcNewLadderLogic_CheckIsOpen() then
            local uuid = human.db._id
            if not tQueryJoinPlayer[uuid] then
                tQueryJoinPlayer[uuid] = os.time()
                JjcNewLadderLogic_JoinLadder(human)
            else
                if tQueryJoinPlayer[uuid] + JjcNewLadderDefine.JJC_NEWLADDER_JOINTIME <= os.time() then
                    tQueryJoinPlayer[uuid] = os.time()
                    JjcNewLadderLogic_JoinLadder(human)
                else
                    return
                end
            end
        else
            JjcNewLadder_GetInterfaceInfo(human)
        end
    else
        JjcNewLadder_GetInterfaceInfo(human)
    end
end

-- 天梯赛对战记录查询
function JjcNewLadderLogic_RecordQuery(human)
    JjcNewLadder_TryDelRecordList(human)
    local tRecordList = JjcNewLadder_GetRecordList(human)
    local tMsgData = Msg.gc.GC_JJC_NEW_LADDER_RECORD_QUERY
    tMsgData.leftFreeCnt = JjcNewLadder_GetHumanFreeNum(human)
    tMsgData.recordList[0] = 0
    if tRecordList then
        for i, v in ipairs(tRecordList) do
            local videoUuid = v.videoUuid
            local combatVideo = CombatVideo.getCombatVideo(videoUuid)
            if combatVideo then
                tMsgData.recordList[0] = tMsgData.recordList[0] + 1
                local net = tMsgData.recordList[tMsgData.recordList[0]]
                local combatInfo = combatVideo.combatInfo
                local parms = combatInfo.endParam and Util.split(combatInfo.endParam, "|")	
                net.time = combatInfo.time		    
                net.identity = combatVideo._id 			
                if combatInfo.attacker.uuid == human.db._id then
                    RoleLogic.makeRoleBase(combatInfo.defender, net.roleBase)
                    net.result = combatInfo.isWin and JJC_RESULT_ATK_WIN or JJC_RESULT_ATK_FAIL
                    net.jifenChange = tonumber(parms and parms[3]) or 0
                else
                    RoleLogic.makeRoleBase(combatInfo.attacker, net.roleBase)
                    net.result = (not combatInfo.isWin) and JJC_RESULT_DEF_WIN or JJC_RESULT_DEF_FAIL
                    net.jifenChange = tonumber(parms and parms[4]) or 0
                end
            end	
        end
    end


    Msg.send(tMsgData, human.fd)
end

-- 天梯赛对手查询
function JjcNewLadderLogic_TargetFind(human)
    if false == JjcNewLadderLogic_CheckIsOpen() then
        JjcNewLadder_PopNoOpenTips(human)
        return
    end
    JjcNewLadderMiddle.JjcNewLadderMiddle_RefreshEnemy(human)
end

-- 膜拜
function JjcNewLadderLogic_Worship(human, uuid)
    if not tLast3RankPlayerInfo or not tLast3RankPlayerInfo.tLastRankInfo  then
        print("[JjcNewLadderLogic_Worship] 不存在上一轮数据排名前3的数据")
        return
    end

    local nState = JjcNewLadderLogic_GetWorShip(human, uuid)
    if nState == JjcNewLadderDefine.JJC_NEWLADDER_WORSHIP then
        return
    end

    local nServerID
    for _, v in pairs(tLast3RankPlayerInfo.tLastRankInfo) do
        if v.uuid == uuid then
            nServerID = v.nServerID
            break
        end
    end

    if not nServerID then
        print("[JjcNewLadderLogic_Worship] 获取对应uuid对应的服务器ID失败 name = "..human.db.name.." uuid = "..uuid)
        return
    end

    JjcNewLadderMiddle.JjcNewLadderMiddle_SendWorShip(human.db._id, uuid, nServerID)

    -- 点赞奖励
    local tItem = {}
    table.insert(tItem, {JjcNewLadderDefine.JJC_NEWLADDER_WORSHIP_GOODID, JjcNewLadderDefine.JJC_NEWLADDER_WORSHIP_GOODNUM})
    BagLogic.addItemList(human, tItem, "jjc_ladder")

    JjcNewLadderLogic_SetWorShip(human, uuid, JjcNewLadderDefine.JJC_NEWLADDER_WORSHIP)
end

-- 排行榜查询
function JjcNewLadderLogic_RankQuery(human)
    if not tCacheRankInfo or not tCacheRankInfo.tRankList then
        local uuid = human.db._id
        if tCacheQueryRankPlaeyer[uuid] then
            return
        else
            tCacheQueryRankPlaeyer[uuid] = 1
            if not bQueryRank then
                bQueryRank = true
                JjcNewLadderMiddle.JjcNewLadderMiddle_QueryRankInfo()
            end

            return
        end
    else
        JjcNewLadder_SendRankInfo(human)

        -- 时间过期了，再次请求排行榜数据
        local nNextUpdateTime = tCacheRankInfo.nNextUpdateTime
        if nNextUpdateTime <= os.time() then
            JjcNewLadderMiddle.JjcNewLadderMiddle_QueryRankInfo()
        end
    end
end

-- 请求一键挑战 -- 直接获取奖励
function JjcNewLadderLogic_OneClickChange(human)
    if false == JjcNewLadderLogic_CheckIsOpen() then
        JjcNewLadder_PopNoOpenTips(human)
        return
    end

    local tConfig = JjcExcel.skyladderFightPrize
    local nFreeNum = JjcNewLadder_GetHumanFreeNum(human)
    if 0 >= nFreeNum then
        return
    end

    local tItem = {}
    for _, v in ipairs(tConfig[1].tPrize) do
        local nGoodNum = v[2] * nFreeNum
        local nGoodID = v[1]

        table.insert(tItem, {nGoodID, nGoodNum})
    end

    if nil ~= _G.next(tItem) then
        BagLogic.addItemList(human, tItem, "jjc_ladder")
        JjcNewLadder_SetHumanFreeNum(human, -nFreeNum)
        JjcNewLadderLogic_Query(human)
        RoleSystemLogic.onDot(human,RoleSystemDefine.ROLE_SYS_ID_1300)
    end
end

-- 请求天梯赛上一轮排名前三
function JjcNewLadderLogic_LastRankQuery(human)
    print("[JjcNewLadderLogic_LastRankQuery] 进入请求天梯赛上一轮排名前三 操作")
    if not tLast3RankPlayerInfo then
        print("[JjcNewLadderLogic_LastRankQuery] 不存在缓存数据进行操作")
        local uuid = human.db._id
        if  tQueryLast3Rank[uuid]  then
            return
        end

        tQueryLast3Rank[uuid] = 1
        if bQueryLast == false then
            print("[JjcNewLadderLogic_LastRankQuery] 开始请求中心服")
            JjcNewLadderMiddle.JjcNewLadderMiddle_QueryLastRank()
            bQueryLast = true
        end
    else
        print("[JjcNewLadderLogic_LastRankQuery] 存在缓存数据直接发送")
        JjcNewLadder_SendLast3Rank(human)
    end
end

-- 请求排行榜奖励信息
function JjcNewLadderLogic_RankPrizeQuery(human)
    local tMsgData = Msg.gc.GC_JJC_NEW_LADDER_QUERY_RANKPRIZE
    tMsgData.nOwnRank = JjcNewLadder_GetHumanRank(human)
    tMsgData.tOwnPrize[0] = 0
    tMsgData.tRankPrize[0] = 0
    for _, v in ipairs(JjcExcel.skyladderRank) do
        tMsgData.tRankPrize[0] = tMsgData.tRankPrize[0] + 1
        local tData = tMsgData.tRankPrize[tMsgData.tRankPrize[0]]
        tData.nMinRank = v.nMinRank
        tData.nMaxRank = v.nMaxRank
        tData.tList[0] = #v.tPrize

        for nIndex, tItem in ipairs(v.tPrize) do
            local nGoodsID = tItem[1]
            local nGoodNum = tItem[2]

            Grid.makeItem(tData.tList[nIndex], nGoodsID, nGoodNum)
        end

        -- 个人排行榜奖励
        if tData.nMinRank <= tMsgData.nOwnRank and tData.nMaxRank >= tMsgData.nOwnRank  then
            tMsgData.tOwnPrize[0] = #v.tPrize
            for nIndex, tItem in ipairs(v.tPrize) do
                local nGoodsID = tItem[1]
                local nGoodNum = tItem[2]
    
                Grid.makeItem(tMsgData.tOwnPrize[nIndex], nGoodsID, nGoodNum)
            end
        end
    end

    Msg.send(tMsgData, human.fd)
end

-- 请求人物所有信息
function JjcNewLadderLogic_QueryAllInfo(human, uuid, nType)
    if false == JjcNewLadderLogic_CheckIsOpen() then
        JjcNewLadder_PopNoOpenTips(human)
        return
    end

    print("[JjcNewLadderLogic_QueryAllInfo] 请求人物所以信息开始 name = "..human.db.name.." uuid = "..uuid.." nType = "..nType)

    local tPlayInfo = JjcNewLadderLogic_GetInfoByUUID(human, uuid, nType)
    if nil == tPlayInfo then
        print("[JjcNewLadderLogic_QueryAllInfo] 获取所有数据失败 name = "..human.db.name)
        return
    end


    -- 假人不用请求了, 直接组装
    if 0 == tPlayInfo.nIsHuman then
        print("[JjcNewLadderLogic_QueryAllInfo] 当前请求的是机器人")

        local msgRet = Msg.gc.GC_JJC_NEW_LADDER_QUERT_ALLINFO
        msgRet.nType = nType
        -- 参考 makePlayInfoByOther 
        RoleLogic.makeOtherPlayInfo(msgRet.data, tPlayInfo)
        msgRet.data.roleBase.uuid = tostring(tPlayInfo.uuid)
        msgRet.data.svrName = tPlayInfo.szServerName or Config.NEW_SVR_INDEX.."区"
        msgRet.data.yjUuid = ""
        print("[JjcNewLadderLogic_QueryAllInfo] 组装数据完成")

        print("[JjcNewLadderLogic_QueryAllInfo] rolebase.uuid = "..msgRet.data.roleBase.uuid.." Type = "..type(msgRet.data.roleBase.uuid))
        for i = 1, msgRet.data.defenceList[0], 1 do
            local tHeroData = msgRet.data.defenceList[i]
            if tHeroData and tHeroData.uuid then
                print("[JjcNewLadderLogic_QueryAllInfo] 组装的英雄对应的 uuid = "..tHeroData.uuid.." type = "..type(tHeroData.uuid))
            end
        end

        Msg.send(msgRet, human.fd)
    else
        print("[JjcNewLadderLogic_QueryAllInfo] 当前请求的是真人")

        local tQueryMsg = 
        {
            uuid = uuid,
            nServerIndex = tPlayInfo.nServerID,
            nFrom = CommonDefine.COMMON_PLAY_INFO_JJC,
            nType = nType,
        }
		MiddleCommonLogic.MiddleCommonLogic_GetChatPlayInfo_LW(human, tQueryMsg)
        print("[JjcNewLadderLogic_QueryAllInfo] 先中心服发送请求数据")
    end
end

-- 获取到跨服玩家信息
function JjcNewLadderLogic_GetPlayerInfo(msg)
    local nSrcUID = msg.nSrcUID
    local human = ObjHuman.onlineUuid[nSrcUID]
    if not human then
        print("[JjcNewLadderLogic_GetPlayerInfo] 玩家不在线直接返回")
        return
    end

    print("[JjcNewLadderLogic_GetPlayerInfo] 获取到玩家数据")

    local msgRet = Msg.gc.GC_JJC_NEW_LADDER_QUERT_ALLINFO
    msgRet.nType = msg.nType
    msgRet.data.roleBase = msg.tData.data.roleBase
    msgRet.data.defenceList = msg.tData.data.defenceList
    msgRet.data.svrName = msg.tData.data.svrName
    msgRet.data.yjUuid = msg.tData.data.yjUuid
    Msg.send(msgRet, human.fd)
end

---------------------------------- 战斗相关 ----------------------------------------------
-- 检测能否战斗
function CheckCanFight(human, uuidEnemy)
    print("[JjcLadderLogic_CheckCanFight] 开始检测能否战斗")
    if false == JjcNewLadderLogic_CheckIsOpen() then
        JjcNewLadder_PopNoOpenTips(human)
        return
    end

    JjcNewLadderMiddle.JjcNewLadderMiddle_CheckCanFightBegin(human, uuidEnemy)
end

function JjcLadderLogic_TrueFight(human, uuidDes)
    print("[JjcLadderLogic_TrueFight] 当前可以战斗, 开始战斗流程")

    local tPlayInfo = JjcNewLadderLogic_GetInfoByUUID(human, uuidDes, JjcNewLadderDefine.JJC_NEWLADDER_QUERYUID_ENEMY_TYPE)
    if not tPlayInfo then
        print("[JjcLadderLogic_TrueFight] 不存在对应的敌方数据, 上一步是如何通过的")
        return
    end

    -- 扣门票
    -- BagLogic.delItem(human, ItemDefine.ITEM_JJC_TICKET_ID, 3, "jjc_fight")		
	local nFreeNum = JjcNewLadder_GetHumanFreeNum(human)
    if nFreeNum > 0 then
        JjcNewLadder_SetHumanFreeNum(human, -1)
    else
        -- local nPayNum = JjcNewLadder_GetHumanPayNum(human)
        -- if nPayNum > 0 then
        --     JjcNewLadder_SetHumanPayNumNum(human, -1)
        -- end
        local nNowNum = BagLogic.getItemCnt(human, ItemDefine.ITEM_JJC_TICKET_ID)
        if JjcNewLadderDefine.JJC_NEWLADDER_COST_ITEM_NUM > nNowNum then
            return
        end

        -- 扣门票
        BagLogic.delItem(human, ItemDefine.ITEM_JJC_TICKET_ID, JjcNewLadderDefine.JJC_NEWLADDER_COST_ITEM_NUM, "jjc_fight")
    end

    if 0 == tPlayInfo.nIsHuman then
        local args = {
            [1] = uuidDes
        }
        print("[JjcLadderLogic_TrueFight] 对战的角色是人机进行对战")
        CombatLogic.combatBegin(human, nil, args, CombatDefine.COMBAT_TYPE15, uuidDes)
    else
        local tData =
        {
            combatType = CombatDefine.COMBAT_TYPE15,
            nServerIndex = tPlayInfo.nServerID,
            param = uuidDes.."|"..human.db._id
        }

        print("[JjcLadderLogic_TrueFight] 与真人开始对战 nServerID = "..tPlayInfo.nServerID)
        MiddleCommonLogic.MiddleCommonLogic_CombatBegin_LW(human, tData)
    end
end

-- 调用开始战斗
function fight(human, args, combatType)
    print("[JjcLadderLogic_fight] 调用开始战斗 ")
    if CombatDefine.COMBAT_TYPE15 ~= combatType then
        return
    end

    -- 对战角色的UID
    local uuidEnemy = args[1]
    local tPlayInfo = JjcNewLadderLogic_GetInfoByUUID(human, uuidEnemy, JjcNewLadderDefine.JJC_NEWLADDER_QUERYUID_ENEMY_TYPE)
    if not tPlayInfo then
        print("[JjcNewLadderLogic_fight] 不存在对应的敌方数据")
        return
    end

    -- 检测挑战次数
    local nFreeNum = JjcNewLadder_GetHumanFreeNum(human)
    if nFreeNum <= 0 then
        local nNowNum = BagLogic.getItemCnt(human, ItemDefine.ITEM_JJC_TICKET_ID)
        if JjcNewLadderDefine.JJC_NEWLADDER_COST_ITEM_NUM > nNowNum then
            print("[JjcNewLadderLogic_fight] 可用的挑战次数不足")
            return
        end
    end

    -- 去中心服查询是否能够战斗
    CheckCanFight(human, uuidEnemy)
    print("[JjcNewLadderLogic_fight] 去中心服查询是否能够战斗结束")
end

-- 获取人物对战列表
function getCombatObjList(human, side, args)
    if side == CombatDefine.ATTACK_SIDE then
        if not human then
            print("[JjcNewLadderLogic_getCombatObjList] 获取进攻方,但是不存在对应的human")
            return
        end

        print("[JjcNewLadderLogic_getCombatObjList] 进攻方 获取人物对战列表")
        local objList, helpList, rolebase, formation,jiban, elfList = CombatLogic.getHumanObjList(human, CombatDefine.COMBAT_TYPE16)
        if not objList or nil == _G.next(objList) then
            print("[JjcNewLadderLogic_getCombatObjList] 没有获取到对应的天梯赛进攻方防守阵容,取默认对战阵容")
            objList, helpList, rolebase, formation,jiban, elfList = CombatLogic.getHumanObjList(human, CombatDefine.COMBAT_TYPE1)
        end

        return objList, helpList, rolebase, formation,jiban, elfList
    elseif CombatDefine.DEFEND_SIDE == side then
        local uuid = args[1]
        if not uuid then
            print("[JjcNewLadderLogic_getCombatObjList] 获取防守方阵容,但是不存在对应的")
            return 
        end
        local fakeHuman = CombatLogic.createCombatFakeHuman(uuid)
        if not fakeHuman then
            print("[JjcNewLadderLogic_getCombatObjList] 获取不到对应的地方db数据")
            return
        end

        print("[JjcNewLadderLogic_getCombatObjList] 防守方 获取人物对战列表")
        local objList, helpList, rolebase, formation,jiban, elfList = CombatLogic.getHumanObjList(fakeHuman, CombatDefine.COMBAT_TYPE16)
        if not objList or nil == _G.next(objList) then
            print("[JjcNewLadderLogic_getCombatObjList] 没有获取到对应的天梯赛防守阵容,取默认对战阵容")
            objList, helpList, rolebase, formation,jiban, elfList = CombatLogic.getHumanObjList(fakeHuman, CombatDefine.COMBAT_TYPE1)
        end

        return objList, helpList, rolebase, formation,jiban, elfList
    else
        print("[JjcNewLadderLogic_getCombatObjList] 不是进攻也不是防守，直接返回")
        return
    end
end

-- 获取机器人对应战斗ID
function getCombatMonsterOutID(human, side, args)
    if side ~= CombatDefine.DEFEND_SIDE then
        return
    end

    print("[JjcNewLadderLogic_getCombatMonsterOutID] 获取机器人对应战斗ID")

	local uuid = args[1]
    local tPlayInfo = JjcNewLadderLogic_GetInfoByUUID(human, uuid, JjcNewLadderDefine.JJC_NEWLADDER_QUERYUID_ENEMY_TYPE)

	if not tPlayInfo then
        return
    end

	if not tPlayInfo.monsterOutID then
        return
    end

	return tPlayInfo.monsterOutID, tPlayInfo.zhandouli
end

-- 挑战结束
function onFightEnd(human, result, combatType, defUuid, combatInfo)
    print("[JjcNewLadderLogic_onFightEnd] 挑战结束 result = "..result)
    local tPrize = result == CombatDefine.RESULT_WIN and JjcExcel.skyladderFightPrize[1] or JjcExcel.skyladderFightPrize[2]

    if result == CombatDefine.RESULT_WIN then
        WarOrder.trigger(human, 5) -- 5 表示战令类型中的天梯战令
    end

    -- 存对战录像
    print("[JjcNewLadderLogic_onFightEnd] 开始存战斗录像")
    JjcNewLadder_SaveRecord(human, combatInfo)
    print("[JjcNewLadderLogic_onFightEnd] 结束存战斗录像")

    -- 添加战报
    print("[JjcNewLadderLogic_onFightEnd] 开始添加战报")
    local nMyRank = JjcNewLadder_GetHumanRank(human)
    local tPlayInfo = JjcNewLadderLogic_GetInfoByUUID(human, defUuid, JjcNewLadderDefine.JJC_NEWLADDER_QUERYUID_ENEMY_TYPE)
    if tPlayInfo then
        -- 不是玩家重新组装
        if tPlayInfo.nIsHuman == 0 then
            combatInfo.defender.head = tPlayInfo.head
            combatInfo.defender.name = tPlayInfo.name
            combatInfo.defender.lv = 50
            combatInfo.defender.zhandouli = tPlayInfo.zhandouli
            combatInfo.defender.uuid = tPlayInfo.uuid
        end
        WarReportLogic.add(WarReportLogic.WAR_REPORT_3, combatInfo, nMyRank, tPlayInfo.nRank)
    else
        print("[JjcNewLadderLogic_onFightEnd] 不存在对战人物的缓存信息")
    end
    print("[JjcNewLadderLogic_onFightEnd] 结束添加战报")

    local tMsgData = Msg.gc.GC_JJC_NEW_LADDER_AFTER_FIGHT
    tMsgData.result = result
    tMsgData.atkPoint = 0
    tMsgData.atkPointChange = 0
    tMsgData.defPoint = 0
    tMsgData.defPointChange = 0

    tMsgData.items[0] = #tPrize.tPrize
    for i, v in ipairs(tPrize.tPrize) do
        local nGoodsID = v[1]
        local nGoodsNum = v[2]
        Grid.makeItem(tMsgData.items[i], nGoodsID, nGoodsNum)
    end

    local tDefInfo = JjcNewLadderLogic_GetInfoByUUID(human, defUuid, JjcNewLadderDefine.JJC_NEWLADDER_QUERYUID_ENEMY_TYPE)
    if not tDefInfo then
        print("[JjcNewLadderLogic_onFightEnd] 为什么获取不到对应的数据！！！")
    else
        JjcNewLaader_GetBaseInfo(tMsgData.defRole, tDefInfo)
    end
    
    JjcNewLaader_GetHumanBaseInfo(tMsgData.atkRole, human)

    Msg.send(tMsgData, human.fd)

    -- 给物品
    print("[JjcNewLadderLogic_onFightEnd] 给奖励")
    for _, v in ipairs(tPrize.tPrize) do
        BagLogic.addItem(human, v[1], v[2], "jjc_fight")
    end
    -- BagLogic.addItemList(human, tPrize.tPrize, "jjc_fight")


    -- 获取对战列表的UID，需要刷新
    local tEnemyUuid = JjcNewLaader_GetAllEnemyUID(human)

    -- 通知中心服对战结果
    print("[JjcNewLadderLogic_onFightEnd] 开始通知中心服对战结果")
    JjcNewLadderMiddle.JjcNewLadderMiddle_FightEndO2C(human.db._id, defUuid, result, tEnemyUuid, human.db.zhandouli)

    RoleSystemLogic.onDot(human,RoleSystemDefine.ROLE_SYS_ID_1300)
end


---------------------------------- 刷新相关 ----------------------------------------------
-- 延迟几秒处理在线玩家
function JjcLadderLogic_LaterResetHuman()
    for _, human in pairs(ObjHuman.onlineUuid) do
        JjcNewLadderLogic_CreatDB(human)
        JjcNewLadderLogic_Query(human)
        JjcNewLadderLogic_WriteLog("[JjcLadderLogic_LaterResetHuman] 对在线玩家数据进行了重置,重新查询数据 进行处理 name = "..human.db.name)
    end
end

function JjcLadderLogic_LaterOperate()
    JjcNewLadderDB.JjcNewLadderDB_SetEndTime()
    JjcNewLadderLogic_WriteLog("[JjcLadderLogic_LaterOperate] 准点处理,重新生成了结束时间")

    -- 清除数据
    JjcNewLadderDB.JjcNewLadderDB_OnHourOpen()

    JjcNewLadderLogic_WriteLog("[JjcLadderLogic_LaterOperate] 清楚数据结束")
    
    local nRandTime = math.random(10, 20)
    Timer.addLater(nRandTime, JjcLadderLogic_LaterResetHuman)
end

-- 定时处理
function onHour(nHour)
    print("[JjcLadderLogic_onHour] 进入定时处理 nHour = "..nHour)
    if JjcNewLadderDefine.JJC_NEWLADDER_OPENTIME ~= nHour and JjcNewLadderDefine.JJC_NEWLADDER_ENDTIME ~= nHour then
        print("[JjcLadderLogic_onHour] 不符合条件不进行处理 nHour = "..nHour)
        return
    end

    print("[JjcLadderLogic_onHour] 当前对应的时间为 nHour = "..nHour)

    local nNowTime = os.time()
    local tDate = os.date("*t",nNowTime)

    -- 凌晨
    if JjcNewLadderDefine.JJC_NEWLADDER_OPENTIME == nHour then
        if JjcNewLadderDefine.JJC_NEWLADDER_OPENDAY == tDate.wday then
            Timer.addLater(JjcNewLadderDefine.JJC_NEWLADDER_OPEN_LATER_TIME, JjcLadderLogic_LaterOperate)
        else
            local nNowEndTime = CommonDB.GetJjcLadderEndTime()
            -- 还在活动时间内
            if nNowEndTime > nNowTime then
                for _, human in pairs(ObjHuman.onlineUuid) do
                    if not human.db.JjcNewLadder then
                        JjcNewLadderLogic_CreatDB(human)
                    else
                        local nLastEndTime = JjcNewLadder_GetHumanLastEndTime(human)
                        if nNowEndTime > nLastEndTime then
                            JjcNewLadderLogic_CreatDB(human)
                        else
                            local nRefreshTime = JjcNewLadder_GetHumanRefreshTime(human)
                            if true ~= Util.isSameDayByTimes(nRefreshTime, nNowTime) then
                                JjcNewLadder_SetHumanRefreshTime(human, nNowTime)
                                JjcNewLadder_SetHumanFreeNum(human, JjcNewLadderDefine.JJC_NEWLADDER_JJC_DAY_FREE_NUM)
                                print("[JjcLadderLogic_onHour] 玩家定时重置数据完成 name = "..human.db.name)
                            end
                        end
                    end
                end
            end
        end
    elseif JjcNewLadderDefine.JJC_NEWLADDER_ENDDAY == tDate.wday and JjcNewLadderDefine.JJC_NEWLADDER_ENDTIME == nHour then
        print("[JjcLadderLogic_onHour] 进入活动结束流程 nHour = "..nHour)
        JjcNewLadderDB.JjcNewLadderDB_SendRankMail()
    end
end

-- 玩家登录处理
function onLogin(human)
    print("[JjcLadderLogic_onLogin] 玩家登录处理开始 name = "..human.db.name)
    if false == JjcNewLadderLogic_CheckIsOpen() then
        print("[JjcLadderLogic_onLogin] 当前活动未开启直接返回")
        return
    end

    print("[JjcLadderLogic_onLogin] 玩家登录 name = "..human.db.name)
    if not human.db.JjcNewLadder then
        JjcNewLadderLogic_CreatDB(human)
    else
        local nLastEndTime = JjcNewLadder_GetHumanLastEndTime(human)
        local nNowEndTime = CommonDB.GetJjcLadderEndTime()
        print("[JjcLadderLogic_onLogin] 玩家登录 name = "..human.db.name.." nLastEndTime = "..nLastEndTime.." nNowEndTime = "..nNowEndTime)
        if nNowEndTime > nLastEndTime then
            JjcNewLadderLogic_CreatDB(human)
        else
            local nRefreshTime = JjcNewLadder_GetHumanRefreshTime(human)
            local nNowTime = os.time()
            print("[JjcLadderLogic_onLogin] 玩家登录 name = "..human.db.name.." nRefreshTime = "..nRefreshTime.." nNowTime = "..nNowTime)
            if true ~= Util.isSameDayByTimes(nRefreshTime, nNowTime) then
                JjcNewLadder_SetHumanRefreshTime(human, nNowTime)
                JjcNewLadder_SetHumanFreeNum(human, JjcNewLadderDefine.JJC_NEWLADDER_JJC_DAY_FREE_NUM)
                print("[JjcLadderLogic_onLogin] 玩家登录重置数据完成 name = "..human.db.name)
            end
        end
    end
end

-- 获取活动状态
function getActState()
    local bRet = JjcNewLadderLogic_CheckIsOpen()
    if false == bRet then
        return JjcActLogic.STATE_NOOPEN, 0
    end

    return JjcActLogic.STATE_START, JjcNewLadder_GetLeftTime()
end

function isActRed(human)
    -- if human.db.lv < 50 then
    --     return false
    -- end

    if false == JjcNewLadderLogic_CheckIsOpen() then
        return false
    end

    if not JjcNewLadderLogic_BaseCheck(human) then
        return false
    end
   
    if not human.db.JjcNewLadder then
        JjcNewLadderLogic_CreatDB(human)
    end

    local nFreeNum = JjcNewLadder_GetHumanFreeNum(human)
    if nFreeNum > 0 then
        return true
    end

    local nPayNum = JjcNewLadder_GetHumanPayNum(human)
    if nPayNum > 0 then
        return true
    end

    return false
end

function Init()
    JjcNewLadderDB.JjcNewLadderDB_initAfterStart()
end

-- function isActRed(human)
--     if true == JjcNewLadderLogic_CheckIsOpen() then
--         local nFreeNum = JjcNewLadder_GetHumanFreeNum(human)
--         if 0 < nFreeNum then
--             return true
--         end
--     end

--     return false
-- end

function MainJieMianDot(human)
    return true == isActRed(human) and 1 or 0
end