--[[ 时间机制 ]] local TimerWheel_Controller = {} local private = {} local EventSystem = require('Event/EventSystem') local TimerWheelData = require "Modules/TimerWheel/TimerWheelData" --key:timeId, value:TimerWheelData private.timerWheelDataList = {} function TimerWheel_Controller.OnStartup() EventSystem.Regist(EventID.New_Day_Refresh, TimerWheel_Controller.InitActivityDatas) end function TimerWheel_Controller.InitActivityDatas() private.RemoveAllTimeData() for __, activityData in DataSheetManager.ActivityData:GetEnumerator() do if activityData.ID == LuaConst.ActivityType.HeroMatch1v1 and MDL.HeroMatch1v1.CheckIsBattleStage() then -- 天地群雄新增活动开启主界面弹窗 local MatchTime = DataSheetManager.MatchTime:GetDataByID(MDL.HeroMatch1v1.GetCurMatchTimeID()) private.AddOneActivityTimeData(activityData.ID, 1, MatchTime.StartTime, MatchTime.EndTime) elseif activityData.ID == LuaConst.ActivityType.HeroMatch3v3 and MDL.HeroMatch3v3.CheckIsBattleStage() then local MatchTime = DataSheetManager.TeamMatchTime:GetDataByID(MDL.HeroMatch3v3.GetCurMatchTimeID()) private.AddOneActivityTimeData(activityData.ID, 1, MatchTime.StartTime, MatchTime.EndTime) elseif activityData.ID == LuaConst.ActivityType.FairSoloTournament and MDL.FairSoloTournament.CheckIsBattleStage() then -- 天地会武新增活动开启主界面弹窗 local timeCfg = DataSheetManager.FairMatchTime:GetDataByID(MDL.FairSoloTournament.GetCurMatchTimeID()) private.AddOneActivityTimeData(activityData.ID, 1, timeCfg.StartTime, timeCfg.EndTime) else for index, timeData in ipairs(activityData.Time_Table) do private.AddOneActivityTimeData(activityData.ID, index, timeData.StartTime, timeData.EndTime) end end end for __, timerWheelData in pairs(private.timerWheelDataList) do timerWheelData:Start() end end function private.RemoveAllTimeData() for __, timerWheelData in pairs(private.timerWheelDataList) do timerWheelData:Stop() end private.timerWheelDataList = {} end function private.AddOneActivityTimeData(activityId, timeIndex, startTimeCfgId, endTimeCfgId) local startTimeCfg = DataSheetManager.TimeRefresh:GetDataByID(startTimeCfgId) local endTimeCfg = DataSheetManager.TimeRefresh:GetDataByID(endTimeCfgId) local startLeftTime, endLeftTime = 0, 0 local serverSecondsIntervalFromZero = UTL.Time.GetServerSecondsIntervalFromZero() if startTimeCfg.Timer == UTL.Time.TimeType.DAILY then startLeftTime = startTimeCfg.HMS - serverSecondsIntervalFromZero endLeftTime = endTimeCfg.HMS - serverSecondsIntervalFromZero elseif startTimeCfg.Timer == UTL.Time.TimeType.WEEKLY then local curWeekDay = UTL.Time.GetServerWeekday() if curWeekDay ~= startTimeCfg.Day then return end startLeftTime = startTimeCfg.HMS - serverSecondsIntervalFromZero endLeftTime = endTimeCfg.HMS - serverSecondsIntervalFromZero end if startLeftTime > 0 or endLeftTime > 0 then TimerWheel_Controller.AddOneData(activityId .. "_" .. timeIndex, startLeftTime, endLeftTime, true, false, activityId, timeIndex) end end function private.GetTimerWheelData(timeId) return private.timerWheelDataList[timeId] end function TimerWheel_Controller.OnTimeStartOver(timeId) local timerWheelData = private.GetTimerWheelData(timeId) if timerWheelData then timerWheelData:OnTimeStartOver() EventSystem.Dispatch(EventID.TimerWheelStartTimeOver, timerWheelData:GetDispatchData()) end end function TimerWheel_Controller.OnTimeEndOver(timeId) local timerWheelData = private.GetTimerWheelData(timeId) if timerWheelData then timerWheelData:OnTimeEndOver() if timerWheelData.isAutoRemove then TimerWheel_Controller.RemoveOneData(timeId) end EventSystem.Dispatch(EventID.TimerWheelEndTimeOver, timerWheelData:GetDispatchData()) end end --添加timer数据 --@param timeId:唯一id --@param startLeftTickTime:还有多长时间活动开启 --@param endLeftTickTime:还有多长时间活动结束 --@param isAutoStart:自动开启 --@param function TimerWheel_Controller.AddOneData(timeId, startLeftTickTime, endLeftTickTime, isNotAutoStart, isNotAutoRemove, param1, param2) if startLeftTickTime <= 0 and endLeftTickTime <= 0 then logging.error(LTAG.ERROR, "Can not Add Neither start nor end timer") return end local timerWheelData = TimerWheelData:new(timeId, not isNotAutoRemove) timerWheelData:SetStartEndTimeLeft(startLeftTickTime, endLeftTickTime) timerWheelData:SetParamData(param1, param2) private.timerWheelDataList[timeId] = timerWheelData if not isNotAutoStart then timerWheelData:Start() end end --移除一个时间数据 function TimerWheel_Controller.RemoveOneData(timeId) local timerWheelData = private.timerWheelDataList[timeId] if timerWheelData then timerWheelData:Stop() private.timerWheelDataList[timeId] = nil end end --主要给活动用 function TimerWheel_Controller.IsActivityInTime(activityId) for __, timerWheelData in pairs(private.timerWheelDataList) do if timerWheelData.param1 == activityId and timerWheelData:IsInTime() then return true, timerWheelData.param2 end end return false, 0 end --通用,判断是否在开启状态 function TimerWheel_Controller.IsInTime(timeId) local timerWheelData = private.timerWheelDataList[timeId] return timerWheelData and timerWheelData:IsInTime() end return TimerWheel_Controller