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. --LogError("抽取数据"..Inspect({ activity_id = activity_id, draw_type = draw_type, draw_count = draw_count }))
  167. ManagerContainer.NetManager:SendMessage(ProtoMsgId.CS_ACTIVITY_SMASH_EGGS_REQ, { activity_id = activity_id, draw_type = draw_type, draw_count = draw_count })
  168. return true
  169. end
  170. --- 获取砸蛋记录
  171. ---@param activity_id integer 活动id
  172. ---@param draw_type integer 4金蛋 5彩蛋
  173. function LuckyEggDataMgr:SendLuckyEggRecordReq(activity_id, draw_type)
  174. ManagerContainer.NetManager:SendMessage(ProtoMsgId.CS_ACTIVITY_SMASH_EGGS_MSG_REQ, { activity_id = activity_id, draw_type = draw_type })
  175. end
  176. --- 判断是否可以发送消息
  177. ---@param key string 键
  178. ---@param cdTime number 倒计时
  179. function LuckyEggDataMgr:IsCanSend(key, cdTime)
  180. local curTime = ManagerContainer.LuaTimerMgr:CurLuaServerTime()
  181. if not self.lastSendMsgTimeMap then
  182. self.lastSendMsgTimeMap = {}
  183. self.lastSendMsgTimeMap[key] = curTime
  184. return true
  185. end
  186. local lastTime = self.lastSendMsgTimeMap[key]
  187. if lastTime then
  188. local cd = cdTime or REQ_LuckyEgg_CD
  189. if (curTime - lastTime) < cd then
  190. return false
  191. end
  192. end
  193. self.lastSendMsgTimeMap[key] = curTime
  194. return true
  195. end
  196. --- 初始化额外奖励进度
  197. function LuckyEggDataMgr:InitLuckyEggMap(datas)
  198. if datas then
  199. self.luckyEggMap = self.luckyEggMap or {}
  200. local drawDatas = datas.draw_system
  201. if drawDatas and #drawDatas > 0 then
  202. local drawData, luckyType, luckyNum
  203. for i = 1, #drawDatas do
  204. drawData = drawDatas[i]
  205. if drawData then
  206. luckyType = drawData.draw_type or 0
  207. luckyNum = drawData.draw_times or 0
  208. self.luckyEggMap[luckyType] = luckyNum
  209. end
  210. end
  211. end
  212. end
  213. self:CalcRedPoint(true)
  214. end
  215. --- 获取额外奖励进度
  216. function LuckyEggDataMgr:GetLuckyEggNum(luckyType)
  217. if self.luckyEggMap then
  218. return self.luckyEggMap[luckyType] or 0
  219. end
  220. return 0
  221. end
  222. --- 设置额外奖励进度
  223. function LuckyEggDataMgr:SetLuckyEggNum(type, num)
  224. if not self.luckyEggMap then
  225. self.luckyEggMap = {}
  226. end
  227. self.luckyEggMap[type] = num
  228. end
  229. function LuckyEggDataMgr:GetLuckyEggData()
  230. return self.luckyEggData
  231. end
  232. function LuckyEggDataMgr:ClearLuckyEggData()
  233. self.luckyEggData = nil
  234. end
  235. function LuckyEggDataMgr:GetSendLuckyEggReqErrorCode(type, idx)
  236. local cfg = ManagerContainer.CfgMgr:GetLuckyEggCfgById(type)
  237. local method = cfg.Method
  238. if not method then
  239. return 2
  240. end
  241. local luckyEggNum = method[idx]
  242. if not luckyEggNum then
  243. return 3
  244. end
  245. local costs = cfg.Cost
  246. if not costs then
  247. return 4
  248. end
  249. local costLength = #costs
  250. if costLength <= 0 then
  251. return 4
  252. end
  253. local vaildCosts = {}
  254. local remainEggNum = luckyEggNum
  255. local cost, costCfgId, costNum, costEggNum, ownNum
  256. for i = 1, costLength do
  257. cost = costs[i]
  258. costCfgId = tonumber(cost[1])
  259. costNum = tonumber(cost[2])
  260. ownNum = CommonUtil.GetOwnResCountByItemId(costCfgId)
  261. costEggNum = Mathf.Floor(ownNum / costNum)
  262. if costEggNum >= remainEggNum then
  263. vaildCosts[#vaildCosts + 1] = { costCfgId, ownNum, costNum, remainEggNum }
  264. return 0, luckyEggNum, 0, vaildCosts
  265. else
  266. remainEggNum = remainEggNum - costEggNum
  267. vaildCosts[#vaildCosts + 1] = { costCfgId, ownNum, costNum, costEggNum }
  268. end
  269. end
  270. return 1, luckyEggNum, remainEggNum, vaildCosts
  271. end
  272. -- region --
  273. function LuckyEggDataMgr:IsRedPoint()
  274. return true
  275. end
  276. function LuckyEggDataMgr:GetLuckyEggMsgDataByType(type)
  277. if self.luckyMsgList and self.luckyMsgList[type] then
  278. return self.luckyMsgList[type]
  279. end
  280. return {}
  281. end
  282. function LuckyEggDataMgr:CalcRedPoint(forceSendMsg)
  283. if not self.cacheLuckyEggData then
  284. self:ReadCacheLuckyEggData()
  285. end
  286. local curTime = ManagerContainer.LuaTimerMgr:GetTimeSecond()
  287. if self.cacheLuckyEggData then
  288. if self.cacheLuckyEggData.rp then
  289. if forceSendMsg then
  290. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.RED_POINT_MGR_NOTICE, Enum.RPNotifyType.LuckyEggChanged, false)
  291. end
  292. return
  293. end
  294. local lastTime = self.cacheLuckyEggData.time
  295. if lastTime then
  296. if curTime < lastTime then
  297. if forceSendMsg then
  298. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.RED_POINT_MGR_NOTICE, Enum.RPNotifyType.LuckyEggChanged, false)
  299. end
  300. return
  301. end
  302. end
  303. end
  304. local isRedPoint = self:IsRedPoint()
  305. if isRedPoint then
  306. if not self.cacheLuckyEggData then
  307. self.cacheLuckyEggData = {}
  308. end
  309. local curDate = os.date("*t", curTime)
  310. -- 以本地时区计算的结果,并非精确值, 如果后续功能需要精确值,需要服务器下发
  311. local nextTime = os.time({year = curDate.year, month = curDate.month, day = curDate.day, hour = 5})
  312. if curDate.hour >= 5 then
  313. nextTime = nextTime + 24 * 3600
  314. end
  315. self.cacheLuckyEggData.rp = isRedPoint
  316. self.cacheLuckyEggData.time = nextTime
  317. self:WriteCacheLuckyEggData()
  318. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.RED_POINT_MGR_NOTICE, Enum.RPNotifyType.LuckyEggChanged, true)
  319. end
  320. end
  321. function LuckyEggDataMgr:ClearRedPoint()
  322. if self.cacheLuckyEggData then
  323. if self.cacheLuckyEggData.rp then
  324. self.cacheLuckyEggData.rp = false
  325. self:WriteCacheLuckyEggData()
  326. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.RED_POINT_MGR_NOTICE, Enum.RPNotifyType.LuckyEggChanged, false)
  327. end
  328. end
  329. end
  330. --- 读取缓存的数据
  331. function LuckyEggDataMgr:ReadCacheLuckyEggData()
  332. local cacheLuckyEggDataStr = ManagerContainer.PlayerPrefsMgr:GetString(CACHE_LUCKYEGG_KEY_NAME, '')
  333. local cacheLuckyEggData = JSON:decode(cacheLuckyEggDataStr)
  334. if not cacheLuckyEggData then
  335. cacheLuckyEggData = {}
  336. end
  337. self.cacheLuckyEggData = cacheLuckyEggData
  338. end
  339. function LuckyEggDataMgr:GetLuckyEggRedPoint()
  340. local red = true
  341. if self.cacheLuckyEggData then
  342. red = self.cacheLuckyEggData.rp
  343. end
  344. return red
  345. end
  346. --- 缓存读取的数据
  347. function LuckyEggDataMgr:WriteCacheLuckyEggData()
  348. if self.cacheLuckyEggData then
  349. local cacheLuckyEggDataStr = JSON:encode(self.cacheLuckyEggData)
  350. ManagerContainer.PlayerPrefsMgr:SetString(CACHE_LUCKYEGG_KEY_NAME, cacheLuckyEggDataStr)
  351. else
  352. ManagerContainer.PlayerPrefsMgr:SetString(CACHE_LUCKYEGG_KEY_NAME, '')
  353. end
  354. end
  355. -- endregion --
  356. return LuckyEggDataMgr