LuckyEggDataMgr.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. local LuckyEggDataMgr = class('LuckyEggDataMgr', require('DataBase'))
  2. local JSON = require('json')
  3. local CACHE_LUCKYEGG_KEY_NAME = "Cache_LuckyEgg_Key"
  4. local ProtocalDataNormal = require("ProtocalDataNormal")
  5. local REQ_LuckyEgg_CD = 1000
  6. function LuckyEggDataMgr:ctor()
  7. self.lastSendMsgTimeMap = nil -- 发送消息的冷却时间,避免操作过快
  8. self.luckyEggData = nil -- 砸蛋后的表现数据
  9. self.luckyEggMap = nil -- 砸蛋累计数据
  10. --- 砸蛋奖励记录
  11. self.luckyMsgList = nil
  12. --- 红点等本地缓存的数据
  13. self.cacheLuckyEggData = nil
  14. end
  15. function LuckyEggDataMgr:Clear()
  16. self.lastSendMsgTimeMap = nil
  17. self.luckyEggData = nil
  18. self.luckyEggMap = nil
  19. self.cacheLuckyEggData = nil
  20. end
  21. function LuckyEggDataMgr:Destroy()
  22. self.lastSendMsgTimeMap = nil
  23. self.luckyEggData = nil
  24. self.luckyEggMap = nil
  25. self.cacheLuckyEggData = nil
  26. self:UnRegisterNetEvents()
  27. end
  28. function LuckyEggDataMgr:RegisterNetEvents()
  29. -- 砸蛋数据
  30. ManagerContainer.NetManager:NetRegister(ProtoMsgId.SC_ACTIVITY_SMASH_EGGS_ACK, self.OnLuckyEggAck, self)
  31. -- 砸蛋记录
  32. ManagerContainer.NetManager:NetRegister(ProtoMsgId.SC_ACTIVITY_SMASH_EGGS_MSG_ACK, self.OnLuckyEggRecordMsg, self)
  33. end
  34. function LuckyEggDataMgr:UnRegisterNetEvents()
  35. ManagerContainer.NetManager:UnRegisterPbIdCallback(ProtoMsgId.SC_ACTIVITY_SMASH_EGGS_ACK)
  36. ManagerContainer.NetManager:UnRegisterPbIdCallback(ProtoMsgId.SC_ACTIVITY_SMASH_EGGS_MSG_ACK)
  37. end
  38. --- 砸蛋数据消息处理
  39. function LuckyEggDataMgr:OnLuckyEggAck(data)
  40. if ManagerContainer.NetManager:IsErrorData(data) then
  41. return
  42. end
  43. local lucky_type = data.draw_type
  44. local lucky_count = data.draw_count
  45. local lucky_add_point = data.add_point
  46. local lucky_total = data.draw_times
  47. local rewards = nil
  48. local extraRewards = nil
  49. local itemList = data.item_list
  50. local item, cfgId, num, isNew
  51. local cfg = ManagerContainer.CfgMgr:GetLuckyEggCfgByType(lucky_type)
  52. self:SetLuckyEggNum(cfg.Id , lucky_total)
  53. if itemList then
  54. rewards = {}
  55. for i = 1, #itemList do
  56. item = itemList[i]
  57. if item then
  58. cfgId = item.item_id
  59. num = item.item_num
  60. isNew = item.is_new
  61. local itemCfgData = ManagerContainer.CfgMgr:GetItemById(cfgId)
  62. if not itemCfgData then
  63. LogError("[Wboy] .. " .. tostring(cfgId) .. " 道具ID不存在")
  64. else
  65. rewards[#rewards + 1] = { cfgId = cfgId, num = num, isNew = isNew }
  66. end
  67. end
  68. end
  69. end
  70. local extraItemList = data.extra_item
  71. if extraItemList then
  72. extraRewards = {}
  73. for i = 1, #extraItemList do
  74. item = extraItemList[i]
  75. if item then
  76. cfgId = item.item_id
  77. num = item.item_num
  78. isNew = item.is_new
  79. local itemCfgData = ManagerContainer.CfgMgr:GetItemById(cfgId)
  80. if not itemCfgData then
  81. LogError("[Wboy] .. " .. tostring(cfgId) .. " 道具ID不存在")
  82. else
  83. extraRewards[#extraRewards + 1] = { cfgId = cfgId, num = num, isNew = isNew }
  84. end
  85. end
  86. end
  87. end
  88. if not rewards or #rewards <= 0 then
  89. self.LuckyEggData = nil
  90. return
  91. end
  92. self.luckyEggData = {
  93. luckyType = lucky_type,
  94. luckyNum = lucky_count,
  95. luckyAdd = lucky_add_point,
  96. luckyTotal = lucky_total,
  97. rewards = rewards,
  98. extraRewards = extraRewards,
  99. }
  100. if data.msg_list then
  101. self:AddNewRewcord(data.msg_list)
  102. end
  103. --- 数据变更通知
  104. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.LUCKYEGG_DATA_CHANGED)
  105. end
  106. --- 砸蛋记录消息处理
  107. function LuckyEggDataMgr:OnLuckyEggRecordMsg(data)
  108. if ManagerContainer.NetManager:IsErrorData(data) then
  109. return
  110. end
  111. if self.luckyMsgList then
  112. self.luckyMsgList = {}
  113. end
  114. self:AddNewRewcord(data.act_msg_list)
  115. end
  116. ---@param a number
  117. ---@param b number
  118. local function TimeSortSystem(a,b)
  119. local sendTimeA = a.sendTime or a.message.sendTime
  120. local sendTimeB = b.sendTime or b.message.sendTime
  121. return sendTimeA < sendTimeB
  122. end
  123. function LuckyEggDataMgr:AddNewRewcord(msgList)
  124. if msgList == nil then
  125. return
  126. end
  127. if self.luckyMsgList == nil then
  128. self.luckyMsgList = {}
  129. end
  130. for idx, value in pairs(msgList) do
  131. local systemData = ProtocalDataNormal.ParseSystemMessageData(value)
  132. local type = systemData.type
  133. if type == Enum.ChatSystemType.LuckyEgg then
  134. -- 玩家名称
  135. local sourceName = systemData.nickname
  136. -- 记录类型
  137. local sourceType = systemData.paramIds[2]
  138. -- 奖励ID
  139. local sourceItemId = systemData.paramIds[3]
  140. -- 奖励数量
  141. local sourceCount = systemData.paramIds[4]
  142. self.luckyMsgList[sourceType] = self.luckyMsgList[sourceType] or {}
  143. local list = self.luckyMsgList[sourceType]
  144. if #list >= 10 then
  145. table.remove(list,1)
  146. end
  147. table.insert(list,{
  148. nickName = sourceName,itemId = sourceItemId,count = sourceCount,sendTime = systemData.sendTime,extraReward = true
  149. })
  150. end
  151. end
  152. for k,msglist in pairs(self.luckyMsgList) do
  153. table.sort(msglist,TimeSortSystem)
  154. end
  155. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.LUCKYEGG_RANK_DATA_CHANGED)
  156. --- 清空红点
  157. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.RED_POINT_MGR_NOTICE, Enum.RPNotifyType.LuckyEggChanged, false)
  158. end
  159. ---@param activity_id integer 活动id
  160. ---@param draw_count integer 抽取次数(单抽,10连抽)
  161. ---@param draw_type integer 4金蛋 5彩蛋
  162. function LuckyEggDataMgr:SendLuckyEggReq(activity_id, draw_count, draw_type)
  163. if not self:IsCanSend(1) then
  164. return false
  165. end
  166. ManagerContainer.NetManager:SendMessage(ProtoMsgId.CS_ACTIVITY_SMASH_EGGS_REQ, { activity_id = activity_id, draw_type = draw_type, draw_count = draw_count })
  167. return true
  168. end
  169. --- 获取砸蛋记录
  170. ---@param activity_id integer 活动id
  171. ---@param draw_type integer 4金蛋 5彩蛋
  172. function LuckyEggDataMgr:SendLuckyEggRecordReq(activity_id, draw_type)
  173. ManagerContainer.NetManager:SendMessage(ProtoMsgId.CS_ACTIVITY_SMASH_EGGS_MSG_REQ, { activity_id = activity_id, draw_type = draw_type })
  174. end
  175. --- 判断是否可以发送消息
  176. ---@param key string 键
  177. ---@param cdTime number 倒计时
  178. function LuckyEggDataMgr:IsCanSend(key, cdTime)
  179. local curTime = ManagerContainer.LuaTimerMgr:CurLuaServerTime()
  180. if not self.lastSendMsgTimeMap then
  181. self.lastSendMsgTimeMap = {}
  182. self.lastSendMsgTimeMap[key] = curTime
  183. return true
  184. end
  185. local lastTime = self.lastSendMsgTimeMap[key]
  186. if lastTime then
  187. local cd = cdTime or REQ_LuckyEgg_CD
  188. if (curTime - lastTime) < cd then
  189. return false
  190. end
  191. end
  192. self.lastSendMsgTimeMap[key] = curTime
  193. return true
  194. end
  195. --- 初始化额外奖励进度
  196. function LuckyEggDataMgr:InitLuckyEggMap(datas)
  197. if datas then
  198. self.luckyEggMap = self.luckyEggMap or {}
  199. local drawDatas = datas.draw_system
  200. if drawDatas and #drawDatas > 0 then
  201. local drawData, luckyType, luckyNum
  202. for i = 1, #drawDatas do
  203. drawData = drawDatas[i]
  204. if drawData then
  205. luckyType = drawData.draw_type or 0
  206. luckyNum = drawData.draw_times or 0
  207. self.luckyEggMap[luckyType] = luckyNum
  208. end
  209. end
  210. end
  211. end
  212. self:CalcRedPoint(true)
  213. end
  214. --- 获取额外奖励进度
  215. function LuckyEggDataMgr:GetLuckyEggNum(luckyType)
  216. if self.luckyEggMap then
  217. return self.luckyEggMap[luckyType] or 0
  218. end
  219. return 0
  220. end
  221. --- 设置额外奖励进度
  222. function LuckyEggDataMgr:SetLuckyEggNum(type, num)
  223. if not self.luckyEggMap then
  224. self.luckyEggMap = {}
  225. end
  226. self.luckyEggMap[type] = num
  227. end
  228. function LuckyEggDataMgr:GetLuckyEggData()
  229. return self.luckyEggData
  230. end
  231. function LuckyEggDataMgr:ClearLuckyEggData()
  232. self.luckyEggData = nil
  233. end
  234. function LuckyEggDataMgr:GetSendLuckyEggReqErrorCode(type, idx)
  235. local cfg = ManagerContainer.CfgMgr:GetLuckyEggCfgById(type)
  236. local method = cfg.Method
  237. if not method then
  238. return 2
  239. end
  240. local luckyEggNum = method[idx]
  241. if not luckyEggNum then
  242. return 3
  243. end
  244. local costs = cfg.Cost
  245. if not costs then
  246. return 4
  247. end
  248. local costLength = #costs
  249. if costLength <= 0 then
  250. return 4
  251. end
  252. local vaildCosts = {}
  253. local remainEggNum = luckyEggNum
  254. local cost, costCfgId, costNum, costEggNum, ownNum
  255. for i = 1, costLength do
  256. cost = costs[i]
  257. costCfgId = tonumber(cost[1])
  258. costNum = tonumber(cost[2])
  259. ownNum = CommonUtil.GetOwnResCountByItemId(costCfgId)
  260. costEggNum = Mathf.Floor(ownNum / costNum)
  261. if costEggNum >= remainEggNum then
  262. vaildCosts[#vaildCosts + 1] = { costCfgId, ownNum, costNum, remainEggNum }
  263. return 0, luckyEggNum, 0, vaildCosts
  264. else
  265. remainEggNum = remainEggNum - costEggNum
  266. vaildCosts[#vaildCosts + 1] = { costCfgId, ownNum, costNum, costEggNum }
  267. end
  268. end
  269. return 1, luckyEggNum, remainEggNum, vaildCosts
  270. end
  271. -- region --
  272. function LuckyEggDataMgr:IsRedPoint()
  273. return true
  274. end
  275. function LuckyEggDataMgr:GetLuckyEggMsgDataByType(type)
  276. if self.luckyMsgList and self.luckyMsgList[type] then
  277. return self.luckyMsgList[type]
  278. end
  279. return {}
  280. end
  281. function LuckyEggDataMgr:CalcRedPoint(forceSendMsg)
  282. if not self.cacheLuckyEggData then
  283. self:ReadCacheLuckyEggData()
  284. end
  285. local curTime = ManagerContainer.LuaTimerMgr:GetTimeSecond()
  286. if self.cacheLuckyEggData then
  287. if self.cacheLuckyEggData.rp then
  288. if forceSendMsg then
  289. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.RED_POINT_MGR_NOTICE, Enum.RPNotifyType.LuckyEggChanged, false)
  290. end
  291. return
  292. end
  293. local lastTime = self.cacheLuckyEggData.time
  294. if lastTime then
  295. if curTime < lastTime then
  296. if forceSendMsg then
  297. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.RED_POINT_MGR_NOTICE, Enum.RPNotifyType.LuckyEggChanged, false)
  298. end
  299. return
  300. end
  301. end
  302. end
  303. local isRedPoint = self:IsRedPoint()
  304. if isRedPoint then
  305. if not self.cacheLuckyEggData then
  306. self.cacheLuckyEggData = {}
  307. end
  308. local curDate = os.date("*t", curTime)
  309. -- 以本地时区计算的结果,并非精确值, 如果后续功能需要精确值,需要服务器下发
  310. local nextTime = os.time({year = curDate.year, month = curDate.month, day = curDate.day, hour = 5})
  311. if curDate.hour >= 5 then
  312. nextTime = nextTime + 24 * 3600
  313. end
  314. self.cacheLuckyEggData.rp = isRedPoint
  315. self.cacheLuckyEggData.time = nextTime
  316. self:WriteCacheLuckyEggData()
  317. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.RED_POINT_MGR_NOTICE, Enum.RPNotifyType.LuckyEggChanged, true)
  318. end
  319. end
  320. function LuckyEggDataMgr:ClearRedPoint()
  321. if self.cacheLuckyEggData then
  322. if self.cacheLuckyEggData.rp then
  323. self.cacheLuckyEggData.rp = false
  324. self:WriteCacheLuckyEggData()
  325. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.RED_POINT_MGR_NOTICE, Enum.RPNotifyType.LuckyEggChanged, false)
  326. end
  327. end
  328. end
  329. --- 读取缓存的数据
  330. function LuckyEggDataMgr:ReadCacheLuckyEggData()
  331. local cacheLuckyEggDataStr = ManagerContainer.PlayerPrefsMgr:GetString(CACHE_LUCKYEGG_KEY_NAME, '')
  332. local cacheLuckyEggData = JSON:decode(cacheLuckyEggDataStr)
  333. if not cacheLuckyEggData then
  334. cacheLuckyEggData = {}
  335. end
  336. self.cacheLuckyEggData = cacheLuckyEggData
  337. end
  338. function LuckyEggDataMgr:GetLuckyEggRedPoint()
  339. local red = true
  340. if self.cacheLuckyEggData then
  341. red = self.cacheLuckyEggData.rp
  342. end
  343. return red
  344. end
  345. --- 缓存读取的数据
  346. function LuckyEggDataMgr:WriteCacheLuckyEggData()
  347. if self.cacheLuckyEggData then
  348. local cacheLuckyEggDataStr = JSON:encode(self.cacheLuckyEggData)
  349. ManagerContainer.PlayerPrefsMgr:SetString(CACHE_LUCKYEGG_KEY_NAME, cacheLuckyEggDataStr)
  350. else
  351. ManagerContainer.PlayerPrefsMgr:SetString(CACHE_LUCKYEGG_KEY_NAME, '')
  352. end
  353. end
  354. -- endregion --
  355. return LuckyEggDataMgr