local BattleAgent_Controller = {} local private = {} local EventSystem = require('Event/EventSystem') local AgentLevelLimit = DataSheetManager.ConstData:GetDataByID("KcpagentConst_Level").IntValue -- 延迟差距等级限制 local AgentPingHintCheckTime = DataSheetManager.ConstData:GetDataByID("KcpagentConst_Frequency").IntValue -- 战斗代理服延迟过高提示检测次数 local AgentPingHigh = DataSheetManager.ConstData:GetDataByID("KcpagentConst_DelayExceed").IntValue -- 高延迟检测 local AgentPingDifference = DataSheetManager.ConstData:GetDataByID("KcpagentConst_DelayDiffer").IntValue -- 延迟差距检测 local PingMaxTimes = 5 -- 对一个地址ping最大次数 BattleAgent_Controller.HintRecord = "UIBatteryPanel_HintRecord" function BattleAgent_Controller.OnStartup() private.fightingShowPing = false -- cheat专用 private.agentPingHintCheckTime = 0 end function BattleAgent_Controller.ReSetAgentPingHintCheckTime() private.agentPingHintCheckTime = 0 end function BattleAgent_Controller.GetFightingShowPing() return private.fightingShowPing end function BattleAgent_Controller.ChangeFightingShowPing() private.fightingShowPing = not private.fightingShowPing end function BattleAgent_Controller.ShowBattleAgent() -- 功能不稳定暂时屏蔽 return false -- 是海外 并且 是非同步战斗中 并且 是61大区或者是显示cheat的情况下 才显示 -- if GameAppInfo.IsOverseaPackage() and (GameAppInfo.IsOverseaHMT() or UNITY_EDITOR or DEF.USE_GAME_CHEAT) and not LevelManager.IsSyncLevel() then -- return true -- else -- return false -- end end function BattleAgent_Controller.UpdateBattleAgentPing(isOpenList) if private.IsOpenTotalPing() then MDL.BattleAgent.RequestAllPing() private.CheckPingState() else if isOpenList then MDL.BattleAgent.RequestAllPing() else MDL.BattleAgent.RequestCurPing() end end end -- 是否开启全量ping function private.IsOpenTotalPing() if MDL.Player.GetRoleLevel() >= AgentLevelLimit and not PersistentData.PlayerData:Get(BattleAgent_Controller.HintRecord, false) then local curAgentData = MDL.BattleAgent.GetCurSelectAgentData() if curAgentData then -- 只有当前线路延迟大于配置才进行全量请求 if MDL.BattleAgent.GetAgentPing(curAgentData) > AgentPingHigh then return true end end end return false end function private.CheckPingState() local curAgentData = MDL.BattleAgent.GetCurSelectAgentData() if not curAgentData then return end local isHiht = false local curAgentPing = MDL.BattleAgent.GetAgentPing(curAgentData) -- 只有当前线路延迟大于配置才进行判断 if curAgentPing > AgentPingHigh then local battleAgentList = MDL.BattleAgent.GetBattleAgentList() for _, v in ipairs(battleAgentList) do -- 只有当前线路延迟 比 其他线路高30以上才进行提示 local otherPing = MDL.BattleAgent.GetAgentPing(v) if curAgentPing > otherPing + AgentPingDifference then isHiht = true break end end end if isHiht then private.agentPingHintCheckTime = private.agentPingHintCheckTime + 1 if private.agentPingHintCheckTime >= AgentPingHintCheckTime then EventSystem.Dispatch(EventID.BattleAgent_ChangeHint) end else private.agentPingHintCheckTime = 0 end end --region kcp ping 战斗代理 local KCPAgentPingIntervalTime = 3 -- 战斗代理服 迭代ping不同的agent的 间隔(注意,这个值一定要比KCPTransport.CheckBattleAgentPing中的持续时间(2秒)大) function BattleAgent_Controller.OnEnterLevel() if not GameAppInfo.IsOverseaPackage() then return end local hasPingedOver = MDL.BattleAgent.GetPingBattleAgentIsOver() if hasPingedOver then return end if BattleDataGetter.IsTownLevel() then local kcpAgentList = MDL.BattleAgent.GetKCPAgentList() if #kcpAgentList > 0 then local kcpAgentList = MDL.BattleAgent.GetKCPAgentList() for i, info in ipairs(kcpAgentList) do if info.hasPingedTimes < PingMaxTimes then private.kcpPingTimer = TimerUtil.Interval(private.OnUpdateAgentKCPPing, KCPAgentPingIntervalTime, #kcpAgentList) private.OnUpdateAgentKCPPing() break end end else MDL.BattleAgent.SetPingBattleAgentIsOver(false) if not private.isRequestingBattleAgentList then MDL.BattleAgent.GetKCPBattleAgentListReq() private.isRequestingBattleAgentList = true end end end end function BattleAgent_Controller.OnExitLevel() BattleAgent_Controller.TryStopPingAgent() end function BattleAgent_Controller.TryStopPingAgent() if private.kcpPingTimer then KCPTransport.CloseBattleAgent() TimerUtil.Remove(private.kcpPingTimer) private.kcpPingTimer = nil private.curPingBattleAgent = nil end end -- 收到列表协议后开始ping function BattleAgent_Controller.StartKCPPingBattleAgent() private.isRequestingBattleAgentList = false local kcpAgentList = MDL.BattleAgent.GetKCPAgentList() if #kcpAgentList > 0 then private.kcpPingTimer = TimerUtil.Interval(private.OnUpdateAgentKCPPing, KCPAgentPingIntervalTime, 0) private.OnUpdateAgentKCPPing() end end -- 战斗代理服的kcp的ping function private.OnUpdateAgentKCPPing(deltaTime, timeSinceUpdate) if private.curPingBattleAgent then local maxPing,minPing,avgPing = BattleExt.GetPingInfo(true) avgPing = avgPing == 0 and 999 or avgPing EventSystem.Dispatch(EventID.BI_LevelMiddle,BIManager.BIType.SyncBattle,"KcpAgentPing", maxPing,minPing,avgPing, private.curPingBattleAgent.name) private.curPingBattleAgent.hasPingedTimes = private.curPingBattleAgent.hasPingedTimes + 1 KCPTransport.CloseBattleAgent() private.curPingBattleAgent = nil end local list = MDL.BattleAgent.GetKCPAgentList() local allIsPinged = true for i, info in ipairs(list) do if info.hasPingedTimes < PingMaxTimes then KCPTransport.ConnectBattleAgent(info.ip, info.port) private.curPingBattleAgent = info allIsPinged = false break end end if allIsPinged then if private.kcpPingTimer then TimerUtil.Remove(private.kcpPingTimer) private.kcpPingTimer = nil MDL.BattleAgent.SetPingBattleAgentIsOver(true) end end end --玩家断线重连后,这个代理的ping需要重新执行 function BattleAgent_Controller.OnTCPReconnect() if private.kcpPingTimer then TimerUtil.Remove(private.kcpPingTimer) private.kcpPingTimer = nil end MDL.BattleAgent.ResetKCPAgentList() private.curPingBattleAgent = nil private.isRequestingBattleAgentList = false MDL.BattleAgent.SetPingBattleAgentIsOver(false) -- 若当前为主城,则立刻开始请求ping列表 if BattleDataGetter.IsTownLevel() then MDL.BattleAgent.GetKCPBattleAgentListReq() private.isRequestingBattleAgentList = true end end --endregion kcp ping 战斗代理 return BattleAgent_Controller