--------------------------------
-- 文件名       ：  MiddleCommonLogic
-- 文件说明     ：  跨服相关-通用管理(用于处理比较杂项的数据)
-- 创建时间     :   2025/02/26
-- 创建人       :   FC
--------------------------------
local Config = require("Config")
local Log = require("common.Log")
local CommonDB = require("common.CommonDB")
local InnerMsg = require("core.InnerMsg")
local RoleLogic = require("role.RoleLogic")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local CombatDefine = require("combat.CombatDefine")
local Util = require("common.Util")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local CombatLogic = require("combat.CombatLogic")
local CombatPosLogic = require("combat.CombatPosLogic")
local MiddleDefine = require("middle.MiddleDefine")
local YunYingLogic = require("yunying.YunYingLogic")
local TriggerDefine = require("trigger.TriggerDefine")
local TriggerLogic = require("trigger.TriggerLogic")
local CommonDefine = require("common.CommonDefine")
local JjcNewLadderLogic = require("jjcnewladder.jjcNewLadderLogic")

local MIDDLE_FRIEND_FIGHT_MAP = 1001

---- 战斗相关缓存
local MIDDLECOMMONCOMBAT = 
{
    --[[
        -- 请求者UID
        [nSrcUID] = 
            {
                nDesUID,        -- 请求对战角色UID
                nCombatType,    -- 战斗类型
                nTime,          -- 请求的时间
            }
    ]]
}

-- 内部写日志说明
local function MiddleCommonLogic_WRITELOG(szText)
    Log.write(Log.LOGID_OSS_COMMON, "跨服通用管理"..szText)
end

-- 是否可以进行战斗
local function MiddleCommonLogic_CanCombatType(nCombatType)
    if not MiddleDefine.MIDDLE_CAN_COMBAT[nCombatType] then
        return false
    end

    return true
end

-- 是否有缓存
local function MiddleCommonLogic_IsCache(nSrcUID)
    return MIDDLECOMMONCOMBAT[nSrcUID]
end

-- 清理战斗缓存
local function MiddleCommonLogic_ClearCache(nSrcUID)
    MIDDLECOMMONCOMBAT[nSrcUID] = nil
end

-- 设置战斗缓存
local function MiddleCommonLogic_SetCache(nSrcUID, nDesUID, nCombatType)
    MIDDLECOMMONCOMBAT[nSrcUID] = 
    {
        nDesUID = nDesUID,
        nCombatType = nCombatType,
        nTime = os.time()
    }
end

-- 弹失败提示
local function MiddleCommonLogic_PopTips(human, nResult)
    if MiddleDefine.MIDDLE_GET_COMBAT_ERR_ONE == nResult then
        Broadcast.sendDown(human, Lang.MIDDLE_COMBAT_NOHUMAN)
    elseif MiddleDefine.MIDDLE_GET_COMBAT_ERR_TWO == nResult then
        Broadcast.sendDown(human, Lang.MIDDLE_COMBAT_NOFANSHOU)
    elseif MiddleDefine.MIDDLE_GET_COMBAT_ERR_THREE == nResult then
        Broadcast.sendDown(human, Lang.MIDDLE_COMBAT_NOHUMAN)
    elseif MiddleDefine.MIDDLE_GET_COMBAT_ERR_FOUR == nResult then
        Broadcast.sendDown(human, Lang.MIDDLE_COMBAT_NOHUMAN)
    end
end

---------------------------- 点击玩家头像处理开始 ------------------------
---
-- 跨服获取玩家信息(源->中心服)
function MiddleCommonLogic_GetChatPlayInfo_LW(human, msg)
    local tMsgData = InnerMsg.lw.LW_CHAT_PLAYER_INFO
    tMsgData.nSrcUID = human.db._id
    tMsgData.nDesUID = msg.uuid
    tMsgData.nSrcServerID = Config.SVR_INDEX
    tMsgData.nDesServerID = msg.nServerIndex
    tMsgData.nFrom = CommonDefine.COMMON_PLAY_INFO_CHAT
    tMsgData.nType = 0
    if msg.nFrom then
        tMsgData.nFrom = msg.nFrom
    end

    if msg.nType then
        tMsgData.nType = msg.nType
    end

    print("[MiddleCommonLogic_GetChatPlayInfo_LW] ", tMsgData.nSrcUID, tMsgData.nDesUID, tMsgData.nSrcServerID, tMsgData.nDesServerID)

    InnerMsg.sendMsg(0, tMsgData)
end

