RoleAttr.lua 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. RoleAttr = {}
  2. local this = {}
  3. ---@class RoleAttrProgrammeData
  4. ---@field current number 当前方案
  5. ---@field freeSwitchAttrPointData table<number,FreeSwitchAttrPointData>
  6. ---@class FreeSwitchAttrPointData
  7. ---@field id number
  8. ---@field attr table
  9. ---@field name string
  10. this.ROLE_ATTR_CHANGE_LINE_KEY = "T$_ROLE_ATTR_CHANGE_LINE_KEY"
  11. RoleAttrKey = {
  12. UNION_ARMBAND_LEVEL = "roleunionarmbandlevelattrkey", -- 战盟臂章等级属性
  13. UNION_ARMBAND_STRONG = "roleunionarmbandstrongattrkey", -- 战盟臂章强化属性
  14. PRIVILEGE_MONTH_DROP_RATE = "privilegemonthdroprateattrpointkey", -- 月卡特权增加爆率
  15. EXP_BOOST_ITEM_USE = "roleexpboostitemuseattrpointkey", -- 经验药水使用增加杀怪经验提升
  16. VIP_GIFT_PACK = "rolevipgiftpackprivilegekey", -- VIP特权增加属性
  17. ANGEL_MAJOR_EQUIPMENT = "roleangelmajorattrkey_%s", -- 大天使装备属性
  18. ANGEL_MAJOR_TALENT = "roleangelmajorattrkey", -- 大天使天赋属性
  19. ANGEL_GRAIL = "roleangelgrailattrkey_%s", -- 大天使圣杯属性
  20. PRIVILEGE_EXP_BOOST = "roleexpboostprivilegekey", -- 经验提升特权
  21. ANGEL_GRAIL_SUIT = "roleangelgrailsuitattrkey", -- 大天使圣杯套装属性
  22. }
  23. function RoleAttr.levelup(actor, level, oldlevel)
  24. this.addAttrPointByCareer(actor, level, oldlevel)
  25. end
  26. -- 添加属性,并把属性保存起来,下次添加属性自动叠加
  27. function RoleAttr.addAndSaveRoleAttr(actor, key, attr_map)
  28. this.addAndSaveRoleAttr(actor, key, attr_map)
  29. end
  30. -- 减少属性,并把属性保存起来,下次添加属性自动相减
  31. function RoleAttr.reduceAndSaveRoleAttr(actor, key, attr_map)
  32. this.reduceAndSaveRoleAttr(actor, key, attr_map)
  33. end
  34. -- 清空指定Key的属性和保存的数据
  35. function RoleAttr.clearRoleAttrAndDB(actor, key)
  36. this.clearRoleAttrAndDB(actor, key)
  37. end
  38. function RoleAttr.createrole(actor)
  39. -- this.createRoleAddAttrPointByCareer(actor)
  40. end
  41. function RoleAttr.setPrivilegeAttrPointProgramme(actor, data)
  42. this.setPrivilegeAttrPointProgramme(actor, data)
  43. end
  44. function RoleAttr.getPrivilegeAttrPointProgramme(actor)
  45. local data = this.getPrivilegeData(actor)
  46. this.jprint("--- 获取属性点方案数据 ---")
  47. this.jprint(data)
  48. sendluamsg(actor, LuaMessageIdToClient.RES_GET_ROLE_ATTR_POINT_PROGRAMME, data)
  49. end
  50. function RoleAttr.changePrivilegeAttrPointProgramme(actor, key)
  51. this.changePrivilegeAttrPointProgramme(actor, key)
  52. end
  53. -----------------------------------
  54. function this.createRoleAddAttrPointByCareer(actor)
  55. --local level = getbaseinfo(actor, "level")
  56. --this.addAttrPointByCareer(actor, level, 0)
  57. end
  58. function this.setPrivilegeAttrPointProgramme(actor, param)
  59. local data = this.getPrivilegeData(actor)
  60. if table.isEmpty(data) then
  61. data = {}
  62. data.current = 1
  63. end
  64. this.jprint("--- param ---")
  65. this.jprint(param)
  66. if table.isEmpty(data.freeSwitchAttrPointData) then
  67. data.freeSwitchAttrPointData = {}
  68. end
  69. data.freeSwitchAttrPointData[param.id] = param
  70. this.setPrivilegeData(actor, data)
  71. RoleAttr.getPrivilegeAttrPointProgramme(actor)
  72. end
  73. ---@return RoleAttrProgrammeData
  74. function this.getPrivilegeData(actor)
  75. return getplaydef(actor, this.ROLE_ATTR_CHANGE_LINE_KEY)
  76. end
  77. function this.setPrivilegeData(actor, data)
  78. setplaydef(actor, this.ROLE_ATTR_CHANGE_LINE_KEY, data)
  79. end
  80. function this.changePrivilegeAttrPointProgramme(actor, key)
  81. local is_has = PrivilegeMonth.hasPrivilege(actor, PrivilegeMonth.PrivilegeType.SWITCHING_POINT_SCHEME)
  82. if not is_has then
  83. this.jprint("玩家没有权限")
  84. return
  85. end
  86. local data = this.getPrivilegeData(actor)
  87. if table.isEmpty(data) then
  88. sendluamsg(actor, LuaMessageIdToClient.RES_CHANGE_ROLE_ATTR_POINT_PROGRAMME, key)
  89. return
  90. end
  91. this.jprint("key")
  92. this.jprint(key)
  93. this.jprint("--- data ---")
  94. this.jprint(data)
  95. if data.current == key then
  96. this.jprint("当前方案和要切换的方案一样,无需切换")
  97. return
  98. end
  99. data.current = key
  100. this.setPrivilegeData(actor, data)
  101. resetattrpoint(actor, 1, 0)
  102. local programme_data = data.freeSwitchAttrPointData[key]
  103. if table.isEmpty(programme_data) then
  104. sendluamsg(actor, LuaMessageIdToClient.RES_CHANGE_ROLE_ATTR_POINT_PROGRAMME, key)
  105. return
  106. end
  107. this.jprint("-- programme_data --")
  108. this.jprint(programme_data)
  109. if not table.isEmpty(programme_data.attr) then
  110. attr_map = {}
  111. for _, v in pairs(programme_data.attr) do
  112. attr_map[v.type] = v.num
  113. end
  114. this.jprint("-- attr_map --")
  115. this.jprint(attr_map)
  116. deployattrpoint(actor, attr_map, 1)
  117. end
  118. sendluamsg(actor, LuaMessageIdToClient.RES_CHANGE_ROLE_ATTR_POINT_PROGRAMME, key)
  119. end
  120. function this.addAttrPointByCareer(actor, level, oldlevel)
  121. this.jprint(oldlevel)
  122. this.jprint(level)
  123. local career = getbaseinfo(actor, "getbasecareer")
  124. career = tonumber(career)
  125. this.jprint("职业:" .. career)
  126. local key = "";
  127. if career == 1 then
  128. key = "swordsmanAttribute"
  129. end
  130. if career == 2 then
  131. key = "magicianAttribute"
  132. end
  133. if career == 3 then
  134. key = "archerAttributes"
  135. end
  136. if career == 4 then
  137. key = "magicSwordsmanAttribute"
  138. end
  139. if career == 5 then
  140. key = "saintMentorAttribute"
  141. end
  142. if career == 6 then
  143. key = "summonerAttribute"
  144. end
  145. if key == "" then
  146. this.jprint("职业错误")
  147. return
  148. end
  149. local attr_map = this.getAttrMap(actor)
  150. for i = oldlevel + 1, level do
  151. this.calculAttr(actor, i, key, attr_map)
  152. end
  153. if next(attr_map) == nil then
  154. return
  155. end
  156. this.jprint("添加属性点")
  157. this.jprint(attr_map)
  158. this.setAttrMap(actor, attr_map)
  159. addrolekmlattributes(actor, "luaaddattrpointbycareer", attr_map)
  160. end
  161. function this.getAttrMap(actor)
  162. local data = getplaydef(actor, "T$_luaaddattrpointbycareer")
  163. if data == nil then
  164. data = {}
  165. end
  166. return data
  167. end
  168. function this.setAttrMap(actor, attr_map)
  169. setplaydef(actor, "T$_luaaddattrpointbycareer", attr_map)
  170. end
  171. function this.calculAttr(actor, level, key, attr_map)
  172. local attr_map_str = ConfigDataManager.getTableValue("cfg_level", key, "id", level)
  173. if attr_map_str == nil or attr_map_str == "" then
  174. return
  175. end
  176. local map = string.toIntIntMap(attr_map_str, "#", "|")
  177. this.jprint("map")
  178. for attr_id, value in pairs(map) do
  179. this.jprint(attr_id)
  180. this.jprint(value)
  181. local attr_value = attr_map[attr_id]
  182. this.jprint("attr_value")
  183. this.jprint(attr_value)
  184. if attr_value ~= nil and tonumber(attr_value) > 0 then
  185. attr_value = attr_value + value
  186. else
  187. attr_value = value
  188. end
  189. attr_map[attr_id] = attr_value
  190. end
  191. end
  192. function this.addAndSaveRoleAttr(actor, key, attr_map)
  193. if table.isEmpty(attr_map) then
  194. return
  195. end
  196. local db_key = "T$_" .. key
  197. local data = getplaydef(actor, db_key)
  198. if table.isEmpty(data) then
  199. data = attr_map
  200. else
  201. for attr_id, value in pairs(attr_map) do
  202. if data[attr_id] == nil then
  203. data[attr_id] = value
  204. else
  205. data[attr_id] = data[attr_id] + value
  206. end
  207. end
  208. end
  209. setplaydef(actor, db_key, data)
  210. addrolekmlattributes(actor, key, data)
  211. this.jprint("---- 添加属性 ----")
  212. this.jprint("key")
  213. this.jprint(key)
  214. this.jprint("data")
  215. this.jprint(data)
  216. end
  217. function this.reduceAndSaveRoleAttr(actor, key, attr_map)
  218. if table.isEmpty(attr_map) then
  219. return
  220. end
  221. local db_key = "T$_" .. key
  222. local data = getplaydef(actor, db_key)
  223. if table.isEmpty(data) then
  224. data = {}
  225. end
  226. for attr_id, value in pairs(attr_map) do
  227. if data[attr_id] ~= nil then
  228. data[attr_id] = math.max(0, data[attr_id] - value)
  229. end
  230. end
  231. setplaydef(actor, db_key, data)
  232. addrolekmlattributes(actor, key, data)
  233. end
  234. function this.clearRoleAttrAndDB(actor, key)
  235. local db_key = "T$_" .. key
  236. setplaydef(actor, db_key, {})
  237. addrolekmlattributes(actor, key, {})
  238. end
  239. ---------------------------- 日志打印 -----------------------------
  240. this.log_open = false
  241. function this.jprint(param)
  242. if not this.log_open then
  243. return
  244. end
  245. if param == nil then
  246. param = "error! 输出内容为空. nil"
  247. end
  248. jprint(param)
  249. end
  250. function this.loginfo(actor, param)
  251. if not this.log_open then
  252. return
  253. end
  254. if param == nil then
  255. param = "error! 日志输出内容为空. nil"
  256. end
  257. jprint(param)
  258. info(actor, param)
  259. end
  260. -- 设置角色属性加成方式
  261. function RoleAttr.setRoleAttAddWay(actor, msgData)
  262. local addAttWay = setroleattaddway(actor, msgData[1])
  263. sendluamsg(actor, LuaMessageIdToClient.RES_ROLE_ATT_ADD_WAY, addAttWay)
  264. end
  265. -- 设置角色属性加成方式
  266. function RoleAttr.getRoleAttAddWay(actor)
  267. local addAttWay = getrolefield(actor, "role.RoleAttribute.attrAddWay")
  268. sendluamsg(actor, LuaMessageIdToClient.RES_ROLE_ATT_ADD_WAY, addAttWay)
  269. end
  270. function RoleAttr.transferCareer(actor, careerName)
  271. local wash = ConfigDataManager.getTableValue("cfg_career", "wash", "name", careerName)
  272. if tonumber(wash) == 1 then
  273. resetattrpoint(actor, 1)
  274. end
  275. local rec = getplaydef(actor, PlayerDefKey.player.CAREER_REC)
  276. local msgData = {}
  277. if not string.isNullOrEmpty(rec) then
  278. msgData[1] = rec
  279. setplaydef(actor, PlayerDefKey.player.CAREER_REC, nil)
  280. else
  281. local careerRec = ConfigDataManager.getTableValue("cfg_career", "careerRec", "name", careerName)
  282. local careerRecTable = string.split(careerRec, "|")
  283. if #careerRecTable ~= 1 then
  284. msgData[1] = -1
  285. end
  286. end
  287. if not table.isNullOrEmpty(msgData) then
  288. RoleAttr.setRoleAttAddWay(actor, msgData)
  289. end
  290. end
  291. -- @description 脚本计算角色最终属性
  292. -- @param 玩家对象
  293. -- @return
  294. function RoleAttr.CalcPlayFinalAttr(actor)
  295. --攻速加成=(1+attackSpeed*攻击速度转换系数)*(AniSpeedBonus)-1
  296. local aniSpeed = getattrinfo(actor, "attackSpeed")
  297. local aniSpeedBonus = getattrinfo(actor, "AniSpeedBonus")
  298. local coefficient = tonumber(ConfigDataManager.getTableValue("cfg_global", "value", "id", 7001))
  299. if coefficient == nil then
  300. coefficient = 0.0067
  301. else
  302. coefficient = coefficient / 10000
  303. end
  304. local atkSpeed = (1 + aniSpeed * coefficient) * aniSpeedBonus - 1
  305. --jprint(name,"属性计算","aniSpeed",aniSpeed)
  306. --jprint(name,"属性计算","coefficient",coefficient)
  307. --jprint(name,"属性计算","aniSpeedBonus",aniSpeedBonus)
  308. --jprint(name,"属性计算","aniSpeed",aniSpeed)
  309. --jprint(name,"属性计算","attackSpeedBonus",atkSpeed)
  310. local id = attrname2id("attackSpeedBonus")
  311. local remarks = tonumber(ConfigDataManager.getTableValue("cfg_att_info", "remarks", "id", id))
  312. if remarks == 2 then
  313. atkSpeed = atkSpeed * 10000
  314. if atkSpeed < 0 then
  315. atkSpeed = 0
  316. end
  317. end
  318. local ret = {}
  319. local innerData = { id = id, value = atkSpeed }
  320. table.insert(ret, innerData)
  321. -- 自动恢复魔法
  322. local mpRecovery = getattrinfo(actor, "MPFullRecoverRate")
  323. if mpRecovery and mpRecovery ~= "" and mpRecovery > 0 then
  324. local roleInfo = getsysvar(SystemVarConst.AUTO_RECOVERY_MAGIC_ROLE_INFO)
  325. if not table.isEmpty(roleInfo) then
  326. local exist = false
  327. for _, v in pairs(roleInfo) do
  328. if actor:toString() == v["actor"]:toString() then
  329. exist = true
  330. end
  331. end
  332. if not exist then
  333. table.insert(roleInfo, {
  334. actor = actor,
  335. recovery = mpRecovery
  336. })
  337. setsysvar(SystemVarConst.AUTO_RECOVERY_MAGIC_ROLE_INFO, roleInfo)
  338. end
  339. else
  340. local temp = {}
  341. table.insert(temp, {
  342. actor = actor,
  343. recovery = mpRecovery
  344. })
  345. setsysvar(SystemVarConst.AUTO_RECOVERY_MAGIC_ROLE_INFO, temp)
  346. GlobalTimer.setontimerex(TimerIds.AUTO_RECOVERY_MAGIC, TimeUnit.SECOND * 3)
  347. end
  348. end
  349. return ret
  350. end
  351. --- 自动恢复魔法
  352. function RoleAttr.autoRecoveryMagic()
  353. local roleInfo = getsysvar(SystemVarConst.AUTO_RECOVERY_MAGIC_ROLE_INFO)
  354. if not table.isEmpty(roleInfo) then
  355. for key, actorInfo in pairs(roleInfo) do
  356. local actor = actorInfo["actor"]
  357. local mpFullRecoverRate = actorInfo["recovery"]
  358. if mpFullRecoverRate > 0 then
  359. local mp = getbaseinfo(actor, "mp")
  360. local maxMp = getbaseinfo(actor, "maxmp")
  361. local incrementMp = maxMp * mpFullRecoverRate
  362. if mp + incrementMp > maxMp then
  363. setmp(actor, maxMp)
  364. table.remove(roleInfo, key)
  365. else
  366. setmp(actor, mp + incrementMp)
  367. end
  368. else
  369. table.remove(roleInfo, key)
  370. end
  371. end
  372. setsysvar(SystemVarConst.AUTO_RECOVERY_MAGIC_ROLE_INFO, roleInfo)
  373. if table.isEmpty(roleInfo) then
  374. GlobalTimer.setofftimer(TimerIds.AUTO_RECOVERY_MAGIC)
  375. end
  376. end
  377. end
  378. --- 切换地图后将actor对象mapId修改
  379. ---@param actor table 玩家对象
  380. ---@param mapCfgId number 地图配置id
  381. function RoleAttr.enterMap(actor, mapCfgId)
  382. local roleInfo = getsysvar(SystemVarConst.AUTO_RECOVERY_MAGIC_ROLE_INFO)
  383. if not table.isEmpty(roleInfo) then
  384. for k, v in pairs(roleInfo) do
  385. local player = v["actor"]
  386. local recovery = v["recovery"]
  387. if player:toString() == actor:toString() then
  388. local getActor = getactor(actor, player:toString(), gamemap.getMapKey(mapCfgId, 1))
  389. table.remove(roleInfo, k)
  390. table.insert(roleInfo, {
  391. actor = getActor,
  392. recovery = recovery
  393. })
  394. break
  395. end
  396. end
  397. setsysvar(SystemVarConst.AUTO_RECOVERY_MAGIC_ROLE_INFO, roleInfo)
  398. end
  399. end