NewLogic.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. QUERY_MERGE_BEFORE_INFO_TYPE = 15 --获取融合前信息
  21. HATCH_SCCUESS_TYPE = 12 -- 孵化成功
  22. MERGE_HERO_TYPE = 13 --融合
  23. QUICK_HATCH_TYPE = 14 --加速
  24. function NewProto(human, type, param)
  25. -- print("newProto:",type,param)
  26. -- local msgRet = Msg.gc.GC_ROLE_CHANGE_BASEINFO
  27. -- msgRet.ret = type
  28. -- msgRet.tip = "test data"
  29. -- Msg.send(msgRet,human.fd)
  30. local msgRet = Msg.gc.GC_ROLE_CHANGE_BASEINFO
  31. -- 观看广告获取召唤券
  32. if type == AD_DRAW_REWARD_TYPE then
  33. human.db.adRewardCnt = human.db.adRewardCnt or 0
  34. if human.db.adRewardCnt > 4 then
  35. Broadcast.sendErr(human, Lang.AD_DRAW_REWARD_LIMIT_ERROR)
  36. return
  37. end
  38. --增加今日观看次数
  39. human.db.adRewardCnt = (human.db.adRewardCnt or 0) + 1
  40. Log.write(Log.LOGID_TEST, "adRewardCnt: " .. human.db.adRewardCnt)
  41. -- 添加高级召唤卷
  42. BagLogic.addItem(human, 118, 1, "draw_ad_reward")
  43. msgRet.ret = AD_DRAW_REWARD_TYPE
  44. msgRet.tip = Lang.AD_DRAW_REWARD_SUCCESS
  45. Msg.send(msgRet, human.fd)
  46. Broadcast.sendErr(human, Lang.AD_DRAW_REWARD_SUCCESS)
  47. return
  48. end
  49. -- 开始融合
  50. if type == MERGE_HERO_TYPE then
  51. local tb = Util.split(param, "|")
  52. if tb[1] and tb[2] then
  53. -- 初始化
  54. initMergeInfo(human)
  55. human.db.heroBag = human.db.heroBag or {}
  56. local itemId = 178 --生命雨露
  57. local fatherHeroBagIndex = tonumber(tb[1]) or 0
  58. local motherHeroBagIndex = tonumber(tb[2]) or 0
  59. Log.write(Log.LOGID_TEST,
  60. "开始融合 fatherHeroBagIndex: " .. fatherHeroBagIndex .. "motherHeroBagIndex: " .. motherHeroBagIndex)
  61. local fatherHeroGrid = human.db.heroBag[fatherHeroBagIndex]
  62. if not fatherHeroGrid then
  63. return
  64. end
  65. local motherHeroGrid = human.db.heroBag[motherHeroBagIndex]
  66. if not motherHeroGrid then
  67. return
  68. end
  69. Log.write(Log.LOGID_TEST, "motherHeroGrid: " .. Json.Encode(motherHeroGrid))
  70. Log.write(Log.LOGID_TEST, "fatherHeroGrid: " .. Json.Encode(fatherHeroGrid))
  71. local fatherHeroAttr = ObjHuman.getHeroAttrs(human, fatherHeroBagIndex)
  72. local motherHeroAttr = ObjHuman.getHeroAttrs(human, motherHeroBagIndex)
  73. Log.write(Log.LOGID_TEST, "fatherHeroAttr: " .. Json.Encode(fatherHeroAttr))
  74. Log.write(Log.LOGID_TEST, "motherHeroAttr: " .. Json.Encode(motherHeroAttr))
  75. local mergeInfo = {}
  76. local attrs = RoleDefine.PANEL_ATTR_KEY
  77. local sonHeroID = mergeHero(fatherHeroGrid.id, motherHeroGrid.id)
  78. if sonHeroID <= 0 then
  79. return
  80. end
  81. Log.write(Log.LOGID_TEST, "sonHeroID: " .. sonHeroID)
  82. for key, value in pairs(attrs) do
  83. local param1 = fatherHeroAttr[key] or 0
  84. local param2 = motherHeroAttr[key] or 0
  85. attrs[key] = math.floor(((param1 + param2) / 4))
  86. end
  87. Log.write(Log.LOGID_TEST, "融合后的属性: " .. Json.Encode(attrs))
  88. local xLv = getxLv(fatherHeroGrid, motherHeroGrid)
  89. local cnt = math.floor(xLv / 2) + 1
  90. local heroSimple = {}
  91. Log.write(Log.LOGID_TEST, "xLv: " .. xLv)
  92. Log.write(Log.LOGID_TEST, "cnt: " .. cnt)
  93. --使用道具
  94. ItemLogic.use(human, itemId, cnt)
  95. Log.write(Log.LOGID_TEST, "删除开始")
  96. -- 删除英雄
  97. HeroLogic.delHeroByIndex(human, fatherHeroBagIndex, "hero_merge")
  98. HeroLogic.delHeroByIndex(human, motherHeroBagIndex, "hero_merge")
  99. Log.write(Log.LOGID_TEST, "删除结束")
  100. -- 记录融合英雄
  101. human.db.mergeInfo.heroInfo.heroID = sonHeroID
  102. human.db.mergeInfo.heroInfo.xLv = xLv
  103. human.db.mergeInfo.heroInfo.heroAttrs = attrs
  104. human.db.mergeInfo.startTime = os.time()
  105. human.db.mergeInfo.endTime = os.time() + getHatchTime(xLv)
  106. human.db.mergeInfo.heroInfo.fatherHeroBagIndex = fatherHeroBagIndex
  107. human.db.mergeInfo.heroInfo.motherHeroBagIndex = motherHeroBagIndex
  108. Log.write(Log.LOGID_TEST, "human.db.mergeInfo : " .. Json.Encode(human.db.mergeInfo))
  109. local cf = sonHeroID and HeroExcel.hero[sonHeroID]
  110. heroSimple.name = cf and cf.name or ""
  111. mergeInfo.mergeTime = human.db.mergeInfo.hatchTime
  112. mergeInfo.xLv = xLv
  113. mergeInfo.heroData = heroSimple
  114. Log.write(Log.LOGID_TEST, "mergeInfo: " .. Json.Encode(mergeInfo))
  115. Broadcast.sendErr(human, Lang.MERGE_SUCCESS)
  116. msgRet.ret = MERGE_HERO_TYPE
  117. msgRet.tip = Json.Encode(mergeInfo)
  118. Msg.send(msgRet, human.fd)
  119. end
  120. return
  121. end
  122. -- 获取融合前的信息
  123. if type == QUERY_MERGE_BEFORE_INFO_TYPE then
  124. local tb = Util.split(param, "|")
  125. if tb[1] and tb[2] then
  126. -- 初始化
  127. initMergeInfo(human)
  128. human.db.heroBag = human.db.heroBag or {}
  129. local fatherHeroBagIndex = tonumber(tb[1]) or 0
  130. local motherHeroBagIndex = tonumber(tb[2]) or 0
  131. local fatherHeroGrid = human.db.heroBag[fatherHeroBagIndex]
  132. if not fatherHeroGrid then
  133. return
  134. end
  135. local motherHeroGrid = human.db.heroBag[motherHeroBagIndex]
  136. if not motherHeroGrid then
  137. return
  138. end
  139. local mergeInfo = {}
  140. local heroSimple = {}
  141. local xLv = getxLv(fatherHeroGrid, motherHeroGrid)
  142. local cnt = math.floor(xLv / 2) + 1
  143. local isHatch = 2
  144. local mergeItem = {}
  145. local itemId = 178
  146. for k = 1, 1 do
  147. mergeItem[k] = mergeItem[k] or {}
  148. mergeItem[k].getway = mergeItem[k].getway or {}
  149. mergeItem[k].suipian = mergeItem[k].suipian or {}
  150. mergeItem[k].equip = mergeItem[k].equip or {}
  151. mergeItem[k].fuwen = mergeItem[k].fuwen or {}
  152. Grid.makeItem(mergeItem[k], itemId, cnt)
  153. end
  154. local sonHeroID = mergeHero(fatherHeroGrid.id, motherHeroGrid.id)
  155. if sonHeroID <= 0 then
  156. return
  157. end
  158. local cf = sonHeroID and HeroExcel.hero[sonHeroID]
  159. heroSimple.name = cf and cf.name or ""
  160. mergeInfo.mergeTime = 0
  161. mergeInfo.xLv = xLv
  162. mergeInfo.mergeItem = mergeItem
  163. mergeInfo.heroData = heroSimple
  164. mergeInfo.isHatch = isHatch
  165. Log.write(Log.LOGID_TEST, "mergeInfo: " .. Json.Encode(mergeInfo))
  166. msgRet.ret = QUERY_MERGE_BEFORE_INFO_TYPE
  167. msgRet.tip = Json.Encode(mergeInfo)
  168. Msg.send(msgRet, human.fd)
  169. end
  170. return
  171. end
  172. -- 获取融合中的信息
  173. if type == QUERY_MERGE_INFO_TYPE then
  174. -- 初始化
  175. initMergeInfo(human)
  176. human.db.heroBag = human.db.heroBag or {}
  177. local mergeInfo = {}
  178. local mergeTime = 0
  179. local heroSimple = {}
  180. local xLv = human.db.mergeInfo.heroInfo.xLv or 0
  181. local heroIndex = 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. heroIndex = hatchHero(human)
  200. end
  201. if heroIndex ~= 0 then
  202. local heroGrid = human.db.heroBag[heroIndex]
  203. if not heroGrid then return end
  204. heroSimple.general = heroSimple.general or {}
  205. HeroGrid.makeHeroSimple(heroSimple, heroGrid, nil, human)
  206. else
  207. local cf = sonHeroID and HeroExcel.hero[sonHeroID]
  208. heroSimple.name = cf and cf.name or ""
  209. isHatch = 1
  210. end
  211. mergeInfo.mergeTime = mergeTime
  212. mergeInfo.xLv = xLv
  213. mergeInfo.heroData = heroSimple
  214. mergeInfo.isHatch = isHatch
  215. Log.write(Log.LOGID_TEST, "mergeInfo: " .. Json.Encode(mergeInfo))
  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. mergeInfo.mergeTime = hatchTime
  278. mergeInfo.heroData = heroSimple
  279. mergeInfo.xLv = human.db.mergeInfo.heroInfo.xLv
  280. mergeInfo.isHatch = isHatch
  281. msgRet.ret = QUICK_HATCH_TYPE
  282. msgRet.tip = Json.Encode(mergeInfo)
  283. Msg.send(msgRet, human.fd)
  284. return
  285. end
  286. return
  287. end
  288. end
  289. -- 孵化英雄
  290. function hatchHero(human)
  291. --添加英雄
  292. local heroIndex, fjlist = HeroLogic.addHero(human, human.db.mergeInfo.heroInfo.heroID, nil, 1,
  293. "hero_merge")
  294. human.db.mergeInfo.startTime = 0
  295. human.db.mergeInfo.endTime = 0
  296. human.db.mergeInfo.heroInfo = {}
  297. human.db.mergeInfo.heroInfo.heroID = 0
  298. human.db.mergeInfo.heroInfo.xLv = 0
  299. human.db.mergeInfo.heroInfo.fatherHeroBagIndex = 0
  300. human.db.mergeInfo.heroInfo.motherHeroBagIndex = 0
  301. human.db.mergeInfo.heroInfo.heroAttrs = RoleDefine.PANEL_ATTR_KEY
  302. human.db.heroBag = human.db.heroBag or {}
  303. return heroIndex
  304. -- local heroGrid = human.db.heroBag[heroIndex]
  305. -- if type(heroGrid) ~= "table" then return end
  306. -- heroSimple.general = heroSimple.general or {}
  307. -- HeroGrid.makeHeroSimple(heroSimple, heroGrid, nil, human)
  308. -- data.heroData = heroSimple
  309. -- data.mergeTime = human.db.mergeInfo.mergeTime
  310. -- msgRet.ret = HATCH_SCCUESS_TYPE
  311. -- msgRet.tip = Json.Encode(data)
  312. -- Msg.send(msgRet, human.fd)
  313. end
  314. -- 初始化融合信息
  315. function initMergeInfo(human)
  316. human.db.mergeInfo = human.db.mergeInfo or {}
  317. human.db.mergeInfo.mergeTime = human.db.mergeInfo.mergeTime or 0 --融合时间 时间戳
  318. human.db.mergeInfo.hatchTime = human.db.mergeInfo.hatchTime or 0 --孵化时间 单位s
  319. human.db.mergeInfo.heroInfo = human.db.mergeInfo.heroInfo or {}
  320. human.db.mergeInfo.heroInfo.heroID = human.db.mergeInfo.heroInfo.heroID or 0
  321. human.db.mergeInfo.heroInfo.xLv = human.db.mergeInfo.heroInfo.xLv or 0
  322. human.db.mergeInfo.heroInfo.fatherHeroBagIndex = human.db.mergeInfo.heroInfo.fatherHeroBagIndex or 0
  323. human.db.mergeInfo.heroInfo.motherHeroBagIndex = human.db.mergeInfo.heroInfo.motherHeroBagIndex or 0
  324. human.db.mergeInfo.heroInfo.heroAttrs = human.db.mergeInfo.heroInfo.heroAttrs or RoleDefine.PANEL_ATTR_KEY
  325. end
  326. -- 获取孵化时间 单位s
  327. function getHatchTime(xLv)
  328. local time = (xLv * 5) + (xLv - 1) * (xLv - 1) - 5
  329. if time < 0 then
  330. return 0
  331. end
  332. return time * 60
  333. end
  334. -- 计算需要钻石数量
  335. function doCalcNeedZuanshi(hatchTime)
  336. local cnt = 0
  337. if hatchTime > 0 then
  338. cnt = math.ceil(hatchTime / 30)
  339. end
  340. return cnt
  341. end
  342. -- 获取xlv
  343. function getxLv(fatherHeroGrid, motherHeroGrid)
  344. local fatherHeroXLv = fatherHeroGrid.xLv or 0
  345. local motherHeroXLv = motherHeroGrid.xLv or 0
  346. local params1 = math.max(fatherHeroXLv, motherHeroXLv)
  347. local params2 = math.floor((fatherHeroGrid.lv + motherHeroGrid.lv) / 200) + 1
  348. return params1 + params2
  349. end
  350. -- 融合获取英雄ID
  351. function mergeHero(fatherHeroID, motherHeroID)
  352. local fatherHeroConfig = HeroExcel.hero[fatherHeroID]
  353. local motherHeroConfig = HeroExcel.hero[motherHeroID]
  354. if not fatherHeroConfig or not motherHeroConfig then
  355. return 0
  356. end
  357. --规则3 heroID + heroID
  358. for _, config in pairs(MergeRule[3].items) do
  359. if fatherHeroID == config[1] and motherHeroID == config[2] then
  360. return config[3]
  361. end
  362. end
  363. --规则2 种族 + heroID
  364. for _, config in pairs(MergeRule[2].items) do
  365. if fatherHeroConfig.camp == config[1] and motherHeroID == config[2] then
  366. return config[3]
  367. end
  368. end
  369. --规则1 种族 + 种族
  370. for _, config in pairs(MergeRule[1].items) do
  371. if fatherHeroConfig.camp == config[1] and motherHeroConfig.camp == config[2] then
  372. return config[3]
  373. end
  374. end
  375. return 0
  376. end