BagVO.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. local BagData = class("BagData")
  2. --[[ README:
  3. 后端不存储背包位置信息 所以需要前端自己储存位置信息
  4. 位置信息保存:
  5. 存本地及内存 增删改变位置信息
  6. 新加入默认存第一空位 若大于40则存入缓存
  7. 服务器实际背包大小为52 40背包 12缓存和快捷栏(缓存数为0-12快捷栏上限为6
  8. 背包显示40 如果背包删后小于40 并且缓存中有存在 则补充至背包至上限
  9. 快捷栏保存道具类型,由于没有类型 存名字 快捷栏拖入时存 脱出或者背包没有同类型道具可以补充时删
  10. ]]
  11. function BagData:ctor()
  12. self.bagVo = {} --背包数据
  13. self.posData = {} --背包位置相关数据
  14. self.quickUseVo = {} --快捷栏数据
  15. self.noPosVo = {} --缓存数据
  16. self.isInit = false
  17. self.posMark = {} --背包位置信息标记
  18. self.quickUsePosMark = {} --快捷栏位置信息
  19. self.bagMax = global.MMO.MAX_ITEM_NUMBER
  20. self.itemCount = 0
  21. self.itemCountByIndex = {}
  22. self.lastPosMark = {} -- 记录分批背包数据上次存储位置
  23. self:InitBagPosMark()
  24. end
  25. function BagData:InitBagPosMark()
  26. local bagPosData = self:GetPosMarkData()
  27. if bagPosData then
  28. self.posMark = bagPosData
  29. else
  30. for i = 1, self.bagMax do
  31. self.posMark[i] = 0
  32. end
  33. end
  34. end
  35. function BagData:SetMaxBag(maxBag)
  36. self.bagMax = maxBag
  37. end
  38. function BagData:GetMaxBag()
  39. return self.bagMax or 0
  40. end
  41. function BagData:SetNewPosMark()
  42. local newPosMark = {}
  43. for i = 1, self.bagMax do
  44. if not self.posMark[i] or self.posMark[i] == 0 then
  45. newPosMark[i] = 0
  46. else
  47. newPosMark[i] = self.posMark[i]
  48. end
  49. end
  50. self.posMark = newPosMark
  51. end
  52. function BagData:AmendHistoryPos(data, sort, startIndex, endIndex)
  53. if not data then
  54. return
  55. end
  56. local newPosMark = {}
  57. local newItems = {}
  58. startIndex = startIndex or 1
  59. endIndex = endIndex or self.bagMax
  60. for i = 1, self.bagMax do
  61. if not self.posMark[i] or self.posMark[i] == 0 then
  62. newPosMark[i] = 0
  63. else
  64. if i >= startIndex and i <= endIndex then
  65. if not sort and self.lastPosMark[i] then
  66. newPosMark[i] = self.posMark[i]
  67. else
  68. newPosMark[i] = 0
  69. end
  70. else
  71. newPosMark[i] = self.posMark[i]
  72. end
  73. end
  74. end
  75. if next(data) == nil then
  76. self.posMark = newPosMark
  77. self:SetPosMarkData()
  78. return
  79. end
  80. for k, item in pairs(data) do
  81. local QuickUseProxy = global.Facade:retrieveProxy(global.ProxyTable.QuickUseProxy)
  82. local quickUsePos = QuickUseProxy:CheckIsInQucikUseList(item)
  83. if not quickUsePos then
  84. local historyPos = self:GetHistoryPos(item.MakeIndex)
  85. if historyPos then
  86. newPosMark[historyPos] = item.MakeIndex
  87. self.posData[item.MakeIndex] = historyPos
  88. self.lastPosMark[historyPos] = item.MakeIndex
  89. else
  90. table.insert(newItems, item)
  91. end
  92. else
  93. local bagPos = self.posData and self.posData[item.MakeIndex]
  94. if bagPos then
  95. print("=====bag and quickuse have same makeindex: quickUsePos: bagPos: ", item.MakeIndex, quickUsePos, bagPos)
  96. end
  97. end
  98. end
  99. local function GetNewPos()
  100. for i = 1, self.bagMax do
  101. if not newPosMark[i] or newPosMark[i] == 0 then
  102. return i
  103. end
  104. end
  105. return nil
  106. end
  107. if sort then
  108. if newItems and next(newItems) and #newItems > 1 then
  109. table.sort(newItems, function(a, b)
  110. if a.Index ~= b.Index then
  111. return a.Index < b.Index
  112. else
  113. return a.MakeIndex < b.MakeIndex
  114. end
  115. end)
  116. end
  117. for k, v in pairs(newItems) do
  118. local newPos = GetNewPos()
  119. if newPos then
  120. newPosMark[newPos] = v.MakeIndex
  121. self.posData[v.MakeIndex] = newPos
  122. else
  123. self.noPosVo[v.MakeIndex] = v
  124. end
  125. end
  126. else
  127. for k, v in pairs(newItems) do
  128. local newPos = GetNewPos()
  129. if newPos then
  130. newPosMark[newPos] = v.MakeIndex
  131. self.posData[v.MakeIndex] = newPos
  132. self.lastPosMark[newPos] = v.MakeIndex
  133. else
  134. self.noPosVo[v.MakeIndex] = v
  135. end
  136. end
  137. end
  138. self.posMark = newPosMark
  139. self:SetPosMarkData()
  140. end
  141. function BagData:IsInited()
  142. return self.isInit
  143. end
  144. function BagData:Inited(bool)
  145. self.isInit = bool
  146. end
  147. function BagData:GetBagData()
  148. return self.bagVo
  149. end
  150. function BagData:isFull()
  151. --没有新位置即为背包满了
  152. local pos = self:NewItemPos()
  153. return not pos
  154. end
  155. function BagData:GetItemByMakeIndex(MakeIndex)
  156. local data = nil
  157. if self.bagVo[MakeIndex] then
  158. data = self.bagVo[MakeIndex]
  159. elseif self.noPosVo[MakeIndex] then
  160. data = self.noPosVo[MakeIndex]
  161. end
  162. return data
  163. end
  164. function BagData:AddItem(item, pos)
  165. local MakeIndex = item.MakeIndex
  166. if pos then
  167. if not self.bagVo[MakeIndex] then
  168. self.itemCount = self.itemCount + 1
  169. end
  170. self.bagVo[MakeIndex] = item
  171. self.noPosVo[MakeIndex] = nil
  172. else
  173. self.noPosVo[MakeIndex] = item
  174. end
  175. self:ChangeItemCountByIndex(item.Index, item.OverLap)
  176. end
  177. function BagData:SubItem(MakeIndex)
  178. if self.bagVo[MakeIndex] then
  179. local item = self.bagVo[MakeIndex]
  180. self:ChangeItemCountByIndex(item.Index, -item.OverLap)
  181. self.bagVo[MakeIndex] = nil
  182. self.itemCount = self.itemCount - 1
  183. elseif self.noPosVo[MakeIndex] then
  184. local item = self.noPosVo[MakeIndex]
  185. self:ChangeItemCountByIndex(item.Index, -item.OverLap)
  186. self.noPosVo[MakeIndex] = nil
  187. end
  188. end
  189. function BagData:ChangeItem(item)
  190. local MakeIndex = item.MakeIndex
  191. local diffnum = 0
  192. if self.bagVo[MakeIndex] then
  193. local newNum = item.OverLap or 1
  194. local oldNum = self.bagVo[MakeIndex].OverLap or 1
  195. diffnum = newNum - oldNum
  196. self.bagVo[MakeIndex] = item
  197. elseif self.noPosVo[MakeIndex] then
  198. local newNum = item.OverLap or 1
  199. local oldNum = self.noPosVo[MakeIndex].OverLap or 1
  200. diffnum = newNum - oldNum
  201. self.noPosVo[MakeIndex] = item
  202. end
  203. self:ChangeItemCountByIndex(item.Index, diffnum)
  204. return diffnum
  205. end
  206. function BagData:SetItemPosData(MakeIndex, pos)
  207. self.posData[MakeIndex] = pos
  208. if pos then
  209. self.posMark[pos] = MakeIndex
  210. self:DelaySetPosMarkData()
  211. end
  212. end
  213. function BagData:CleanItemPosData(MakeIndex)
  214. local historyPos = self.posData[MakeIndex]
  215. if historyPos then
  216. self.posMark[historyPos] = 0
  217. self:DelaySetPosMarkData()
  218. end
  219. self.posData[MakeIndex] = nil
  220. end
  221. function BagData:DelaySetPosMarkData()
  222. if self._delayTimerID then
  223. UnSchedule(self._delayTimerID)
  224. self._delayTimerID = nil
  225. end
  226. self._delayTimerID = PerformWithDelayGlobal(function()
  227. self:SetPosMarkData()
  228. self._delayTimerID = nil
  229. end, 0.5)
  230. end
  231. function BagData:NewItemPos()
  232. for i = 1, self.bagMax do
  233. if self.posMark[i] and self.posMark[i] == 0 then
  234. return i
  235. end
  236. end
  237. return nil
  238. end
  239. function BagData:GetMakeIndexByBagPos(pos)
  240. for k, v in pairs(self.posData) do
  241. if v == pos then
  242. return k
  243. end
  244. end
  245. return nil
  246. end
  247. function BagData:GetBagDataByBagPos(startPos, endPos)
  248. local data = {}
  249. for MakeIndex, pos in pairs(self.posData) do
  250. if pos >= startPos and pos <= endPos then
  251. local itemData = self:GetItemByMakeIndex(MakeIndex)
  252. data[MakeIndex] = itemData
  253. end
  254. end
  255. return data
  256. end
  257. function BagData:GetBagNoPosData()
  258. return self.noPosVo
  259. end
  260. function BagData:GetBagNoPosDataByMakeIndex(MakeIndex)
  261. return self.noPosVo[MakeIndex]
  262. end
  263. function BagData:GetBagPosByMakeIndex(MakeIndex)
  264. return self.posData[MakeIndex]
  265. end
  266. function BagData:ChangeItemCountByIndex(Index, diff)
  267. if not Index then
  268. return
  269. end
  270. if not self.itemCountByIndex then
  271. self.itemCountByIndex = {}
  272. end
  273. local diffCount = diff or 0
  274. local oldCount = self.itemCountByIndex[Index] or 0
  275. local newCount = oldCount + diffCount
  276. self.itemCountByIndex[Index] = newCount > 0 and newCount or 0
  277. end
  278. function BagData:GetItemCountByIndex(Index)
  279. if not self.itemCountByIndex then
  280. self.itemCountByIndex = {}
  281. end
  282. local count = clone(self.itemCountByIndex[Index] or 0)
  283. return count
  284. end
  285. function BagData:ClearItemData()
  286. self.bagVo = {}
  287. self.noPosVo = {}
  288. self.itemCount = 0
  289. self.itemCountByIndex = {}
  290. self.posData = {}
  291. self.lastPosMark = {}
  292. end
  293. function BagData:GetItemCount()
  294. return self.itemCount
  295. end
  296. function BagData:GetHistoryPos(MakeIndex)
  297. for k, v in pairs(self.posMark) do
  298. if v == MakeIndex then
  299. return k
  300. end
  301. end
  302. return nil
  303. end
  304. function BagData:CleanBagPosData()
  305. self.posMark = {}
  306. for i = 1, self.bagMax do
  307. self.posMark[i] = 0
  308. end
  309. self.posData = {}
  310. self:SetPosMarkData()
  311. end
  312. --背包
  313. function BagData:GetPosMarkData()
  314. local PosData = UserData:new("BagPosData")
  315. local proxy = global.Facade:retrieveProxy(global.ProxyTable.Login)
  316. local flag = proxy:GetSelectedRoleID() or "errorName"
  317. local clientData = PosData:getStringForKey("bag" .. flag)
  318. if not clientData or clientData == "" then
  319. return nil
  320. end
  321. local cjson = require("cjson")
  322. local lastJsonData = cjson.decode(clientData)
  323. return lastJsonData
  324. end
  325. function BagData:SetPosMarkData()
  326. local PosData = UserData:new("BagPosData")
  327. local proxy = global.Facade:retrieveProxy(global.ProxyTable.Login)
  328. local flag = proxy:GetSelectedRoleID() or "errorName"
  329. local cjson = require("cjson")
  330. local bagPosData = cjson.encode(self.posMark)
  331. local clientData = PosData:setStringForKey("bag" .. flag, bagPosData)
  332. end
  333. return BagData