ShopDataMgr.lua 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. local ShopDataMgr = class("ShopDataMgr", require("DataBase"))
  2. local ShopData = require('Shop/ShopData')
  3. local SHOP_REQ_CD = 1000
  4. function ShopDataMgr:ctor()
  5. self.shopDataMap = {}
  6. end
  7. function ShopDataMgr:Clear()
  8. self.shopDataMap = {}
  9. end
  10. function ShopDataMgr:Destroy()
  11. self.shopDataMap = nil
  12. self.lastSendMsgTimeMap = nil
  13. self:UnRegisterNetEvents()
  14. end
  15. function ShopDataMgr:RegisterNetEvents()
  16. ManagerContainer.NetManager:NetRegister(ProtoMsgId.SC_SHOP_INFO_ACK, self.OnShopInfoAck, self)
  17. ManagerContainer.NetManager:NetRegister(ProtoMsgId.SC_SHOP_REFRESH_ACK, self.OnShopRefreshAck, self)
  18. ManagerContainer.NetManager:NetRegister(ProtoMsgId.SC_SHOP_BUY_ITEM_ACK, self.OnShopBuyItemAck, self)
  19. ManagerContainer.NetManager:NetRegister(ProtoMsgId.SC_RED_BAG_EXCHANGE_ACK, self.OnRedBagExchangeAck, self)
  20. end
  21. function ShopDataMgr:UnRegisterNetEvents()
  22. ManagerContainer.NetManager:UnRegisterPbIdCallback(ProtoMsgId.SC_SHOP_INFO_ACK)
  23. ManagerContainer.NetManager:UnRegisterPbIdCallback(ProtoMsgId.SC_SHOP_REFRESH_ACK)
  24. ManagerContainer.NetManager:UnRegisterPbIdCallback(ProtoMsgId.SC_SHOP_BUY_ITEM_ACK)
  25. end
  26. function ShopDataMgr:OnShopInfoAck(data)
  27. --LogError('[wboy] SC_SHOP_INFO_ACK '.. Inspect(data))
  28. if ManagerContainer.NetManager:IsErrorData(data) then
  29. return
  30. end
  31. local shopDataMsg = data.shop_data
  32. if not shopDataMsg then return end
  33. local shopId = shopDataMsg.goods_type
  34. local shopData = self.shopDataMap[shopId]
  35. --LogError('[wboy] SC_SHOP_INFO_ACK '.. Inspect(self.shopDataMap))
  36. if not shopData then
  37. shopData = ShopData:new()
  38. self.shopDataMap[shopId] = shopData
  39. end
  40. shopData:SetData(shopDataMsg)
  41. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.SHOP_DATA_CHANGED, shopId)
  42. if shopId == 1 then
  43. self:ReportEnterShop(true)
  44. end
  45. end
  46. function ShopDataMgr:ReportEnterShop(success)
  47. if not SDKMgr.Instance:IsReportAction() then
  48. return
  49. end
  50. local datas = System.Collections.Generic.Dictionary_object_object()
  51. datas:Add('event', 'mj_store')
  52. datas:Add('is_achieve', (success and 1 or 0))
  53. --SDKMgr.Instance:ReportAction(datas)
  54. end
  55. function ShopDataMgr:OnShopRefreshAck(data)
  56. -- LogError('[wboy] SC_SHOP_REFRESH_ACK '.. Inspect(data))
  57. if ManagerContainer.NetManager:IsErrorData(data) then
  58. return
  59. end
  60. local shopDataMsg = data.shop_data
  61. if not shopDataMsg then return end
  62. local shopId = shopDataMsg.goods_type
  63. local shopData = self.shopDataMap[shopId]
  64. if not shopData then
  65. shopData = ShopData:new()
  66. self.shopDataMap[shopId] = shopData
  67. end
  68. shopData:SetData(shopDataMsg)
  69. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.SHOP_DATA_CHANGED, shopId)
  70. end
  71. function ShopDataMgr:OnShopBuyItemAck(data)
  72. --LogError('[wboy] SC_SHOP_BUY_ITEM_ACK '.. Inspect(data))
  73. if ManagerContainer.NetManager:IsErrorData(data) then
  74. return
  75. end
  76. local shopId = data.goods_type
  77. local shopData = self.shopDataMap[shopId]
  78. if shopData then
  79. shopData:RefreshBuyInfoData(data)
  80. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.SHOP_DATA_CHANGED, shopId)
  81. end
  82. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.SHOP_BUY_SUCCESS)
  83. local itemList = data.item_list
  84. if itemList then
  85. local itemLength = #itemList
  86. if itemLength > 0 then
  87. local addItemMap = {}
  88. local cfgId, addNum, item
  89. for i = 1, itemLength do
  90. item = itemList[i]
  91. cfgId = item.key
  92. addNum = item.value
  93. if addItemMap[cfgId] then
  94. addItemMap[cfgId] = addItemMap[cfgId] + addNum
  95. else
  96. addItemMap[cfgId] = addNum
  97. end
  98. end
  99. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.EID_EQUIP_AND_ITEM_ADD, addItemMap)
  100. end
  101. end
  102. end
  103. function ShopDataMgr:SendShopInfoReq(shopId)
  104. if self:IsCanSend('ShopInfo' .. tostring(shopId)) then
  105. ManagerContainer.NetManager:SendMessage(ProtoMsgId.CS_SHOP_INFO_REQ, {goods_type = shopId})
  106. return true
  107. end
  108. return false
  109. end
  110. function ShopDataMgr:SendRefreshShopReq(shopId)
  111. if self:IsCanSend(2) then
  112. ManagerContainer.NetManager:SendMessage(ProtoMsgId.CS_SHOP_REFRESH_REQ, {goods_type = shopId})
  113. return true
  114. end
  115. return false
  116. end
  117. function ShopDataMgr:SendShopBuyItemReq(shopId, goodsId, goodsNum)
  118. if self:IsCanSend(3) then
  119. ManagerContainer.NetManager:SendMessage(ProtoMsgId.CS_SHOP_BUY_ITEM_REQ, {goods_type = shopId, goods_id = goodsId, goods_num = goodsNum})
  120. return true
  121. end
  122. return false
  123. end
  124. function ShopDataMgr:SendRedBagExchangeReq()
  125. if self:IsCanSend(1000) then
  126. ManagerContainer.NetManager:SendMessage(ProtoMsgId.CS_RED_BAG_EXCHANGE_REQ, {})
  127. return true
  128. end
  129. return false
  130. end
  131. function ShopDataMgr:OnRedBagExchangeAck(data)
  132. if data.error ~= Enum.NetErrorCode.ERROR_OK then return end
  133. CommonUtil.ACKShowRewardList(data.reward)
  134. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.RED_ENVELOPE_CASH_EXCHANGE_SUCCESS)
  135. end
  136. function ShopDataMgr:GetShopData(shopId, forceRefresh)
  137. local shopData = self.shopDataMap[shopId]
  138. if shopData then
  139. -- 数据是否过期了,如果过期了,需要去服务器上请求新数据
  140. if forceRefresh or ManagerContainer.LuaTimerMgr:GetRemainSeconds(shopData:GetNeedRefreshDataTime()) <= 0 then
  141. self:SendShopInfoReq(shopId)
  142. end
  143. else
  144. self:SendShopInfoReq(shopId)
  145. end
  146. return shopData
  147. end
  148. function ShopDataMgr:IsCanSend(key, cdTime)
  149. local curTime = ManagerContainer.LuaTimerMgr:CurLuaServerTime()
  150. if not self.lastSendMsgTimeMap then
  151. self.lastSendMsgTimeMap = {}
  152. self.lastSendMsgTimeMap[key] = curTime
  153. return true
  154. end
  155. local lastTime = self.lastSendMsgTimeMap[key]
  156. if lastTime then
  157. local cd = cdTime or SHOP_REQ_CD
  158. if (curTime - lastTime) < cd then
  159. return false
  160. end
  161. end
  162. self.lastSendMsgTimeMap[key] = curTime
  163. return true
  164. end
  165. return ShopDataMgr