local Lang = require("common.Lang")
local BattleExcel = require("excel.battle")
local BattleExcel_Elite = require("excel.battle_elite")
local BattleExcel_Hard = require("excel.battle_hard")
local MonsterExcel = require("excel.monster")
local BattleMonsterExcel = require("excel.battlemonster")
local BattleMonsterExcel2 = require("excel.battlemonster2")
local DropExcel = require("excel.drop")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local ItemDefine = require("bag.ItemDefine")
local Broadcast = require("broadcast.Broadcast")
local CombatDefine = require("combat.CombatDefine")
local CombatPosLogic = require("combat.CombatPosLogic")
local CombatLogic = require("combat.CombatLogic")
local DailyTaskLogic = require("dailyTask.DailyTaskLogic")
local RoleDefine = require("role.RoleDefine")
local ChengjiuLogic = require("chengjiu.ChengjiuLogic")
local ChengjiuDefine = require("chengjiu.ChengjiuDefine")
local HeroExcel = require("excel.hero")
local SkillExcel = require("excel.skill")
-- local MonthactExcel = require("excel.monthAct")
local Util = require("common.Util")
local KingWorldLogic = require("present.KingWorldLogic")
local ChatPaoMaLogic = require("chat.ChatPaoMaLogic")
local BRoleLogic = require("billboard.BRoleLogic")
local BillboardDefine = require("billboard.BillboardDefine")
local SkinLogic = require("skin.SkinLogic")
local RoleSystemLogic = require("roleSystem.RoleSystemLogic")
local RoleSystemDefine = require("roleSystem.RoleSystemDefine")
local GuideLogic = require("guide.GuideLogic")
-- local PanelDefine = require("broadcast.PanelDefine")
local HeroGrid = require("hero.HeroGrid")
local TuiSongLiBao = require("present.TuiSongLiBao")
local CombatVideo = require("combat.CombatVideo")
local RoleLogic = require("role.RoleLogic")
local BattleDBLogic = require("battle.BattleDBLogic")
local ProjectLogic = require("platform.ProjectLogic")
local LuaMongo = _G.lua_mongo
local DB = require("common.DB")
local VipLogic = require("vip.VipLogic")
-- local MoshouLogic = require("moshou.MoshouLogic")
local RoleExcel = require("excel.role")
local TequanShopLogic = require("present.TequanShopLogic")
local HeroDefine = require("hero.HeroDefine")
local BarTaskLogic = require("bar.BarTaskLogic")
-- local ItemExcel = require("excel.item").item
-- local EquipExcel = require("excel.equip").equip
local HeroLogic = require("hero.HeroLogic")
local SuipianLogic = require("bag.SuipianLogic")
local JibanLogic = require("combat.JibanLogic")
local YunYingLogic = require("yunying.YunYingLogic")
local PanelDefine = require("broadcast.PanelDefine")
-- local YyHandler = require("yunying.Handler")
local EquipLogic = require("equip.EquipLogic")
local SysParameter = require("common.SysParameter")
-- local XingYaoGongMing = require("xingYaoMen.XingYaoGongMing")
-- local AbsActLogic = require("absAct.AbsActLogic")
local HeroLogLogic = require("absAct.HeroLogLogic")
local DropExchangeLogic = require("absAct.DropExchangeLogic")
local MengxinLogic = require("present.MengxinLogic")
local GiftLogic = require("topup.GiftLogic")
local WarOrder = require("shop.WarOrder")
local Log = require("common.Log")
local EliteDefine = require("battle.EliteDefine")
local MaiDianDefine = require("MaiDian.MaiDianDefine")
local MainDianLogic = require("MaiDian.MaiDianLogic")
local TalismanLogic = require("talisman.TalismanLogic")
local TriggerDefine = require("trigger.TriggerDefine")
local TriggerLogic = require("trigger.TriggerLogic")
local CombatImpl = require("combat.CombatImpl")
local battleRougeCfg = require("excel.battleRouge").Sheet1
local WeekTaskLogic = require("dailyTask.WeekTaskLogic")
local TreasureChestLogic = require("treasurechest.TreasureChestLogic")

local BreakThroughTheme
local BattleGift

BATTLE_HANG_EXP_OUT_PERIOD = 60 -- 经验产出时间间隔
BATTLE_HANG_ITEM_OUT_PERIOD = 300 -- 战利品道具产出时间间隔
BATTLE_HANG_ITEM_OUT_PERIOD_2 = 7200 -- 战利品道具产出时间间隔2小时掉落一次

BATTLE_LOGIN_LEV_TIPS = 50 -- 登录检测挂机时间过久提示 最高等级
BATTLE_LOGIN_TIME_TIPS = 10 * 60 -- 登录检测挂机超过多久提示

BATTLE_MOPUP_CNT = 4
BATTLE_MOPUP_CNT_VIP = 8  --购买贵族之证礼包后, 扫荡次数

BATTLE_MOPUP_FREE_CNT = 1
BATTLE_MOPUP_FREE_CNT_VIP = 3

BATTLE_LEVEL_PLAYER_LIST = { }

local STATUS_NONE = 1 -- 不可领
local STATUS_CANGET = 2 -- 可领

local DIAMOND_COST_PER_HOUR = 20
local HOUR_SEC = 60 * 60
local DIAMOND_ADDITION = 1.5

-- @mafei 是否需要集中定义一个define 枚举
local PRINCIPAL_LINE_EVNET = 1
local BATTLE_ORDER_TYPE = 1

-- 挂机收益最多累计x秒
function getHangMaxTime(human)
    local hour = VipLogic.getPowerArgs(human, VipLogic.VIP_POWER10)
    local talismanAdd_HangHours = TalismanLogic.getTalismanAdd(human, TalismanLogic.OTHER_EFFECT_TBL.GJ_HANG_HOURS) or 0
    hour = hour + talismanAdd_HangHours
    return(hour or 0) * 3600
end

local function wrapHangFightNet(net, heroConfig, skillConfig)
    net.body = heroConfig.body
    net.height = heroConfig.height
    net.weight = heroConfig.width
    net.fireType = skillConfig and skillConfig.fireType or 0
    net.attackEffect = skillConfig and skillConfig.attackEffect or ""
    net.flyEffect = skillConfig and skillConfig.flyEffect or ""
    net.flyCoords = skillConfig and skillConfig.flyCoords or ""
    net.hitEffect = skillConfig and skillConfig.hitEffect or ""
    net.hitSound = skillConfig and skillConfig.hitSound or ""
end

local function fontBattleSkillNet(skillNet, skillID)
    local skillConfig = SkillExcel.skill[skillID]
    if not skillConfig then
        return
    end
    skillNet.skillID = skillID
    skillNet.fireType = skillConfig.fireType
    skillNet.readyAction = skillConfig.readyAction
    skillNet.attackPart = skillConfig.attackPart
    skillNet.attackAction = skillConfig.attackAction
    skillNet.readyEffect = skillConfig.readyEffect
    skillNet.attackEffect = skillConfig.attackEffect
    skillNet.hitEffect = skillConfig.hitEffect
    skillNet.flyEffect = skillConfig.flyEffect
    skillNet.flyCoords = skillConfig.flyCoords
    skillNet.hitBack = skillConfig.hitBack
    skillNet.effectTime = skillConfig.effectTime
    skillNet.contentType = skillConfig.contentType
    skillNet.content = skillConfig.content
    skillNet.movieEffect = skillConfig.movieEffect
    skillNet.quake = skillConfig.quake
    skillNet.flySound = skillConfig.flySound
    skillNet.fireSound = skillConfig.fireSound
    skillNet.hitSound = skillConfig.hitSound
    skillNet.cvSound = skillConfig.cvSound
    skillNet.screenMask = skillConfig.screenMask
    skillNet.screenCam = skillConfig.screenCam
    skillNet.largeEffect = skillConfig.largeEffect
    skillNet.lie = skillConfig.lie
    skillNet.cuoZhen = skillConfig.cuoZhen
    skillNet.skillDelay = skillConfig.skillDelay
    skillNet.isFraming = skillConfig.isFraming
    return true
end


-- 处理策划修改开放不同模式条件的函数
local function initBattleSpeData(human)
    human.db.battleSpeData = {
        [EliteDefine.COPY_ELITE_NORMAL] = true
    }
end

-- 处理策划修改开放不同模式条件的函数
local function getBattleSpeData(human)
    return human.db.battleSpeData
end

-- 处理策划修改开放不同模式条件的函数
local function updateBattleSpeData(human, modeType, state)
    human.db.battleSpeData[modeType] = state
end


-- 获取当前战役类型
local function BattleLogic_GetBattleType(human)
    if not human then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_GetBattleType human is null")
        return EliteDefine.COPY_ELITE_ERROR
    end

    return human.db.battleType
end

-- 设置当前战役类型
local function BattleLogic_SetBattleType(human, nBattleType)
    if not human or EliteDefine.COPY_ELITE_NORMAL > nBattleType or EliteDefine.COPY_ELITE_DIFFICULTY < nBattleType then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_SetBattleType 参数不正确!!!")
        return false
    end

    human.db.battleType = nBattleType
    print("[BattleLogic_SetBattleType] 玩家设置战役类型成功 id = "
            ..human.db._id.." nBattleType1 = "..nBattleType.." nBattleType2 = "..human.db.battleType)
    return true
end

-- 获取战役即将战斗id
local function BattleLogic_GetBattleBattleID(human)
    if not human then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "[BattleLogic_GetBattleBattleID] human is null")
        return -1
    end

    local nBattleType = BattleLogic_GetBattleType(human)
    --print("[BattleLogic_GetBattleBattleID] 获取到的战斗类型信息 id = "..human.db._id.." nBattleType = "..nBattleType)

    if EliteDefine.COPY_ELITE_ERROR >= nBattleType or EliteDefine.COPY_ELITE_DIFFICULTY < nBattleType then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_GetBattleBattleID 获取到的战斗类型不正确 id = "
                    ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name.." nBattleType = "..nBattleType)
        return -1
    end

    if EliteDefine.COPY_ELITE_NORMAL == nBattleType then
        return human.db.battleID
    elseif EliteDefine.COPY_LEVEL_ELITE == nBattleType then
        return human.db.battleID_elite
    elseif EliteDefine.COPY_ELITE_DIFFICULTY == nBattleType then
        return human.db.battleID_hard
    else
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_GetBattleBattleID 未处理的战斗类型 id = "
                    ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name.." nBattleType = "..nBattleType)
        return -1
    end
end

-- 根据类型获取战斗ID
local function BattleLogic_GetBattleIDByType(human, nBattleType)
    if not human or EliteDefine.COPY_ELITE_ERROR >= nBattleType or EliteDefine.COPY_ELITE_DIFFICULTY < nBattleType then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_GetBattleIDByType 传入的参数不正确 id = "
                    ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name.." nBattleType = "..nBattleType)
        return -1
    end

    if EliteDefine.COPY_ELITE_NORMAL == nBattleType then
        return human.db.battleID
    elseif EliteDefine.COPY_LEVEL_ELITE == nBattleType then
        return human.db.battleID_elite
    elseif EliteDefine.COPY_ELITE_DIFFICULTY == nBattleType then
        return human.db.battleID_hard
    end

    return -1
end

-- 设置 战役即将战斗id
local function BattleLogic_SetBattleBattleID(human, nValue)
    if not human or 0 > nValue then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_SetBattleBattleID 参数不正确")
        return false
    end

    local nBattleType = BattleLogic_GetBattleType(human)
    if EliteDefine.COPY_ELITE_ERROR >= nBattleType or EliteDefine.COPY_ELITE_DIFFICULTY < nBattleType then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_SetBattleBattleID 获取到的战斗类型不正确 id = "
                    ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name.." nBattleType = "..nBattleType)
        return false
    end

    if EliteDefine.COPY_ELITE_NORMAL == nBattleType then
        human.db.battleID = nValue
    elseif EliteDefine.COPY_LEVEL_ELITE == nBattleType then
        human.db.battleID_elite = nValue
    elseif EliteDefine.COPY_ELITE_DIFFICULTY == nBattleType then
        human.db.battleID_hard = nValue
    else
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_GetBattleGuaJiID 未处理的战斗类型 id = "
        ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name.." nBattleType = "..nBattleType)
        return false
    end

    return true
end

-- 设置 某个模式 战役即将战斗id
local function setBattleBattleIDByType(human, nBattleType, nValue)
    if EliteDefine.COPY_ELITE_NORMAL == nBattleType then
        human.db.battleID = nValue
    elseif EliteDefine.COPY_LEVEL_ELITE == nBattleType then
        human.db.battleID_elite = nValue
    elseif EliteDefine.COPY_ELITE_DIFFICULTY == nBattleType then
        human.db.battleID_hard = nValue
    end
end


-- 获取当前战役 已通关关卡/挂机关卡
local function BattleLogic_GetBattleGuaJiID(human)
    if not human then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_GetBattleGuaJiID human is null")
        return -1
    end

    local nBattleType = BattleLogic_GetBattleType(human)
    print("[BattleLogic_GetBattleGuaJiID] 获取到的战斗类型信息 id = "..human.db._id.." nBattleType = "..nBattleType)

    if EliteDefine.COPY_ELITE_ERROR >= nBattleType or EliteDefine.COPY_ELITE_DIFFICULTY < nBattleType then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_GetBattleGuaJiID 获取到的战斗类型不正确 id = "
                    ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name.." nBattleType = "..nBattleType)
        return -1
    end

    if EliteDefine.COPY_ELITE_NORMAL == nBattleType then
        return human.db.guajiID
    elseif EliteDefine.COPY_LEVEL_ELITE == nBattleType then
        return human.db.guajiID_elite
    elseif EliteDefine.COPY_ELITE_DIFFICULTY == nBattleType then
        return human.db.guajiID_hard
    else
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_GetBattleGuaJiID 未处理的战斗类型 id = "
                    ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name.." nBattleType = "..nBattleType)
        return -1
    end
end

-- 通过战斗类型获取当前战役 已通关关卡/挂机关卡 
local function BattleLogic_GetBattleGuaJiIDByType(human, nBattleType)
    if not human or EliteDefine.COPY_ELITE_ERROR >= nBattleType or EliteDefine.COPY_ELITE_DIFFICULTY < nBattleType then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_GetBattleGuaJiIDByType 传入的参数不正确 id = "
                    ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name.." nBattleType = "..nBattleType)
        return -1
    end

    if EliteDefine.COPY_ELITE_NORMAL == nBattleType then
        return human.db.guajiID
    elseif EliteDefine.COPY_LEVEL_ELITE == nBattleType then
        return human.db.guajiID_elite
    elseif EliteDefine.COPY_ELITE_DIFFICULTY == nBattleType then
        return human.db.guajiID_hard
    else
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_GetBattleGuaJiIDByType 未处理的战斗类型 id = "
                    ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name.." nBattleType = "..nBattleType)
        return -1
    end
end

-- 设置当前战役 已通关关卡/挂机关卡
local function BattleLogic_SetBattleGuaJiID(human, nValue)
    if not human or 0 > nValue then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_SetBattleGuaJiID 参数不正确")
        return false
    end

    local nBattleType = BattleLogic_GetBattleType(human)
    if EliteDefine.COPY_ELITE_ERROR >= nBattleType or EliteDefine.COPY_ELITE_DIFFICULTY < nBattleType then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_SetBattleGuaJiID 获取到的战斗类型不正确 id = "
                    ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name.." nBattleType = "..nBattleType)
        return false
    end

    if EliteDefine.COPY_ELITE_NORMAL == nBattleType then
        human.db.guajiID = nValue
    elseif EliteDefine.COPY_LEVEL_ELITE == nBattleType then
        human.db.guajiID_elite = nValue
    elseif EliteDefine.COPY_ELITE_DIFFICULTY == nBattleType then
        human.db.guajiID_hard = nValue
    else
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_GetBattleGuaJiID 未处理的战斗类型 id = "
        ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name.." nBattleType = "..nBattleType)
        return false
    end

    return true
end

-- 设置某个模式 已通关关卡/挂机关卡
local function setBattleGuaJiIDByType(human, nBattleType, nValue)
    if not human or 0 > nValue then
        return false
    end

    if EliteDefine.COPY_ELITE_NORMAL == nBattleType then
        human.db.guajiID = nValue
    elseif EliteDefine.COPY_LEVEL_ELITE == nBattleType then
        human.db.guajiID_elite = nValue
    elseif EliteDefine.COPY_ELITE_DIFFICULTY == nBattleType then
        human.db.guajiID_hard = nValue
    end
end


-- 获取配置信息
local function BattleLogic_GetBattleConfig(human)
    if not human then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_GetBattleConfig 参数不正确")
        return nil
    end

    local nBattleType = BattleLogic_GetBattleType(human)
    if EliteDefine.COPY_ELITE_ERROR >= nBattleType or EliteDefine.COPY_ELITE_DIFFICULTY < nBattleType then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_GetBattleConfig 获取到的战斗类型不正确 id = "
                    ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name.." nBattleType = "..nBattleType)
        return nil
    end

    if EliteDefine.COPY_ELITE_NORMAL == nBattleType then
        return BattleExcel
    elseif EliteDefine.COPY_LEVEL_ELITE == nBattleType then
        return BattleExcel_Elite
    elseif EliteDefine.COPY_ELITE_DIFFICULTY == nBattleType then
        return BattleExcel_Hard
    else
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_GetBattleGuaJiID 未处理的战斗类型 id = "
        ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name.." nBattleType = "..nBattleType)
        return nil
    end
end

-- 获取某个闯关模式的配置
local function getBattleCfgByType(nBattleType)
        if EliteDefine.COPY_ELITE_NORMAL == nBattleType then
        return BattleExcel
    elseif EliteDefine.COPY_LEVEL_ELITE == nBattleType then
        return BattleExcel_Elite
    elseif EliteDefine.COPY_ELITE_DIFFICULTY == nBattleType then
        return BattleExcel_Hard
    end
end


-- 获取每日通关层数
local function getDailyPassLevelNum(human)
    return human.db.battle_dailyPassLevelNum or 0
end

-- 增加每日通关层数
local function addDailyPassLevelNum(human, addNum)
    if not addNum or addNum <= 0 then
        return
    end

    human.db.battle_dailyPassLevelNum = (human.db.battle_dailyPassLevelNum or 0) + addNum
end

-- 重置每日通关层数
local function resetDailyPassLevelNum(human)
    if not human.db.battle_dailyPassLevelNum then
        return
    end

    human.db.battle_dailyPassLevelNum = 0
end

-- 跨天时, 对每日通关层数的处理
local function dailyPassLevelHandle(human)
    resetDailyPassLevelNum(human)
    Battle_DailyPassLevelInfo_Query(human)
end

------------------------------------------------多队伍挑战相关------------------------------------

-- 是否开启多队伍挑战
local function isOpenOtherTeam(human)
    local nowBattleID = BattleLogic_GetBattleBattleID(human)
    if nowBattleID >= EliteDefine.OPEN_OTHER_TEAM_LEVEL then
        return true
    end

    return false
end

-- 更新当前模式的出战队伍记录
local function updateTeamRecord(human, teamIdx)
    human.teamRecord = human.teamRecord or {}

    local teamRecord = human.teamRecord
    local nBattleType = BattleLogic_GetBattleType(human)
    teamRecord[nBattleType] = teamRecord[nBattleType] or {}

    teamRecord[nBattleType][teamIdx] = 1
end

-- 重置当前模式的出战队伍记录
local function resetTeamRecord(human)
    if not human.teamRecord then
        return
    end

    local nBattleType = BattleLogic_GetBattleType(human)
    human.teamRecord[nBattleType] = nil
end


-- 当前应该是第几支队伍出战(目前闯关最多有三支队伍)
local function getTeamIdx(human)
    local teamIdx = 1

    if not isOpenOtherTeam(human) then
        return teamIdx
    end

    local nBattleType = BattleLogic_GetBattleType(human)

    local teamRecord = human.teamRecord
    if teamRecord and teamRecord[nBattleType] then
        teamIdx = #teamRecord[nBattleType] + 1
    end

    return teamIdx
end

-- 获取当前出战阵容类型
local function getCombatTypeByTeamIdx(teamIdx)
    if teamIdx == 1 then
        return CombatDefine.COMBAT_TYPE1
    elseif teamIdx == 2 then
        return CombatDefine.COMBAT_TYPE30
    elseif teamIdx == 3 then
        return CombatDefine.COMBAT_TYPE38
    else
        -- assert(false, "错误类型")

        return CombatDefine.COMBAT_TYPE1
    end
end

-- 获取当前模式下当前关卡需要的队伍数量
local function getLevelTeamCnt(human)
    local teamCnt = 1
    local nowBattleID = BattleLogic_GetBattleBattleID(human)
    if nowBattleID >= EliteDefine.OPEN_OTHER_TEAM_LEVEL then
        local cfg = BattleLogic_GetBattleConfig(human)
        local singleCfg = cfg.node[nowBattleID]
        teamCnt = #singleCfg.monsterOutID
    end

    return teamCnt
end

-----------------------------------------------------------------------------------------------------




-----------------------------------------------------肉鸽玩法-----------------------------------------------------

--获取某个闯关模式的属性加成
local function getBattleAttrByType(human, battleType)
    if EliteDefine.COPY_ELITE_NORMAL == battleType then
        return human.db.normalBattleAttrData
    elseif EliteDefine.COPY_LEVEL_ELITE == battleType then
        return human.db.eliteBattleAttrData
    elseif EliteDefine.COPY_ELITE_DIFFICULTY == battleType then
        return human.db.hardBattleAttrData
    else
        assert(false, "类型错误")
    end
end

--统计当前已获得属性的类型
local function getAttrTypeList(ids)
    if not ids then
        return
    end

    local tpList = {}
    for id in pairs(ids) do
        local cfg = battleRougeCfg[id]
        tpList[cfg.type] = 1
    end
    return tpList
end


--获取某个ID所属类型中品质最高的ID
local function getMaxQualityId(id)
    local targetType = battleRougeCfg[id].type

    local q = 0
    local targetId = 0

    for k, cfg in ipairs(battleRougeCfg) do
        if cfg.type == targetType and cfg.quality > q then
            targetId = k
            q = cfg.quality
        end
    end

    return targetId
end


--生成随机数据
local function generateCfgWeightInfo(excludeTypeList)
    local totalWeight = 0
    local type2List = {}

    for k,v in ipairs(battleRougeCfg) do
        local tp = v.type
        if not excludeTypeList or not excludeTypeList[tp] then
            local weight = v.weight
            type2List[tp] = type2List[tp] or {weight = 0, typeList = {}}
            type2List[tp].weight = type2List[tp].weight + weight
            table.insert(type2List[tp].typeList, {id = k, weight = weight})
            totalWeight = totalWeight + weight
        end
    end

    local len = 0
    local randList = {}
    for _, tpData in pairs(type2List) do
        len = len + 1
        randList[len] = tpData
    end

    return totalWeight, randList
end


--随机出配置ID
local function getIndexByRand(totalWeight, randList)
    local tpList
    local weight = 0
    local randWeight = math.random(0, totalWeight)
    for _,v in ipairs(randList) do
        weight = weight + v.weight
        if randWeight <= weight then
            tpList = v
            break
        end
    end

    weight = 0
    randWeight = math.random(0, tpList.weight)
    for _,v in ipairs(tpList.typeList) do
        weight = weight + v.weight
        if randWeight <= weight then
            return v.id
        end
    end
end

--单条属性刷新时的随机处理
local function getIndexByRand2(totalWeight, randList, times)
    local tpList
    local weight = 0
    local randWeight = math.random(0, totalWeight)
    for _,v in ipairs(randList) do
        weight = weight + v.weight
        if randWeight <= weight then
            tpList = v
            break
        end
    end

    local id2NumList = {}
    for i=1, times do
        weight = 0
        randWeight = math.random(0, tpList.weight)
        for _,v in ipairs(tpList.typeList) do
            weight = weight + v.weight
            if randWeight <= weight then
                id2NumList[v.id] = (id2NumList[v.id] or 0)  +  1
                break
            end
        end
    end

    local finalId = 0
    local tbl = {lv = 0, cfgNum = 0}
    for id, num in pairs(id2NumList) do
        finalId = id
        local cfg = battleRougeCfg[id]
        tbl.lv = tbl.lv + num * cfg.lv
        tbl.cfgNum = tbl.cfgNum + num
    end

    if tbl.lv >= 4 then
        finalId = getMaxQualityId(finalId)
    end

    return finalId, tbl
