NPCStorageProxy.lua 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. local RemoteProxy = requireProxy("remote/RemoteProxy")
  2. local NPCStorageProxy = class("NPCStorageProxy", RemoteProxy)
  3. NPCStorageProxy.NAME = global.ProxyTable.NPCStorageProxy
  4. function NPCStorageProxy:ctor()
  5. NPCStorageProxy.super.ctor(self)
  6. local StorageVO = requireVO("remote/StorageVO")
  7. self.VOdata = StorageVO.new()
  8. self.storageData = {}
  9. self.maxPage = 1 -- 仓库的最大页
  10. self.npcIndex = nil
  11. self.touchType = 0
  12. self.openedCount = 0
  13. self.pageIndex = 1
  14. self._schedule_id = nil --请求存储的计时器 0.5s
  15. end
  16. function NPCStorageProxy:LoadConfig()
  17. local maxNum = SL:GetMetaValue("GAME_DATA","warehouse_max_num") or 240
  18. self.maxPage = math.ceil(maxNum / (GUIDefine.STORAGE_PER_PAGE_MAX or global.MMO.NPC_STORAGE_MAX_PAGE))
  19. end
  20. -- 获取仓库的最大页
  21. function NPCStorageProxy:GetMaxPage()
  22. return self.maxPage
  23. end
  24. function NPCStorageProxy:SetStorageData(data)
  25. local newList = {}
  26. for _, v in ipairs(data) do
  27. local changeData = ChangeItemServersSendDatas(v)
  28. table.insert(newList, changeData)
  29. end
  30. self.storageData = newList or {}
  31. -- clear data
  32. self.VOdata:ClearItemData()
  33. -- 更新位置
  34. self.VOdata:AmendHistoryPos(newList)
  35. local makeindex = {}
  36. for _, v in ipairs(newList) do
  37. local itemPos = self.VOdata:GetHistoryPos(v.MakeIndex) or self.VOdata:NewItemPos()
  38. if itemPos then
  39. self.VOdata:SetItemPosData(v.MakeIndex, itemPos)
  40. end
  41. self.VOdata:AddItem(v, itemPos)
  42. end
  43. self.VOdata:SetPosMarkData()
  44. -- 重新排列数据
  45. self.storageData = {}
  46. local data = {}
  47. data = self.VOdata:GetStorageData()
  48. for i, v in pairs(data) do
  49. local pos = self.VOdata:GetStoragePosByMakeIndex(v.MakeIndex)
  50. if pos then
  51. self.storageData[pos] = v
  52. end
  53. end
  54. end
  55. function NPCStorageProxy:GetStorageData()
  56. return self.storageData
  57. end
  58. function NPCStorageProxy:SetStoragePageIndex(Index)
  59. self.pageIndex = math.min(Index or 1, self.maxPage)
  60. end
  61. function NPCStorageProxy:GetStoragePageIndex()
  62. return self.pageIndex
  63. end
  64. function NPCStorageProxy:GetStorageDataByPage(page)
  65. local newList = {}
  66. if not page then
  67. return newList
  68. end
  69. for k, v in pairs(self.storageData) do
  70. if v.Where == page then
  71. table.insert(newList, v)
  72. end
  73. end
  74. return newList
  75. end
  76. function NPCStorageProxy:SetNpcIndex(idx)
  77. self.npcIndex = idx
  78. end
  79. function NPCStorageProxy:GetNpcIndex()
  80. return self.npcIndex
  81. end
  82. function NPCStorageProxy:SetOpenSize(openCount)
  83. local size = SL:GetMetaValue("GAME_DATA","warehouse_num")
  84. local setBaseSize = size and tonumber(size)
  85. local base = setBaseSize or 24
  86. local open = openCount or 0
  87. self.openedCount = base + open
  88. if self.openedCount > 240 then
  89. self.openedCount = 240
  90. end
  91. self.VOdata:SetMaxStorage(self.openedCount)
  92. self.VOdata:SetNewStoragePosMark()
  93. end
  94. function NPCStorageProxy:GetOpenSize()
  95. return self.openedCount
  96. end
  97. function NPCStorageProxy:GetTouchType()
  98. return self.touchType
  99. end
  100. function NPCStorageProxy:SetTouchType(type)
  101. self.touchType = type
  102. end
  103. function NPCStorageProxy:CleanData()
  104. self.storageData = {}
  105. self.npcIndex = nil
  106. if self._schedule_id then
  107. UnSchedule(self._schedule_id)
  108. self._schedule_id = nil
  109. end
  110. end
  111. function NPCStorageProxy:AddStorageData(data, where)
  112. local itemData = data
  113. if not itemData or next(itemData) == nil then
  114. return
  115. end
  116. -- 记录位置
  117. local itemPos = self.VOdata:GetHistoryPos(itemData.MakeIndex) or self.VOdata:NewItemPos()
  118. if itemPos then
  119. self.VOdata:AddItem(itemData, itemPos)
  120. self.VOdata:SetItemPosData(itemData.MakeIndex, itemPos)
  121. self.VOdata:SetPosMarkData()
  122. end
  123. itemData.Where = where
  124. table.insert(self.storageData, itemData)
  125. global.Facade:sendNotification(global.NoticeTable.Layer_NPC_Storage_Update, where)
  126. local data = {
  127. opera = global.MMO.Operator_Add,
  128. item = itemData,
  129. page = where
  130. }
  131. SLBridge:onLUAEvent(LUA_EVENT_STORAGE_DATA_CHANGE, data)
  132. end
  133. function NPCStorageProxy:DeleteStorageData(data)
  134. if data and data.MakeIndex and self.storageData and next(self.storageData) then
  135. for k, v in pairs(self.storageData) do
  136. if data.MakeIndex and v.MakeIndex and data.MakeIndex == v.MakeIndex then
  137. -- 记录位置
  138. self.VOdata:SubItem(data.MakeIndex)
  139. self.VOdata:CleanItemPosData(data.MakeIndex)
  140. self.VOdata:SetPosMarkData()
  141. table.remove(self.storageData, k)
  142. global.Facade:sendNotification(global.NoticeTable.Layer_NPC_Storage_Update, data.Where)
  143. local data = {
  144. opera = global.MMO.Operator_Sub,
  145. item = data,
  146. page = data.Where
  147. }
  148. SLBridge:onLUAEvent(LUA_EVENT_STORAGE_DATA_CHANGE, data)
  149. break
  150. end
  151. end
  152. end
  153. end
  154. -- 更新物品的叠加数据
  155. function NPCStorageProxy:UpdateStorageData(MakeIndex, nums)
  156. if MakeIndex then
  157. for _, v in pairs(self.storageData) do
  158. if v.MakeIndex and MakeIndex == v.MakeIndex then
  159. v.OverLap = nums
  160. global.Facade:sendNotification(global.NoticeTable.Layer_NPC_Storage_Update, v.Where)
  161. local data = {
  162. opera = global.MMO.Operator_Change,
  163. item = v,
  164. page = v.Where
  165. }
  166. SLBridge:onLUAEvent(LUA_EVENT_STORAGE_DATA_CHANGE, data)
  167. break
  168. end
  169. end
  170. end
  171. end
  172. function NPCStorageProxy:GetStorageDataByMakeIndex(MakeIndex)
  173. local MakeIndex = MakeIndex or 0
  174. local data = nil
  175. for _, v in pairs(self.storageData) do
  176. if MakeIndex and v.MakeIndex and MakeIndex == v.MakeIndex then
  177. data = v
  178. break
  179. end
  180. end
  181. return data
  182. end
  183. -- 整理仓库
  184. function NPCStorageProxy:ArrangeStorageData(page)
  185. local ItemConfigProxy = global.Facade:retrieveProxy(global.ProxyTable.ItemConfigProxy)
  186. local function sortFunc(a, b)
  187. local powera = GUIFunction:GetEquipPower(a)
  188. local powerb = GUIFunction:GetEquipPower(b)
  189. if powera ~= powerb then
  190. return powera > powerb
  191. else
  192. local isBindA, bindIndexA = ItemConfigProxy:CheckItemBind(a.Index)
  193. local isBindB, bindIndexB = ItemConfigProxy:CheckItemBind(b.Index)
  194. if isBindA == isBindB then
  195. return a.Index < b.Index
  196. else
  197. return isBindA
  198. end
  199. end
  200. end
  201. local data = {}
  202. for _, v in pairs(self.storageData) do
  203. table.insert(data, v)
  204. end
  205. if not data or next(data) == nil then
  206. return
  207. end
  208. -- 按照战力排序
  209. table.sort(data, sortFunc)
  210. -- 更新位置
  211. self.VOdata:CleanStoragePosData()
  212. self.VOdata:AmendHistoryPos(data)
  213. self.VOdata:SetPosMarkData()
  214. -- 重新排列数据
  215. self.storageData = {}
  216. local data = {}
  217. data = self.VOdata:GetStorageData()
  218. for i, v in pairs(data) do
  219. local pos = self.VOdata:GetStoragePosByMakeIndex(v.MakeIndex)
  220. if pos then
  221. self.storageData[pos] = v
  222. end
  223. end
  224. global.Facade:sendNotification(global.NoticeTable.Layer_NPC_Storage_Update, page)
  225. local data = {
  226. opera = 4, -- 整理/刷新页
  227. page = page
  228. }
  229. SLBridge:onLUAEvent(LUA_EVENT_STORAGE_DATA_CHANGE, data)
  230. end
  231. -- 请求存储
  232. function NPCStorageProxy:RequestSaveToStorage(MakeIndex, Name)
  233. local npcIndex = self:GetNpcIndex()
  234. if not npcIndex then
  235. return
  236. end
  237. if not self:CheckPutInStorageState() or self._schedule_id then
  238. local systipStr = self._schedule_id and GET_STRING(90100005) or GET_STRING(90100004)
  239. global.Facade:sendNotification(global.NoticeTable.SystemTips, systipStr)
  240. -- 背包中的重新刷出来
  241. local data = {
  242. storage = {
  243. state = 1,
  244. MakeIndex = MakeIndex,
  245. }
  246. }
  247. global.Facade:sendNotification(global.NoticeTable.Bag_State_Change, data)
  248. return
  249. end
  250. self._schedule_id = PerformWithDelayGlobal(function()
  251. self._schedule_id = nil
  252. end, 0.3)
  253. local pageIndex = self:GetStoragePageIndex() or 1
  254. LuaSendMsg(global.MsgType.MSG_CS_STORAGE_STORE_REQUEST, npcIndex, MakeIndex, pageIndex, 0, Name, string.len(Name))
  255. end
  256. function NPCStorageProxy:RequestSaveToStorageAllPage(MakeIndex, Name, setPageIndex)
  257. --请求存储
  258. local npcIndex = self:GetNpcIndex()
  259. if not npcIndex then
  260. return
  261. end
  262. local pageIndex = 1
  263. local needCheckSpace = false
  264. if setPageIndex then
  265. pageIndex = setPageIndex
  266. needCheckSpace = true
  267. else
  268. for i = 1, self.maxPage do
  269. local index = i
  270. local openCount = self.openedCount
  271. local pageData = self:GetStorageDataByPage(index)
  272. local itemCount = #pageData
  273. local pageMaxNum = GUIDefine.STORAGE_PER_PAGE_MAX or global.MMO.NPC_STORAGE_MAX_PAGE
  274. local thisPageOpenCount = openCount - (index-1) * pageMaxNum
  275. if thisPageOpenCount > pageMaxNum then
  276. thisPageOpenCount = pageMaxNum
  277. end
  278. if itemCount < thisPageOpenCount then
  279. pageIndex = i
  280. break
  281. end
  282. pageIndex = nil
  283. end
  284. end
  285. if not pageIndex or self._schedule_id or (needCheckSpace and not self:CheckPutInStorageState()) then
  286. local systipStr = self._schedule_id and GET_STRING(90100005) or GET_STRING(90100004)
  287. global.Facade:sendNotification(global.NoticeTable.SystemTips, systipStr)
  288. -- 背包中的重新刷出来
  289. local data = {
  290. storage = {
  291. MakeIndex = MakeIndex,
  292. state = 1
  293. }
  294. }
  295. global.Facade:sendNotification(global.NoticeTable.Bag_State_Change, data)
  296. return
  297. end
  298. self._schedule_id = PerformWithDelayGlobal(function()
  299. self._schedule_id = nil
  300. end, 0.3)
  301. LuaSendMsg(global.MsgType.MSG_CS_STORAGE_STORE_REQUEST, npcIndex, MakeIndex, pageIndex, 0, Name, string.len(Name))
  302. end
  303. function NPCStorageProxy:CheckPutInStorageState()
  304. local index = self:GetStoragePageIndex()
  305. local pageData = self:GetStorageDataByPage(index)
  306. local itemCount = #pageData
  307. local pageMaxNum = GUIDefine.STORAGE_PER_PAGE_MAX or global.MMO.NPC_STORAGE_MAX_PAGE
  308. local thisPageOpenCount = self.openedCount - (index - 1) * pageMaxNum
  309. local isWinMode = SL:GetMetaValue("WINPLAYMODE")
  310. local storage_row_col = SL:GetMetaValue("GAME_DATA", "bag_storage_row_col_max")
  311. local bBig = false -- 是否大仓库
  312. if isWinMode and storage_row_col then
  313. local slices = string.split(storage_row_col, "|")
  314. local row = tonumber(slices[1]) or 8
  315. local col = tonumber(slices[2]) or 6
  316. if row * col > pageMaxNum then
  317. bBig = true
  318. end
  319. end
  320. if not bBig and thisPageOpenCount > pageMaxNum then
  321. thisPageOpenCount = pageMaxNum
  322. end
  323. return itemCount < thisPageOpenCount
  324. end
  325. --请求仓库数据
  326. function NPCStorageProxy:RequestStorageData()
  327. LuaSendMsg(global.MsgType.MSG_CS_STORAGEDATA_REQUEST, 0, 0, 0, 0, 0, 0)
  328. end
  329. -- 请求取出
  330. function NPCStorageProxy:RequestPutOutStorageData(MakeIndex, Name)
  331. local npcIndex = self:GetNpcIndex()
  332. if not npcIndex then
  333. return
  334. end
  335. -- 背包已满,仓库中重新刷出来
  336. local BagProxy = global.Facade:retrieveProxy(global.ProxyTable.Bag)
  337. if BagProxy:isToBeFull(true) then
  338. local data = {
  339. resetItem = {
  340. state = 1,
  341. MakeIndex = MakeIndex,
  342. }
  343. }
  344. global.Facade:sendNotification(global.NoticeTable.Layer_NPC_Storage_Item_State, data)
  345. return
  346. end
  347. LuaSendMsg(global.MsgType.MSG_CS_STORAGE_PICK_OUT_REQUEST, npcIndex, MakeIndex, 0, 0, Name, string.len(Name))
  348. end
  349. function NPCStorageProxy:handle_MSG_SC_STOR_DATA(msg)
  350. dump("handle_MSG_SC_STOR_DATA___")
  351. local data = ParseRawMsgToJson(msg)
  352. if not data or not next(data) then
  353. return
  354. end
  355. self:CleanData()
  356. self:SetOpenSize(data.Count)
  357. self:SetStorageData(data.Items)
  358. self:SetNpcIndex(data.index)
  359. local npcProxy = global.Facade:retrieveProxy(global.ProxyTable.NPC)
  360. npcProxy:Cleanup()
  361. local header = msg:GetHeader()
  362. local isself = header.recog == 1 --是自己请求的还是脚本发的
  363. if not isself then
  364. global.Facade:sendNotification(global.NoticeTable.Layer_NPC_Storage_Open)
  365. end
  366. end
  367. function NPCStorageProxy:handle_MSG_SC_STORAGE_STORE_STORED_ADD(msg)
  368. print("存仓成功")
  369. local header = msg:GetHeader()
  370. local MakeIndex = header.recog
  371. if MakeIndex and MakeIndex ~= 0 then
  372. local BagProxy = global.Facade:retrieveProxy(global.ProxyTable.Bag)
  373. local item = BagProxy:GetItemDataByMakeIndex(MakeIndex)
  374. if item then
  375. local pageIdnex = header.param1
  376. self:AddStorageData(item, pageIdnex)
  377. BagProxy:DelItemData(item, true)
  378. end
  379. end
  380. end
  381. function NPCStorageProxy:handle_MSG_SC_STORAGE_STORE_FULL(msg)
  382. print("存仓失败, 仓库满")
  383. local header = msg:GetHeader()
  384. local MakeIndex = header.recog
  385. if MakeIndex then
  386. -- 背包中的重新刷出来
  387. local data = {
  388. storage = {
  389. state = 1,
  390. MakeIndex = MakeIndex,
  391. }
  392. }
  393. global.Facade:sendNotification(global.NoticeTable.Bag_State_Change, data)
  394. end
  395. end
  396. function NPCStorageProxy:handle_MSG_SC_STORAGE_STORE_FORBIDDEN(msg)
  397. print("存仓失败")
  398. local header = msg:GetHeader()
  399. local MakeIndex = header.recog
  400. if MakeIndex then
  401. -- 背包中的重新刷出来
  402. local data = {
  403. storage = {
  404. state = 1,
  405. MakeIndex = MakeIndex,
  406. }
  407. }
  408. global.Facade:sendNotification(global.NoticeTable.Bag_State_Change, data)
  409. end
  410. end
  411. function NPCStorageProxy:handle_MSG_SC_STORAGE_PICK_OUT_FORBIDDEN(msg)
  412. print("取出失败")
  413. local header = msg:GetHeader()
  414. local MakeIndex = header.recog
  415. if MakeIndex then
  416. -- 仓库中重新刷出来
  417. local data = {
  418. resetItem = {
  419. state = 1,
  420. MakeIndex = MakeIndex,
  421. }
  422. }
  423. global.Facade:sendNotification(global.NoticeTable.Layer_NPC_Storage_Item_State, data)
  424. end
  425. end
  426. function NPCStorageProxy:handle_MSG_SC_STORAGE_PICK_OUT_SUCC(msg)
  427. print("取出成功")
  428. local header = msg:GetHeader()
  429. local MakeIndex = header.recog
  430. if MakeIndex and MakeIndex ~= 0 then
  431. local item = self:GetStorageDataByMakeIndex(MakeIndex)
  432. if item then
  433. self:DeleteStorageData(item)
  434. end
  435. end
  436. end
  437. function NPCStorageProxy:handle_MSG_SC_STORAGE_PICK_OUT_FULL(msg)
  438. print("背包满 取出失败")
  439. local header = msg:GetHeader()
  440. local MakeIndex = header.recog
  441. if MakeIndex then
  442. -- 背包已满,仓库中重新刷出来
  443. local data = {
  444. resetItem = {
  445. state = 1,
  446. MakeIndex = MakeIndex,
  447. }
  448. }
  449. global.Facade:sendNotification(global.NoticeTable.Layer_NPC_Storage_Item_State, data)
  450. end
  451. end
  452. function NPCStorageProxy:handle_MSG_SC_STORAGE_UPDATE(msg)
  453. local header = msg:GetHeader()
  454. local MakeIndex = header.recog
  455. if MakeIndex and MakeIndex ~= 0 then
  456. local itemNums = header.param1
  457. self:UpdateStorageData(MakeIndex, itemNums)
  458. local bagMakeIndex = ParseRawMsgToJson(msg)
  459. local BagProxy = global.Facade:retrieveProxy(global.ProxyTable.Bag)
  460. local item = BagProxy:GetItemDataByMakeIndex(bagMakeIndex)
  461. if item then
  462. BagProxy:DelItemData(item, true)
  463. end
  464. end
  465. end
  466. function NPCStorageProxy:handle_MSG_SC_STORAGE_COUNT_CHANGE(msg)
  467. local header = msg:GetHeader()
  468. local count = header.recog
  469. self:SetOpenSize(count)
  470. global.Facade:sendNotification(global.NoticeTable.Layer_NPC_Storage_Size_Change)
  471. end
  472. function NPCStorageProxy:RegisterMsgHandler()
  473. NPCStorageProxy.super.RegisterMsgHandler(self)
  474. local msgType = global.MsgType
  475. -- 仓库数据
  476. LuaRegisterMsgHandler(msgType.MSG_SC_STOR_DATA, handler(self, self.handle_MSG_SC_STOR_DATA))
  477. -- 存仓成功
  478. LuaRegisterMsgHandler(msgType.MSG_SC_STORAGE_STORE_STORED_ADD, handler(self, self.handle_MSG_SC_STORAGE_STORE_STORED_ADD))
  479. -- 仓库满 存仓失败
  480. LuaRegisterMsgHandler(msgType.MSG_SC_STORAGE_STORE_FULL, handler(self, self.handle_MSG_SC_STORAGE_STORE_FULL))
  481. -- 其他 存仓失败
  482. LuaRegisterMsgHandler(msgType.MSG_SC_STORAGE_STORE_FORBIDDEN, handler(self, self.handle_MSG_SC_STORAGE_STORE_FORBIDDEN))
  483. -- 其他 取出失败
  484. LuaRegisterMsgHandler(msgType.MSG_SC_STORAGE_PICK_OUT_FORBIDDEN, handler(self, self.handle_MSG_SC_STORAGE_PICK_OUT_FORBIDDEN))
  485. -- 取出成功
  486. LuaRegisterMsgHandler(msgType.MSG_SC_STORAGE_PICK_OUT_SUCC, handler(self, self.handle_MSG_SC_STORAGE_PICK_OUT_SUCC))
  487. -- 背包满 取出失败
  488. LuaRegisterMsgHandler(msgType.MSG_SC_STORAGE_PICK_OUT_FULL, handler(self, self.handle_MSG_SC_STORAGE_PICK_OUT_FULL))
  489. --仓库,存物品时候,存的叠加物品,需要更新
  490. LuaRegisterMsgHandler(msgType.MSG_SC_STORAGE_UPDATE, handler(self, self.handle_MSG_SC_STORAGE_UPDATE))
  491. -- 仓库格子数增加
  492. LuaRegisterMsgHandler(msgType.MSG_SC_STORAGE_COUNT_CHANGE, handler(self, self.handle_MSG_SC_STORAGE_COUNT_CHANGE))
  493. end
  494. return NPCStorageProxy