-- 跨服鏈接和重连管理
local msg_parse = _G.msg_inner_parse
local Config = require("Config")
local Log = require("common.Log")
local CommonDB = require("common.CommonDB")
local InnerMsg = require("core.InnerMsg")
local MiddleManager = require("middle.MiddleManager")
local MiddleHeartBeat = require("middle.MiddleHeartBeat")
local Define = require("platform.Define")
local Json = require("common.Json")
local JjcLadderMiddle = require("jjcLadder.JjcLadderMiddle")
local WarZoneConf = require("excel.WarZone")    --- 战区配置
local ChatHandler = require("chat.Handler")
local MiddleCommonRank = require("middle.MiddleCommonRank")
local BanLogic = require("scene.BanLogic")

IS_MIDDLE_CONNECT = IS_MIDDLE_CONNECT or nil -- 是否連上跨服 收到WLhello才認為連上了

local nServerOffSet = 810538  -- 配置中服务器ID偏移量
YY_ACT_FLAG = nil

-- 请求middleInfo
local s2aParam = {}
local function questMiddleInfo()
    s2aParam.sid = Config.SVR_INDEX
	s2aParam.api_cbMethod = "setMiddleInfo"
    _G.thread_http.send(Define.MIDDLE_INFO_URL, Json.Encode(s2aParam))
end

-- 获取战区最小和最大服务器ID
function MiddleConnect_GetWarZoneServer(nSeverID)
    local tWarZoneConf = WarZoneConf.group
    if not tWarZoneConf then
        return 0, 0
    end

    for _, v in pairs(tWarZoneConf) do
        if v.nMinServerID <= nSeverID and v.nMaxServerID >= nSeverID then
            return v.nMinServerID, v.nMaxServerID
        end
    end

    return 0, 0
end

-- 获取战区服务器ID
function MiddleConnect_ConfServerID2TrueServerID(nServerIDConf)
    return nServerIDConf + nServerOffSet - 1
end

-- 获取战区配置服务器ID
function MiddleConnect_TrueServerID2ConfServerID(nServerID)
    return nServerID - nServerOffSet + 1
end

local function MiddleConnect_GetWarFirstServerFD(nSrcServerID)
    local nConfigServerID = MiddleConnect_TrueServerID2ConfServerID(nSrcServerID)
    local nWarServerID = MiddleConnect_GetWarZoneServer(nConfigServerID)

    if 0 >= nWarServerID then
        print("[MiddleConnect_GetWarFirstServerFD] 获取不到对应的战区ID nSrcServerID = "..nSrcServerID
        .." nConfigServerID = "..nConfigServerID.." nWarServerID = "..nWarServerID)
        return
    end

    -- 转换成战区真正的ID
    nWarServerID = MiddleConnect_ConfServerID2TrueServerID(nWarServerID)

    local nWarFD = MiddleManager.getFDBySvrIndex(nWarServerID)
    if not nWarFD then
        print("[MiddleConnect_GetWarFirstServerFD] 获取不到对应的战区的FD nSrcServerID = "..nSrcServerID
        .." nConfigServerID = "..nConfigServerID.." nWarServerID = "..nWarServerID)
        return
    end

    return nWarFD
end

function setMiddleInfo(ip, port, host)
	if _G.is_middle == true then return end

    local middleDBInfo = CommonDB.getMiddleInfo()
	if middleDBInfo.ip == ip and middleDBInfo.port == port and middleDBInfo.host == host then
		-- 沒變化
		return
	end

    local oldIp = middleDBInfo.ip
    local oldPort = middleDBInfo.port

    CommonDB.setMiddleInfo(ip, port, host)
	if oldIp == ip and oldPort == port then
    	-- 只有host变了 不需要重連
    	return
    else
        tryConnectMiddle(ip, port) -- try的時候會把之前的鏈接斷開
   	end
end

function getMiddleInfo()
	--调试模式使用配置的IP
    if Config.IS_DEBUG then
        return Config.MIDDLE_IP, Config.MIDDLE_PORT, Config.MIDDLE_HOST
    end

    local middleDBInfo = CommonDB.getMiddleInfo()
    return middleDBInfo.ip, middleDBInfo.port, middleDBInfo.host
end

function initAfterStart()
    onTimer() -- 啟動拉取一下middleInfo
    MiddleCommonRank.MiddleCommonRank_InitAfterStart()

    local ServerCommerceActBattleGroundCS = require("serverCommerce.ServerCommerceActBattleGroundCS")
    ServerCommerceActBattleGroundCS.BattleGround_InitAfterStart()
