ChipData.lua 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. local ChipData = class("ChipData", require("DataBase"))
  2. function ChipData:ctor()
  3. self.data = {}
  4. end
  5. function ChipData:InitChipData(data)
  6. local chips = {}
  7. local cfgMgr = ManagerContainer.CfgMgr
  8. if data and data.chip_list then
  9. for i = 1, #data.chip_list do
  10. local chipData = data.chip_list[i]
  11. if chipData then
  12. local chip = self:ProtocalDataToChipData(chipData)
  13. local itemCfgData = cfgMgr:GetItemById(chip.cfgId)
  14. if not itemCfgData then
  15. LogError("[Wboy] .. ".. chip.cfgId .. "道具ID不存在")
  16. else
  17. chips[chip.cfgId] = chip
  18. end
  19. end
  20. end
  21. end
  22. self.data.chips = chips
  23. end
  24. function ChipData:GetChipById(id)
  25. return self.data.chips[id]
  26. end
  27. function ChipData:GetChipCountById(id)
  28. local data = self.data.chips[id]
  29. if data then
  30. return data.num
  31. end
  32. return 0
  33. end
  34. function ChipData:GetAllChipDatas()
  35. return self.data.chips
  36. end
  37. function ChipData:RegisterNetEvents()
  38. ManagerContainer.NetManager:NetRegister(ProtoMsgId.SC_CHIP_CHANGE_NTF, function(data)
  39. local chips = self.data.chips
  40. local addChipMap = {}
  41. local cfgMgr = ManagerContainer.CfgMgr
  42. for _,chipData in ipairs(data.chip_list) do
  43. local cfgId = chipData.config_id
  44. local itemCfgData = cfgMgr:GetItemById(cfgId)
  45. if not itemCfgData then
  46. LogError("[Wboy] .. ".. cfgId .. "道具ID不存在")
  47. else
  48. local num = chipData.num
  49. if num <= 0 then
  50. -- 删除碎片
  51. chips[cfgId] = nil
  52. else
  53. local chip = chips[cfgId]
  54. local lastNum = chip and chip.num or 0
  55. local newChip = self:ProtocalDataToChipData(chipData)
  56. chips[cfgId] = newChip
  57. local addNum = newChip.num - lastNum
  58. -- 当前增加的碎片
  59. if addNum > 0 then
  60. if addChipMap[cfgId] then
  61. addChipMap[cfgId] = addChipMap[cfgId] + addNum
  62. else
  63. addChipMap[cfgId] = addNum
  64. end
  65. end
  66. end
  67. end
  68. end
  69. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.EID_EQUIP_AND_ITEM_ADD, addChipMap)
  70. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.CHIP_CHANGE)
  71. end)
  72. end
  73. function ChipData:Clear()
  74. self.data = {}
  75. end
  76. function ChipData:Destroy()
  77. if self.Clear then
  78. self:Clear()
  79. end
  80. self:UnRegisterNetEvents()
  81. end
  82. function ChipData:UnRegisterNetEvents()
  83. end
  84. function ChipData:ProtocalDataToChipData(protocalData)
  85. local chipData = {}
  86. chipData.cfgId = protocalData.config_id
  87. chipData.num = protocalData.num
  88. chipData.timeStamp = protocalData.time_stamp
  89. return chipData
  90. end
  91. return ChipData