end


--生成老号不同闯关模式的属性加成
local function generateDiffBattleAttr(human, battleType)
    local config
    local battleID = 0
    if EliteDefine.COPY_ELITE_NORMAL == battleType then
        battleID = human.db.guajiID
        config = BattleExcel.node
    elseif EliteDefine.COPY_LEVEL_ELITE == battleType then
        battleID = human.db.guajiID_elite
        config = BattleExcel_Elite.node
    elseif EliteDefine.COPY_ELITE_DIFFICULTY == battleType then
        battleID = human.db.guajiID_hard
        config = BattleExcel_Hard.node
    end

    local times = 0
    for i=1, battleID do
        local cfg = config[i]
        if cfg and cfg.isRouge and type(cfg.isRouge) == "number" and cfg.isRouge == 1 then
            times = times + 1
        end
    end

    if times > 0 then
        local totalWeight, randList = generateCfgWeightInfo()
        if totalWeight <= 0 then
            return
        end

        local type2Id = {}
        local attrList = {}

        for i=1, times do
            local randId = getIndexByRand(totalWeight, randList)
            local cfg = battleRougeCfg[randId]
            local targetId = type2Id[cfg.type]

            if targetId then
                attrList[targetId].lv = attrList[targetId].lv + cfg.lv
                attrList[targetId].cfgNum = attrList[targetId].cfgNum + 1
            else
                attrList[randId] = {
                    lv = cfg.lv,
                    cfgNum = 1
                }

                type2Id[cfg.type] = randId
            end
        end

        local tbl = {}
        for id, info in pairs(attrList) do
            if info.lv >= 4 then
                local newId = getMaxQualityId(id)
                tbl[id] = newId
            end
        end


        for oldId, newId in pairs(tbl) do
            attrList[newId] = {
                lv = attrList[oldId].lv,
                cfgNum = attrList[oldId].cfgNum
            }
            attrList[oldId] = nil
        end

        return attrList
    end
end


--获取当前ID所属类型一级的属性加成值
local function getMinAttrValeByID(id)
    local cfg = battleRougeCfg[id]
    if cfg.lv == 1 then
        return cfg.attrs[2]
    end

    local tp = cfg.type
    for k,v in ipairs(battleRougeCfg) do
        if v.type == tp and v.lv == 1 then
            return v.attrs[2]
        end
    end
end

--计算出战斗时使用的属性
local function calcAttr(attrData)
    local attrList = {}
    for id, info in pairs(attrData) do
        local attrCfg = battleRougeCfg[id]

        if attrCfg then
            local attrID = attrCfg.attrs[1]
            local minAttrVal = getMinAttrValeByID(id)
            attrList[attrID] = (attrList[attrID] or 0) + (minAttrVal  * info.lv)
        end
    end
    return attrList
end



-----------------------------------------------------------------------------------------------------------------


-- 获取奖励信息
local function BattleLogic_GetBattleRewards(human)
    if not human then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_GetBattleRewards 参数不正确")
        return nil
    end

    local nBattleType = BattleLogic_GetBattleType(human)
    if EliteDefine.COPY_ELITE_ERROR >= nBattleType or EliteDefine.COPY_ELITE_DIFFICULTY < nBattleType then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_GetBattleRewards 获取到的战斗类型不正确 id = "
                    ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name.." nBattleType = "..nBattleType)
        return nil
    end

    local tRewards = nil
    if EliteDefine.COPY_ELITE_NORMAL == nBattleType then
        if not human.db.battleRewards then
            Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_GetBattleRewards 玩家普通奖励未初始化进行初始化 id = "
                ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name.." nBattleType = "..nBattleType)
            human.db.battleRewards = {}
        end

        tRewards = human.db.battleRewards
    elseif EliteDefine.COPY_LEVEL_ELITE == nBattleType then
        if not human.db.battleRewards_elite then
            Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_GetBattleRewards 玩家精英奖励未初始化进行初始化 id = "
                ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name.." nBattleType = "..nBattleType)
            human.db.battleRewards_elite = {}
        end

        tRewards = human.db.battleRewards_elite
    elseif EliteDefine.COPY_ELITE_DIFFICULTY == nBattleType then
        if not human.db.battleRewards_hard then
            Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_GetBattleRewards 玩家困难奖励未初始化进行初始化 id = "
                ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name.." nBattleType = "..nBattleType)
            human.db.battleRewards_hard = {}
        end

        tRewards = human.db.battleRewards_hard
    else
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_GetBattleRewards 未处理的战斗类型 id = "
            ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name.." nBattleType = "..nBattleType)
    end

    return tRewards
end

-- 根据类型获取奖励信息
local function BattleLogic_GetBattleRewardsByType(human, nBattleType)
    if not human or EliteDefine.COPY_ELITE_ERROR >= nBattleType or EliteDefine.COPY_ELITE_DIFFICULTY < nBattleType then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "[BattleLogic_GetBattleRewardsByType] 参数不正确")
        return nil
    end

    local tRewards = nil
    if EliteDefine.COPY_ELITE_NORMAL == nBattleType then
        if not human.db.battleRewards then
            Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "[BattleLogic_GetBattleRewardsByType] 玩家普通奖励未初始化进行初始化 id = "
                ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name.." nBattleType = "..nBattleType)
            human.db.battleRewards = {}
        end

        --print("[BattleLogic_GetBattleRewardsByType] 返回普通奖励表")
        tRewards = human.db.battleRewards
    elseif EliteDefine.COPY_LEVEL_ELITE == nBattleType then
        if not human.db.battleRewards_elite then
            Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "[BattleLogic_GetBattleRewardsByType] 玩家精英奖励未初始化进行初始化 id = "
                ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name.." nBattleType = "..nBattleType)
            human.db.battleRewards_elite = {}
        end

        --print("[BattleLogic_GetBattleRewardsByType] 返回精英奖励表")
        tRewards = human.db.battleRewards_elite
    elseif EliteDefine.COPY_ELITE_DIFFICULTY == nBattleType then
        if not human.db.battleRewards_hard then
            Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "[BattleLogic_GetBattleRewardsByType] 玩家困难奖励未初始化进行初始化 id = "
                ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name.." nBattleType = "..nBattleType)
            human.db.battleRewards_hard = {}
        end

        --print("[BattleLogic_GetBattleRewardsByType] 返回困难奖励表")
        tRewards = human.db.battleRewards_hard
    else
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "[BattleLogic_GetBattleRewardsByType] 未处理的战斗类型 id = "
            ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name.." nBattleType = "..nBattleType)
    end

    return tRewards
end

local WeiLen = 30
-- 设置奖励信息
local function BattleLogic_SetBattleRewards(human, id)
    if not human or 0 >= id then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_SetBattleRewards 参数不正确")
        return false
    end


    local tRewards = BattleLogic_GetBattleRewards(human)
    if not tRewards then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_SetBattleRewards 获取到的奖励为空 id = "
            ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name)
        return false
    end

    local intIndex = math.ceil(id / WeiLen)
    local byteIndex = id % WeiLen
    tRewards[intIndex] = tRewards[intIndex] or 0
    tRewards[intIndex] = Util.setBit(tRewards[intIndex], byteIndex)

    return true
end

-- 获取挂机奖励表
local function BattleLogic_GetBattleOut(human)
    if not human then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_GetBattleOut 参数不正确")
        return nil
    end

    local nBattleType = BattleLogic_GetBattleType(human)
    if EliteDefine.COPY_ELITE_ERROR >= nBattleType or EliteDefine.COPY_ELITE_DIFFICULTY < nBattleType then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_GetBattleOut 获取到的战斗类型不正确 id = "
                    ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name.." nBattleType = "..nBattleType)
        return nil
    end

    return human.db.battleOut
    
    -- if EliteDefine.COPY_ELITE_NORMAL == nBattleType then
    --     -- if not human.db.battleRewards then
    --     --     Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_GetBattleRewards 玩家普通奖励未初始化进行初始化 id = "
    --     --         ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name.." nBattleType = "..nBattleType)
    --     --     human.db.battleOut = {}
    --     -- end

    --     tRewards = human.db.battleOut
    -- elseif EliteDefine.COPY_LEVEL_ELITE == nBattleType then
    --     -- if not human.db.battleRewards_elite then
    --     --     Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_GetBattleRewards 玩家精英奖励未初始化进行初始化 id = "
    --     --         ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name.." nBattleType = "..nBattleType)
    --     --     human.db.battleRewards_elite = {}
    --     -- end

    --     tRewards = human.db.battleOut_elite
    -- elseif EliteDefine.COPY_ELITE_DIFFICULTY == nBattleType then
    --     -- if not human.db.battleRewards_hard then
    --     --     Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_GetBattleRewards 玩家困难奖励未初始化进行初始化 id = "
    --     --         ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name.." nBattleType = "..nBattleType)
    --     --     human.db.battleRewards_hard = {}
    --     -- end

    --     tRewards = human.db.battleOut_hard
    -- else
    --     Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_GetBattleOut 未处理的战斗类型 id = "
    --         ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name.." nBattleType = "..nBattleType)
    -- end
end

function BattleLogic_InitOneBattleOut(human, now)

    human.db.battleOut = { }
    human.db.battleOut.expTs1 = now
    -- 经验开始产出时间戳
    human.db.battleOut.expTs2 = now
    -- 经验产出结算时间戳
    human.db.battleOut.itemTs1 = now
    -- 道具产出时间戳
    human.db.battleOut.itemTs2 = now
    -- 道具产出结算时间戳
    human.db.battleOut.exp = 0
    human.db.battleOut.jinbi = 0
    human.db.battleOut.greenExp = 0
    human.db.battleOut.qingbao = 0
    human.db.battleOut.items = nil
    -- 产出的待收获的战利品
    human.db.battleOut.equip = nil
    -- 产出的待收获的装备

    return true
end

-- 初始化所有挂机奖励领取列表
local function BattleLogic_InitAllBattleOut(human, now)
    if not human then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_InitAllBattleOut 参数不正确")
        return false
    end

    if not human.db.battleOut then
        BattleLogic_InitOneBattleOut(human, now)
    end

    -- if not human.db.battleOut_elite then
    --     human.db.battleOut_elite = {}
    --     BattleLogic_InitOneBattleOut(human.db.battleOut_elite, now)
    -- end 

    -- if not human.db.battleOut_hard then
    --     human.db.battleOut_hard = {}
    --     BattleLogic_InitOneBattleOut(human.db.battleOut_hard, now)
    -- end

    return true
end


-- 根据战斗类型获取配置
local function BattleLogic_GetBattleConfigByType(nBattleType)
    if EliteDefine.COPY_ELITE_ERROR >= nBattleType or EliteDefine.COPY_ELITE_DIFFICULTY < nBattleType then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_GetBattleConfigByType 参数不正确")
        return nil
    end

    if EliteDefine.COPY_ELITE_NORMAL == nBattleType then
        return BattleExcel
    elseif EliteDefine.COPY_LEVEL_ELITE == nBattleType then
        return BattleExcel_Elite
    elseif EliteDefine.COPY_ELITE_DIFFICULTY == nBattleType then
        return BattleExcel_Hard
    end

    return nil
end

-- 获取章节奖励
local function BattleLogic_GetChapterReward(human)
    if not human then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_GetChapterReward 参数不正确")
        return nil
    end

    local nBattleType = BattleLogic_GetBattleType(human)
    print("[BattleLogic_GetChapterReward] nBattleType = "..nBattleType)

    if EliteDefine.COPY_ELITE_ERROR >= nBattleType or EliteDefine.COPY_ELITE_DIFFICULTY < nBattleType then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_GetChapterReward 获取到的战斗类型不正确 id = "
                    ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name.." nBattleType = "..nBattleType)
        return nil
    end

    if EliteDefine.COPY_ELITE_NORMAL == nBattleType then
        if not human.db.chapterReward then
            human.db.chapterReward = {}
        end

        return human.db.chapterReward
    elseif EliteDefine.COPY_LEVEL_ELITE == nBattleType then
        if not human.db.chapterReward_Elite then
            human.db.chapterReward_Elite = {}
        end

        return human.db.chapterReward_Elite
    elseif EliteDefine.COPY_ELITE_DIFFICULTY == nBattleType then
        if not human.db.chapterReward_Hard then
            human.db.chapterReward_Hard = {}
        end

        return human.db.chapterReward_Hard
    else
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_GetChapterReward 未处理的战斗类型 id = "
        ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name.." nBattleType = "..nBattleType)
        return nil
    end
end

-- 查询难度
local function BattleLogic_QueryDifficulty(human)
    local nNowBattleType = BattleLogic_GetBattleType(human)
    if EliteDefine.COPY_ELITE_ERROR >= nNowBattleType then
        print("[BattleLogic_QueryDifficulty] 查询难度获取到的战斗ID数值、战斗类型不正确 id = "
                    ..human.db._id.." nNowBattleType = "..nNowBattleType)
        return
    end

    local msgRet = Msg.gc.GC_ELITE_OPEN
    msgRet.curCopyType = nNowBattleType

    msgRet.copyOpenType[0]=0
    
    local battleSpeData = getBattleSpeData(human)


    --精英副本开启, 普通关卡符合条件
    local nBattleID = human.db.battleID
    local nIndex = EliteDefine.COPY_LEVEL_ELITE - 1

    -- if human.db.lv >= EliteDefine.COPY_ELITE_ULEV and 
    --     nBattleID > EliteDefine.COPY_ELITE_LEVEL then
    --     msgRet.copyOpenType[0] = msgRet.copyOpenType[0]+1
    --     msgRet.copyOpenType[nIndex] = 1
    -- else
    --     msgRet.copyOpenType[0] = msgRet.copyOpenType[0]+1
    --     msgRet.copyOpenType[nIndex] = 0
    -- end


    if battleSpeData[EliteDefine.COPY_LEVEL_ELITE] then
        msgRet.copyOpenType[0] = msgRet.copyOpenType[0]+1
        msgRet.copyOpenType[nIndex] = 1
    else
        if human.db.lv >= EliteDefine.COPY_ELITE_ULEV and 
            nBattleID > EliteDefine.COPY_ELITE_LEVEL then
            msgRet.copyOpenType[0] = msgRet.copyOpenType[0]+1
            msgRet.copyOpenType[nIndex] = 1
        else
            msgRet.copyOpenType[0] = msgRet.copyOpenType[0]+1
            msgRet.copyOpenType[nIndex] = 0
        end
    end


    --困难副本开启，精英关卡符合条件
    local nBattleIDElite = human.db.battleID_elite
    nIndex = EliteDefine.COPY_ELITE_DIFFICULTY - 1
    -- if human.db.lv >= EliteDefine.COPY_ELITE_ULEV and 
    --     nBattleIDElite > EliteDefine.COPY_ELITE_LEVEL then
    --     msgRet.copyOpenType[0] = msgRet.copyOpenType[0]+1
    --     msgRet.copyOpenType[nIndex] = 1
    -- else
    --     msgRet.copyOpenType[0] = msgRet.copyOpenType[0]+1
    --     msgRet.copyOpenType[nIndex] = 0
    -- end

    if battleSpeData[EliteDefine.COPY_ELITE_DIFFICULTY] then
        msgRet.copyOpenType[0] = msgRet.copyOpenType[0]+1
        msgRet.copyOpenType[nIndex] = 1
    else
        if human.db.lv >= EliteDefine.COPY_ELITE_ULEV and 
            nBattleIDElite > EliteDefine.COPY_ELITE_LEVEL then
            msgRet.copyOpenType[0] = msgRet.copyOpenType[0]+1
            msgRet.copyOpenType[nIndex] = 1
        else
            msgRet.copyOpenType[0] = msgRet.copyOpenType[0]+1
            msgRet.copyOpenType[nIndex] = 0
        end
    end


    Msg.send(msgRet,human.fd)

    local info=Util.printTable(msgRet)
    print(info)
end

-- 设置战役通关标记
local function BattleLogic_SetBattleAdopt(human, nBattleType, nValue)
    if not human or EliteDefine.COPY_ELITE_ERROR >= nBattleType or EliteDefine.COPY_ELITE_DIFFICULTY < nBattleType then
        return
    end

    if EliteDefine.COPY_ELITE_NORMAL == nBattleType then
        human.db.battleadopt = nValue
    elseif EliteDefine.COPY_LEVEL_ELITE == nBattleType then
        human.db.battleadopt_elite = nValue
    elseif EliteDefine.COPY_ELITE_DIFFICULTY == nBattleType then
        human.db.battleadopt_haed = nValue
    end
end

-- 获取战役通关标记
local function BattleLogic_GetBattleAdopt(human, nBattleType)
    if not human or EliteDefine.COPY_ELITE_ERROR >= nBattleType or EliteDefine.COPY_ELITE_DIFFICULTY < nBattleType then
        return 0
    end

    if EliteDefine.COPY_ELITE_NORMAL == nBattleType then
        return human.db.battleadopt 
    elseif EliteDefine.COPY_LEVEL_ELITE == nBattleType then
        return human.db.battleadopt_elite
    elseif EliteDefine.COPY_ELITE_DIFFICULTY == nBattleType then
        return human.db.battleadopt_haed
    end
end

-- 获取额外增加扫荡次数
local function BattleLogic_GetAddFreeMopupNum(human)
    if not human then
        return -1
    end

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

    return human.db.mopupAddFreeCnt
end

-- 设置额外增加扫荡次数
local function BattleLogic_SetAddFreeMopupNum(human, nValue)
    if not human then
        return
    end

    print("[BattleLogic_SetAddFreeMopupNum] 消耗的额外次数 nValue = "..nValue)
    human.db.mopupAddFreeCnt = human.db.mopupAddFreeCnt + nValue

    -- if 0 > human.db.mopupAddFreeCnt then
    --     human.db.mopupAddFreeCnt = 0
    -- end
end

--计算当前关卡能获得多少次小游戏次数
local function calcGameTimes(config, levelId)
    local gameTimes = 0

    local allCfg = config
    for i=1, levelId-1 do
        local cfg = allCfg[i]
        if cfg and cfg and type(cfg.gameType) == "number" and cfg.gameType == 1 then
            gameTimes = gameTimes + 1
        end
    end
    return gameTimes
end


function getBattleRoleList(nBattleType, worldMapId)
    if not BATTLE_LEVEL_PLAYER_LIST then
        BATTLE_LEVEL_PLAYER_LIST = {}
    end

    if not BATTLE_LEVEL_PLAYER_LIST[nBattleType] then
        BATTLE_LEVEL_PLAYER_LIST[nBattleType] = {}
    end

    BATTLE_LEVEL_PLAYER_LIST[nBattleType][worldMapId] = BATTLE_LEVEL_PLAYER_LIST[nBattleType][worldMapId] or { }

    local tBattleConfig = BattleLogic_GetBattleConfigByType(nBattleType)
    if not tBattleConfig then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "[getBattleRoleList] 获取不到对应的配置 nBattleType = "
                ..nBattleType.." worldMapId ="..worldMapId)
        print("[getBattleRoleList] 获取配置失败")
        return
    end

    -- local config = BattleExcel.map[worldMapId]
    local config = tBattleConfig.map[worldMapId]
    QueryRoleByNodeID = { guajiID = { ["$gte"] = config.minLevel, ["$lte"] = config.maxLevel }, nBattleType = nBattleType}
    local fields = { }
    RoleLogic.makeRoleBaseFields(fields)
    LuaMongo.find(DB.db_char, QueryRoleByNodeID, fields, 50)
    local len = 0
    while true do
        local charDb = { }
        if not LuaMongo.next(charDb) then
            break
        end
        len = len + 1
        BATTLE_LEVEL_PLAYER_LIST[nBattleType][worldMapId][charDb._id] = { }
        RoleLogic.makeRoleBase(charDb, BATTLE_LEVEL_PLAYER_LIST[nBattleType][worldMapId][charDb._id])
        if len >= 50 then
            return
        end
    end
end

function initAfterStart()
    local len = #BattleExcel.map
    for i = 1, len do
        getBattleRoleList(EliteDefine.COPY_ELITE_NORMAL, i)
    end

    len = #BattleExcel_Elite.map
    for i = 1, len do
        getBattleRoleList(EliteDefine.COPY_LEVEL_ELITE, i)
    end

    len = #BattleExcel_Hard.map
    for i = 1, len do
        getBattleRoleList(EliteDefine.COPY_ELITE_DIFFICULTY, i)
    end
end


-- 使用老的条件来判断当前模式是否开启
local function isOpenModeByOldCond(human, nSelectDiff)
    -- 默认难度直接通过
    if EliteDefine.COPY_ELITE_NORMAL == nSelectDiff then
        return true
    end

    -- 等级不够
    if human.db.lv < EliteDefine.COPY_ELITE_ULEV then
        return false
    end

    local nChoseBattleID = (EliteDefine.COPY_LEVEL_ELITE == nSelectDiff) and human.db.battleID or human.db.battleID_elite
    
    return nChoseBattleID > EliteDefine.COPY_ELITE_LEVEL_OLD and true or false
end

-- 修正三个模式中已经通关的模式的battleID, GuajiaID
local function restoreReallyBattleID(human)
    for i=EliteDefine.COPY_ELITE_NORMAL, EliteDefine.COPY_ELITE_DIFFICULTY do
        local tag = BattleLogic_GetBattleAdopt(human, i)
        local nowBattleID = BattleLogic_GetBattleIDByType(human, i)
        local battleCfg = getBattleCfgByType(i)
        if tag == 1 and #battleCfg.node > nowBattleID then
            setBattleGuaJiIDByType(human, i, nowBattleID)

            setBattleBattleIDByType(human, i, nowBattleID+1)

            BattleLogic_SetBattleAdopt(human, i, 0)
        end
    end
end




function onLogin(human)
    --处理老号已经在小游戏玩法上线前已经推过图没有获得小游戏次数
    if not human.db.battleGameTimes then
        human.db.battleGameTimes = calcGameTimes(BattleExcel.node, human.db.battleID)
    end

    --新增精英次数补偿
    if not human.db.eliteBattleGameTimes then
        human.db.eliteBattleGameTimes = calcGameTimes(BattleExcel_Elite.node, human.db.battleID_elite)
    end

    --新增困难次数补偿
    if not human.db.hardBattleGameTimes then
        human.db.hardBattleGameTimes = calcGameTimes(BattleExcel_Hard.node, human.db.battleID_hard)
    end

    -- 处理老号的闯关的属性加成
    local normalBattleAttrData = getBattleAttrByType(human, EliteDefine.COPY_ELITE_NORMAL)
    if not normalBattleAttrData then
        human.db.normalBattleAttrData = {}
        human.db.normalBattleAttrData.chosenAttrList = generateDiffBattleAttr(human, EliteDefine.COPY_ELITE_NORMAL)

        human.db.eliteBattleAttrData = {}
        human.db.eliteBattleAttrData.chosenAttrList = generateDiffBattleAttr(human, EliteDefine.COPY_LEVEL_ELITE)

        human.db.hardBattleAttrData = {}
        human.db.hardBattleAttrData.chosenAttrList = generateDiffBattleAttr(human, EliteDefine.COPY_ELITE_DIFFICULTY)
    end

    local  battleSpeData = getBattleSpeData(human)
    if not battleSpeData then
        initBattleSpeData(human)

        battleSpeData = getBattleSpeData(human)

        -- 精英模式
        local res = isOpenModeByOldCond(human, EliteDefine.COPY_LEVEL_ELITE)
        updateBattleSpeData(human, EliteDefine.COPY_LEVEL_ELITE, res)

        -- 困难模式
        res = isOpenModeByOldCond(human, EliteDefine.COPY_ELITE_DIFFICULTY)
        updateBattleSpeData(human, EliteDefine.COPY_ELITE_DIFFICULTY, res)
    end


    restoreReallyBattleID(human)


    if human.db.lv >= BATTLE_LOGIN_LEV_TIPS then
        return
    end
    calcBattleOut(human)
    local now = os.time()
    -- local expTime = now - human.db.battleOut.expTs1
    local tBattleOut = BattleLogic_GetBattleOut(human)
    if tBattleOut then
        local expTime = now - tBattleOut.expTs1

        if expTime >= BATTLE_LOGIN_TIME_TIPS then
            local msgRet = Msg.gc.GC_BATTLE_HANG_TIME_SPILL
            msgRet.time = expTime
            Msg.send(msgRet, human.fd)
        end
    else
        print("[BattleLogic_onLogin] 获取不到奖励数据 id = "
                    ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name)
    end
