--------------------------------
-- 文件名       :   jjcNewLadderMiddle.lua
-- 文件说明     :   天梯赛-跨服相关
-- 创建时间     :   2025/05/21
-- 创建人       :   FC
--------------------------------

local Util = require("common.Util")
local WarZoneConf = require("excel.WarZone")
local MiddleConnect = require("middle.MiddleConnect")
local MiddleManager = require("middle.MiddleManager")
local InnerMsg = require("core.InnerMsg")
local Config = require("Config")
local CommonDB = require("common.CommonDB")
local Timer = require("core.Timer")
local Log = require("common.Log")
local JjcNewLogic = require("jjcnewladder.jjcNewLadderLogic")
-- local JjcNewLadderDB = require("jjcnewladder.jjcNewLadderDB")
local CommonDefine = require("common.CommonDefine")

----------------------------------- 内部操作 ---------------------------
-- 判断是否是战区第一个服
local function JjcNewLadderMiddle_CheckIsFirstWarServer(nServerID)
    local nConfServerID = nServerID - CommonDefine.COMMON_SERVER_OFFSET
    local tWarZoneConf = WarZoneConf.group

    for _, v in pairs(tWarZoneConf) do
        if v.nMinServerID == nConfServerID then
            return true
        end
    end

    return false
end

--------------------------- 跨服请求 ------------------------------
-- 普通服请求报名参加 
function JjcNewLadderMiddle_JoinLadder(human)
    local tMsgData = InnerMsg.lw.LW_JJC_NEWLADDER_JOINLADDER_O2C
    tMsgData.uuid = human.db._id
    tMsgData.name = human.db.name
    tMsgData.nSrcServerID = Config.SVR_INDEX
    tMsgData.head = human.db.head
    tMsgData.headFrame = human.db.headFrame
    tMsgData.szServerName = Config.NEW_SVR_INDEX.."区"
    tMsgData.zhandouli = human.db.zhandouli

    InnerMsg.sendMsg(0, tMsgData)
    
end

-- 数据服回复中心服报名数据
function JjcNewLadderMiddle_JoinLadder_D2C(uuid, nSrcServerID, nRank, tRivalData, nPoint)
    local tMsgData = InnerMsg.lw.LW_JJC_NEWLADDER_JOINLADDER_D2C
    tMsgData.uuid = uuid
    tMsgData.nSrcServerID = nSrcServerID
    tMsgData.nRank = nRank
    tMsgData.tEnemy = tRivalData
    tMsgData.nPoint = nPoint

    InnerMsg.sendMsg(0, tMsgData)
    print("[JjcNewLadderMiddle_JoinLadder_D2C] 数据服回复中心服报名数据")
end

-- 普通服请求刷新对手
function JjcNewLadderMiddle_RefreshEnemy(human)
    local tMsgData = InnerMsg.lw.LW_JJC_NEWLADDER_REFRESH_O2C
    tMsgData.uuid = human.db._id
    tMsgData.nSrcServerID = Config.SVR_INDEX

    InnerMsg.sendMsg(0, tMsgData)
    print("[JjcNewLadderMiddle_RefreshEnemy] 普通服请求刷新对手发送协议完成")
end

-- 数据服回复中心服刷新对战列表
function JjcNewLadderMiddle_RefreshEnemy_D2C(uuid, nSrcServerID, tRivalData)
    local tMsgData = InnerMsg.lw.LW_JJC_NEWLADDER_REFRESH_D2C
    tMsgData.uuid = uuid
    tMsgData.nSrcServerID = nSrcServerID
    tMsgData.tEnemy = tRivalData

    InnerMsg.sendMsg(0, tMsgData)
end

-- 请求上一轮前3名玩家信息
function JjcNewLadderMiddle_QueryLastRank()
    local tMsgData = InnerMsg.lw.LW_JJC_NEWLADDER_LAST3RANK_O2C
    tMsgData.nSrcServerID = Config.SVR_INDEX

    InnerMsg.sendMsg(0, tMsgData)
end

-- 点赞对应玩家(发起人, 被点赞人, 被点赞人服务器ID)
function JjcNewLadderMiddle_SendWorShip(uuidSrc, uuidDes, nDesServerID)
    local tMsgData = InnerMsg.lw.LW_JJC_NEWLADDER_SEND_WORSHIP_O2C
    tMsgData.uuidSrc = uuidSrc
    tMsgData.uuidDes = uuidDes
    tMsgData.nSrcServerID = Config.SVR_INDEX
    tMsgData.nDesServerID = nDesServerID

    InnerMsg.sendMsg(0, tMsgData)