end

function onTimer()
	if _G.is_middle == true then return end

	-- 如果没有middleip 去找后台请求middleip 1分钟1次
	local ip, port = getMiddleInfo()
	if ip == nil then
		-- reyes todo get ip
        print("no middleInfo try to get")
        questMiddleInfo()
		return
	end

	-- 如果ip有了 但是一直没连上 1分钟重连1次
    if isConnect() ~= true then
        tryConnectMiddle(ip, port)
		return
	end

	-- 如果3分钟之内 没有收到middle的心跳包 并且底层还是连接中的状态 我们认为出故障了 这个时候重连一下middle 这是一个异常处理
    if MiddleHeartBeat.LAST_HEARTBEAT_TIME then
	    local now = os.time()
        if now - MiddleHeartBeat.LAST_HEARTBEAT_TIME > 180 then
            stopConnectMiddle()
        end
    end
end

function autoResetMiddle()
    if _G.is_middle == true then return end

    -- 每周一凌晨2點 斷開鏈接 并且重新取一下php的跨服信息 重新連新跨服
    CommonDB.setMiddleInfo(nil, nil, nil)

    stopConnectMiddle()

    onTimer() -- 拉取一下middleInfo
end


function isConnect()
    -- 這個接口和IS_MIDDLE_CONNECT的區別是 這個表示底層鏈接連上了 但是不一定正確收到了middle會包 lua層業務判斷最好別用這個接口 盡量用IS_MIDDLE_CONNECT
    local ret = msg_parse.check_connect()
    if ret == true then
        return true
    end
end

function tryConnectMiddle(ip, port)
    print("try connect middle ", ip, port)
    Log.write(Log.LOGID_INNER_CLOSE, "tryConnectMiddle", ip, port)
    msg_parse.set_connect(ip, port)
end

function stopConnectMiddle()
    print("stop connect middle ")
    Log.write(Log.LOGID_INNER_CLOSE, "stopConnectMiddle")
    msg_parse.close_connect()

    IS_MIDDLE_CONNECT = nil
    MiddleHeartBeat.LAST_HEARTBEAT_TIME = nil
    MiddleHeartBeat.LAST_SEND_HB_TS = nil
end

function LW_HELLO(fd, msg)
    print("LW Hello fd = "..fd)
    --table.print_lua_table(msg)
    --local stackTrace = debug.traceback("", 2)
	--print("[LW_HELLO] 堆栈信息 "..stackTrace)
    -- Log.write(Log.LOGID_INNER_CLOSE, "LW_HELLO")

    Log.write(Log.LOGID_INNER_CLOSE, string.format("LW_HELLO, client: %s, fd: %s",  msg.svrIndex, fd))

    MiddleManager.addLogicServer(fd, msg.svrIndex)

    local szMsgData = InnerMsg.wl.WL_HELLO
    --table.print_lua_table(szMsgData)
    szMsgData.nGetSvrID = msg.svrIndex
    szMsgData.nNowSvrID = Config.SVR_INDEX
    --table.print_lua_table(msg)
    --table.print_lua_table(szMsgData)
    InnerMsg.sendMsg(fd, szMsgData)

    msg = InnerMsg.wl.WL_HEARTBEAT
    InnerMsg.sendMsg(fd, msg)
    -- 同步其它信息
    -- JjcLadderMiddle.sendWLMobaiCnt(fd)
end

function WL_HELLO(fd, msg)
    print("WL hello fd = "..fd)
    Log.write(Log.LOGID_INNER_CLOSE, "WL_HELLO")

    if not IS_MIDDLE_CONNECT then
        print("middle server upup!!!---")
        IS_MIDDLE_CONNECT = true

        local openTime = CommonDB.getServerOpenTime()
        if openTime ~= 0 then
            -- local msg = InnerMsg.lw.LW_SET
            -- msg.openTime = openTime or 0
            -- msg.svrIndex = Config.SVR_INDEX
            -- InnerMsg.sendMsg(fd, msg)
        end

        BanLogic.NS_Get_BanData()
    end
end

function LW_SET(fd,msg)
    MiddleManager.setServerOpenDay(fd,msg.svrIndex,msg.openTime)
end

function LW_DISCONNECT(fd, msg)  
    print("LW_DISCONNECT")
  
    MiddleManager.delLogicServer(fd) 
end

--middle断线，C++层触发
function WL_DISCONNECT(fd, msg)
    if IS_MIDDLE_CONNECT then
        IS_MIDDLE_CONNECT = nil
        MiddleHeartBeat.LAST_HEARTBEAT_TIME = nil
        MiddleHeartBeat.LAST_SEND_HB_TS = nil

        Log.write(Log.LOGID_INNER_CLOSE, "middle server disconnect!!!")
        print("middle server disconnect!!!---")
    end
