RuneShopBaseData.lua 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. local RuneShopBaseData = class('RuneShopBaseData')
  2. local RuneShopGoodsData = require('RuneShop/RuneShopGoodsData')
  3. local function CompValidTimeData(a, b)
  4. if a and a > 0 then
  5. if not b or b > a then
  6. b = a
  7. end
  8. end
  9. return b
  10. end
  11. function RuneShopBaseData:ctor()
  12. self.runeShopType = nil
  13. self.runeShopSubType = nil
  14. self.endTime = nil
  15. self.goodsDataMap = nil
  16. self.needRefreshTime = nil
  17. self.needRefreshDataTime = nil
  18. self.showGoodsDatas = nil
  19. self.hasFreeCanBuy = nil
  20. end
  21. function RuneShopBaseData:Dispose()
  22. self.endTime = nil
  23. self.goodsDataMap = nil
  24. self.needRefreshTime = nil
  25. self.needRefreshDataTime = nil
  26. self.showGoodsDatas = nil
  27. self.hasFreeCanBuy = nil
  28. end
  29. function RuneShopBaseData:SetData(data)
  30. local runeShopType = data.shop_type
  31. local runeShopSubType = data.sub_shop
  32. local goodsList = data.goods_list
  33. local endTime = data.end_time
  34. local goodsDataMap = {}
  35. local goodsData = nil
  36. if goodsList then
  37. local curNum = #goodsList
  38. for i = 1, curNum do
  39. local goods = goodsList[i]
  40. if goods then
  41. local id = goods.goods_id
  42. local cfgData = ManagerContainer.CfgMgr:GetRuneShopCfgByFeature(runeShopType, runeShopSubType, id)
  43. if cfgData then
  44. if self.goodsDataMap then
  45. goodsData = self.goodsDataMap[id]
  46. else
  47. goodsData = nil
  48. end
  49. if not goodsData then
  50. goodsData = RuneShopGoodsData:new()
  51. end
  52. goodsData:SetData(goods, cfgData)
  53. if goodsDataMap[id] then
  54. LogWarning('[wboy] Find Same Goods Info !!! id = ' .. tostring(id))
  55. end
  56. goodsDataMap[id] = goodsData
  57. else
  58. LogError('[wboy] Config Not Find Goods Info !!! id = ' .. tostring(id) .. ' runeShopType = ' .. tostring(runeShopType) .. ' runeShopSubType = ' .. tostring(runeShopSubType))
  59. end
  60. end
  61. end
  62. end
  63. self.runeShopType = runeShopType
  64. self.runeShopSubType = runeShopSubType
  65. self.endTime = endTime
  66. self.goodsDataMap = goodsDataMap
  67. self:RefreshShowGoodsDatas()
  68. end
  69. function RuneShopBaseData:RefreshOneGoodsData(data)
  70. local id = data.goods_id
  71. local goodsData = self.goodsDataMap[id]
  72. if goodsData then
  73. local isShow, refreshTime, refreshDataTime = goodsData:GetShowInfo()
  74. goodsData:RefreshData(data)
  75. local isShowNew, refreshTimeNew, refreshDataTimeNew = goodsData:GetShowInfo()
  76. if isShow ~= isShowNew or refreshTime ~= refreshTimeNew or refreshDataTime ~= refreshDataTimeNew then
  77. self:RefreshShowGoodsDatas()
  78. else
  79. local hasFreeCanBuy = false
  80. for _,goodsData in pairs(self.showGoodsDatas) do
  81. if not hasFreeCanBuy and goodsData:IsFree() and goodsData:IsCanBuy() then
  82. hasFreeCanBuy = true
  83. break
  84. end
  85. end
  86. local rpChange = (self.hasFreeCanBuy ~= hasFreeCanBuy)
  87. self.hasFreeCanBuy = hasFreeCanBuy
  88. if rpChange then
  89. self:RedPointNotify()
  90. end
  91. end
  92. end
  93. end
  94. function RuneShopBaseData:RefreshShowGoodsDatas()
  95. local needRefreshTime = nil
  96. local needRefreshDataTime = nil
  97. local hasFreeCanBuy = false
  98. local showGoodsDatas = {}
  99. for _, goodsData in pairs(self.goodsDataMap) do
  100. local isShow, refreshTime, refreshDataTime = goodsData:GetShowInfo()
  101. if isShow then
  102. showGoodsDatas[#showGoodsDatas+1] = goodsData
  103. if not hasFreeCanBuy and goodsData:IsFree() and goodsData:IsCanBuy() then
  104. hasFreeCanBuy = true
  105. end
  106. end
  107. needRefreshTime = CompValidTimeData(refreshTime, needRefreshTime)
  108. needRefreshDataTime = CompValidTimeData(refreshDataTime, needRefreshDataTime)
  109. end
  110. needRefreshTime = CompValidTimeData(self.endTime, needRefreshTime)
  111. needRefreshDataTime = CompValidTimeData(self.endTime, needRefreshDataTime)
  112. local rpChange = (self.hasFreeCanBuy ~= hasFreeCanBuy)
  113. self.needRefreshTime = needRefreshTime
  114. self.needRefreshDataTime = needRefreshDataTime
  115. self.showGoodsDatas = showGoodsDatas
  116. self.hasFreeCanBuy = hasFreeCanBuy
  117. self:SortShowData()
  118. if rpChange then
  119. self:RedPointNotify()
  120. end
  121. end
  122. function RuneShopBaseData:SortShowData()
  123. if not self.showGoodsDatas then return end
  124. table.sort(self.showGoodsDatas, function (a, b)
  125. local isSoldOutA = a:IsSoldout()
  126. local isSoldOutB = b:IsSoldout()
  127. if isSoldOutA == isSoldOutB then
  128. return a:GetSortId() < b:GetSortId()
  129. else
  130. return not isSoldOutA
  131. end
  132. end)
  133. end
  134. function RuneShopBaseData:GetGoodsDataByIdx(idx)
  135. return self.goodsDataMap[idx]
  136. end
  137. function RuneShopBaseData:GetShowGoodsDatas()
  138. return self.showGoodsDatas
  139. end
  140. function RuneShopBaseData:IsValidShow()
  141. if not self.needRefreshTime then
  142. return true
  143. end
  144. local remainTime = ManagerContainer.LuaTimerMgr:GetRemainSecondsWithUInt64(self.needRefreshTime)
  145. return remainTime >= 0
  146. end
  147. function RuneShopBaseData:IsValidData()
  148. if not self.needRefreshDataTime then
  149. return true
  150. end
  151. local remainTime = ManagerContainer.LuaTimerMgr:GetRemainSecondsWithUInt64(self.needRefreshDataTime)
  152. return remainTime >= 0
  153. end
  154. function RuneShopBaseData:RefreshRemainTime()
  155. if not self.needRefreshDataTime then
  156. return nil
  157. end
  158. return ManagerContainer.LuaTimerMgr:GetRemainSecondsWithUInt64(self.needRefreshDataTime)
  159. end
  160. function RuneShopBaseData:RedPointNotify()
  161. if self.runeShopType == Enum.RuneShopType.Gifts then
  162. if self.runeShopSubType == Enum.RuneShopSubType.Daily then
  163. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.RED_POINT_MGR_NOTICE, Enum.RPNotifyType.RuneShopDaily, self.hasFreeCanBuy)
  164. elseif self.runeShopSubType == Enum.RuneShopSubType.Week then
  165. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.RED_POINT_MGR_NOTICE, Enum.RPNotifyType.RuneShopWeek, self.hasFreeCanBuy)
  166. elseif self.runeShopSubType == Enum.RuneShopSubType.Month then
  167. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.RED_POINT_MGR_NOTICE, Enum.RPNotifyType.RuneShopMonth, self.hasFreeCanBuy)
  168. end
  169. end
  170. end
  171. return RuneShopBaseData