PetRelationData.lua 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. local PetRelationData = class("PetRelationData")
  2. local function GetMaxActiveRelationIdx(relationPets, cfgData)
  3. if not cfgData then
  4. return 0
  5. end
  6. local count = 3
  7. local maxIdx = 0
  8. for j = 3, 1, -1 do
  9. local condition = cfgData['Condition'..j]
  10. local state
  11. if condition then
  12. local curCount = 0
  13. if relationPets then
  14. for k,v1 in pairs(relationPets) do
  15. local cfgId = condition[k][1]
  16. local num = condition[k][2]
  17. if v1.cfgId == cfgId and v1.advanceLevel >= num and v1.petId > 0 then
  18. curCount = curCount + 1
  19. end
  20. end
  21. end
  22. state = curCount >= count
  23. if state then
  24. if maxIdx == 0 then
  25. maxIdx = j
  26. end
  27. end
  28. end
  29. end
  30. return maxIdx
  31. end
  32. function PetRelationData:ctor()
  33. self.data = {}
  34. self.data.relationDatas = {}
  35. self.data.relationPlayerNameMap = {}
  36. self.data.relationAttrMap = {}
  37. self.data.mySupportPets = {}
  38. for i = 1, Constant.MyPetSupportLimit do
  39. self.data.mySupportPets[i] = {}
  40. self.data.mySupportPets[i].id = 0
  41. self.data.mySupportPets[i].cd = 0
  42. self.data.mySupportPets[i].idx = i
  43. end
  44. self.data.relationRPStates = nil
  45. end
  46. function PetRelationData:InitData(datas)
  47. --LogError("===========PetRelationData:InitData============")
  48. if datas and datas.bond_list then
  49. for _,v in pairs(datas.bond_list) do
  50. local data = ProtocalDataNormal.ParsePetBondData(v)
  51. self.data.relationDatas[data.cfgId] = data
  52. for _,v in pairs(data.relationPets) do
  53. if v.uid == 0 and v.petId > 0 then
  54. local petData = ManagerContainer.DataMgr.PetDataMgr:GetPetDataById(v.petId)
  55. --LogError("===========PetRelationData:InitData=======12122121212====="..v.petId)
  56. if petData then
  57. petData.isRelevant = true
  58. --LogError("===========PetRelationData:InitData============"..v.petId.." ==="..Inspect(petData))
  59. end
  60. else
  61. if v.petId == 0 then
  62. v.cfgId = 0
  63. v.advanceLevel = 0
  64. end
  65. end
  66. end
  67. self:IsNewRelationActived(data.cfgId, nil, data, nil, nil)
  68. end
  69. end
  70. if datas.uid_name_list then
  71. for k,v in pairs(datas.uid_name_list) do
  72. self.data.relationPlayerNameMap[v.key] = v.str_val
  73. end
  74. end
  75. end
  76. function PetRelationData:RefreshData(bondDataList, nameList)
  77. --LogError("===========PetRelationData:RefreshData============")
  78. self:RefreshRelationBondList(bondDataList)
  79. if nameList then
  80. for k,v in pairs(nameList) do
  81. self.data.relationPlayerNameMap[v.key] = v.str_val
  82. end
  83. end
  84. end
  85. function PetRelationData:RefreshRelationBondList(bondDataList)
  86. if bondDataList then
  87. local oldAttrDatas, newAttrDatas = {}, {}
  88. local changedList = {}
  89. for _,v in pairs(bondDataList) do
  90. local data = ProtocalDataNormal.ParsePetBondData(v)
  91. local oldData = self.data.relationDatas[data.cfgId]
  92. if oldData then
  93. for _,v in pairs(oldData.relationPets) do
  94. if v.uid == 0 and v.petId > 0 then
  95. local petData = ManagerContainer.DataMgr.PetDataMgr:GetPetDataById(v.petId)
  96. if petData then
  97. petData.isRelevant = false
  98. ManagerContainer.DataMgr.PetDataMgr:RefreshMasterAttr(v.petId)
  99. end
  100. end
  101. end
  102. end
  103. self:RemovePetRelationAttrs(oldData, data.cfgId)
  104. changedList[#changedList + 1] = data.cfgId
  105. --self:RecalcMasterAttr()
  106. self.data.relationDatas[data.cfgId] = data
  107. for _,v in pairs(data.relationPets) do
  108. if v.uid == 0 and v.petId > 0 then
  109. local petData = ManagerContainer.DataMgr.PetDataMgr:GetPetDataById(v.petId)
  110. petData.isRelevant = true
  111. LogError("===========PetRelationData:InitData============"..v.petId)
  112. else
  113. if v.petId == 0 then
  114. v.cfgId = 0
  115. v.advanceLevel = 0
  116. end
  117. end
  118. end
  119. self:IsNewRelationActived(data.cfgId, oldData, data, oldAttrDatas, newAttrDatas)
  120. end
  121. self:AddPetRelationAttrs(changedList)
  122. self:RecalcMasterAttr()
  123. ManagerContainer.LuaUIMgr:AttrNoticeDisplay1({oldAttrDatas, newAttrDatas})
  124. ManagerContainer.RedPointMgr.PetRPCtr:RefreshRelationRPState()
  125. end
  126. end
  127. function PetRelationData:RecalcBattlePetRelationAttr(isBattle, id)
  128. local relationDatas = self:GetAllPetRelationDatas()
  129. local list = {}
  130. for _,v in pairs(relationDatas) do
  131. local cfgData = ManagerContainer.CfgMgr:GetPetPartnerDataById(v.cfgId)
  132. local maxIdx = GetMaxActiveRelationIdx(v.relationPets, cfgData)
  133. if maxIdx > 0 then
  134. for i = maxIdx, 1, -1 do
  135. for k, v1 in pairs(cfgData["attribute"..i]) do
  136. if not list[v1[1]] then
  137. list[v1[1]] = 0
  138. end
  139. list[v1[1]] = list[v1[1]] + v1[2]
  140. end
  141. end
  142. end
  143. end
  144. local petActorData = ManagerContainer.DataMgr.PetDataMgr:GetPetActorData(id)
  145. for k,v in pairs(list) do
  146. if isBattle then
  147. if k <= Enum.HeroAttrType.RealHurt then
  148. local eAttr = petActorData:GetAdditionalAttr(k)
  149. local value = SDataUtil.Add(eAttr,v)
  150. petActorData:SetAdditionalAttr(k, value)
  151. elseif k >= Enum.HeroAttrType.STR_Percent then
  152. local eAttr = petActorData:GetAdditionalAttrPercent(k)
  153. local value = SDataUtil.Add(eAttr,v*0.0001)
  154. petActorData:SetAdditionalAttrPercent(k, value)
  155. end
  156. else
  157. if k <= Enum.HeroAttrType.RealHurt then
  158. local eAttr = petActorData:GetAdditionalAttr(k)
  159. local value = SDataUtil.InvConvert(SDataUtil.Sub(eAttr,v))
  160. if value < 0 then
  161. value = 0
  162. end
  163. petActorData:SetAdditionalAttr(k, value)
  164. elseif k >= Enum.HeroAttrType.STR_Percent then
  165. local eAttr = petActorData:GetAdditionalAttrPercent(k)
  166. local value = SDataUtil.InvConvert(SDataUtil.Sub(eAttr,v*0.0001))
  167. if value < 0 then
  168. value = 0
  169. end
  170. petActorData:SetAdditionalAttrPercent(k, value)
  171. end
  172. end
  173. end
  174. end
  175. function PetRelationData:AddPetRelationAttrs(changedList)
  176. local totalList = {}
  177. for k,v in pairs(self.data.relationDatas) do
  178. if not changedList or (changedList and CommonUtil.EleInTable(v.cfgId, changedList)) then
  179. local cfgData = ManagerContainer.CfgMgr:GetPetPartnerDataById(v.cfgId)
  180. local maxIdx = GetMaxActiveRelationIdx(v.relationPets, cfgData)
  181. if maxIdx > 0 and v.relationPets then
  182. for j = maxIdx,1,-1 do
  183. local attribute = cfgData['attribute'..j]
  184. if attribute then
  185. for i = 1,#attribute do
  186. local attrId = attribute[i][1]
  187. local value = attribute[i][2]
  188. if not totalList[attrId] then
  189. totalList[attrId] = 0
  190. end
  191. totalList[attrId] = totalList[attrId] + value
  192. end
  193. end
  194. end
  195. end
  196. end
  197. end
  198. for m = 1, 6 do
  199. local heroData = CommonUtil.GetHeroLogicDataByUid(m)
  200. if (m == 1 or heroData.owned) and heroData.battlePetId > 0 then
  201. local petId = heroData.battlePetId
  202. local petActorData = ManagerContainer.DataMgr.PetDataMgr:GetPetActorData(petId)
  203. for k,v1 in pairs(totalList) do
  204. if k <= Enum.HeroAttrType.RealHurt then
  205. local eAttr = petActorData:GetAdditionalAttr(k)
  206. petActorData:SetAdditionalAttr(k, SDataUtil.Add(eAttr,v1))
  207. elseif k >= Enum.HeroAttrType.STR_Percent then
  208. local eAttr = petActorData:GetAdditionalAttrPercent(k)
  209. petActorData:SetAdditionalAttrPercent(k, SDataUtil.Add(eAttr,v1 * 0.0001))
  210. end
  211. end
  212. end
  213. end
  214. end
  215. function PetRelationData:RecalcMasterAttr()
  216. for m = 1, 6 do
  217. local heroData = CommonUtil.GetHeroLogicDataByUid(m)
  218. if heroData.battlePetId > 0 then
  219. if m == 1 then
  220. ManagerContainer.DataMgr.UserData:CalcPetAttrs()
  221. elseif heroData.owned then
  222. ManagerContainer.DataMgr.PartnerData:CalcPetAttrsById(m)
  223. end
  224. end
  225. end
  226. end
  227. function PetRelationData:IsNewRelationActived(cfgId, oldData, newData, oldAttrDatas, newAttrDatas)
  228. local cfgData = ManagerContainer.CfgMgr:GetPetPartnerDataById(cfgId)
  229. local oldMaxIdx = oldData and GetMaxActiveRelationIdx(oldData.relationPets, cfgData) or 0
  230. local newMaxIdx = GetMaxActiveRelationIdx(newData.relationPets, cfgData)
  231. if newMaxIdx > 0 then
  232. if oldAttrDatas and newAttrDatas and newMaxIdx > oldMaxIdx then
  233. for i = newMaxIdx , oldMaxIdx + 1, -1 do
  234. for k, v in pairs(cfgData["attribute"..i]) do
  235. local key = v[1]
  236. local val = key > 21 and CommonUtil.GetPreciseDecimal(v[2] * 0.0001, 3) or math.floor(v[2])
  237. oldAttrDatas[key] = 0
  238. newAttrDatas[key] = newAttrDatas[key] and newAttrDatas[key] + val or val
  239. end
  240. end
  241. end
  242. end
  243. newData.newActived = newMaxIdx > oldMaxIdx and newMaxIdx or 0
  244. end
  245. function PetRelationData:RemovePetRelationAttrs(oldData, cfgId)
  246. local totalList = {}
  247. local cfgData = ManagerContainer.CfgMgr:GetPetPartnerDataById(cfgId)
  248. local maxIdx = oldData and GetMaxActiveRelationIdx(oldData.relationPets, cfgData) or 0
  249. if maxIdx > 0 and oldData and oldData.relationPets then
  250. for j = maxIdx,1,-1 do
  251. local attribute = cfgData['attribute'..j]
  252. if attribute then
  253. for i = 1,#attribute do
  254. local attrId = attribute[i][1]
  255. local value = attribute[i][2]
  256. if not totalList[attrId] then
  257. totalList[attrId] = 0
  258. end
  259. totalList[attrId] = totalList[attrId] + value
  260. end
  261. end
  262. end
  263. end
  264. for m = 1, 6 do
  265. local heroData = CommonUtil.GetHeroLogicDataByUid(m)
  266. if (m == 1 or heroData.owned) and heroData.battlePetId > 0 then
  267. local petId = heroData.battlePetId
  268. local petActorData = ManagerContainer.DataMgr.PetDataMgr:GetPetActorData(petId)
  269. for k,v1 in pairs(totalList) do
  270. if k <= Enum.HeroAttrType.RealHurt then
  271. local eAttr = petActorData:GetAdditionalAttr(k)
  272. local value = SDataUtil.InvConvert(SDataUtil.Sub(eAttr,v1))
  273. if value < 0 then
  274. value = 0
  275. end
  276. petActorData:SetAdditionalAttr(k, value)
  277. elseif k >= Enum.HeroAttrType.STR_Percent then
  278. local eAttr = petActorData:GetAdditionalAttrPercent(k)
  279. local value = SDataUtil.InvConvert(SDataUtil.Sub(eAttr,v1*0.0001))
  280. if value < 0 then
  281. value = 0
  282. end
  283. petActorData:SetAdditionalAttrPercent(k, value)
  284. end
  285. end
  286. end
  287. end
  288. end
  289. function PetRelationData:RefreshMySupportPets(list)
  290. if #list == 0 then
  291. return
  292. end
  293. local supportList = {}
  294. for k,v in pairs(list) do
  295. supportList[#supportList + 1] = v.pet_id
  296. if not self.data.mySupportPets[k] then
  297. self.data.mySupportPets[k] = {}
  298. end
  299. self.data.mySupportPets[k].id = v.pet_id
  300. self.data.mySupportPets[k].cd = v.end_cd_time
  301. self.data.mySupportPets[k].idx = k
  302. end
  303. local petDatas = ManagerContainer.DataMgr.PetDataMgr:GetPetDatas()
  304. for _,v in pairs(petDatas) do
  305. v.isSupport = CommonUtil.EleInTable(v.id, supportList)
  306. --LogError("=====PetRelationData:RefreshMySupportPets========="..Inspect(v))
  307. end
  308. end
  309. function PetRelationData:DeleteMySupportPets(list)
  310. if not list or #list == 0 then
  311. return
  312. end
  313. for k,v in pairs(self.data.mySupportPets) do
  314. if CommonUtil.EleInTable(v.id, list) then
  315. v.id = 0
  316. v.cd = 0
  317. end
  318. end
  319. end
  320. function PetRelationData:GetMySupportPets()
  321. return self.data.mySupportPets
  322. end
  323. function PetRelationData:GetAllPetRelationDatas()
  324. return self.data.relationDatas
  325. end
  326. function PetRelationData:GetPetRelationDataByCfgId(cfgId)
  327. return self.data.relationDatas[cfgId]
  328. end
  329. function PetRelationData:GetRelationPlayerNameByUid(uid)
  330. return self.data.relationPlayerNameMap[uid]
  331. end
  332. function PetRelationData:RefreshFriendSupportDatas(list)
  333. self.data.curFriendSupportDatas = {}
  334. for _,v in pairs(list) do
  335. local data = ProtocalDataNormal.ParseAssistData(v)
  336. self.data.curFriendSupportDatas[#self.data.curFriendSupportDatas + 1] = data
  337. end
  338. end
  339. function PetRelationData:GetCurFriendSupportDatas()
  340. return self.data.curFriendSupportDatas
  341. end
  342. function PetRelationData:QueryPetRelation()
  343. ManagerContainer.NetManager:SendMessage(ProtoMsgId.CS_PET_BOND_LIST_REQ, {})
  344. end
  345. function PetRelationData:QueryAssistList(cfgId)
  346. ManagerContainer.NetManager:SendMessage(ProtoMsgId.CS_PET_BOND_ASSIST_LIST_REQ, {bond_cfg_id = cfgId})
  347. end
  348. function PetRelationData:SendMySupport(datas)
  349. ManagerContainer.NetManager:SendMessage(ProtoMsgId.CS_PET_ASSIST_REQ, {pet_id_list = datas})
  350. end
  351. function PetRelationData:GetReplacePetRelationDataByUidAndPetId(uid, petId)
  352. for _,v in pairs(self.data.relationDatas) do
  353. for _,v1 in pairs(v.relationPets) do
  354. if v1.uid == uid and v1.petId == petId then
  355. return v
  356. end
  357. end
  358. end
  359. return nil
  360. end
  361. function PetRelationData:SendPetRelationActive(datas)
  362. local list = {}
  363. for _,v in pairs(datas) do
  364. local bondData = {}
  365. bondData.bond_cfg_id = v.cfgId
  366. bondData.bond_list = {}
  367. if v.relationPets then
  368. for i = 1, 3 do
  369. local data1 = v.relationPets[i]
  370. bondData.bond_list[i] = {}
  371. if data1 then
  372. bondData.bond_list[i].owner_uid = data1.uid
  373. bondData.bond_list[i].pet_id = data1.petId
  374. bondData.bond_list[i].pet_cfg_id = data1.cfgId
  375. bondData.bond_list[i].advance_level = data1.advanceLevel
  376. else
  377. bondData.bond_list[i].owner_uid = 0
  378. bondData.bond_list[i].pet_id = 0
  379. bondData.bond_list[i].pet_cfg_id = 0
  380. bondData.bond_list[i].advance_level = 0
  381. end
  382. end
  383. end
  384. list[#list + 1] = bondData
  385. end
  386. ManagerContainer.NetManager:SendMessage(ProtoMsgId.CS_PET_BOND_ACTIVE_REQ, {bond_data_list = list})
  387. end
  388. function PetRelationData:GetRelationRPStates()
  389. return self.data.relationRPStates
  390. end
  391. function PetRelationData:RefreshRelationRPStates()
  392. if not self.data.relationRPStates then
  393. self.data.relationRPStates = {}
  394. end
  395. local relationCfgs = ManagerContainer.CfgMgr:GetAllPetPartnerDatas()
  396. for i = 1, #relationCfgs do
  397. local cfgId = relationCfgs[i].Id
  398. if not self.data.relationRPStates[cfgId] then
  399. self.data.relationRPStates[cfgId] = {false, false, false}
  400. end
  401. local rpIdxList = ManagerContainer.RedPointMgr.PetRPCtr:RefresgPetRelationState(cfgId)
  402. for i = 1, 3 do
  403. local isIn = CommonUtil.EleInTable(i, rpIdxList)
  404. self.data.relationRPStates[cfgId][i] = isIn
  405. end
  406. end
  407. end
  408. function PetRelationData:RegisterNetEvents()
  409. ManagerContainer.NetManager:NetRegister(ProtoMsgId.SC_PET_BOND_LIST_NTF, function(data)
  410. self:RefreshRelationBondList(data.bond_list)
  411. end)
  412. ManagerContainer.NetManager:NetRegister(ProtoMsgId.SC_PET_BOND_LIST_ACK, function(data)
  413. if data.error == Enum.NetErrorCode.ERROR_OK then
  414. self:InitData(data)
  415. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.PET_RELATION_OPEN_NTF)
  416. end
  417. end)
  418. ManagerContainer.NetManager:NetRegister(ProtoMsgId.SC_PET_BOND_ASSIST_LIST_ACK, function(data)
  419. if data.error == Enum.NetErrorCode.ERROR_OK then
  420. self:RefreshFriendSupportDatas(data.bond_pet_list)
  421. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.PET_FRIEND_SUPPORT_OPEN_NTF, data.bond_cfg_id)
  422. end
  423. end)
  424. ManagerContainer.NetManager:NetRegister(ProtoMsgId.SC_PET_ASSIST_ACK, function(data)
  425. if data.error == Enum.NetErrorCode.ERROR_OK then
  426. self:RefreshMySupportPets(data.assist_list)
  427. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.PET_MY_SUPPORT_CHANGE_SUCCESS_NTF, true)
  428. end
  429. end)
  430. ManagerContainer.NetManager:NetRegister(ProtoMsgId.SC_PET_BOND_ACTIVE_ACK, function(data)
  431. if data.error == Enum.NetErrorCode.ERROR_OK then
  432. self:RefreshData(data.bond_data_list, data.uid_name_list)
  433. self:RefreshRelationRPStates()
  434. --ManagerContainer.RedPointMgr.PetRPCtr:RefreshRelationRPState()
  435. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.PET_RELATION_CHANGE_SUCCESS_NTF)
  436. end
  437. end)
  438. end
  439. function PetRelationData:Clear()
  440. self.data = {}
  441. self.data.relationDatas = {}
  442. self.data.relationPlayerNameMap = {}
  443. self.data.relationAttrMap = {}
  444. self.data.mySupportPets = {}
  445. for i = 1, Constant.MyPetSupportLimit do
  446. self.data.mySupportPets[i] = {}
  447. self.data.mySupportPets[i].id = 0
  448. self.data.mySupportPets[i].cd = 0
  449. self.data.mySupportPets[i].idx = i
  450. end
  451. self.data.relationRPStates = nil
  452. end
  453. function PetRelationData:Destroy()
  454. if self.Clear then
  455. self:Clear()
  456. end
  457. self:UnRegisterNetEvents()
  458. end
  459. function PetRelationData:UnRegisterNetEvents()
  460. ManagerContainer.NetManager:UnRegisterPbIdCallback(ProtoMsgId.SC_PET_BOND_LIST_NTF)
  461. ManagerContainer.NetManager:UnRegisterPbIdCallback(ProtoMsgId.SC_PET_BOND_LIST_ACK)
  462. ManagerContainer.NetManager:UnRegisterPbIdCallback(ProtoMsgId.SC_PET_BOND_ASSIST_LIST_ACK)
  463. ManagerContainer.NetManager:UnRegisterPbIdCallback(ProtoMsgId.SC_PET_ASSIST_ACK)
  464. ManagerContainer.NetManager:UnRegisterPbIdCallback(ProtoMsgId.SC_PET_BOND_ACTIVE_ACK)
  465. end
  466. return PetRelationData