-- 用于普通服收到跨服通知上传战斗数据的处理

local InnerMsg = require("core.InnerMsg")
local CombatDefine = require("combat.CombatDefine")
local CombatLogic = require("combat.CombatLogic")
local MiddleDefine = require("middle.MiddleDefine")
local CombatPosLogic = require("combat.CombatPosLogic")




function CreateBattleData(fd, msg)
    local tMsgData = InnerMsg.lw.LW_COMBAT_DATA
    tMsgData.moduleTag = msg.moduleTag
    tMsgData.extraArgs = msg.extraArgs

    local playerUuid = msg.playerUuid
    local nCombatType = msg.nCombatType or CombatDefine.COMBAT_TYPE1
    local fakeHuman = CombatLogic.createCombatFakeHuman(playerUuid)

	if not fakeHuman then
        tMsgData.errCode = MiddleDefine.MIDDLE_GET_COMBAT_ERR_ONE
        print("[BattleDataNS] 没有获取到对应玩家")
        InnerMsg.sendMsg(0, tMsgData)
        return
    end

	-- local combatHero = CombatPosLogic.getCombatHeros(fakeHuman, CombatDefine.COMBAT_TYPE1, nil, true)
    local combatHero = CombatPosLogic.getCombatHeros(fakeHuman, nCombatType, nil, true)
	if not combatHero or not next(combatHero) then
        print("[BattleDataNS] 没有获取到对应玩家的英雄列表, 取默认战斗列表")
        combatHero = CombatPosLogic.getCombatHeros(fakeHuman, CombatDefine.COMBAT_TYPE1, nil, true)
        if not combatHero or not next(combatHero) then
            print("[BattleDataNS] 取默认战斗列表还是没有获取到英雄列表")
            tMsgData.errCode = MiddleDefine.MIDDLE_GET_COMBAT_ERR_TWO
            InnerMsg.sendMsg(0, tMsgData)
            return
        end
	end

    local moduleFn = CombatLogic.getModule(msg.combatType)
    if not moduleFn or not moduleFn.getCombatObjList then
        print("[BattleDataNS] 未实现的获取战斗对象函数")
        tMsgData.errCode = MiddleDefine.MIDDLE_GET_COMBAT_ERR_THREE
        InnerMsg.sendMsg(0, tMsgData)
        return
    else
        local args  = {
            [1] = playerUuid
        }
        local objList, helpList, rolebase, formation,jiban, elfList = moduleFn.getCombatObjList(nil, CombatDefine.DEFEND_SIDE, args, msg.combatType)
        if objList then
            tMsgData.errCode = MiddleDefine.MIDDLE_GET_COMBAT_OK
            tMsgData.objList = objList
            tMsgData.helpList = helpList
            tMsgData.roleBase = rolebase
            tMsgData.formation = formation
            tMsgData.jiBan = jiban
            tMsgData.elfList = elfList or {}
            InnerMsg.sendMsg(0, tMsgData)
        else
            print("[BattleDataNS] 获取对战英雄列表失败")
            tMsgData.errCode = MiddleDefine.MIDDLE_GET_COMBAT_ERR_FOUR
            InnerMsg.sendMsg(0, tMsgData)
            return
        end
    end
end