end

-- 是否领取
-- local WeiLen = 30
function isGetReward(human, id)
    local flags = BattleLogic_GetBattleRewards(human)
    if not flags then
        return false
    end

    -- if not human.db.battleRewards then
    --     return
    -- end

    --local flags = human.db.battleRewards
    local intIndex = math.ceil(id / WeiLen)
    if not flags[intIndex] then
        return
    end
    local byteIndex = id % WeiLen
    if Util.getBit(flags[intIndex], byteIndex) > 0 then
        return true
    end
end

-- 通过类型获取奖励 判断是否领取
function isGetRewardByType(human, id, nBattleType)
    local flags = BattleLogic_GetBattleRewardsByType(human, nBattleType)
    if not flags then
        print("[isGetRewardByType] 获取不到对应的奖励表 id = "..human.db._id .. " nBattleType = "..nBattleType)
        return false
    end

    local intIndex = math.ceil(id / WeiLen)
    if not flags[intIndex] then
        print("[isGetRewardByType] 没有对对应的ID领取奖励 intIndex id = "..id .. " nBattleType = "..nBattleType)
        return false
    end

    local byteIndex = id % WeiLen
    -- if Util.getBit(flags[intIndex], byteIndex) > 0 then
    --     return true
    -- end

    local nBit = Util.getBit(flags[intIndex], byteIndex)
    -- print("[isGetRewardByType] nBit =  ".. nBit)

    return Util.getBit(flags[intIndex], byteIndex) > 0 and true or false
end

function getTongguanIndex(human)
    local tBattleRewards =  BattleLogic_GetBattleRewards(human)
    local tBattleConfig = BattleLogic_GetBattleConfig(human)
    if not tBattleRewards or not tBattleConfig then
        return 1
    end
    
    --local flags = human.db.battleRewards
    -- for id, v in ipairs(BattleExcel.node) do
    for id, v in ipairs(tBattleConfig.node) do
        if #v.tongguan ~= 0 then
            local intIndex = math.ceil(id / WeiLen)
            -- if not flags[intIndex] then
            if not tBattleRewards[intIndex] then
                return id
            end
            local byteIndex = id % WeiLen
            -- if Util.getBit(flags[intIndex], byteIndex) == 0 then
            if Util.getBit(tBattleRewards[intIndex], byteIndex) == 0 then
                return id
            end
        end
    end
    return
end

-- 设置领取
function setGetReward(human, id)
    local tBattleRewards =  BattleLogic_GetBattleRewards(human)
    if not tBattleRewards then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_GetBattleRewards 参数不正确")
        print("[setGetReward] 不存在对应的 奖励配置")
        return
    end
    -- if not human.db.battleRewards then
    --     human.db.battleRewards = { }
    -- end
    -- local flags = human.db.battleRewards
    local intIndex = math.ceil(id / WeiLen)
    local byteIndex = id % WeiLen
    tBattleRewards[intIndex] = tBattleRewards[intIndex] or 0
    tBattleRewards[intIndex] = Util.setBit(tBattleRewards[intIndex], byteIndex)
end

-- 通过类型设置领取
function setGetRewardByType(human, id, nBattleType)
    local tBattleRewards =  BattleLogic_GetBattleRewardsByType(human, nBattleType)
    if not tBattleRewards then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_GetBattleRewards 参数不正确")
        print("[setGetReward] 不存在对应的 奖励配置")
        return
    end
  
    local intIndex = math.ceil(id / WeiLen)
    local byteIndex = id % WeiLen
    tBattleRewards[intIndex] = tBattleRewards[intIndex] or 0
    tBattleRewards[intIndex] = Util.setBit(tBattleRewards[intIndex], byteIndex)
end

-- 是否有可领取的
function hasCanGetReward(human)
    local tBattleConfig = BattleLogic_GetBattleConfig(human)
    local nGuaJiID = BattleLogic_GetBattleGuaJiID(human)
    if not tBattleConfig or -1 >= nGuaJiID then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_GetBattleRewards 参数不正确")
        print("[hasCanGetReward] 不存在对应的 奖励配置")
        return
    end

    for id, cf in ipairs(tBattleConfig.node) do
        if id > nGuaJiID then
            break
        end
        if #cf.tongguan > 0 and not isGetReward(human, id) then
            return true
        end
    end
end

local BATTLEID_2_NAME = nil
function getBattleName(human, battleID)
    local tBattleConfig = BattleLogic_GetBattleConfig(human)
    local nBattleType = BattleLogic_GetBattleType(human)
    if not tBattleConfig or EliteDefine.COPY_ELITE_ERROR >= nBattleType then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_GetBattleRewards 参数不正确")
        print("[getBattleName] 不存在对应的 奖励配置")
        return
    end

    if not BATTLEID_2_NAME then
        BATTLEID_2_NAME = { }
    end

    if not BATTLEID_2_NAME[nBattleType]  then
        BATTLEID_2_NAME[nBattleType] = {}
        
        for id, cf in ipairs(tBattleConfig.node) do
            -- BATTLEID_2_NAME[id] = cf.name
            BATTLEID_2_NAME[nBattleType][id] = cf.name
        end
    end
    
    local nodeConfig = tBattleConfig.node[battleID]
    local mapConfig = nodeConfig and tBattleConfig.map[nodeConfig.mapID]
    local mapName = mapConfig and mapConfig.name or ""
    return BATTLEID_2_NAME[nBattleType][battleID] or "", mapName
end

function getBattleNameByType(battleID, nBattleType)
    local tBattleConfig = BattleLogic_GetBattleConfigByType( nBattleType)
    if not tBattleConfig or EliteDefine.COPY_ELITE_ERROR >= nBattleType then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "BattleLogic_GetBattleRewards 参数不正确")
        print("[getBattleNameByType] 不存在对应的 奖励配置")
        return
    end

    if not BATTLEID_2_NAME then
        BATTLEID_2_NAME = { }
    end

    if not BATTLEID_2_NAME[nBattleType]  then
        BATTLEID_2_NAME[nBattleType] = {}

        for id, cf in ipairs(tBattleConfig.node) do
            -- BATTLEID_2_NAME[id] = cf.name
            BATTLEID_2_NAME[nBattleType][id] = cf.name
        end
    end
    
    local nodeConfig = tBattleConfig.node[battleID]
    local mapConfig = nodeConfig and tBattleConfig.map[nodeConfig.mapID]
    local mapName = mapConfig and mapConfig.name or ""
    return BATTLEID_2_NAME[nBattleType][battleID] or "", mapName
end

function hangFightQuery(human)
    local nBattleID = BattleLogic_GetBattleBattleID(human)
    local tBattleExcel = BattleLogic_GetBattleConfig(human)
    if -1 >= nBattleID or not tBattleExcel then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "hangFightQuery 参数不正确")
        print("[hangFightQuery] 不存在对应的 奖励配置 和战斗关卡ID id = "..human.db._id)
        return
    end

    -- local battleID = human.db.battleID > #BattleExcel.node and #BattleExcel.node or human.db.battleID
    local battleID = nBattleID > #tBattleExcel.node and #tBattleExcel.node or nBattleID
    local nowNodeConfig = tBattleExcel.node[battleID]
    -- local monsterOutID = nowNodeConfig.monsterOutID
    
    local teamIdx = getTeamIdx(human)
    local monsterOutID = nowNodeConfig.monsterOutID[teamIdx]

    --local monsterOutConfig = MonsterExcel.monsterOut[monsterOutID]
    local monsterOutConfig = BattleMonsterExcel.monsterOut[monsterOutID] or BattleMonsterExcel2.monsterOut[monsterOutID]

    local msgRet = Msg.gc.GC_BATTLE_HANG_FIGHT
    local len = 0
    for _, monster in ipairs(monsterOutConfig.member) do
        local monsterID = monster[1]
        len = len + 1
        -- local monsterConfig = MonsterExcel.monster[monsterID]
        local monsterConfig = BattleMonsterExcel.monster[monsterID] or BattleMonsterExcel2.monster[monsterID]
        local skillConfig = SkillExcel.skill[monsterConfig.normalAtkID]
        wrapHangFightNet(msgRet.monsterList[len], monsterConfig, skillConfig)
    end
    msgRet.monsterList[0] = len
    len = 0
    if human.db.combatHero[CombatDefine.COMBAT_TYPE1] then
        local tb = { }
        for i = 1, #human.db.combatHero[CombatDefine.COMBAT_TYPE1] do
            tb[#tb + 1] = human.db.combatHero[CombatDefine.COMBAT_TYPE1][i]
        end
        local tb2 = { }
        for i = 1, 3 do
            local r = math.ceil(math.random(0, #tb))
            r = r == 0 and 1 or r
            tb2[#tb2 + 1] = tb[r]
            tb[r] = tb[#tb]
            tb[#tb] = nil
            if not tb[1] then
                break
            end
        end
        for i = 1, #tb2 do
            len = len + 1
            local heroGrid = HeroLogic.getHeroGridByUuid(human, tb2[i])
            if heroGrid then
                local heroConfig = HeroExcel.hero[heroGrid.id]
                local _, skinSkillConf = SkinLogic.getHeroSkin(human, tb2[i])
                local skillConfig = SkillExcel.skill[heroConfig.normalAtkID]
                if skinSkillConf then
                    skillConfig = SkillExcel.skill[skinSkillConf.normalAtkID]
                end
                wrapHangFightNet(msgRet.attackerList[i], heroConfig, skillConfig)
                local body, head = SkinLogic.getBody(human, tb2[i])
                if body then
                    msgRet.attackerList[i].body = body
                end
            end
        end
    end
    msgRet.attackerList[0] = len
    -- Msg.trace(msgRet)
    Msg.send(msgRet, human.fd)
end

local function getExtraRewardID(human)
    local tBattleExcel = BattleLogic_GetBattleConfig(human)
    if not tBattleExcel then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "[getExtraRewardID] 参数不正确")
        print("[getExtraRewardID] 不存在对应的 奖励配置 id = "..human.db._id)
        return nil, nil
    end

    local config = tBattleExcel.extraReward
    local battleExtraReward = human.db.battleExtraReward
    for id, data in ipairs(config) do
        if not battleExtraReward or not battleExtraReward[id] then
            return id, data
        end
    end
end

local function getExtraRewardState(human, extraRewardID)
    local tBattleExcel = BattleLogic_GetBattleConfig(human)
    local nGuaJiID = BattleLogic_GetBattleGuaJiID(human)
    if not tBattleExcel or -1 >= nGuaJiID then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "[getExtraRewardState] 参数不正确")
        print("[getExtraRewardState] 不存在对应的 奖励配置 id = "..human.db._id)
        return 0
    end

    local config = tBattleExcel.extraReward[extraRewardID]
    if not config then
        return 0
    end

    if human.db.battleExtraReward and human.db.battleExtraReward[extraRewardID] then
        return 0
    end

    local guajiID = nGuaJiID
    if guajiID >= config.needBattleID then
        return 1
    end
    return 0
end

local function makeExtraRewardShow(human, net)
    local tBattleExcel = BattleLogic_GetBattleConfig(human)
    if not tBattleExcel then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "[makeExtraRewardShow] 参数不正确")
        print("[makeExtraRewardShow] 不存在对应的 奖励配置 id = "..human.db._id)
        return
    end

    local config = tBattleExcel.extraReward
    local extraID, data = getExtraRewardID(human)
    if not extraID or not data then
        return
    end

    net[0] = 1
    net[1].needBattleID = data.needBattleID
    net[1].descID = data.descID
    net[1].desc = data.desc or ""
    net[1].extraHero[0] = 0
    net[1].extraItem[0] = 0
    if data.extraHeroID > 0 then
        net[1].extraHero[0] = 1
        HeroGrid.makeHeroSimpleByID(net[1].extraHero[1], data.extraHeroID, nil, nil, human)
        HeroGrid.makeHeroSimpleByGeneral(net[1].extraHero[1], data.extraHeroID)
    else
        net[1].extraItem[0] = 1
        Grid.makeItem(net[1].extraItem[1], data.extraReward[1], data.extraReward[2])
    end

    net[1].state = getExtraRewardState(human, extraID)
end

-- 随机道具
local function randItemOut(dropRule, weightSum)
    if weightSum < 1 then
        return
    end
    local r = math.random(1, weightSum)
    for i = 1, #dropRule do
        local itemID = dropRule[i][1]
        local itemCnt1 = dropRule[i][2]
        local itemCnt2 = dropRule[i][3]
        local weight = dropRule[i][4]
        if r <= weight then
            local itemCnt = math.random(itemCnt1, itemCnt2)
            return itemID, itemCnt
        end
        r = r - weight
    end
end

function fontExtraDataSSZH(net, mapID, nowBattleID, human)
    local nodeCnt = 0
    local tBattleExcel = BattleLogic_GetBattleConfig(human)
    if not tBattleExcel then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "[fontExtraDataSSZH] 参数不正确")
        print("[fontExtraDataSSZH] 不存在对应的 奖励配置 id = "..human.db._id)
        return
    end

    if ProjectLogic.isSszh() then
        for i = 1, #tBattleExcel.node do
            local nodeTempConfig = tBattleExcel.node[i]
            if nodeTempConfig.mapID == mapID then
                nodeCnt = nodeCnt + 1
                local posNet = net.posList[nodeCnt]
                local pos = nodeTempConfig.pos
                posNet.battleID = i
                posNet.posX = pos and pos[1] or 0
                posNet.posY = pos and pos[2] or 0
                posNet.nodeName = getBattleName(human, i)
                posNet.roleBase[0] = 0
                if i == nowBattleID then
                    local videoTb = BattleDBLogic.queryBattleDbByNodeID(i)
                    local sharkData = videoTb and videoTb.shark[2]
                    if sharkData then
                        posNet.roleBase[0] = 1
                        RoleLogic.makeRoleBase(videoTb.roleBase, posNet.roleBase[1])
                    end
                end
                posNet.isBig = nodeTempConfig.isBig or 0
            end
        end
    end
    net.posList[0] = nodeCnt
end

function fontVideoInfo(human, net, monsterConfig)
    local combatHero = CombatPosLogic.getCombatHeros(human, CombatDefine.COMBAT_TYPE1)
    local len = #combatHero
    -- 自己没队伍时，不显示
    if len == 0 then
        net.ownBody[0] = 0
        net.enemyBody[0] = 0
    else
        -- 设置真实数据
        local cnt = 0
        for k, v in pairs(combatHero) do
            -- body数据
            if v ~= "0" then
                local heroGird, bagIndex = HeroLogic.getHeroGridByUuid(human, v)
                if heroGird ~= nil then
                    cnt = cnt + 1
                    local config = HeroExcel.hero[heroGird.id]
                    if config then
                        local attrConfig = HeroDefine.getAttrConfig(heroGird.id, config.star)
                        local normalAtkID = attrConfig.normalAtkID
                        local skill = attrConfig.battleSkill

                        local body, head = SkinLogic.getBody(human, bagIndex)

                        net.ownBody[cnt].body = body or config.body
                        fontBattleSkillNet(net.ownBody[cnt].atkId, normalAtkID)
                        fontBattleSkillNet(net.ownBody[cnt].skill, skill)
                        net.ownBody[cnt].pos = k
                        net.ownBody[cnt].atkType = SkillExcel.skill[normalAtkID].areaPara

                        local attrs = ObjHuman.getHeroAttrs(human, heroGird.bagIndex)
                        net.ownBody[cnt].hp = attrs[RoleDefine.HP]
                    end
                end
            end
        end
        net.ownBody[0] = cnt

        cnt = 0
        local memberConfig = monsterConfig.member
        for k, v in pairs(memberConfig) do
            -- body数据
            cnt = cnt + 1
            -- local config = MonsterExcel.monster[v[1]]
            local config = BattleMonsterExcel.monster[v[1]] or BattleMonsterExcel2.monster[v[1]]
            if config then
                local normalAtkID = config.normalAtkID

                net.enemyBody[cnt].body = config.body
                fontBattleSkillNet(net.enemyBody[cnt].atkId, normalAtkID)
                fontBattleSkillNet(net.enemyBody[cnt].skill, normalAtkID)
                net.enemyBody[cnt].pos = k
                net.enemyBody[cnt].atkType = SkillExcel.skill[normalAtkID].areaPara

                local attrID = monsterConfig.attrID[k]
                -- local attrConfig = MonsterExcel.monsterAttr[attrID].attrs
                local attrConfig = BattleMonsterExcel.monsterAttr[attrID] and BattleMonsterExcel.monsterAttr[attrID].attrs
                if not attrConfig then
                    attrConfig = BattleMonsterExcel2.monsterAttr[attrID] and BattleMonsterExcel2.monsterAttr[attrID].attrs
                end
                if not attrConfig then
                    print("[fontVideoInfo] 根据attrID获取到的 attrConfig 为空 k = "..k.. " attrID = "..attrID)
                end

                for _, value in pairs(attrConfig) do
                    if value[1] == RoleDefine.HP then
                        net.enemyBody[cnt].hp = value[2]
                        break
                    end
                end
            end
        end
        net.enemyBody[0] = cnt
    end
end

-- 战役主界面查询
function query(human)
    local msgRet = Msg.gc.GC_BATTLE_QUERY
    local nNowBattleID = BattleLogic_GetBattleBattleID(human)
    local tBattleConfig = BattleLogic_GetBattleConfig(human)
    local nNowGuaJiID = BattleLogic_GetBattleGuaJiID(human)
    local nBattleType = BattleLogic_GetBattleType(human)
    if -1 >= nNowBattleID or not tBattleConfig or -1 >= nNowGuaJiID or -1 >= nBattleType then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "[query] 获取到的战斗ID或奖励配置不正确")
        print("[query] 获取到的战斗ID或奖励配置不正确 id = "..human.db._id)
        return
    end
    
    print("[BattleLogic query] nNowGuaJiID ="..nNowGuaJiID.." nNowBattleID ="..nNowBattleID)
    
    msgRet.nowBattleID = nNowBattleID
    local configNode = #tBattleConfig.node
    if nNowBattleID > configNode then
        print("[query] 玩家的战斗ID不正确,进行纠正 nNowBattleID = "..nNowBattleID.." 最大战斗ID = "..configNode)
        nNowBattleID = configNode
        BattleLogic_SetBattleBattleID(human, nNowBattleID)
    end
    
    if nNowGuaJiID > configNode then
        print("[query] 玩家的挂机ID不正确,进行纠正 nNowGuaJiID = "..nNowGuaJiID.." 最大挂机ID = "..configNode)
        nNowGuaJiID = configNode
        BattleLogic_SetBattleGuaJiID(human, nNowGuaJiID)
    end

    local nBattleAdopt = BattleLogic_GetBattleAdopt(human, nBattleType)

    if nBattleAdopt == 1 then
        print("[query] nNowBattleID 为最大值 nNowBattleID = "
                ..nNowBattleID.." nNowGuaJiID = "..nNowGuaJiID.." nBattleType = "..nBattleType)
        msgRet.nowBattleID = configNode + 1
    end
    
    -- local nowBattleID = human.db.battleID > configNode and configNode or human.db.battleID

    local nowNodeConfig = tBattleConfig.node[nNowBattleID]
    if not nowNodeConfig then
        print("[query] 不存在对应的当前战斗ID对应的配置 nNowBattleID = ", nNowBattleID)
        return
    end
    
    local nowMapConfig = tBattleConfig.map[nowNodeConfig.mapID]
    if not nowMapConfig then
        print("[query] 不存在对应的当前地图对应的地图配置 nNowBattleID = "
                ..nNowBattleID.." mapID ="..nowNodeConfig.mapID)
        return
    end

    local nJudeNum = nNowGuaJiID
    -- 已通关
    if nBattleAdopt == 1 then
        nJudeNum = configNode
    else
        nJudeNum = nNowBattleID - 1
    end

    msgRet.maxBattleID = configNode
    msgRet.mapID = nowNodeConfig.mapID
    msgRet.nodeID = nowNodeConfig.curLevel
    msgRet.sceneID = nowNodeConfig.sceneID

    msgRet.mapName = nowMapConfig.name
    msgRet.nodeName = nowNodeConfig.name
    msgRet.needLv = nowNodeConfig.needLv
    msgRet.bg = nowMapConfig.bg
    msgRet.canBattle = 0
    msgRet.doubleCnt = 0
    if nowNodeConfig.needLv > human.db.lv then
        msgRet.canBattle = nowNodeConfig.needLv
    end

    local double = RoleSystemLogic.isDouble(human, RoleSystemDefine.ROLE_SYS_ID_1104)
    if
        double and human.db.bar and human.db.bar.doubleCnt and
        human.db.bar.doubleCnt <= BarTaskLogic.DEFAULT_MAX_DOUBLE_CNT
    then
        msgRet.doubleCnt = BarTaskLogic.DEFAULT_MAX_DOUBLE_CNT - human.db.bar.doubleCnt
    elseif double and not human.db.bar then
        msgRet.doubleCnt = BarTaskLogic.DEFAULT_MAX_DOUBLE_CNT
    end

    msgRet.tongguan[0] = 0
    local id = getTongguanIndex(human)
    if id == nil then
        msgRet.tongguan[0] = 0
    else
        msgRet.tongguan[0] = 1
        local state = isGetReward(human, id)
        local cf = tBattleConfig.node[id].tongguan
        local net = msgRet.tongguan[1]
        if state == true then
            net.status = STATUS_NONE
        else
            net.status =(id > nJudeNum) and STATUS_CANGET or STATUS_NONE
        end
        net.levelName = getBattleName(human,id)
        net.index = id
        net.nowBattle = getBattleName(human,nJudeNum)
        net.reward[0] = 0
        net.heroReward[0] = 0
        -- 是英雄
        if cf[1][3] then
            net.heroReward[0] = 1
            local other = { }
            other.star = cf[1][3]
            HeroGrid.makeHeroSimpleByID(net.heroReward[1], cf[1][1], nil, other, human)
            HeroGrid.makeHeroSimpleByGeneral(net.heroReward[1], cf[1][1])
        else
            net.reward[0] = 1
            for i = 1, net.reward[0] do
                local itemID = cf[i][1]
                local itemCnt = cf[i][2]
                Grid.makeItem(net.reward[i], itemID, itemCnt)
            end
        end
    end

    local outSec = 0
    local tBattleOut = BattleLogic_GetBattleOut(human)
    --if human.db.battleOut ~= nil then
    if tBattleOut ~= nil then
        local now = os.time()
        outSec = now - tBattleOut.expTs1
    end

    local maxHangTime = getHangMaxTime(human)
    msgRet.maxTime = maxHangTime
    if maxHangTime >= outSec then
        msgRet.time = outSec
    else
        msgRet.time = maxHangTime
    end

    -- 悬赏情报数量
    msgRet.xushang = human.db.bar and human.db.bar.qingbao or 0
    msgRet.xushangMax = RoleExcel.exp[human.db.lv].qingBaoMax

    --秘宝对悬赏情报上限加成
    local qingBaoAdd = TalismanLogic.getTalismanAdd(human, TalismanLogic.OTHER_EFFECT_TBL.XUANSHANG_QB_MAX) or 0
    msgRet.xushangMax = msgRet.xushangMax + qingBaoAdd


    -- 悬赏红点
    msgRet.xsDot = 0
    if BarTaskLogic.isDot(human) then
        msgRet.xsDot = 1
    end

    msgRet.canMopup = 0
    if TequanShopLogic.isActiveMopup(human) then
        msgRet.canMopup = human.db.mopupDoCnt < BATTLE_MOPUP_CNT_VIP and 1 or 0
    else
        msgRet.canMopup = human.db.mopupDoCnt < BATTLE_MOPUP_CNT and 1 or 0
    end

    -- 判断额外增加的扫荡次数
    --秘宝增加免费扫荡次数
    local talismanAdd_dailyFeeCnt = TalismanLogic.getTalismanAdd(human, TalismanLogic.OTHER_EFFECT_TBL.GJ_SD_FREE_CNT) or 0
    if 0 < (BattleLogic_GetAddFreeMopupNum(human) + talismanAdd_dailyFeeCnt) and 0 == msgRet.canMopup then
        msgRet.canMopup = 1
    end

    -- local monsterOutId = nowNodeConfig.monsterOutID

    local teamIdx = getTeamIdx(human)
    local monsterOutId = nowNodeConfig.monsterOutID[teamIdx]

    if not monsterOutId then
        print("[BattleLogic query] monsterOutId 为空")
        return
    end

    -- local monsterConfig = MonsterExcel.monsterOut[monsterOutId]
    local monsterConfig = BattleMonsterExcel.monsterOut[monsterOutId] or BattleMonsterExcel2.monsterOut[monsterOutId]
    if not monsterConfig then
        print("[BattleLogic query] 获取到的 monsterConfig 为空 monsterOutId = "..monsterOutId)
        return
    end
    
    fontVideoInfo(human, msgRet.videoInfo, monsterConfig)
    fontExtraDataSSZH(msgRet.extraDataSSZH, nowNodeConfig.mapID, nNowBattleID, human)

    msgRet.battleGameTimes = (human.db.battleGameTimes or 0) + (human.db.eliteBattleGameTimes or 0) + (human.db.hardBattleGameTimes or 0)
    -- Msg.trace(msgRet)
    Msg.send(msgRet, human.fd)

    hangFightQuery(human)
end

-- 获取当前地图ID
function getMapID(human, args)
    local nNowBattleID = BattleLogic_GetBattleBattleID(human)
    local tBattleConfig = BattleLogic_GetBattleConfig(human)
    if -1 >= nNowBattleID or not tBattleConfig then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "[getMapID] 获取到的战斗ID或奖励配置不正确")
        print("[getMapID] 获取到的战斗ID或奖励配置不正确 id = "..human.db._id)
        return
    end

    -- local battleID = human.db.battleID
    -- local config = BattleExcel.node[battleID]
    local config = tBattleConfig.node[nNowBattleID]
    if not config then
        return
    end

    local mapConfig = tBattleConfig.map[config.mapID]
    if not mapConfig then
        return
    end

    return config.sceneID
