NewLogic.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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 itemSpeedTime = 0
  238. if itemId == 179 then
  239. local speedTime = (60 * 5 * cnt) -- 正常数量加速券的加速时间
  240. local lastSpeedTime = (60 * 5 * (cnt - 1)) --少一张加速券的加速时间
  241. local itemCnt = 0
  242. --使用道具
  243. if hatchTime > lastSpeedTime and hatchTime <= speedTime then
  244. itemCnt = cnt
  245. elseif hatchTime > 0 and hatchTime <= lastSpeedTime then
  246. itemCnt = cnt - 1
  247. elseif hatchTime >= speedTime then
  248. itemCnt = cnt
  249. else
  250. itemCnt = 0
  251. end
  252. itemSpeedTime = (60 * 5 * itemCnt)
  253. --使用道具
  254. ItemLogic.use(human, itemId, itemCnt)
  255. else
  256. local zuanshiCnt = doCalcNeedZuanshi(hatchTime)
  257. -- 判断消耗
  258. if not ObjHuman.checkRMB(human, zuanshiCnt) then
  259. return
  260. end
  261. itemSpeedTime = (30 * zuanshiCnt)
  262. -- 扣钻石
  263. ObjHuman.decZuanshi(human, -zuanshiCnt, "hero_merge")
  264. end
  265. human.db.mergeInfo.endTime = human.db.mergeInfo.endTime - itemSpeedTime
  266. Log.write(Log.LOGID_TEST, "human.db.mergeInfo.endTime: " .. human.db.mergeInfo.endTime)
  267. local hatchTime = human.db.mergeInfo.endTime - nowTime
  268. -- 孵化成功
  269. if hatchTime < 0 then
  270. hatchTime = 0
  271. isHatch = 3
  272. end
  273. Log.write(Log.LOGID_TEST, "加速后 human.db.mergeInfo : " .. Json.Encode(human.db.mergeInfo))
  274. Broadcast.sendErr(human, Lang.QUICK_HATCH_SUCCESS)
  275. local mergeInfo = {}
  276. local heroSimple = {}
  277. local msgRet = Msg.gc.GC_ROLE_CHANGE_BASEINFO
  278. mergeInfo.mergeTime = hatchTime
  279. mergeInfo.heroData = heroSimple
  280. mergeInfo.xLv = human.db.mergeInfo.heroInfo.xLv
  281. mergeInfo.isHatch = isHatch
  282. msgRet.ret = QUICK_HATCH_TYPE
  283. msgRet.tip = Json.Encode(mergeInfo)
  284. Msg.send(msgRet, human.fd)
  285. return
  286. end
  287. return
  288. end
  289. end
  290. -- 孵化英雄
  291. function hatchHero(human)
  292. --添加英雄
  293. local heroIndex, fjlist = HeroLogic.addHero(human, human.db.mergeInfo.heroInfo.heroID, nil, 1,
  294. "hero_merge")
  295. human.db.mergeInfo.startTime = 0
  296. human.db.mergeInfo.endTime = 0
  297. human.db.mergeInfo.heroInfo = {}
  298. human.db.mergeInfo.heroInfo.heroID = 0
  299. human.db.mergeInfo.heroInfo.xLv = 0
  300. human.db.mergeInfo.heroInfo.fatherHeroBagIndex = 0
  301. human.db.mergeInfo.heroInfo.motherHeroBagIndex = 0
  302. human.db.mergeInfo.heroInfo.heroAttrs = RoleDefine.PANEL_ATTR_KEY
  303. human.db.heroBag = human.db.heroBag or {}
  304. return heroIndex
  305. -- local heroGrid = human.db.heroBag[heroIndex]
  306. -- if type(heroGrid) ~= "table" then return end
  307. -- heroSimple.general = heroSimple.general or {}
  308. -- HeroGrid.makeHeroSimple(heroSimple, heroGrid, nil, human)
  309. -- data.heroData = heroSimple
  310. -- data.mergeTime = human.db.mergeInfo.mergeTime
  311. -- msgRet.ret = HATCH_SCCUESS_TYPE
  312. -- msgRet.tip = Json.Encode(data)
  313. -- Msg.send(msgRet, human.fd)
  314. end
  315. -- 初始化融合信息
  316. function initMergeInfo(human)
  317. human.db.mergeInfo = human.db.mergeInfo or {}
  318. human.db.mergeInfo.mergeTime = human.db.mergeInfo.mergeTime or 0 --融合时间 时间戳
  319. human.db.mergeInfo.hatchTime = human.db.mergeInfo.hatchTime or 0 --孵化时间 单位s
  320. human.db.mergeInfo.heroInfo = human.db.mergeInfo.heroInfo or {}
  321. human.db.mergeInfo.heroInfo.heroID = human.db.mergeInfo.heroInfo.heroID or 0
  322. human.db.mergeInfo.heroInfo.xLv = human.db.mergeInfo.heroInfo.xLv or 0
  323. human.db.mergeInfo.heroInfo.fatherHeroBagIndex = human.db.mergeInfo.heroInfo.fatherHeroBagIndex or 0
  324. human.db.mergeInfo.heroInfo.motherHeroBagIndex = human.db.mergeInfo.heroInfo.motherHeroBagIndex or 0
  325. human.db.mergeInfo.heroInfo.heroAttrs = human.db.mergeInfo.heroInfo.heroAttrs or RoleDefine.PANEL_ATTR_KEY
  326. end
  327. -- 获取孵化时间 单位s
  328. function getHatchTime(xLv)
  329. local time = (xLv * 5) + (xLv - 1) * (xLv - 1) - 5
  330. if time < 0 then
  331. return 0
  332. end
  333. return time * 60
  334. end
  335. -- 计算需要钻石数量
  336. function doCalcNeedZuanshi(hatchTime)
  337. local cnt = 0
  338. if hatchTime > 0 then
  339. cnt = math.ceil(hatchTime / 30)
  340. end
  341. return cnt
  342. end
  343. -- 获取xlv
  344. function getxLv(fatherHeroGrid, motherHeroGrid)
  345. local params1 = math.max(fatherHeroGrid.xLv, motherHeroGrid.xLv)
  346. local params2 = math.floor((fatherHeroGrid.lv + motherHeroGrid.lv) / 200) + 1
  347. return params1 + params2
  348. end
  349. -- 融合获取英雄ID
  350. function mergeHero(fatherHeroID, motherHeroID)
  351. local fatherHeroConfig = HeroExcel.hero[fatherHeroID]
  352. local motherHeroConfig = HeroExcel.hero[motherHeroID]
  353. if not fatherHeroConfig or not motherHeroConfig then
  354. return 0
  355. end
  356. --规则3 heroID + heroID
  357. for _, config in pairs(MergeRule[3].items) do
  358. if fatherHeroID == config[1] and motherHeroID == config[2] then
  359. return config[3]
  360. end
  361. end
  362. --规则2 种族 + heroID
  363. for _, config in pairs(MergeRule[2].items) do
  364. if fatherHeroConfig.camp == config[1] and motherHeroID == config[2] then
  365. return config[3]
  366. end
  367. end
  368. --规则1 种族 + 种族
  369. for _, config in pairs(MergeRule[1].items) do
  370. if fatherHeroConfig.camp == config[1] and motherHeroConfig.camp == config[2] then
  371. return config[3]
  372. end
  373. end
  374. return 0
  375. end