end

-- 跨服聊天请求
function LW_MIDDLE_CHAT(fd, msg)
    local tAllConnectFD = MiddleManager.MiddleManager_GetAllFD()
    if not _G.next(tAllConnectFD) then
        print("[LW_MIDDLE_CHAT] 不存在连接上的服务器")
        return
    end

    local szMsgData = InnerMsg.wl.WL_MIDDLE_CHAT
    szMsgData.tChatMsg = msg.tChatMsg
    
    local nMsgType = szMsgData.tChatMsg.item.msgType
    if ChatHandler.CHAT_TYPE_MIDDLE ==  nMsgType then
        for _, nFD in pairs(tAllConnectFD) do
            InnerMsg.sendMsg(nFD, szMsgData)
        end
        
        print("[LW_MIDDLE_CHAT] 全服发送消息结束")
    elseif ChatHandler.CHAT_TYPE_WARZONE ==  nMsgType then
        local nSendServerID = msg.svrIndex
        local nConfServerID = nSendServerID - nServerOffSet + 1
        local nMinServerID, nMaxServerID = MiddleConnect_GetWarZoneServer(nConfServerID)
        if 0 >= nMinServerID or 0 >= nMaxServerID then
            print("[LW_MIDDLE_CHAT 获取不到配置 nSendServerID = "..nSendServerID.." nConfServerID = "..nConfServerID)
            return
        end

        local nServeL, nServerR = nMinServerID + nServerOffSet - 1, nMaxServerID + nServerOffSet - 1
        for i = nServeL, nServerR, 1 do
            local nFD = MiddleManager.getFDBySvrIndex(i)
            if nFD then
                InnerMsg.sendMsg(nFD, szMsgData)
            else
                print("[LW_MIDDLE_CHAT] 不存在对应的fd i = "..i)
            end
        end

        -- print("[LW_MIDDLE_CHAT] 战区发送消息结束")
    else
        print("[LW_MIDDLE_CHAT] 未处理的发送消息结束")
    end

end

-- 获取聊天英雄信息
function LW_MIDDLE_CHAT_QUERY_HERO_DATA(fd, msg)
    local nDesServerID = msg.nDesServerID
    local nDesFD = MiddleManager.getFDBySvrIndex(nDesServerID)
    if not nDesFD then
        print("[LW_MIDDLE_CHAT_QUERY_HERO_DATA] 不存在对应的fd nDesServerID = "..nDesServerID)
        return
    end
    print("[LW_MIDDLE_CHAT_QUERY_HERO_DATA] nDesServerID = "..nDesServerID.." nDesFD = "..nDesFD)

    local szMsgData = InnerMsg.wl.WL_MIDDLE_CHAT_QUERY_HERO_DATA
    szMsgData.nSrcServerID = msg.nSrcServerID
    szMsgData.nSrcUID = msg.nSrcUID
    szMsgData.nDesUID = msg.nDesUID
    szMsgData.nHeroIndex = msg.nHeroIndex
    szMsgData.nChatType = msg.nChatType

    InnerMsg.sendMsg(nDesFD, szMsgData)
end

function LW_MIDDLE_CHAT_GET_HERO_DATA(fd, msg)
    local nSrcServerID = msg.nSrcServerID
    local nSrcFD =  MiddleManager.getFDBySvrIndex(nSrcServerID)
    if not nSrcFD then
        print("[LW_MIDDLE_CHAT_GET_HERO_DATA] 不存在对应的fd nSrcServerID = "..nSrcServerID)
        return
    end

    print("[LW_MIDDLE_CHAT_GET_HERO_DATA] nSrcServerID = "..nSrcServerID.." nSrcFD = "..nSrcFD)

    local szMsgData = InnerMsg.wl.WL_MIDDLE_CHAT_GET_HERO_DATA
    szMsgData.nSrcUID = msg.nSrcUID
    szMsgData.tHeroData = msg.tHeroData

    InnerMsg.sendMsg(nSrcFD, szMsgData)
end

