TitleData.lua 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. local TitleData = class("TitleData", require("DataBase"))
  2. function TitleData:ctor()
  3. self.AllTitleDatas = {}
  4. self.TypeList = {}
  5. self.redPoint = {}
  6. self.currWearerId = 0
  7. self:Init()
  8. end
  9. function TitleData:Init()
  10. local DesignationCfg = ManagerContainer.CfgMgr:GetDesignationCfg()
  11. self.AllTitleDatas = {}
  12. for k, v in pairs(DesignationCfg) do
  13. if v.Type > 0 then
  14. if not self.AllTitleDatas[v.Type] then
  15. self.AllTitleDatas[v.Type] = {}
  16. self.redPoint[v.Type] = false
  17. table.insert(self.TypeList,v.Type)
  18. end
  19. local data = {head_id = k}
  20. self.AllTitleDatas[v.Type][k] = self:InitTitleData(data)
  21. end
  22. end
  23. table.sort(self.TypeList,function(a,b)
  24. return a < b
  25. end)
  26. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.RED_POINT_MGR_NOTICE, Enum.RPNotifyType.TitleRedPoint, self:GetRedPointResult())
  27. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.RED_POINT_MGR_NOTICE, Enum.RPNotifyType.None, self:GetRedPointResult())
  28. end
  29. function TitleData:JudgeTotal(_type, _total)
  30. if _type == Enum.TaskType.Level_Battle_Count then --同关
  31. return 1;
  32. else
  33. return _total;
  34. end
  35. end
  36. function TitleData:GetTitleDataByType(type)
  37. return self.AllTitleDatas[type]
  38. end
  39. function TitleData:GetTitleDataByTypeAndId(type,id)
  40. return self.AllTitleDatas[type] and self.AllTitleDatas[type][id]
  41. end
  42. function TitleData:GetAllActiveTitleListAttrs()
  43. local list = {}
  44. for _, value in pairs(self.AllTitleDatas) do
  45. for k, v in pairs(value) do
  46. if v.state >= 3 then
  47. local titleCfgData = ManagerContainer.CfgMgr:GetDesignationCfgById(v.titleId)
  48. local attrs = titleCfgData.Attribute
  49. if attrs and #attrs > 0 then
  50. for i = 1, #attrs do
  51. if not list[attrs[i][1]] then
  52. list[attrs[i][1]] = 0
  53. end
  54. list[attrs[i][1]] = list[attrs[i][1]] + attrs[i][2]
  55. end
  56. end
  57. end
  58. end
  59. end
  60. return list
  61. end
  62. function TitleData:GetAllActiveTitleList()
  63. local list = {}
  64. for _, value in pairs(self.AllTitleDatas) do
  65. for k, v in pairs(value) do
  66. if v.state >= 3 then
  67. table.insert(list,v)
  68. end
  69. end
  70. end
  71. table.sort(list,function(a, b)
  72. local titleCfgDataA = ManagerContainer.CfgMgr:GetDesignationCfgById(a.titleId)
  73. local titleCfgDataB = ManagerContainer.CfgMgr:GetDesignationCfgById(b.titleId)
  74. if titleCfgDataA.Type == titleCfgDataB.Type then
  75. return titleCfgDataA.Sort > titleCfgDataB.Sort
  76. else
  77. return titleCfgDataA.Type > titleCfgDataB.Type
  78. end
  79. end)
  80. return list
  81. end
  82. function TitleData:GetRedPointResult()
  83. local isshowRed = false
  84. for i = 1, #self.redPoint do
  85. self.redPoint[i] = false
  86. end
  87. for _, value in pairs(self.AllTitleDatas) do
  88. for k, v in pairs(value) do
  89. if v.state == 2 then
  90. isshowRed = true
  91. self.redPoint[_] = true
  92. end
  93. end
  94. end
  95. return isshowRed
  96. end
  97. function TitleData:GetTitleTypeList()
  98. return self.TypeList
  99. end
  100. function TitleData:GetTitleRedPointList()
  101. return self.redPoint
  102. end
  103. function TitleData:GetCurrWearerTitleId()
  104. return self.currWearerId
  105. end
  106. function TitleData:InitData(data)
  107. if not data or not self.AllTitleDatas then
  108. return
  109. end
  110. local headList = data.head
  111. local changeData = {}
  112. if headList and #headList > 0 then
  113. for i = 1, #headList do
  114. local id = headList[i].head_id
  115. local titleData = ManagerContainer.CfgMgr:GetDesignationCfgById(id)
  116. if titleData then
  117. if not self.AllTitleDatas[titleData.Type] then
  118. self.AllTitleDatas[titleData.Type] = {}
  119. end
  120. changeData[id] = self:InitTitleData(headList[i])
  121. self.AllTitleDatas[titleData.Type][id] = changeData[id]
  122. if self.currWearerId == id and self.AllTitleDatas[titleData.Type][id].state ~= 3 then
  123. self.currWearerId = 0
  124. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.TITLE_WEARER_DISBOARD_NOTICE)
  125. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.TITLE_ACTIVE_REDPOINT_NOTICE)
  126. end
  127. if self.AllTitleDatas[titleData.Type][id].state == 3 then
  128. self.currWearerId = id
  129. end
  130. if self.AllTitleDatas[titleData.Type][id].state == 2 then
  131. self.redPoint[titleData.Type] = true
  132. end
  133. end
  134. end
  135. end
  136. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.EID_TITLE_CHANGE,changeData)
  137. local isShowRedPoint = self:GetRedPointResult()
  138. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.RED_POINT_MGR_NOTICE, Enum.RPNotifyType.TitleRedPoint, isShowRedPoint)
  139. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.RED_POINT_MGR_NOTICE, Enum.RPNotifyType.None, isShowRedPoint)
  140. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.TITLE_ACTIVE_REDPOINT_NOTICE)
  141. end
  142. function TitleData:InitTitleData(data)
  143. if not data then
  144. return
  145. end
  146. local temp = {}
  147. temp.titleId = data.head_id
  148. temp.state = data.state and data.state + 1 or 1
  149. temp.endTime = data.end_time or 0
  150. if data.task_data and data.task_data.progress and #data.task_data.progress > 0 then
  151. temp.currCount = data.task_data.progress[1].value
  152. else
  153. temp.currCount = 0
  154. end
  155. local titleCfgData = ManagerContainer.CfgMgr:GetDesignationCfgById(data.head_id)
  156. local total = 0
  157. if titleCfgData and titleCfgData.TaskCondition and titleCfgData.TaskCondition then
  158. local _conditions = titleCfgData.TaskCondition[1]
  159. total = self:JudgeTotal(_conditions[1], _conditions[#_conditions])
  160. end
  161. temp.totalCount = total
  162. return temp
  163. end
  164. function TitleData:RegisterNetEvents()
  165. --称号数据请求返回
  166. ManagerContainer.NetManager:NetRegister(ProtoMsgId.SC_HEAD_OPERATE_ACK,self.RefreshTitleDataByAck,self)
  167. --称号状态更新
  168. ManagerContainer.NetManager:NetRegister(ProtoMsgId.SC_HEAD_UPDATE_NTF,self.RefreshTitleDataByNtf,self)
  169. end
  170. function TitleData:RefreshTitleDataByAck(data)
  171. if data.error == 0 then
  172. local type = data.operateType
  173. if type == 1 then --激活操作
  174. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay("ActivateSucess")
  175. elseif type == 2 then --佩戴操作
  176. ManagerContainer.LuaUIMgr:ErrorNoticeDisplay("WearSucess")
  177. self.currWearerId = data.headId
  178. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.TITLE_WEARER_DISBOARD_NOTICE)
  179. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.TITLE_ACTIVE_REDPOINT_NOTICE)
  180. elseif type == 3 then --卸下操作
  181. self.currWearerId = 0
  182. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.TITLE_WEARER_DISBOARD_NOTICE)
  183. ManagerContainer.LuaEventMgr:Dispatch(UIEventNames.TITLE_ACTIVE_REDPOINT_NOTICE)
  184. end
  185. end
  186. end
  187. function TitleData:Clear()
  188. end
  189. function TitleData:RefreshTitleDataByNtf(data)
  190. self:InitData(data)
  191. end
  192. function TitleData:Destroy()
  193. self.AllTitleDatas = nil
  194. self.TypeList = nil
  195. self.redPoint = nil
  196. self.currWearerId = nil
  197. self:UnRegisterNetEvents()
  198. end
  199. function TitleData:UnRegisterNetEvents()
  200. ManagerContainer.NetManager:UnRegisterPbIdCallback(ProtoMsgId.SC_HEAD_OPERATE_ACK)
  201. ManagerContainer.NetManager:UnRegisterPbIdCallback(ProtoMsgId.SC_HEAD_UPDATE_NTF)
  202. end
  203. return TitleData