NewLogic.lua 16 KB

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