HeroBagVO.lua 8.1 KB

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