NewLogic.lua 19 KB

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