UIRoleNaturePointCtr.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. local UIRoleNaturePointCtr = class("UIRoleNaturePointCtr", require("UICtrBase"))
  2. local isLeader = false
  3. local remainPoints = 0
  4. local num1, num2
  5. local curOwnIndex = 1
  6. local sortedPartnerDatas
  7. function UIRoleNaturePointCtr:Init(view)
  8. local val = GlobalConfig.Instance:GetConfigStrValue(153)
  9. local list = CommonUtil.DeserializeGlobalStrToTable(val)
  10. num1 = tonumber(list[1])
  11. num2 = tonumber(list[2])
  12. self.view = view
  13. end
  14. function UIRoleNaturePointCtr:SetData(data)
  15. self.asyncIdx = 0
  16. if data == nil then return end
  17. self.data = data
  18. isLeader = data == 1
  19. self:SetCurOwnerIndexById(data)
  20. end
  21. function UIRoleNaturePointCtr:GetAsyncIdx()
  22. self.asyncIdx = self.asyncIdx + 1
  23. return self.asyncIdx
  24. end
  25. function UIRoleNaturePointCtr:GetData()
  26. return self.data
  27. end
  28. function UIRoleNaturePointCtr:SetCurOwnerIndexById(id)
  29. if sortedPartnerDatas == nil then
  30. self:GetSlotSortPartnerDatas()
  31. end
  32. if id == 1 then
  33. curOwnIndex = 1
  34. else
  35. for k,v in pairs(sortedPartnerDatas) do
  36. if v.id == id then
  37. curOwnIndex = k + 1
  38. end
  39. end
  40. end
  41. end
  42. function UIRoleNaturePointCtr:GetSlotSortPartnerDatas()
  43. local partnerDatas = ManagerContainer.DataMgr.PartnerData:GetPartnerDatas()
  44. sortedPartnerDatas = {}
  45. for _,v in pairs(partnerDatas) do
  46. if v.owned then
  47. sortedPartnerDatas[#sortedPartnerDatas + 1] = v
  48. end
  49. end
  50. table.sort(sortedPartnerDatas, function (a, b)
  51. if a.owned and b.owned then
  52. return a.post < b.post
  53. else
  54. if a.owned and not b.owned then
  55. return true
  56. elseif not a.owned and b.owned then
  57. return false
  58. else
  59. return a.post < b.post
  60. end
  61. end
  62. end)
  63. end
  64. local function IndexOffset(curIndex, offset)
  65. local max = #sortedPartnerDatas + 1
  66. local num = curIndex + offset
  67. local index = num
  68. if num <= 0 then
  69. index = max
  70. elseif num > max then
  71. index = 1
  72. end
  73. return index
  74. end
  75. function UIRoleNaturePointCtr:GetCurOwnerIndex()
  76. return curOwnIndex
  77. end
  78. function UIRoleNaturePointCtr:SwichParterHero(offset)
  79. local curIndex = curOwnIndex
  80. curOwnIndex = IndexOffset(curIndex, offset)
  81. if curOwnIndex == 1 then
  82. self:SetData(1)
  83. else
  84. self:SetData(sortedPartnerDatas[curOwnIndex - 1].id)
  85. end
  86. return
  87. end
  88. function UIRoleNaturePointCtr:ResetRemainPoints()
  89. remainPoints = self:GetRoleDataAttrPoints(self.data)
  90. end
  91. function UIRoleNaturePointCtr:ChangeRemainPoint(num)
  92. -- remainPoints = remainPoints + num
  93. remainPoints = SDataUtil.Add(remainPoints,num)
  94. return remainPoints
  95. end
  96. function UIRoleNaturePointCtr:GetRemainPoint()
  97. return remainPoints
  98. end
  99. function UIRoleNaturePointCtr:GetAttrPlanePercent(val)
  100. return (val - 1) * num1 - num2 * math.pow(val - 1, 2)
  101. end
  102. function UIRoleNaturePointCtr:SendAttrChange(attrs)
  103. local table = {}
  104. for k,v in pairs(attrs) do
  105. if v > 0 then
  106. table[#table + 1] = {key = k, value = v}
  107. end
  108. end
  109. ManagerContainer.NetManager:SendMessage(ProtoMsgId.CS_ADD_ATTR_POINT_REQ, {attr_list = table, hero_id = self.data})
  110. end
  111. function UIRoleNaturePointCtr:SendResetPoint()
  112. ManagerContainer.NetManager:SendMessage(ProtoMsgId.CS_RESET_ATTR_POINT_REQ, {hero_id = self.data})
  113. end
  114. local function Attr2Points(totalCount, attr, pointData)
  115. if attr > 1 then
  116. attr = attr - 1
  117. totalCount = SDataUtil.Add(totalCount,pointData[attr].cost)
  118. -- totalCount = totalCount + pointData[attr].cost
  119. else
  120. return totalCount
  121. end
  122. return Attr2Points(totalCount, attr, pointData)
  123. end
  124. local function Attr2Points1(addPoint, totalCount, attr, pointData)
  125. local newAttr = attr + addPoint
  126. if SDataUtil.IsGreaterEqual(totalCount, pointData[newAttr].cost) then
  127. totalCount = SDataUtil.Sub(totalCount,pointData[newAttr].cost)
  128. addPoint = addPoint + 1
  129. else
  130. return addPoint, totalCount
  131. end
  132. return Attr2Points1(addPoint, totalCount, attr, pointData)
  133. end
  134. local function Points2Attr(totalCount, addAttr, attr, pointData)
  135. -- if pointData[attr].cost > remainPoints then
  136. -- return addAttr, totalCount
  137. -- end
  138. if SDataUtil.IsGreater(pointData[attr].cost, remainPoints) then
  139. return addAttr, totalCount
  140. end
  141. -- if pointData[attr].cost <= totalCount then
  142. if SDataUtil.IsLessEqual(pointData[attr].cost, totalCount) then
  143. -- totalCount = totalCount - pointData[attr].cost
  144. totalCount = SDataUtil.Sub(totalCount,pointData[attr].cost)
  145. attr = attr + 1
  146. addAttr = addAttr + 1
  147. else
  148. return addAttr, totalCount
  149. end
  150. return Points2Attr(totalCount, addAttr, attr, pointData)
  151. end
  152. local function AddNewAttr(addPoint, val, newVal, totalCount, pointData, percent, isEnd)
  153. addPoint, newVal = Attr2Points1(addPoint, newVal, val, pointData)
  154. if SDataUtil.IsGreater(newVal,0) and SDataUtil.IsGreater(totalCount,newVal) and not isEnd and percent < 100 then
  155. newVal = SDataUtil.Add(newVal,1)
  156. totalCount = SDataUtil.Sub(totalCount,1)
  157. return AddNewAttr(addPoint, val, newVal, totalCount, pointData, percent, isEnd)
  158. else
  159. return addPoint, newVal, totalCount
  160. end
  161. end
  162. local function AddOneNewAttr(val, totalCount, pointData)
  163. -- if pointData[val].cost <= totalCount then
  164. if SDataUtil.IsLessEqual(pointData[val].cost,totalCount) then
  165. -- totalCount = totalCount - pointData[val].cost
  166. totalCount = SDataUtil.Sub(totalCount,pointData[val].cost)
  167. return 1, totalCount
  168. end
  169. return 0, totalCount
  170. end
  171. function UIRoleNaturePointCtr:CalcAutoPoints()
  172. local userData = self:GetHeroLogicData()
  173. local baseAttrs = self:GetLastBaseAttrs()
  174. local autoData
  175. if isLeader then
  176. local jobData = ManagerContainer.CfgMgr:GetJobDataById(userData.configId)
  177. autoData = jobData.AutoAddPoint
  178. else
  179. local partnerData = ManagerContainer.CfgMgr:GetPartnerDataById(userData.configId)
  180. autoData = partnerData.AddPoint
  181. end
  182. local pointData = ManagerContainer.CfgMgr.QualityPointCfg
  183. local str = SDataUtil.InvConvert(baseAttrs[Enum.HeroAttrType.STR])
  184. local agi = SDataUtil.InvConvert(baseAttrs[Enum.HeroAttrType.AGI])
  185. local int = SDataUtil.InvConvert(baseAttrs[Enum.HeroAttrType.INT])
  186. local vit = SDataUtil.InvConvert(baseAttrs[Enum.HeroAttrType.VIT])
  187. local dex = SDataUtil.InvConvert(baseAttrs[Enum.HeroAttrType.DEX])
  188. local luk = SDataUtil.InvConvert(baseAttrs[Enum.HeroAttrType.LUK])
  189. local totalCount = self:GetRoleDataAttrPoints(self.data)
  190. remainPoints = totalCount
  191. local oldTotalCount = 0
  192. local curTotalCountMap = {}
  193. for _,v in pairs(autoData) do
  194. local type = v[1]
  195. local curTotalCount = 0
  196. if type == Enum.HeroAttrType.STR then
  197. curTotalCount = Attr2Points(curTotalCount, str, pointData)
  198. elseif type == Enum.HeroAttrType.AGI then
  199. curTotalCount = Attr2Points(curTotalCount, agi, pointData)
  200. elseif type == Enum.HeroAttrType.INT then
  201. curTotalCount = Attr2Points(curTotalCount, int, pointData)
  202. elseif type == Enum.HeroAttrType.VIT then
  203. curTotalCount = Attr2Points(curTotalCount, vit, pointData)
  204. elseif type == Enum.HeroAttrType.DEX then
  205. curTotalCount = Attr2Points(curTotalCount, dex, pointData)
  206. elseif type == Enum.HeroAttrType.LUK then
  207. curTotalCount = Attr2Points(curTotalCount, luk, pointData)
  208. end
  209. curTotalCountMap[type] = curTotalCount
  210. oldTotalCount = SDataUtil.Add(oldTotalCount, curTotalCount)
  211. end
  212. local oneAttrAddDic = {}
  213. local totalStatus = {}
  214. local function NeedOver()
  215. for k, v in pairs(totalStatus) do
  216. if v then
  217. return false
  218. end
  219. end
  220. return true
  221. end
  222. local newTotalCount = SDataUtil.Add(oldTotalCount, totalCount)
  223. for i = 1, #autoData do
  224. totalStatus[i] = true
  225. local type = autoData[i][1]
  226. local percent = autoData[i][2]
  227. local attrNewCount = 0
  228. local isEnd = false
  229. if i < #autoData then
  230. -- attrNewCount = math.floor(totalCount * percent / 100)
  231. attrNewCount = SDataUtil.Div(SDataUtil.Multiply(newTotalCount,percent),100)
  232. if SDataUtil.IsGreaterEqual(attrNewCount, curTotalCountMap[type]) then
  233. local delta = SDataUtil.Sub(attrNewCount, curTotalCountMap[type])
  234. if SDataUtil.IsGreater(delta, 0) then
  235. if SDataUtil.IsGreater(totalCount, delta) then
  236. attrNewCount = delta
  237. totalCount = SDataUtil.Sub(totalCount, delta)
  238. else
  239. attrNewCount = totalCount
  240. totalCount = 0
  241. end
  242. else
  243. attrNewCount = 0
  244. end
  245. else
  246. attrNewCount = 0
  247. end
  248. else
  249. isEnd = true
  250. attrNewCount = totalCount
  251. totalCount = 0
  252. end
  253. oneAttrAddDic[type] = 0
  254. if SDataUtil.IsGreater(attrNewCount, 0) then
  255. local addPoint = 0
  256. local remain = 0
  257. if type == Enum.HeroAttrType.STR then
  258. oneAttrAddDic[type], remain, totalCount = AddNewAttr(addPoint, str, attrNewCount, totalCount, pointData, percent, isEnd)
  259. str = str + oneAttrAddDic[type]
  260. elseif type == Enum.HeroAttrType.AGI then
  261. oneAttrAddDic[type], remain, totalCount = AddNewAttr(addPoint, agi, attrNewCount, totalCount, pointData, percent, isEnd)
  262. agi = agi + oneAttrAddDic[type]
  263. elseif type == Enum.HeroAttrType.INT then
  264. oneAttrAddDic[type], remain, totalCount = AddNewAttr(addPoint, int, attrNewCount, totalCount, pointData, percent, isEnd)
  265. int = int + oneAttrAddDic[type]
  266. elseif type == Enum.HeroAttrType.VIT then
  267. oneAttrAddDic[type], remain, totalCount = AddNewAttr(addPoint, vit, attrNewCount, totalCount, pointData, percent, isEnd)
  268. vit = vit + oneAttrAddDic[type]
  269. elseif type == Enum.HeroAttrType.DEX then
  270. oneAttrAddDic[type], remain, totalCount = AddNewAttr(addPoint, dex, attrNewCount, totalCount, pointData, percent, isEnd)
  271. dex = dex + oneAttrAddDic[type]
  272. elseif type == Enum.HeroAttrType.LUK then
  273. oneAttrAddDic[type], remain, totalCount = AddNewAttr(addPoint, luk, attrNewCount, totalCount, pointData, percent, isEnd)
  274. luk = luk + oneAttrAddDic[type]
  275. end
  276. if SDataUtil.IsGreater(remain, 0) then
  277. totalCount = SDataUtil.Add(totalCount, remain)
  278. end
  279. end
  280. end
  281. while(true) do
  282. for k,v in pairs(autoData) do
  283. local type = v[1]
  284. local add = 0
  285. if type == Enum.HeroAttrType.STR then
  286. add, totalCount = AddOneNewAttr(str, totalCount, pointData)
  287. str = str + add
  288. elseif type == Enum.HeroAttrType.AGI then
  289. add, totalCount = AddOneNewAttr(agi, totalCount, pointData)
  290. agi = agi + add
  291. elseif type == Enum.HeroAttrType.INT then
  292. add, totalCount = AddOneNewAttr(int, totalCount, pointData)
  293. int = int + add
  294. elseif type == Enum.HeroAttrType.VIT then
  295. add, totalCount = AddOneNewAttr(vit, totalCount, pointData)
  296. vit = vit + add
  297. elseif type == Enum.HeroAttrType.DEX then
  298. add, totalCount = AddOneNewAttr(dex, totalCount, pointData)
  299. dex = dex + add
  300. elseif type == Enum.HeroAttrType.LUK then
  301. add, totalCount = AddOneNewAttr(luk, totalCount, pointData)
  302. luk = luk + add
  303. end
  304. oneAttrAddDic[type] = oneAttrAddDic[type] + add
  305. if add == 0 then
  306. totalStatus[k] = false
  307. end
  308. end
  309. if NeedOver() then
  310. break
  311. end
  312. end
  313. --if SDataUtil.IsGreater(totalCount,0) then
  314. -- local add = 0
  315. -- add, totalCount = AddOneNewAttr(luk, totalCount, pointData)
  316. -- if oneAttrAddDic[Enum.HeroAttrType.LUK] == nil then
  317. -- oneAttrAddDic[Enum.HeroAttrType.LUK] = 0
  318. -- end
  319. -- oneAttrAddDic[Enum.HeroAttrType.LUK] = oneAttrAddDic[Enum.HeroAttrType.LUK] + add
  320. --end
  321. remainPoints = totalCount
  322. return oneAttrAddDic
  323. end
  324. function UIRoleNaturePointCtr:IsLeader()
  325. return isLeader
  326. end
  327. function UIRoleNaturePointCtr:GetHeroLogicData()
  328. if isLeader then
  329. return ManagerContainer.DataMgr.UserData:GetHeroData()
  330. else
  331. return ManagerContainer.DataMgr.PartnerData:GetPartnerDataByUniqueId(self.data)
  332. end
  333. end
  334. function UIRoleNaturePointCtr:GetLastBaseAttrs()
  335. if isLeader then
  336. return ManagerContainer.DataMgr.UserData:GetLastBaseAttrs()
  337. else
  338. return ManagerContainer.DataMgr.PartnerData:GetLastBaseAttrs(self.data)
  339. end
  340. end
  341. function UIRoleNaturePointCtr:GetRoleDataAttrPoints(id)
  342. if id == 1 then
  343. return ManagerContainer.DataMgr.UserData:GetRoleDataAttrPoints()
  344. else
  345. return ManagerContainer.DataMgr.PartnerData:GetRoleDataAttrPoints(id)
  346. end
  347. end
  348. function UIRoleNaturePointCtr:OnDispose()
  349. sortedPartnerDatas = nil
  350. self.data = nil
  351. self.view = nil
  352. end
  353. return UIRoleNaturePointCtr