local LuckyEggDataMgr = class('LuckyEggDataMgr', require('DataBase')) local JSON = require('json') local CACHE_LUCKYEGG_KEY_NAME = "Cache_LuckyEgg_Key" local ProtocalDataNormal = require("ProtocalDataNormal") local REQ_LuckyEgg_CD = 1000 function LuckyEggDataMgr:ctor() self.lastSendMsgTimeMap = nil -- 发送消息的冷却时间,避免操作过快 self.luckyEggData = nil -- 砸蛋后的表现数据 self.luckyEggMap = nil -- 砸蛋累计数据 --- 砸蛋奖励记录 self.luckyMsgList = nil --- 红点等本地缓存的数据 self.cacheLuckyEggData = nil self.curActID = nil end function LuckyEggDataMgr:Clear() self.lastSendMsgTimeMap = nil self.luckyEggData = nil self.luckyEggMap = nil self.cacheLuckyEggData = nil end function LuckyEggDataMgr:Destroy() self.lastSendMsgTimeMap = nil self.luckyEggData = nil self.luckyEggMap = nil self.cacheLuckyEggData = nil self:UnRegisterNetEvents() end function LuckyEggDataMgr:RegisterNetEvents() -- 砸蛋数据 ManagerContainer.NetManager:NetRegister(ProtoMsgId.SC_ACTIVITY_SMASH_EGGS_ACK, self.OnLuckyEggAck, self) -- 砸蛋记录 ManagerContainer.NetManager:NetRegister(ProtoMsgId.SC_ACTIVITY_SMASH_EGGS_MSG_ACK, self.OnLuckyEggRecordMsg, self) end function LuckyEggDataMgr:UnRegisterNetEvents() ManagerContainer.NetManager:UnRegisterPbIdCallback(ProtoMsgId.SC_ACTIVITY_SMASH_EGGS_ACK) ManagerContainer.NetManager:UnRegisterPbIdCallback(ProtoMsgId.SC_ACTIVITY_SMASH_EGGS_MSG_ACK) end --- 砸蛋数据消息处理 function LuckyEggDataMgr:OnLuckyEggAck(data) if ManagerContainer.NetManager:IsErrorData(data) then return end local lucky_type = data.draw_type local lucky_count = data.draw_count local lucky_add_point = data.add_point local lucky_total = data.draw_times local rewards = nil local extraRewards = nil local itemList = data.item_list local item, cfgId, num, isNew local cfg = ManagerContainer.CfgMgr:GetLuckyEggCfgByType(lucky_type) self:SetLuckyEggNum(cfg.Id , lucky_total) if itemList then rewards = {} for i = 1, #itemList do item = itemList[i] if item then cfgId = item.item_id num = item.item_num isNew = item.is_new local itemCfgData = ManagerContainer.CfgMgr:GetItemById(cfgId) if not itemCfgData then LogError("[Wboy] .. " .. tostring(cfgId) .. " 道具ID不存在") else rewards[#rewards + 1] = { cfgId = cfgId, num = num, isNew = isNew } end end end end local extraItemList = data.extra_item if extraItemList then extraRewards = {} for i = 1, #extraItemList do item = extraItemList[i] if item then cfgId = item.item_id num = item.item_num isNew = item.is_new local itemCfgData = ManagerContainer.CfgMgr:GetItemById(cfgId) if not itemCfgData then LogError("[Wboy] .. " .. tostring(cfgId) .. " 道具ID不存在") else extraRewards[#extraRewards + 1] = { cfgId = cfgId, num = num, isNew = isNew } end end end end if not rewards or #rewards <= 0 then self.LuckyEggData = nil return end self.luckyEggData = { luckyType = lucky_type, luckyNum = lucky_count, luckyAdd = lucky_add_point, luckyTotal = lucky_total, rewards = rewards, extraRewards = extraRewards, } if data.msg_list then self:AddNewRewcord(data.msg_list) end --- 数据变更通知 ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.LUCKYEGG_DATA_CHANGED) end --- 砸蛋记录消息处理 function LuckyEggDataMgr:OnLuckyEggRecordMsg(data) if ManagerContainer.NetManager:IsErrorData(data) then return end if self.luckyMsgList then self.luckyMsgList = {} end self:AddNewRewcord(data.act_msg_list) end ---@param a number ---@param b number local function TimeSortSystem(a,b) local sendTimeA = a.sendTime or a.message.sendTime local sendTimeB = b.sendTime or b.message.sendTime return sendTimeA < sendTimeB end function LuckyEggDataMgr:AddNewRewcord(msgList) if msgList == nil then return end if self.luckyMsgList == nil then self.luckyMsgList = {} end for idx, value in pairs(msgList) do local systemData = ProtocalDataNormal.ParseSystemMessageData(value) local type = systemData.type if type == Enum.ChatSystemType.LuckyEgg then -- 玩家名称 local sourceName = systemData.nickname -- 记录类型 local sourceType = systemData.paramIds[2] -- 奖励ID local sourceItemId = systemData.paramIds[3] -- 奖励数量 local sourceCount = systemData.paramIds[4] self.luckyMsgList[sourceType] = self.luckyMsgList[sourceType] or {} local list = self.luckyMsgList[sourceType] if #list >= 10 then table.remove(list,1) end table.insert(list,{ nickName = sourceName,itemId = sourceItemId,count = sourceCount,sendTime = systemData.sendTime,extraReward = true }) end end for k,msglist in pairs(self.luckyMsgList) do table.sort(msglist,TimeSortSystem) end ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.LUCKYEGG_RANK_DATA_CHANGED) --- 清空红点 ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.RED_POINT_MGR_NOTICE, Enum.RPNotifyType.LuckyEggChanged, false) end ---@param activity_id integer 活动id ---@param draw_count integer 抽取次数(单抽,10连抽) ---@param draw_type integer 4金蛋 5彩蛋 function LuckyEggDataMgr:SendLuckyEggReq(activity_id, draw_count, draw_type) if not self:IsCanSend(1) then return false end --LogError("抽取数据"..Inspect({ activity_id = activity_id, draw_type = draw_type, draw_count = draw_count })) ManagerContainer.NetManager:SendMessage(ProtoMsgId.CS_ACTIVITY_SMASH_EGGS_REQ, { activity_id = activity_id, draw_type = draw_type, draw_count = draw_count }) return true end --- 获取砸蛋记录 ---@param activity_id integer 活动id ---@param draw_type integer 4金蛋 5彩蛋 function LuckyEggDataMgr:SendLuckyEggRecordReq(activity_id, draw_type) ManagerContainer.NetManager:SendMessage(ProtoMsgId.CS_ACTIVITY_SMASH_EGGS_MSG_REQ, { activity_id = activity_id, draw_type = draw_type }) end --- 判断是否可以发送消息 ---@param key string 键 ---@param cdTime number 倒计时 function LuckyEggDataMgr:IsCanSend(key, cdTime) local curTime = ManagerContainer.LuaTimerMgr:CurLuaServerTime() if not self.lastSendMsgTimeMap then self.lastSendMsgTimeMap = {} self.lastSendMsgTimeMap[key] = curTime return true end local lastTime = self.lastSendMsgTimeMap[key] if lastTime then local cd = cdTime or REQ_LuckyEgg_CD if (curTime - lastTime) < cd then return false end end self.lastSendMsgTimeMap[key] = curTime return true end --- 初始化额外奖励进度 function LuckyEggDataMgr:InitLuckyEggMap(datas) if datas then self.luckyEggMap = self.luckyEggMap or {} local drawDatas = datas.draw_system if drawDatas and #drawDatas > 0 then local drawData, luckyType, luckyNum for i = 1, #drawDatas do drawData = drawDatas[i] if drawData then luckyType = drawData.draw_type or 0 luckyNum = drawData.draw_times or 0 self.luckyEggMap[luckyType] = luckyNum end end end end self:CalcRedPoint(true) end --- 获取额外奖励进度 function LuckyEggDataMgr:GetLuckyEggNum(luckyType) if self.luckyEggMap then return self.luckyEggMap[luckyType] or 0 end return 0 end --- 设置额外奖励进度 function LuckyEggDataMgr:SetLuckyEggNum(type, num) if not self.luckyEggMap then self.luckyEggMap = {} end self.luckyEggMap[type] = num end function LuckyEggDataMgr:GetLuckyEggData() return self.luckyEggData end function LuckyEggDataMgr:ClearLuckyEggData() self.luckyEggData = nil end function LuckyEggDataMgr:GetSendLuckyEggReqErrorCode(type, idx) local cfg = ManagerContainer.CfgMgr:GetLuckyEggCfgById(type) local method = cfg.Method if not method then return 2 end local luckyEggNum = method[idx] if not luckyEggNum then return 3 end local costs = cfg.Cost if not costs then return 4 end local costLength = #costs if costLength <= 0 then return 4 end local vaildCosts = {} local remainEggNum = luckyEggNum local cost, costCfgId, costNum, costEggNum, ownNum for i = 1, costLength do cost = costs[i] costCfgId = tonumber(cost[1]) costNum = tonumber(cost[2]) ownNum = CommonUtil.GetOwnResCountByItemId(costCfgId) costEggNum = Mathf.Floor(ownNum / costNum) if costEggNum >= remainEggNum then vaildCosts[#vaildCosts + 1] = { costCfgId, ownNum, costNum, remainEggNum } return 0, luckyEggNum, 0, vaildCosts else remainEggNum = remainEggNum - costEggNum vaildCosts[#vaildCosts + 1] = { costCfgId, ownNum, costNum, costEggNum } end end return 1, luckyEggNum, remainEggNum, vaildCosts end -- region -- function LuckyEggDataMgr:IsRedPoint() return true end function LuckyEggDataMgr:GetLuckyEggMsgDataByType(type) if self.luckyMsgList and self.luckyMsgList[type] then return self.luckyMsgList[type] end return {} end function LuckyEggDataMgr:CalcRedPoint(forceSendMsg) if not self.cacheLuckyEggData then self:ReadCacheLuckyEggData() end local curTime = ManagerContainer.LuaTimerMgr:GetTimeSecond() if self.cacheLuckyEggData then if self.cacheLuckyEggData.rp then if forceSendMsg then ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.RED_POINT_MGR_NOTICE, Enum.RPNotifyType.LuckyEggChanged, false) end return end local lastTime = self.cacheLuckyEggData.time if lastTime then if curTime < lastTime then if forceSendMsg then ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.RED_POINT_MGR_NOTICE, Enum.RPNotifyType.LuckyEggChanged, false) end return end end end local isRedPoint = self:IsRedPoint() if isRedPoint then if not self.cacheLuckyEggData then self.cacheLuckyEggData = {} end local curDate = os.date("*t", curTime) -- 以本地时区计算的结果,并非精确值, 如果后续功能需要精确值,需要服务器下发 local nextTime = os.time({year = curDate.year, month = curDate.month, day = curDate.day, hour = 5}) if curDate.hour >= 5 then nextTime = nextTime + 24 * 3600 end self.cacheLuckyEggData.rp = isRedPoint self.cacheLuckyEggData.time = nextTime self:WriteCacheLuckyEggData() ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.RED_POINT_MGR_NOTICE, Enum.RPNotifyType.LuckyEggChanged, true) end end function LuckyEggDataMgr:ClearRedPoint() if self.cacheLuckyEggData then if self.cacheLuckyEggData.rp then self.cacheLuckyEggData.rp = false self:WriteCacheLuckyEggData() ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.RED_POINT_MGR_NOTICE, Enum.RPNotifyType.LuckyEggChanged, false) end end end --- 读取缓存的数据 function LuckyEggDataMgr:ReadCacheLuckyEggData() local cacheLuckyEggDataStr = ManagerContainer.PlayerPrefsMgr:GetString(CACHE_LUCKYEGG_KEY_NAME, '') local cacheLuckyEggData = JSON:decode(cacheLuckyEggDataStr) if not cacheLuckyEggData then cacheLuckyEggData = {} end self.cacheLuckyEggData = cacheLuckyEggData end function LuckyEggDataMgr:GetLuckyEggRedPoint() local red = true if self.cacheLuckyEggData then red = self.cacheLuckyEggData.rp end return red end --- 缓存读取的数据 function LuckyEggDataMgr:WriteCacheLuckyEggData() if self.cacheLuckyEggData then local cacheLuckyEggDataStr = JSON:encode(self.cacheLuckyEggData) ManagerContainer.PlayerPrefsMgr:SetString(CACHE_LUCKYEGG_KEY_NAME, cacheLuckyEggDataStr) else ManagerContainer.PlayerPrefsMgr:SetString(CACHE_LUCKYEGG_KEY_NAME, '') end end -- endregion -- return LuckyEggDataMgr