end

-- 回复点赞数据
function JjcNewLadderMiddle_AddWorShip_D2C(tData, nWorShipNum)
    local tMsgData = InnerMsg.lw.LW_JJC_NEWLADDER_SEND_WORSHIP_D2C
    tMsgData.uuidSrc = tData.uuidSrc
    tMsgData.nSrcServerID = tData.nSrcServerID
    tMsgData.uuidDes = tData.uuidDes
    tMsgData.nNowWorShip = nWorShipNum

    InnerMsg.sendMsg(0, tMsgData)
end

-- 请求排行榜数据
function JjcNewLadderMiddle_QueryRankInfo()
    local tMsgData = InnerMsg.lw.LW_JJC_NEWLADDER_QUERY_RANK_O2C
    tMsgData.nSrcServerID = Config.SVR_INDEX

    InnerMsg.sendMsg(0, tMsgData)
end

-- 回复排行榜数据给中心服
function JjcNewLadderMiddle_QueryRankInfo_D2C(tRankData, nFinish, nServerID, nFirst)
    local tMsgData = InnerMsg.lw.LW_JJC_NEWLADDER_QUERY_RANK_D2C
    tMsgData.nSrcServerID = nServerID
    tMsgData.nIsEnd = nFinish
    tMsgData.nFirst = nFirst
    tMsgData.tRankInfo = tRankData

    InnerMsg.sendMsg(0, tMsgData)
end

-- 请求检测能否进入战斗
function JjcNewLadderMiddle_CheckCanFightBegin(human, uuidEnemy)
    local tMsgData = InnerMsg.lw.LW_JJC_NEWLADDER_QUERY_CAN_FIGHT_O2C
    tMsgData.uuid = human.db._id
    tMsgData.uuidDes = uuidEnemy
    tMsgData.nSrcServerID = Config.SVR_INDEX

    InnerMsg.sendMsg(0, tMsgData)
    print("[JjcNewLadderMiddle_CheckCanFightBegin] 去请求检测能否进入战斗 ")
end

-- 数据服回复检测战斗完成
function JjcNewLadderMiddle_ReplyCheckFight(tData, nCanFight)
    local tMsgData = InnerMsg.lw.LW_JJC_NEWLADDER_QUERY_CAN_FIGHT_D2C
    tMsgData.uuid = tData.uuid
    tMsgData.uuidDes = tData.uuidDes
    tMsgData.nSrcServerID = tData.nSrcServerID
    tMsgData.nIsFight = nCanFight

    InnerMsg.sendMsg(0, tMsgData)
end

-- 通知中心服战斗结果
function JjcNewLadderMiddle_FightEndO2C(uuid, uuidDes, nResult, tEnemyUid, nZhanDouLi)
    local tMsgData = InnerMsg.lw.LW_JJC_NEWLADDER_CANCEL_FIGHT_END_O2C
    tMsgData.uuid = uuid
    tMsgData.uuidDes = uuidDes
    tMsgData.nResult = nResult
    tMsgData.nSrcServerID = Config.SVR_INDEX
    tMsgData.tEnemyUid = tEnemyUid
    tMsgData.nZhanDouLi = nZhanDouLi

    InnerMsg.sendMsg(0, tMsgData)
end

-- 回复中心服战斗后最新的排名等数据
function JjcNewLadderMiddle_ReplyFightD2C(uuid, uuidDes, nSrcServerID, nNewRank, nNewPoint, tOldEnemyData, tNewOneEnemy)
    local tMsgData = InnerMsg.lw.LW_JJC_NEWLADDER_CANCEL_FIGHT_END_D2C
    tMsgData.uuid = uuid
    tMsgData.uuidDes = uuidDes
    tMsgData.nSrcServerID = nSrcServerID
    tMsgData.nNewRank = nNewRank
    tMsgData.nNewPoint = nNewPoint
    tMsgData.tOldEnemyData = tOldEnemyData
    tMsgData.tNewOneEnemy = tNewOneEnemy

    InnerMsg.sendMsg(0, tMsgData)
end

