local ShopData = class("ShopData", BaseData) function ShopData:ctor() self.data.isDirty = false end function ShopData:initBase() self:initActChapterStoreData() self:initCrossDay() end function ShopData:clear() DataManager:unregisterCrossDayFunc("ShopData") end function ShopData:setDirty() self.data.isDirty = not self.data.isDirty end function ShopData:initCrossDay() DataManager:registerCrossDayFunc("ShopData", function() self:resetMallDaily() self:setDirty() end) end -- 通用礼包部分 ********************************************************************************************** -- 初始化购买的礼包 function ShopData:initActGift(act) if EDITOR_MODE then Logger.logHighlight("初始化 initActGift") Logger.printTable(act) end act = act or {} local gifts = act.gifts or {} -- 转为map结构 for _, gift in ipairs(gifts) do local giftType = gift.act_type local giftId = gift.id local buyCount = gift.buy_count -- 已购次数 local latestBuyTime = gift.latest_buy_at -- 最后一次购买时间 local latestOrderUuid = gift.latest_order_uuid -- 正在支付的订单ID if not self.giftMap then self.giftMap = {} end if not self.giftMap[giftType] then self.giftMap[giftType] = {} end self.giftMap[giftType][giftId] = gift end self:setDirty() end function ShopData:getActGiftConfig() return ConfigManager:getConfig("act_gift") end -- 已购买的礼包 function ShopData:getActGiftMap() return self.giftMap end function ShopData:getActGiftMapByType(actType) return self.giftMap and self.giftMap[actType] end function ShopData:getActGiftDetailData(actType, actId) if self.giftMap then if self.giftMap[actType] then return self.giftMap[actType][actId] end end return nil end -- 获得一个礼包的已购次数 function ShopData:getGiftBoughtNum(actType, actId) local data = self:getActGiftDetailData(actType, actId) if data then return data.buy_count end return 0 end function ShopData:updateGiftInfo(gift) local giftType = gift.act_type local giftId = gift.id if not self.giftMap then self.giftMap = {} end if not self.giftMap[giftType] then self.giftMap[giftType] = {} end self.giftMap[giftType][giftId] = gift if giftType == PayManager.PURCHARSE_TYPE.ACT_GIFT then local cfgName = PayManager.PURCHARSE_TYPE_CONFIG[giftType] if cfgName then local cfg = ConfigManager:getConfig(cfgName) if cfg and cfg[giftId] and cfg[giftId].type == PayManager.PURCHARSE_ACT_TYPE.LEVEL_FUND then DataManager.FundData:onBoughtFund(giftId) end end end self:setDirty() end -- 标记一个礼包需要弹出 在条件允许时会弹出 function ShopData:markPopUpGift(actType, actId) if not self.needPopUpGift then self.needPopUpGift = {} end if not self.needPopUpGift[actType] then self.needPopUpGift[actType] = {} end Logger.logHighlight("markPopUpGift -- actType:%s actId:%s",actType, actId) table.insert(self.needPopUpGift[actType], actId) end function ShopData:removePopUpGift(actType, actId) if self.needPopUpGift then if self.needPopUpGift[actType] then for index, id in ipairs(self.needPopUpGift[actType]) do if id == actId then Logger.logHighlight("removePopUpGift -- actType:%s actId:%s",actType, actId) table.remove(self.needPopUpGift[actType], index) break end end end end end -- 获取待处理弹出礼包结构 {type:[id1,id2,...]} function ShopData:getPopUpGift() return self.needPopUpGift end function ShopData:getPopUpGiftByType(actType) return self.needPopUpGift and self.needPopUpGift[actType] end -- 获取下一个需要弹出的同类型礼包数据,特别的,如果是主界面部分则多个不同类型的也要考虑进去 function ShopData:getNextPopGiftData(actType, actId) if GConst.ShopConst.MAIN_UI_POP_TYPE[actType] then -- 优先找自己类型的,如果是act类则需要找同类型的 local popUpGift = self:getPopUpGiftByType(actType) if popUpGift and #popUpGift > 0 then if actType == PayManager.PURCHARSE_TYPE.ACT_GIFT then for _, nextActId in ipairs(popUpGift) do if self:getActGiftConfig()[nextActId].type == self:getActGiftConfig()[actId].type then return actType, nextActId end end else for _, nextActId in ipairs(popUpGift) do return actType, nextActId end end end -- 按顺序找其他类型的 if actType ~= PayManager.PURCHARSE_TYPE.ACT_GIFT then local popUpGift = self:getPopUpGiftByType(PayManager.PURCHARSE_TYPE.ACT_GIFT) if popUpGift and #popUpGift > 0 then for _, nextActId in ipairs(popUpGift) do -- 新手礼包 if self:getActGiftConfig()[nextActId].type == PayManager.PURCHARSE_ACT_TYPE.BEGINNER_GIFT then return PayManager.PURCHARSE_TYPE.ACT_GIFT, nextActId end -- 助力礼包 if self:getActGiftConfig()[nextActId].type == PayManager.PURCHARSE_ACT_TYPE.LEVEL_UP_GIFT then return PayManager.PURCHARSE_TYPE.ACT_GIFT, nextActId end end end end if actType ~= PayManager.PURCHARSE_TYPE.CHAPTER_GIFT then local popUpGift = self:getPopUpGiftByType(PayManager.PURCHARSE_TYPE.CHAPTER_GIFT) if popUpGift and #popUpGift > 0 then for _, nextActId in ipairs(popUpGift) do return PayManager.PURCHARSE_TYPE.CHAPTER_GIFT, nextActId end end end else -- 直接返回同类型的即可 local popUpGift = self:getPopUpGiftByType(actType) if popUpGift and #popUpGift > 0 then for _, nextActId in ipairs(popUpGift) do return actType, nextActId end end end return nil end -- 通用礼包结束 ---------------------------------------------------------------------------------------------- -- 每日特惠部分 ********************************************************************************************** function ShopData:getMallDailyConfig() return ConfigManager:getConfig("mall_daily") end -- 初始化每日特惠 function ShopData:initMallDaily(mallDaily) -- if EDITOR_MODE then -- Logger.logHighlight("初始化 initMallDaily") -- Logger.printTable(mallDaily) -- end mallDaily = mallDaily or {} self.mallDailyAdResetCount = mallDaily.ad_reset_Count or 0 self.mallDailyDiamondResetCount = mallDaily.diamond_reset_Count or 0 self.mallDailyGoods = mallDaily.goods or {} -- {id,good_index,bought} self:markMallDailyDirty(false) self:setDirty() end -- 购买成功后更新数据 function ShopData:updateMallDailyGoods(info) local goods = self:getMallDailyGoods() for _, data in ipairs(goods) do if data.id == info.id and data.good_index == info.good_index then data.bought = info.bought break end end self:setDirty() end -- 每日特惠跨天重置 function ShopData:resetMallDaily() self.mallDailyAdResetCount = 0 self.mallDailyDiamondResetCount = 0 self:markMallDailyDirty(true) -- 标记需要重新请求数据,在界面/下次打开界面时请求 end function ShopData:getMallDailyDirty() return self.data.mallDailyDirty end function ShopData:markMallDailyDirty(isDirty) self.data.mallDailyDirty = isDirty end function ShopData:getMallDailyAdResetCount() return self.mallDailyAdResetCount end function ShopData:addMallDailyAdResetCount() self.mallDailyAdResetCount = self.mallDailyAdResetCount + 1 end function ShopData:getMallDailyAdLeftCount() return 1 - self.mallDailyAdResetCount -- TODOJ 目前无配置表 end function ShopData:getMallDailyDiamondResetCount() return self.mallDailyDiamondResetCount end function ShopData:addMallDailyDiamondResetCount() self.mallDailyDiamondResetCount = self.mallDailyDiamondResetCount + 1 end function ShopData:getMallDailyDiamondLeftCount() return 1 - self.mallDailyDiamondResetCount -- TODOJ 目前无配置表 end function ShopData:getMallDailyGoods() return self.mallDailyGoods end function ShopData:getMallDailyDiamondResetCost() return 30 -- TODOJ 目前无配置表 end -- 每日特惠 广告商品最大购买次数 function ShopData:getMallDailyFirstItemAdMaxCount() return 1 -- TODOJ 目前无配置表 end -- 每日特惠 常规商品最大购买次数 function ShopData:getMallDailyGoodsLimitCount() return 1 -- TODOJ 目前无配置表 end -- 每日特惠 是否开启 function ShopData:getMallDailyIsOpen() return ModuleManager:getIsOpen() end -- 根据商品id获取本日的随机商品数据 function ShopData:getMallDailyRewardAndCost(id) local cfg = self:getMallDailyConfig() local index for _, info in ipairs(self:getMallDailyGoods()) do if info.id == id then index = info.good_index break end end if index then return cfg[id].good[index], cfg[id].cost and cfg[id].cost[index] end end -- 每日特惠结束 ---------------------------------------------------------------------------------------------- -- 常驻金币礼包 ********************************************************************************************** function ShopData:getMallGoldConfig() return ConfigManager:getConfig("mall_gold") end function ShopData:initCommonDailyGoldGift(ad_count) -- if EDITOR_MODE then -- Logger.logHighlight("初始化 initCommonDailyGoldGift") -- Logger.printTable(ad_count) -- end ad_count = ad_count or {} self.commonDailyGoldBuyCount = ad_count[1] or 0 -- 已购的金币广告礼包次数 self:setDirty() end function ShopData:getCommonDailyCoinAdBuyCount() return self.commonDailyGoldBuyCount end -- 根据时间得到当前挂机金币奖励 function ShopData:getCommonDailyCoinNum(time) local coinPerHour = DataManager.IdleData:getGoldPerHour() -- TODOJ return coinPerHour * time // 3600 end function ShopData:getCommonDailyCoinDataById(id) local cfgInfo = self:getMallGoldConfig()[id] if cfgInfo then return self:getCommonDailyCoinNum(cfgInfo.idel_time), cfgInfo.cost end end -- 常驻金币礼包结束 ---------------------------------------------------------------------------------------------- -- 常驻钻石礼包 ********************************************************************************************** function ShopData:getMallTreasureConfig() return ConfigManager:getConfig("mall_treasure") end -- 常驻钻石礼包结束 ---------------------------------------------------------------------------------------------- -- 章节礼包 act_chapter_store ********************************************************************************************** function ShopData:initActChapterStoreData() if not self.actChapterStoreMap then self.actChapterStoreId2ChapterIdMap = {} self.actChapterStoreChapterId2IdMap = {} local cfg = self:getActChapterStoreConfig() for id, cfgInfo in pairs(cfg) do self.actChapterStoreId2ChapterIdMap[id] = cfgInfo.chapter self.actChapterStoreChapterId2IdMap[cfgInfo.chapter] = id end end end function ShopData:getActChapterStoreConfig() return ConfigManager:getConfig("act_chapter_store") end -- 特定章节礼包是否已购买 function ShopData:getActChapterStoreHasBuy(chapterId) -- 章节礼包的id-chapterId相互对应map local actId = self.actChapterStoreChapterId2IdMap[chapterId] if self:getGiftBoughtNum(PayManager.PURCHARSE_TYPE.CHAPTER_GIFT, actId) == 0 then return false else return true end end -- 根据当前章节,获取可购买的id数组 function ShopData:getActChapterStoreCanBuyActIds() local list = {} local curChapterId = DataManager.ChapterData:getMaxChapterId() for id = curChapterId, 1, -1 do if not self:getActChapterStoreHasBuy(id) then table.insert(list, self.actChapterStoreChapterId2IdMap[id]) end end return list end function ShopData:markPopUpGiftForActChapterStore(chapterId) local actId = self.actChapterStoreChapterId2IdMap[chapterId] self:markPopUpGift(PayManager.PURCHARSE_TYPE.CHAPTER_GIFT, actId) end -- 章节礼包结束 ---------------------------------------------------------------------------------------------- -- 新手礼包 ********************************************************************************************** function ShopData:markPopUpGiftForBeginnerGift() self:markPopUpGift(PayManager.PURCHARSE_TYPE.ACT_GIFT, GConst.ShopConst.BEGINNER_GIFT_ID) end function ShopData:getBeginnerGiftHasBuy() if self:getGiftBoughtNum(PayManager.PURCHARSE_TYPE.ACT_GIFT, GConst.ShopConst.BEGINNER_GIFT_ID) == 0 then return false else return true end end -- 新手礼包结束 ---------------------------------------------------------------------------------------------- -- 助力与金币礼包(act_gift) ********************************************************************************************** -- 初始化助力与金币礼包 function ShopData:initLevelUpGift(levelUpGift) -- if EDITOR_MODE then -- Logger.logHighlight("初始化 助力与金币礼包") -- Logger.printTable(levelUpGift) -- end levelUpGift = levelUpGift or {} self.levelUpGifts = levelUpGift.gifts or {} -- [{trigger_level,current_level_up_gift,trigger_at}] -- 金币礼包部分会一直保留 self:initCoinGift(levelUpGift.gold_gift_id, levelUpGift.gold_gift_trigger_at) self.data.currentPayAmount = levelUpGift.current_pay_amount or 0 -- 已经计算过降档的付费数额 self:setDirty() end function ShopData:initCoinGift(id, triggerTime) -- if EDITOR_MODE then -- Logger.logHighlight("初始化 金币礼包 -- id:%s time:%s", id, triggerTime) -- end self.coinGiftId = id or 0 -- 金币礼包触发id self.coinGiftTriggerTime = triggerTime or 0 -- 金币礼包触发时间 self:setDirty() end function ShopData:getPayAmount() return self.data.currentPayAmount end function ShopData:addPayment(rechargeId) local cfg = ConfigManager:getConfig("recharge")[rechargeId] self.data.currentPayAmount = self.data.currentPayAmount + cfg.price end function ShopData:getLevelUpGift() return self.levelUpGifts end function ShopData:onTriggerLevelUpGift(gift) table.insert(self.levelUpGifts, gift) -- 标记弹窗 self:markPopUpGift(PayManager.PURCHARSE_TYPE.ACT_GIFT, gift.current_level_up_gift) end function ShopData:removeLevelUpGift(actId) for index, gift in ipairs(self.levelUpGifts) do if gift.current_level_up_gift == actId then table.remove(self.levelUpGifts, index) break end end end -- 获得可购买的助力礼包Ids function ShopData:getLevelUpGiftActIds() local list = {} for index, gift in ipairs(self.levelUpGifts) do -- 剔除已购的助力礼包 这个礼包只会有一次 if self:getGiftBoughtNum(PayManager.PURCHARSE_TYPE.ACT_GIFT, gift.current_level_up_gift) == 0 then table.insert(list, gift.current_level_up_gift) end end table.sort(list) return list end -- 获得当前金币礼包的id function ShopData:getCoinGiftId() return self.coinGiftId end -- 获得金币礼包的触发时间 function ShopData:getCoinGiftTriggerTime() return self.coinGiftTriggerTime // 1000 end -- 获得当前可购买的金币礼包id,需要未购买且时间满足 function ShopData:getValidCoinGiftId() local actId = self:getCoinGiftId() if actId > 0 then local cfgInfo = self:getActGiftConfig()[actId] local triggerTime = self:getCoinGiftTriggerTime() local remainTime = triggerTime + (cfgInfo.limit_time or 0) * 3600 - Time:getServerTime() if remainTime > 0 then -- 查看最近一次购买时间 local actGiftDetailData = self:getActGiftDetailData(PayManager.PURCHARSE_TYPE.ACT_GIFT, actId) if actGiftDetailData then local latestBuyTime = actGiftDetailData.latest_buy_at // 1000 if latestBuyTime <= 0 then return actId else -- 目前规则上有CD 所以如果购买时间在CD范围内则该礼包无效 local limitTime = (self:getActGiftConfig()[actId].limit_time or 0) * 3600 if Time:getServerTime() > latestBuyTime + limitTime then return actId end end else -- 没有买过,这个肯定可以购买 return actId end end end return nil end -- 获取金币礼包付款条件cfgInfo数组 从小到大 function ShopData:_getCoinGiftPayConditionList() if not self.coinGiftPayConditionList then self.coinGiftPayConditionList = {} for id, cfgInfo in pairs(self:getActGiftConfig()) do if cfgInfo.type == PayManager.PURCHARSE_ACT_TYPE.COIN_GIFT then local tmpCfgInfo = clone(cfgInfo) tmpCfgInfo.id = id table.insert(self.coinGiftPayConditionList, tmpCfgInfo) end end table.sort(self.coinGiftPayConditionList, function (a, b) return a.pay_condition[1] < b.pay_condition[1] end) end return self.coinGiftPayConditionList end -- 根据当前付费和金币礼包数据 判断并返回合法的金币礼包触发id function ShopData:checkAndGetCoinGiftTriggerId() local triggerId local canTrigger = true local curActId = self:getCoinGiftId() if curActId > 0 then local cfgInfo = self:getActGiftConfig()[curActId] local cdTime = (cfgInfo.cd or 0) * 3600 local curTriggerTime = self:getCoinGiftTriggerTime() -- 是否还在已触发限时内 if Time:getServerTime() < curTriggerTime + cdTime then canTrigger = false end end if canTrigger then -- 从表中找出符合付费金额的actId local payAmount = self:getPayAmount() local payConditionList = self:_getCoinGiftPayConditionList() for index, info in ipairs(payConditionList) do local p1 = info.pay_condition[1] local p2 = info.pay_condition[2] if p1 <= payAmount and payAmount < p2 then triggerId = info.id break end end -- 超出上限 if not triggerId then local count = #payConditionList triggerId = payConditionList[count].id end end return triggerId end function ShopData:onTriggerCoinGift(actId) self:initCoinGift(actId, Time:getServerTime() * 1000) -- 标记弹窗 self:markPopUpGift(PayManager.PURCHARSE_TYPE.ACT_GIFT, actId) end -- 助力礼包结束 ---------------------------------------------------------------------------------------------- -- 成长礼包 ********************************************************************************************** function ShopData:getActGrowUpGiftConfig() return ConfigManager:getConfig("act_growup_gift") end -- 初始化成长礼包 function ShopData:initGrowUpGift(growUpGift, loginTime) if EDITOR_MODE then Logger.logHighlight("初始化成长礼包 growUpGift -- now_ts:%s", loginTime) Logger.printTable(growUpGift) end growUpGift = growUpGift or {} self.growUpGifts = growUpGift.grow_up_gift or {} -- {hero_id, current_grow_up_id, trigger_at} for _, gift in ipairs(self.growUpGifts) do -- 标记需要弹出的礼包 if gift.trigger_at == loginTime then self:markPopUpGiftForGrowUpGift(gift.current_grow_up_id) end end self:setDirty() end function ShopData:getGrowUpGifts() return self.growUpGifts end -- 获取目前时间有效的成长礼包 function ShopData:getValidGrowUpGifts() local list = {} for index, gift in ipairs(self:getGrowUpGifts()) do local cfgInfo = self:getActGrowUpGiftConfig()[gift.current_grow_up_id] if cfgInfo then local cd = (cfgInfo.cd or 0) * 3600 local durationTime = (cfgInfo.limit_time or 0) * 3600 local triggerTime = gift.trigger_at // 1000 local maxDurationTime = triggerTime + durationTime local maxEndTime = triggerTime + cd -- 是否在有效范围内 local isInDurationTime = Time:getServerTime() < maxDurationTime if isInDurationTime then -- 从通用act里找到这个礼包的最后购买时间 如果有且在这个范围内 则这个礼包已经被购买了 local hasBuy = false local actGiftDetailData = self:getActGiftDetailData(PayManager.PURCHARSE_TYPE.GROW_UP_GIFT, gift.current_grow_up_id) if actGiftDetailData then local latestBuyTime = actGiftDetailData.latest_buy_at // 1000 if latestBuyTime > 0 and triggerTime < latestBuyTime and latestBuyTime < maxEndTime then hasBuy = true end end if not hasBuy then table.insert(list, gift) end end end end return list end -- 触发了成长礼包 function ShopData:onTriggerGrowUpGift(gift) table.insert(self.growUpGifts, gift) self:setDirty() end function ShopData:markPopUpGiftForGrowUpGift(actId) self:markPopUpGift(PayManager.PURCHARSE_TYPE.GROW_UP_GIFT, actId) end -- 成长礼包结束 ---------------------------------------------------------------------------------------------- return ShopData