UIPetRelationsCtr.lua 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. local UIPetRelationsCtr = class("UIPetRelationsCtr", require("UICtrBase"))
  2. function UIPetRelationsCtr:Init(view)
  3. self.view = view
  4. end
  5. function UIPetRelationsCtr:SetData(data)
  6. self.asyncIdx = 0
  7. if data == nil then return end
  8. self.data = data
  9. end
  10. function UIPetRelationsCtr:GetAsyncIdx()
  11. self.asyncIdx = self.asyncIdx + 1
  12. return self.asyncIdx
  13. end
  14. function UIPetRelationsCtr:GetData()
  15. return self.data
  16. end
  17. function UIPetRelationsCtr:RefreshRelationDatas()
  18. if not self.relationDatas then
  19. self.relationDatas = ManagerContainer.CfgMgr:GetAllPetPartnerDatas()
  20. end
  21. return self.relationDatas
  22. end
  23. function UIPetRelationsCtr:GetRelationDataLength()
  24. return #self.relationDatas
  25. end
  26. function UIPetRelationsCtr:GetRelationShowDataByIdx(idx)
  27. return self.relationDatas[idx + 1]
  28. end
  29. function UIPetRelationsCtr:SetRelationDataByRelationIdAndSlotIdx(cfgId, idx, petData)
  30. local datas = {}
  31. local replaceUid, replaceCfgId, replacePetId, replaceAdvanceLevel
  32. local relationData = ManagerContainer.DataMgr.PetDataMgr:GetPetRelationDataByCfgId(cfgId)
  33. local cloneData = clone(relationData)
  34. if not cloneData then
  35. cloneData = {}
  36. cloneData.cfgId = cfgId
  37. cloneData.relationPets = {}
  38. end
  39. if cloneData.relationPets then
  40. local data = cloneData.relationPets[idx]
  41. if data == nil then
  42. data = {}
  43. end
  44. replaceUid, replaceCfgId, replacePetId, replaceAdvanceLevel = data.uid, data.cfgId, data.id or data.petId, data.advanceLevel
  45. data.uid = petData.uid;
  46. data.petId = petData.id;
  47. data.cfgId = petData.cfgId;
  48. data.advanceLevel = petData.advanceLevel
  49. cloneData.relationPets[idx] = data
  50. end
  51. datas[#datas + 1] = cloneData
  52. local replaceRelationData = ManagerContainer.DataMgr.PetDataMgr:GetReplacePetRelationDataByUidAndPetId(petData.uid, petData.id)
  53. if replaceRelationData then
  54. local cloneData = clone(replaceRelationData)
  55. if cloneData.relationPets then
  56. for _,v in pairs(cloneData.relationPets) do
  57. if v.uid == petData.uid and v.petId == petData.id then
  58. v.uid = replaceUid or 0
  59. v.cfgId = replaceCfgId or 0
  60. v.petId = replacePetId or 0
  61. v.advanceLevel = replaceAdvanceLevel or 0
  62. end
  63. end
  64. end
  65. datas[#datas + 1] = cloneData
  66. end
  67. ManagerContainer.DataMgr.PetDataMgr:SendPetRelationActive(datas)
  68. end
  69. local function GetMaxActiveRelationIdx(relationPets, cfgData)
  70. local count = 3
  71. local maxIdx = 0
  72. for j = 3, 1, -1 do
  73. local condition = cfgData['Condition'..j]
  74. local state
  75. if condition then
  76. local curCount = 0
  77. if relationPets then
  78. for k,v1 in pairs(relationPets) do
  79. local cfgId = condition[k][1]
  80. local num = condition[k][2]
  81. if v1.cfgId == cfgId and v1.advanceLevel >= num and v1.petId > 0 then
  82. curCount = curCount + 1
  83. end
  84. end
  85. end
  86. state = curCount >= count
  87. if state then
  88. if maxIdx == 0 then
  89. maxIdx = j
  90. end
  91. end
  92. end
  93. end
  94. return maxIdx
  95. end
  96. function UIPetRelationsCtr:RefreshAllAttrs()
  97. self.allAttrs = {}
  98. local list = {7,8,9,10,11,12,13,14,15,16,91,92,93,94,95,96,97}
  99. for i = 1, #list do
  100. table.insert(self.allAttrs, {list[i], 0})
  101. end
  102. local function AddAttr(attrId, val)
  103. for _,v in pairs(self.allAttrs) do
  104. if attrId == v[1] then
  105. v[2] = v[2] + val
  106. end
  107. end
  108. end
  109. local relationDatas = ManagerContainer.DataMgr.PetDataMgr:GetAllPetRelationDatas()
  110. for k,v in pairs(relationDatas) do
  111. local cfgData = ManagerContainer.CfgMgr:GetPetPartnerDataById(v.cfgId)
  112. local maxIdx = GetMaxActiveRelationIdx(v.relationPets, cfgData)
  113. if maxIdx > 0 and v.relationPets then
  114. for j = maxIdx,1,-1 do
  115. local attribute = cfgData['attribute'..j]
  116. if attribute then
  117. for i = 1,#attribute do
  118. local attrId = attribute[i][1]
  119. local value = attribute[i][2]
  120. AddAttr(attrId, value)
  121. end
  122. end
  123. end
  124. end
  125. end
  126. end
  127. function UIPetRelationsCtr:GetAllAttrs()
  128. return self.allAttrs
  129. end
  130. function UIPetRelationsCtr:GetRelationRPStates()
  131. if not self.relationRPStates then
  132. self.relationRPStates = ManagerContainer.DataMgr.PetDataMgr:GetRelationRPStates()
  133. if not self.relationRPStates then
  134. ManagerContainer.DataMgr.PetDataMgr:RefreshRelationRPStates()
  135. end
  136. end
  137. return self.relationRPStates
  138. end
  139. function UIPetRelationsCtr:OnDispose()
  140. self.relationDatas = nil
  141. self.allAttrs = nil
  142. self.relationRPStates = nil
  143. self.data = nil
  144. self.view = nil
  145. end
  146. return UIPetRelationsCtr