NewLogic.lua 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. local Json = require("common.Json")
  18. AD_DRAW_REWARD_TYPE = 10 --观看广告领取召唤券
  19. QUERY_MERGE_INFO_TYPE = 11 --获取融合信息
  20. HATCH_SCCUESS_TYPE = 12 -- 孵化成功
  21. MERGE_HERO_TYPE = 13 --融合
  22. QUICK_HATCH_TYPE = 14 --加速
  23. QUERY_MERGE_BEFORE_INFO_TYPE = 15 --获取融合前信息
  24. GET_MERGE_HERO_TYPE = 16 --获取融合英雄
  25. AD_HATCH_RESET_FREECNT = 4 --看广告加速孵化次数
  26. function NewProto(human, type, param)
  27. -- print("newProto:",type,param)
  28. -- local msgRet = Msg.gc.GC_ROLE_CHANGE_BASEINFO
  29. -- msgRet.ret = type
  30. -- msgRet.tip = "test data"
  31. -- Msg.send(msgRet,human.fd)
  32. local msgRet = Msg.gc.GC_ROLE_CHANGE_BASEINFO
  33. -- 观看广告获取召唤券
  34. if type == AD_DRAW_REWARD_TYPE then
  35. human.db.adRewardCnt = human.db.adRewardCnt or 0
  36. if human.db.adRewardCnt > 4 then
  37. Broadcast.sendErr(human, Lang.AD_DRAW_REWARD_LIMIT_ERROR)
  38. return
  39. end
  40. --增加今日观看次数
  41. human.db.adRewardCnt = (human.db.adRewardCnt or 0) + 1
  42. Log.write(Log.LOGID_TEST, "adRewardCnt: " .. human.db.adRewardCnt)
  43. -- 添加高级召唤卷
  44. BagLogic.addItem(human, 118, 1, "draw_ad_reward")
  45. msgRet.ret = AD_DRAW_REWARD_TYPE
  46. msgRet.tip = Lang.AD_DRAW_REWARD_SUCCESS
  47. Msg.send(msgRet, human.fd)
  48. Broadcast.sendErr(human, Lang.AD_DRAW_REWARD_SUCCESS)
  49. return
  50. end
  51. -- 获取融合前的信息
  52. if type == QUERY_MERGE_BEFORE_INFO_TYPE then
  53. local tb = Util.split(param, "|")
  54. if tb[1] and tb[2] then
  55. -- 初始化
  56. initMergeInfo(human)
  57. human.db.heroBag = human.db.heroBag or {}
  58. local fatherHeroBagIndex = tonumber(tb[1]) or 0
  59. local motherHeroBagIndex = tonumber(tb[2]) or 0
  60. local fatherHeroGrid = human.db.heroBag[fatherHeroBagIndex]
  61. if not fatherHeroGrid then
  62. return
  63. end
  64. local motherHeroGrid = human.db.heroBag[motherHeroBagIndex]
  65. if not motherHeroGrid then
  66. return
  67. end
  68. local mergeInfo = {}
  69. local heroSimple = {}
  70. local xLv = getxLv(fatherHeroGrid, motherHeroGrid)
  71. local cnt = math.floor(xLv / 2) + 1
  72. local isHatch = 2
  73. local mergeItem = {}
  74. local itemId = 178
  75. for k = 1, 1 do
  76. mergeItem[k] = mergeItem[k] or {}
  77. mergeItem[k].getway = mergeItem[k].getway or {}
  78. mergeItem[k].suipian = mergeItem[k].suipian or {}
  79. mergeItem[k].equip = mergeItem[k].equip or {}
  80. mergeItem[k].fuwen = mergeItem[k].fuwen or {}
  81. Grid.makeItem(mergeItem[k], itemId, cnt)
  82. end
  83. local sonHeroID = mergeHero(fatherHeroGrid.id, motherHeroGrid.id)
  84. if sonHeroID <= 0 then
  85. return
  86. end
  87. local cf = sonHeroID and HeroExcel.hero[sonHeroID]
  88. heroSimple.name = cf and cf.name or ""
  89. mergeInfo.mergeTime = 0
  90. mergeInfo.xLv = xLv
  91. mergeInfo.mergeItem = mergeItem
  92. mergeInfo.heroData = heroSimple
  93. mergeInfo.isHatch = isHatch
  94. mergeInfo.adHatchRewardCnt = getResetAdHatchCnt(human)
  95. Log.write(Log.LOGID_TEST, "查询融合前的 mergeInfo: " .. Json.Encode(mergeInfo))
  96. msgRet.ret = QUERY_MERGE_BEFORE_INFO_TYPE
  97. msgRet.tip = Json.Encode(mergeInfo)
  98. Msg.send(msgRet, human.fd)
  99. end
  100. return
  101. end
  102. -- 开始融合
  103. if type == MERGE_HERO_TYPE then
  104. local tb = Util.split(param, "|")
  105. if tb[1] and tb[2] then
  106. -- 初始化
  107. initMergeInfo(human)
  108. human.db.heroBag = human.db.heroBag or {}
  109. local itemId = 178 --生命雨露
  110. local fatherHeroBagIndex = tonumber(tb[1]) or 0
  111. local motherHeroBagIndex = tonumber(tb[2]) or 0
  112. local fatherHeroGrid = human.db.heroBag[fatherHeroBagIndex]
  113. if not fatherHeroGrid then
  114. return
  115. end
  116. local motherHeroGrid = human.db.heroBag[motherHeroBagIndex]
  117. if not motherHeroGrid then
  118. return
  119. end
  120. local fatherHeroAttr = ObjHuman.getHeroAttrs(human, fatherHeroBagIndex)
  121. local motherHeroAttr = ObjHuman.getHeroAttrs(human, motherHeroBagIndex)
  122. local mergeInfo = {}
  123. local attrs = RoleDefine.PANEL_ATTR_KEY
  124. local sonHeroID = mergeHero(fatherHeroGrid.id, motherHeroGrid.id)
  125. if sonHeroID <= 0 then
  126. return
  127. end
  128. for key, value in pairs(attrs) do
  129. local param1 = fatherHeroAttr[key] or 0
  130. local param2 = motherHeroAttr[key] or 0
  131. attrs[key] = math.floor(((param1 + param2) / 4))
  132. end
  133. Log.write(Log.LOGID_TEST, "融合后的属性: " .. Json.Encode(attrs))
  134. local xLv = getxLv(fatherHeroGrid, motherHeroGrid)
  135. local cnt = math.floor(xLv / 2) + 1
  136. local heroSimple = {}
  137. Log.write(Log.LOGID_TEST, "xLv: " .. xLv)
  138. Log.write(Log.LOGID_TEST, "cnt: " .. cnt)
  139. local bagCnt = BagLogic.getItemCnt(human, itemId, true)
  140. if bagCnt < cnt then
  141. Broadcast.sendErr(human, Lang.ITEM_USE_ERR_NO)
  142. return
  143. end
  144. --使用道具
  145. ItemLogic.use(human, itemId, cnt)
  146. -- 删除英雄
  147. HeroLogic.delHeroByIndex(human, fatherHeroBagIndex, "hero_merge")
  148. HeroLogic.delHeroByIndex(human, motherHeroBagIndex, "hero_merge")
  149. -- 记录融合英雄
  150. human.db.mergeInfo.heroInfo.heroID = sonHeroID
  151. human.db.mergeInfo.heroInfo.xLv = xLv
  152. human.db.mergeInfo.heroInfo.heroAttrs = attrs
  153. human.db.mergeInfo.startTime = os.time()
  154. human.db.mergeInfo.endTime = os.time() + getHatchTime(xLv)
  155. human.db.mergeInfo.heroInfo.fatherHeroBagIndex = fatherHeroBagIndex
  156. human.db.mergeInfo.heroInfo.motherHeroBagIndex = motherHeroBagIndex
  157. Log.write(Log.LOGID_TEST, "human.db.mergeInfo : " .. Json.Encode(human.db.mergeInfo))
  158. local cf = sonHeroID and HeroExcel.hero[sonHeroID]
  159. heroSimple.name = cf and cf.name or ""
  160. mergeInfo.mergeTime = human.db.mergeInfo.hatchTime
  161. mergeInfo.xLv = xLv
  162. mergeInfo.heroData = heroSimple
  163. mergeInfo.isHatch = 1
  164. mergeInfo.adHatchRewardCnt = getResetAdHatchCnt(human)
  165. Log.write(Log.LOGID_TEST, "融合 mergeInfo: " .. Json.Encode(mergeInfo))
  166. Broadcast.sendErr(human, Lang.MERGE_SUCCESS)
  167. msgRet.ret = MERGE_HERO_TYPE
  168. msgRet.tip = Json.Encode(mergeInfo)
  169. Msg.send(msgRet, human.fd)
  170. end
  171. return
  172. end
  173. -- 获取融合中的信息
  174. if type == QUERY_MERGE_INFO_TYPE then
  175. -- 初始化
  176. initMergeInfo(human)
  177. human.db.heroBag = human.db.heroBag or {}
  178. local mergeInfo = {}
  179. local mergeTime = 0
  180. local heroSimple = {}
  181. local xLv = human.db.mergeInfo.heroInfo.xLv or 0
  182. local sonHeroID = human.db.mergeInfo.heroInfo.heroID or 0
  183. local isHatch = 2
  184. if sonHeroID == 0 then
  185. mergeInfo.mergeTime = mergeTime
  186. mergeInfo.xLv = xLv
  187. mergeInfo.heroData = heroSimple
  188. mergeInfo.isHatch = isHatch
  189. msgRet.ret = QUERY_MERGE_INFO_TYPE
  190. msgRet.tip = Json.Encode(mergeInfo)
  191. Msg.send(msgRet, human.fd)
  192. return
  193. end
  194. local mergeTime = human.db.mergeInfo.endTime - os.time()
  195. if mergeTime < 0 then
  196. -- 孵化成功
  197. mergeTime = 0
  198. isHatch = 3
  199. else
  200. local cf = sonHeroID and HeroExcel.hero[sonHeroID]
  201. heroSimple.name = cf and cf.name or ""
  202. isHatch = 1
  203. end
  204. mergeInfo.mergeTime = mergeTime
  205. mergeInfo.xLv = xLv
  206. mergeInfo.heroData = heroSimple
  207. mergeInfo.isHatch = isHatch
  208. mergeInfo.adHatchRewardCnt = getResetAdHatchCnt(human)
  209. msgRet.ret = QUERY_MERGE_INFO_TYPE
  210. msgRet.tip = Json.Encode(mergeInfo)
  211. Log.write(Log.LOGID_TEST, "融合中 msgRet: " .. Json.Encode(msgRet))
  212. Msg.send(msgRet, human.fd)
  213. return
  214. end
  215. -- 获取融合英雄
  216. if type == GET_MERGE_HERO_TYPE then
  217. local nowTime = os.time()
  218. local mergeInfo = {}
  219. local heroSimple = {}
  220. local xLv = human.db.mergeInfo.heroInfo.xLv or 0
  221. local sonHeroID = human.db.mergeInfo.heroInfo.heroID or 0
  222. local hatchTime = human.db.mergeInfo.endTime - nowTime
  223. local isHatch = 2
  224. local heroIndex = 0
  225. local msgTyep = GET_MERGE_HERO_TYPE
  226. if hatchTime <= 0 then
  227. -- 孵化成功
  228. hatchTime = 0
  229. isHatch = 3
  230. heroIndex = hatchHero(human)
  231. local heroGrid = human.db.heroBag[heroIndex]
  232. if not heroGrid then return end
  233. heroSimple.general = heroSimple.general or {}
  234. HeroGrid.makeHeroSimple(heroSimple, heroGrid, nil, human)
  235. else
  236. local cf = sonHeroID and HeroExcel.hero[sonHeroID]
  237. heroSimple.name = cf and cf.name or ""
  238. isHatch = 1
  239. msgTyep = QUERY_MERGE_INFO_TYPE
  240. end
  241. mergeInfo.mergeTime = hatchTime
  242. mergeInfo.xLv = xLv
  243. mergeInfo.heroData = heroSimple
  244. mergeInfo.isHatch = isHatch
  245. mergeInfo.adHatchRewardCnt = getResetAdHatchCnt(human)
  246. msgRet.ret = msgTyep
  247. msgRet.tip = Json.Encode(mergeInfo)
  248. Log.write(Log.LOGID_TEST, "获取英雄 ret: " .. Json.Encode(msgRet))
  249. Msg.send(msgRet, human.fd)
  250. return
  251. end
  252. -- 加速孵化
  253. if type == QUICK_HATCH_TYPE then
  254. local tb = Util.split(param, "|")
  255. local quickType = tonumber(tb[1]) or 0
  256. local cnt = tonumber(tb[2]) or 0
  257. local isHatch = 1
  258. if tb[1] and tb[2] then
  259. -- 初始化
  260. initMergeInfo(human)
  261. -- 剩余时间
  262. local nowTime = os.time()
  263. local hatchTime = human.db.mergeInfo.endTime - nowTime
  264. if hatchTime <= 0
  265. then
  266. Broadcast.sendErr(human, Lang.QUICK_HATCH_TIME_OUT)
  267. return
  268. end
  269. local itemSpeedTime = 0
  270. if quickType == 1 then
  271. local speedTime = (60 * 5 * cnt) -- 正常数量加速券的加速时间
  272. local lastSpeedTime = (60 * 5 * (cnt - 1)) --少一张加速券的加速时间
  273. local itemCnt = 0
  274. if hatchTime > lastSpeedTime and hatchTime <= speedTime then
  275. itemCnt = cnt
  276. elseif hatchTime > 0 and hatchTime <= lastSpeedTime then
  277. itemCnt = cnt - 1
  278. elseif hatchTime >= speedTime then
  279. itemCnt = cnt
  280. else
  281. itemCnt = 0
  282. end
  283. itemSpeedTime = (60 * 5 * itemCnt)
  284. local bagCnt = BagLogic.getItemCnt(human, 179, true)
  285. if bagCnt < itemCnt then
  286. Broadcast.sendErr(human, Lang.ITEM_USE_ERR_NO)
  287. return
  288. end
  289. --使用道具
  290. ItemLogic.use(human, 179, itemCnt)
  291. elseif quickType == 2 then
  292. local zuanshiCnt = doCalcNeedZuanshi(hatchTime)
  293. -- 判断消耗
  294. if not ObjHuman.checkRMB(human, zuanshiCnt) then
  295. return
  296. end
  297. itemSpeedTime = (30 * zuanshiCnt)
  298. -- 扣钻石
  299. ObjHuman.decZuanshi(human, -zuanshiCnt, "hero_merge")
  300. elseif quickType == 3 then
  301. human.db.adHatchRewardCnt = human.db.adHatchRewardCnt or 0
  302. if human.db.adHatchRewardCnt > 4 then
  303. Broadcast.sendErr(human, Lang.AD_DRAW_REWARD_LIMIT_ERROR)
  304. return
  305. end
  306. --加速30分钟
  307. itemSpeedTime = 30 * 60
  308. --增加今日观看次数
  309. human.db.adHatchRewardCnt = (human.db.adHatchRewardCnt or 0) + 1
  310. end
  311. human.db.mergeInfo.endTime = human.db.mergeInfo.endTime - itemSpeedTime
  312. local hatchTime = human.db.mergeInfo.endTime - nowTime
  313. -- 孵化成功
  314. if hatchTime < 0 then
  315. hatchTime = 0
  316. isHatch = 3
  317. end
  318. local mergeInfo = {}
  319. local heroSimple = {}
  320. mergeInfo.mergeTime = hatchTime
  321. mergeInfo.heroData = heroSimple
  322. mergeInfo.xLv = human.db.mergeInfo.heroInfo.xLv
  323. mergeInfo.isHatch = isHatch
  324. mergeInfo.adHatchRewardCnt = getResetAdHatchCnt(human)
  325. msgRet.ret = QUICK_HATCH_TYPE
  326. msgRet.tip = Json.Encode(mergeInfo)
  327. Log.write(Log.LOGID_TEST, "加速孵化 ret: " .. Json.Encode(ret))
  328. Msg.send(msgRet, human.fd)
  329. return
  330. end
  331. return
  332. end
  333. end
  334. -- 孵化英雄
  335. function hatchHero(human)
  336. --添加英雄
  337. local heroIndex, fjlist = HeroLogic.addHero(human, human.db.mergeInfo.heroInfo.heroID, nil, 1,
  338. "hero_merge")
  339. Log.write(Log.LOGID_TEST, "孵化英雄成功 下标" .. heroIndex)
  340. local attrs = RoleDefine.PANEL_ATTR_KEY
  341. for key, value in pairs(attrs) do
  342. attrs[key] = 1
  343. end
  344. human.db.mergeInfo.startTime = 0
  345. human.db.mergeInfo.endTime = 0
  346. human.db.mergeInfo.heroInfo = {}
  347. human.db.mergeInfo.heroInfo.heroID = 0
  348. human.db.mergeInfo.heroInfo.xLv = 0
  349. human.db.mergeInfo.heroInfo.fatherHeroBagIndex = 0
  350. human.db.mergeInfo.heroInfo.motherHeroBagIndex = 0
  351. human.db.mergeInfo.heroInfo.heroAttrs = attrs
  352. return heroIndex
  353. end
  354. -- 初始化融合信息
  355. function initMergeInfo(human)
  356. human.db.mergeInfo = human.db.mergeInfo or {}
  357. human.db.mergeInfo.mergeTime = human.db.mergeInfo.mergeTime or 0 --融合时间 时间戳
  358. human.db.mergeInfo.hatchTime = human.db.mergeInfo.hatchTime or 0 --孵化时间 单位s
  359. human.db.mergeInfo.heroInfo = human.db.mergeInfo.heroInfo or {}
  360. human.db.mergeInfo.heroInfo.heroID = human.db.mergeInfo.heroInfo.heroID or 0
  361. human.db.mergeInfo.heroInfo.xLv = human.db.mergeInfo.heroInfo.xLv or 0
  362. human.db.mergeInfo.heroInfo.fatherHeroBagIndex = human.db.mergeInfo.heroInfo.fatherHeroBagIndex or 0
  363. human.db.mergeInfo.heroInfo.motherHeroBagIndex = human.db.mergeInfo.heroInfo.motherHeroBagIndex or 0
  364. human.db.mergeInfo.heroInfo.heroAttrs = human.db.mergeInfo.heroInfo.heroAttrs or RoleDefine.PANEL_ATTR_KEY
  365. end
  366. -- 获取孵化时间 单位s
  367. function getHatchTime(xLv)
  368. local time = (xLv * 5) + (xLv - 1) * (xLv - 1) - 5
  369. if time < 0 then
  370. return 0
  371. end
  372. return time * 60
  373. end
  374. -- 计算需要钻石数量
  375. function doCalcNeedZuanshi(hatchTime)
  376. local cnt = 0
  377. if hatchTime > 0 then
  378. cnt = math.ceil(hatchTime / 30)
  379. end
  380. return cnt
  381. end
  382. -- 获取xlv
  383. function getxLv(fatherHeroGrid, motherHeroGrid)
  384. local fatherHeroXLv = fatherHeroGrid.xLv or 0
  385. local motherHeroXLv = motherHeroGrid.xLv or 0
  386. local params1 = math.max(fatherHeroXLv, motherHeroXLv)
  387. local params2 = math.floor((fatherHeroGrid.lv + motherHeroGrid.lv) / 200) + 1
  388. local xLv = params1 + params2
  389. if xLv > 50 then
  390. return 50
  391. end
  392. return xLv
  393. end
  394. -- 融合获取英雄ID
  395. function mergeHero(fatherHeroID, motherHeroID)
  396. local fatherHeroConfig = HeroExcel.hero[fatherHeroID]
  397. local motherHeroConfig = HeroExcel.hero[motherHeroID]
  398. if not fatherHeroConfig or not motherHeroConfig then
  399. return 0
  400. end
  401. --规则3 heroID + heroID
  402. for _, config in pairs(MergeRule[3].items) do
  403. if fatherHeroID == config[1] and motherHeroID == config[2] then
  404. return config[3]
  405. end
  406. end
  407. --规则2 种族 + heroID
  408. for _, config in pairs(MergeRule[2].items) do
  409. if fatherHeroConfig.camp == config[1] and motherHeroID == config[2] then
  410. return config[3]
  411. end
  412. end
  413. --规则1 种族 + 种族
  414. for _, config in pairs(MergeRule[1].items) do
  415. if fatherHeroConfig.camp == config[1] and motherHeroConfig.camp == config[2] then
  416. return config[3]
  417. end
  418. end
  419. return 0
  420. end
  421. -- 获取剩余融化孵化加速广告观看次数
  422. function getResetAdHatchCnt(human)
  423. local adHatchRewardCnt = human.db.adHatchRewardCnt or 0
  424. return math.max(AD_HATCH_RESET_FREECNT - adHatchRewardCnt, 0)
  425. end