function LW_WARREPORT_GET_COMBATINFO(fd, msg)
    local nDesServerID = msg.nDesServerID
    local nDesFD = MiddleManager.getFDBySvrIndex(nDesServerID)
    --local nSrcFD = MiddleManager.getFDBySvrIndex(msg.nSrcServerID)
    if not nDesFD then
        print("[LW_WARREPORT_GET_COMBATINFO] 不存在对应的fd nDesServerID = "..nDesServerID.." nSrcServerID = "..msg.nSrcServerID)
        return
    end

    local tMsgData = InnerMsg.wl.WL_WARREPORT_GET_COMBATINFO
    tMsgData.nSrcUID = msg.nSrcUID
    tMsgData.type = msg.type
    tMsgData.id = msg.id
    tMsgData.nSrcServerID = msg.nSrcServerID
    tMsgData.mode = msg.mode

    InnerMsg.sendMsg(nDesFD, tMsgData)
end

function LW_WARREPORT_SEND_COMBATINFO(fd, msg)
    local nSrcServerID = msg.nSrcServerID
    local nSrcFD =  MiddleManager.getFDBySvrIndex(nSrcServerID)
    if not nSrcFD then
        print("[LW_MIDDLE_CHAT_GET_HERO_DATA] 不存在对应的fd nSrcServerID = "..nSrcServerID)
        return
    end

    local tMsgData = InnerMsg.wl.WL_WARREPORT_SEND_COMBATINFO
    tMsgData.nSrcUID = msg.nSrcUID
    tMsgData.combatInfo = msg.combatInfo
    tMsgData.mode = msg.mode

    InnerMsg.sendMsg(nSrcFD, tMsgData)
end

-------------------- 跨服玩家头像数据开始 --------------------
function LW_CHAT_PLAYER_INFO(fd, msg)
    local nDesFD = MiddleManager.getFDBySvrIndex(msg.nDesServerID)
    if not nDesFD then
        print("[LW_CHAT_PLAYER_INFO] 不存在对应的fd nDesServerID = "..msg.nDesServerID.." nSrcServerID = "..msg.nSrcServerID)
        return
    end

    local tMsgData = InnerMsg.wl.WL_CHAT_PLAYER_INFO
    tMsgData.nSrcUID = msg.nSrcUID
    tMsgData.nDesUID = msg.nDesUID
    tMsgData.nSrcServerID = msg.nSrcServerID
    tMsgData.nFrom = msg.nFrom
    tMsgData.nType = msg.nType
    
    InnerMsg.sendMsg(nDesFD, tMsgData)
end

function LW_CHAT_PLAYER_INFO_SEND(fd, msg)
    local nSrcFD =  MiddleManager.getFDBySvrIndex(msg.nSrcServerID)
    if not nSrcFD then
        print("[LW_CHAT_PLAYER_INFO_SEND] 不存在对应的fd nSrcServerID = "..msg.nSrcServerID)
        return
    end

    local tMsgData = InnerMsg.wl.WL_CHAT_PLAYER_INFO_SEND
    tMsgData.nSrcUID = msg.nSrcUID
    tMsgData.tData = msg.tData
    tMsgData.nFrom = msg.nFrom
    tMsgData.nType = msg.nType

    InnerMsg.sendMsg(nSrcFD, tMsgData)
end
-------------------- 跨服玩家头像数据结束 --------------------

-------------------- 跨服请求战斗数据开始 --------------------
function LW_COMBAT_GETINFO(fd, msg)
    local nDesFD = MiddleManager.getFDBySvrIndex(msg.nDesServerID)
    if not nDesFD then
        print("[LW_COMBAT_QIECUO_GETINFO] 不存在对应的fd nDesServerID = "..msg.nDesServerID.." nSrcServerID = "..msg.nSrcServerID)
        return
    end

    print("[LW_COMBAT_GETINFO] 收到")
    local tMsgData = InnerMsg.wl.WL_COMBAT_GETINFO
    tMsgData.nSrcUID = msg.nSrcUID
    tMsgData.nDesUID = msg.nDesUID
    tMsgData.nSrcServerID = msg.nSrcServerID
    tMsgData.nCombatType = msg.nCombatType
    tMsgData.extraArgs = msg.extraArgs or {}

    InnerMsg.sendMsg(nDesFD, tMsgData)
    print("[LW_COMBAT_GETINFO] 发送 WL_COMBAT_QIECUO_GETINFO")

end