end

-- 战役挂机界面查询
function onHookQuery(human)
    local msgRet = Msg.gc.GC_BATTLE_HANG_QUERY
    calcBattleOut(human)
    local tBattleOut = BattleLogic_GetBattleOut(human)
    local tBattleConfig = BattleExcel
    local nGuaJiID = human.db.guajiID

    if not tBattleConfig or -1 >= nGuaJiID or not tBattleOut then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "[onHookQuery] 获取到的战斗挂机奖励配置不正确 或 配置信息、挂机ID不正确 id = "..human.db._id)
        print("[onHookQuery] 获取到的战斗挂机奖励配置不正确 或 配置信息、挂机ID不正确 id = "..human.db._id)
        return
    end

    -- local expAdd = human.db.battleOut.exp
    -- local jinbiAdd = human.db.battleOut.jinbi
    -- local greenExpAdd = human.db.battleOut.greenExp
    -- local qingbaoAdd = human.db.battleOut.qingbao

    local expAdd = tBattleOut.exp
    local jinbiAdd = tBattleOut.jinbi
    local greenExpAdd = tBattleOut.greenExp
    local qingbaoAdd = tBattleOut.qingbao


    local talismanAdd_qb = (TalismanLogic.getTalismanAdd(human, TalismanLogic.OTHER_EFFECT_TBL.GJ_QB) or 0) / 100
    local talismanAdd_hero_exp = (TalismanLogic.getTalismanAdd(human, TalismanLogic.OTHER_EFFECT_TBL.GJ_HERO_EXP) or 0) / 100

    qingbaoAdd = qingbaoAdd + math.ceil(qingbaoAdd * talismanAdd_qb)
    greenExpAdd = greenExpAdd + math.ceil(greenExpAdd * talismanAdd_hero_exp)



    local skip = GuideLogic.getGuideSkip(human, GuideLogic.SKIPTYPE_JUMP_BATTLE_EXP)
    if not skip then
        expAdd = 1510
    end

    local items = { }
    local len = #items
    local tempTable = { }
    if tBattleOut.items then
        tempTable = Util.copyTable(tBattleOut.items)
    end
    tempTable[ItemDefine.ITEM_EXP_ID] = tempTable[ItemDefine.ITEM_EXP_ID] or 0
    tempTable[ItemDefine.ITEM_EXP_ID] = expAdd
    tempTable[ItemDefine.ITEM_JINBI_ID] = tempTable[ItemDefine.ITEM_JINBI_ID] or 0
    tempTable[ItemDefine.ITEM_JINBI_ID] = jinbiAdd
    tempTable[ItemDefine.ITEM_GREEN_EXP_ID] = tempTable[ItemDefine.ITEM_GREEN_EXP_ID] or 0
    tempTable[ItemDefine.ITEM_GREEN_EXP_ID] = greenExpAdd
    tempTable[ItemDefine.ITEM_QINGBAO_ID] = tempTable[ItemDefine.ITEM_QINGBAO_ID] or 0
    tempTable[ItemDefine.ITEM_QINGBAO_ID] = qingbaoAdd

    local list = DropExchangeLogic.getAbsCanDrop(human)
    if list then
        for k, v in pairs(list) do
            if tempTable[k] and tempTable[k] > 0 then
                len = len + 1
                items[len] = { }
                items[len].id = k
                items[len].cnt = tempTable[k]
            end
        end
    end

    for k, v in pairs(tempTable) do
        if not list or not list[k] then
            len = len + 1
            items[len] = { }
            items[len].id = k
            items[len].cnt = v
        end
    end

    -- 发消息
    local guajiID = nGuaJiID > #tBattleConfig.node and #tBattleConfig.node or nGuaJiID
    if guajiID == 0 then
        return Broadcast.sendErr(human, Lang.BATTLE_GUAJI_ERR)
    end

    local nodeConfig = tBattleConfig.node[guajiID]
    local now = os.time()
    local outSec = now - human.db.battleOut.expTs1
    local maxHangTime = getHangMaxTime(human)
    local idx = 1
   
    for i = 1, #items do
        Grid.makeItem(msgRet.items[i], items[i].id, items[i].cnt)
    end
    local len = #items
    if human.db.battleOut.equip then
        for k, equipGrid in ipairs(tBattleOut.equip) do
            if len >= 10 then 
                msgRet.items[0] = len
                msgRet.isEnd = 0
                msgRet.index = idx
                msgRet.maxTime = maxHangTime
                if maxHangTime >= outSec then
                    msgRet.time = outSec
                else
                    msgRet.time = maxHangTime
                end
                local minHour = math.floor(msgRet.time / HOUR_SEC)
                minHour =  (minHour ~= 0) and minHour or 1
                msgRet.diamond = minHour * DIAMOND_COST_PER_HOUR
                
                msgRet.exp = nodeConfig.hangExp
                msgRet.jinbi = nodeConfig.hangJinbi
                msgRet.greenExp = nodeConfig.hangGreenExp
                msgRet.qingbao = nodeConfig.hangQingbao or 0
                Msg.send(msgRet, human.fd)
                len = 0
                idx = idx + 1
                msgRet = Msg.gc.GC_BATTLE_HANG_QUERY
            end
            len = len + 1
            Grid.makeItem(msgRet.items[len], equipGrid.id, 1, nil, equipGrid)
        end
    end
    msgRet.isEnd = 1
    msgRet.index = idx
    msgRet.items[0] = len
    msgRet.maxTime = maxHangTime
    if maxHangTime >= outSec then
        msgRet.time = outSec
    else
        msgRet.time = maxHangTime
    end
    local minHour = math.floor(msgRet.time / HOUR_SEC)
    minHour =  (minHour ~= 0) and minHour or 1
    msgRet.diamond = minHour * DIAMOND_COST_PER_HOUR
    msgRet.exp = nodeConfig.hangExp
    msgRet.jinbi = nodeConfig.hangJinbi
    msgRet.greenExp = nodeConfig.hangGreenExp
    msgRet.qingbao = nodeConfig.hangQingbao or 0
    Msg.send(msgRet, human.fd)
end

local function isChapterDot(human, mapID)
    local chapterReward = BattleLogic_GetChapterReward(human)
    local nBattleID = BattleLogic_GetBattleBattleID(human)
    local tBattleConfig = BattleLogic_GetBattleConfig(human)
    if not chapterReward or not tBattleConfig or -1 >= nBattleID or chapterReward[mapID] then
        return false
    end

    local battleID = nBattleID
    local battleNodeConfig = tBattleConfig.node[battleID]
    if not battleNodeConfig then
        if battleID == #tBattleConfig.node + 1 then
            return true
        else
            return false
        end
    else
        local roleMapId = battleNodeConfig.mapID
        if roleMapId <= mapID then
            return false
        end
    end

    return true
end

function battleWorldMapQuery(human)
    -- 先取配置
    local tBattleConfig = BattleLogic_GetBattleConfig(human)
    local nBattleID = BattleLogic_GetBattleBattleID(human)
    local nBattleType = BattleLogic_GetBattleType(human)
    if not tBattleConfig or -1 >= nBattleID or -1 >= nBattleType then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "[battleWorldMapQuery] 获取到的战斗挂机奖励配置不正确 或 配置信息、战斗关卡ID不正确 id = "..human.db._id)
        print("[battleWorldMapQuery] 获取到的战斗挂机奖励配置不正确 或 配置信息、挂机ID不正确 id = "..human.db._id)
        return
    end

    local msgRet = Msg.gc.GC_BATTLE_WORLD_MAP_QUERY
    local worldMapConfig = tBattleConfig.map
    local battleID = nBattleID > #tBattleConfig.node and #tBattleConfig.node or nBattleID
    local battleNodeConfig = tBattleConfig.node[battleID]
    local len = #worldMapConfig

    if not BATTLE_LEVEL_PLAYER_LIST[nBattleType] then
        BATTLE_LEVEL_PLAYER_LIST[nBattleType]  = {}
    end
    
    local tBattleLevelPlayerList = BATTLE_LEVEL_PLAYER_LIST[nBattleType]

    -- for i = 1, len do
    --     local v = worldMapConfig[i]
    --     msgRet.worldMap[i].mapID = i
    --     msgRet.worldMap[i].mapName = v.name or ""
    --     msgRet.worldMap[i].mapBuild = v.build or 0
    --     msgRet.worldMap[i].mapBg = v.bg or 0
    --     msgRet.worldMap[i].mapPos[0] = 2
    --     msgRet.worldMap[i].mapPos[1] = v.pos[1]
    --     msgRet.worldMap[i].mapPos[2] = v.pos[2]
    --     msgRet.worldMap[i].status =(battleNodeConfig.mapID >= i) and 1 or 0
    --     msgRet.worldMap[i].story = v.des or ""
    --     if battleNodeConfig.mapID == i then
    --         msgRet.worldMap[i].maxLevel = battleNodeConfig.maxLevel
    --         msgRet.worldMap[i].curLevel = battleNodeConfig.curLevel
    --     else
    --         msgRet.worldMap[i].maxLevel = 0
    --         msgRet.worldMap[i].curLevel = 0
    --     end

    --     local reward = v.reward
    --     local l = 0
    --     msgRet.worldMap[i].chapterReward[0] = #reward
    --     for _, rewardData in pairs(reward) do
    --         l = l + 1
    --         Grid.makeItem(msgRet.worldMap[i].chapterReward[l], rewardData[1], rewardData[2])
    --     end

    --     msgRet.worldMap[i].chapterDot = 0
    --     if isChapterDot(human, i) then
    --         msgRet.worldMap[i].chapterDot = 1
    --     end

    --     local cnt = 0
    --     -- BATTLE_LEVEL_PLAYER_LIST[i] = BATTLE_LEVEL_PLAYER_LIST[i] or { }
    --     tBattleLevelPlayerList[i] = tBattleLevelPlayerList[i] or { }
    --     -- for s, t in pairs(BATTLE_LEVEL_PLAYER_LIST[i]) do
    --     for s, t in pairs(tBattleLevelPlayerList[i]) do
    --         cnt = cnt + 1
    --         RoleLogic.makeRoleBase(t, msgRet.worldMap[i].roleList[cnt])
    --         if cnt >= 3 then
    --             break
    --         end
    --     end
    --     msgRet.worldMap[i].roleList[0] = cnt
    -- end

    -- msgRet.worldMap[0] = len


    msgRet.worldMap[0] = 0
    msgRet.isStart = 1
    msgRet.isEnd = 0

    local i = 0
    local msgOneceMaxLen = 20

    for k, v in ipairs(worldMapConfig) do
        i = i + 1
        msgRet.worldMap[0] = i

        msgRet.worldMap[i].mapID = k
        msgRet.worldMap[i].mapName = v.name or ""
        msgRet.worldMap[i].mapBuild = v.build or 0
        msgRet.worldMap[i].mapBg = v.bg or 0
        msgRet.worldMap[i].mapPos[0] = 2
        msgRet.worldMap[i].mapPos[1] = v.pos[1]
        msgRet.worldMap[i].mapPos[2] = v.pos[2]
        msgRet.worldMap[i].status =(battleNodeConfig.mapID >= k) and 1 or 0
        msgRet.worldMap[i].story = v.des or ""
        if battleNodeConfig.mapID == i then
            msgRet.worldMap[i].maxLevel = battleNodeConfig.maxLevel
            msgRet.worldMap[i].curLevel = battleNodeConfig.curLevel
        else
            msgRet.worldMap[i].maxLevel = 0
            msgRet.worldMap[i].curLevel = 0
        end

        local reward = v.reward
        local l = 0
        msgRet.worldMap[i].chapterReward[0] = #reward
        for _, rewardData in pairs(reward) do
            l = l + 1
            Grid.makeItem(msgRet.worldMap[i].chapterReward[l], rewardData[1], rewardData[2])
        end

        msgRet.worldMap[i].chapterDot = 0
        if isChapterDot(human, k) then
            msgRet.worldMap[i].chapterDot = 1
        end

        local cnt = 0
        -- BATTLE_LEVEL_PLAYER_LIST[i] = BATTLE_LEVEL_PLAYER_LIST[i] or { }
        tBattleLevelPlayerList[k] = tBattleLevelPlayerList[k] or { }
        -- for s, t in pairs(BATTLE_LEVEL_PLAYER_LIST[i]) do
        for s, t in pairs(tBattleLevelPlayerList[k]) do
            cnt = cnt + 1
            RoleLogic.makeRoleBase(t, msgRet.worldMap[i].roleList[cnt])
            if cnt >= 3 then
                break
            end
        end
        msgRet.worldMap[i].roleList[0] = cnt

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

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

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

function battleNodeQuery(human, mapID)
    local tBattleConfig = BattleLogic_GetBattleConfig(human)
    local nBattleID = BattleLogic_GetBattleBattleID(human)
    if not tBattleConfig or -1 >= nBattleID then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "[battleNodeQuery] 获取到的战斗挂机奖励配置不正确 或 配置信息、战斗关卡ID不正确 id = "..human.db._id)
        print("[battleNodeQuery] 获取到的战斗挂机奖励配置不正确 或 配置信息、挂机ID不正确 id = "..human.db._id)
        return
    end

    local msgRet = Msg.gc.GC_BATTLE_NODE_QUERY
    local nodeConfig = tBattleConfig.node
    local lenConfig = #nodeConfig
    local len = 0
    msgRet.battleID = nBattleID or 0
    for i = 1, lenConfig do
        local v = nodeConfig[i]
        if v.mapID == mapID then
            len = len + 1
            msgRet.nodeInfo[len].nodeName = v.name
            msgRet.nodeInfo[len].battleID = i
            msgRet.nodeInfo[len].needLv = v.needLv
            msgRet.nodeInfo[len].needZDL = v.needZDL
            msgRet.nodeInfo[len].hasSuipian = v.hasSuipian
        end
    end
    msgRet.nodeInfo[0] = len
    Msg.send(msgRet, human.fd)
end

function initBattleOut(human, now)
    if human.db.battleOut then
        return
    end

    if false == BattleLogic_InitAllBattleOut(human, now) then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "initBattleOut 初始化挂机奖励表失败 id = "
            ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name)
        return
    end
    

    -- human.db.battleOut = { }
    -- human.db.battleOut.expTs1 = now
    -- -- 经验开始产出时间戳
    -- human.db.battleOut.expTs2 = now
    -- -- 经验产出结算时间戳
    -- human.db.battleOut.itemTs1 = now
    -- -- 道具产出时间戳
    -- human.db.battleOut.itemTs2 = now
    -- -- 道具产出结算时间戳
    -- human.db.battleOut.exp = 0
    -- human.db.battleOut.jinbi = 0
    -- human.db.battleOut.greenExp = 0
    -- human.db.battleOut.qingbao = 0
    -- human.db.battleOut.items = nil
    -- -- 产出的待收获的战利品
    -- human.db.battleOut.equip = nil
    -- -- 产出的待收获的装备
end

function calcBattleOut(human,isDiamond)
    local now = os.time()

    initBattleOut(human, now)

    calcBattleExpOut(human, now)
    
    calcBattleItemOut(human, now)
    
    if isDiamond then  -- 钻石消耗 所有产出翻1.5倍
        local tBattleOut = BattleLogic_GetBattleOut(human)
        if not tBattleOut then
            Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "calcBattleOut 获取挂机奖励列表失败 id = "
                    ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name)
            return
        end

        -- human.db.battleOut.exp = math.floor(human.db.battleOut.exp * DIAMOND_ADDITION)
        -- human.db.battleOut.jinbi =  math.floor(human.db.battleOut.jinbi * DIAMOND_ADDITION)
        -- human.db.battleOut.greenExp = math.floor(human.db.battleOut.greenExp * DIAMOND_ADDITION)
        -- human.db.battleOut.qingbao =  math.floor(human.db.battleOut.qingbao * DIAMOND_ADDITION)
        -- local items = human.db.battleOut.items or {}

        tBattleOut.exp = math.floor(tBattleOut.exp * DIAMOND_ADDITION)
        tBattleOut.jinbi =  math.floor(tBattleOut.jinbi * DIAMOND_ADDITION)
        tBattleOut.greenExp = math.floor(tBattleOut.greenExp * DIAMOND_ADDITION)
        tBattleOut.qingbao =  math.floor(tBattleOut.qingbao * DIAMOND_ADDITION)
        local items = tBattleOut.items or {}
        for id,cnt in pairs(items) do 
            items[id] = math.floor(cnt * DIAMOND_ADDITION)
        end
        -- local equipList = human.db.battleOut.equip or {}
        local equipList = tBattleOut.equip or {}
        local equipMap = {}
        for _,equip in ipairs(equipList) do 
            equipMap[equip.id] = equipMap[equip.id] or 0
            equipMap[equip.id] = equipMap[equip.id] + 1
        end
        for id,cnt in pairs(equipMap) do 
            local count = math.floor(cnt * DIAMOND_ADDITION)
            for i = cnt,count do 
                local equipGrid = EquipLogic.makeEquip(id)
                equipList[#equipList + 1] = equipGrid
            end
        end
    end
end

-- 获取正确的剩余可收益的结算时间
function getSurMaxHangTime(maxHangTime, ts1, now)
    if ts1 + maxHangTime > now then
        return now
    else
        return ts1 + maxHangTime
    end
end

function calcBattleExpOut(human, now)
    local tBattleOut = BattleLogic_GetBattleOut(human)
    if not tBattleOut then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "calcBattleExpOut 获取挂机奖励列表失败 id = "
                    ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name)
        return
    end

    local maxHangTime = getHangMaxTime(human)
    if tBattleOut.expTs2 - tBattleOut.expTs1 >= maxHangTime then
        return
    end
    
    local outTime = getSurMaxHangTime(maxHangTime, tBattleOut.expTs1, now)
    local outSec = outTime - tBattleOut.expTs2
    local outCnt = math.floor(outSec / BATTLE_HANG_EXP_OUT_PERIOD)
    if outCnt < 1 then
        return
    end

    local tBattleNodeConfig = BattleExcel
    local nGuaJiID = human.db.guajiID

    if not tBattleNodeConfig or 0 > nGuaJiID then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "calcBattleExpOut 获取到的配置表不正确或挂机ID不正确 id = "
                    ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name)
        return
    end

    local guajiID = nGuaJiID > #tBattleNodeConfig.node and #tBattleNodeConfig.node or nGuaJiID
    if guajiID == 0 then
        return
    end

    local nowNodeConfig = tBattleNodeConfig.node[guajiID]

    tBattleOut.expTs2 = tBattleOut.expTs2 + outCnt * BATTLE_HANG_EXP_OUT_PERIOD
    if now - tBattleOut.expTs1 >= maxHangTime then
        tBattleOut.expTs2 = now
    end

    local vipExpAdd =(VipLogic.getPowerArgs(human, VipLogic.VIP_POWER7) or 0) / 100
    local vipJinAdd =(VipLogic.getPowerArgs(human, VipLogic.VIP_POWER8) or 0) / 100
    local vipGreenExpAdd =(VipLogic.getPowerArgs(human, VipLogic.VIP_POWER9) or 0) / 100
    local vipQingbaoAdd =(VipLogic.getPowerArgs(human, VipLogic.VIP_POWER19) or 0) / 100

    --秘宝方面的加成
    local talismanAdd_qb = (TalismanLogic.getTalismanAdd(human, TalismanLogic.OTHER_EFFECT_TBL.GJ_QB) or 0) / 100
    local talismanAdd_hero_exp = (TalismanLogic.getTalismanAdd(human, TalismanLogic.OTHER_EFFECT_TBL.GJ_HERO_EXP) or 0) / 100

    local exp = nowNodeConfig.hangExp
    local jinbi = nowNodeConfig.hangJinbi
    local greenExp = nowNodeConfig.hangGreenExp
    local qingbao = nowNodeConfig.hangQingbao
    tBattleOut.exp = tBattleOut.exp + math.floor(outCnt * exp *(1 + vipExpAdd))
    tBattleOut.jinbi = tBattleOut.jinbi + math.floor(outCnt * jinbi *(1 + vipJinAdd))
    tBattleOut.greenExp = tBattleOut.greenExp + math.floor(outCnt * greenExp *(1 + vipGreenExpAdd))
    tBattleOut.qingbao = tBattleOut.qingbao + math.floor(outCnt * qingbao *(1 + vipQingbaoAdd))

    -- tBattleOut.qingbao = tBattleOut.qingbao + math.ceil(tBattleOut.qingbao * talismanAdd_qb)
    -- tBattleOut.greenExp = tBattleOut.greenExp + math.ceil(tBattleOut.greenExp * talismanAdd_hero_exp)
end