-- 发送战败邮件
function JjcNewLadderMiddle_SendFailMail(uuidDes, nServerID, SrcServerName, SrcName, nNewRank)
    print("[JjcNewLadderMiddle_SendFailMail] 开始发送战斗邮件 uuidDes = "..uuidDes.." nServerID = "..nServerID
            .." SrcServerName = "..SrcServerName.." SrcName = "..SrcName.." nNewRank = "..nNewRank)
    local tMsgData = InnerMsg.lw.LW_JJC_NEWLADDER_SEND_MAIL_D2C
    tMsgData.uuidDes = uuidDes
    tMsgData.nDesServerID = nServerID
    tMsgData.szServerName = SrcServerName
    tMsgData.szName = SrcName
    tMsgData.nNewRank = nNewRank

    InnerMsg.sendMsg(0, tMsgData)
    print("[JjcNewLadderMiddle_SendFailMail] 发送结束")
end

-- 发送奖励邮件
function JjcNewLadderMiddle_SendPrizeMail(uuid, nServerID, nRank)
    local tMsgData = InnerMsg.lw.LW_JJC_NEWLADDER_SEND_RANK_PRIZE_D2C
    tMsgData.uuid = uuid
    tMsgData.nServerID = nServerID
    tMsgData.nRank = nRank

    InnerMsg.sendMsg(0, tMsgData)
end

-- 请求玩家当前的点赞数
function JjcNewLadderMiddle_QueryWorShip(tQueryWorShip)
    for _, v in ipairs(tQueryWorShip) do
        local tMsgData = InnerMsg.lw.LW_JJC_NEWLADDER_QUERY_WORSHIP_D2C
        tMsgData.uuid = v.uuid
        tMsgData.nServerID = v.nServerID
        tMsgData.nFromServerID = Config.SVR_INDEX

        InnerMsg.sendMsg(0, tMsgData)
    end
end

function JjcNewLadderMiddle_QueryWorShipO2C(uuid, nFromServerID, nWorShip)
    local tMsgData = InnerMsg.lw.LW_JJC_NEWLADDER_QUERY_WORSHIP_O2C
    tMsgData.uuid = uuid
    tMsgData.nFromServerID = nFromServerID
    tMsgData.nWorShip = nWorShip

    InnerMsg.sendMsg(0, tMsgData)
end

--------------------------- 收到跨服请求-----------------------------
-- 收到其他服报名请求
function WL_JJC_NEWLADDER_JOINLADDER_C2D(fd, msg)
    JjcNewLogic.JjcNewLadderLogic_JoinHuman(msg)
end

-- 收到报名回包
function WL_JJC_NEWLADDER_JOINLADDER_C2O(fd, msg)
    JjcNewLogic.JjcNewLadderLogic_JoinHumanOk(msg)
end

-- 数据服收到刷新对战列表
function WL_JJC_NEWLADDER_REFRESH_C2D(fd, msg)
    print("[WL_JJC_NEWLADDER_REFRESH_C2D] 数据服收到刷新对战列表")
    JjcNewLogic.JjcNewLadderLogic_RefeshEnemy(msg)
end

-- 请求刷新对战列表(中心-普通)
function WL_JJC_NEWLADDER_REFRESH_C2O(fd, msg)
    JjcNewLogic.JjcNewLadderLogic_RefeshEnemyOK(msg)
end

-- 请求上一轮前3玩家信息(中心->数据)
function WL_JJC_NEWLADDER_LAST3RANK_C2D(msg)
    local tMsgData = InnerMsg.lw.LW_JJC_NEWLADDER_LAST3RANK_D2O
    tMsgData.nSrcServerID = msg.nSrcServerID

    local tLast3RankInfo = CommonDB.GetJjcLadderLast3RankInfo()
    if not tLast3RankInfo then
        tMsgData.nExist = 0
        tMsgData.tLastRankInfo = {}
    else
        tMsgData.nExist = 1
        tMsgData.tLastRankInfo = tLast3RankInfo
    end

    print("[WL_JJC_NEWLADDER_LAST3RANK_C2D] 获取到数据开始回复中心服")
    InnerMsg.sendMsg(0, tMsgData)
end

-- 请求上一轮前3玩家信息(中心->普通)
function WL_JJC_NEWLADDER_LAST3RANK_C2O(msg)
    JjcNewLogic.JjcNewLadderLogic_GetLast3RankOK(msg)
end