function LW_COMBAT_GETINFO_SEND(fd, msg)
    local nSrcFD =  MiddleManager.getFDBySvrIndex(msg.nSrcServerID)
    if not nSrcFD then
        print("[LW_COMBAT_GETINFO_SEND] 不存在对应的fd nSrcServerID = "..msg.nSrcServerID)
        return
    end
    print("[LW_COMBAT_GETINFO_SEND] 收到")

    local tMsgData = InnerMsg.wl.WL_COMBAT_GETINFO_SEND
    tMsgData.nResult = msg.nResult
    tMsgData.nSrcUID = msg.nSrcUID
    tMsgData.tObjList = msg.tObjList
    tMsgData.tHelpList = msg.tHelpList
    tMsgData.tRoleBase = msg.tRoleBase
    tMsgData.formation = msg.formation
    tMsgData.tJiBan = msg.tJiBan
    tMsgData.tElfList = msg.tElfList

    InnerMsg.sendMsg(nSrcFD, tMsgData)
    print("[LW_COMBAT_GETINFO_SEND] 发送 WL_COMBAT_GETINFO_SEND")

end
-------------------- 跨服请求战斗数据结束 --------------------

-------------------- 天梯赛开始 ---------------------------
-- 请求参加天梯赛(普通->中心)
function LW_JJC_NEWLADDER_JOINLADDER_O2C(fd, msg)
    local nSrcServerID = msg.nSrcServerID
    local nWarFD = MiddleConnect_GetWarFirstServerFD(nSrcServerID)
    if not nWarFD then
        print("[LW_JJC_NEWLADDER_JOINLADDER_O2C] 获取不到对应的战区的FD nSrcServerID = "..nSrcServerID)
        return
    end

    print("[LW_JJC_NEWLADDER_JOINLADDER_O2C] 玩家请求参加天梯赛")

    local tMsgData = InnerMsg.wl.WL_JJC_NEWLADDER_JOINLADDER_C2D
    tMsgData.uuid = msg.uuid
    tMsgData.name = msg.name
    tMsgData.head = msg.head
    tMsgData.headFrame = msg.headFrame
    tMsgData.nSrcServerID = msg.nSrcServerID
    tMsgData.szServerName = msg.szServerName
    tMsgData.zhandouli = msg.zhandouli
    
    InnerMsg.sendMsg(nWarFD, tMsgData)

    print("[LW_JJC_NEWLADDER_JOINLADDER_O2C] 玩家请求参加天梯赛, 发送到对应的战区数据服成功")
end

-- 请求参加天梯赛(数据->中心)
function LW_JJC_NEWLADDER_JOINLADDER_D2C(fd, msg)
    local nFd = MiddleManager.getFDBySvrIndex(msg.nSrcServerID)
    if not nFd then
        print("[LW_JJC_NEWLADDER_JOINLADDER_D2C] 获取不到对应的天梯赛回复的服务器FD  nSrcServerID = "..msg.nSrcServerID)
        return
    end

    local tMsgData = InnerMsg.wl.WL_JJC_NEWLADDER_JOINLADDER_C2O
    tMsgData.uuid = msg.uuid
    tMsgData.nRank = msg.nRank
    tMsgData.tEnemy = msg.tEnemy
    tMsgData.nPoint = msg.nPoint

    InnerMsg.sendMsg(nFd, tMsgData)
    print("[LW_JJC_NEWLADDER_JOINLADDER_D2C] 请求参加天梯赛(数据->中心) 回复结束")
end

-- 请求刷新对战列表(普通->中心)
function LW_JJC_NEWLADDER_REFRESH_O2C(msg)
    local nSrcServerID = msg.nSrcServerID

    local nWarFD = MiddleConnect_GetWarFirstServerFD(nSrcServerID)
    if not nWarFD then
        print("[LW_JJC_NEWLADDER_REFRESH_O2C] 获取不到对应的战区的FD nSrcServerID = "..nSrcServerID)
        return
    end

    print("[LW_JJC_NEWLADDER_REFRESH_O2C] 玩家请求刷新对战列表")

    local tMsgData = InnerMsg.wl.WL_JJC_NEWLADDER_REFRESH_C2D
    tMsgData.uuid = msg.uuid
    tMsgData.nSrcServerID = msg.nSrcServerID

    InnerMsg.sendMsg(nWarFD, tMsgData)
    print("[LW_JJC_NEWLADDER_REFRESH_O2C] 玩家请求刷新对战列表, 发送到对应的战区数据服成功")
end

-- 请求刷新对战列表(数据->中心)
function LW_JJC_NEWLADDER_REFRESH_D2C(msg)
    local nFd = MiddleManager.getFDBySvrIndex(msg.nSrcServerID)
    if not nFd then
        print("[LW_JJC_NEWLADDER_REFRESH_D2C] 获取不到对应的天梯赛回复的服务器FD  nSrcServerID = "..msg.nSrcServerID)
        return
    end

    local tMsgData = InnerMsg.wl.WL_JJC_NEWLADDER_REFRESH_C2O
    tMsgData.uuid = msg.uuid
    tMsgData.tEnemy = msg.tEnemy

    InnerMsg.sendMsg(nFd, tMsgData)
    print("[LW_JJC_NEWLADDER_REFRESH_D2C] 请求刷新对战列表(数据->中心) 发送完成")
