UIShopCtr.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. local UIShopCtr = class("UIShopCtr", require("UICtrBase"))
  2. function UIShopCtr:Init(view)
  3. self.view = view
  4. end
  5. function UIShopCtr:SetData(data)
  6. self.asyncIdx = 0
  7. self.data = data
  8. self.isResetSelect = true
  9. end
  10. function UIShopCtr:GetAsyncIdx()
  11. self.asyncIdx = self.asyncIdx + 1
  12. return self.asyncIdx
  13. end
  14. function UIShopCtr:GetData()
  15. return self.data
  16. end
  17. function UIShopCtr:OnDispose()
  18. self.data = nil
  19. self.view = nil
  20. self.isResetSelect = false
  21. self.nearestRemainTime = nil
  22. self.shopLs = nil
  23. self.shopEndTimes = nil
  24. self.curSelectIdx = nil
  25. self.curShopCfgData = nil
  26. self.curShopData = nil
  27. end
  28. function UIShopCtr:InitData()
  29. local shopLs = {}
  30. local cfgDatas = ManagerContainer.CfgMgr:GetShopTypeCfg()
  31. for _, cfgData in pairs(cfgDatas) do
  32. if cfgData.ActShop == 0 then
  33. if Constant.OpenPay or not cfgDatas.NoPay then
  34. shopLs[#shopLs+1] = cfgData
  35. end
  36. end
  37. end
  38. local shopEndTimes = {}
  39. local nearestRemainTime = nil
  40. local remainTime = nil
  41. local actItemList = ManagerContainer.DataMgr.ActsDataMgr.actItemList
  42. if actItemList then
  43. for _, actItem in pairs(actItemList) do
  44. if actItem and actItem.type == Enum.ActivityType.ACTIVITY_TYPE_SHOP then
  45. if actItem:IsUnlocked() and actItem:IsOpened() then
  46. if actItem.actShopIds then
  47. LogError("====================="..Inspect(actItem))
  48. for _, actShopId in pairs(actItem.actShopIds) do
  49. cfgDatas = ManagerContainer.CfgMgr:GetShopTypeCfgById(actShopId)
  50. if cfgDatas then
  51. if Constant.OpenPay or not cfgDatas.NoPay then
  52. if actItem:IsLimited() then
  53. if not actItem:IsOutofDate() then
  54. remainTime = actItem:LeftTime()
  55. shopEndTimes[actShopId] = actItem.endTime
  56. if not nearestRemainTime or nearestRemainTime > remainTime then
  57. nearestRemainTime = remainTime
  58. end
  59. shopLs[#shopLs+1] = cfgDatas
  60. end
  61. else
  62. shopLs[#shopLs+1] = cfgDatas
  63. end
  64. end
  65. end
  66. end
  67. end
  68. end
  69. end
  70. end
  71. end
  72. self.nearestRemainTime = nearestRemainTime
  73. self.shopEndTimes = shopEndTimes
  74. LogError("==================="..Inspect(shopLs))
  75. table.sort(shopLs, function(a, b)
  76. return a.SortId < b.SortId
  77. end)
  78. self.shopLs = shopLs
  79. local curSelectIdx = self.curSelectIdx
  80. local num = #shopLs
  81. if not curSelectIdx or self.isResetSelect then
  82. if self.data then
  83. local shopId = nil
  84. if type(self.data) ~= 'number' and self.data.actId then
  85. local actItem = ManagerContainer.DataMgr.ActsDataMgr:GetActivityItemById(self.data.actId)
  86. if actItem and actItem.actShopIds and #actItem.actShopIds > 0 then
  87. shopId = actItem.actShopIds[1]
  88. end
  89. else
  90. shopId = self.data
  91. end
  92. if shopId then
  93. for i = 1, #shopLs do
  94. if self.shopLs[i].Id == shopId then
  95. curSelectIdx = i
  96. break
  97. end
  98. end
  99. end
  100. end
  101. end
  102. if not curSelectIdx then
  103. curSelectIdx = (num > 0 and 1 or 0)
  104. end
  105. self.curSelectIdx = curSelectIdx
  106. end
  107. function UIShopCtr:RefreshCurShopData(forceRefresh)
  108. self.curShopCfgData = self.shopLs[self.curSelectIdx]
  109. if self.curShopCfgData then
  110. self.curShopData = ManagerContainer.DataMgr.ShopDataMgr:GetShopData(self.curShopCfgData.Id, forceRefresh)
  111. else
  112. self.curShopData = nil
  113. end
  114. if self.curShopData then
  115. if ManagerContainer.LuaTimerMgr:GetRemainSeconds(self.curShopData:GetNeedRefreshTime()) <= 0 then
  116. self.curShopData:RefreshShowGoodsDatas()
  117. end
  118. end
  119. end
  120. function UIShopCtr:GetNearestRemainTime()
  121. return self.nearestRemainTime
  122. end
  123. function UIShopCtr:GeCurEndTime()
  124. if self.shopLs then
  125. local cfgData = self.shopLs[self.curSelectIdx]
  126. if cfgData then
  127. return self.shopEndTimes and self.shopEndTimes[cfgData.Id] or nil
  128. end
  129. end
  130. return nil
  131. end
  132. function UIShopCtr:GetShopLs()
  133. return self.shopLs
  134. end
  135. function UIShopCtr:GetShopByIdx(idx)
  136. return self.shopLs and self.shopLs[idx] or nil
  137. end
  138. function UIShopCtr:GetCurSelectIdx()
  139. return self.curSelectIdx
  140. end
  141. function UIShopCtr:SetCurSelectIdx(newIdx)
  142. if self.curSelectIdx == newIdx then
  143. return false
  144. end
  145. self.curSelectIdx = newIdx
  146. self:RefreshCurShopData(true)
  147. return true
  148. end
  149. function UIShopCtr:GetCurShopCfgData()
  150. return self.curShopCfgData
  151. end
  152. function UIShopCtr:GetCurShopData()
  153. return self.curShopData
  154. end
  155. function UIShopCtr:GetRefreshCostInfo()
  156. local costNum, costCfgId
  157. if self.curShopData and self.curShopCfgData then
  158. local payForRefreshs = self.curShopCfgData.PayForRefresh
  159. local refreshNum = self.curShopData:GetRefreshCount() or 0
  160. if payForRefreshs and #payForRefreshs > 0 then
  161. refreshNum = Mathf.Clamp(refreshNum+1, 1, #payForRefreshs)
  162. local payForRefresh = payForRefreshs[refreshNum]
  163. if payForRefresh then
  164. costCfgId = payForRefresh[1]
  165. costNum = payForRefresh[2]
  166. end
  167. end
  168. end
  169. return costNum, costCfgId
  170. end
  171. function UIShopCtr:SendShopInfoReq()
  172. if not self.curShopData then
  173. return 398
  174. end
  175. if not ManagerContainer.DataMgr.ShopDataMgr:SendShopInfoReq(self.curShopData.shopId) then
  176. return 100007
  177. end
  178. return 0
  179. end
  180. function UIShopCtr:SendRefreshShopReq()
  181. if not self.curShopCfgData or not self.curShopData then
  182. return 398
  183. end
  184. -- if self.curShopCfgData.ShopType ~= 2 then
  185. -- return 397
  186. -- end
  187. -- local refreshNum = self.curShopData:GetRefreshCount() or 0
  188. -- local refreshMaxNum = self.curShopCfgData.RefreshNum or 0
  189. -- if (refreshMaxNum - refreshNum) <= 0 then
  190. -- return 395
  191. -- end
  192. -- local costNum, costCfgId = self:GetRefreshCostInfo()
  193. -- local ownNum = CommonUtil.GetOwnResCountByItemId(costCfgId)
  194. -- if ownNum < costNum then
  195. -- return 396, costCfgId
  196. -- end
  197. if not ManagerContainer.DataMgr.ShopDataMgr:SendRefreshShopReq(self.curShopData.shopId) then
  198. return 100007
  199. end
  200. return 0
  201. end
  202. return UIShopCtr