-- 进行点赞(中心->数据(被点赞玩家所在服))
function WL_JJC_NEWLADDER_SEND_WORSHIP_C2D(msg)
    JjcNewLogic.JjcNewLadderLogic_AddWorShip(msg)
end

-- 进行点赞回复(中心->普通)
function WL_JJC_NEWLADDER_SEND_WORSHIP_C2O(msg)
    JjcNewLogic.JjcNewLadderLogic_AddWorShipOK(msg)
end

-- 改变战区数据服记录的点赞数量
function WL_JJC_NEWLADDER_WORSHIP_CHANGE_C2D(msg)
    local tLast3RankInfo = CommonDB.GetJjcLadderLast3RankInfo()
    local uuid, nAddNum = msg.uuidDes, msg.nAddNum

    local nNowWorShip, nSrcServerID
    for _, v in pairs(tLast3RankInfo) do
        if v.uuid == uuid then
            v.worshipCnt = v.worshipCnt + nAddNum
            nNowWorShip = v.worshipCnt + 1
            nSrcServerID = v.nServerID
            break
        end
    end

    if not nNowWorShip then
        print("[WL_JJC_NEWLADDER_WORSHIP_CHANGE_C2D] 获取不到对应的点赞数据")
        return
    end

    CommonDB.SetJjcLadderLast3RankInfo(tLast3RankInfo)

    local tMsgData = InnerMsg.lw.LW_JJC_NEWLADDER_WORSHIP_CHANGE_D2C
    tMsgData.uuidDes = uuid
    tMsgData.nNowWorShip = nNowWorShip
    tMsgData.nSrcServerID = nSrcServerID

    InnerMsg.sendMsg(0, tMsgData)
end

-- 通知对应战区所属服务器更新最新的点赞数
function WL_JJC_NEWLADDER_WORSHIP_UPDATE_D2C(msg)
    JjcNewLogic.JjcNewLadderLogic_UpdateWorShip(msg)
end

-- 请求天梯赛排行榜数据(中心->战区数据服)
function WL_JJC_NEWLADDER_QUERY_RANK_C2D(msg)
    JjcNewLogic.JjcNewLadderLogic_GetRankInfo(msg)
end

-- 获取到天梯赛排行榜数据(中心->普通)
function WL_JJC_NEWLADDER_QUERY_RANK_C2O(msg)
    JjcNewLogic.JjcNewLadderLogic_GetRankInfoOK(msg)
end

-- 查询是否能够战斗(中心->战区数据服)
function WL_JJC_NEWLADDER_QUERY_CAN_FIGHT_C2D(msg)
    JjcNewLogic.JjcNewLadderLogic_CheckCanFight(msg)
end

-- 查询是否能够战斗(中心->普通)
function WL_JJC_NEWLADDER_QUERY_CAN_FIGHT_C2O(msg)
    JjcNewLogic.JjcNewLadderLogic_CheckCanFightOK(msg)
end

-- 战斗结束(中心->战区数据服)
function WL_JJC_NEWLADDER_CANCEL_FIGHT_END_C2D(msg)
    JjcNewLogic.JjcNewLadderLogic_FightEnd(msg)
end

-- 战斗结束(中心 -> 普通)
function WL_JJC_NEWLADDER_CANCEL_FIGHT_END_C2O(msg)
    JjcNewLogic.JjcNewLadderLogic_FightEndOK(msg)
end

function WL_JJC_NEWLADDER_SEND_MAIL_C2O(msg)
    JjcNewLogic.JjcNewLadderLogic_SendFailMailOK(msg)
end

function WL_JJC_NEWLADDER_SEND_RANK_PRIZE_C2O(msg)
    JjcNewLogic.JjcNewLadderLogic_SendPrizeMail(msg)
end

function WL_JJC_NEWLADDER_QUERY_WORSHIP_C20(msg)
    JjcNewLogic.JjcNewLadderLogic_QueryWorship(msg)
end

function WL_JJC_NEWLADDER_QUERY_WORSHIP_C2D(msg)
    local tLast3RankInfo = CommonDB.GetJjcLadderLast3RankInfo()
    if nil ~= _G.next(tLast3RankInfo) then
        for _, v in ipairs(tLast3RankInfo) do
            if v.uuid == msg.uuid then
                v.worshipCnt = msg.nWorShip
                break
            end
        end

        CommonDB.SetJjcLadderLast3RankInfo(tLast3RankInfo)
    end
end