talent.lua 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. local schema = require "model.schema"
  2. local logger = require "logger"
  3. local stringify = require "stringify"
  4. local asset = require "model.asset"
  5. local payment = require "model.payment"
  6. local module_name = "talent"
  7. local _M = schema.new(module_name, {
  8. list = {
  9. -- [type] = {type = 1, id = id}
  10. }
  11. })
  12. local REQUEST = {}
  13. local CMD = {}
  14. local MODULE = {}
  15. local THIS = {}
  16. local function func_ret(fname, character, args)
  17. local f = THIS[fname]
  18. if not f then
  19. logger.error("func_ret not fname:%s !!!", fname)
  20. return {errno = STD_ERR.COMMON_SYS_ERR}
  21. end
  22. local errno, ret = f(character, args)
  23. if errno ~= 0 then
  24. return {errno = errno}
  25. end
  26. ret = ret or {}
  27. ret.errno = 0
  28. return ret
  29. end
  30. function MODULE.list_request_interests() return REQUEST end
  31. function MODULE.list_command_interests() return CMD end
  32. -- TODO: 解析/升级模块数据 在这里把数据初始化好
  33. function MODULE.parse(character)
  34. local d = _M.load(character)
  35. if not d.list then
  36. d.list = {}
  37. end
  38. end
  39. -- TODO: 侦听事件
  40. function MODULE.monitor(character)
  41. end
  42. -- TODO: 类似泰利的 prepare 接口
  43. function MODULE.launch(character)
  44. local d = _M.assert_get(character)
  45. local u = _M.assert_runtime(character)
  46. end
  47. -- TODO: 与客户端同步数据
  48. function MODULE.ready(character)
  49. local d = _M.assert_get(character)
  50. logger.test("%s:ready, %s", module_name, stringify(d or {}))
  51. end
  52. -- TODO: 玩家下线时的处理
  53. function MODULE.saybye(character)
  54. end
  55. function MODULE.talent_get_data(character)
  56. local d = _M.assert_get(character)
  57. local ret = {}
  58. for _, v in pairs(d.list or {}) do
  59. if v.type == 1 then
  60. ret.id1 = v.id
  61. elseif v.type == 2 then
  62. ret.id2 = v.id
  63. end
  64. end
  65. return ret
  66. end
  67. function THIS.talent_get_data(character, args)
  68. local ret = MODULE.talent_get_data(character)
  69. return 0, ret
  70. end
  71. function THIS.talent_activate(character, args)
  72. local id = args.id
  73. if not id then
  74. return STD_ERR.PLYAER_PARM_LIMIT -- 参数异常
  75. end
  76. local d = _M.assert_get(character)
  77. local conf_list = assert(asset.TalentConfig_proto, "TalentConfig_proto")
  78. local conf = conf_list[id]
  79. if not conf then
  80. return STD_ERR.PLYAER_PARM_LIMIT -- 参数异常
  81. end
  82. if character.level < conf.lv then
  83. return STD_ERR.PLYAER_PARM_LIMIT -- 参数异常
  84. end
  85. local data = d.list[conf.talentType] or {type = conf.talentType, id = 0}
  86. if data.id ~= conf.condition then
  87. return STD_ERR.PLYAER_PARM_LIMIT -- 参数异常
  88. end
  89. local pay_list = {}
  90. for i = 1, #conf.goods, 2 do
  91. local item_id = conf.goods[i]
  92. local item_num = conf.goods[i+1]
  93. if item_id and item_num and item_id > 0 and item_num > 0 then
  94. table.insert(pay_list, {id = item_id, num = item_num})
  95. end
  96. end
  97. if next(pay_list) then
  98. local receipt = {
  99. module="talent",
  100. brief="天赋激活",
  101. context="天赋激活:"..id,
  102. notify={
  103. flags="talent_activate"
  104. },
  105. detail=pay_list
  106. }
  107. local errno = payment(character, receipt)
  108. if errno ~= 0 then
  109. return errno
  110. end
  111. end
  112. data.id = id
  113. d.list[conf.talentType] = data
  114. _M.persist(character)
  115. character.dispatch("telent_activate")
  116. local ctx = _M.assert_runtime(character)
  117. ctx.special_list = nil
  118. return 0, {id = args.id}
  119. end
  120. -- 天赋数据
  121. function REQUEST.talent_get_data(character, args)
  122. return func_ret("talent_get_data", character, args)
  123. end
  124. function REQUEST.talent_activate(character, args)
  125. return func_ret("talent_activate", character, args)
  126. end
  127. function MODULE.get_special_attr(character)
  128. local ctx = _M.assert_runtime(character)
  129. if ctx.special_list then
  130. return ctx.special_list
  131. end
  132. local list = {}
  133. local d = _M.assert_get(character) or {}
  134. for _, talent_conf in pairs(asset.TalentConfig_proto)do
  135. local data = d.list[talent_conf.talentType]
  136. if data and data.id >= talent_conf.ID then
  137. local conf = asset.EntryConfig_proto[talent_conf.entryID]
  138. if conf and conf.type and conf.type >= 100 then
  139. if conf.rateArr[1] and conf.rateArr[1] > 0 then
  140. list[conf.type] = (list[conf.type] or 0) + conf.rateArr[1]
  141. end
  142. end
  143. end
  144. end
  145. ctx.special_list = list
  146. return ctx.special_list
  147. end
  148. function MODULE.get_talent_data(character)
  149. local d = _M.assert_get(character)
  150. return d.list
  151. end
  152. return MODULE