-- 根据时间计算道具产出
local ITEM_OUT_LIST = { }
function getItemOutsByTime(config, sec)
    Util.cleanTable(ITEM_OUT_LIST)
    local outCnt = math.floor(sec / BATTLE_HANG_ITEM_OUT_PERIOD)
    if outCnt < 1 then
        return ITEM_OUT_LIST
    end

    -- 普通掉落
    local dropID = config.dropID
    local dropConfig = DropExcel.dropBattle[dropID]
    local weightSum = 0
    for i = 1, #dropConfig.dropRule do
        local tempConfig = dropConfig.dropRule[i]
        weightSum = weightSum + tempConfig[4]
    end
    for i = 1, outCnt do
        local itemID, itemCnt = randItemOut(dropConfig.dropRule, weightSum)
        if itemID and itemCnt and itemCnt > 0 then
            ITEM_OUT_LIST[itemID] =(ITEM_OUT_LIST[itemID] or 0) + itemCnt
        end
    end
    for i = 1, #dropConfig.dropRule2 do
        local tempConfig = dropConfig.dropRule2[i]
        local itemID = tempConfig[1]
        local itemMin = tempConfig[2]
        local itemMax = tempConfig[3]
        local itemCnt = math.random(itemMin, itemMax) * outCnt
        if itemCnt > 0 then
            ITEM_OUT_LIST[itemID] =(ITEM_OUT_LIST[itemID] or 0) + itemCnt
        end
    end

    -- 三小时掉落
    outCnt = math.floor(sec / BATTLE_HANG_ITEM_OUT_PERIOD_2)
    if outCnt < 1 then
        return ITEM_OUT_LIST
    end
    weightSum = 0
    for i = 1, #dropConfig.dropRule3 do
        local tempConfig = dropConfig.dropRule3[i]
        weightSum = weightSum + tempConfig[4]
    end
    for i = 1, outCnt do
        local itemID, itemCnt = randItemOut(dropConfig.dropRule3, weightSum)
        if itemID and itemCnt and itemCnt > 0 then
            ITEM_OUT_LIST[itemID] =(ITEM_OUT_LIST[itemID] or 0) + itemCnt
        end
    end

    return ITEM_OUT_LIST
end


function calcBattleItemOut(human, now,isDiamond)
    local tBattleOut = BattleLogic_GetBattleOut(human)
    if not tBattleOut then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "calcBattleItemOut 获取挂机奖励列表失败 id = "
                    ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name)
        return
    end

    local maxHangTime = getHangMaxTime(human)
    if tBattleOut.itemTs2 - tBattleOut.itemTs1 >= maxHangTime then
        return
    end

    local outTime = getSurMaxHangTime(maxHangTime, tBattleOut.itemTs1, now)
    local outSec = outTime - tBattleOut.itemTs2
    local outCnt = math.floor(outSec / BATTLE_HANG_ITEM_OUT_PERIOD)
    if outCnt < 1 then
        return
    end

    local tBattleNodeConfig = BattleExcel
    local nGuaJiID = human.db.guajiID

    if not tBattleNodeConfig or 0 > nGuaJiID then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "calcBattleItemOut 获取到的配置表不正确或挂机ID不正确 id = "
                    ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name)
        return
    end

    local guajiID = nGuaJiID > #tBattleNodeConfig.node and #tBattleNodeConfig.node or nGuaJiID
    if guajiID == 0 then
        return
    end

    local sec = outCnt * BATTLE_HANG_ITEM_OUT_PERIOD
    tBattleOut.itemTs2 = tBattleOut.itemTs2 + sec
    if now - tBattleOut.itemTs1 >= maxHangTime then
        tBattleOut.itemTs2 = now
    end

    local talismanAdd_zs = (TalismanLogic.getTalismanAdd(human, TalismanLogic.OTHER_EFFECT_TBL.GJ_ZS) or 0) / 100
    
    local nodeConfig = tBattleNodeConfig.node[guajiID]
    local items = getItemOutsByTime(nodeConfig, sec)
    for itemID, itemCnt in pairs(items) do
        -- 判定是否是装备
        if ItemDefine.isEquip(itemID) then
            -- 生成装备
            local equipGrid = EquipLogic.makeEquip(itemID)
            if equipGrid then
                tBattleOut.equip = tBattleOut.equip or { }
                tBattleOut.equip[#tBattleOut.equip + 1] = equipGrid
            end
        else
            tBattleOut.items = tBattleOut.items or { }
            tBattleOut.items[itemID] = tBattleOut.items[itemID] or 0
            if itemID == ItemDefine.ITEM_ZUANSHI_ID and talismanAdd_zs > 0 then
                itemCnt = itemCnt + math.ceil(itemCnt * talismanAdd_zs)
            end
            tBattleOut.items[itemID] = tBattleOut.items[itemID] + itemCnt
        end
    end

    DropExchangeLogic.getDropItem(human, outSec, BATTLE_HANG_ITEM_OUT_PERIOD, tBattleOut)
end

local function hangExpGet(human,isDiamond)
    local tBattleOut = BattleLogic_GetBattleOut(human)
    if not tBattleOut then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "hangExpGet 获取挂机奖励列表失败 id = "
                    ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name)
        return
    end

    local needDiamond = 0
    -- 是否消耗钻石
    if isDiamond then 
        local now = os.time()
        local outSec = now - tBattleOut.expTs1
        local maxHangTime = getHangMaxTime(human)
        outSec = outSec > maxHangTime and maxHangTime or outSec
        local minHour = math.floor(outSec / HOUR_SEC)
        minHour = (minHour ~= 0) and minHour or 1
        local diamondCnt = minHour * DIAMOND_COST_PER_HOUR
        if not ObjHuman.checkRMB(human,diamondCnt) then 
            return 
        end
        needDiamond = -diamondCnt
    end

    calcBattleOut(human,isDiamond)
    if tBattleOut.exp == 0 then
        return 1
    end
    local jinbiAdd = tBattleOut.jinbi
    if not ObjHuman.canAddJinbi(human, jinbiAdd) then
        return 2
    end

    -- 检测装备数量
    if tBattleOut.equip then
        local equipCnt = #tBattleOut.equip
        if not EquipLogic.checkEmptyCnt(human, equipCnt) then
            return
        end
    end

    local items = { }
    local len = #items
    local maxHangTime = getHangMaxTime(human)
    if tBattleOut.expTs2 - tBattleOut.expTs1 >= maxHangTime then
        local now = os.time()
        tBattleOut.expTs2 = now
    end

    if tBattleOut.itemTs2 - tBattleOut.itemTs1 >= maxHangTime then
        local now = os.time()
        tBattleOut.itemTs2 = now
    end

    tBattleOut.itemTs1 = tBattleOut.itemTs2

    -- 改db
    local tempTable = tBattleOut.items or { }
    tBattleOut.items = nil

    for k, v in pairs(tempTable) do
        len = len + 1
        items[len] = { }
        items[len].id = k
        items[len].cnt = v
    end

    tBattleOut.expTs1 = tBattleOut.expTs2

    local expAdd = tBattleOut.exp
    local greenExpAdd = tBattleOut.greenExp
    local qingbaoAdd = tBattleOut.qingbao


    local talismanAdd_qb = (TalismanLogic.getTalismanAdd(human, TalismanLogic.OTHER_EFFECT_TBL.GJ_QB) or 0) / 100
    local talismanAdd_hero_exp = (TalismanLogic.getTalismanAdd(human, TalismanLogic.OTHER_EFFECT_TBL.GJ_HERO_EXP) or 0) / 100

    qingbaoAdd = qingbaoAdd + math.ceil(qingbaoAdd * talismanAdd_qb)
    greenExpAdd = greenExpAdd + math.ceil(greenExpAdd * talismanAdd_hero_exp)


    -- 新手指引 强制经验
    local flag = GuideLogic.getGuideSkip(human, GuideLogic.SKIPTYPE_JUMP_BATTLE_EXP)
    if not flag then
        expAdd = 1510
    end
    GuideLogic.setDoSpecialGuide(human, GuideLogic.SKIPTYPE_JUMP_BATTLE_EXP)
    GuideLogic.setDoSpecialGuide(human, GuideLogic.SKIPTYPE_JUMP_BATTLE_GUAJI)

    tBattleOut.exp = 0
    tBattleOut.jinbi = 0
    tBattleOut.greenExp = 0
    tBattleOut.qingbao = 0

    len = len + 1
    items[len] = { }
    items[len].id = ItemDefine.ITEM_EXP_ID
    items[len].cnt = expAdd

    len = len + 1
    items[len] = { }
    items[len].id = ItemDefine.ITEM_JINBI_ID
    items[len].cnt = jinbiAdd

    len = len + 1
    items[len] = { }
    items[len].id = ItemDefine.ITEM_GREEN_EXP_ID
    items[len].cnt = greenExpAdd

    len = len + 1
    items[len] = { }
    items[len].id = ItemDefine.ITEM_QINGBAO_ID
    items[len].cnt = qingbaoAdd

    -- 给道具
    for i = 1, #items do
        local tempID = items[i].id
        local tempCnt = items[i].cnt

        BagLogic.addItem(human, tempID, tempCnt, "battle")

    end

    -- 给装备
    if tBattleOut.equip then
        for k, equipGrid in ipairs(tBattleOut.equip) do
            EquipLogic.addByEquipGrid(human, equipGrid, "battle", true)
        end
        tBattleOut.equip = nil
    end
    if needDiamond < 0 then 
        ObjHuman.decZuanshi(human,needDiamond,"battle")
    end
    return 0, items
end

-- 下发挂机奖励满载时间戳
-- fullTime = expTs1 + maxHangTime
--   若 fullTime <= now  则奖励已满，满的时刻为 fullTime
--   若 fullTime >  now  则奖励未满，将在 fullTime 时满
function sendHangFullTime(human)
    local tBattleOut = BattleLogic_GetBattleOut(human)
    if not tBattleOut then return end
    local maxHangTime = getHangMaxTime(human)
    local msgRet = Msg.gc.GC_BATTLE_HANG_FULL_TIME_QUERY
    msgRet.fullTime = tBattleOut.expTs1 + maxHangTime
    Msg.send(msgRet, human.fd)
end

-- 获取挂机收益
function hangGet(human,isDiamond)
    local ret, items = hangExpGet(human,false)
    if ret == 1 then
        return Broadcast.sendErr(human, Lang.BATTLE_HANG_GET_EXP_ERR_NONE)
    elseif ret == 2 then
        return Broadcast.sendErr(human, Lang.COMMON_ADD_JINBI_LIMIT)
    elseif ret == 0 then
        query(human)
        sendHangFullTime(human)
        RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_1101)
        TreasureChestLogic.TreasureChestLogic_GetAllBoxNum(human)
    end

end


-- 挂机节点设置
function nodeSet(human, battleID)
    local tBattleConfig = BattleLogic_GetBattleConfig(human)
    local nNowBattleID = BattleLogic_GetBattleBattleID(human)
    if not tBattleConfig or -1 >= nNowBattleID then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "hangExpGet 获取挂机奖励列表失败 id = "
            ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name)
        print("[nodeSet] 获取不到战斗配置")
        return
    end

    local nodeConfig = tBattleConfig.node[battleID]
    if nodeConfig == nil then
        return
    end

    -- 前置条件判断
    if battleID > nNowBattleID then
        return Broadcast.sendErr(human, Lang.BATTLE_ID_OVER)
    end

    -- 等级判断
    if human.db.lv < nodeConfig.needLv then
        return Broadcast.sendErr(human, Util.format(Lang.ROLE_LEV_ERROR, nodeConfig.needLv))
    end

    calcBattleOut(human)

    print("[nodeSet] 玩家进行了挂机节点设置 battleID = ".. battleID)
    BattleLogic_SetBattleGuaJiID(human ,battleID)
    --human.db.guajiID = battleID
    setBattleID(human, battleID)

    Msg.send(Msg.gc.GC_BATTLE_NODE_SET, human.fd)

    query(human)
end

-- 挂机节点详细信息查询
function nodeDetailQuery(human, battleID)
    local tBattleConfig = BattleLogic_GetBattleConfig(human)
    local nNowBattleID = BattleLogic_GetBattleBattleID(human)
    if not tBattleConfig or -1 >= nNowBattleID then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "nodeDetailQuery 获取挂机奖励列表失败 id = "
            ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name)
        print("[nodeDetailQuery] 获取不到战斗配置")
        return
    end

    -- local nodeConfig = BattleExcel.node[battleID]
    local nodeConfig = tBattleConfig.node[battleID]
    if nodeConfig == nil then
        return
    end

    local msgRet = Msg.gc.GC_BATTLE_NODE_DETAIL_QUERY
    msgRet.battleID = battleID
    msgRet.exp = nodeConfig.hangExp
    msgRet.jinbi = nodeConfig.hangJinbi
    msgRet.greenExp = nodeConfig.hangGreenExp
    msgRet.qingbao = nodeConfig.hangQingbao or 0
    msgRet.nodeName = getBattleName(human, battleID)
    local dropID = nodeConfig.dropID
    local dropConfig = DropExcel.dropBattle[dropID]
    local dropCnt = 0
    local list = DropExchangeLogic.getAbsCanDrop(human)
    if list then
        for k, v in pairs(list) do
            dropCnt = dropCnt + 1
            Grid.makeItem(msgRet.items[dropCnt], k, v)
        end
    end

    for i = 1, #dropConfig.dropRule do
        local tempConfig = dropConfig.dropRule[i]
        local itemID = tempConfig[1]
        local itemCnt = tempConfig[3]
        local itemRate = tempConfig[4]
        if itemCnt > 0 and itemRate > 0 then
            dropCnt = dropCnt + 1
            Grid.makeItem(msgRet.items[dropCnt], itemID, 1)
        end
    end
    for i = 1, #dropConfig.dropRule3 do
        local tempConfig = dropConfig.dropRule3[i]
        local itemID = tempConfig[1]
        local itemCnt = tempConfig[3]
        local itemRate = tempConfig[4]
        if itemCnt > 0 and itemRate > 0 then
            dropCnt = dropCnt + 1
            Grid.makeItem(msgRet.items[dropCnt], itemID, 1)
        end
    end
    msgRet.items[0] = dropCnt

    Msg.send(msgRet, human.fd)
end


--处理战斗前属性加成
function onFightBegin(human, cbParam, combatType, param)
    local nBattleType = BattleLogic_GetBattleType(human)
    if not nBattleType then
        return
    end
    local attrAddData = getBattleAttrByType(human, nBattleType)

    if not attrAddData or not attrAddData.chosenAttrList then
        return
    end

    local attrList = calcAttr(attrAddData.chosenAttrList)

    for index = 1, CombatDefine.COMBAT_HERO_CNT do
        local atkPos = CombatLogic.getPos(CombatDefine.ATTACK_SIDE, index)
        local atkObj = CombatImpl.objList[atkPos]

        if atkObj then
            for attrId, attrVal in pairs(attrList) do
                atkObj.sysAttr[attrId] = (atkObj.sysAttr[attrId] or 0) + attrVal
            end
            atkObj.isSysAttrChange = true
        end
    end
end


-- 挑战当前挂机节点
function fight(human)
    local battleID = BattleLogic_GetBattleBattleID(human)
    local tBattleConfig = BattleLogic_GetBattleConfig(human)
    if not tBattleConfig or -1 >= battleID then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "nodeDetailQuery 获取挂机奖励列表失败 id = "
            ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name)
        print("[nodeDetailQuery] 获取不到战斗配置")
        return
    end

    -- 已通关
    local nBattleType = BattleLogic_GetBattleType(human)
    local tag = BattleLogic_GetBattleAdopt(human, nBattleType)
    if tag == 1 then
        return Broadcast.sendErr(human, Lang.DRILL_IS_FINSH)
    end


    -- local battleID = human.db.battleID

    -- if BattleExcel.node[human.db.battleID] == nil then
    if tBattleConfig.node[battleID] == nil then
        local msgRet = Msg.gc.GC_BATTLE_GOBACK_MAIN
        msgRet.panelIDs[0] = 1
        msgRet.panelIDs[1] = PanelDefine.PANEL_ID_1008
        Msg.send(msgRet, human.fd)
        return Broadcast.sendErr(human, Lang.BATTLE_MAX_LEVEL)
    end

    -- if human.db.lv < BattleExcel.node[human.db.battleID].needLv then
    if human.db.lv < tBattleConfig.node[battleID].needLv then
        local msgRet = Msg.gc.GC_BATTLE_GOBACK_MAIN
        msgRet.panelIDs[0] = 1
        msgRet.panelIDs[1] = PanelDefine.PANEL_ID_1008
        Msg.send(msgRet, human.fd)
        return Broadcast.sendErr(human, Util.format(Lang.ROLE_LEV_ERROR, tBattleConfig.node[battleID].needLv))
    end

    --如果有加成属性还没有选也不能挑战
    local nBattleType = BattleLogic_GetBattleType(human)
    local attrAddData = getBattleAttrByType(human, nBattleType)
    if attrAddData and attrAddData.unSelectedAttrList then
        return Broadcast.sendErr(human, Lang.BATTLE_CHOOSE_ATTR)
    end

    local currentPassLevelNum = getDailyPassLevelNum(human)
    if currentPassLevelNum >= EliteDefine.BATTLE_DAILY_MAX_PASSLEVEL_NUM then
        return Broadcast.sendErr(human, Lang.COMMON_DAILY_PASSLEVEL_LIMIT)
    end


    local config = tBattleConfig.node[battleID]
    -- local mapConfig = tBattleConfig.map[config.mapID]
    -- local monsterOutID = config.monsterOutID
    -- local mapID = mapConfig.bg
    -- CombatLogic.combatBegin(human, config.sceneID, monsterOutID, CombatDefine.COMBAT_TYPE1, battleID)
    -- GuideLogic.setDoSpecialGuide(human, GuideLogic.SKIPTYPE_JUMP_BATTLE)

    local teamIdx = getTeamIdx(human)
    local monsterOutID = config.monsterOutID[teamIdx]
    local combatType = getCombatTypeByTeamIdx(teamIdx)
    print("[combat_fight] 开始正式进入战斗")
    CombatLogic.combatBegin(human, config.sceneID, monsterOutID, combatType, battleID)
    print("[combat_fight] 开始正式进入战斗结束")
    
    GuideLogic.setDoSpecialGuide(human, GuideLogic.SKIPTYPE_JUMP_BATTLE)
end

local function challengeWinHandle(human)
    addDailyPassLevelNum(human, 1)
    Battle_DailyPassLevelInfo_Query(human)
end

function onFightEnd(human, result, fightTypeID, param1, combatInfo)
    -- 设置一些战斗结算信息
    combatInfo.defender.name = Util.format(Lang.COMBAT_BATTLE_DEFEND_NAME, param1)

    -- if CombatDefine.RESULT_WIN ~= result then
    --     return
    -- end

    local bl = isOpenOtherTeam(human)

    if CombatDefine.RESULT_WIN ~= result then
        if bl then
            resetTeamRecord(human)
        end

        return
    end


    local nBattleType = BattleLogic_GetBattleType(human)
    local guajiID = BattleLogic_GetBattleGuaJiID(human)
    local battleID = BattleLogic_GetBattleBattleID(human)
    if EliteDefine.COPY_ELITE_ERROR == nBattleType or -1 >= guajiID or -1 >= battleID then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "onFightEnd 获取战斗类型、挂机ID、战斗关卡ID失败 id = "
            ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name)
        print("[onFightEnd] 获取战斗类型、挂机ID、战斗关卡ID失败")
        return
    end

    local tBattleConfig = BattleLogic_GetBattleConfig(human)
    if not tBattleConfig then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "onFightEnd 获取战斗配置失败 id = "
            ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name)
        print("[onFightEnd] 获取战斗配置失败")
        return
    end

    -- local guajiID = human.db.guajiID
    -- local battleID = human.db.battleID

    if battleID ~= param1 then
        return
    end

    -- 如果当前关卡是多队伍关卡，则需要所有队伍都胜利才算通关
    if bl then
        local needTeamCnt = getLevelTeamCnt(human)
        local nowTeamIdx = getTeamIdx(human)

        if needTeamCnt <= nowTeamIdx then
            resetTeamRecord(human)
        else
            updateTeamRecord(human, nowTeamIdx)
            local nextTeamIdx = getTeamIdx(human)
            combatInfo.nextCombatType = getCombatTypeByTeamIdx(nextTeamIdx)
            return
        end
    end


    if EliteDefine.COPY_ELITE_NORMAL == nBattleType then
        MainDianLogic.MaiDian_Begin(human, MaiDianDefine.MAIDIAN_TYPE_BATTLE, {nValue = battleID})
    end

    local nAllFightLen = #tBattleConfig.node
    local nextGuajiID = (guajiID + 1) > nAllFightLen and nAllFightLen or guajiID + 1
    local nextBattleID = (battleID + 1) > nAllFightLen and nAllFightLen or battleID + 1
    -- local nextBattleConfig = BattleExcel.node[nextBattleID]
    -- local config = BattleExcel.node[battleID]

    if (battleID + 1) > nAllFightLen then
        BattleLogic_SetBattleAdopt(human, nBattleType, 1)
    end

    local nextBattleConfig = tBattleConfig.node[nextBattleID]
    local config = tBattleConfig.node[battleID]

    if not BATTLE_LEVEL_PLAYER_LIST[nBattleType] then
        BATTLE_LEVEL_PLAYER_LIST[nBattleType] = {}
    end

    local tBattleLevelPlayerList = BATTLE_LEVEL_PLAYER_LIST[nBattleType]
    -- 如果过大关卡
    if nextBattleConfig and nextBattleConfig.mapID ~= config.mapID then
        -- 清除记录
        tBattleLevelPlayerList[config.mapID] = tBattleLevelPlayerList[config.mapID] or { }
        tBattleLevelPlayerList[config.mapID][human.db._id] = nil
        -- 增加新纪录
        tBattleLevelPlayerList[nextBattleConfig.mapID] = tBattleLevelPlayerList[nextBattleConfig.mapID] or { }
        tBattleLevelPlayerList[nextBattleConfig.mapID][human.db._id] = { }
        RoleLogic.getRoleBase(human, tBattleLevelPlayerList[nextBattleConfig.mapID][human.db._id])
    else
        if config.mapID == 1 then
            tBattleLevelPlayerList[config.mapID] = tBattleLevelPlayerList[config.mapID] or { }
            if tBattleLevelPlayerList[config.mapID][human.db._id] == nil then
                -- 增加新纪录
                tBattleLevelPlayerList[config.mapID][human.db._id] = { }
                RoleLogic.getRoleBase(human, tBattleLevelPlayerList[config.mapID][human.db._id])
            end
        else
            if not tBattleLevelPlayerList[config.mapID] or not tBattleLevelPlayerList[config.mapID][human.db._id] then
                tBattleLevelPlayerList[config.mapID] = tBattleLevelPlayerList[config.mapID] or { }
                tBattleLevelPlayerList[config.mapID][human.db._id] = { }
                RoleLogic.getRoleBase(human, tBattleLevelPlayerList[config.mapID][human.db._id])
            end
        end
    end

    BattleLogic_SetBattleBattleID(human, nextBattleID)

    --增加小游戏次数
    if config and type(config.gameType) == "number" and config.gameType == 1 then
        if EliteDefine.COPY_ELITE_NORMAL == nBattleType then
            human.db.battleGameTimes = (human.db.battleGameTimes or 0) + 1
        elseif EliteDefine.COPY_LEVEL_ELITE == nBattleType then
            human.db.eliteBattleGameTimes = (human.db.eliteBattleGameTimes or 0) + 1
        elseif EliteDefine.COPY_ELITE_DIFFICULTY == nBattleType then
            human.db.hardBattleGameTimes = (human.db.hardBattleGameTimes or 0) + 1
        end
    end


    -- human.db.battleID = battleID + 1
    -- 通过第10关，默认开启二倍速
    if nextBattleID == 10 and EliteDefine.COPY_ELITE_NORMAL == nBattleType then
        human.db.combatSpeed = 2
    end

    combatInfo.attacker.oldLv = human.db.lv
    -- 给奖励
    combatInfo.rewardItem = { }
    for i = 1, #config.winReward do
        local itemID = config.winReward[i][1]
        local itemCnt = config.winReward[i][2]

        -- 装备不在这显示
        combatInfo.rewardItem[i] = combatInfo.rewardItem[i] or { }
        combatInfo.rewardItem[i] = { itemID, itemCnt }
        -- combatInfo.rewardItem[i] = { itemID, itemCnt, [4] = sourceId}
        BagLogic.addItem(human, itemID, itemCnt, "battle_win")
    end
    combatInfo.attacker.lv = human.db.lv

    combatInfo.getEquip = human.getEquip
    human.getEquip = nil

    -- TODO:记录主线关卡
    Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, human.db._id, human.db.newUniqueTag, human.db.name, human.db.battleID)

    if nBattleType ==  EliteDefine.COPY_ELITE_NORMAL then

        RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_1101)
        GuideLogic.onCallback(human)
        JibanLogic.onCallback(human, 1, battleID)
        TuiSongLiBao.tuiSongLiBaoOnTask(
        human,
        TuiSongLiBao.TUISONGLIBAOTASK_ZHENGZHAN,
        -- human.db.guajiID - 1,
        -- human.db.guajiID - 2
        guajiID - 1,
        guajiID - 2
        )

        BreakThroughTheme = BreakThroughTheme or require("battle.BreakThroughTheme")
        BreakThroughTheme.EntranceRedDotUpdate(human)

        ChengjiuLogic.onCallback(human, ChengjiuDefine.CJ_TASK_TYPE_1, nextGuajiID)
        MengxinLogic.onCallBack(human, MengxinLogic.MX_TASK_TYPE_1, nextGuajiID)
        
        for k, v in pairs(KingWorldLogic.funcID) do
            YunYingLogic.updateIcon(KingWorldLogic.YYInfo[k], human)
            break
        end
    end
    -- 存储战斗记录
    local videoUuid = CombatVideo.saveBattleVideo(human.db._id, combatInfo)
    human.db.battleVideoUuid = videoUuid

    BattleDBLogic.updateBattleDB(combatInfo.attacker, battleID, combatInfo, videoUuid, nBattleType)
    if nextBattleConfig and nextBattleConfig.mapID ~= config.mapID then
        -- 新的地图
        -- 通知客户端
        Msg.send(Msg.gc.GC_BATTLE_NODE_SET, human.fd)
    end

    BattleLogic_SetBattleGuaJiID(human, nextGuajiID)
    --human.db.guajiID = nextGuajiID
    if nextBattleConfig then
        setBattleID(human, nextGuajiID)
    end

    local showNext = 1
    -- if BattleExcel.node[human.db.battleID] and human.db.lv < BattleExcel.node[human.db.battleID].needLv then
    if tBattleConfig.node[nextBattleID] and human.db.lv < tBattleConfig.node[nextBattleID].needLv then
        showNext = 2
    end

    if human.db.lv < 5 then
        showNext = 0
    end

    -- nextBattleConfig = BattleExcel.node[human.db.battleID]
    nextBattleConfig = tBattleConfig.node[nextBattleID]
    if not nextBattleConfig then
        showNext = 0
    end
    
    if battleID == 3 then
        showNext = 0
    end

    RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_1105)

    -- 额外奖励
    local needLevel, itemName = getNextRewardName(human)
    if needLevel ~= nil then
        local tb = { needLevel, Lang.BATTLE_FIGHT_GUAN, "|", itemName, "|", showNext }
        local str = table.concat(tb)
        combatInfo.endParam = str
    else
        local tb = { showNext }
        local str = table.concat(tb)
        combatInfo.endParam = str
    end
    -- 触发事件
    GiftLogic.trigger(human,PRINCIPAL_LINE_EVNET,{id = battleID})
    WarOrder.trigger(human,BATTLE_ORDER_TYPE)

    BattleGift = BattleGift or require("battle.BattleGift")
    BattleGift.StageClearHandle(human, nextGuajiID, nBattleType)

    --肉鸽属性加成判断
    local nowStageCfg = tBattleConfig.node[battleID]
    if nowStageCfg.isRouge and type(nowStageCfg.isRouge) == "number" and nowStageCfg.isRouge == 1 then
        local attrAddData = getBattleAttrByType(human, nBattleType)

        --需求修改: 普通模式第一次战斗获得肉鸽属性时为指定属性: 9,5,4
        if not human.db.isBattleFirst and nBattleType == EliteDefine.COPY_ELITE_NORMAL and (not attrAddData.chosenAttrList or not next(attrAddData.chosenAttrList))
        and battleRougeCfg[9] and battleRougeCfg[5] and battleRougeCfg[4] then
            attrAddData.unSelectedAttrList = attrAddData.unSelectedAttrList or {}

            attrAddData.unSelectedAttrList[1] = 9
            attrAddData.unSelectedAttrList[2] = 5
            attrAddData.unSelectedAttrList[3] = 4

            human.db.isBattleFirst = true
        else
            local totalWeight, randList = generateCfgWeightInfo()
            if totalWeight > 0 then
                for i=1, 3 do
                    local id = getIndexByRand(totalWeight, randList)
                    -- local cfg = battleRougeCfg[id]
                    attrAddData.unSelectedAttrList = attrAddData.unSelectedAttrList or {}
                    -- attrAddData.unSelectedAttrList[id] = cfg.lv
                    attrAddData.unSelectedAttrList[i] = id
                end
            end
        end

        QueryDiffBattleUnSelectAttr(human)
    end

    if nBattleType == EliteDefine.COPY_ELITE_NORMAL then
        TriggerLogic.PublishEvent(TriggerDefine.BATTLE_NORMAL_MODE_PASS, human.db._id, 1)
    end

    challengeWinHandle(human)
