local FreeBuy_Model = {} local EventSystem = require('Event/EventSystem') local private = {} local free_purchase_opt_pb = require "Proto.free_purchase_opt_pb" local FREEBUY_NEW_KEY = "FreeBuy_New" function FreeBuy_Model.OnStartup() NetManager.RegisterMsgCB(MessageEnums.MessageFreePurchaseByItems, private.FreePurchaseByItemsResp)--通过普通道具购买零元购礼包 NetManager.RegisterMsgCB(MessageEnums.MessageClaimPurchaseReturn, private.ClaimPurchaseReturnResp)--领取零元购返还 EventSystem.Regist(EventID.UI_DirectGift_GiftInfoUpdate, private.GiftInfoUpdate) end --存在多个活动的情况 function FreeBuy_Model.GetActivityIDArray() return private.activityIDArray end --通过普通道具购买零元购礼包(Req) function FreeBuy_Model.FreePurchaseByItemsReq(activityID,hotPackageID) local msg = free_purchase_opt_pb.FreePurchaseByItemsReq() msg.ActivityID = activityID msg.HotPackageID = hotPackageID --目标礼包ID --logging.error(LTAG.ERROR,"FreePurchaseByItemsReq",activityID,hotPackageID) NetManager.SendMessage(MessageEnums.MessageFreePurchaseByItems, msg) end --通过普通道具购买零元购礼包(Resp) function private.FreePurchaseByItemsResp(msgResp) if msgResp.Resp.Code == 0 then local hotPackageID = msgResp.HotPackageID --目标礼包ID local activityID = msgResp.ActivityID --logging.error(LTAG.ERROR,"FreePurchaseByItemsResp",activityID,hotPackageID) CTL.FreeBuy.AddGiftPayBackTimer(activityID,hotPackageID) EventSystem.Dispatch(EventID.UI_FreeBuy_RefreshRedPoint) EventSystem.Dispatch(EventID.UI_FreeBuy_RefreshUI) end end --领取零元购返还(Req) function FreeBuy_Model.ClaimPurchaseReturnReq(activityID,hotPackageID) local msg = free_purchase_opt_pb.ClaimPurchaseReturnReq() msg.ActivityID = activityID msg.HotPackageID = hotPackageID --目标礼包ID NetManager.SendMessage(MessageEnums.MessageClaimPurchaseReturn, msg) end --领取零元购返还(Resp) function private.ClaimPurchaseReturnResp(msgResp) if msgResp.Resp.Code == 0 then local hotPackageID = msgResp.HotPackageID --目标礼包ID local activityID = msgResp.ActivityID CTL.FreeBuy.RemoveGiftPayBackTimer(activityID,hotPackageID) EventSystem.Dispatch(EventID.UI_FreeBuy_RefreshRedPoint) EventSystem.Dispatch(EventID.UI_FreeBuy_RefreshUI) end end --同步活动信息 function FreeBuy_Model.SyncActivityInfo(freePurOpens) logging.log(LTAG.LOG,"FreeBuy_Model.SyncActivityInfo",freePurOpens) if IsServerDataNotNil(freePurOpens) then private.activityIDArray = freePurOpens --多个开启的0元购活动ID end end --同步0元购IAP礼包返还龙玉数据 function FreeBuy_Model.SyncIapGiftFreeReturnInfo(iapGiftFreeReturn) private.iapGiftFreeReturn = iapGiftFreeReturn --零元购IAP礼包返还龙玉数据 end --获取0元购IAP礼包返还龙玉数据 function FreeBuy_Model.GetIapGiftFreeReturnInfo(giftID) for _, v in ipairs(private.iapGiftFreeReturn) do if giftID == v.HotGiftInfoID then return v.ReturnDragonJade, MDL.Currency.CurrencyType.DragonJade end end return 0, MDL.Currency.CurrencyType.DragonJade end --礼包信息更新 function private.GiftInfoUpdate() logging.log(LTAG.LOG,"FreeBuy_Model.GiftInfoUpdate") -- 初始化下礼包数据 FreeBuy_Model.GetPackageData() CTL.FreeBuy.RefreshAllGiftPayBackTimer() CTL.FreeBuy.RefreshTimer() end --是否为已购买礼包 function FreeBuy_Model.IsHasBuyPackage(activityID,hotPackageID) local data = FreeBuy_Model.GetDataByActivityIDAndPackageID(activityID,hotPackageID) if data then return data.info.backTime ~=0 end return false end --获取返还时间 function FreeBuy_Model.GetReturnTime(activityID,hotPackageID) local data = FreeBuy_Model.GetDataByActivityIDAndPackageID(activityID,hotPackageID) if data then return data.info.backTime end return nil end --根据活动id、礼包Id 获取礼包 function FreeBuy_Model.GetDataByActivityIDAndPackageID(activityID, hotPackageID) local packageData = FreeBuy_Model.GetPackageData() for i,v in pairs(packageData) do if v.activityId == activityID and v.data.HotPackageID == hotPackageID then return v end end return nil end function FreeBuy_Model.GetLockedDataByActivityIDAndPackageID(activityID, hotPackageID) local packageData = FreeBuy_Model.GetLockedPackageData() for i,v in pairs(packageData) do if v.activityId == activityID and v.data.HotPackageID == hotPackageID then return v end end return nil end --是否有可领取返还的礼包 function FreeBuy_Model.IsCanGetReturnPackage(activityID,hotPackageID) if FreeBuy_Model.IsPackageNotEnd(activityID,hotPackageID) then local nowTime = UTL.Time.GetServerTimestamp() local returnTime = FreeBuy_Model.GetReturnTime(activityID,hotPackageID) if returnTime > 0 and nowTime > returnTime then return true end end return false end --是否为已返还礼包 function FreeBuy_Model.IsHasReturnPackage(activityID,hotPackageID) --礼包超时了,肯定返还了 if not FreeBuy_Model.IsPackageNotEnd(activityID,hotPackageID) then return true end --已购买,且返还完毕的 local data = FreeBuy_Model.GetDataByActivityIDAndPackageID(activityID,hotPackageID) if data then return data.info.backTime == -1 end return false end --是否礼包还没结束 function FreeBuy_Model.IsPackageNotEnd(activityID,hotPackageID) local data = FreeBuy_Model.GetDataByActivityIDAndPackageID(activityID,hotPackageID) if data then local nowTime = UTL.Time.GetServerTimestamp() return data.info.payBackShowEndTime > nowTime end return false end --获取礼包数据 function FreeBuy_Model.GetPackageData() local idArray = FreeBuy_Model.GetActivityIDArray() if idArray then private.packageData = {} local hasData = false for i,id in ipairs(idArray) do for _, v in MDL.DirectGift.GetDirectGiftEnumerator(MDL.DirectGift.DirectGiftType.Base, id) do local nowTime = UTL.Time.GetServerTimestamp() --排除掉超过时间、 已经返还的礼包 if v.info.payBackShowEndTime > nowTime and v.info.backTime ~= -1 then --logging.log(LTAG.LOG,"insert package",v.activityId,v.data.HotPackageID,v.info.startTime) table.insert(private.packageData,v) hasData = true end end end if hasData then table.sort(private.packageData,private.SortPackage) end end return private.packageData end --todo by gjh,完善筛选逻辑 function FreeBuy_Model.GetLockedPackageData() local idArray = FreeBuy_Model.GetActivityIDArray() if idArray then private.lockedPackageData = {} local hasData = false for i,id in ipairs(idArray) do for _, v in MDL.DirectGift.GetLockedDirectGiftEnumerator(MDL.DirectGift.DirectGiftType.Base, id) do local nowTime = UTL.Time.GetServerTimestamp() --排除掉超过时间、 已经返还的礼包 if v.info.payBackShowEndTime > nowTime and v.info.backTime ~= -1 then --logging.log(LTAG.LOG,"insert locked package",v.activityId,v.data.HotPackageID,v.info.startTime) table.insert(private.lockedPackageData,v) hasData = true end end end if hasData then table.sort(private.lockedPackageData,private.SortPackage) end end return private.lockedPackageData end function FreeBuy_Model.IsLockedPackage(activityID,hotPackageID) if private.lockedPackageData then for _, v in pairs(private.lockedPackageData) do if v.activityId == activityID and v.data.HotPackageID == hotPackageID then return true end end end return false end --礼包排序 function private.SortPackage(a,b) return a.data.HotPackageWeight > b.data.HotPackageWeight end --首次展示某个礼包的红点,需要存盘,跟随账号. function FreeBuy_Model.SetFirstShow(activityId, packageId) local dataChange = false if not activityId or not packageId then return dataChange end dataChange = true local t = PersistentData.PlayerData:Get(FREEBUY_NEW_KEY) if not t then t = {} end for _, v in ipairs(t) do if v.aId == activityId and v.pId == packageId then dataChange = false --本来就有这个数据,不用插入了 break end end if dataChange then table.insert(t, { aId = activityId, pId = packageId }) PersistentData.PlayerData:Set(FREEBUY_NEW_KEY, t, true) end return dataChange end --首次展示的红点 function FreeBuy_Model.GetFirstShowRP(activityId, packageId) if not activityId or not packageId then return false --无效数据,不显示红点 end local t = PersistentData.PlayerData:Get(FREEBUY_NEW_KEY) if t then for _, v in ipairs(t) do if v.aId == activityId and v.pId == packageId then return false --能找到数据说明已经展示过了,不再显示红点 end end end return true --找不到数据说明没展示过,可显示红点 end --有没有新活动没展示过的,需要在城镇记为红点 function FreeBuy_Model.AnyFirstShowRP() local pkgData = FreeBuy_Model.GetPackageData() for _, v in pairs(pkgData) do local showRP = FreeBuy_Model.GetFirstShowRP(v.activityId, v.data.HotPackageID) if showRP then return true end end return false end return FreeBuy_Model