end

-- 请求上一轮排名前三数据(普通->中心)
function LW_JJC_NEWLADDER_LAST3RANK_O2C(msg)
    local nWarFD = MiddleConnect_GetWarFirstServerFD(msg.nSrcServerID)
    if not nWarFD then
        print("[LW_JJC_NEWLADDER_LAST3RANK_O2C] 获取不到对应的战区的FD nSrcServerID = "..msg.nSrcServerID)
        return
    end

    print("[LW_JJC_NEWLADDER_LAST3RANK_O2C] 开始先数据服发送请求上一轮前三")
    local tMsgData = InnerMsg.wl.WL_JJC_NEWLADDER_LAST3RANK_C2D
    tMsgData.nSrcServerID = msg.nSrcServerID

    InnerMsg.sendMsg(nWarFD, tMsgData)
end

-- 请求上一轮排名前三数据(数据->中心)
function LW_JJC_NEWLADDER_LAST3RANK_D2O(msg)
    local nFd = MiddleManager.getFDBySvrIndex(msg.nSrcServerID)
    if not nFd then
        print("[LW_JJC_NEWLADDER_LAST3RANK_D2O] 获取不到对应的天梯赛回复的服务器FD  nSrcServerID = "..msg.nSrcServerID)
        return
    end

    print("[LW_JJC_NEWLADDER_LAST3RANK_D2O] 获取到对应的 上一轮排名前三数据")
    local tMsgData = InnerMsg.wl.WL_JJC_NEWLADDER_LAST3RANK_C2O
    tMsgData.nExist = msg.nExist
    tMsgData.tLastRankInfo = msg.tLastRankInfo

    InnerMsg.sendMsg(nFd, tMsgData)
end

-- 进行点赞(普通->中心)
function LW_JJC_NEWLADDER_SEND_WORSHIP_O2C(msg)
    local nDesServerID = msg.nDesServerID
    local nDesFD = MiddleManager.getFDBySvrIndex(nDesServerID)
    if not nDesFD then
        print("[LW_JJC_NEWLADDER_SEND_WORSHIP_O2C] 获取不到需要传递的服务器FD  nSrcServerID = "..msg.nSrcServerID.." nDesServerID = "..nDesServerID)
        return
    end

    local tMsgData = InnerMsg.wl.WL_JJC_NEWLADDER_SEND_WORSHIP_C2D
    tMsgData.uuidSrc = msg.uuidSrc
    tMsgData.uuidDes = msg.uuidDes
    tMsgData.nSrcServerID = msg.nSrcServerID

    InnerMsg.sendMsg(nDesFD, tMsgData)
end

-- 进行点赞(数据(被点赞玩家所在服)->中心)
function LW_JJC_NEWLADDER_SEND_WORSHIP_D2C(msg)
    local nSrcFD = MiddleManager.getFDBySvrIndex(msg.nSrcServerID)
    if not nSrcFD then
        print("[LW_JJC_NEWLADDER_SEND_WORSHIP_D2C] 获取不到回复的服务器FD  nSrcServerID = "..msg.nSrcServerID)
        return
    end

    local tMsgData = InnerMsg.wl.WL_JJC_NEWLADDER_SEND_WORSHIP_C2O
    tMsgData.uuidSrc = msg.uuidSrc
    tMsgData.uuidDes = msg.uuidDes
    tMsgData.nNowWorShip = msg.nNowWorShip

    -- 回复对应的服务器
    InnerMsg.sendMsg(nSrcFD, tMsgData)

    -- 发给战区数据服，更新点赞数
    local nWarFD = MiddleConnect_GetWarFirstServerFD(msg.nSrcServerID)
    if nWarFD then
        tMsgData = InnerMsg.wl.WL_JJC_NEWLADDER_WORSHIP_CHANGE_C2D
        tMsgData.uuidDes = msg.uuidDes
        tMsgData.nAddNum = 1
        InnerMsg.sendMsg(nWarFD, tMsgData)
    else
        print("[LW_JJC_NEWLADDER_SEND_WORSHIP_D2C] 不存在对应的战区fd")
    end
end