end

function setBattleID(human, guajiID)
    local nNowGuaJiID = BattleLogic_GetBattleGuaJiID(human)
    local tBattleConfig = BattleLogic_GetBattleConfig(human)
    if -1 >= nNowGuaJiID or not tBattleConfig then
        return
    end
    
    -- if guajiID > human.db.guajiID and human.db.guajiID ~= 0 then
    if guajiID > nNowGuaJiID and nNowGuaJiID ~= 0 then
        -- 下发挂机收益升级提示
        local msgRet = Msg.gc.GC_BATTLE_REWARD_UP

        Grid.makeItem(msgRet.itemExp, ItemDefine.ITEM_EXP_ID, 1)
        Grid.makeItem(msgRet.itemJinbi, ItemDefine.ITEM_JINBI_ID, 1)
        Grid.makeItem(msgRet.itemGreenExp, ItemDefine.ITEM_GREEN_EXP_ID, 1)
        Grid.makeItem(msgRet.itemQingbao, ItemDefine.ITEM_QINGBAO_ID, 1)

        -- local oldConfig = BattleExcel.node[human.db.guajiID]
        -- local newConfig = BattleExcel.node[guajiID]
        local oldConfig = tBattleConfig.node[human.db.guajiID]
        local newConfig = tBattleConfig.node[guajiID]

        msgRet.expOld = oldConfig.hangExp
        msgRet.expNew = newConfig.hangExp
        msgRet.jinbiOld = oldConfig.hangJinbi
        msgRet.jinbiNew = newConfig.hangJinbi
        msgRet.greenExpOld = oldConfig.hangGreenExp
        msgRet.greenExpNew = newConfig.hangGreenExp
        msgRet.qingbaoOld = oldConfig.hangQingbao
        msgRet.qingbaoNew = newConfig.hangQingbao

        Msg.send(msgRet, human.fd)
    end

    -- 刷新排行榜
    human.db.maxBattleTime = os.time()
    BRoleLogic.updateData(BillboardDefine.TYPE_BATTLE, human.db)


    BreakThroughTheme = BreakThroughTheme or require("battle.BreakThroughTheme")
    BreakThroughTheme.EntranceRedDotUpdate(human)
end

-- GM
function setBattleByGm(human, val, maxBattleID)
    val = tonumber(val)
    if not val or val <= 1 then
        return
    end
    local tBattleConfig = BattleLogic_GetBattleConfig(human)
    if not tBattleConfig then
        return
    end

    local nodeConfig = tBattleConfig.node[val]
    if not nodeConfig then
        return
    end

    if maxBattleID then
        maxBattleID = tonumber(maxBattleID)
    else
        maxBattleID = val
    end

    local nBattleType = BattleLogic_GetBattleType(human)
    if EliteDefine.COPY_ELITE_NORMAL == nBattleType or  EliteDefine.COPY_LEVEL_ELITE == nBattleType or EliteDefine.COPY_ELITE_DIFFICULTY == nBattleType then
        local gameTimes = 0
        local battleID = BattleLogic_GetBattleGuaJiID(human)
        local allCfg = BattleExcel.node
        for i=battleID, val-1 do
            local singleCfg = allCfg[i]
            if singleCfg and type(singleCfg.gameType) == "number" and singleCfg.gameType == 1 then
                gameTimes = gameTimes + 1
            end
        end
        if EliteDefine.COPY_ELITE_NORMAL == nBattleType then
            human.db.battleGameTimes = (human.db.battleGameTimes or 0) + gameTimes
        elseif EliteDefine.COPY_LEVEL_ELITE == nBattleType then
            human.db.eliteBattleGameTimes = (human.db.eliteBattleGameTimes or 0) + gameTimes
        elseif EliteDefine.COPY_ELITE_DIFFICULTY == nBattleType then
            human.db.hardBattleGameTimes = (human.db.hardBattleGameTimes or 0) + gameTimes
        end

    end


    BattleLogic_SetBattleBattleID(human, val)
    BattleLogic_SetBattleGuaJiID(human, val)
    setBattleID(human, val)
    --human.db.battleID = val
    BattleLogic_QueryDifficulty(human)

    BreakThroughTheme = BreakThroughTheme or require("battle.BreakThroughTheme")
    BreakThroughTheme.EntranceRedDotUpdate(human)

    RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_1105)
end

--GM命令， 增加挂机时间，hourVal单位为：h
--如果超过最大可挂机时间，那么就增加最大挂机时间
function gmSetHangTime(human, hourVal)
    local tBattleOut = BattleLogic_GetBattleOut(human)
    if not tBattleOut then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "gmSetHangTime  func  calcBattleExpOut 获取挂机奖励列表失败 id = "
                    ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name)
        return
    end

    if not hourVal then
        return
    end

    local setHandTime = hourVal * 3600
    local maxHangTime = getHangMaxTime(human)


    if setHandTime > maxHangTime then
        setHandTime = maxHangTime
    end


    tBattleOut.expTs1 = tBattleOut.expTs1 - setHandTime
    tBattleOut.expTs2 = tBattleOut.expTs2 - setHandTime


    tBattleOut.itemTs1 = tBattleOut.itemTs1 - setHandTime
    tBattleOut.itemTs2 = tBattleOut.itemTs2 - setHandTime

end

function mopupQuery(human)
    ObjHuman.updateDaily(human)
    local nNowGuaJiID = human.db.guajiID
    local tBattleConfig = BattleExcel
    if -1 >= nNowGuaJiID or not tBattleConfig then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "mopupQuery 获取战斗配置、挂机ID 失败 id = "
                ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name)
        print("[mopupQuery] 获取战斗配置、挂机ID 失败")
        return
    end

    local msgRet = Msg.gc.GC_BATTLE_MOPUP_QUERY
    -- local guajiID = human.db.guajiID > #BattleExcel.node and #BattleExcel.node or human.db.guajiID
    local guajiID = nNowGuaJiID > #tBattleConfig.node and #tBattleConfig.node or nNowGuaJiID
    if guajiID == 0 then
        return Broadcast.sendErr(human, Lang.BATTLE_MOPUP_ERR)
    end

    human.db.mopupDoCnt = human.db.mopupDoCnt or 0
    local nextDoCnt = human.db.mopupDoCnt + 1
    nextDoCnt = nextDoCnt >= EliteDefine.BATTLE_MOPUP_MAX_LEN and EliteDefine.BATTLE_MOPUP_MAX_LEN or nextDoCnt
    -- local nodeConfig = BattleExcel.node[guajiID]
    local nodeConfig = tBattleConfig.node[guajiID]
    local dropID = nodeConfig.dropID
    local dropConfig = DropExcel.dropBattle[dropID]
    local dropCnt = 0

    --秘宝加成
    local talismanAdd_qb = (TalismanLogic.getTalismanAdd(human, TalismanLogic.OTHER_EFFECT_TBL.GJ_QB) or 0) / 100
    local talismanAdd_hero_exp = (TalismanLogic.getTalismanAdd(human, TalismanLogic.OTHER_EFFECT_TBL.GJ_HERO_EXP) or 0) / 100
    local talismanAdd_zs = (TalismanLogic.getTalismanAdd(human, TalismanLogic.OTHER_EFFECT_TBL.GJ_ZS) or 0) / 100
    local talismanAdd_dailyFeeCnt = TalismanLogic.getTalismanAdd(human, TalismanLogic.OTHER_EFFECT_TBL.GJ_SD_FREE_CNT) or 0

    -- ABS掉落活动显示到第一个
    dropCnt = DropExchangeLogic.getDropItemSaoQuery(human, msgRet, dropCnt)
    for i = 1, #dropConfig.dropRule do
        if dropCnt >= #msgRet.item then
            break
        end
        dropCnt = dropCnt + 1
        local tempConfig = dropConfig.dropRule[i]
        local itemID = tempConfig[1]
        local itemCnt = 1
        if itemID == ItemDefine.ITEM_ZUANSHI_ID and talismanAdd_zs > 0 then
            itemCnt = itemCnt + math.ceil(itemCnt * talismanAdd_zs)
        end
        Grid.makeItem(msgRet.item[dropCnt], itemID, itemCnt)
    end
    for i = 1, #dropConfig.dropRule3 do
        if dropCnt >= #msgRet.item then
            break
        end
        dropCnt = dropCnt + 1
        local tempConfig = dropConfig.dropRule3[i]
        local itemID = tempConfig[1]
        local itemCnt = 1
        if itemID == ItemDefine.ITEM_ZUANSHI_ID and talismanAdd_zs > 0 then
            itemCnt = itemCnt + math.ceil(itemCnt * talismanAdd_zs)
        end
        Grid.makeItem(msgRet.item[dropCnt], itemID, itemCnt)
    end

    -- local mapConfig = BattleExcel.map[nodeConfig.mapID]
    local mapConfig = tBattleConfig.map[nodeConfig.mapID]
    msgRet.name = mapConfig.name
    msgRet.item[0] = dropCnt
    local nNowAddFreeCnt = BattleLogic_GetAddFreeMopupNum(human)
    nNowAddFreeCnt = nNowAddFreeCnt + talismanAdd_dailyFeeCnt


    if TequanShopLogic.isActiveMopup(human) then
        msgRet.leftCnt = BATTLE_MOPUP_CNT_VIP - human.db.mopupDoCnt
    else
        msgRet.leftCnt = BATTLE_MOPUP_CNT - human.db.mopupDoCnt
    end

    -- 剩余次数小于0则为0，再加额外的
    msgRet.leftCnt = msgRet.leftCnt < 0 and 0 or msgRet.leftCnt

    -- 增加额外次数
    msgRet.leftCnt = msgRet.leftCnt + nNowAddFreeCnt
    msgRet.leftCnt = math.max(msgRet.leftCnt, 0)
    
    -- local mupopExcel = BattleExcel.mupop[nextDoCnt]
    local mupopExcel = tBattleConfig.mupop[nextDoCnt]
    if mupopExcel then
        if not TequanShopLogic.isActiveMopup(human) then
            msgRet.need = mupopExcel.cost
            if human.db.mopupFreeCnt < BATTLE_MOPUP_FREE_CNT then
                msgRet.need = 0
            end
        else
            msgRet.need = mupopExcel.vipCost
            if human.db.mopupFreeCnt < BATTLE_MOPUP_FREE_CNT_VIP then
                msgRet.need = 0
            end
        end
    else
        msgRet.need = 0
    end

    -- 如果有额外增加次数则需要为0
    if 0 < nNowAddFreeCnt then
        msgRet.need = 0
    end

    msgRet.vip = TequanShopLogic.isActiveMopup(human) and 1 or 0
    msgRet.nowCnt = human.db.mopupDoCnt
    msgRet.exp = nodeConfig.hangExp
    msgRet.jinbi = nodeConfig.hangJinbi
    --msgRet.greenExp = nodeConfig.hangGreenExp

    msgRet.greenExp = nodeConfig.hangGreenExp + math.ceil(nodeConfig.hangGreenExp * talismanAdd_hero_exp)
    msgRet.qingbao = nodeConfig.hangQingbao + math.ceil(nodeConfig.hangQingbao * talismanAdd_qb)
    
    print("[mopupQuery] nowCnt = "..msgRet.nowCnt.." leftCnt = "..msgRet.leftCnt.." nNowAddFreeCnt = "..nNowAddFreeCnt)
    Msg.send(msgRet, human.fd)
end

function mopupFight(human)
    -- 等级判断
    ObjHuman.updateDaily(human)
    local nNowGuaJiID = human.db.guajiID
    local tBattleConfig = BattleExcel
    if -1 >= nNowGuaJiID or not tBattleConfig then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "mopupFight 获取战斗配置、挂机ID 失败 id = "
                ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name)
        print("[mopupFight] 获取战斗配置、挂机ID 失败")
        return
    end

    local need = 0
    human.db.mopupDoCnt = human.db.mopupDoCnt or 0
    human.db.mopupFreeCnt = human.db.mopupFreeCnt or 0
    local nextDoCnt = human.db.mopupDoCnt + 1
    nextDoCnt = nextDoCnt >= EliteDefine.BATTLE_MOPUP_MAX_LEN and EliteDefine.BATTLE_MOPUP_MAX_LEN or nextDoCnt
    -- local mupopExcel = BattleExcel.mupop[nextDoCnt]
    local mupopExcel = tBattleConfig.mupop[nextDoCnt]

    local talismanAdd_dailyFeeCnt = TalismanLogic.getTalismanAdd(human, TalismanLogic.OTHER_EFFECT_TBL.GJ_SD_FREE_CNT) or 0
    local nNowAddFreeCnt = BattleLogic_GetAddFreeMopupNum(human)
    nNowAddFreeCnt = nNowAddFreeCnt + talismanAdd_dailyFeeCnt

    if not mupopExcel then
        return Broadcast.sendErr(human, Lang.BATTLE_MOPUP_CNT_OVER)
    end

    local msgRet = Msg.gc.GC_BATTLE_MOPUP_FIGHT
    msgRet.isDailyFirstMopup = human.db.mopupFreeCnt <= 0 and 1 or 0

    local bUseFree = false
    if not TequanShopLogic.isActiveMopup(human) then
        if human.db.mopupDoCnt >= BATTLE_MOPUP_CNT and 0 >= nNowAddFreeCnt then
            return Broadcast.sendErr(human, Lang.BATTLE_MOPUP_CNT_OVER)
        end
        need = mupopExcel.cost
        -- 如果免费次数小于1次，则按免费算
        if human.db.mopupFreeCnt < BATTLE_MOPUP_FREE_CNT then
            bUseFree = true
            need = 0
        end
    else
        if human.db.mopupDoCnt >= BATTLE_MOPUP_CNT_VIP and 0 >= nNowAddFreeCnt then
            return Broadcast.sendErr(human, Lang.BATTLE_MOPUP_CNT_OVER)
        end
        need = mupopExcel.vipCost
        -- 如果免费次数小于3次，则按免费算
        if human.db.mopupFreeCnt < BATTLE_MOPUP_FREE_CNT_VIP then
            bUseFree = true
            need = 0
        end
    end

    local bUseAddFree = false
    if need <= 0 and bUseFree == true then
        human.db.mopupFreeCnt = human.db.mopupFreeCnt + 1
    else
        if nNowAddFreeCnt > 0 then
            need = 0
            BattleLogic_SetAddFreeMopupNum(human, -1)
            bUseAddFree = true
        end
    end

    if need > 0 then
        if not ObjHuman.checkRMB(human, need) then
            return
        end
        ObjHuman.decZuanshi(human, - need, "battleMopup")
    end

    human.db.mopupDoCnt = bUseAddFree == true and human.db.mopupDoCnt or human.db.mopupDoCnt + 1

    -- local guajiID = human.db.guajiID > #BattleExcel.node and #BattleExcel.node or human.db.guajiID
    local guajiID = nNowGuaJiID > #tBattleConfig.node and #tBattleConfig.node or nNowGuaJiID
    if guajiID == 0 then
        return Broadcast.sendErr(human, Lang.BATTLE_MOPUP_ERR)
    end

    -- local config = BattleExcel.node[guajiID]
    local config = tBattleConfig.node[guajiID]
    -- local msgRet = Msg.gc.GC_BATTLE_MOPUP_FIGHT

    local vipExpAdd =(VipLogic.getPowerArgs(human, VipLogic.VIP_POWER7) or 0) / 100
    local vipJinAdd =(VipLogic.getPowerArgs(human, VipLogic.VIP_POWER8) or 0) / 100
    local vipGreenExpAdd =(VipLogic.getPowerArgs(human, VipLogic.VIP_POWER9) or 0) / 100
    local vipQingbaoAdd =(VipLogic.getPowerArgs(human, VipLogic.VIP_POWER19) or 0) / 100

    --秘宝加成
    local talismanAdd_qb = (TalismanLogic.getTalismanAdd(human, TalismanLogic.OTHER_EFFECT_TBL.GJ_QB) or 0) / 100
    local talismanAdd_hero_exp = (TalismanLogic.getTalismanAdd(human, TalismanLogic.OTHER_EFFECT_TBL.GJ_HERO_EXP) or 0) / 100
    local talismanAdd_zs = (TalismanLogic.getTalismanAdd(human, TalismanLogic.OTHER_EFFECT_TBL.GJ_ZS) or 0) / 100

    local exp = config.hangExp * 120
    local jinbi = config.hangJinbi * 120
    local greenExp = config.hangGreenExp * 120
    local qingbao = config.hangQingbao * 120
    local addExp = math.floor(exp *(1 + vipExpAdd))
    local addJinbi = math.floor(jinbi *(1 + vipJinAdd))
    local addGreenExp = math.floor(greenExp *(1 + vipGreenExpAdd))
    local addQingbao = math.floor(qingbao *(1 + vipQingbaoAdd))

    addQingbao = addQingbao + math.ceil(addQingbao * talismanAdd_qb)
    addGreenExp = addGreenExp + math.ceil(addGreenExp * talismanAdd_hero_exp)

    local time = 7200
    local itemTable = getItemOutsByTime(config, time)
    -- 掉落活动
    DropExchangeLogic.getDropItemSao(human, time, itemTable)

    local flag = GuideLogic.getGuideSkip(human, GuideLogic.SKIPTYPE_JUMP_BATTLE_SD)
    if not flag then
        local equipID = SysParameter.getSysParameter(SysParameter.PARAMETER_10)
        itemTable[equipID] = 1
    end

    itemTable[ItemDefine.ITEM_JINBI_ID] = itemTable[ItemDefine.ITEM_JINBI_ID] or 0
    itemTable[ItemDefine.ITEM_EXP_ID] = itemTable[ItemDefine.ITEM_EXP_ID] or 0
    itemTable[ItemDefine.ITEM_GREEN_EXP_ID] = itemTable[ItemDefine.ITEM_GREEN_EXP_ID] or 0
    itemTable[ItemDefine.ITEM_QINGBAO_ID] = itemTable[ItemDefine.ITEM_QINGBAO_ID] or 0

    itemTable[ItemDefine.ITEM_JINBI_ID] = itemTable[ItemDefine.ITEM_JINBI_ID] + addJinbi
    itemTable[ItemDefine.ITEM_EXP_ID] = itemTable[ItemDefine.ITEM_EXP_ID] + addExp
    itemTable[ItemDefine.ITEM_GREEN_EXP_ID] = itemTable[ItemDefine.ITEM_GREEN_EXP_ID] + addGreenExp
    itemTable[ItemDefine.ITEM_QINGBAO_ID] = itemTable[ItemDefine.ITEM_QINGBAO_ID] + addQingbao
    msgRet.item[0] = 0

    -- 双倍日
    local double = RoleSystemLogic.isDouble(human, RoleSystemDefine.ROLE_SYS_ID_1101)
    local rewardCnt = double and 2 or 1
    for itemID, itemCnt in pairs(itemTable) do
        itemCnt = itemCnt * rewardCnt
        
        if itemID == ItemDefine.ITEM_ZUANSHI_ID and talismanAdd_zs > 0 then
            itemCnt = itemCnt + math.ceil(itemCnt * talismanAdd_zs)
        end

        if not ItemDefine.isEquip(itemID) then
            msgRet.item[0] = msgRet.item[0] + 1
            Grid.makeItem(msgRet.item[msgRet.item[0]], itemID, itemCnt)
        end

        BagLogic.addItem(human, itemID, itemCnt, "battle")
    end
    msgRet.item[0] = EquipLogic.makeEquipItem(human, msgRet.item, msgRet.item[0])
    msgRet.double = double and 1 or 0

    msgRet.exp = itemTable[ItemDefine.ITEM_EXP_ID] * rewardCnt
    msgRet.jinbi = itemTable[ItemDefine.ITEM_JINBI_ID] * rewardCnt
    msgRet.greenExp = itemTable[ItemDefine.ITEM_GREEN_EXP_ID] * rewardCnt
    msgRet.qingbao = itemTable[ItemDefine.ITEM_QINGBAO_ID] * rewardCnt

    Msg.send(msgRet, human.fd)
    mopupQuery(human)
    query(human)

    TreasureChestLogic.TreasureChestLogic_GetAllBoxNum(human)
    RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_1101)
    GuideLogic.setDoSpecialGuide(human, GuideLogic.SKIPTYPE_JUMP_BATTLE_SD)
    DailyTaskLogic.recordDailyTaskFinishCnt(human, DailyTaskLogic.DAILY_TASK_ID_12, 1)
    ChengjiuLogic.onCallback(human, ChengjiuDefine.CJ_TASK_TYPE_3, 1)

    HeroLogLogic.finishTaskCB(human, HeroLogLogic.HERO_LOG_TYPE_3, 1)
    RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_2001)
    YunYingLogic.onCallBack(human, "onMopup", 1)
    WeekTaskLogic.recordWeekTaskFinishCnt(human, WeekTaskLogic.WEEK_TASK_ID_6, 1)

    TriggerLogic.PublishEvent(TriggerDefine.EVENT_TYPE_SWEEP, human.db._id, 1)
