NewLogic.lua 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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. WATCH_AD_QUICK_HATCH_TYPE = 16 --观看广告加速孵化 30分钟
  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 == WATCH_AD_QUICK_HATCH_TYPE then
  53. human.db.adHatchRewardCnt = human.db.adHatchRewardCnt or 0
  54. if human.db.adHatchRewardCnt > 4 then
  55. Broadcast.sendErr(human, Lang.AD_DRAW_REWARD_LIMIT_ERROR)
  56. return
  57. end
  58. local itemSpeedTime = 30 * 60
  59. local nowTime = os.time()
  60. local mergeInfo = {}
  61. local mergeTime = 0
  62. local heroSimple = {}
  63. local xLv = human.db.mergeInfo.heroInfo.xLv or 0
  64. local heroIndex = 0
  65. local sonHeroID = human.db.mergeInfo.heroInfo.heroID or 0
  66. local isHatch = 2
  67. local cf = sonHeroID and HeroExcel.hero[sonHeroID]
  68. heroSimple.name = cf and cf.name or ""
  69. --增加今日观看次数
  70. human.db.adHatchRewardCnt = (human.db.adHatchRewardCnt or 0) + 1
  71. human.db.mergeInfo.endTime = human.db.mergeInfo.endTime - itemSpeedTime
  72. Log.write(Log.LOGID_TEST, "human.db.mergeInfo.endTime: " .. human.db.mergeInfo.endTime)
  73. local hatchTime = human.db.mergeInfo.endTime - nowTime
  74. if hatchTime < 0 then
  75. -- 孵化成功
  76. mergeTime = 0
  77. isHatch = 3
  78. Log.write(Log.LOGID_TEST, "孵化成功: " .. Json.Encode(human.db.mergeInfo))
  79. heroIndex = hatchHero(human)
  80. end
  81. if heroIndex ~= 0 then
  82. local heroGrid = human.db.heroBag[heroIndex]
  83. if not heroGrid then return end
  84. heroSimple.general = heroSimple.general or {}
  85. HeroGrid.makeHeroSimple(heroSimple, heroGrid, nil, human)
  86. else
  87. local cf = sonHeroID and HeroExcel.hero[sonHeroID]
  88. heroSimple.name = cf and cf.name or ""
  89. isHatch = 1
  90. end
  91. mergeInfo.mergeTime = mergeTime
  92. mergeInfo.xLv = xLv
  93. mergeInfo.heroData = heroSimple
  94. mergeInfo.isHatch = isHatch
  95. mergeInfo.adHatchRewardCnt = getResetAdHatchCnt(human)
  96. Log.write(Log.LOGID_TEST, "看广告 mergeInfo: " .. Json.Encode(mergeInfo))
  97. msgRet.ret = WATCH_AD_QUICK_HATCH_TYPE
  98. msgRet.tip = Json.Encode(mergeInfo)
  99. Msg.send(msgRet, human.fd)
  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_BEFORE_INFO_TYPE then
  175. local tb = Util.split(param, "|")
  176. if tb[1] and tb[2] then
  177. -- 初始化
  178. initMergeInfo(human)
  179. human.db.heroBag = human.db.heroBag or {}
  180. local fatherHeroBagIndex = tonumber(tb[1]) or 0
  181. local motherHeroBagIndex = tonumber(tb[2]) or 0
  182. local fatherHeroGrid = human.db.heroBag[fatherHeroBagIndex]
  183. if not fatherHeroGrid then
  184. return
  185. end
  186. local motherHeroGrid = human.db.heroBag[motherHeroBagIndex]
  187. if not motherHeroGrid then
  188. return
  189. end
  190. local mergeInfo = {}
  191. local heroSimple = {}
  192. local xLv = getxLv(fatherHeroGrid, motherHeroGrid)
  193. local cnt = math.floor(xLv / 2) + 1
  194. local isHatch = 2
  195. local mergeItem = {}
  196. local itemId = 178
  197. for k = 1, 1 do
  198. mergeItem[k] = mergeItem[k] or {}
  199. mergeItem[k].getway = mergeItem[k].getway or {}
  200. mergeItem[k].suipian = mergeItem[k].suipian or {}
  201. mergeItem[k].equip = mergeItem[k].equip or {}
  202. mergeItem[k].fuwen = mergeItem[k].fuwen or {}
  203. Grid.makeItem(mergeItem[k], itemId, cnt)
  204. end
  205. local sonHeroID = mergeHero(fatherHeroGrid.id, motherHeroGrid.id)
  206. if sonHeroID <= 0 then
  207. return
  208. end
  209. local cf = sonHeroID and HeroExcel.hero[sonHeroID]
  210. heroSimple.name = cf and cf.name or ""
  211. mergeInfo.mergeTime = 0
  212. mergeInfo.xLv = xLv
  213. mergeInfo.mergeItem = mergeItem
  214. mergeInfo.heroData = heroSimple
  215. mergeInfo.isHatch = isHatch
  216. mergeInfo.adHatchRewardCnt = getResetAdHatchCnt(human)
  217. Log.write(Log.LOGID_TEST, "查询融合前的 mergeInfo: " .. Json.Encode(mergeInfo))
  218. msgRet.ret = QUERY_MERGE_BEFORE_INFO_TYPE
  219. msgRet.tip = Json.Encode(mergeInfo)
  220. Msg.send(msgRet, human.fd)
  221. end
  222. return
  223. end
  224. -- 获取融合中的信息
  225. if type == QUERY_MERGE_INFO_TYPE then
  226. -- 初始化
  227. initMergeInfo(human)
  228. human.db.heroBag = human.db.heroBag or {}
  229. local mergeInfo = {}
  230. local mergeTime = 0
  231. local heroSimple = {}
  232. local xLv = human.db.mergeInfo.heroInfo.xLv or 0
  233. local heroIndex = 0
  234. local sonHeroID = human.db.mergeInfo.heroInfo.heroID or 0
  235. local isHatch = 2
  236. if sonHeroID == 0 then
  237. mergeInfo.mergeTime = mergeTime
  238. mergeInfo.xLv = xLv
  239. mergeInfo.heroData = heroSimple
  240. mergeInfo.isHatch = isHatch
  241. msgRet.ret = QUERY_MERGE_INFO_TYPE
  242. msgRet.tip = Json.Encode(mergeInfo)
  243. Msg.send(msgRet, human.fd)
  244. return
  245. end
  246. local mergeTime = human.db.mergeInfo.endTime - os.time()
  247. if mergeTime < 0 then
  248. -- 孵化成功
  249. mergeTime = 0
  250. isHatch = 3
  251. Log.write(Log.LOGID_TEST, "孵化成功: " .. Json.Encode(human.db.mergeInfo))
  252. heroIndex = hatchHero(human)
  253. end
  254. if heroIndex ~= 0 then
  255. local heroGrid = human.db.heroBag[heroIndex]
  256. if not heroGrid then return end
  257. heroSimple.general = heroSimple.general or {}
  258. HeroGrid.makeHeroSimple(heroSimple, heroGrid, nil, human)
  259. else
  260. local cf = sonHeroID and HeroExcel.hero[sonHeroID]
  261. heroSimple.name = cf and cf.name or ""
  262. isHatch = 1
  263. end
  264. mergeInfo.mergeTime = mergeTime
  265. mergeInfo.xLv = xLv
  266. mergeInfo.heroData = heroSimple
  267. mergeInfo.isHatch = isHatch
  268. mergeInfo.adHatchRewardCnt = getResetAdHatchCnt(human)
  269. Log.write(Log.LOGID_TEST, "融合中 mergeInfo: " .. Json.Encode(mergeInfo))
  270. msgRet.ret = QUERY_MERGE_INFO_TYPE
  271. msgRet.tip = Json.Encode(mergeInfo)
  272. Msg.send(msgRet, human.fd)
  273. return
  274. end
  275. -- 加速孵化
  276. if type == QUICK_HATCH_TYPE then
  277. local tb = Util.split(param, "|")
  278. local itemId = tonumber(tb[1]) or 0
  279. local cnt = tonumber(tb[2]) or 0
  280. local isHatch = 2
  281. if tb[1] and tb[2] then
  282. -- 初始化
  283. initMergeInfo(human)
  284. -- 剩余时间
  285. local nowTime = os.time()
  286. local hatchTime = human.db.mergeInfo.endTime - nowTime
  287. if hatchTime <= 0
  288. then
  289. return
  290. end
  291. local itemSpeedTime = 0
  292. if itemId == 179 then
  293. local speedTime = (60 * 5 * cnt) -- 正常数量加速券的加速时间
  294. local lastSpeedTime = (60 * 5 * (cnt - 1)) --少一张加速券的加速时间
  295. local itemCnt = 0
  296. if hatchTime > lastSpeedTime and hatchTime <= speedTime then
  297. itemCnt = cnt
  298. elseif hatchTime > 0 and hatchTime <= lastSpeedTime then
  299. itemCnt = cnt - 1
  300. elseif hatchTime >= speedTime then
  301. itemCnt = cnt
  302. else
  303. itemCnt = 0
  304. end
  305. itemSpeedTime = (60 * 5 * itemCnt)
  306. local bagCnt = BagLogic.getItemCnt(human, itemId, true)
  307. if bagCnt < itemCnt then
  308. Broadcast.sendErr(human, Lang.ITEM_USE_ERR_NO)
  309. return
  310. end
  311. --使用道具
  312. ItemLogic.use(human, itemId, itemCnt)
  313. else
  314. local zuanshiCnt = doCalcNeedZuanshi(hatchTime)
  315. -- 判断消耗
  316. if not ObjHuman.checkRMB(human, zuanshiCnt) then
  317. return
  318. end
  319. itemSpeedTime = (30 * zuanshiCnt)
  320. -- 扣钻石
  321. ObjHuman.decZuanshi(human, -zuanshiCnt, "hero_merge")
  322. end
  323. human.db.mergeInfo.endTime = human.db.mergeInfo.endTime - itemSpeedTime
  324. Log.write(Log.LOGID_TEST, "human.db.mergeInfo.endTime: " .. human.db.mergeInfo.endTime)
  325. local hatchTime = human.db.mergeInfo.endTime - nowTime
  326. -- 孵化成功
  327. if hatchTime < 0 then
  328. hatchTime = 0
  329. isHatch = 3
  330. end
  331. Broadcast.sendErr(human, Lang.QUICK_HATCH_SUCCESS)
  332. local mergeInfo = {}
  333. local heroSimple = {}
  334. mergeInfo.mergeTime = hatchTime
  335. mergeInfo.heroData = heroSimple
  336. mergeInfo.xLv = human.db.mergeInfo.heroInfo.xLv
  337. mergeInfo.isHatch = isHatch
  338. mergeInfo.adHatchRewardCnt = getResetAdHatchCnt(human)
  339. Log.write(Log.LOGID_TEST, "加速孵化 mergeInfo: " .. Json.Encode(mergeInfo))
  340. msgRet.ret = QUICK_HATCH_TYPE
  341. msgRet.tip = Json.Encode(mergeInfo)
  342. Msg.send(msgRet, human.fd)
  343. return
  344. end
  345. return
  346. end
  347. end
  348. -- 孵化英雄
  349. function hatchHero(human)
  350. --添加英雄
  351. local heroIndex, fjlist = HeroLogic.addHero(human, human.db.mergeInfo.heroInfo.heroID, nil, 1,
  352. "hero_merge")
  353. Log.write(Log.LOGID_TEST, "孵化英雄成功 下标" .. heroIndex)
  354. local attrs = RoleDefine.PANEL_ATTR_KEY
  355. for key, value in pairs(attrs) do
  356. attrs[key] = 1
  357. end
  358. human.db.mergeInfo.startTime = 0
  359. human.db.mergeInfo.endTime = 0
  360. human.db.mergeInfo.heroInfo = {}
  361. human.db.mergeInfo.heroInfo.heroID = 0
  362. human.db.mergeInfo.heroInfo.xLv = 0
  363. human.db.mergeInfo.heroInfo.fatherHeroBagIndex = 0
  364. human.db.mergeInfo.heroInfo.motherHeroBagIndex = 0
  365. human.db.mergeInfo.heroInfo.heroAttrs = attrs
  366. return heroIndex
  367. end
  368. -- 初始化融合信息
  369. function initMergeInfo(human)
  370. human.db.mergeInfo = human.db.mergeInfo or {}
  371. human.db.mergeInfo.mergeTime = human.db.mergeInfo.mergeTime or 0 --融合时间 时间戳
  372. human.db.mergeInfo.hatchTime = human.db.mergeInfo.hatchTime or 0 --孵化时间 单位s
  373. human.db.mergeInfo.heroInfo = human.db.mergeInfo.heroInfo or {}
  374. human.db.mergeInfo.heroInfo.heroID = human.db.mergeInfo.heroInfo.heroID or 0
  375. human.db.mergeInfo.heroInfo.xLv = human.db.mergeInfo.heroInfo.xLv or 0
  376. human.db.mergeInfo.heroInfo.fatherHeroBagIndex = human.db.mergeInfo.heroInfo.fatherHeroBagIndex or 0
  377. human.db.mergeInfo.heroInfo.motherHeroBagIndex = human.db.mergeInfo.heroInfo.motherHeroBagIndex or 0
  378. human.db.mergeInfo.heroInfo.heroAttrs = human.db.mergeInfo.heroInfo.heroAttrs or RoleDefine.PANEL_ATTR_KEY
  379. end
  380. -- 获取孵化时间 单位s
  381. function getHatchTime(xLv)
  382. local time = (xLv * 5) + (xLv - 1) * (xLv - 1) - 5
  383. if time < 0 then
  384. return 0
  385. end
  386. return time * 60
  387. end
  388. -- 计算需要钻石数量
  389. function doCalcNeedZuanshi(hatchTime)
  390. local cnt = 0
  391. if hatchTime > 0 then
  392. cnt = math.ceil(hatchTime / 30)
  393. end
  394. return cnt
  395. end
  396. -- 获取xlv
  397. function getxLv(fatherHeroGrid, motherHeroGrid)
  398. local fatherHeroXLv = fatherHeroGrid.xLv or 0
  399. local motherHeroXLv = motherHeroGrid.xLv or 0
  400. local params1 = math.max(fatherHeroXLv, motherHeroXLv)
  401. local params2 = math.floor((fatherHeroGrid.lv + motherHeroGrid.lv) / 200) + 1
  402. local xLv = params1 + params2
  403. if xLv > 50 then
  404. return 50
  405. end
  406. return xLv
  407. end
  408. -- 融合获取英雄ID
  409. function mergeHero(fatherHeroID, motherHeroID)
  410. local fatherHeroConfig = HeroExcel.hero[fatherHeroID]
  411. local motherHeroConfig = HeroExcel.hero[motherHeroID]
  412. if not fatherHeroConfig or not motherHeroConfig then
  413. return 0
  414. end
  415. --规则3 heroID + heroID
  416. for _, config in pairs(MergeRule[3].items) do
  417. if fatherHeroID == config[1] and motherHeroID == config[2] then
  418. return config[3]
  419. end
  420. end
  421. --规则2 种族 + heroID
  422. for _, config in pairs(MergeRule[2].items) do
  423. if fatherHeroConfig.camp == config[1] and motherHeroID == config[2] then
  424. return config[3]
  425. end
  426. end
  427. --规则1 种族 + 种族
  428. for _, config in pairs(MergeRule[1].items) do
  429. if fatherHeroConfig.camp == config[1] and motherHeroConfig.camp == config[2] then
  430. return config[3]
  431. end
  432. end
  433. return 0
  434. end
  435. -- 获取剩余融化孵化加速广告观看次数
  436. function getResetAdHatchCnt(human)
  437. local adHatchRewardCnt = human.db.adHatchRewardCnt or 0
  438. return math.max(AD_HATCH_RESET_FREECNT - adHatchRewardCnt, 0)
  439. end