BagProxy.lua 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  1. local RemoteProxy = requireProxy("remote/RemoteProxy")
  2. local BagProxy = class("BagProxy", RemoteProxy)
  3. BagProxy.NAME = global.ProxyTable.Bag
  4. local tinsert = table.insert
  5. local function MAKE_OPER_DATA(item, isHad, number)
  6. local operator = {}
  7. operator.item = item
  8. operator.isHad = isHad
  9. operator.change = number
  10. operator.MakeIndex = item.MakeIndex
  11. return operator
  12. end
  13. BagProxy.BAG_SELECT_TYPE = {
  14. TYPE_ALL = 0, -- 全部
  15. TYPE_EQUIP = 1, -- 装备
  16. TYPE_EXP = 2, -- 经验
  17. TYPE_REIN = 3, -- 转生
  18. TYPE_PET = 4, -- 宠物
  19. TYPE_OTHER = 5 -- 其他
  20. }
  21. function BagProxy:ctor()
  22. BagProxy.super.ctor(self)
  23. local BagVO = requireVO("remote/BagVO")
  24. self.VOdata = BagVO.new()
  25. self._maxBagNum = 0 -- 服务端发的最大背包格子数(不包括快捷栏)
  26. self._init_count = 0 -- 初始数量
  27. self._curSelPage = 1 -- 当前选中切页
  28. self._prompt_data = nil -- cfg_game_data的prompt
  29. self._selfDropItems = {} -- 自己丢弃的物品
  30. self._on_sell_repaire = nil -- 正在交易或者修理中物品
  31. self._collimator_MakeIndex = nil -- 记录背包选中的准星道具
  32. self._reconnect = false -- 是否重连
  33. self._customAttrs = {} -- 自定义属性 key: makeindex value: attrs
  34. self._receiveBagData = false -- 是否已经收到背包数据
  35. self._bigPerPageNum = nil -- 大背包单页大小
  36. self._bagFullInterval = tonumber(global.ConstantConfig.bag_full_interval) or 0.5 -- 默认给0.5
  37. self._bagFullTimerID = nil -- 背包满定时器
  38. end
  39. function BagProxy:SetServerBagNum(maxBag)
  40. self._maxBagNum = maxBag
  41. end
  42. function BagProxy:GetServerBagNum()
  43. return self._maxBagNum
  44. end
  45. function BagProxy:SetMaxBag(maxBag)
  46. self.VOdata:SetMaxBag(maxBag)
  47. self.VOdata:SetNewPosMark()
  48. end
  49. function BagProxy:GetMaxBag()
  50. return self.VOdata:GetMaxBag()
  51. end
  52. function BagProxy:SetCurPage(curPage)
  53. self._curSelPage = curPage
  54. end
  55. function BagProxy:GetCurPage(curPage)
  56. return self._curSelPage
  57. end
  58. function BagProxy:GetSelfDropItems()
  59. return self._selfDropItems
  60. end
  61. -- item提示:类似右上角满 获取cfg_game_data表的prompt字段的数据
  62. function BagProxy:GetPromptGameData()
  63. if not self._prompt_data then
  64. self._prompt_data = {}
  65. local prompt = SL:GetMetaValue("GAME_DATA","prompt")
  66. if prompt and string.len(prompt) then
  67. local promptArray = string.split(prompt, "|")
  68. for _, v in ipairs(promptArray) do
  69. if v and string.len(v) > 0 then
  70. local valueArray = string.split(v, "#")
  71. tinsert(self._prompt_data, {
  72. resPath = valueArray[1],
  73. posX = valueArray[2] and tonumber(valueArray[2]) or nil,
  74. posY = valueArray[3] and tonumber(valueArray[3]) or nil,
  75. resScale = valueArray[4] and tonumber(valueArray[4]) or nil
  76. })
  77. end
  78. end
  79. end
  80. end
  81. if global.isWinPlayMode then
  82. return self._prompt_data[1] or {}
  83. end
  84. return self._prompt_data[2] or {}
  85. end
  86. function BagProxy:Inited(bool)
  87. self.VOdata:Inited(bool)
  88. end
  89. function BagProxy:SetReconnect(bool)
  90. self._reconnect = bool
  91. end
  92. function BagProxy:GetBagData()
  93. return self.VOdata:GetBagData()
  94. end
  95. function BagProxy:GetTotalItemCount()
  96. return self.VOdata:GetItemCount()
  97. end
  98. function BagProxy:isToBeFull(tips)
  99. if tips and self.VOdata:isFull() then
  100. global.Facade:sendNotification(global.NoticeTable.SystemTips, GET_STRING(800050))
  101. end
  102. return self.VOdata:isFull()
  103. end
  104. function BagProxy:CheckNeedSpace(itemID, itemCount, tips)
  105. local ItemConfigProxy = global.Facade:retrieveProxy(global.ProxyTable.ItemConfigProxy)
  106. local itemData = ItemConfigProxy:GetItemDataByIndex(itemID)
  107. local isLap = ItemConfigProxy:CheckItemOverLap(itemData)
  108. local needPos = itemCount
  109. if isLap then
  110. needPos = math.ceil(itemCount / itemData.OverLap)
  111. end
  112. local bagItemNum = self:GetTotalItemCount()
  113. local maxBag = self:GetMaxBag()
  114. local TradeProxy = global.Facade:retrieveProxy(global.ProxyTable.TradeProxy)
  115. local onTradingNum = TradeProxy:GetSelfItemNum()
  116. local totalBagItemNum = bagItemNum + onTradingNum
  117. if bagItemNum + needPos > maxBag then
  118. if tips then
  119. global.Facade:sendNotification(global.NoticeTable.SystemTips, GET_STRING(1021))
  120. end
  121. return false
  122. end
  123. return true
  124. end
  125. -- 通过唯一id获取在背包中的切页
  126. function BagProxy:GetBagPageByMakeIndex(MakeIndex)
  127. local posPage = nil
  128. MakeIndex = MakeIndex and tonumber(MakeIndex) or nil
  129. if MakeIndex then
  130. local pos = self:GetBagPosByMakeIndex(MakeIndex)
  131. if pos and type(pos) == "number" then
  132. posPage = math.ceil(pos / (self:GetBigPerPageNum() or global.MMO.MAX_ITEM_NUMBER))
  133. end
  134. end
  135. return posPage
  136. end
  137. function BagProxy:GetBigPerPageNum()
  138. local bag_row_col = SL:GetMetaValue("GAME_DATA", "bag_row_col_max")
  139. if not SL:GetMetaValue("WINPLAYMODE") or not bag_row_col or self._bigPerPageNum then
  140. return self._bigPerPageNum
  141. end
  142. local param = string.split(bag_row_col, "|")
  143. local col = tonumber(param[1]) or 8
  144. local row = tonumber(param[2]) or 5
  145. self._bigPerPageNum = col * row
  146. return self._bigPerPageNum
  147. end
  148. -- 通过唯一id获取在背包中的位置
  149. function BagProxy:GetBagPosByMakeIndex(MakeIndex)
  150. return self.VOdata:GetBagPosByMakeIndex(MakeIndex)
  151. end
  152. -- 通过在背包中的位置获取唯一id
  153. function BagProxy:GetMakeIndexByBagPos(pos)
  154. return self.VOdata:GetMakeIndexByBagPos(pos)
  155. end
  156. -- 通过唯一id获取物品数据
  157. function BagProxy:GetItemDataByMakeIndex(MakeIndex)
  158. return self.VOdata:GetItemByMakeIndex(MakeIndex)
  159. end
  160. -- 获取背包数据 startPos: 开始的位置 endPos: 结束的位置
  161. function BagProxy:GetBagDataByBagPos(startPos, endPos)
  162. return self.VOdata:GetBagDataByBagPos(startPos, endPos)
  163. end
  164. -- 获取新位置
  165. function BagProxy:NewItemPos()
  166. return self.VOdata:NewItemPos()
  167. end
  168. -- 修正历史数据与当前数据位置信息
  169. function BagProxy:AmendHistoryPos(data, sort, startIndex, endIndex)
  170. self.VOdata:AmendHistoryPos(data, sort, startIndex, endIndex)
  171. end
  172. -- 清理背包数据及位置
  173. function BagProxy:CleanBagPosData()
  174. self.VOdata:CleanBagPosData()
  175. end
  176. -- 通过itemIndex获取物品数据
  177. function BagProxy:GetItemDataByItemIndex(itemIndex)
  178. local someItems = {}
  179. if not itemIndex then
  180. return someItems
  181. end
  182. local items = self:GetBagData()
  183. for _, v in pairs(items) do
  184. if v.Index == itemIndex then
  185. tinsert(someItems, v)
  186. end
  187. end
  188. return someItems
  189. end
  190. -- 通过物品名称获取物品数据
  191. function BagProxy:GetItemDataByItemName(itemName)
  192. if not itemName then
  193. return nil
  194. end
  195. local someItems = {}
  196. local items = self:GetBagData()
  197. for _, v in pairs(items) do
  198. if v.Name == itemName then
  199. tinsert(someItems, v)
  200. end
  201. end
  202. if not next(someItems) then
  203. return nil
  204. end
  205. return someItems
  206. end
  207. -- 通过itemIndex获取物品数量 famlilar是否包含绑定数量
  208. function BagProxy:GetItemCountByIndex(Index, famlilar)
  209. Index = Index or 0
  210. local count = 0
  211. local ItemConfigProxy = global.Facade:retrieveProxy(global.ProxyTable.ItemConfigProxy)
  212. local isBind, bindIndex = ItemConfigProxy:CheckItemBind(Index)
  213. local myCount = self.VOdata:GetItemCountByIndex(Index)
  214. local famlilarCount = 0
  215. if isBind and bindIndex ~= Index and famlilar then
  216. famlilarCount = self.VOdata:GetItemCountByIndex(bindIndex)
  217. end
  218. local totalCount = myCount + famlilarCount
  219. return totalCount
  220. end
  221. -- 设置正在交易或修理中
  222. function BagProxy:SetOnSellOrRepaire(MakeIndex)
  223. self._on_sell_repaire = MakeIndex
  224. global.Facade:sendNotification(global.NoticeTable.Bag_Item_Pos_Change)
  225. end
  226. -- 获取是否在交易或修理
  227. function BagProxy:GetOnSellOrRepaire()
  228. return self._on_sell_repaire
  229. end
  230. -- 清理交易或修理
  231. function BagProxy:CleanOnSellOrRepaire()
  232. self._on_sell_repaire = nil
  233. global.Facade:sendNotification(global.NoticeTable.Bag_Item_Pos_Change)
  234. end
  235. -- 添加物品
  236. function BagProxy:AddItemData(item, noNotice)
  237. if not item then
  238. return
  239. end
  240. local itemPos = self.VOdata:GetHistoryPos(item.MakeIndex) or self:NewItemPos()
  241. if itemPos then
  242. self.VOdata:SetItemPosData(item.MakeIndex, itemPos)
  243. end
  244. if not noNotice then
  245. self:ShowGetOrCostItems(item.OverLap or 1, item.Name)
  246. end
  247. self.VOdata:AddItem(item, itemPos)
  248. local ItemManagerProxy = global.Facade:retrieveProxy(global.ProxyTable.ItemManagerProxy)
  249. ItemManagerProxy:SetItemBelong(item.MakeIndex, global.MMO.ITEM_FROM_BELONG_BAG)
  250. end
  251. -- 显示获得或消耗物品
  252. function BagProxy:ShowGetOrCostItems(diff, name)
  253. if self.VOdata:IsInited() then
  254. return
  255. end
  256. local nData = {}
  257. nData.name = name
  258. nData.num = math.abs(diff)
  259. local notice = diff > 0 and global.NoticeTable.ShowGetBagItem or global.NoticeTable.ShowCostItem
  260. global.Facade:sendNotification(notice, nData)
  261. end
  262. -- 增加物品并通知
  263. function BagProxy:AddItemDataAndNotice(item)
  264. self:AddItemData(item)
  265. local operator = {}
  266. operator.opera = global.MMO.Operator_Add
  267. operator.operID = {}
  268. tinsert(operator.operID, MAKE_OPER_DATA(item, false))
  269. ssr.ssrBridge:OnPreBagOperData(operator)
  270. global.Facade:sendNotification(global.NoticeTable.Bag_Oper_Data, operator)
  271. ssr.ssrBridge:OnBagOperData(operator)
  272. SLBridge:onLUAEvent(LUA_EVENT_BAG_ITEM_CHANGE, operator)
  273. -- 延迟通知
  274. self:DelayNotifyBagOper()
  275. end
  276. -- 延迟通知,部分系统不需要实时监听背包改变,并且不需要知道背包操作类型,节省性能
  277. function BagProxy:DelayNotifyBagOper()
  278. if self._delayNotifyTimerID then
  279. UnSchedule(self._delayNotifyTimerID)
  280. self._delayNotifyTimerID = nil
  281. end
  282. self._delayNotifyTimerID = PerformWithDelayGlobal(function()
  283. global.Facade:sendNotification(global.NoticeTable.Bag_Oper_Data_Delay)
  284. end, 0.5)
  285. end
  286. -- 删除物品
  287. function BagProxy:DelItemData(data, showTip, noCleanPos, noNotify, isBaitan)
  288. if not data or not next(data) then
  289. return
  290. end
  291. local MakeIndex = data.MakeIndex
  292. local itemHasPos = true
  293. local noPosData = self.VOdata:GetBagNoPosDataByMakeIndex(MakeIndex)
  294. local item = self:GetItemDataByMakeIndex(MakeIndex)
  295. if noPosData then
  296. itemHasPos = false
  297. item = noPosData
  298. end
  299. if item then
  300. if not self.VOdata:IsInited() then
  301. global.Facade:sendNotification(global.NoticeTable.Layer_Auto_Use_UnAttach, {
  302. id = item.MakeIndex
  303. })
  304. end
  305. local operator = {}
  306. operator.opera = global.MMO.Operator_Sub
  307. operator.operID = {}
  308. operator.isBaitan = isBaitan
  309. local operitem = self:BagOperItemByMakeIndex(MakeIndex, itemHasPos)
  310. tinsert(operator.operID, operitem)
  311. self.VOdata:SubItem(MakeIndex)
  312. if not noCleanPos then
  313. self.VOdata:CleanItemPosData(MakeIndex)
  314. end
  315. local ItemManagerProxy = global.Facade:retrieveProxy(global.ProxyTable.ItemManagerProxy)
  316. ItemManagerProxy:SetItemBelong(item.MakeIndex, nil)
  317. if not noNotify then
  318. ssr.ssrBridge:OnPreBagOperData(operator)
  319. global.Facade:sendNotification(global.NoticeTable.Bag_Oper_Data, operator)
  320. ssr.ssrBridge:OnBagOperData(operator)
  321. SLBridge:onLUAEvent(LUA_EVENT_BAG_ITEM_CHANGE, operator)
  322. -- 延迟通知
  323. self:DelayNotifyBagOper()
  324. end
  325. if showTip then
  326. self:ShowGetOrCostItems(-(item.OverLap or 1), item.Name)
  327. end
  328. -- 缓存中的数据自动补充到背包
  329. local noPosData = self.VOdata:GetBagNoPosData()
  330. if noPosData and next(noPosData) and itemHasPos then
  331. for _, v in pairs(noPosData) do
  332. if v and next(v) then
  333. self:DelItemData(v, nil, nil, true)
  334. self:AddItemDataAndNotice(v)
  335. break
  336. end
  337. end
  338. end
  339. -- 清理删除红点
  340. local RedDotProxy = global.Facade:retrieveProxy(global.ProxyTable.RedDotProxy)
  341. RedDotProxy:DeleteBagRedDot(MakeIndex)
  342. end
  343. end
  344. function BagProxy:BagOperItemByMakeIndex(MakeIndex, isShowInBag)
  345. local item = self:GetItemDataByMakeIndex(MakeIndex)
  346. if not isShowInBag then
  347. item = self.VOdata:GetBagNoPosDataByMakeIndex(MakeIndex)
  348. end
  349. local operator = {}
  350. if item then
  351. local MakeIndex = item.MakeIndex
  352. operator.MakeIndex = MakeIndex
  353. operator.item = item
  354. end
  355. return operator
  356. end
  357. -- 更改物品数据
  358. function BagProxy:ChangeItemData(item)
  359. local makeIndex = item.MakeIndex
  360. local newnum = item.OverLap or 1
  361. local diff = 0
  362. local data = self:GetItemDataByMakeIndex(makeIndex)
  363. if data then
  364. local oldnum = data.OverLap or 1
  365. diff = newnum - oldnum
  366. self.VOdata:ChangeItem(item)
  367. if diff ~= 0 then
  368. self:ShowGetOrCostItems(diff, item.Name)
  369. end
  370. end
  371. return diff
  372. end
  373. -- 清理背包
  374. function BagProxy:ClearItemData(isReconnect)
  375. local array = self.VOdata:GetBagData()
  376. for key, var in pairs(array) do
  377. self:DelItemData(var, nil, true, isReconnect)
  378. end
  379. local arrayNoPos = self.VOdata:GetBagNoPosData()
  380. for _, item in pairs(arrayNoPos) do
  381. self:DelItemData(item, nil, nil, isReconnect)
  382. end
  383. self.VOdata:ClearItemData()
  384. end
  385. -- 换位
  386. function BagProxy:ExchangePos(MakeIndex1, pos1, MakeIndex2, pos2)
  387. self.VOdata:CleanItemPosData(MakeIndex1)
  388. self.VOdata:CleanItemPosData(MakeIndex2)
  389. self.VOdata:SetItemPosData(MakeIndex1, pos1)
  390. self.VOdata:SetItemPosData(MakeIndex2, pos2)
  391. global.Facade:sendNotification(global.NoticeTable.Bag_Item_Pos_Change, {MakeIndex1, MakeIndex2})
  392. end
  393. -- 修改位置数据
  394. function BagProxy:SetItemPosData(MakeIndex, pos)
  395. self.VOdata:CleanItemPosData(MakeIndex)
  396. self.VOdata:SetItemPosData(MakeIndex, pos)
  397. global.Facade:sendNotification(global.NoticeTable.Bag_Item_Pos_Change, {MakeIndex})
  398. end
  399. -- 获取背包道具的使用状态(0:使用 1:使用准星道具 2:取消准星 3:不可使用)
  400. function BagProxy:GetOnBagItemUseState(data)
  401. if self._collimator_MakeIndex then
  402. if self._collimator_MakeIndex == -1 then -- 脚本
  403. return 1
  404. end
  405. if data and self._collimator_MakeIndex == data.MakeIndex then
  406. return 2
  407. end
  408. return 1
  409. end
  410. return 0
  411. end
  412. -- 设置准星道具
  413. function BagProxy:SetBagCollimator(data)
  414. self._collimator_MakeIndex = data
  415. end
  416. -- 获取准星道具
  417. function BagProxy:GetBagCollimator()
  418. return self._collimator_MakeIndex
  419. end
  420. -- 清理准星道具
  421. function BagProxy:ClearBagCollimator()
  422. self._collimator_MakeIndex = nil
  423. end
  424. -- 获取自定义属性数据
  425. function BagProxy:GetCustomAttrData(MakeIndex)
  426. if not MakeIndex then
  427. return
  428. end
  429. return self._customAttrs[MakeIndex]
  430. end
  431. function BagProxy:IsReceiveBagData()
  432. return self._receiveBagData
  433. end
  434. function BagProxy:InitBagSizeWithData(bagSize)
  435. self:SetServerBagNum(bagSize)
  436. local QuickUseProxy = global.Facade:retrieveProxy(global.ProxyTable.QuickUseProxy)
  437. local quickUseSize = QuickUseProxy:GetQuickUseSize()
  438. local maxBag = self:GetServerBagNum() + global.MMO.QUICK_USE_SIZE - quickUseSize
  439. self:SetMaxBag(maxBag)
  440. end
  441. -- 是否背包满等待
  442. function BagProxy:IsBagFullWait()
  443. -- body
  444. if self._bagFullTimerID then
  445. return true
  446. end
  447. return false
  448. end
  449. -- 背包满等待计时
  450. function BagProxy:BagFullTimerBegan()
  451. if not self._bagFullTimerID then
  452. local function callback()
  453. self._bagFullTimerID = nil
  454. end
  455. self._bagFullTimerID = PerformWithDelayGlobal(callback, self._bagFullInterval)
  456. end
  457. end
  458. -- 背包满计时重置
  459. function BagProxy:BagFullTimerReset()
  460. -- body
  461. if self._bagFullTimerID then
  462. UnSchedule( self._bagFullTimerID )
  463. end
  464. self._bagFullTimerID = nil
  465. end
  466. -- 背包数据初始化
  467. function BagProxy:handle_MSG_SC_RETURN_BAGDATA(msg)
  468. local msgHdr = msg:GetHeader()
  469. local data = ParseRawMsgToJson(msg)
  470. -- print("------------------------------------")
  471. -- print(" into here to get and set bag data")
  472. -- print("------------------------------------")
  473. if not data then
  474. return
  475. end
  476. self._receiveBagData = true
  477. local isInit = msgHdr.param1 == 1
  478. if isInit then
  479. self:Inited(true)
  480. end
  481. local changedData = {}
  482. local dataCount = 0
  483. for _, v in ipairs(data) do
  484. local changeData = ChangeItemServersSendDatas(v)
  485. tinsert(changedData, changeData)
  486. dataCount = dataCount + 1
  487. end
  488. data = changedData
  489. -- 清理缓存
  490. if self.VOdata:IsInited() then
  491. self._init_count = 0
  492. self:Inited(false)
  493. self:ClearItemData()
  494. if msgHdr.recog > 0 then
  495. self:InitBagSizeWithData(msgHdr.recog)
  496. end
  497. end
  498. local startIndex = self._init_count + 1
  499. local endIndex = self._init_count + dataCount
  500. -- 301 最后一次的数据
  501. if msgHdr.msgId == global.MsgType.MSG_SC_RETURN_BAGDATA_REMAINING then
  502. endIndex = self:GetMaxBag()
  503. end
  504. self._init_count = self._init_count + dataCount
  505. -- 修正本地位置信息数据
  506. self:AmendHistoryPos(data, false, startIndex, endIndex)
  507. local history = {}
  508. for key, var in pairs(self.VOdata:GetBagData()) do
  509. history[var.MakeIndex] = var.MakeIndex
  510. end
  511. local operator = {}
  512. operator.initbool = false
  513. operator.opera = global.MMO.Operator_Init
  514. operator.operID = {}
  515. local QuickUseProxy = global.Facade:retrieveProxy(global.ProxyTable.QuickUseProxy)
  516. for _, item in pairs(data) do
  517. local quickUsePos = QuickUseProxy:CheckIsInQucikUseList(item)
  518. if quickUsePos then -- 快捷栏道具
  519. QuickUseProxy:SetQuickUsePosData(quickUsePos, item)
  520. else
  521. self:AddItemData(item, true)
  522. if not history[item.MakeIndex] then
  523. tinsert(operator.operID, MAKE_OPER_DATA(item, false))
  524. end
  525. end
  526. end
  527. if msgHdr.msgId == global.MsgType.MSG_SC_RETURN_BAGDATA_REMAINING then -- 301 最后一次的数据
  528. QuickUseProxy:RequireHistoryQuickyUseList() -- 修正本地信息
  529. end
  530. if not self._reconnect then
  531. ssr.ssrBridge:OnPreBagOperData(operator)
  532. global.Facade:sendNotification(global.NoticeTable.Bag_Oper_Data, operator)
  533. ssr.ssrBridge:OnBagOperData(operator)
  534. SLBridge:onLUAEvent(LUA_EVENT_BAG_ITEM_CHANGE, operator)
  535. global.Facade:sendNotification(global.NoticeTable.QuickUseDataInit)
  536. ssr.ssrBridge:OnQuickUseOperData({
  537. opera = 0
  538. })
  539. SLBridge:onLUAEvent(LUA_EVENT_QUICKUSE_DATA_OPER, {opera = 0})
  540. -- 延迟通知
  541. self:DelayNotifyBagOper()
  542. end
  543. end
  544. -- 增加道具
  545. function BagProxy:handle_MSG_SC_BAG_ADD_ITEM(msg)
  546. local header = msg:GetHeader()
  547. local recog = header.recog
  548. local data = ParseRawMsgToJson(msg)
  549. -- print("------------------------------------")
  550. -- print(" into here to add bag item")
  551. -- print("------------------------------------")
  552. if not recog or not data or next(data) == nil then
  553. return
  554. end
  555. if recog == -1 then
  556. data = ChangeItemServersSendDatas(data)
  557. if _DEBUG then
  558. local _ditem = self:GetItemDataByMakeIndex(data.MakeIndex)
  559. if _ditem then
  560. print("ERROR BAG ITEM EXIST, CAN'T ADD IT AGAIN", data.MakeIndex)
  561. end
  562. end
  563. local QuickUseProxy = global.Facade:retrieveProxy(global.ProxyTable.QuickUseProxy)
  564. local isCanAddToQuick = QuickUseProxy:CheckItemCanAddToQuickUse(data)
  565. local quickUsePos = QuickUseProxy:CheckQuickUseHasEmpty()
  566. -- 快捷栏道具
  567. if isCanAddToQuick and quickUsePos then
  568. self:ShowGetOrCostItems(data.OverLap or 1, data.Name)
  569. QuickUseProxy:SetQuickUsePosData(quickUsePos, data)
  570. else
  571. self:AddItemData(data)
  572. local operator = {}
  573. operator.opera = global.MMO.Operator_Add
  574. operator.operID = {}
  575. tinsert(operator.operID, MAKE_OPER_DATA(data, false))
  576. ssr.ssrBridge:OnPreBagOperData(operator)
  577. global.Facade:sendNotification(global.NoticeTable.Bag_Oper_Data, operator)
  578. ssr.ssrBridge:OnBagOperData(operator)
  579. SLBridge:onLUAEvent(LUA_EVENT_BAG_ITEM_CHANGE, operator)
  580. -- 延迟通知
  581. self:DelayNotifyBagOper()
  582. end
  583. else
  584. data = ChangeItemServersSendDatas(data)
  585. local proxy = global.Facade:retrieveProxy(global.ProxyTable.Equip)
  586. data.Where = recog
  587. proxy:AddEquipData(data)
  588. end
  589. end
  590. -- 删除道具
  591. function BagProxy:handle_MSG_SC_BAG_DEL_ITEM(msg)
  592. local header = msg:GetHeader()
  593. local makeIndex = header.recog
  594. local ItemManagerProxy = global.Facade:retrieveProxy(global.ProxyTable.ItemManagerProxy)
  595. local itemBelong = ItemManagerProxy:GetItemBelong(makeIndex)
  596. if not itemBelong then
  597. print("delete item error, can't find item belong")
  598. return
  599. end
  600. if itemBelong == global.MMO.ITEM_FROM_BELONG_BAG then
  601. local itemData = self:GetItemDataByMakeIndex(makeIndex)
  602. if not itemData then
  603. print("delete item error, can't find item")
  604. return
  605. end
  606. self:DelItemData(itemData, true)
  607. elseif itemBelong == global.MMO.ITEM_FROM_BELONG_EQUIP then
  608. local EquipProxy = global.Facade:retrieveProxy(global.ProxyTable.Equip)
  609. local itemData = EquipProxy:GetEquipDataByMakeIndex(makeIndex)
  610. if not itemData then
  611. print("delete item error, can't find item")
  612. return
  613. end
  614. EquipProxy:DelEquipData(itemData)
  615. elseif itemBelong == global.MMO.ITEM_FROM_BELONG_QUICKUSE then
  616. local QuickUseProxy = global.Facade:retrieveProxy(global.ProxyTable.QuickUseProxy)
  617. local itemData = QuickUseProxy:GetQucikUseDataByMakeIndex(makeIndex)
  618. if not itemData then
  619. print("delete item error, can't find item")
  620. return
  621. end
  622. self:ShowGetOrCostItems(-(itemData.OverLap or 1), itemData.Name)
  623. -- 自动补充
  624. local pos = QuickUseProxy:GetQucikUsePosByMakeIndex(itemData.MakeIndex)
  625. QuickUseProxy:SetQuickUsePosData(pos, itemData, true)
  626. local function delayAutoAddd()
  627. QuickUseProxy:AutoAddBagItemToQuick(itemData, pos)
  628. end
  629. PerformWithDelayGlobal(delayAutoAddd, 0.5)
  630. elseif itemBelong == global.MMO.ITEM_FROM_BELONG_STALL then
  631. local StallProxy = global.Facade:retrieveProxy(global.ProxyTable.StallProxy)
  632. StallProxy:PutOutItem(makeIndex, true)
  633. end
  634. self:BagFullTimerReset()
  635. -- 清理一下自定义属性
  636. if self._customAttrs[makeIndex] then
  637. self._customAttrs[makeIndex] = nil
  638. SLBridge:onLUAEvent(LUA_EVENT_ITEM_CUSTOM_ATTR, {
  639. MakeIndex = makeIndex,
  640. data = nil
  641. })
  642. end
  643. end
  644. -- 更新道具
  645. function BagProxy:handle_MSG_SC_BAG_UPDATE_ITEM(msg)
  646. local data = ParseRawMsgToJson(msg)
  647. -- print("------------------------------------")
  648. -- print(" into here to update bag item")
  649. -- print("------------------------------------")
  650. if not data or next(data) == nil then
  651. return
  652. end
  653. local itemMakeIndex = data.MakeIndex or data.makeindex
  654. local ItemManagerProxy = global.Facade:retrieveProxy(global.ProxyTable.ItemManagerProxy)
  655. local itemBelong = ItemManagerProxy:GetItemBelong(itemMakeIndex)
  656. if not itemBelong then
  657. return
  658. end
  659. data = ChangeItemServersSendDatas(data)
  660. if global.MMO.ITEM_FROM_BELONG_BAG == itemBelong then
  661. local operator = {}
  662. operator.opera = global.MMO.Operator_Change
  663. operator.operID = {}
  664. local diff = self:ChangeItemData(data)
  665. tinsert(operator.operID, MAKE_OPER_DATA(data, true, diff))
  666. ssr.ssrBridge:OnPreBagOperData(operator)
  667. global.Facade:sendNotification(global.NoticeTable.Bag_Oper_Data, operator)
  668. ssr.ssrBridge:OnBagOperData(operator)
  669. SLBridge:onLUAEvent(LUA_EVENT_BAG_ITEM_CHANGE, operator)
  670. -- 延迟通知
  671. self:DelayNotifyBagOper()
  672. elseif global.MMO.ITEM_FROM_BELONG_EQUIP == itemBelong then
  673. local proxy = global.Facade:retrieveProxy(global.ProxyTable.Equip)
  674. proxy:ChangeEquipData(data, true, true)
  675. elseif itemBelong == global.MMO.ITEM_FROM_BELONG_QUICKUSE then
  676. local QuickUseProxy = global.Facade:retrieveProxy(global.ProxyTable.QuickUseProxy)
  677. QuickUseProxy:UpDateQuickUseItemData(data)
  678. end
  679. -- for item update.
  680. local updateData = {
  681. from = itemBelong,
  682. item = data
  683. }
  684. global.Facade:sendNotification(global.NoticeTable.ItemUpdate, updateData)
  685. end
  686. function BagProxy:handle_MSG_SC_BAG_ITEM_USE_SUCCESS(msg)
  687. local header = msg:GetHeader()
  688. local ItemIndex = header.recog
  689. end
  690. -- 道具使用失败
  691. function BagProxy:handle_MSG_SC_BAG_ITEM_USE_FAILED(msg)
  692. print("道具使用失败消息")
  693. local header = msg:GetHeader()
  694. local makeIndex = header.recog
  695. local itemData = self:GetItemDataByMakeIndex(makeIndex)
  696. if itemData and itemData.StdMode == 49 then
  697. -- 49类型道具使用失败提醒 0关闭 非0开启
  698. if SL:GetMetaValue("GAME_DATA","Pearl_on_off") ~= 0 then
  699. global.Facade:sendNotification(global.NoticeTable.SystemTips, GET_STRING(50000321))
  700. end
  701. end
  702. end
  703. -- 道具拾取失败 -1背包满 -2负重满 -3物品禁止拾取 -99假道具拾取
  704. function BagProxy:handle_MSG_SC_DROP_ITEM_GET_FAILED(msg)
  705. local header = msg:GetHeader()
  706. local recog = header.recog
  707. if recog == -1 then
  708. self:BagFullTimerBegan()
  709. global.Facade:sendNotification(global.NoticeTable.SystemTips, GET_STRING(800050))
  710. elseif recog == -2 then
  711. global.Facade:sendNotification(global.NoticeTable.SystemTips, GET_STRING(800052))
  712. elseif recog == -3 then
  713. global.Facade:sendNotification(global.NoticeTable.SystemTips, GET_STRING(800056))
  714. elseif recog == -99 then
  715. local mapX = header.param1
  716. local mapY = header.param2
  717. local dItems = global.dropItemManager:FindDropItemAllInMapXY(mapX, mapY)
  718. if not dItems then
  719. return
  720. end
  721. for k, dItem in pairs(dItems) do
  722. if dItem:GetPickState() >= 1 or dItem:IsPickTimeout() then --移除
  723. global.gameMapController:rmvDropItem(dItem:GetID())
  724. end
  725. end
  726. end
  727. end
  728. -- 丢弃道具成功
  729. function BagProxy:handle_MSG_SC_DROP_ITEM_SUCCESS(msg)
  730. local header = msg:GetHeader()
  731. local MakeIndex = header.recog
  732. if MakeIndex then
  733. tinsert(self._selfDropItems, MakeIndex)
  734. if #self._selfDropItems > 100 then -- 记录自己丢弃的物品 挂机拾取的时候忽略
  735. table.remove(self._selfDropItems, 1)
  736. end
  737. local ItemManagerProxy = global.Facade:retrieveProxy(global.ProxyTable.ItemManagerProxy)
  738. local belong = ItemManagerProxy:GetItemBelong(MakeIndex)
  739. if global.MMO.ITEM_FROM_BELONG_BAG == belong then
  740. -- 背包中的重新刷出来
  741. local data = {
  742. dropping = {
  743. MakeIndex = MakeIndex,
  744. state = 1
  745. }
  746. }
  747. global.Facade:sendNotification(global.NoticeTable.Bag_State_Change, data)
  748. elseif global.MMO.ITEM_FROM_BELONG_QUICKUSE == belong then
  749. local QuickUseProxy = global.Facade:retrieveProxy(global.ProxyTable.QuickUseProxy)
  750. local itemPos = QuickUseProxy:GetQucikUsePosByMakeIndex(MakeIndex)
  751. local data = {
  752. pos = itemPos,
  753. state = 1
  754. }
  755. global.Facade:sendNotification(global.NoticeTable.QuickUseItemRefresh, data)
  756. ssr.ssrBridge:OnQuickUseOperData({
  757. opera = 4,
  758. param = data
  759. })
  760. end
  761. end
  762. local AutoProxy = global.Facade:retrieveProxy(global.ProxyTable.Auto)
  763. AutoProxy:ClearAutoMining()
  764. end
  765. -- 丢弃道具失败
  766. function BagProxy:handle_MSG_SC_DROP_ITEM_FAILED(msg)
  767. local header = msg:GetHeader()
  768. local MakeIndex = header.recog
  769. local ItemManagerProxy = global.Facade:retrieveProxy(global.ProxyTable.ItemManagerProxy)
  770. local belong = ItemManagerProxy:GetItemBelong(MakeIndex)
  771. if global.MMO.ITEM_FROM_BELONG_BAG == belong then
  772. -- 背包中的重新刷出来
  773. local data = {
  774. dropping = {
  775. MakeIndex = MakeIndex,
  776. state = 1
  777. }
  778. }
  779. global.Facade:sendNotification(global.NoticeTable.Bag_State_Change, data)
  780. elseif global.MMO.ITEM_FROM_BELONG_QUICKUSE == belong then
  781. local QuickUseProxy = global.Facade:retrieveProxy(global.ProxyTable.QuickUseProxy)
  782. local itemPos = QuickUseProxy:GetQucikUsePosByMakeIndex(MakeIndex)
  783. local data = {
  784. pos = itemPos,
  785. state = 1
  786. }
  787. global.Facade:sendNotification(global.NoticeTable.QuickUseItemRefresh, data)
  788. ssr.ssrBridge:OnQuickUseOperData({
  789. opera = 4,
  790. param = data
  791. })
  792. end
  793. end
  794. -- 移动到英雄背包失败
  795. function BagProxy:handle_MSG_SC_HUMBAG_TO_HEROBAG_FAIL(msg)
  796. local header = msg:GetHeader()
  797. local MakeIndex = header.recog
  798. dump(header, "handle_MSG_SC_HUMBAG_TO_HEROBAG_FAIL")
  799. local ItemManagerProxy = global.Facade:retrieveProxy(global.ProxyTable.ItemManagerProxy)
  800. local belong = ItemManagerProxy:GetItemBelong(MakeIndex)
  801. if global.MMO.ITEM_FROM_BELONG_BAG == belong then
  802. -- 背包中的重新刷出来
  803. local data = {
  804. dropping = {
  805. MakeIndex = MakeIndex,
  806. state = 1
  807. }
  808. }
  809. global.Facade:sendNotification(global.NoticeTable.Bag_State_Change, data)
  810. elseif global.MMO.ITEM_FROM_BELONG_QUICKUSE == belong then
  811. local QuickUseProxy = global.Facade:retrieveProxy(global.ProxyTable.QuickUseProxy)
  812. local itemPos = QuickUseProxy:GetQucikUsePosByMakeIndex(MakeIndex)
  813. local data = {
  814. pos = itemPos,
  815. state = 1
  816. }
  817. global.Facade:sendNotification(global.NoticeTable.QuickUseItemRefresh, data)
  818. ssr.ssrBridge:OnQuickUseOperData({
  819. opera = 4,
  820. param = data
  821. })
  822. end
  823. end
  824. -- 整理背包
  825. function BagProxy:handle_MSG_SC_RESET_BAG_POS(msg)
  826. local QuickUseProxy = global.Facade:retrieveProxy(global.ProxyTable.QuickUseProxy)
  827. local bagData = self:GetBagData()
  828. for _, v in pairs(bagData) do
  829. local isCanAddToQuick = QuickUseProxy:CheckItemCanAddToQuickUse(v)
  830. local quickUsePos = QuickUseProxy:CheckQuickUseHasEmpty()
  831. -- 快捷栏道具
  832. if isCanAddToQuick and quickUsePos then
  833. QuickUseProxy:AutoAddBagItemToQuick(v, quickUsePos)
  834. end
  835. end
  836. local newData = self:GetBagData()
  837. self:CleanBagPosData()
  838. self:AmendHistoryPos(newData, true)
  839. global.Facade:sendNotification(global.NoticeTable.Bag_Pos_Reset)
  840. SLBridge:onLUAEvent(LUA_EVENT_REF_ITEM_LIST)
  841. end
  842. -- 开启准星
  843. function BagProxy:handle_MSG_SM_COLLIMATOR_RESPONSE(msg)
  844. local jsonData = ParseRawMsgToJson(msg)
  845. if jsonData and jsonData.UseMakeIndex then
  846. if jsonData.UseMakeIndex > -1 then
  847. self:SetBagCollimator(jsonData.UseMakeIndex)
  848. global.Facade:sendNotification(global.NoticeTable.Bag_Item_Collimator, jsonData.UseMakeIndex)
  849. elseif jsonData.UseMakeIndex == -1 then
  850. self:SetBagCollimator(jsonData.UseMakeIndex)
  851. global.Facade:sendNotification(global.NoticeTable.Layer_Sight_Bead_Show)
  852. end
  853. end
  854. end
  855. function BagProxy:CheckNeedRefreshEquipShow(makeIndex)
  856. if not makeIndex then
  857. return
  858. end
  859. local ItemManagerProxy = global.Facade:retrieveProxy(global.ProxyTable.ItemManagerProxy)
  860. local itemBelong = ItemManagerProxy:GetItemBelong(makeIndex)
  861. if not itemBelong then
  862. return
  863. end
  864. if itemBelong == global.MMO.ITEM_FROM_BELONG_EQUIP then
  865. local EquipProxy = global.Facade:retrieveProxy(global.ProxyTable.Equip)
  866. local itemData = EquipProxy:GetEquipDataByMakeIndex(makeIndex)
  867. if not itemData then
  868. return
  869. end
  870. local makeIndex = itemData.MakeIndex
  871. local where = itemData.Where
  872. local sData = {}
  873. sData.MakeIndex = makeIndex
  874. sData.Where = where
  875. sData.opera = global.MMO.Operator_Change
  876. sData.bagDelayUpdate = nil
  877. sData.isChangeLook = true
  878. global.Facade:sendNotification(global.NoticeTable.PlayEquip_Oper_Data, sData)
  879. SLBridge:onLUAEvent(LUA_EVENT_PLAYER_EQUIP_CHANGE, sData)
  880. end
  881. end
  882. -- 物品自定义属性
  883. function BagProxy:handle_MSG_SC_ITEM_PARAMS(msg)
  884. local jsonData = ParseRawMsgToJson(msg)
  885. if not jsonData then
  886. return
  887. end
  888. for i,v in pairs(jsonData) do
  889. self._customAttrs[v.m] = v.p
  890. SLBridge:onLUAEvent(LUA_EVENT_ITEM_CUSTOM_ATTR, {
  891. MakeIndex = v.m,
  892. data = v.p
  893. })
  894. if SL:GetMetaValue("GAME_DATA", "CustomAttrUpdatePlayerEquip") == 1 then
  895. self:CheckNeedRefreshEquipShow(v.m)
  896. end
  897. end
  898. end
  899. -- 交易行物品自定义属性需要客户端自行设置
  900. function BagProxy:SetTradingBankItemParams(makeIndex, param)
  901. if not makeIndex then
  902. return
  903. end
  904. self._customAttrs[makeIndex] = param
  905. SLBridge:onLUAEvent(LUA_EVENT_ITEM_CUSTOM_ATTR, {
  906. MakeIndex = makeIndex,
  907. data = param
  908. })
  909. end
  910. -- 批量设置交易行角色 背包/仓库/身上物品自定义属性
  911. function BagProxy:SetTradingBankItemParamsBatch(jsonData)
  912. if not jsonData then
  913. return
  914. end
  915. releasePrint("!SetTradingBankItemParamsBatch______")
  916. for i, v in ipairs(jsonData) do
  917. self._customAttrs[v.m] = v.p
  918. SLBridge:onLUAEvent(LUA_EVENT_ITEM_CUSTOM_ATTR, {
  919. MakeIndex = v.m,
  920. data = v.p
  921. })
  922. end
  923. end
  924. -- 请求背包数据
  925. function BagProxy:RequestBagData()
  926. self:Inited(true)
  927. LuaSendMsg(global.MsgType.MSG_CS_REQUEST_BAGDATA)
  928. end
  929. -- 叠加道具
  930. function BagProxy:RequestItemTwoToOne(MakeIndex1, MakeIndex2)
  931. LuaSendMsg(global.MsgType.MSG_CS_ITEM_TWO_TO_ONE, MakeIndex1, MakeIndex2, 0, 0)
  932. end
  933. -- 拆分道具
  934. function BagProxy:RequestCountItem(makeIndex, num)
  935. LuaSendMsg(global.MsgType.MSG_CS_ITEM_NUMBER_CHANGE, makeIndex, num, 0, 0)
  936. end
  937. -- 请求准星瞄准消息(使用)
  938. function BagProxy:RequestCollimator(MakeIndex)
  939. if MakeIndex then
  940. LuaSendMsg(global.MsgType.MSG_CM_COLLIMATOR_REQUEST, MakeIndex)
  941. end
  942. end
  943. -- 请求取消准星瞄准
  944. function BagProxy:RequestCanceCollimator()
  945. LuaSendMsg(global.MsgType.MSG_CM_CANCELCOLLIMATOR_REQUEST, 0, 0, 0, 0)
  946. end
  947. function BagProxy:onRegister()
  948. BagProxy.super.onRegister(self)
  949. end
  950. function BagProxy:RegisterMsgHandler()
  951. BagProxy.super.RegisterMsgHandler(self)
  952. local msgType = global.MsgType
  953. LuaRegisterMsgHandler(msgType.MSG_SC_RETURN_BAGDATA, handler(self, self.handle_MSG_SC_RETURN_BAGDATA))
  954. LuaRegisterMsgHandler(msgType.MSG_SC_BAG_ADD_ITEM, handler(self, self.handle_MSG_SC_BAG_ADD_ITEM))
  955. LuaRegisterMsgHandler(msgType.MSG_SC_BAG_DEL_ITEM, handler(self, self.handle_MSG_SC_BAG_DEL_ITEM))
  956. LuaRegisterMsgHandler(msgType.MSG_SC_BAG_UPDATE_ITEM, handler(self, self.handle_MSG_SC_BAG_UPDATE_ITEM))
  957. LuaRegisterMsgHandler(msgType.MSG_SC_BAG_ITEM_USE_SUCCESS, handler(self, self.handle_MSG_SC_BAG_ITEM_USE_SUCCESS) )
  958. LuaRegisterMsgHandler(msgType.MSG_SC_BAG_ITEM_USE_FAILED, handler(self, self.handle_MSG_SC_BAG_ITEM_USE_FAILED))
  959. LuaRegisterMsgHandler(msgType.MSG_SC_DROP_ITEM_GET_FAILED, handler(self, self.handle_MSG_SC_DROP_ITEM_GET_FAILED))
  960. LuaRegisterMsgHandler(msgType.MSG_SC_DROP_ITEM_SUCCESS, handler(self, self.handle_MSG_SC_DROP_ITEM_SUCCESS))
  961. LuaRegisterMsgHandler(msgType.MSG_SC_DROP_ITEM_FAILED, handler(self, self.handle_MSG_SC_DROP_ITEM_FAILED))
  962. LuaRegisterMsgHandler(msgType.MSG_SC_HUMBAG_TO_HEROBAG_FAIL, handler(self, self.handle_MSG_SC_HUMBAG_TO_HEROBAG_FAIL))
  963. LuaRegisterMsgHandler(msgType.MSG_SC_RESET_BAG_POS, handler(self, self.handle_MSG_SC_RESET_BAG_POS))
  964. LuaRegisterMsgHandler(msgType.MSG_SM_COLLIMATOR_RESPONSE, handler(self, self.handle_MSG_SM_COLLIMATOR_RESPONSE))
  965. LuaRegisterMsgHandler(msgType.MSG_SC_RETURN_BAGDATA_REMAINING, handler(self, self.handle_MSG_SC_RETURN_BAGDATA))
  966. LuaRegisterMsgHandler(msgType.MSG_SC_ITEM_PARAMS, handler(self, self.handle_MSG_SC_ITEM_PARAMS))
  967. end
  968. return BagProxy