end

function updateDaily(human)
    human.db.mopupFreeCnt = 0
    human.db.mopupDoCnt = 0
    human.db.mopupAddFreeCnt = 0

    dailyPassLevelHandle(human)
end

function getTongGuanReward(human, id)
    local tBattleConfig = BattleLogic_GetBattleConfig(human)
    local nNowGuaJiID = BattleLogic_GetBattleGuaJiID(human)
    local nBattleID = BattleLogic_GetBattleBattleID(human)
    if not tBattleConfig or -1 >= nNowGuaJiID or -1 >= nBattleID then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "[getTongGuanReward] 获取战斗配置、挂机ID、战斗节点ID 失败 id = "
                ..human.db._id.. "newUniqueTag = "..human.db.newUniqueTag.." name = "..human.db.name)
        print("[getTongGuanReward] 获取战斗配置、挂机ID 失败")
        return
    end

    local cf = tBattleConfig.node[id]
    if not cf then
        return
    end
    if #cf.tongguan < 1 then
        return
    end

    print("[getTongGuanReward] 玩家请求领取奖励 当前的节点信息 id = "
            ..id.." nNowGuaJiID = "..nNowGuaJiID.." nBattleID = "..nBattleID)

    if nBattleID < id then
        return
    end

    if isGetReward(human, id) then
        return
    end

    if #cf.tongguan == 1 and cf.tongguan[1][3] then
        -- 英雄
        if HeroLogic.getEmptyCnt(human) < 1 then
            return Broadcast.sendErr(human, Lang.HERO_BAG_FULL)
        end
        local heroID = cf.tongguan[1][1]
        local cnt = cf.tongguan[1][2]
        local star = cf.tongguan[1][3]
        if cnt <= 0 then
            return
        end
        setGetReward(human, id)
        local msg = Msg.gc.GC_SUIPIAN_SUMMON
        for i = 1, cnt do
            local heroGrid = HeroGrid.createHeroGrid(heroID, star)

            HeroLogic.addHeroByGrid(human, heroGrid, "battle_extra_reward")

            SuipianLogic.makeResultItemData(msg.heroList[i], heroID, 1, 1)
        end

        msg.isHero = 1
        msg.list[0] = 0
        msg.heroList[0] = cnt
        msg.fenJieList[0] = 0

        Msg.send(msg, human.fd)
    else
        setGetReward(human, id)
        BagLogic.addItemList(human, cf.tongguan, "tongguan")
    end

    -- 触发国王君临奖励
    ChatPaoMaLogic.broadcast(human, ChatPaoMaLogic.PAOMA_TYPE_BROAD_TYPE5, id)
    query(human)

    RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_1101)
    if id == 80 then
        KingWorldLogic.setState(human, 1)
    end
end

function isDot(human)
    -- 通过大关卡奖励
    local chapterReward = BattleLogic_GetChapterReward(human)
    local nBattleID = BattleLogic_GetBattleBattleID(human)
    local tBattleExcel = BattleLogic_GetBattleConfig(human)
    if -1 >= nBattleID or not tBattleExcel or not chapterReward then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "[isDot] 获取不到对应的大关卡奖励表 或配置表、战斗ID")
        print("[isDot] 获取不到对应的大关卡奖励表 或配置表、战斗ID id = "..human.db._id)
        return
    end

    -- local chapterReward = human.db.chapterReward or { }
    -- local battleID = human.db.battleID
    -- local battleNodeConfig = BattleExcel.node[battleID]
    local battleID = nBattleID
    local battleNodeConfig = tBattleExcel.node[battleID]
    if not battleNodeConfig then
        local nodeCnt = #tBattleExcel.node
        local mapCnt = #tBattleExcel.map
        if battleID == nodeCnt + 1 and not chapterReward[mapCnt] then
            return true
        else
            return false
        end
    else
        local mapID = battleNodeConfig.mapID
        for i = mapID - 1, 1, -1 do
            if not chapterReward[i] then
                return true
            end
        end
    end

    return false

    -- if human.db.lv < 9 then
    --     return false
    -- end

    -- -- 有免费扫荡次数的时候有红点
    -- human.db.mopupFreeCnt = human.db.mopupFreeCnt or 0
    -- if TequanShopLogic.isActiveMopup(human) then
    --     if human.db.mopupFreeCnt < 3 then
    --         return true
    --     end
    -- else
    --     if human.db.mopupFreeCnt < 1 then
    --         return true
    --     end
    -- end
    -- -- 有通关奖励可领取的时候有红点
    -- if hasCanGetReward(human) then
    --     return true
    -- end

    -- -- 战役挂机时间超过1小时的时候有红点
    -- if human.db.battleOut ~= nil then
    --     local now = os.time()
    --     local outSec = now - human.db.battleOut.expTs1
    --     if outSec >= 3600 then
    --         return true
    --     end
    -- end

    -- return false
end

function clacItemTwoHours(human)
    local tBattleConfig = BattleLogic_GetBattleConfig(human)
    local nGuaJiID = BattleLogic_GetBattleGuaJiID(human)
    if not tBattleConfig or -1 >= nGuaJiID then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "[clacItemTwoHours] 获取到的挂机ID、战斗配置不正确 id = "..human.db._id)
        print("[clacItemTwoHours] 获取到的挂机ID、战斗配置不正确 id = "..human.db._id)
        return
    end

    local items = { }
    local outCnt = math.floor(7200 / BATTLE_HANG_ITEM_OUT_PERIOD)
    -- local guajiID = human.db.guajiID > #BattleExcel.node and #BattleExcel.node or human.db.guajiID
    local guajiID = nGuaJiID > #tBattleConfig.node and #tBattleConfig.node or nGuaJiID
    if guajiID == 0 then
        return items
    end

    -- local nodeConfig = BattleExcel.node[guajiID]
    local nodeConfig = tBattleConfig.node[guajiID]
    local dropID = nodeConfig.dropID
    local dropConfig = DropExcel.dropBattle[dropID]
    local totalWeight = 0
    for i = 1, #dropConfig.dropRule do
        local tempConfig = dropConfig.dropRule[i]
        totalWeight = totalWeight + tempConfig[4]
    end
    for j = 1, outCnt do
        local randomNum = math.random(1, totalWeight)
        for i = 1, #dropConfig.dropRule do
            local tempConfig = dropConfig.dropRule[i]
            local itemID = tempConfig[1]
            local itemCnt1 = tempConfig[2]
            local itemCnt2 = tempConfig[3]
            local itemRate = tempConfig[4]
            if randomNum <= itemRate then
                local realAdd = math.random(itemCnt1, itemCnt2)
                items[itemID] = realAdd
                break
            end
            randomNum = randomNum - itemRate
        end
    end

    return items
end

local MailManager = require("mail.MailManager")
function clacItemCntGM(human, cnt, guajiID)
    local tBattleConfig = BattleLogic_GetBattleConfig(human)
    if not tBattleConfig then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "[clacItemCntGM] 获取到战斗配置不正确 id = "..human.db._id)
        print("[clacItemTwoHours] 获取到的战斗配置不正确 id = "..human.db._id)
        return
    end

    local items = { }
    local outCnt = cnt
    local nodeConfig = tBattleConfig.node[guajiID]
    local dropID = nodeConfig.dropID
    local dropConfig = DropExcel.dropBattle[dropID]
    local totalWeight = 0
    for i = 1, #dropConfig.dropRule do
        local tempConfig = dropConfig.dropRule[i]
        totalWeight = totalWeight + tempConfig[4]
    end
    for j = 1, outCnt do
        local randomNum = math.random(1, totalWeight)
        for i = 1, #dropConfig.dropRule do
            local tempConfig = dropConfig.dropRule[i]
            local itemID = tempConfig[1]
            local itemCnt1 = tempConfig[2]
            local itemCnt2 = tempConfig[3]
            local itemRate = tempConfig[4]
            if randomNum <= itemRate then
                local realAdd = math.random(itemCnt1, itemCnt2)
                items[itemID] = items[itemID] or 0
                items[itemID] = items[itemID] + realAdd
                break
            end
            randomNum = randomNum - itemRate
        end
    end
    local item = { }
    local index = 0
    for k, v in pairs(items) do
        index = index + 1
        item[index] = { k, v }
    end
    MailManager.add(MailManager.SYSTEM, human.db._id, "", "", item, "")
end

function getBattleID(human)
    local num = 0

    local nGuaJiID = BattleLogic_GetBattleGuaJiID(human)
    if -1 < nGuaJiID then
        num = nGuaJiID
    end
    -- if human.db.guajiID then
    --     num = human.db.guajiID or 0
    -- end
    return num
end

-- 根据类型获取已经通关的最大关卡Id
function GetBattleIdByType(human, battleType)
    return BattleLogic_GetBattleGuaJiIDByType(human, battleType)
end

--------------------------------------------- combat ----------------------------------------------
function getCombatMonsterOutID(human, side)
    if side ~= CombatDefine.DEFEND_SIDE then
        return
    end

    local nBattleID = BattleLogic_GetBattleBattleID(human)
    local tBattleExcel = BattleLogic_GetBattleConfig(human)
    if not tBattleExcel or -1 >= nBattleID then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "[getCombatMonsterOutID] 获取到的战斗配置、或战斗ID不正确 id = "..human.db._id)
        print("[getCombatMonsterOutID] 获取到的战斗配置、或战斗ID不正确 id = "..human.db._id)
        return
    end

    -- local config = BattleExcel.node[human.db.battleID]
    local config = tBattleExcel.node[nBattleID]
    if not config then
        return
    end

    -- return config.monsterOutID

    local teamIdx = getTeamIdx(human)
    return config.monsterOutID[teamIdx]
end

function getCombatName(human)
    local nBattleID = BattleLogic_GetBattleBattleID(human)
    if -1 >= nBattleID then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "[getCombatMonsterOutID] 获取到的战斗ID不正确 id = "..human.db._id)
        print("[getCombatMonsterOutID] 获取到的战斗ID不正确 id = "..human.db._id)
        return ""
    end

    local name = getBattleName(human, nBattleID)

    return Util.format(Lang.COMBAT_BATTLE_EXNAME, name)
end
-------------------------------------------combat end--------------------

function battleSharkQuery(human, nodeID)
    local msgRet = Msg.gc.GC_BATTLE_SHARK_QUERY
    local videoTb = BattleDBLogic.queryBattleDbByNodeID(nodeID)
    if videoTb == nil then
        msgRet.battleShark[0] = 0
    else
        local len = #videoTb.shark
        for i = 1, len do
            local v = videoTb.shark[i]
            RoleLogic.makeRoleBase(v.roleBase, msgRet.battleShark[i].roleBase)
            msgRet.battleShark[i].type = i
            msgRet.battleShark[i].videoUuid = v.videoUuid
            msgRet.battleShark[i].param = v.param or 0
        end
        msgRet.battleShark[0] = len
    end
    -- Msg.trace(msgRet)
    Msg.send(msgRet, human.fd)
end

local QueryRoleByNodeID = { }
function worldMapRoleListQuery(human, worldMapId)
    -- if worldMapId == 1 then return end
    local nNowBattleType = BattleLogic_GetBattleType(human)
    if EliteDefine.COPY_ELITE_ERROR >= nNowBattleType then
        nNowBattleType = EliteDefine.COPY_ELITE_NORMAL
    end

    local msgRet = Msg.gc.GC_BATTLE_WORLD_MAP_ROLELIST_QUERY
    local len = 0
    for k, v in pairs(BATTLE_LEVEL_PLAYER_LIST[nNowBattleType][worldMapId]) do
        len = len + 1
        RoleLogic.makeRoleBase(v, msgRet.roleList[len])
        if len >= 50 then
            break
        end
    end
    msgRet.roleList[0] = len
    Msg.send(msgRet, human.fd)
end

-- 章节(地图)掉落列表
function sendMapDroItemsList(human)
    local nBattleID = BattleLogic_GetBattleBattleID(human)
    local tBattleExcel = BattleLogic_GetBattleConfig(human)
    if not tBattleExcel then
        print("[sendMapDroItemsList] 获取当前战斗配置失败 id = "..human.db._id)
        return
    end

    local battleID = 1
    if -1 < nBattleID then
        battleID = nBattleID
    end

    local maxBattleConfig = tBattleExcel.node[battleID]
    local maxMapID = maxBattleConfig and maxBattleConfig.mapID or(#tBattleExcel.node + 1)
    local msgRet = Msg.gc.GC_BATTLE_MAP_DROPITEMS_LIST
    msgRet.list[0] = 0
    for mapID, mapConfig in ipairs(tBattleExcel.map) do
        msgRet.list[0] = msgRet.list[0] + 1
        local net = msgRet.list[msgRet.list[0]]
        net.mapID = mapID
        net.mapName = mapConfig.name
        net.isOpen =(mapID <= maxMapID) and 1 or 0
    end
    -- Msg.trace(msgRet)
    Msg.send(msgRet, human.fd)
end

-- 章节(地图)掉落详情
function sendMapDroItemsDetail(human, mapID)
    local tBattleExcel = BattleLogic_GetBattleConfig(human)
    if not tBattleExcel then
        print("[sendMapDroItemsDetail] 获取当前战斗配置失败 id = "..human.db._id)
        return
    end

    local mapConfig = tBattleExcel.map[mapID]
    if not mapConfig then
        return
    end

    local msgRet = Msg.gc.GC_BATTLE_MAP_DROPITEMS_DETAIL
    msgRet.mapID = mapID
    msgRet.items[0] = math.min(#mapConfig.dropItemsShow, #msgRet.items)
    for i = 1, msgRet.items[0] do
        local itemID = mapConfig.dropItemsShow[i][1]
        local itemCnt = mapConfig.dropItemsShow[i][2]
        Grid.makeItem(msgRet.items[i], itemID, itemCnt)
    end
    -- Msg.trace(msgRet)
    Msg.send(msgRet, human.fd)
end

-- 获取当前战役的 怪物ID
function getBattleMonsterOutID(human)
    local nBattleID = BattleLogic_GetBattleBattleID(human)
    local tBattleExcel = BattleLogic_GetBattleConfig(human)
    if -1 >= nBattleID or not tBattleExcel then
        print("[getBattleMonsterOutID] 获取当前战斗ID、配置失败 id = "..human.db._id)
        return 0
    end

    -- local battleID = human.db.battleID
    -- local config = BattleExcel.node[battleID]
    local config = tBattleExcel.node[nBattleID]
    -- return config.monsterOutID

    local teamIdx = getTeamIdx(human)
    return config.monsterOutID[teamIdx]
end

function onUpdatePos(human)
    BRoleLogic.updateData(BillboardDefine.TYPE_ZHANDOULI, human.db)
end

function getNextRewardName(human)
    local guajiID = BattleLogic_GetBattleGuaJiID(human)
    local tBattleConfig = BattleLogic_GetBattleConfig(human)
    if not tBattleConfig or -1 >= guajiID then
        return nil, nil
    end

    -- for i = human.db.guajiID + 1, #BattleExcel.node do
    for i = guajiID + 1, #tBattleConfig.node do
        if #tBattleConfig.node[i].tongguan > 0 then
            local itemID = tBattleConfig.node[i].tongguan[1][1]
            local itemConfig = ItemDefine.getConfig(itemID)
            if itemConfig == nil then
                return
            else
                return i - guajiID, itemConfig.name
            end
        end
    end
end

--[[
    通关章节后领取奖励
]]
function battleChapterReward(human, mapID)
    local tBattleConfig = BattleLogic_GetBattleConfig(human)
    local nBattleID = BattleLogic_GetBattleBattleID(human)
    local tChapterReward = BattleLogic_GetChapterReward(human)
    if not tBattleConfig or -1 >= nBattleID or not tChapterReward then
        print("[battleChapterReward] 获取到的战斗ID、配置、通关章节奖励表 不正确 id = "..human.db._id)
        return
    end

    -- local mapConf = tBattleConfig.map[mapID]
    -- if not mapConf then
    --     return
    -- end

    local gujiID = BattleLogic_GetBattleGuaJiID(human)
    if gujiID <= 0 then
        return
    end

    local nBattleType = BattleLogic_GetBattleType(human)
    local tag = BattleLogic_GetBattleAdopt(human, nBattleType)

    -- 没有通过当前大关卡
    -- local battleID = nBattleID
    local battleNodeConfig = tBattleConfig.node[gujiID]
    local roleMapId
    if not battleNodeConfig then
        if gujiID ~= #tBattleConfig.node + 1 then
            return
        else
            roleMapId = #tBattleConfig.map
        end
    else
        roleMapId = battleNodeConfig.mapID

        -- 每40层升一个地图
        if tag ~= 1 and gujiID % 40 ~= 0 then
            roleMapId = roleMapId - 1
        end
    end

    -- if mapID > roleMapId then
    --     return
    -- end

    -- -- if not human.db.chapterReward then
    -- --     human.db.chapterReward = { }
    -- -- end

    -- -- 判断是否已经领取了当前大关卡的奖励
    -- -- local chapterReward = human.db.chapterReward
    -- local chapterReward = tChapterReward

    -- if chapterReward[mapID] then
    --     return Broadcast.sendErr(human, Lang.FRIEND_HEART_GET_HAD)
    -- end

    -- chapterReward[mapID] = true
    -- ObjHuman.save(human)

    -- local reward = mapConf.reward
    -- BagLogic.addItemList(human, reward, "chapterReward")
    -- RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_1105)


    -- 新修改: 一键领取所有可领取的奖励
    local len = 0
    local itemLArr = {}
    local getMapIdList = {}

    for mapId, mapCfg in ipairs(tBattleConfig.map) do
        if not tChapterReward[mapId] and roleMapId >= mapId then
            for _, itemInfo in ipairs(mapCfg.reward) do
                len = len + 1
                itemLArr[len] = {itemInfo[1], itemInfo[2]}
            end

            getMapIdList[mapId] = true
            tChapterReward[mapId] = true
        end
    end

    if len > 0 then
        BagLogic.addItemList(human, itemLArr, "chapterReward")
        RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_1105)

        len = 0
        local msgRet = Msg.gc.GC_BATTLE_CHAPTER_REWARD
        local mapIDList = msgRet.mapIDList
        for mapId in pairs(getMapIdList) do
            len = len + 1
            mapIDList[len] = mapId
        end
        mapIDList[0] = len

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

-- 判断难度是否解锁
local function BattleLogic_CheckDiffIsOK(human, nSelectDiff)
    -- 默认难度直接通过
    if EliteDefine.COPY_ELITE_NORMAL == nSelectDiff then
        return EliteDefine.COPY_SELECT_SUCC
    end

    -- 等级不够
    if human.db.lv < EliteDefine.COPY_ELITE_ULEV then
        return EliteDefine.COPY_SELECT_LEV
    end

    local nChoseBattleID = (EliteDefine.COPY_LEVEL_ELITE == nSelectDiff) and human.db.battleID or human.db.battleID_elite
    
    return nChoseBattleID > EliteDefine.COPY_ELITE_LEVEL and EliteDefine.COPY_SELECT_SUCC or EliteDefine.COPY_SELECT_MAIN
end

-- 获取未领取奖励
local function BattleLogic_GetNorewardReceived(human, nBattleType, cIndex)
    --print("1:[BattleLogic_GetNorewardReceived] id = "..human.db._id.." nBattleType = "..nBattleType)

    local nBattleID = BattleLogic_GetBattleIDByType(human, nBattleType)
    local tBattleConfig = BattleLogic_GetBattleConfigByType(nBattleType)
    local nGuaJiID = BattleLogic_GetBattleGuaJiIDByType(human, nBattleType)
    if -1 >= nBattleID or not tBattleConfig or -1 >= nGuaJiID then
        print("[BattleLogic_GetNorewardReceived] 获取配置失败")
        return
    end

    local msgRet = Msg.gc.GC_BATTLE_QUERY_ALL
    msgRet.index = nBattleType
    msgRet.list[0] = 0
    msgRet.isEnd = 0
    msgRet.curIndex = 0
    local curIndex = 1
    -- human.db.battleID = human.db.battleID or 1
    -- if human.db.battleID == 0 then
    --     human.db.battleID = 1
    -- end
    human.db.mopupDoCnt = human.db.mopupDoCnt or 0

    local curBattleNumber = cIndex
    local curTotalNumber =  0

    local battleNumber = #tBattleConfig.node

    -- local nGuajiID = human.db.guajiID or 0
    --print("[BattleLogic_GetNorewardReceived] nGuajiID = "..nGuaJiID.. " nBattleID = "..nBattleID)

    -- 判断ID得用战斗ID-1, 因为挂机ID可以设置
    local nJudeNum = nGuaJiID
    local nBattleAdopt = BattleLogic_GetBattleAdopt(human, nBattleType)
    -- 已通过
    if nBattleAdopt == 1 then
        nJudeNum = battleNumber
    else
        nJudeNum = nBattleID - 1
    end

    --print("[BattleLogic_GetNorewardReceived] nJudeNum = "..nJudeNum)

    for k,v in ipairs(tBattleConfig.node) do
        if #v.tongguan ~= 0 then
            -- 领取状态
            local state = false
            if k <= nJudeNum then
                state = isGetRewardByType(human, k, nBattleType)
            end

            if not (state == true) then
                local r = STATUS_CANGET 
                --(k > nGuaJiID) and STATUS_CANGET or STATUS_NONE
                --未领取
                if r == STATUS_CANGET then
                    curTotalNumber = curTotalNumber + 1
                    if curTotalNumber > curBattleNumber then
                        --构造
                        local cf = tBattleConfig.node[k].tongguan
                        local net =  msgRet.list[curIndex]
                        net.status = k <= nJudeNum and STATUS_CANGET or STATUS_NONE
                        net.levelName = getBattleNameByType(k, nBattleType)
                        net.index = k
                        net.nowBattle = getBattleNameByType(nJudeNum, nBattleType)
                        net.reward[0] = 0
                        net.heroReward[0] = 0
                        if cf[1][3] then
                            net.heroReward[0] = 1
                            local other = { }
                            other.star = cf[1][3]
                            HeroGrid.makeHeroSimpleByID(net.heroReward[1], cf[1][1], nil, other, human)
                            HeroGrid.makeHeroSimpleByGeneral(net.heroReward[1], cf[1][1])

                            -- local tHeroInfo = Util.printTable(net.heroReward)
                            -- print("[BattleLogic_GetNorewardReceived] 英雄数据 k = "..k.." 获取的数据 = "..tHeroInfo)
                        else
                            net.reward[0] = 1
                            for i = 1, net.reward[0] do
                                local itemID = cf[i][1]
                                local itemCnt = cf[i][2]
                                Grid.makeItem(net.reward[i], itemID, itemCnt)
                            end
                        end
                        curIndex = curIndex + 1
                        msgRet.list[0] = msgRet.list[0] + 1   

                        if msgRet.list[0] >= 15 then
                            local ret = 0
                            if k < battleNumber then
                                for kk, vv in ipairs(tBattleConfig.node) do
                                    if kk > k then
                                        if v.tongguan ~= 0 then
                                            local nNextstate = isGetRewardByType(human,kk, nBattleType)
                                            if not (nNextstate == true) then  
                                                local rr =(kk > nGuaJiID) and STATUS_CANGET or STATUS_NONE
                                                --未领取
                                                if rr == STATUS_CANGET then
                                                    ret = 1
                                                    break
                                                end
                                            end
                                        end
                                    end
                                end
                            end

                            if ret == 1 then
                                --没有结束
                                msgRet.isEnd = 0 
                            else
                                msgRet.isEnd = 1
                            end

                            msgRet.curIndex=curTotalNumber
                            -- curIndex = 1
                            Msg.send(msgRet, human.fd)

                            --local info=Util.printTable(msgRet)
                            --print("getNorewardReceived0->>:"..info)

                            return
                        end
                    end
                end
            else
                print("[BattleLogic_GetNorewardReceived] 奖励已领取:"..k)
            end
        end
    end
    --结束
    msgRet.isEnd = 1
    msgRet.curIndex=0

    Msg.send(msgRet, human.fd)