-- 跨服获取玩家信息(中心服->目标)
function MiddleCommonLogic_GetChatPlayInfo_WL(fd, msg)
    local msgRet = Msg.gc.GC_CHAT_PLAYER_INFO
    local nCombatType = CombatDefine.COMBAT_TYPE1
    if msg.nFrom == CommonDefine.COMMON_PLAY_INFO_JJC then
        nCombatType = CombatDefine.COMBAT_TYPE16
    end

    print("[MiddleCommonLogic_GetChatPlayInfo_WL] 获取到的参数\n")
    table.print_lua_table(msg)

    if not RoleLogic.makePlayInfo(msgRet.data, msg.nDesUID, nCombatType) then
        MiddleCommonLogic_WRITELOG("跨服获取玩家数据失败 nDesUID = "..msg.nDesUID.." nSrcUID = "..msg.nSrcUID.." nSrcServerID = "..msg.nSrcServerID)
        return
    end

    -- 获取数据发回中心服
    local tMsgData = InnerMsg.lw.LW_CHAT_PLAYER_INFO_SEND
    tMsgData.nSrcUID = msg.nSrcUID
    tMsgData.nSrcServerID = msg.nSrcServerID
    tMsgData.tData = msgRet
    tMsgData.nFrom = msg.nFrom
    tMsgData.nType = msg.nType

    InnerMsg.sendMsg(0, tMsgData)
    print("[MiddleCommonLogic_GetChatPlayInfo_WL] 跨服获取玩家信息(中心服->目标)")
end

-- 获取到数据发送给请求的玩家
function MiddleCommonLogic_SendChatPlayInfo_WL(fd, msg)
    local nSrcUID = msg.nSrcUID
    local human = ObjHuman.onlineUuid[nSrcUID]
    if not human then
        print("[MiddleCommonLogic_SendChatPlayInfo_WL] 玩家不在线直接返回")
        return
    end

    if CommonDefine.COMMON_PLAY_INFO_CHAT == msg.nFrom then
        local msgRet = Msg.gc.GC_CHAT_PLAYER_INFO
        msgRet.data = msg.tData.data
        Msg.send(msgRet, human.fd)
        print("[MiddleCommonLogic_SendChatPlayInfo_WL] 获取到数据发送给请求的玩家")
    elseif CommonDefine.COMMON_PLAY_INFO_JJC == msg.nFrom then
        JjcNewLadderLogic.JjcNewLadderLogic_GetPlayerInfo(msg)
    else
        print("[MiddleCommonLogic_SendChatPlayInfo_WL] 未处理的类型")
    end
end

---------------------------- 点击玩家头像处理结束 ------------------------

---------------------------- 战斗相关开始 --------------------------------
function MiddleCommonLogic_CombatBegin_LW(human, msg)
    if false == MiddleCommonLogic_CanCombatType(msg.combatType) then
        print("[MiddleCommonLogic_CombatBegin] 不用处理的对战类型直接返回 combatType = "..msg.combatType)
        return
    end

    local tCombatCache = MiddleCommonLogic_IsCache(human)
    if tCombatCache  then
        local nNowTime = os.time()
        if tCombatCache.nTime + MiddleDefine.MIDDLE_GET_COMBAT_TIME <= nNowTime then
            MiddleCommonLogic_ClearCache(human.db._id)
        else
            return Broadcast.sendDown(human, Lang.MIDDLE_COMBAT_WAIT)
        end
    end

    local args = Util.split(msg.param, "|")
    MiddleCommonLogic_SetCache(human.db._id, args[1], msg.combatType)

    local tMsgData = InnerMsg.lw.LW_COMBAT_GETINFO
    tMsgData.nSrcUID = human.db._id
    tMsgData.nDesUID = args[1]
    tMsgData.nSrcServerID = Config.SVR_INDEX
    tMsgData.nDesServerID = msg.nServerIndex
    tMsgData.nCombatType = msg.combatType
    tMsgData.extraArgs = msg.extraArgs or {}

    InnerMsg.sendMsg(0, tMsgData)

    print("[MiddleCommonLogic_CombatBegin_LW] 发送给中心服 = ")
end



local sp_combatType_list = {
    [CombatDefine.COMBAT_TYPE35] = true,
    [CombatDefine.COMBAT_TYPE37] = true,
    [CombatDefine.COMBAT_TYPE39] = true,
    [CombatDefine.COMBAT_TYPE40] = true,
    [CombatDefine.COMBAT_TYPE41] = true,
    [CombatDefine.COMBAT_TYPE42] = true,
    [CombatDefine.COMBAT_TYPE43] = true,
}

