LuckyEggDataMgr.lua 13 KB

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