NewLogic.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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_BEFOR_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_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 nowTime = os.time()
  145. local mergeTime = 0
  146. local heroSimple = {}
  147. local xLv = getxLv(fatherHeroGrid, motherHeroGrid)
  148. local cnt = math.floor(xLv / 2) + 1
  149. local sonHeroID = mergeHero(fatherHeroGrid.id, motherHeroGrid.id)
  150. if sonHeroID <= 0 then
  151. return
  152. end
  153. if human.db.mergeInfo.mergeEndTime ~= 0 then
  154. mergeTime = human.db.mergeInfo.mergeEndTime - nowTime
  155. -- 孵化成功
  156. if mergeTime <= 0 then
  157. hatchHero(human)
  158. return
  159. end
  160. end
  161. local cf = sonHeroID and HeroExcel.hero[sonHeroID]
  162. heroSimple.name = cf and cf.name or ""
  163. mergeInfo.mergeTime = human.db.mergeInfo.mergeTime
  164. mergeInfo.xLv = xLv
  165. mergeInfo.heroData = heroSimple
  166. Log.write(Log.LOGID_TEST, "mergeInfo: " .. Json.Encode(mergeInfo))
  167. local msgRet = Msg.gc.GC_ROLE_CHANGE_BASEINFO
  168. msgRet.ret = QUERY_MERGE_INFO_TYPE
  169. msgRet.tip = Json.Encode(mergeInfo)
  170. Msg.send(msgRet, human.fd)
  171. end
  172. return
  173. end
  174. -- 加速孵化
  175. if type == QUICK_HATCH_TYPE then
  176. local tb = Util.split(param, "|")
  177. local itemId = tonumber(tb[1]) or 0
  178. local cnt = tonumber(tb[2]) or 0
  179. if tb[1] and tb[2] then
  180. -- 初始化
  181. initMergeInfo(human)
  182. --使用道具
  183. ItemLogic.use(human, itemId, cnt)
  184. local remainingTime = human.db.mergeInfo.mergeTime - (60 * cnt)
  185. Log.write(Log.LOGID_TEST, "remainingTime: " .. remainingTime)
  186. -- 孵化成功
  187. if remainingTime <= 0 then
  188. hatchHero(human)
  189. -- return
  190. end
  191. Log.write(Log.LOGID_TEST, "加速后 human.db.mergeInfo : " .. Json.Encode(human.db.mergeInfo))
  192. Broadcast.sendErr(human, Lang.QUICK_HATCH_SUCCESS)
  193. local mergeInfo = {}
  194. local heroSimple = {}
  195. -- heroSimple.general = heroSimple.general or {}
  196. -- HeroGrid.makeHeroSimpleByID(heroSimple, human.db.mergeInfo.heroId, nil, nil, human)
  197. mergeInfo.mergeTime = human.db.mergeInfo.mergeTime
  198. mergeInfo.heroData = heroSimple
  199. local msgRet = Msg.gc.GC_ROLE_CHANGE_BASEINFO
  200. msgRet.ret = QUICK_HATCH_TYPE
  201. msgRet.tip = Json.Encode(mergeInfo)
  202. Msg.send(msgRet, human.fd)
  203. return
  204. end
  205. return
  206. end
  207. end
  208. -- 孵化英雄
  209. function hatchHero(human)
  210. --添加英雄
  211. local heroIndex, fjlist = HeroLogic.addHero(human, human.db.mergeInfo.heroInfo.heroID, nil, 1,
  212. "hero_merge")
  213. local data = {}
  214. local heroSimple = {}
  215. local msgRet = Msg.gc.GC_ROLE_CHANGE_BASEINFO
  216. human.db.mergeInfo.mergeStartTime = 0
  217. human.db.mergeInfo.mergeEndTime = 0
  218. human.db.mergeInfo.mergeTime = 0
  219. human.db.mergeInfo.heroInfo = {}
  220. human.db.mergeInfo.heroInfo.heroID = 0
  221. human.db.mergeInfo.heroInfo.xLv = 0
  222. human.db.mergeInfo.fatherHeroBagIndex = 0
  223. human.db.mergeInfo.motherHeroBagIndex = 0
  224. human.db.mergeInfo.heroInfo.heroAttrs = RoleDefine.PANEL_ATTR_KEY
  225. human.db.heroBag = human.db.heroBag or {}
  226. local heroGrid = human.db.heroBag[heroIndex]
  227. if type(heroGrid) ~= "table" then return end
  228. heroSimple.general = heroSimple.general or {}
  229. HeroGrid.makeHeroSimple(heroSimple, heroGrid, nil, human)
  230. data.heroData = heroSimple
  231. data.mergeTime = human.db.mergeInfo.mergeTime
  232. msgRet.ret = HATCH_SCCUESS_TYPE
  233. msgRet.tip = Json.Encode(data)
  234. Msg.send(msgRet, human.fd)
  235. end
  236. -- 初始化融合信息
  237. function initMergeInfo(human)
  238. human.db.mergeInfo = human.db.mergeInfo or {}
  239. human.db.mergeInfo.mergeStartTime = human.db.mergeInfo.mergeStartTime or 0
  240. human.db.mergeInfo.mergeEndTime = human.db.mergeInfo.mergeEndTime or 0
  241. human.db.mergeInfo.mergeTime = human.db.mergeInfo.mergeTime or 0
  242. human.db.mergeInfo.heroInfo = human.db.mergeInfo.heroInfo or {}
  243. human.db.mergeInfo.heroInfo.heroID = human.db.mergeInfo.heroInfo.heroID or 0
  244. human.db.mergeInfo.heroInfo.xLv = human.db.mergeInfo.heroInfo.xLv or 0
  245. human.db.mergeInfo.heroInfo.fatherHeroBagIndex = human.db.mergeInfo.heroInfo.fatherHeroBagIndex or 0
  246. human.db.mergeInfo.heroInfo.motherHeroBagIndex = human.db.mergeInfo.heroInfo.motherHeroBagIndex or 0
  247. human.db.mergeInfo.heroInfo.heroAttrs = human.db.mergeInfo.heroInfo.heroAttrs or RoleDefine.PANEL_ATTR_KEY
  248. end
  249. -- 获取孵化时间 单位s
  250. function getHatchTime(human, xLv)
  251. local time = (xLv * 5) + (xLv - 1) * (xLv - 1) - 5
  252. if time < 0 then
  253. return 0
  254. end
  255. return time * 60
  256. end
  257. -- 获取xlv
  258. function getxLv(fatherHeroGrid, motherHeroGrid)
  259. local params1 = math.max(fatherHeroGrid.xLv, motherHeroGrid.xLv)
  260. local params2 = math.floor((fatherHeroGrid.lv + motherHeroGrid.lv) / 200) + 1
  261. return params1 + params2
  262. end
  263. -- 融合获取英雄ID
  264. function mergeHero(fatherHeroID, motherHeroID)
  265. local fatherHeroConfig = HeroExcel.hero[fatherHeroID]
  266. local motherHeroConfig = HeroExcel.hero[motherHeroID]
  267. if not fatherHeroConfig or not motherHeroConfig then
  268. return 0
  269. end
  270. --规则3 heroID + heroID
  271. for _, config in pairs(MergeRule[3].items) do
  272. if fatherHeroID == config[1] and motherHeroID == config[2] then
  273. return config[3]
  274. end
  275. end
  276. --规则2 种族 + heroID
  277. for _, config in pairs(MergeRule[2].items) do
  278. if fatherHeroConfig.camp == config[1] and motherHeroID == config[2] then
  279. return config[3]
  280. end
  281. end
  282. --规则1 种族 + 种族
  283. for _, config in pairs(MergeRule[1].items) do
  284. if fatherHeroConfig.camp == config[1] and motherHeroConfig.camp == config[2] then
  285. return config[3]
  286. end
  287. end
  288. return 0
  289. end