embattle.lua 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. local schema = require "model.schema"
  2. local logger = require "logger"
  3. local asset = require "model.asset"
  4. local stringify = require "stringify"
  5. local common_fun = require "model.common_fun"
  6. local hero
  7. -- local skill_card
  8. local powerfunc
  9. local math_floor = math.floor
  10. local module_name = "embattle"
  11. local _M = schema.new(module_name, {
  12. adventure = {
  13. list = {
  14. -- [pos] = {
  15. -- sid = sid,
  16. -- pos = pos,
  17. -- }
  18. },
  19. card_list = {}
  20. }
  21. })
  22. local REQUEST = {}
  23. local CMD = {}
  24. local MODULE = {}
  25. local THIS = {}
  26. local function func_ret(fname, character, args)
  27. local f = THIS[fname]
  28. if not f then
  29. logger.error("func_ret not fname:%s !!!", fname)
  30. return {errno = STD_ERR.COMMON_SYS_ERR}
  31. end
  32. local errno, ret = f(character, args)
  33. if errno ~= 0 then
  34. return {errno = errno}
  35. end
  36. ret = ret or {}
  37. ret.errno = 0
  38. return ret
  39. end
  40. function MODULE.list_request_interests() return REQUEST end
  41. function MODULE.list_command_interests() return CMD end
  42. -- TODO: 解析/升级模块数据 在这里把数据初始化好
  43. function MODULE.parse(character)
  44. local d = _M.load(character)
  45. d.adventure = d.adventure or {}
  46. d.adventure.list = d.adventure.list or {}
  47. d.adventure.card_list = d.adventure.card_list or {}
  48. end
  49. -- TODO: 侦听事件
  50. function MODULE.monitor(character)
  51. end
  52. --
  53. function MODULE.launch(character)
  54. local d = _M.assert_get(character)
  55. hero = hero or require "model.hero"
  56. -- skill_card = skill_card or require "model.skill_card"
  57. end
  58. --
  59. function MODULE.ready(character)
  60. local d = _M.assert_get(character)
  61. logger.test("%s:ready, %s", module_name, stringify(d or {}))
  62. end
  63. -- TODO: 玩家下线时的处理
  64. function MODULE.saybye(character)
  65. end
  66. function MODULE.battle_power(character)
  67. local all = 0
  68. local d = _M.assert_get(character) or {}
  69. powerfunc = powerfunc or require "model.powerfunc"
  70. for k, v in pairs(d.adventure.list) do
  71. local hero_data = hero.hero_get(character, v.sid)
  72. local power = powerfunc.build_role(character, hero_data)
  73. if power and power > 0 then
  74. all = all + power
  75. end
  76. end
  77. -- for k, v in pairs(d.adventure.card_list) do
  78. -- local card_data = skill_card.skill_card_get(character, v.sid)
  79. -- local power = powerfunc.build_card(character, card_data)
  80. -- if power and power > 0 then
  81. -- all = all + power
  82. -- end
  83. -- end
  84. return all
  85. end
  86. function MODULE.first_battle(character)
  87. local d = _M.assert_get(character) or {}
  88. local hero_list = hero.hero_get_list(character)
  89. local Data_conf = asset.DataConfig_proto[4]
  90. for pos, v in ipairs(Data_conf.data5 or {}) do
  91. if v > 0 then
  92. for _, info in pairs(hero_list) do
  93. if info.id == v then
  94. d.adventure.list[pos] = {
  95. sid = info.sid,
  96. pos = pos
  97. }
  98. end
  99. end
  100. end
  101. end
  102. logger.trace("adventure"..stringify(d.adventure.list))
  103. _M.persist(character)
  104. end
  105. function MODULE.max_power_hero(character)
  106. local d = _M.assert_get(character) or {}
  107. powerfunc = powerfunc or require "model.powerfunc"
  108. local max_power = 0
  109. local hero_info
  110. for k, v in pairs(d.adventure.list) do
  111. local hero_data = hero.hero_get(character, v.sid)
  112. local power = powerfunc.build_role(character, hero_data)
  113. if power and power > max_power then
  114. max_power = power
  115. hero_info = hero_data
  116. end
  117. end
  118. return max_power, hero_info
  119. end
  120. -- 获得阵上的英雄
  121. function MODULE.get_embattle_hero_list(character)
  122. local d = _M.assert_get(character)
  123. local list = {}
  124. for _, v in pairs(d.adventure.list) do
  125. list[v.sid] = v.sid
  126. end
  127. return list
  128. end
  129. local function battle_sort(list, num)
  130. local ret = {}
  131. if num <= 0 then
  132. return ret
  133. end
  134. for i = 1, num do
  135. if list[i] then
  136. table.insert(ret, list[i])
  137. else
  138. table.insert(ret, {pos = 0, sid = ""})
  139. end
  140. end
  141. return ret
  142. end
  143. function THIS.embattle_battle(character, args)
  144. local d = _M.assert_get(character)
  145. local data_conf_list = assert(asset.DataConfig_proto, "DataConfig_proto")
  146. local pos = args.pos
  147. local sid = args.sid
  148. if sid == "" then
  149. sid = nil
  150. end
  151. if not pos then
  152. return STD_ERR.COMMON_PARM_ERR -- 参数异常
  153. end
  154. local data_conf
  155. local list
  156. local check_func
  157. if args.skill then
  158. data_conf = data_conf_list[4]
  159. list = d.adventure.card_list
  160. -- check_func = function()
  161. -- return skill_card.skill_card_check(character, sid)
  162. -- end
  163. else
  164. data_conf = data_conf_list[3]
  165. list = d.adventure.list
  166. check_func = function()
  167. return hero.hero_check(character, sid)
  168. end
  169. end
  170. local max_pos = data_conf.data1
  171. if pos > max_pos then
  172. return STD_ERR.COMMON_PARM_ERR -- 参数异常
  173. end
  174. if not sid then
  175. list[pos] = nil
  176. _M.persist(character)
  177. return 0, {
  178. list = battle_sort(d.adventure.list, data_conf_list[3].data1),
  179. card_list = battle_sort(d.adventure.card_list, data_conf_list[4].data1)
  180. }
  181. end
  182. -- 检查是否存在
  183. if not check_func or not check_func() then
  184. return STD_ERR.EMBATTLE_NOT_TARGET -- 未找到目标
  185. end
  186. local old_pos = -1
  187. for _, v in pairs(list) do
  188. if v.sid == sid then
  189. old_pos = v.pos
  190. break
  191. end
  192. end
  193. if old_pos == pos then
  194. return STD_ERR.COMMON_PARM_ERR -- 参数异常
  195. end
  196. -- 有旧位置,先处理旧位置逻辑
  197. if old_pos > 0 then
  198. --相当于换位置
  199. if list[pos] then
  200. list[old_pos] = list[pos]
  201. else
  202. list[old_pos] = nil
  203. end
  204. else
  205. if not args.skill then
  206. local temp = {}
  207. for _, v in pairs(d.adventure.list) do
  208. -- 不用检查相同位置上的同名卡
  209. if v.pos~= pos then
  210. local hero_info = hero.hero_get(character, v.sid)
  211. if hero_info then
  212. temp[common_fun.get_hero_type(hero_info.id)] = 1
  213. end
  214. end
  215. end
  216. local hero_info = hero.hero_get(character, sid)
  217. if temp[common_fun.get_hero_type(hero_info.id)] then
  218. return STD_ERR.EMBATTLE_SAME_NAME_HERO -- 不满足上阵需求
  219. end
  220. local hero_conf = asset.RoleConfig_proto[hero_info.id]
  221. if not hero_conf then
  222. return STD_ERR.COMMON_CONF_ERR
  223. end
  224. if hero_conf.profession ~= pos then
  225. return STD_ERR.EMBATTLE_SAME_NAME_HERO
  226. end
  227. end
  228. end
  229. list[pos] = {
  230. sid = sid,
  231. pos = pos,
  232. }
  233. _M.persist(character)
  234. return 0, {
  235. list = battle_sort(d.adventure.list, data_conf_list[3].data1),
  236. card_list = battle_sort(d.adventure.card_list, data_conf_list[4].data1)
  237. }
  238. end
  239. function THIS.embattle_get_data(character, args)
  240. local data_conf_list = assert(asset.DataConfig_proto, "DataConfig_proto")
  241. local d = _M.assert_get(character)
  242. return 0, {
  243. list = battle_sort(d.adventure.list, data_conf_list[3].data1),
  244. card_list = battle_sort(d.adventure.card_list, data_conf_list[4].data1)
  245. }
  246. end
  247. -- 上阵
  248. function REQUEST.embattle_battle(character, args)
  249. return func_ret("embattle_battle", character, args)
  250. end
  251. -- 上阵数据
  252. function REQUEST.embattle_get_data(character, args)
  253. return func_ret("embattle_get_data", character, args)
  254. end
  255. return MODULE