-- msg = WL_COMBAT_GETINFO
function MiddleCommonLogic_CombatBegin_WL(fd, msg)
    local tMsgData = InnerMsg.lw.LW_COMBAT_GETINFO_SEND
    tMsgData.nSrcUID = msg.nSrcUID
    tMsgData.nSrcServerID = msg.nSrcServerID
    local nCombatType = msg.nCombatType or CombatDefine.COMBAT_TYPE1

    local nDesUID = msg.nDesUID
    local fakeHuman = CombatLogic.createCombatFakeHuman(nDesUID)
	if not fakeHuman then 
        tMsgData.nResult = MiddleDefine.MIDDLE_GET_COMBAT_ERR_ONE
        print("[MiddleCommonLogic_CombatBegin_WL] 没有获取到对应玩家")
        InnerMsg.sendMsg(0, tMsgData)
        return
     end

	-- local combatHero = CombatPosLogic.getCombatHeros(fakeHuman, CombatDefine.COMBAT_TYPE1, nil, true)

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


    local moduleFn = CombatLogic.getModule(msg.nCombatType)
    if not moduleFn or not moduleFn.getCombatObjList then
        print("[MiddleCommonLogic_CombatBegin_WL] 未实现的获取战斗对象函数")
        tMsgData.nResult = MiddleDefine.MIDDLE_GET_COMBAT_ERR_THREE
        MiddleCommonLogic_WRITELOG("[MiddleCommonLogic_CombatBegin_WL] 未实现的获取战斗对象函数 nCombatType = "..msg.nCombatType)
        InnerMsg.sendMsg(0, tMsgData)
        return
    else
        local args  = {
            [1] = nDesUID
        }
        local objList, helpList, rolebase, formation,jiban, elfList = moduleFn.getCombatObjList(nil, CombatDefine.DEFEND_SIDE, args, msg.nCombatType, msg.extraArgs)

        if not objList and msg.nCombatType == CombatDefine.COMBAT_TYPE35 then
            if moduleFn.getCombatMonsterOutID then
                args[2] = msg.extraArgs.cityId
                local monsterOutId = moduleFn.getCombatMonsterOutID(nil, CombatDefine.DEFEND_SIDE, args)

                if monsterOutId then
                    objList, helpList, rolebase, formation, jiban, elfList = CombatLogic.getMonsterObjList(monsterOutId)
                end
            end

        end

        if objList then
            tMsgData.nResult = MiddleDefine.MIDDLE_GET_COMBAT_OK
            tMsgData.tObjList = objList
            tMsgData.tHelpList = helpList
            tMsgData.tRoleBase = rolebase
            tMsgData.formation = formation
            tMsgData.tJiBan = jiban
            tMsgData.tElfList = elfList or {}
            InnerMsg.sendMsg(0, tMsgData)
        else
            print("[MiddleCommonLogic_CombatBegin_WL] 获取对战英雄列表失败")
            MiddleCommonLogic_WRITELOG("[MiddleCommonLogic_CombatBegin_WL] 未实现的获取战斗对象列表失败 nCombatType = "..msg.nCombatType)
            tMsgData.nResult = MiddleDefine.MIDDLE_GET_COMBAT_ERR_FOUR
            InnerMsg.sendMsg(0, tMsgData)
            return
        end
    end
end

function MiddleCommonLogic_CombatBegin_SendWL(fd, msg)
    local nSrcUID = msg.nSrcUID
    local human = ObjHuman.onlineUuid[nSrcUID]
    if not human then
        print("[MiddleCommonLogic_CombatBegin_SendWL] 玩家不在线直接返回")
        return
    end

    print("[MiddleCommonLogic_CombatBegin_SendWL] 收到信息进行打印")
    local tCombatCache = MiddleCommonLogic_IsCache(nSrcUID)
    if not tCombatCache then
        print("[MiddleCommonLogic_CombatBegin_SendWL] 不存在对应的战斗缓存信息")
        return
    else
        local nNowTime = os.time()
        -- 已经超时
        if tCombatCache.nTime + MiddleDefine.MIDDLE_GET_COMBAT_TIME <= nNowTime then
            print("[MiddleCommonLogic_CombatBegin_SendWL] 已经超时")
            MiddleCommonLogic_ClearCache(nSrcUID)
            return
        end
    end

    local nResult = msg.nResult
    if MiddleDefine.MIDDLE_GET_COMBAT_OK ~= nResult then
        print("[MiddleCommonLogic_CombatBegin_SendWL] 获取的数据不正确,弹Tips并返回")
        MiddleCommonLogic_PopTips(human, nResult)
        return
    end

    local args =  {
        defender = msg.tObjList,
        defHelp = msg.tHelpList,
        defRBase = msg.tRoleBase,
        defFormation = msg.formation,
        defJiban = msg.tJiBan,
        defElfList = msg.tElfList,
    }

    YunYingLogic.onCallBack(human, "friendCombat", 1)
    TriggerLogic.PublishEvent(TriggerDefine.FRIEND_COMBAT, human.db._id, 1)

    print("[MiddleCommonLogic_CombatBegin_SendWL] 开始进行战斗")

    CombatLogic.combatBegin(human, MIDDLE_FRIEND_FIGHT_MAP, args, tCombatCache.nCombatType, tCombatCache.nDesUID)

    MiddleCommonLogic_ClearCache(nSrcUID)

    print("[MiddleCommonLogic_CombatBegin_SendWL] 战斗结束清理数据")
end
---------------------------- 战斗相关结束 --------------------------------

