NewLogic.lua 19 KB

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