-- 通知战区下属服务器最新的点赞数
function LW_JJC_NEWLADDER_WORSHIP_CHANGE_D2C(msg)
    local tMsgData = InnerMsg.wl.WL_JJC_NEWLADDER_WORSHIP_UPDATE_D2C
    tMsgData.uuidDes = msg.uuidDes
    tMsgData.nNowWorShip = msg.nNowWorShip

    local nConfigServerID = MiddleConnect_TrueServerID2ConfServerID(msg.nSrcServerID)
    local nMinServerID, nMaxServerID = MiddleConnect_GetWarZoneServer(nConfigServerID)
    
    for i = nMinServerID, nMaxServerID, 1 do
        local nTrueServerID = MiddleConnect_ConfServerID2TrueServerID(i)
        local nFD = MiddleManager.getFDBySvrIndex(nTrueServerID)
        if nFD then
            InnerMsg.sendMsg(nFD, tMsgData)
        else
            print("[LW_JJC_NEWLADDER_WORSHIP_CHANGE_D2C] 没有获取到对应的 nTrueServerID = "..nTrueServerID)
        end
    end
end

-- 请求天梯赛排行榜数据(普通->中心)
function LW_JJC_NEWLADDER_QUERY_RANK_O2C(msg)
    local nWarFD = MiddleConnect_GetWarFirstServerFD(msg.nSrcServerID)
    if not nWarFD then
        print("[LW_JJC_NEWLADDER_QUERY_RANK_O2C] 获取不到对应的战区的FD")    
        return
    end

    local tMsgData = InnerMsg.wl.WL_JJC_NEWLADDER_QUERY_RANK_C2D
    tMsgData.nSrcServerID = msg.nSrcServerID

    InnerMsg.sendMsg(nWarFD, tMsgData)
end

-- 回复天梯赛排行榜数据(中心->数据服)
function LW_JJC_NEWLADDER_QUERY_RANK_D2C(msg)
    local nFD =  MiddleManager.getFDBySvrIndex(msg.nSrcServerID)
    if not nFD then
        print("[LW_JJC_NEWLADDER_QUERY_RANK_D2C] 找不到请求服务器对应的FD nSrcServerID = "..msg.nSrcServerID)
        return
    end

    local tMsgData = InnerMsg.wl.WL_JJC_NEWLADDER_QUERY_RANK_C2O
    tMsgData.nIsEnd = msg.nIsEnd
    tMsgData.nFirst = msg.nFirst
    tMsgData.tRankInfo = msg.tRankInfo

    InnerMsg.sendMsg(nFD, tMsgData)
end

-- 查询是否能够战斗(普通->中心)
function LW_JJC_NEWLADDER_QUERY_CAN_FIGHT_O2C(msg)
    local nWarFD = MiddleConnect_GetWarFirstServerFD(msg.nSrcServerID)
    if not nWarFD then
        print("[LW_JJC_NEWLADDER_QUERY_CAN_FIGHT_O2C] 获取不到对应的战区的FD")    
        return
    end

    local tMsgData = InnerMsg.wl.WL_JJC_NEWLADDER_QUERY_CAN_FIGHT_C2D
    tMsgData.uuid = msg.uuid
    tMsgData.uuidDes = msg.uuidDes
    tMsgData.nSrcServerID = msg.nSrcServerID

    InnerMsg.sendMsg(nWarFD, tMsgData)
    print("[LW_JJC_NEWLADDER_QUERY_CAN_FIGHT_O2C] 查询是否能够战斗(普通->中心)")
end

-- 查询是否能够战斗(数据服->中心)
function LW_JJC_NEWLADDER_QUERY_CAN_FIGHT_D2C(msg)
    local nFD =  MiddleManager.getFDBySvrIndex(msg.nSrcServerID)
    if not nFD then
        print("[LW_JJC_NEWLADDER_QUERY_CAN_FIGHT_D2C] 找不到请求服务器对应的FD nSrcServerID = "..msg.nSrcServerID)
        return
    end

    local tMsgData = InnerMsg.wl.WL_JJC_NEWLADDER_QUERY_CAN_FIGHT_C2O
    tMsgData.uuid = msg.uuid
    tMsgData.uuidDes = msg.uuidDes
    tMsgData.nIsFight = msg.nIsFight

    InnerMsg.sendMsg(nFD, tMsgData)
end

