NewLogic.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. -- 记录融合英雄
  98. human.db.mergeInfo.heroInfo.heroID = sonHeroID
  99. human.db.mergeInfo.heroInfo.xLv = xLv
  100. human.db.mergeInfo.heroInfo.heroAttrs = attrs
  101. human.db.mergeInfo.startTime = os.time()
  102. human.db.mergeInfo.endTime = os.time() + getHatchTime(xLv)
  103. human.db.mergeInfo.heroInfo.fatherHeroBagIndex = fatherHeroBagIndex
  104. human.db.mergeInfo.heroInfo.motherHeroBagIndex = motherHeroBagIndex
  105. Log.write(Log.LOGID_TEST, "human.db.mergeInfo : " .. Json.Encode(human.db.mergeInfo))
  106. local cf = sonHeroID and HeroExcel.hero[sonHeroID]
  107. heroSimple.name = cf and cf.name or ""
  108. mergeInfo.mergeTime = human.db.mergeInfo.hatchTime
  109. mergeInfo.xLv = xLv
  110. mergeInfo.heroData = heroSimple
  111. Log.write(Log.LOGID_TEST, "mergeInfo: " .. Json.Encode(mergeInfo))
  112. Broadcast.sendErr(human, Lang.MERGE_SUCCESS)
  113. local msgRet = Msg.gc.GC_ROLE_CHANGE_BASEINFO
  114. msgRet.ret = MERGE_HERO_TYPE
  115. msgRet.tip = Json.Encode(mergeInfo)
  116. Msg.send(msgRet, human.fd)
  117. end
  118. return
  119. end
  120. -- 获取融合前的信息
  121. if type == QUERY_MERGE_BEFORE_INFO_TYPE then
  122. local tb = Util.split(param, "|")
  123. if tb[1] and tb[2] then
  124. -- 初始化
  125. initMergeInfo(human)
  126. human.db.heroBag = human.db.heroBag or {}
  127. local fatherHeroBagIndex = tonumber(tb[1]) or 0
  128. local motherHeroBagIndex = tonumber(tb[2]) or 0
  129. local fatherHeroGrid = human.db.heroBag[fatherHeroBagIndex]
  130. if not heroGrid then
  131. return
  132. end
  133. local motherHeroGrid = human.db.heroBag[motherHeroBagIndex]
  134. if not heroGrid then
  135. return
  136. end
  137. local mergeInfo = {}
  138. local heroSimple = {}
  139. local xLv = getxLv(fatherHeroGrid, motherHeroGrid)
  140. local cnt = math.floor(xLv / 2) + 1
  141. local isHatch = 2
  142. local mergeItem = {}
  143. local itemId = 178
  144. for k = 1, 1 do
  145. mergeItem[k] = mergeItem[k] or {}
  146. mergeItem[k].getway = mergeItem[k].getway or {}
  147. mergeItem[k].suipian = mergeItem[k].suipian or {}
  148. mergeItem[k].equip = mergeItem[k].equip or {}
  149. mergeItem[k].fuwen = mergeItem[k].fuwen or {}
  150. Grid.makeItem(mergeItem[k], itemId, cnt)
  151. end
  152. local sonHeroID = mergeHero(fatherHeroGrid.id, motherHeroGrid.id)
  153. if sonHeroID <= 0 then
  154. return
  155. end
  156. local cf = sonHeroID and HeroExcel.hero[sonHeroID]
  157. heroSimple.name = cf and cf.name or ""
  158. mergeInfo.mergeTime = 0
  159. mergeInfo.xLv = xLv
  160. mergeInfo.mergeItem = mergeItem
  161. mergeInfo.heroData = heroSimple
  162. mergeInfo.isHatch = isHatch
  163. Log.write(Log.LOGID_TEST, "mergeInfo: " .. Json.Encode(mergeInfo))
  164. local msgRet = Msg.gc.QUERY_MERGE_BEFORE_INFO_TYPE
  165. msgRet.ret = QUERY_MERGE_INFO_TYPE
  166. msgRet.tip = Json.Encode(mergeInfo)
  167. Msg.send(msgRet, human.fd)
  168. end
  169. return
  170. end
  171. -- 获取融合中的信息
  172. if type == QUERY_MERGE_INFO_TYPE then
  173. -- 初始化
  174. initMergeInfo(human)
  175. human.db.heroBag = human.db.heroBag or {}
  176. local mergeInfo = {}
  177. local mergeTime = 0
  178. local heroSimple = {}
  179. local xLv = human.db.mergeInfo.heroInfo.xLv or 0
  180. local heroIndex = 0
  181. local sonHeroID = human.db.mergeInfo.heroInfo.heroID or 0
  182. local isHatch = 2
  183. if sonHeroID == 0 then
  184. mergeInfo.mergeTime = mergeTime
  185. mergeInfo.xLv = xLv
  186. mergeInfo.heroData = heroSimple
  187. mergeInfo.isHatch = isHatch
  188. msgRet.ret = QUERY_MERGE_INFO_TYPE
  189. msgRet.tip = Json.Encode(mergeInfo)
  190. Msg.send(msgRet, human.fd)
  191. return
  192. end
  193. local mergeTime = human.db.mergeInfo.endTime - os.time()
  194. if mergeTime < 0 then
  195. -- 孵化成功
  196. mergeTime = 0
  197. isHatch = 3
  198. heroIndex = hatchHero(human)
  199. end
  200. if heroIndex ~= 0 then
  201. local heroGrid = human.db.heroBag[heroIndex]
  202. if type(heroGrid) ~= "table" then return end
  203. heroSimple.general = heroSimple.general or {}
  204. HeroGrid.makeHeroSimple(heroSimple, heroGrid, nil, human)
  205. else
  206. local cf = sonHeroID and HeroExcel.hero[sonHeroID]
  207. heroSimple.name = cf and cf.name or ""
  208. isHatch = 1
  209. end
  210. mergeInfo.mergeTime = mergeTime
  211. mergeInfo.xLv = xLv
  212. mergeInfo.heroData = heroSimple
  213. mergeInfo.isHatch = isHatch
  214. Log.write(Log.LOGID_TEST, "mergeInfo: " .. Json.Encode(mergeInfo))
  215. local msgRet = Msg.gc.GC_ROLE_CHANGE_BASEINFO
  216. msgRet.ret = QUERY_MERGE_INFO_TYPE
  217. msgRet.tip = Json.Encode(mergeInfo)
  218. Msg.send(msgRet, human.fd)
  219. return
  220. end
  221. -- 加速孵化
  222. if type == QUICK_HATCH_TYPE then
  223. local tb = Util.split(param, "|")
  224. local itemId = tonumber(tb[1]) or 0
  225. local cnt = tonumber(tb[2]) or 0
  226. local isHatch = 2
  227. if tb[1] and tb[2] then
  228. -- 初始化
  229. initMergeInfo(human)
  230. -- 剩余时间
  231. local nowTime = os.time()
  232. local hatchTime = human.db.mergeInfo.endTime - nowTime
  233. if hatchTime <= 0
  234. then
  235. return
  236. end
  237. local speedTime = 0 -- 加速时间
  238. if itemId == 179 then
  239. speedTime = (60 * 5 * cnt)
  240. local checkTime = (human.db.mergeInfo.endTime - nowTime) - speedTime
  241. if checkTime < 0 then
  242. Broadcast.sendErr(human, Lang.QUICK_HATCH_TIME_OUT)
  243. return
  244. end
  245. --使用道具
  246. ItemLogic.use(human, itemId, cnt)
  247. else
  248. -- 判断消耗
  249. if not ObjHuman.checkRMB(human, cnt) then
  250. return
  251. end
  252. speedTime = (30 * cnt)
  253. local checkTime = (human.db.mergeInfo.endTime - nowTime) - speedTime
  254. if checkTime < 0 then
  255. Broadcast.sendErr(human, Lang.QUICK_HATCH_TIME_OUT)
  256. return
  257. end
  258. -- 扣钻石
  259. ObjHuman.decZuanshi(human, -cnt, "hero_merge")
  260. end
  261. human.db.mergeInfo.endTime = human.db.mergeInfo.endTime - speedTime
  262. Log.write(Log.LOGID_TEST, "human.db.mergeInfo.endTime: " .. human.db.mergeInfo.endTime)
  263. local hatchTime = human.db.mergeInfo.endTime - nowTime
  264. -- 孵化成功
  265. if hatchTime < 0 then
  266. hatchTime = 0
  267. isHatch = 3
  268. end
  269. Log.write(Log.LOGID_TEST, "加速后 human.db.mergeInfo : " .. Json.Encode(human.db.mergeInfo))
  270. Broadcast.sendErr(human, Lang.QUICK_HATCH_SUCCESS)
  271. local mergeInfo = {}
  272. local heroSimple = {}
  273. local msgRet = Msg.gc.GC_ROLE_CHANGE_BASEINFO
  274. mergeInfo.mergeTime = hatchTime
  275. mergeInfo.heroData = heroSimple
  276. mergeInfo.xLv = human.db.mergeInfo.heroInfo.xLv
  277. mergeInfo.isHatch = isHatch
  278. msgRet.ret = QUICK_HATCH_TYPE
  279. msgRet.tip = Json.Encode(mergeInfo)
  280. Msg.send(msgRet, human.fd)
  281. return
  282. end
  283. return
  284. end
  285. end
  286. -- 孵化英雄
  287. function hatchHero(human)
  288. --添加英雄
  289. local heroIndex, fjlist = HeroLogic.addHero(human, human.db.mergeInfo.heroInfo.heroID, nil, 1,
  290. "hero_merge")
  291. human.db.mergeInfo.startTime = 0
  292. human.db.mergeInfo.endTime = 0
  293. human.db.mergeInfo.heroInfo = {}
  294. human.db.mergeInfo.heroInfo.heroID = 0
  295. human.db.mergeInfo.heroInfo.xLv = 0
  296. human.db.mergeInfo.heroInfo.fatherHeroBagIndex = 0
  297. human.db.mergeInfo.heroInfo.motherHeroBagIndex = 0
  298. human.db.mergeInfo.heroInfo.heroAttrs = RoleDefine.PANEL_ATTR_KEY
  299. human.db.heroBag = human.db.heroBag or {}
  300. return heroIndex
  301. -- local heroGrid = human.db.heroBag[heroIndex]
  302. -- if type(heroGrid) ~= "table" then return end
  303. -- heroSimple.general = heroSimple.general or {}
  304. -- HeroGrid.makeHeroSimple(heroSimple, heroGrid, nil, human)
  305. -- data.heroData = heroSimple
  306. -- data.mergeTime = human.db.mergeInfo.mergeTime
  307. -- msgRet.ret = HATCH_SCCUESS_TYPE
  308. -- msgRet.tip = Json.Encode(data)
  309. -- Msg.send(msgRet, human.fd)
  310. end
  311. -- 初始化融合信息
  312. function initMergeInfo(human)
  313. human.db.mergeInfo = human.db.mergeInfo or {}
  314. human.db.mergeInfo.mergeTime = human.db.mergeInfo.mergeTime or 0 --融合时间 时间戳
  315. human.db.mergeInfo.hatchTime = human.db.mergeInfo.hatchTime or 0 --孵化时间 单位s
  316. human.db.mergeInfo.heroInfo = human.db.mergeInfo.heroInfo or {}
  317. human.db.mergeInfo.heroInfo.heroID = human.db.mergeInfo.heroInfo.heroID or 0
  318. human.db.mergeInfo.heroInfo.xLv = human.db.mergeInfo.heroInfo.xLv or 0
  319. human.db.mergeInfo.heroInfo.fatherHeroBagIndex = human.db.mergeInfo.heroInfo.fatherHeroBagIndex or 0
  320. human.db.mergeInfo.heroInfo.motherHeroBagIndex = human.db.mergeInfo.heroInfo.motherHeroBagIndex or 0
  321. human.db.mergeInfo.heroInfo.heroAttrs = human.db.mergeInfo.heroInfo.heroAttrs or RoleDefine.PANEL_ATTR_KEY
  322. end
  323. -- 获取孵化时间 单位s
  324. function getHatchTime(human, xLv)
  325. local time = (xLv * 5) + (xLv - 1) * (xLv - 1) - 5
  326. if time < 0 then
  327. return 0
  328. end
  329. return time * 60
  330. end
  331. -- 获取xlv
  332. function getxLv(fatherHeroGrid, motherHeroGrid)
  333. local params1 = math.max(fatherHeroGrid.xLv, motherHeroGrid.xLv)
  334. local params2 = math.floor((fatherHeroGrid.lv + motherHeroGrid.lv) / 200) + 1
  335. return params1 + params2
  336. end
  337. -- 融合获取英雄ID
  338. function mergeHero(fatherHeroID, motherHeroID)
  339. local fatherHeroConfig = HeroExcel.hero[fatherHeroID]
  340. local motherHeroConfig = HeroExcel.hero[motherHeroID]
  341. if not fatherHeroConfig or not motherHeroConfig then
  342. return 0
  343. end
  344. --规则3 heroID + heroID
  345. for _, config in pairs(MergeRule[3].items) do
  346. if fatherHeroID == config[1] and motherHeroID == config[2] then
  347. return config[3]
  348. end
  349. end
  350. --规则2 种族 + heroID
  351. for _, config in pairs(MergeRule[2].items) do
  352. if fatherHeroConfig.camp == config[1] and motherHeroID == config[2] then
  353. return config[3]
  354. end
  355. end
  356. --规则1 种族 + 种族
  357. for _, config in pairs(MergeRule[1].items) do
  358. if fatherHeroConfig.camp == config[1] and motherHeroConfig.camp == config[2] then
  359. return config[3]
  360. end
  361. end
  362. return 0
  363. end