end


-- 客户端请求查询难度
function CG_ELITE_OPEN(human)
    BattleLogic_QueryDifficulty(human)
end

-- 客户端请求切换难度
function CG_ELITE_SELECT(human, nSelectDiff)
    if EliteDefine.COPY_ELITE_NORMAL > nSelectDiff or EliteDefine.COPY_ELITE_DIFFICULTY < nSelectDiff then
        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "[CG_ELITE_SELECT] 客户端传入的参数不正确 id = "..human.db._id .. "nSelectDiff = "..nSelectDiff)
        print("[CG_ELITE_SELECT] 客户端选择难度，上传的难度不正确 id = "..human.db._id .. "nSelectDiff = "..nSelectDiff)
        return
    end

    -- local nStatus =  BattleLogic_CheckDiffIsOK(human, nSelectDiff)

    local nStatus = -1
    local battleSpeData = getBattleSpeData(human)
    if battleSpeData[nSelectDiff] then
        nStatus = EliteDefine.COPY_SELECT_SUCC
    else
        nStatus = BattleLogic_CheckDiffIsOK(human, nSelectDiff)
    end


    print("[CG_ELITE_SELECT] nStatus = "..nStatus.. " 选择的难度 nSelectDiff = "..nSelectDiff)
    -- 设置类型
    if EliteDefine.COPY_SELECT_SUCC == nStatus then
        BattleLogic_SetBattleType(human,nSelectDiff)
    end

    local msgRet = Msg.gc.GC_ELITE_SELECT
    msgRet.status = nStatus

    Msg.send(msgRet,human.fd)
end

-- 客户端请求领取奖励
function CG_BATTLE_TONGGUAN_REWARD_GET_COPY(human, nBattleType)
    local tBattleConfig = BattleLogic_GetBattleConfig(human)
    local nGuaJiID = BattleLogic_GetBattleGuaJiIDByType(human, nBattleType)
    local nBattleID = BattleLogic_GetBattleIDByType(human, nBattleType)
    if not tBattleConfig or -1 >= nGuaJiID or -1 >= nBattleID then
        return
    end
    
    local tMsgRet = Msg.gc.GC_BATTLE_TONGGUAN_REWARD_GET_COPY
    local nAdopt = BattleLogic_GetBattleAdopt(human, nBattleType)
    local nChoseLen = nBattleID - 1
    local nConfigLen = #tBattleConfig.node

    if 1 == nAdopt then
        nChoseLen = nBattleID
    else
        if 0 >= nChoseLen or nChoseLen > nConfigLen then
            tMsgRet.status = 0
            Msg.send(tMsgRet, human.fd)
            return
        end
    end

    print("[CG_BATTLE_TONGGUAN_REWARD_GET_COPY] nChoseLen = "..nChoseLen.. " nBattleID = "..nBattleID)

    tMsgRet.status = 1
    local tItemList = {}
    for id = 1, nChoseLen, 1 do
        local tNodeCofig = tBattleConfig.node[id]
        if tNodeCofig and #tNodeCofig.tongguan >= 1 then
            -- 未领取
            if false == isGetRewardByType(human, id, nBattleType) then
                print("[CG_BATTLE_TONGGUAN_REWARD_GET_COPY] 未领取的 id = "..id)
                if #tNodeCofig.tongguan == 1 and tNodeCofig.tongguan[1][3] then
                    -- 英雄
                    if HeroLogic.getEmptyCnt(human) < 1 then
                        return Broadcast.sendErr(human, Lang.HERO_BAG_FULL)
                    end
                    local heroID = tNodeCofig.tongguan[1][1]
                    local cnt = tNodeCofig.tongguan[1][2]
                    local star = tNodeCofig.tongguan[1][3]
                    if cnt > 0 then
                        -- 设置已领取
                        setGetRewardByType(human, id, nBattleType)
    
                        local msg = Msg.gc.GC_SUIPIAN_SUMMON
                        for i = 1, cnt do
                            local heroGrid = HeroGrid.createHeroGrid(heroID, star)
            
                            HeroLogic.addHeroByGrid(human, heroGrid, "battle_extra_reward")
            
                            SuipianLogic.makeResultItemData(msg.heroList[i], heroID, 1, 1)
                        end
            
                        msg.isHero = 1
                        msg.list[0] = 0
                        msg.heroList[0] = cnt
                        msg.fenJieList[0] = 0
            
                        Msg.send(msg, human.fd)
                    else
                        Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "[CG_BATTLE_TONGGUAN_REWARD_GET_COPY] 领取奖励配置的英雄cnt为<= 0 id = "..human.db.name)
                        print("[CG_BATTLE_TONGGUAN_REWARD_GET_COPY] 不正确的 英雄碎片数量")
                    end
                else
                    setGetRewardByType(human, id, nBattleType)

                    for k, v in ipairs(tNodeCofig.tongguan) do
                        local nItemID = v[1]
                        local nItemNum = v[2]
                        tItemList[nItemID] = tItemList[nItemID] or 0
                        tItemList[nItemID] = tItemList[nItemID] + nItemNum
                    end
                    
                    --BagLogic.addItemList(human, tNodeCofig.tongguan, "tongguan")
                end

                -- 触发国王君临奖励
                ChatPaoMaLogic.broadcast(human, ChatPaoMaLogic.PAOMA_TYPE_BROAD_TYPE5, id)

                RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_1101)
                if id == 80 then
                    KingWorldLogic.setState(human, 1)
                end
            end
        end
    end

    if nil ~= _G.next(tItemList) then
        local tPrize = {}
        for k, v in pairs(tItemList) do
            table.insert(tPrize, {k,v})
        end

        BagLogic.addItemList(human, tPrize, "tongguan")
    end
    
    query(human)

    Msg.send(tMsgRet, human.fd)
end

-- 客户端请求查询所有未领取奖励
function CG_BATTLE_QUERY_ALL(human, nBattleType, curIndex)
    if EliteDefine.COPY_ELITE_NORMAL > nBattleType or EliteDefine.COPY_ELITE_DIFFICULTY < nBattleType then
        print("[CG_BATTLE_QUERY_ALL] 客户端请求的战斗类型不正确 id = "..human.db._id.." nType = "..nBattleType)
        return
    end

    BattleLogic_GetNorewardReceived(human, nBattleType, curIndex)
end

-- 判断是否是最后一关
function BattleLogic_IsLastLevels(human, nBattleType)
    local tBattleConfig = BattleLogic_GetBattleConfigByType(nBattleType)
    local nBattleType = BattleLogic_GetBattleType(human)
    if not tBattleConfig or -1 >= nBattleType then
        return false
    end

    local nValue = BattleLogic_GetBattleAdopt(human, nBattleType)
    return nValue == 1
end

-- 增加扫荡次数
function BattleLogic_AddMopup(human, nGoodsID, nGoodsNum)
    if not human or 0 >= nGoodsID or 0 >= nGoodsNum then
        return
    end

    if EliteDefine.BATTLE_ADD_MOPUP_GOODID ~= nGoodsID then
        return
    end

    -- -- 检测数量
    -- if false == BagLogic.checkItemCnt(human, nGoodsID, nGoodsNum) then
    --     return
    -- end

    -- -- 消耗物品
    -- BagLogic.delItem(human, nGoodsID, nGoodsNum, "battle")

    -- 增加次数
    BattleLogic_SetAddFreeMopupNum(human, nGoodsNum)

    Broadcast.sendUp(human, Util.format(Lang.BATTLE_GET_MOPUPNUM, nGoodsNum))

    Log.write(Log.LOGID_OSS_BATTLE_MAINGATE, "[BattleLogic_AddMopup] 增加扫荡次数 name = "
                ..human.db.name.." nAddNum ="..nGoodsNum)

    print("[BattleLogic_AddMopup] 玩家增加扫荡次数 name = "
            ..human.db.name.." nAddNum ="..nGoodsNum)
end

--获取某个模式的通关层数
function GetLevelByType(human, battleType)
    if EliteDefine.COPY_ELITE_NORMAL == battleType then
        return human.db.guajiID
    elseif EliteDefine.COPY_LEVEL_ELITE == battleType then
        return human.db.guajiID_elite
    elseif EliteDefine.COPY_ELITE_DIFFICULTY == battleType then
        return human.db.guajiID_hard
    else
        return -1
    end
end


--获取小游戏奖励
function GetGameReward(human, indexStr)
    local battleGameTimes = human.db.battleGameTimes or 0
    local eliteBattleGameTimes = human.db.eliteBattleGameTimes or 0
    local hardBattleGameTimes = human.db.hardBattleGameTimes or 0


    if battleGameTimes <= 0 and eliteBattleGameTimes <= 0 and hardBattleGameTimes <= 0 then
        return Broadcast.sendErr(human, Lang.JINBI_EXCHANGE_ERR_CNT)
    end

    local indexVec = Util.split(indexStr, "|")
    if #indexVec <= 0 or #indexVec > 3 then
        return Broadcast.sendErr(human, Lang.COMMON_ARGUMENT_ERROR)
    end

    local maxTimes = 1
    local maxTimesIdx
    local sumIndexTbl = {}

    for _, idx in ipairs(indexVec) do
        idx = tonumber(idx)
        if not sumIndexTbl[idx] then
            sumIndexTbl[idx] = 1
        else
            sumIndexTbl[idx] = sumIndexTbl[idx] + 1
            maxTimes = sumIndexTbl[idx]
            maxTimesIdx = idx
        end
    end

    local itemId, itemCnt = 0,0
    local configs = BattleExcel.gameReward

    if maxTimes <= 1 then
        local randIndex = math.random(1, #indexVec)
        maxTimesIdx = tonumber(indexVec[randIndex])
    end

    for _, v in ipairs(configs) do
        if v.itemID == maxTimesIdx then
            itemId = v.itemID
            if maxTimes <= 1 then
                itemCnt = v.itemCnt
            else
                itemCnt = v["itemCnt"..maxTimes]
            end
            break
        end
    end

    if not itemId or not itemCnt  then
        return Broadcast.sendErr(human, Lang.COMMON_COMFIG_ERROR)
    end

    if battleGameTimes > 0 then
        battleGameTimes = battleGameTimes - 1
        human.db.battleGameTimes = battleGameTimes
    elseif eliteBattleGameTimes > 0 then
        eliteBattleGameTimes = eliteBattleGameTimes - 1
        human.db.eliteBattleGameTimes = eliteBattleGameTimes
    elseif hardBattleGameTimes > 0 then
        hardBattleGameTimes = hardBattleGameTimes - 1
        human.db.hardBattleGameTimes = hardBattleGameTimes
    end

    BagLogic.addItemList(human, { {itemId, itemCnt} }, "battle_game")

    local msgRet = Msg.gc.GC_BATTLE_GETGAMEREWARD
    msgRet.battleGameTimes = battleGameTimes + eliteBattleGameTimes + hardBattleGameTimes
    Msg.send(msgRet, human.fd)
end


----------------------------肉鸽玩法---------------------------------
--查询所有闯关模式是否有未选择的属性
function QueryDiffBattleUnSelectAttr(human)
    local msgRet = Msg.gc.GC_ISNEEDSELECTATTR
    msgRet.noraml = 0
    msgRet.elite = 0
    msgRet.hard = 0

    local normalAttrData = getBattleAttrByType(human, EliteDefine.COPY_ELITE_NORMAL)
    if normalAttrData and normalAttrData.unSelectedAttrList then
        msgRet.noraml = 1
    end

    local eliteAttrData = getBattleAttrByType(human, EliteDefine.COPY_LEVEL_ELITE)
    if eliteAttrData and eliteAttrData.unSelectedAttrList then
        msgRet.elite = 1
    end

    local hardAttrData = getBattleAttrByType(human, EliteDefine.COPY_ELITE_DIFFICULTY)
    if hardAttrData and hardAttrData.unSelectedAttrList then
        msgRet.hard = 1
    end

    Msg.send(msgRet, human.fd)
end

--获取当前闯关模式可选择的加成属性信息
function GetNowBattleModeUnSelectAttr(human)
    local nBattleType = BattleLogic_GetBattleType(human)
    local attrAddData = getBattleAttrByType(human, nBattleType)

    if not attrAddData or not attrAddData.unSelectedAttrList then
        return Broadcast.sendErr(human, Lang.BATTLE_NO_ATTR)
    end

    local len = 0
    local itemCnt = 100

    local msgRet = Msg.gc.GC_GETCHOOSEATTR_INFO
    msgRet.attrInfo[0] = 0
    for _, id in ipairs(attrAddData.unSelectedAttrList) do
        len = len + 1
        local cfg = battleRougeCfg[id]
        msgRet.attrInfo[len].id = id
        msgRet.attrInfo[len].level = cfg.lv
        msgRet.attrInfo[len].icon = cfg.icon
        msgRet.attrInfo[len].name = cfg.name
        msgRet.attrInfo[len].quality = cfg.quality
        msgRet.attrInfo[len].attr.key = cfg.attrs[1]
        msgRet.attrInfo[len].attr.value = cfg.attrs[2]
    end
    msgRet.attrInfo[0] = len
    Grid.makeItem(msgRet.refreshCost, ItemDefine.ITEM_ZUANSHI_ID, itemCnt)
    Msg.send(msgRet, human.fd)
end

--下发当前闯关模式的所有属性加成信息
local function sendNowBattleModeAllAttr(human, chosenAttrList)
    local len = 0
    local max_len = 20

    local msgRet = Msg.gc.GC_GETALLATTR_INFO
    msgRet.attrInfo[0] = 0
    msgRet.isEnd = 0

    local num = 0
    local itemCnt = 100
    for _, _ in pairs(chosenAttrList) do
        num = num + 1
    end


    for id, attrInfo in pairs(chosenAttrList) do
        len = len + 1
        local cfg = battleRougeCfg[id]
        msgRet.attrInfo[len].id = id
        msgRet.attrInfo[len].level = attrInfo.lv
        msgRet.attrInfo[len].name = cfg.name
        msgRet.attrInfo[len].icon = cfg.icon
        msgRet.attrInfo[len].quality = cfg.quality
        msgRet.attrInfo[len].attr.key = cfg.attrs[1]
        local minAttrVal = getMinAttrValeByID(id)
        msgRet.attrInfo[len].attr.value = minAttrVal * attrInfo.lv

        if len >= max_len then
            msgRet.attrInfo[0] = len
            Grid.makeItem(msgRet.refreshCost, ItemDefine.ITEM_ZUANSHI_ID, itemCnt)

            num = num - max_len
            if num <= 0 then
                msgRet.isEnd = 1
                return Msg.send(msgRet, human.fd)
            end

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

    msgRet.isEnd = 1
    msgRet.attrInfo[0] = len
    Grid.makeItem(msgRet.refreshCost, ItemDefine.ITEM_ZUANSHI_ID, itemCnt)
    Msg.send(msgRet, human.fd)
end

-- 获取当前模式所有加成属性信息
function GetNowBattleModeAllAttr(human)
    local nBattleType = BattleLogic_GetBattleType(human)
    local attrAddData = getBattleAttrByType(human, nBattleType)

    if not attrAddData or not attrAddData.chosenAttrList then
        return
    end

    sendNowBattleModeAllAttr(human, attrAddData.chosenAttrList)
end


--选择属性
function ChooseAttr(human, idx)
    local nBattleType = BattleLogic_GetBattleType(human)
    local attrAddData = getBattleAttrByType(human, nBattleType)

    if not attrAddData or not attrAddData.unSelectedAttrList or not attrAddData.unSelectedAttrList[idx] then
        return Broadcast.sendErr(human, Lang.BATTLE_NO_ATTR)
    end

    local id = attrAddData.unSelectedAttrList[idx]
    local lv = battleRougeCfg[id].lv

    local targetId = 0
    local targetType = battleRougeCfg[id].type

    attrAddData.chosenAttrList = attrAddData.chosenAttrList or {}
    local chosenAttrList = attrAddData.chosenAttrList

    for attrId in pairs(chosenAttrList) do
        local cfg = battleRougeCfg[attrId]
        if cfg.type == targetType then
            targetId = attrId
            break
        end
    end

    if targetId ~= 0 then
        chosenAttrList[targetId].lv = chosenAttrList[targetId].lv + lv
        chosenAttrList[targetId].cfgNum = chosenAttrList[targetId].cfgNum + 1

        --等级大于等于4级时, 使用最高品质的ID
        if chosenAttrList[targetId].lv >= 4 then
            local newId = getMaxQualityId(targetId)

            if newId ~= targetId then
                chosenAttrList[newId] = {
                    lv = chosenAttrList[targetId].lv,
                    cfgNum = chosenAttrList[targetId].cfgNum
                }

                chosenAttrList[targetId] = nil
            end
        end
    else
        chosenAttrList[id] = {
            lv = lv,
            cfgNum = 1
        }
    end

    attrAddData.unSelectedAttrList = nil

    sendNowBattleModeAllAttr(human, attrAddData.chosenAttrList)

    QueryDiffBattleUnSelectAttr(human)
end

--刷新属性
function RefreshAttr(human, type_m, targetId)
    local nBattleType = BattleLogic_GetBattleType(human)
    local attrAddData = getBattleAttrByType(human, nBattleType)

    if not attrAddData then
        return Broadcast.sendErr(human, Lang.BATTLE_NO_ATTR)
    end

    local itemCnt = 100
    if type_m == 1 then --刷新未选择的3个属性
        if not attrAddData.unSelectedAttrList then
            return Broadcast.sendErr(human, Lang.BATTLE_NO_ATTR)
        end

        if BagLogic.getItemCnt(human, ItemDefine.ITEM_ZUANSHI_ID) < itemCnt then
            return Broadcast.sendErr(human, Lang.COMMON_NO_ZUANSHI)
        end
        BagLogic.delItem(human, ItemDefine.ITEM_ZUANSHI_ID, itemCnt, "battle")


        local unSelectedAttrList = attrAddData.unSelectedAttrList
        Util.initTable(unSelectedAttrList)
        local totalWeight, randList = generateCfgWeightInfo()

        if totalWeight == 0 then
            return Broadcast.sendErr(human, Lang.BATTLE_GET_ALL_ATTR)
        end

        for i=1, 3 do
            local id = getIndexByRand(totalWeight, randList)
            unSelectedAttrList[i] = id
        end

        GetNowBattleModeUnSelectAttr(human)

    elseif type_m == 2 then --刷新已选择的某个属性
        if not attrAddData.chosenAttrList or not attrAddData.chosenAttrList[targetId] then
            return Broadcast.sendErr(human, Lang.BATTLE_NO_ATTR)
        end

        local chosenAttrList = attrAddData.chosenAttrList
        local excludeTypeList = getAttrTypeList(chosenAttrList)
        local nowType = battleRougeCfg[targetId].type
        excludeTypeList[nowType] = nil


        local totalWeight, randList = generateCfgWeightInfo(excludeTypeList)
        if totalWeight == 0 then
            return Broadcast.sendErr(human, Lang.BATTLE_GET_ALL_ATTR)
        end


        if BagLogic.getItemCnt(human, ItemDefine.ITEM_ZUANSHI_ID) < itemCnt then
            return Broadcast.sendErr(human, Lang.COMMON_NO_ZUANSHI)
        end
        BagLogic.delItem(human, ItemDefine.ITEM_ZUANSHI_ID, itemCnt, "battle")

        --随机
        local oldAttrData = chosenAttrList[targetId]

        local newId, newAttrInfo = getIndexByRand2(totalWeight, randList, oldAttrData.cfgNum)
        chosenAttrList[targetId] = nil
        chosenAttrList[newId] = newAttrInfo

        local cfg = battleRougeCfg[newId]
        local msgRet = Msg.gc.GC_REFRESH_ATTR
        msgRet.oldId = targetId
        msgRet.newAttrData.name = cfg.name
        msgRet.newAttrData.icon = cfg.icon
        msgRet.newAttrData.id = newId
        msgRet.newAttrData.quality = cfg.quality
        msgRet.newAttrData.level = newAttrInfo.lv

        msgRet.newAttrData.attr.key = cfg.attrs[1]
        local minAttrVal = getMinAttrValeByID(newId)
        msgRet.newAttrData.attr.value = minAttrVal * newAttrInfo.lv

        Msg.send(msgRet, human.fd)

    end
end

--GM, 清理当前闯关模式的加成属性数据
function GM_ClearNowModelAttrData(human)
    -- local battleType = BattleLogic_GetBattleType(human)

    -- if EliteDefine.COPY_ELITE_NORMAL == battleType then
    --     human.db.normalBattleAttrData = nil
    -- elseif EliteDefine.COPY_LEVEL_ELITE == battleType then
    --     human.db.eliteBattleAttrData = nil
    -- elseif EliteDefine.COPY_ELITE_DIFFICULTY == battleType then
    --     human.db.hardBattleAttrData = nil
    -- end

    human.db.normalBattleAttrData = nil
    human.db.eliteBattleAttrData = nil
    human.db.hardBattleAttrData = nil
end



-----------------------------多队伍--------------------------------------------------------
-- 请求当前模式当前关卡需要几支队伍
function Battle_LevelTeamCnt_Query(human)
    local teamCnt = getLevelTeamCnt(human)
    local msgRet = Msg.gc.GC_BATTLE_TEAM_COUNT
    msgRet.teamCnt = teamCnt

    Msg.send(msgRet, human.fd)
end

-------------------------------------------------------------------------------------------

-- 每日通关信息查询
function Battle_DailyPassLevelInfo_Query(human)
    local msgRet = Msg.gc.GC_BATTLE_DAILY_PASSLVEL_INFO_QUERY
    msgRet.currentPassLevelNum = getDailyPassLevelNum(human)
    msgRet.maxPassLevelNum = EliteDefine.BATTLE_DAILY_MAX_PASSLEVEL_NUM

    Msg.send(msgRet, human.fd)
end