-- 战斗结束(普通->中心)
function LW_JJC_NEWLADDER_CANCEL_FIGHT_END_O2C(msg)
    local nWarFD = MiddleConnect_GetWarFirstServerFD(msg.nSrcServerID)
    if not nWarFD then
        print("[LW_JJC_NEWLADDER_CANCEL_FIGHT_END_O2C] 获取不到对应的战区的FD")    
        return
    end

    local tMsgData = InnerMsg.wl.WL_JJC_NEWLADDER_CANCEL_FIGHT_END_C2D
    tMsgData.uuid = msg.uuid
    tMsgData.uuidDes = msg.uuidDes
    tMsgData.nSrcServerID = msg.nSrcServerID
    tMsgData.nResult = msg.nResult
    tMsgData.tEnemyUid = msg.tEnemyUid
    tMsgData.nZhanDouLi = msg.nZhanDouLi

    InnerMsg.sendMsg(nWarFD, tMsgData)
end

-- 战斗结束(数据服->中心)
function LW_JJC_NEWLADDER_CANCEL_FIGHT_END_D2C(msg)
    local nFD =  MiddleManager.getFDBySvrIndex(msg.nSrcServerID)
    if not nFD then
        print("[LW_JJC_NEWLADDER_CANCEL_FIGHT_END_D2C] 找不到请求服务器对应的FD nSrcServerID = "..msg.nSrcServerID)
        return
    end

    local tMsgData = InnerMsg.wl.WL_JJC_NEWLADDER_CANCEL_FIGHT_END_C2O
    tMsgData.uuid = msg.uuid
    tMsgData.nNewRank = msg.nNewRank
    tMsgData.nNewPoint = msg.nNewPoint
    tMsgData.uuidDes = msg.uuidDes
    tMsgData.tNewOneEnemy = msg.tNewOneEnemy
    tMsgData.tOldEnemyData = msg.tOldEnemyData

    InnerMsg.sendMsg(nFD, tMsgData)
end

--发送失败邮件
function LW_JJC_NEWLADDER_SEND_MAIL_D2C(msg)
    local nFD =  MiddleManager.getFDBySvrIndex(msg.nDesServerID)
    if not nFD then
        print("[LW_JJC_NEWLADDER_SEND_MAIL_D2C] 找不到请求服务器对应的FD nDesServerID = "..msg.nDesServerID)
        return
    end

    local tMsgData = InnerMsg.wl.WL_JJC_NEWLADDER_SEND_MAIL_C2O
    tMsgData.uuidDes = msg.uuidDes
    tMsgData.szServerName = msg.szServerName
    tMsgData.szName = msg.szName
    tMsgData.nNewRank = msg.nNewRank

    InnerMsg.sendMsg(nFD, tMsgData)
end

function LW_JJC_NEWLADDER_SEND_RANK_PRIZE_D2C(msg)
    local nFD =  MiddleManager.getFDBySvrIndex(msg.nServerID)
    if not nFD then
        print("[LW_JJC_NEWLADDER_SEND_RANK_PRIZE_D2C] 找不到请求服务器对应的FD nServerID = "..msg.nServerID)
        return
    end

    local tMsgData = InnerMsg.wl.WL_JJC_NEWLADDER_SEND_RANK_PRIZE_C2O
    tMsgData.uuid = msg.uuid
    tMsgData.nRank = msg.nRank

    InnerMsg.sendMsg(nFD, tMsgData)
    print("[LW_JJC_NEWLADDER_SEND_RANK_PRIZE_D2C] 发送对应玩家的奖励邮件")
end

function LW_JJC_NEWLADDER_QUERY_WORSHIP_D2C(msg)
    local nFD =  MiddleManager.getFDBySvrIndex(msg.nServerID)
    if not nFD then
        print("[LW_JJC_NEWLADDER_QUERY_WORSHIP_D2C] 找不到请求服务器对应的FD nServerID = "..msg.nServerID)
        return
    end

    local tMsgData = InnerMsg.wl.WL_JJC_NEWLADDER_QUERY_WORSHIP_C20
    tMsgData.uuid = msg.uuid
    tMsgData.nFromServerID = msg.nFromServerID

    InnerMsg.sendMsg(nFD, tMsgData)
end

function LW_JJC_NEWLADDER_QUERY_WORSHIP_O2C(msg)
    local nFD =  MiddleManager.getFDBySvrIndex(msg.nFromServerID)
    if not nFD then
        print("[LW_JJC_NEWLADDER_QUERY_WORSHIP_O2C] 找不到请求服务器对应的FD nServerID = "..msg.nFromServerID)
        return
    end

    local tMsgData = InnerMsg.wl.WL_JJC_NEWLADDER_QUERY_WORSHIP_C2D
    tMsgData.uuid = msg.uuid
    tMsgData.nWorShip = msg.nWorShip

    InnerMsg.sendMsg(nFD, tMsgData)
end

-------------------- 天梯赛结束 ---------------------------
