QuickUseProxy.lua 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. local RemoteProxy = requireProxy("remote/RemoteProxy")
  2. local QuickUseProxy = class("QuickUseProxy", RemoteProxy)
  3. QuickUseProxy.NAME = global.ProxyTable.QuickUseProxy
  4. --数据全部来自背包
  5. function QuickUseProxy:ctor()
  6. QuickUseProxy.super.ctor(self)
  7. self.ItemBelongType = global.MMO.ITEM_FROM_BELONG_QUICKUSE
  8. self.quickUseData = {} --具体数据 key:pos v:itemData
  9. self.quickUseList = {} --初始化的时候读取本地列表用
  10. self.quickUseSize = global.MMO.QUICK_USE_SIZE
  11. self.quickUsableList = nil
  12. self:InitQuickUseHistoryData()
  13. end
  14. function QuickUseProxy:InitQuickUseHistoryData()
  15. local historyData = self:GetQuickUsePosMarkData()
  16. if historyData and next(historyData) then
  17. for k, v in ipairs(historyData) do
  18. self.quickUseList[k] = v
  19. end
  20. end
  21. for i = 1, self.quickUseSize do -- 自动补全
  22. if not self.quickUseList[i] then
  23. self.quickUseList[i] = 0
  24. end
  25. end
  26. local AttrConfigProxy = global.Facade:retrieveProxy(global.ProxyTable.AttrConfigProxy)
  27. local QuickUsable = AttrConfigProxy:GetItemsConfigQuickUsable()
  28. self.quickUsableList = QuickUsable
  29. end
  30. --历史数据修正
  31. function QuickUseProxy:RequireHistoryQuickyUseList()
  32. local newList = {}
  33. for i = 1, self.quickUseSize do
  34. newList[i] = 0
  35. end
  36. for k, v in pairs(self.quickUseList) do
  37. local data = self:GetQucikUseDataByMakeIndex(v)
  38. if data and k <= self.quickUseSize then -- 因为多平台原因 不同平台格子数变化
  39. newList[k] = v
  40. end
  41. end
  42. self.quickUseList = newList
  43. self:SetQuickPosMarkData()
  44. end
  45. function QuickUseProxy:CheckIsInQucikUseList(item)
  46. if item and item.MakeIndex then
  47. for k, v in pairs(self.quickUseList) do
  48. if v == item.MakeIndex then
  49. return k
  50. end
  51. end
  52. end
  53. return false
  54. end
  55. function QuickUseProxy:AutoAddBagItemToQuick(item, pos)
  56. if not item or next(item) == nil or not pos then
  57. return
  58. end
  59. local posData = self:GetQucikUseDataByPos(pos)
  60. if posData then
  61. return
  62. end
  63. local autoAddItemMakeIndex = nil
  64. local itemIndex = item.Index
  65. local itemData = nil
  66. local BagProxy = global.Facade:retrieveProxy(global.ProxyTable.Bag)
  67. local sameItems = BagProxy:GetItemDataByItemIndex(itemIndex)
  68. if sameItems and next(sameItems) then
  69. itemData = sameItems[1]
  70. autoAddItemMakeIndex = itemData.MakeIndex
  71. end
  72. if itemData and autoAddItemMakeIndex and autoAddItemMakeIndex ~= 0 then
  73. local Bag = global.Facade:retrieveProxy(global.ProxyTable.Bag)
  74. Bag:DelItemData(itemData) -- 从背包中移除
  75. self:SetQuickUsePosData(pos, itemData)
  76. end
  77. end
  78. function QuickUseProxy:CheckQuickUseHasEmpty()
  79. local size = self.quickUseSize or 6
  80. for i = 1, size do
  81. if not self.quickUseList[i] or self.quickUseList[i] <= 0 then
  82. return i
  83. end
  84. end
  85. return false
  86. end
  87. function QuickUseProxy:CheckItemCanAddToQuickUse(itemData)
  88. if not itemData or not next(itemData) then
  89. return false
  90. end
  91. local itemStdMode = itemData.StdMode
  92. if itemStdMode and self.quickUsableList[itemStdMode] then
  93. return true
  94. end
  95. return false
  96. end
  97. function QuickUseProxy:GetQucikUsePosByMakeIndex(MakeIndex)
  98. if not MakeIndex then
  99. return false
  100. end
  101. local size = self.quickUseSize or 6
  102. for i = 1, size do
  103. if self.quickUseList[i] and self.quickUseList[i] == MakeIndex then
  104. return i
  105. end
  106. end
  107. return false
  108. end
  109. function QuickUseProxy:GetItemCountByIndex(Index)
  110. local itemList = 0
  111. if not Index then
  112. return itemList
  113. end
  114. for k, v in pairs(self.quickUseData) do
  115. if v.Index == Index then
  116. local count = v.OverLap and v.OverLap or 1
  117. itemList = itemList + count
  118. end
  119. end
  120. return itemList
  121. end
  122. function QuickUseProxy:GetQucikUseDataByMakeIndex(MakeIndex)
  123. if not MakeIndex then
  124. return false
  125. end
  126. for k, v in pairs(self.quickUseData) do
  127. if v.MakeIndex == MakeIndex then
  128. return v
  129. end
  130. end
  131. return false
  132. end
  133. function QuickUseProxy:GetQucikUseDataByPos(position)
  134. if not position then
  135. return false
  136. end
  137. return self.quickUseData[position]
  138. end
  139. function QuickUseProxy:GetQuickUseData()
  140. return self.quickUseData
  141. end
  142. function QuickUseProxy:GetQuickUseLocalList()
  143. return self.quickUseList
  144. end
  145. function QuickUseProxy:GetQucikUseDataByIndex(Index)
  146. local itemList = {}
  147. if not Index then
  148. return itemList
  149. end
  150. for k, v in pairs(self.quickUseData) do
  151. if v.Index == Index then
  152. table.insert(itemList, v)
  153. end
  154. end
  155. return itemList
  156. end
  157. function QuickUseProxy:GetQuickUseItemNum(Index, famlilar)
  158. Index = Index or 0
  159. local count = 0
  160. local ItemConfigProxy = global.Facade:retrieveProxy(global.ProxyTable.ItemConfigProxy)
  161. local isBind, bindIndex = ItemConfigProxy:CheckItemBind(Index)
  162. local function GetQucikUseNumByIndex(ItemIndex)
  163. local itemCount = 0
  164. local itemList = self:GetQucikUseDataByIndex(ItemIndex)
  165. if itemList and next(itemList) then
  166. for k, v in pairs(itemList) do
  167. local itemNum = v.OverLap > 0 and v.OverLap or 1
  168. itemCount = itemCount + itemNum
  169. end
  170. end
  171. return itemCount
  172. end
  173. local myCount = GetQucikUseNumByIndex(Index)
  174. local famlilarCount = 0
  175. if isBind and bindIndex ~= Index and famlilar then
  176. famlilarCount = GetQucikUseNumByIndex(bindIndex)
  177. end
  178. local totalCount = myCount + famlilarCount
  179. return totalCount
  180. end
  181. function QuickUseProxy:UpDateQuickUseItemData(item)
  182. if not item then
  183. return
  184. end
  185. local MakeIndex = item.MakeIndex
  186. local BagProxy = global.Facade:retrieveProxy(global.ProxyTable.Bag)
  187. local pos = self:GetQucikUsePosByMakeIndex(MakeIndex)
  188. if pos and self.quickUseData[pos] then
  189. local oldNum = self.quickUseData[pos].OverLap or 1
  190. local newNum = item.OverLap or 1
  191. local changeNum = newNum - oldNum
  192. if changeNum ~= 0 then
  193. BagProxy:ShowGetOrCostItems(changeNum, item.Name)
  194. end
  195. self.quickUseData[pos] = item
  196. local msgData = {
  197. index = pos,
  198. itemData = item,
  199. isAdd = changeNum and changeNum > 0
  200. }
  201. global.Facade:sendNotification(global.NoticeTable.QuickUseItemChange, msgData)
  202. ssr.ssrBridge:OnQuickUseOperData({ opera = 3, param = msgData })
  203. SLBridge:onLUAEvent(LUA_EVENT_QUICKUSE_DATA_OPER, {opera = 3, param = msgData})
  204. end
  205. end
  206. function QuickUseProxy:SetQuickUsePosData(pos, data, isDelete)
  207. self.quickUseData[pos] = (not isDelete) and data or nil
  208. self.quickUseList[pos] = (not isDelete) and data.MakeIndex or 0
  209. local ItemManagerProxy = global.Facade:retrieveProxy(global.ProxyTable.ItemManagerProxy)
  210. ItemManagerProxy:SetItemBelong(data.MakeIndex, (not isDelete) and self.ItemBelongType or nil)
  211. self:SetQuickPosMarkData()
  212. local msgData = {
  213. index = pos,
  214. itemData = data
  215. }
  216. local notice = isDelete and global.NoticeTable.QuickUseItemRmv or global.NoticeTable.QuickUseItemAdd
  217. global.Facade:sendNotification(notice, msgData)
  218. ssr.ssrBridge:OnQuickUseOperData({opera = isDelete and 2 or 1, param = msgData})
  219. SLBridge:onLUAEvent(LUA_EVENT_QUICKUSE_DATA_OPER, {opera = isDelete and 2 or 1, param = msgData})
  220. end
  221. --快捷栏
  222. function QuickUseProxy:GetQuickUsePosMarkData()
  223. local PosData = UserData:new("BagPosData")
  224. local proxy = global.Facade:retrieveProxy(global.ProxyTable.Login)
  225. local flag = proxy:GetSelectedRoleID() or "errorName"
  226. local clientData = PosData:getStringForKey("quickUse" .. flag)
  227. if not clientData or clientData == "" then
  228. return nil
  229. end
  230. local cjson = require("cjson")
  231. local lastJsonData = cjson.decode(clientData)
  232. return lastJsonData
  233. end
  234. function QuickUseProxy:SetQuickPosMarkData()
  235. local PosData = UserData:new("BagPosData")
  236. local proxy = global.Facade:retrieveProxy(global.ProxyTable.Login)
  237. local flag = proxy:GetSelectedRoleID() or "errorName"
  238. local cjson = require("cjson")
  239. local bagPosData = cjson.encode(self.quickUseList)
  240. local clientData = PosData:setStringForKey("quickUse" .. flag, bagPosData)
  241. end
  242. function QuickUseProxy:GetQuickUseItemTotalCount()
  243. local size = self.quickUseSize or 6
  244. local count = 0
  245. for i = 1, size do
  246. if self.quickUseList[i] and self.quickUseList[i] > 0 then
  247. count = count + 1
  248. end
  249. end
  250. return count
  251. end
  252. function QuickUseProxy:GetQuickUseItemLeftNum()
  253. local size = self.quickUseSize or 6
  254. local count = self:GetQuickUseItemTotalCount()
  255. return size - count
  256. end
  257. function QuickUseProxy:SetQuickUseSize(num)
  258. if num and tonumber(num) then
  259. num = math.max(math.min(num, 6), 0)
  260. self.quickUseSize = tonumber(num)
  261. local BagProxy = global.Facade:retrieveProxy(global.ProxyTable.Bag)
  262. if BagProxy:GetServerBagNum() ~= 0 then
  263. local maxBag = BagProxy:GetServerBagNum() + global.MMO.QUICK_USE_SIZE - self.quickUseSize
  264. BagProxy:SetMaxBag(maxBag)
  265. end
  266. end
  267. end
  268. function QuickUseProxy:GetQuickUseSize(num)
  269. return self.quickUseSize
  270. end
  271. function QuickUseProxy:onRegister()
  272. QuickUseProxy.super.onRegister(self)
  273. end
  274. return QuickUseProxy