NewLogic.lua 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. local Msg = require("core.Msg")
  2. local Broadcast = require("broadcast.Broadcast")
  3. local Lang = require("common.Lang")
  4. local ItemDefine = require("bag.ItemDefine")
  5. local Grid = require("bag.Grid")
  6. local DrawCardLogic = require("drawCard.DrawCardLogic")
  7. local MergeRule = require("excel.mergeConfig").rule
  8. local BagLogic = require("bag.BagLogic")
  9. local Log = require("common.Log")
  10. local Util = require("common.Util")
  11. local ItemLogic = require("bag.ItemLogic")
  12. local HeroLogic = require("hero.HeroLogic")
  13. local HeroExcel = require("excel.hero")
  14. local RoleDefine = require("role.RoleDefine")
  15. local ObjHuman = require("core.ObjHuman")
  16. local HeroGrid = require("hero.HeroGrid")
  17. AD_DRAW_REWARD_TYPE = 10 --观看广告领取召唤券
  18. QUERY_MERGE_INFO_TYPE = 11 --获取融合信息
  19. QUERY_MERGE_BEFORE_INFO_TYPE = 15 --获取融合前信息
  20. HATCH_SCCUESS_TYPE = 12 -- 孵化成功
  21. MERGE_HERO_TYPE = 13 --融合
  22. QUICK_HATCH_TYPE = 14 --加速
  23. function NewProto(human, type, param)
  24. -- print("newProto:",type,param)
  25. -- local msgRet = Msg.gc.GC_ROLE_CHANGE_BASEINFO
  26. -- msgRet.ret = type
  27. -- msgRet.tip = "test data"
  28. -- Msg.send(msgRet,human.fd)
  29. -- 观看广告获取召唤券
  30. if type == AD_DRAW_REWARD_TYPE then
  31. human.db.adRewardCnt = human.db.adRewardCnt or 0
  32. if human.db.adRewardCnt > 4 then
  33. Broadcast.sendErr(human, Lang.AD_DRAW_REWARD_LIMIT_ERROR)
  34. return
  35. end
  36. --增加今日观看次数
  37. human.db.adRewardCnt = (human.db.adRewardCnt or 0) + 1
  38. -- 添加高级召唤卷
  39. BagLogic.addItem(human, 118, 1, "draw_ad_reward")
  40. local msgRet = Msg.gc.GC_ROLE_CHANGE_BASEINFO
  41. msgRet.ret = AD_DRAW_REWARD_TYPE
  42. msgRet.tip = Lang.AD_DRAW_REWARD_SUCCESS
  43. Msg.send(msgRet, human.fd)
  44. Broadcast.sendErr(human, Lang.AD_DRAW_REWARD_SUCCESS)
  45. end
  46. -- 开始融合
  47. if type == MERGE_HERO_TYPE then
  48. local tb = Util.split(param, "|")
  49. if tb[1] and tb[2] then
  50. -- 初始化
  51. initMergeInfo(human)
  52. human.db.heroBag = human.db.heroBag or {}
  53. local itemId = 178 --生命雨露
  54. local fatherHeroBagIndex = tonumber(tb[1]) or 0
  55. local motherHeroBagIndex = tonumber(tb[2]) or 0
  56. Log.write(Log.LOGID_TEST,
  57. "开始融合 fatherHeroBagIndex: " .. fatherHeroBagIndex .. "motherHeroBagIndex: " .. motherHeroBagIndex)
  58. local fatherHeroGrid = human.db.heroBag[fatherHeroBagIndex]
  59. if not heroGrid then
  60. return
  61. end
  62. local motherHeroGrid = human.db.heroBag[motherHeroBagIndex]
  63. if not heroGrid then
  64. return
  65. end
  66. Log.write(Log.LOGID_TEST, "motherHeroGrid: " .. Json.Encode(motherHeroGrid))
  67. Log.write(Log.LOGID_TEST, "fatherHeroGrid: " .. Json.Encode(fatherHeroGrid))
  68. local fatherHeroAttr = ObjHuman.getHeroAttrs(human, fatherHeroBagIndex)
  69. local motherHeroAttr = ObjHuman.getHeroAttrs(human, motherHeroBagIndex)
  70. Log.write(Log.LOGID_TEST, "fatherHeroAttr: " .. Json.Encode(fatherHeroAttr))
  71. Log.write(Log.LOGID_TEST, "motherHeroAttr: " .. Json.Encode(motherHeroAttr))
  72. local mergeInfo = {}
  73. local attrs = RoleDefine.PANEL_ATTR_KEY
  74. local sonHeroID = mergeHero(fatherHeroGrid.id, motherHeroGrid.id)
  75. if sonHeroID <= 0 then
  76. return
  77. end
  78. Log.write(Log.LOGID_TEST, "sonHeroID: " .. sonHeroID)
  79. for key, value in pairs(attrs) do
  80. local param1 = fatherHeroAttr[key] or 0
  81. local param2 = motherHeroAttr[key] or 0
  82. attrs[key] = math.floor(((param1 + param2) / 4))
  83. end
  84. Log.write(Log.LOGID_TEST, "融合后的属性: " .. Json.Encode(attrs))
  85. local xLv = getxLv(fatherHeroGrid, motherHeroGrid)
  86. local cnt = math.floor(xLv / 2) + 1
  87. local heroSimple = {}
  88. Log.write(Log.LOGID_TEST, "xLv: " .. xLv)
  89. Log.write(Log.LOGID_TEST, "cnt: " .. cnt)
  90. --使用道具
  91. ItemLogic.use(human, itemId, cnt)
  92. Log.write(Log.LOGID_TEST, "删除开始")
  93. -- 删除英雄
  94. HeroLogic.delHeroByIndex(human, fatherHeroBagIndex, "hero_merge")
  95. HeroLogic.delHeroByIndex(human, motherHeroBagIndex, "hero_merge")
  96. Log.write(Log.LOGID_TEST, "删除结束")
  97. -- for heroIndex = 1, human.db.heroBag[0] do
  98. -- local heroGrid = human.db.heroBag[heroIndex]
  99. -- if heroGrid.id == fatherHeroID or heroGrid.id == motherHeroID then
  100. -- end
  101. -- end
  102. -- 记录融合英雄
  103. human.db.mergeInfo.heroInfo.heroID = sonHeroID
  104. human.db.mergeInfo.heroInfo.xLv = xLv
  105. human.db.mergeInfo.heroInfo.heroAttrs = attrs
  106. human.db.mergeInfo.mergeStartTime = os.time()
  107. human.db.mergeInfo.mergeEndTime = os.time() + getHatchTime(xLv)
  108. human.db.mergeInfo.mergeTime = getHatchTime(xLv)
  109. human.db.mergeInfo.heroInfo.fatherHeroBagIndex = fatherHeroBagIndex
  110. human.db.mergeInfo.heroInfo.motherHeroBagIndex = motherHeroBagIndex
  111. Log.write(Log.LOGID_TEST, "human.db.mergeInfo : " .. Json.Encode(human.db.mergeInfo))
  112. local cf = sonHeroID and HeroExcel.hero[sonHeroID]
  113. heroSimple.name = cf and cf.name or ""
  114. mergeInfo.mergeTime = human.db.mergeInfo.mergeTime
  115. mergeInfo.xLv = xLv
  116. mergeInfo.heroData = heroSimple
  117. Log.write(Log.LOGID_TEST, "mergeInfo: " .. Json.Encode(mergeInfo))
  118. Broadcast.sendErr(human, Lang.MERGE_SUCCESS)
  119. local msgRet = Msg.gc.GC_ROLE_CHANGE_BASEINFO
  120. msgRet.ret = MERGE_HERO_TYPE
  121. msgRet.tip = Json.Encode(mergeInfo)
  122. Msg.send(msgRet, human.fd)
  123. end
  124. return
  125. end
  126. -- 获取融合前的信息
  127. if type == QUERY_MERGE_BEFORE_INFO_TYPE then
  128. local tb = Util.split(param, "|")
  129. if tb[1] and tb[2] then
  130. -- 初始化
  131. initMergeInfo(human)
  132. human.db.heroBag = human.db.heroBag or {}
  133. local fatherHeroBagIndex = tonumber(tb[1]) or 0
  134. local motherHeroBagIndex = tonumber(tb[2]) or 0
  135. local fatherHeroGrid = human.db.heroBag[fatherHeroBagIndex]
  136. if not heroGrid then
  137. return
  138. end
  139. local motherHeroGrid = human.db.heroBag[motherHeroBagIndex]
  140. if not heroGrid then
  141. return
  142. end
  143. local mergeInfo = {}
  144. local heroSimple = {}
  145. local xLv = getxLv(fatherHeroGrid, motherHeroGrid)
  146. local cnt = math.floor(xLv / 2) + 1
  147. local sonHeroID = mergeHero(fatherHeroGrid.id, motherHeroGrid.id)
  148. if sonHeroID <= 0 then
  149. return
  150. end
  151. local cf = sonHeroID and HeroExcel.hero[sonHeroID]
  152. heroSimple.name = cf and cf.name or ""
  153. mergeInfo.mergeTime = human.db.mergeInfo.mergeTime
  154. mergeInfo.xLv = xLv
  155. mergeInfo.heroData = heroSimple
  156. Log.write(Log.LOGID_TEST, "mergeInfo: " .. Json.Encode(mergeInfo))
  157. local msgRet = Msg.gc.QUERY_MERGE_BEFORE_INFO_TYPE
  158. msgRet.ret = QUERY_MERGE_INFO_TYPE
  159. msgRet.tip = Json.Encode(mergeInfo)
  160. Msg.send(msgRet, human.fd)
  161. end
  162. return
  163. end
  164. -- 获取融合中的信息
  165. if type == QUERY_MERGE_INFO_TYPE then
  166. -- 初始化
  167. initMergeInfo(human)
  168. human.db.heroBag = human.db.heroBag or {}
  169. local mergeInfo = {}
  170. local nowTime = os.time()
  171. local mergeTime = 0
  172. local heroSimple = {}
  173. local xLv = human.db.mergeInfo.heroInfo.xLv
  174. local sonHeroID = human.db.mergeInfo.heroInfo.heroID
  175. if sonHeroID <= 0 then
  176. return
  177. end
  178. if human.db.mergeInfo.mergeEndTime ~= 0 then
  179. mergeTime = human.db.mergeInfo.mergeEndTime - nowTime
  180. -- 孵化成功
  181. if mergeTime <= 0 then
  182. hatchHero(human)
  183. return
  184. end
  185. end
  186. local cf = sonHeroID and HeroExcel.hero[sonHeroID]
  187. heroSimple.name = cf and cf.name or ""
  188. mergeInfo.mergeTime = human.db.mergeInfo.mergeTime
  189. mergeInfo.xLv = xLv
  190. mergeInfo.heroData = heroSimple
  191. Log.write(Log.LOGID_TEST, "mergeInfo: " .. Json.Encode(mergeInfo))
  192. local msgRet = Msg.gc.GC_ROLE_CHANGE_BASEINFO
  193. msgRet.ret = QUERY_MERGE_INFO_TYPE
  194. msgRet.tip = Json.Encode(mergeInfo)
  195. Msg.send(msgRet, human.fd)
  196. return
  197. end
  198. -- 加速孵化
  199. if type == QUICK_HATCH_TYPE then
  200. local tb = Util.split(param, "|")
  201. local itemId = tonumber(tb[1]) or 0
  202. local cnt = tonumber(tb[2]) or 0
  203. if tb[1] and tb[2] then
  204. -- 初始化
  205. initMergeInfo(human)
  206. --使用道具
  207. ItemLogic.use(human, itemId, cnt)
  208. local remainingTime = human.db.mergeInfo.mergeTime - (60 * cnt)
  209. Log.write(Log.LOGID_TEST, "remainingTime: " .. remainingTime)
  210. -- 孵化成功
  211. if remainingTime <= 0 then
  212. hatchHero(human)
  213. -- return
  214. end
  215. Log.write(Log.LOGID_TEST, "加速后 human.db.mergeInfo : " .. Json.Encode(human.db.mergeInfo))
  216. Broadcast.sendErr(human, Lang.QUICK_HATCH_SUCCESS)
  217. local mergeInfo = {}
  218. local heroSimple = {}
  219. -- heroSimple.general = heroSimple.general or {}
  220. -- HeroGrid.makeHeroSimpleByID(heroSimple, human.db.mergeInfo.heroId, nil, nil, human)
  221. mergeInfo.mergeTime = human.db.mergeInfo.mergeTime
  222. mergeInfo.heroData = heroSimple
  223. local msgRet = Msg.gc.GC_ROLE_CHANGE_BASEINFO
  224. msgRet.ret = QUICK_HATCH_TYPE
  225. msgRet.tip = Json.Encode(mergeInfo)
  226. Msg.send(msgRet, human.fd)
  227. return
  228. end
  229. return
  230. end
  231. end
  232. -- 孵化英雄
  233. function hatchHero(human)
  234. --添加英雄
  235. local heroIndex, fjlist = HeroLogic.addHero(human, human.db.mergeInfo.heroInfo.heroID, nil, 1,
  236. "hero_merge")
  237. local data = {}
  238. local heroSimple = {}
  239. local msgRet = Msg.gc.GC_ROLE_CHANGE_BASEINFO
  240. human.db.mergeInfo.mergeStartTime = 0
  241. human.db.mergeInfo.mergeEndTime = 0
  242. human.db.mergeInfo.mergeTime = 0
  243. human.db.mergeInfo.heroInfo = {}
  244. human.db.mergeInfo.heroInfo.heroID = 0
  245. human.db.mergeInfo.heroInfo.xLv = 0
  246. human.db.mergeInfo.heroInfo.fatherHeroBagIndex = 0
  247. human.db.mergeInfo.heroInfo.motherHeroBagIndex = 0
  248. human.db.mergeInfo.heroInfo.heroAttrs = RoleDefine.PANEL_ATTR_KEY
  249. human.db.heroBag = human.db.heroBag or {}
  250. local heroGrid = human.db.heroBag[heroIndex]
  251. if type(heroGrid) ~= "table" then return end
  252. heroSimple.general = heroSimple.general or {}
  253. HeroGrid.makeHeroSimple(heroSimple, heroGrid, nil, human)
  254. data.heroData = heroSimple
  255. data.mergeTime = human.db.mergeInfo.mergeTime
  256. msgRet.ret = HATCH_SCCUESS_TYPE
  257. msgRet.tip = Json.Encode(data)
  258. Msg.send(msgRet, human.fd)
  259. end
  260. -- 初始化融合信息
  261. function initMergeInfo(human)
  262. human.db.mergeInfo = human.db.mergeInfo or {}
  263. human.db.mergeInfo.mergeStartTime = human.db.mergeInfo.mergeStartTime or 0
  264. human.db.mergeInfo.mergeEndTime = human.db.mergeInfo.mergeEndTime or 0
  265. human.db.mergeInfo.mergeTime = human.db.mergeInfo.mergeTime or 0
  266. human.db.mergeInfo.heroInfo = human.db.mergeInfo.heroInfo or {}
  267. human.db.mergeInfo.heroInfo.heroID = human.db.mergeInfo.heroInfo.heroID or 0
  268. human.db.mergeInfo.heroInfo.xLv = human.db.mergeInfo.heroInfo.xLv or 0
  269. human.db.mergeInfo.heroInfo.fatherHeroBagIndex = human.db.mergeInfo.heroInfo.fatherHeroBagIndex or 0
  270. human.db.mergeInfo.heroInfo.motherHeroBagIndex = human.db.mergeInfo.heroInfo.motherHeroBagIndex or 0
  271. human.db.mergeInfo.heroInfo.heroAttrs = human.db.mergeInfo.heroInfo.heroAttrs or RoleDefine.PANEL_ATTR_KEY
  272. end
  273. -- 获取孵化时间 单位s
  274. function getHatchTime(human, xLv)
  275. local time = (xLv * 5) + (xLv - 1) * (xLv - 1) - 5
  276. if time < 0 then
  277. return 0
  278. end
  279. return time * 60
  280. end
  281. -- 获取xlv
  282. function getxLv(fatherHeroGrid, motherHeroGrid)
  283. local params1 = math.max(fatherHeroGrid.xLv, motherHeroGrid.xLv)
  284. local params2 = math.floor((fatherHeroGrid.lv + motherHeroGrid.lv) / 200) + 1
  285. return params1 + params2
  286. end
  287. -- 融合获取英雄ID
  288. function mergeHero(fatherHeroID, motherHeroID)
  289. local fatherHeroConfig = HeroExcel.hero[fatherHeroID]
  290. local motherHeroConfig = HeroExcel.hero[motherHeroID]
  291. if not fatherHeroConfig or not motherHeroConfig then
  292. return 0
  293. end
  294. --规则3 heroID + heroID
  295. for _, config in pairs(MergeRule[3].items) do
  296. if fatherHeroID == config[1] and motherHeroID == config[2] then
  297. return config[3]
  298. end
  299. end
  300. --规则2 种族 + heroID
  301. for _, config in pairs(MergeRule[2].items) do
  302. if fatherHeroConfig.camp == config[1] and motherHeroID == config[2] then
  303. return config[3]
  304. end
  305. end
  306. --规则1 种族 + 种族
  307. for _, config in pairs(MergeRule[1].items) do
  308. if fatherHeroConfig.camp == config[1] and motherHeroConfig.camp == config[2] then
  309. return config[3]
  310. end
  311. end
  312. return 0
  313. end