local PushGift_Model = {} local EventSystem = require('Event/EventSystem') local private = {} local operation_activity_pb = require "Proto.operation_activity_pb" private.pushGiftArray = {} private.pushGiftArrayWithoutSpecial = {} private.serverPushData = nil function PushGift_Model.OnStartup() NetManager.RegisterMsgCB(MessageEnums.MessageAcquireRealPushGift, private.AcquireRealPushGiftResp) end function private.SortRule(a, b) if a.startTime == b.startTime then local data1 = MDL.OperationActivity.PushGiftData:GetDataByActivityID_ID(a.activityId, a.giftId) local data2 = MDL.OperationActivity.PushGiftData:GetDataByActivityID_ID(b.activityId, b.giftId) if data1 and data2 then return data1.Orderid > data2.Orderid end end return a.startTime > b.startTime end -- 获取权重最高的礼包(最新的、时间相同权重最高的) function PushGift_Model.GetHighestOrderInfo() local giftInfo = nil if #private.pushGiftArray > 0 then --可能有多个,找到权重最高的那个 local startTime = -1 local orderId for _, v in ipairs(private.pushGiftArray) do local activityId = v.activityId local giftId = v.giftId if not CTL.PushGift.IsSpecialPushGift(activityId, giftId) then if v.startTime == 0 or v.startTime > startTime then startTime = v.startTime giftInfo = v local data = MDL.OperationActivity.PushGiftData:GetDataByActivityID_ID(activityId, giftId) if data then orderId = data.Orderid else logging.error(LTAG.ERROR, "can`t find PushGift Data", activityId, giftId) end elseif v.startTime == startTime then local data = MDL.OperationActivity.PushGiftData:GetDataByActivityID_ID(activityId, giftId) if data then if data.Orderid > orderId then startTime = v.startTime giftInfo = v orderId = data.Orderid end else logging.error(LTAG.ERROR, "can`t find PushGift Data", activityId, giftId, "请策划检查活动数据表 hot5PushGift") end end end end end return giftInfo end function PushGift_Model.SetPushGiftArray(info) private.serverPushData = info if private.refreshTimer then TimerUtil.Remove(private.refreshTimer) end --由于proto数据不能排序,这里重新构造了个表来排序 local minTimeOffset --最小结束时间,计时刷新数据 table.clearArray(private.pushGiftArray) table.clearArray(private.pushGiftArrayWithoutSpecial) for i, v in ipairs(info) do if v.timing then local offset = v.endTime - UTL.Time.GetServerTimestamp() if offset > 0 then private.pushGiftArray[i] = v if not CTL.PushGift.IsSpecialPushGift(v.activityId, v.giftId) then table.insert( private.pushGiftArrayWithoutSpecial, v) end if minTimeOffset == nil or offset < minTimeOffset then minTimeOffset = offset end end else private.pushGiftArray[i] = v if not CTL.PushGift.IsSpecialPushGift(v.activityId, v.giftId) then table.insert( private.pushGiftArrayWithoutSpecial, v) end end end if minTimeOffset then private.refreshTimer = TimerUtil.Interval(private.RefreshPushGiftArray, minTimeOffset, 1) end if #private.pushGiftArray > 1 then table.sort(private.pushGiftArray, private.SortRule) end if #private.pushGiftArrayWithoutSpecial > 1 then table.sort(private.pushGiftArrayWithoutSpecial, private.SortRule) end --自动弹出 if private.IsUnCountedPushGift() then CTL.PushGift.DealAutoOpen() end if private.IsUnCountedSpecialPushGift() then CTL.PushGift.DealAutoOpenSpecial() end EventSystem.Dispatch(EventID.UI_PushGift_NewGiftChange) end --与策划约定:同一时间仅会开一个定向活动,开多个定向运营活动的时候也不会出现重复的礼包(即礼包ID唯一) --此接口建立在与策划约定好的基础上,尽量减少调用次数,目前仅在直购页签中显示特定定向礼包内容的时候使用 function PushGift_Model.GetPushGiftInfoById(giftId, giftRewardId) for _, v in ipairs(private.pushGiftArray) do if v.giftId == giftId then local info = MDL.OperationActivity.PushGiftData:GetDataByActivityID_ID(v.activityId, v.giftId) if info and info.RewardId == giftRewardId then return v end end end end function PushGift_Model.GetPushGiftInfo(activityId, giftId) for _, v in ipairs(private.pushGiftArray) do if v.activityId == activityId and v.giftId == giftId then return v end end end function PushGift_Model.GetPushGiftArray(isSpecial) if isSpecial then return private.pushGiftArray else return private.pushGiftArrayWithoutSpecial end end --计时刷新数据,将时间过期的数据清理掉 function private.RefreshPushGiftArray() TimerUtil.Remove(private.refreshTimer) local minTimeOffset for i = #private.pushGiftArray, 1, -1 do local info = private.pushGiftArray[i] local offset = info.endTime - UTL.Time.GetServerTimestamp() if offset > 0 then if minTimeOffset == nil or offset < minTimeOffset then minTimeOffset = offset end else table.remove(private.pushGiftArray, i) end end for i = #private.pushGiftArrayWithoutSpecial, 1, -1 do local info = private.pushGiftArrayWithoutSpecial[i] local offset = info.endTime - UTL.Time.GetServerTimestamp() if offset > 0 then if minTimeOffset == nil or offset < minTimeOffset then minTimeOffset = offset end else table.remove(private.pushGiftArrayWithoutSpecial, i) end end EventSystem.Dispatch(EventID.UI_PushGift_TimeRefresh) if minTimeOffset then private.refreshTimer = TimerUtil.Interval(private.RefreshPushGiftArray, minTimeOffset, 1) end end function PushGift_Model.GetMinTimeOffset() local minTimeOffset for i = 1, #private.pushGiftArray do local info = private.pushGiftArray[i] local offset = info.endTime - UTL.Time.GetServerTimestamp() if offset > 0 then if minTimeOffset == nil or offset < minTimeOffset then minTimeOffset = offset end end end return minTimeOffset end function PushGift_Model.GetIfOpen() return #private.pushGiftArrayWithoutSpecial > 0 end --是否过期 function PushGift_Model.GetIfExpire() return #private.pushGiftArray == 0 and private.serverPushData and #private.serverPushData > 0 end --是否有未倒计时的普通礼包 function private.IsUnCountedPushGift() if #private.pushGiftArrayWithoutSpecial == 0 then return false end for i, v in ipairs(private.pushGiftArrayWithoutSpecial) do if v.timing == false then return true end end return false end --是否有未倒计时的特殊礼包 function private.IsUnCountedSpecialPushGift() if #private.pushGiftArray == 0 then return false end for i, v in ipairs(private.pushGiftArray) do if v.timing == false and CTL.PushGift.IsSpecialPushGift(v.activityId, v.giftId) then return true end end return false end --打开界面,通知服务器开始倒计时,只有当未开启倒计时的情况才请求 function PushGift_Model.AcquireRealPushGiftReq() if private.IsUnCountedPushGift() or private.IsUnCountedSpecialPushGift() then local msg = operation_activity_pb.AcquireRealPushGiftReq() NetManager.SendMessage(MessageEnums.MessageAcquireRealPushGift, msg) end end function private.AcquireRealPushGiftResp(msgResp) end return PushGift_Model