CountInfo.lua 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. ---@class CountInfo
  2. CountInfo = class()
  3. local this = CountInfo
  4. function this:ctor()
  5. end
  6. function this:Reset()
  7. this.TotalRefreshTbl = {}
  8. -- this.KeyGroupEventTbl = {}
  9. end
  10. --总countKey数据表
  11. this.TotalRefreshTbl = {}
  12. --group表 刷新事件
  13. this.KeyGroupEventTbl = {}
  14. --数据刷新类型
  15. this.TypeEnum =
  16. {
  17. --eg:Recharge = count表内group,
  18. DAILY_PACK_RECHARGE_CHANGE = 2,
  19. WEEK_PACK_RECHARGE_CHANGE = 8,
  20. NOOB_PACK_RECHARGE_CHANGE = 10,
  21. LifetimeGift = 6,
  22. OpenServerAthleticsGift = 13,
  23. MOUNT_PRE_COUNT_RECHARGE = 7,
  24. DAILY_DIAMOND_RECHARGE_CHANGE = 12,
  25. OPERATE_ADD_RECHARGE_CHANGE = 16,
  26. RUNACTIVE_DIRECT_COUNT_CHANGE = 15,
  27. }
  28. ---@class CountTbl
  29. ---@field key number @Count_count表id
  30. ---@field count number @已使用次数
  31. ---@field total number @总次数
  32. function this:Init()
  33. self:InitData()
  34. self:RegistMessages()
  35. -- this.KeyGroupEventTbl[this.TypeEnum.Recharge] = OutEvent 新增事件
  36. this.KeyGroupEventTbl[this.TypeEnum.DAILY_PACK_RECHARGE_CHANGE] = LUA_EVENT_DAILYPACK_RECHARGE_COUNT_CHANGE
  37. this.KeyGroupEventTbl[this.TypeEnum.WEEK_PACK_RECHARGE_CHANGE] = LUA_EVENT_WEEKPACK_RECHARGE_COUNT_CHANGE
  38. this.KeyGroupEventTbl[this.TypeEnum.LifetimeGift] = LUA_EVENT_LIFETIME_GIFT_COUNT_RECHARGE_CHANGE
  39. this.KeyGroupEventTbl[this.TypeEnum.NOOB_PACK_RECHARGE_CHANGE] = LUA_EVENT_NOOBPACK_RECHARGE_COUNT_CHANGE
  40. this.KeyGroupEventTbl[this.TypeEnum.OpenServerAthleticsGift] = LUA_OPEN_SERVER_GIFT_COUNT_CHANGE
  41. this.KeyGroupEventTbl[this.TypeEnum.MOUNT_PRE_COUNT_RECHARGE] = LUA_EVENT_MOUNT_PRE_COUNT_RECHARGE_CHANGE
  42. this.KeyGroupEventTbl[this.TypeEnum.DAILY_DIAMOND_RECHARGE_CHANGE] = LUA_EVENT_DAILY_DIAMOND_RECHARGE_COUNT_CHANGE
  43. this.KeyGroupEventTbl[this.TypeEnum.OPERATE_ADD_RECHARGE_CHANGE] = LUA_EVENT_OPERATE_ADD_RECHARGE_CHANGE
  44. this.KeyGroupEventTbl[this.TypeEnum.RUNACTIVE_DIRECT_COUNT_CHANGE] = LUA_EVENT_RUNACTIVE_DIRECT_COUNT_CHANGE
  45. end
  46. function this:InitData()
  47. end
  48. function this:RegistMessages()
  49. SL:RegisterLuaNetMsg(LuaMessageIdToClient.RES_ALL_COUNT_INFO,self.OnResCounts,self)
  50. SL:RegisterLuaNetMsg(LuaMessageIdToClient.RES_UPDATE_COUNT_INFO,self.OnResCount,self)
  51. end
  52. ---@param refresh CountTbl
  53. function this:AddCountKeyTbl(refresh)
  54. this.TotalRefreshTbl[refresh.key] = refresh
  55. end
  56. function this:RemoveCountKeyByKey(key)
  57. this.TotalRefreshTbl[key] = nil
  58. end
  59. function this:GetEventTblByKey(group)
  60. return this.KeyGroupEventTbl[group]
  61. end
  62. ---派发
  63. function this:TypeDispatchByKey(counKeyTbl)
  64. for _, countKey in pairs(counKeyTbl) do
  65. local countTbl = SL:GetConfig("Count_count",countKey)
  66. if(countTbl == nil) then
  67. SL:LogError("刷新数据前后端不一致"..countKey)
  68. else
  69. local eventId = this:GetEventTblByKey(countTbl.group)
  70. if eventId then
  71. SL:onLUAEvent(eventId,countKey)
  72. end
  73. end
  74. end
  75. end
  76. ---@param refresh CountTbl
  77. function this:AddTypeDispatch(refresh)
  78. local parseTbl = SL:GetConfig("Count_count",refresh.key)
  79. if(parseTbl == nil) then
  80. logError('刷新数据前后端不一致',refresh.key)
  81. return
  82. end
  83. --[[ local limitCount = refresh.total - refresh.count --剩余次数
  84. if limitCount >= 0 and parseTbl.type == 1 then --终身限购完成
  85. this:RemoveCountKeyByKey(refresh.key)
  86. else
  87. this:TypeDispatchByKey({refresh.key})
  88. end]]
  89. this:TypeDispatchByKey({refresh.key})
  90. end
  91. function this:AddTypeDispatchTbl()
  92. for _, refresh in pairs(this.TotalRefreshTbl) do
  93. this:AddTypeDispatch(refresh)
  94. end
  95. end
  96. ---@param msg table<number,CountTbl>
  97. function this:OnResCounts(_,msg)
  98. if not table.isNullOrEmpty(msg) then
  99. local data = msg
  100. for _, refresh in pairs(data) do
  101. this:AddCountKeyTbl(refresh)
  102. end
  103. this:AddTypeDispatchTbl()
  104. end
  105. end
  106. ---@param msg CountTbl
  107. function this:OnResCount(_,msg)
  108. if msg then
  109. local data = msg
  110. this:AddCountKeyTbl(data)
  111. this:AddTypeDispatch(data)
  112. end
  113. end
  114. function this:GetRefreshByKey(key)
  115. return this.TotalRefreshTbl[key]
  116. end
  117. ---获取剩余次数和总次数
  118. function this:GetLimitAndTotalCountByKey(id)
  119. --注意:type为0不应该进入此方法 代表无限购
  120. local countData = this:GetRefreshByKey(id)
  121. if not countData then
  122. countData = {count = 0}
  123. end
  124. local countTbl = SL:GetConfig("Count_count",id)
  125. if table.isNullOrEmpty(countTbl) then
  126. SL:LogError("Count_count表内未配置相关内容id"..id)
  127. return 0,0
  128. end
  129. local totalCount = countTbl.total
  130. --countData.count 已购买次数
  131. --totalCount-countData.count 剩余购买次数
  132. return totalCount-countData.count,totalCount
  133. end
  134. ---获取剩余次数和总次数 通过rechargeID
  135. function this:GetLimitAndTotalCountByRechargeId(rechargeId)
  136. if rechargeId then
  137. local rechargeTbl =SL:GetConfig("cfg_recharge",rechargeId)
  138. if rechargeTbl then
  139. --注意:type为0不应该进入此方法 代表无限购
  140. local countData = this:GetRefreshByKey(rechargeTbl.Countkey)
  141. if not countData then
  142. countData = {count = 0}
  143. end
  144. local countTbl = SL:GetConfig("Count_count",rechargeTbl.Countkey)
  145. if table.isNullOrEmpty(countTbl) then
  146. SL:LogError("Count_count表内未配置相关内容id"..rechargeTbl.Countkey)
  147. return 0,0
  148. end
  149. local totalCount = countTbl.total
  150. return totalCount-countData.count,totalCount
  151. end
  152. end
  153. return 0,0
  154. end