hero.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. local schema = require "model.schema"
  2. local logger = require "logger"
  3. local common_fun = require "model.common_fun"
  4. local module_fun = require "model.module_fun"
  5. local asset = require "model.asset"
  6. local payment = require "model.payment"
  7. local reward = require "model.reward"
  8. local stringify = require "stringify"
  9. -- local keygen = require "model.keygen"
  10. local embattle
  11. local equip
  12. -- local altar
  13. local logger_trace = logger.trace
  14. local table_insert = table.insert
  15. local math_min = math.min
  16. local MAX_NUM = 999999999999 -- 英雄最大堆叠数量
  17. local MAX_UPGRADE = 1000 -- 最大升级数量
  18. local module_name = "hero"
  19. local _M = schema.new(module_name, {
  20. list = {
  21. --[[
  22. [sid] = {
  23. sid = 类型id,
  24. id = 模板id,
  25. lv = 1,
  26. equip = {穿戴的装备}
  27. }
  28. ]]
  29. },
  30. })
  31. local REQUEST = {}
  32. local CMD = {}
  33. local MODULE = {}
  34. local THIS = {}
  35. local tm_list = {}
  36. tm_list.hero_onekey_upgrade_star = 0
  37. local function func_ret(fname, character, args)
  38. local f = THIS[fname]
  39. if not f then
  40. logger.error("func_ret not fname:%s !!!", fname)
  41. return {errno = STD_ERR.COMMON_SYS_ERR}
  42. end
  43. if tm_list[fname] then
  44. local now = os.time()
  45. if now - tm_list[fname] == 0 then
  46. return {errno = STD_ERR.COMMON_CLICK_TOO_FAST} -- 点击太快了
  47. else
  48. tm_list[fname] = now
  49. end
  50. end
  51. local errno, ret = f(character, args)
  52. if errno ~= 0 then
  53. return {errno = errno}
  54. end
  55. ret = ret or {}
  56. ret.errno = 0
  57. return ret
  58. end
  59. function MODULE.list_request_interests() return REQUEST end
  60. function MODULE.list_command_interests() return CMD end
  61. -- TODO: 解析/升级模块数据 在这里把数据初始化好
  62. function MODULE.parse(character)
  63. local d = _M.load(character)
  64. d.list = d.list or {}
  65. end
  66. -- TODO: 侦听事件
  67. function MODULE.monitor(character)
  68. end
  69. -- TODO: 类似泰利的 prepare 接口
  70. function MODULE.launch(character)
  71. local d = _M.assert_get(character)
  72. local u = _M.assert_runtime(character)
  73. embattle = embattle or require "model.embattle"
  74. equip = equip or require "model.equip"
  75. -- altar = altar or require "model.altar"
  76. end
  77. -- TODO: 与客户端同步数据
  78. function MODULE.ready(character)
  79. local d = _M.assert_get(character)
  80. end
  81. -- TODO: 玩家下线时的处理
  82. function MODULE.saybye(character)
  83. end
  84. function MODULE.add_hero(character, id, num, collect, dispose, addition)
  85. local d = _M.assert_get(character) or {}
  86. local role_conf_list = assert(asset.RoleConfig_proto, "RoleConfig_proto")
  87. local conf = role_conf_list[id]
  88. if not conf and dispose then
  89. dispose(nil, id, num)
  90. return
  91. end
  92. if num >= 1 and num <= MAX_NUM then
  93. local add_num = num
  94. local hero_type = tostring(common_fun.get_hero_type(id))
  95. if not d.list[hero_type] then
  96. d.list[hero_type] = {
  97. sid = hero_type,
  98. id = id,
  99. lv = 1,
  100. num = 0,
  101. }
  102. add_num = num -1;
  103. end
  104. d.list[hero_type].num = d.list[hero_type].num + add_num
  105. character.dispatch("hero_add_num", id, num)
  106. collect(GOODS_HERO, d.list[hero_type])
  107. addition(GOODS_HERO, id, add_num)
  108. _M.persist(character)
  109. return true, add_num
  110. else
  111. if dispose then
  112. dispose(nil, id, num)
  113. end
  114. end
  115. end
  116. function MODULE.hero_check(character, sid)
  117. local d = _M.assert_get(character)
  118. return d.list[sid] and true or false
  119. end
  120. function MODULE.hero_get(character, sid)
  121. local d = _M.assert_get(character)
  122. return d.list[sid]
  123. end
  124. function MODULE.hero_get_list(character)
  125. local d = _M.assert_get(character)
  126. return d.list
  127. end
  128. function MODULE.hero_save(character)
  129. _M.persist(character)
  130. end
  131. function THIS.hero_get_data(character, args)
  132. local d = _M.assert_get(character)
  133. local ret = {}
  134. -- local altar_info = altar.get_altar_info(character)
  135. -- local altar_battle = altar.get_altar_battle_info(character)
  136. for sid, data in pairs(d.list) do
  137. -- if altar_battle[sid] then
  138. -- local temp = altar.altar_change_hero_info(altar_info, data)
  139. -- table.insert(ret, temp)
  140. -- else
  141. -- table.insert(ret, data)
  142. -- end
  143. table.insert(ret, data)
  144. end
  145. return 0, {list = ret}
  146. end
  147. local function get_max_upgrade(character, cur_lv, max_lv)
  148. local max_lv = max_lv or cur_lv + MAX_UPGRADE
  149. local level_conf_list = assert(asset.RoleLevelConfig_proto, "RoleLevelConfig_proto")
  150. local num = 0
  151. local goods_list = {}
  152. local flag = true
  153. for lv = cur_lv+1, max_lv do
  154. local level_conf = level_conf_list[lv]
  155. if not level_conf then
  156. break
  157. end
  158. for i = 1, #level_conf.goods do
  159. local item_id = level_conf.goods[i]
  160. local item_num = level_conf.goodNum[i]
  161. if not item_id or not item_num or item_id <=0 or item_num <= 0 then
  162. break
  163. end
  164. if not goods_list[item_id] then
  165. goods_list[item_id] = module_fun.get_goods_num(character, item_id)
  166. end
  167. if goods_list[item_id] < item_num then
  168. flag = false
  169. break
  170. end
  171. goods_list[item_id] = goods_list[item_id] - item_num
  172. end
  173. if not flag then
  174. break
  175. end
  176. num = lv - cur_lv
  177. end
  178. return num
  179. end
  180. local function reset_level(lv, list)
  181. local level_conf_list = assert(asset.RoleLevelConfig_proto, "RoleLevelConfig_proto")
  182. for lv = 2, lv do
  183. local level_conf = level_conf_list[lv]
  184. if not level_conf then
  185. return STD_ERR.COMMON_SYS_ERR -- 系统异常
  186. end
  187. for i = 1, #level_conf.goods do
  188. local item_id = level_conf.goods[i]
  189. local item_num = level_conf.goodNum[i]
  190. if item_id and item_num and item_num > 0 then
  191. table_insert(list, {id = item_id, num = item_num})
  192. else
  193. break
  194. end
  195. end
  196. end
  197. return 0
  198. end
  199. function THIS.hero_upgrade(character, args)
  200. local d = _M.assert_get(character)
  201. local sid = args.sid
  202. local num = args.num or 0
  203. if not sid then
  204. return STD_ERR.COMMON_PARM_ERR -- 参数异常
  205. end
  206. if num < 0 or num > MAX_UPGRADE then
  207. return STD_ERR.COMMON_PARM_ERR -- 参数异常
  208. end
  209. -- local altar_battle = altar.get_altar_battle_info(character)
  210. -- if altar_battle[sid] then
  211. -- return STD_ERR.COMMON_PARM_ERR -- 参数异常
  212. -- end
  213. local data = d.list[sid]
  214. local level = data.lv
  215. if not data then
  216. return STD_ERR.COMMON_PARM_ERR -- 参数异常
  217. end
  218. local hero_conf_list = assert(asset.RoleConfig_proto, "RoleConfig_proto")
  219. local hero_conf = hero_conf_list[data.id]
  220. if not hero_conf then
  221. logger_trace("RoleConfig_proto:%d", data.id)
  222. return STD_ERR.COMMON_CONF_ERR -- 配置异常
  223. end
  224. if num == 0 then
  225. num = get_max_upgrade(character, level)
  226. if num <= 0 then
  227. return STD_ERR.HERO_MAX_LEVEL -- 超过最大等级
  228. end
  229. end
  230. local level_conf_list = assert(asset.RoleLevelConfig_proto, "RoleLevelConfig_proto")
  231. local pay_list = {}
  232. for i = 1, num do
  233. local conf = level_conf_list[level+i]
  234. if not conf then
  235. return STD_ERR.COMMON_CONF_ERR-- 配置异常
  236. end
  237. for j = 1, #conf.goods do
  238. local item_id = conf.goods[j]
  239. local item_num = conf.goodNum[j]
  240. if item_id and item_num then
  241. table_insert(pay_list, {id = item_id, num = item_num})
  242. end
  243. end
  244. end
  245. local receipt = {
  246. module="hero",
  247. brief="角色升级",
  248. context=string.format("角色id%d, 从%d升级到%d", data.id, level, level+num),
  249. notify={
  250. flags="hero_upgrade"
  251. },
  252. detail=pay_list
  253. }
  254. local errno = payment(character, receipt)
  255. if errno ~=0 then
  256. return errno
  257. end
  258. local cur_lv = data.lv+num
  259. local pre_lv = data.lv
  260. data.lv = data.lv+num
  261. _M.persist(character)
  262. character.dispatch("hero_upgrade", data.id, pre_lv, cur_lv)
  263. character.dispatch("hero_upgrade_info", data)
  264. return 0, {data = data}
  265. end
  266. function THIS.hero_upgrade_star(character, args)
  267. local d = _M.assert_get(character) or {}
  268. local sid = args.sid
  269. if not sid then
  270. return STD_ERR.COMMON_PARM_ERR -- 参数异常
  271. end
  272. local data = d.list[sid]
  273. if not data then
  274. return STD_ERR.COMMON_PARM_ERR -- 参数异常
  275. end
  276. local role_conf_list = assert(asset.RoleConfig_proto, "RoleConfig_proto")
  277. local role_conf = role_conf_list[data.id]
  278. if not role_conf then
  279. logger_trace("RoleConfig_proto:%d", data.id)
  280. return STD_ERR.COMMON_CONF_ERR -- 配置异常
  281. end
  282. local cur_quality = role_conf.quality
  283. if not cur_quality then
  284. return STD_ERR.COMMON_CONF_ERR -- 配置异常
  285. end
  286. local max_quality = asset.DataConfig_proto[5].data1
  287. if cur_quality >= max_quality then
  288. return STD_ERR.HERO_START_MAX -- 达到最大突破等级
  289. end
  290. local conf_id = data.id + 1
  291. role_conf = role_conf_list[conf_id]
  292. if not role_conf or role_conf.quality ~= cur_quality+1 then
  293. return STD_ERR.COMMON_CONF_ERR -- 配置异常
  294. end
  295. if data.num < role_conf.consume then
  296. return STD_ERR.HERO_SAME_PAY_ERR
  297. end
  298. data.id = conf_id
  299. data.num = data.num - role_conf.consume
  300. _M.persist(character)
  301. return 0, {data = d.list[sid]}
  302. end
  303. function THIS.hero_onekey_upgrade_star(character, args)
  304. local d = _M.assert_get(character)
  305. return 0
  306. end
  307. --
  308. function THIS.hero_reset(character, args)
  309. return 0
  310. end
  311. function THIS.hero_wear_equip(character, args)
  312. local sid = args.sid
  313. local list = args.list or {}
  314. local d = _M.assert_get(character)
  315. if not sid or not d.list[sid] then
  316. return STD_ERR.COMMON_PARM_ERR -- 参数异常
  317. end
  318. local data = d.list[sid]
  319. -- -- 清除冗余字段
  320. -- for i = #list , 1, -1 do
  321. -- if list[i] == "" then
  322. -- list[i] = nil
  323. -- else
  324. -- break
  325. -- end
  326. -- end
  327. if #list ~= 6 and #list ~= 0 then
  328. return STD_ERR.COMMON_PARM_ERR -- 参数异常
  329. end
  330. local errno = equip.equip_wear(character, data, data.equip, list)
  331. if errno ~= 0 then
  332. return errno
  333. end
  334. local empty = true
  335. -- 清除冗余字段
  336. for i = #list , 1, -1 do
  337. if list[i] ~= "" then
  338. empty = false
  339. break
  340. end
  341. end
  342. if empty then
  343. list = nil
  344. end
  345. data.equip = list
  346. _M.persist(character)
  347. return 0, {list = args.list, data = data}
  348. end
  349. -- 英雄背包数据
  350. function REQUEST.hero_get_data(character, args)
  351. return func_ret("hero_get_data", character, args)
  352. end
  353. -- 英雄升级
  354. function REQUEST.hero_upgrade(character, args)
  355. return func_ret("hero_upgrade", character, args)
  356. end
  357. -- 英雄升星
  358. function REQUEST.hero_upgrade_star(character, args)
  359. return func_ret("hero_upgrade_star", character, args)
  360. end
  361. -- 一键升星
  362. function REQUEST.hero_onekey_upgrade_star(character, args)
  363. return func_ret("hero_onekey_upgrade_star", character, args)
  364. end
  365. -- 重置
  366. function REQUEST.hero_reset(character, args)
  367. return func_ret("hero_reset", character, args)
  368. end
  369. -- 装备穿戴
  370. function REQUEST.hero_wear_equip(character, args)
  371. return func_ret("hero_wear_equip", character, args)
  372. end
  373. -- 英雄的特殊属性
  374. function MODULE.get_special_attr(character)
  375. local d = _M.assert_get(character)
  376. local battle_list = embattle.get_embattle_hero_list(character)
  377. local list = {}
  378. for sid in pairs(battle_list or {}) do
  379. local data = d.list[sid]
  380. if not data then
  381. goto CONTINUE
  382. end
  383. for _, esid in ipairs(data.equip or {}) do
  384. if not esid or esid == "" then
  385. goto CONTINUE2
  386. end
  387. local equip_info = equip.get_equip_data(character, esid)
  388. if not equip_info then
  389. goto CONTINUE2
  390. end
  391. local equip_conf = asset.ArmorConfig_proto[equip_info.id]
  392. if equip_conf and equip_conf.entry and equip_conf.entry > 0 then
  393. local conf = asset.EntryConfig_proto[equip_conf.entry]
  394. if conf and conf.type and conf.type >= 100 then
  395. if conf.rateArr[1] and conf.rateArr[1] > 0 then
  396. list[conf.type] = (list[conf.type] or 0) + conf.rateArr[1]
  397. end
  398. end
  399. end
  400. ::CONTINUE2::
  401. end
  402. ::CONTINUE::
  403. end
  404. logger.test("equip get_special_attr"..stringify(list or {}))
  405. return list
  406. end
  407. return MODULE