XianzhiLogic.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. -- 英雄召唤+英雄置换
  2. -- 每次抽奖会额外赠送卷轴和珠子
  3. local Lang = require("common.Lang")
  4. local Msg = require("core.Msg")
  5. local XianzhiExcel = require("excel.xianzhi")
  6. local HeroExcel = require("excel.hero")
  7. local ItemDefine = require("bag.ItemDefine")
  8. local BagLogic = require("bag.BagLogic")
  9. local HeroLogic = require("hero.HeroLogic")
  10. local Grid = require("bag.Grid")
  11. local HeroGrid = require("hero.HeroGrid")
  12. local HeroBook = require("hero.HeroBook")
  13. local Broadcast = require("broadcast.Broadcast")
  14. local HeroDefine = require("hero.HeroDefine")
  15. local ChengjiuLogic = require("chengjiu.ChengjiuLogic")
  16. local ChengjiuDefine = require("chengjiu.ChengjiuDefine")
  17. local Util = require("common.Util")
  18. local ChatPaoMaLogic = require("chat.ChatPaoMaLogic")
  19. -- local DrawCardLogic = require("drawCard.DrawCardLogic")
  20. local RoleSystemLogic = require("roleSystem.RoleSystemLogic")
  21. local RoleSystemDefine = require("roleSystem.RoleSystemDefine")
  22. local HeroGrowUp = require("absAct.HeroGrowUp")
  23. local YunYingLogic = require("yunying.YunYingLogic")
  24. local TriggerDefine = require("trigger.TriggerDefine")
  25. local TriggerLogic = require("trigger.TriggerLogic")
  26. local WeekTaskLogic = require("dailyTask.WeekTaskLogic")
  27. local ClutterDataLogic = require("clutter.ClutterDataLogic")
  28. local ClutterDataDefine = require("clutter.ClutterDataDefine")
  29. local Log = require("common.Log")
  30. local xianzhiOutPutId = 126
  31. local xianzhiDelitem_Normal = 100
  32. local xianzhiDelitem_Special = 300
  33. local function makeShopElem(human,config)
  34. end
  35. function detail(human, camp)
  36. local xianzhiConfig = XianzhiExcel.xianzhi[camp]
  37. if not xianzhiConfig then return end
  38. local msgRet = Msg.gc.GC_XIANZHI_DETAIL
  39. local cnt = 0
  40. local bf = false
  41. local itemCnt = 0
  42. for heroID,heroConfig in pairs(HeroExcel.hero) do
  43. bf = false
  44. itemCnt = heroConfig.star == 5 and 50 or 30
  45. if camp == 4 then
  46. if (heroConfig.camp == 4 or heroConfig.camp == 5) and heroConfig.seerLv ~= 0 then
  47. bf = true
  48. end
  49. else
  50. if heroConfig.camp == camp and heroConfig.seerLv ~= 0 then
  51. bf = true
  52. end
  53. end
  54. -- UR不显示
  55. if heroConfig.grade == 5 then
  56. bf = false
  57. end
  58. if bf then
  59. cnt = cnt + 1
  60. Grid.makeItem(msgRet.item[cnt], heroID, itemCnt)
  61. end
  62. end
  63. for j = 1, #xianzhiConfig.zhaohuan do
  64. local tempConfig = xianzhiConfig.zhaohuan[j]
  65. if cnt > 50 then
  66. break
  67. end
  68. if j == #xianzhiConfig.zhaohuan or j == #xianzhiConfig.zhaohuan - 1 then
  69. else
  70. for k, v in ipairs(tempConfig[1]) do
  71. cnt = cnt + 1
  72. local minCnt = tempConfig[2][1]
  73. local maxCnt = tempConfig[2][2]
  74. Grid.makeItem(msgRet.item[cnt], v[1], minCnt)
  75. end
  76. end
  77. end
  78. msgRet.item[0] = cnt
  79. Msg.send(msgRet, human.fd)
  80. end
  81. -- 召唤
  82. function zhaohuanDo(human, camp, cnt, skip)
  83. local flag = RoleSystemLogic.isOpen(human,RoleSystemDefine.ROLE_SYS_ID_402)
  84. if flag ~= true then
  85. return Broadcast.sendErr(human, Lang.XIANZHI_OPEN_NEED_LV)
  86. end
  87. if cnt < 1 then return end
  88. local xianzhiConfig = XianzhiExcel.xianzhi[camp]
  89. if not xianzhiConfig then return end
  90. local getID = nil
  91. local getCnt = nil
  92. -- 判断消耗
  93. local needItemID = ItemDefine.ITEM_XIANZHI_BAOZHU_ID
  94. local needItemCnt = cnt
  95. local nowItemCnt = BagLogic.getItemCnt(human, needItemID)
  96. if nowItemCnt < needItemCnt then
  97. return Broadcast.sendErr(human, Util.format(Lang.XIANZHI_NO_ITEM,ItemDefine.getValue(needItemID,"name")))
  98. end
  99. getID,getCnt = zhaohuanHero(human,camp, cnt)
  100. if getID == nil or getCnt == nil then
  101. return
  102. end
  103. human.db.drawCard.skip = skip or 0
  104. -- 扣消耗
  105. BagLogic.delItem(human, needItemID, needItemCnt, "xianzhi_zhaohuan")
  106. ClutterDataLogic.UpdateDiffLotteryTimes(human, ClutterDataDefine.CLUTTER_TYPE4, cnt, ClutterDataDefine.CLUTTER_TYPE4)
  107. -- 增加物品
  108. local msgRet = Msg.gc.GC_XIANZHI_ZHAOHUAN_DO
  109. msgRet.camp = camp
  110. BagLogic.cleanMomentItemList()
  111. -- 合并相同ID的数据
  112. local mergedData = {} -- {id = {cnt, isNew, heroConfig}}
  113. for i = 1, #getID do
  114. local heroID = getID[i]
  115. local heroCnt = getCnt[i]
  116. local heroConfig = HeroExcel.hero[heroID]
  117. -- 添加到背包临时列表
  118. BagLogic.updateMomentItem(2, heroID, heroCnt)
  119. -- 合并数据
  120. if not mergedData[heroID] then
  121. local isNew = nil
  122. if heroConfig then
  123. isNew = not HeroBook.isGet(human, heroConfig.id, heroConfig.star)
  124. -- 广播只发送一次
  125. ChatPaoMaLogic.broadcast(human, ChatPaoMaLogic.PAOMA_TYPE_BROAD_TYPE7, heroConfig.seerLv, heroID)
  126. end
  127. mergedData[heroID] = {
  128. cnt = heroCnt,
  129. isNew = isNew,
  130. heroConfig = heroConfig
  131. }
  132. else
  133. -- 相同ID,累加数量
  134. mergedData[heroID].cnt = mergedData[heroID].cnt + heroCnt
  135. end
  136. end
  137. -- 填充协议数据(协议最大支持30个)
  138. local listIndex = 0
  139. local maxListCount = 30
  140. for id, data in pairs(mergedData) do
  141. if listIndex >= maxListCount then
  142. break
  143. end
  144. listIndex = listIndex + 1
  145. HeroGrid.makeHeroNice(msgRet.list[listIndex], id, data.cnt, data.isNew)
  146. -- 日志:添加到协议显示列表
  147. local heroConfig = HeroExcel.hero[id]
  148. -- if heroConfig then
  149. -- Log.write(Log.LOGID_DEBUG, "[zhaohuanDo] 添加到协议显示列表(英雄): uuid="..human.db._id..", heroID="..id..", cnt="..data.cnt..", isNew="..(data.isNew and 1 or 0))
  150. -- else
  151. -- Log.write(Log.LOGID_DEBUG, "[zhaohuanDo] 添加到协议显示列表(物品): uuid="..human.db._id..", itemID="..id..", cnt="..data.cnt)
  152. -- end
  153. end
  154. BagLogic.updateMomentItem(2, xianzhiOutPutId, cnt * 1) --每次召唤获得精华1个
  155. -- 额外增加的物品 30珠子 1转轴
  156. local exItemCnt1 = cnt * 100
  157. BagLogic.updateMomentItem(2, ItemDefine.ITEM_XIANZHI_ZHUFU_ID, exItemCnt1)
  158. BagLogic.addMomentItemList(human, "xianzhi_zhaohuan")
  159. -- 注意:召唤只给碎片,不给完整英雄
  160. -- 英雄碎片已经通过 BagLogic.updateMomentItem 和 BagLogic.addMomentItemList 添加到背包
  161. -- 添加天命结晶到协议(如果还有空间)
  162. if listIndex < maxListCount then
  163. listIndex = listIndex + 1
  164. HeroGrid.makeHeroNice(msgRet.list[listIndex], ItemDefine.ITEM_XIANZHI_ZHUFU_ID, exItemCnt1, nil)
  165. --Log.write(Log.LOGID_DEBUG, "[zhaohuanDo] 添加到协议显示列表(天命结晶): uuid="..human.db._id..", itemID="..ItemDefine.ITEM_XIANZHI_ZHUFU_ID..", cnt="..exItemCnt1)
  166. else
  167. --Log.write(Log.LOGID_DEBUG, "[zhaohuanDo] 协议已满,天命结晶未添加到显示列表: uuid="..human.db._id..", itemID="..ItemDefine.ITEM_XIANZHI_ZHUFU_ID..", cnt="..exItemCnt1..", listIndex="..listIndex)
  168. end
  169. -- 添加珠子到协议(如果还有空间)
  170. if listIndex < maxListCount then
  171. listIndex = listIndex + 1
  172. HeroGrid.makeHeroNice(msgRet.list[listIndex], xianzhiOutPutId, cnt * 1, nil)
  173. --Log.write(Log.LOGID_DEBUG, "[zhaohuanDo] 添加到协议显示列表(珠子): uuid="..human.db._id..", itemID="..xianzhiOutPutId..", cnt="..(cnt * 1))
  174. else
  175. --Log.write(Log.LOGID_DEBUG, "[zhaohuanDo] 协议已满,珠子未添加到显示列表: uuid="..human.db._id..", itemID="..xianzhiOutPutId..", cnt="..(cnt * 1)..", listIndex="..listIndex)
  176. end
  177. msgRet.list[0] = listIndex
  178. --Log.write(Log.LOGID_DEBUG, "[zhaohuanDo] 协议填充完成: uuid="..human.db._id..", 最终listIndex="..listIndex..", 协议list[0]="..msgRet.list[0])
  179. Msg.send(msgRet, human.fd)
  180. ChengjiuLogic.onCallback(human,ChengjiuDefine.CJ_TASK_TYPE_14,cnt)
  181. HeroGrowUp.onCallback(human, HeroGrowUp.TASKTYPE2, cnt)
  182. YunYingLogic.onCallBack(human, "onTMDrawCard", cnt)
  183. WeekTaskLogic.recordWeekTaskFinishCnt(human, WeekTaskLogic.WEEK_TASK_ID_3, cnt)
  184. TriggerLogic.PublishEvent(TriggerDefine.EVENT_TYPE_FATESUMMON, human.db._id, cnt)
  185. end
  186. -- 置换查询
  187. function zhihuanQuery(human, heroID, heroIndex)
  188. local flag = RoleSystemLogic.isOpen(human,RoleSystemDefine.ROLE_SYS_ID_402)
  189. if flag ~= true then
  190. return Broadcast.sendErr(human, Lang.XIANZHI_OPEN_NEED_LV)
  191. end
  192. local heroGrid = HeroLogic.getHeroGrid(human, heroID, heroIndex)
  193. if not heroGrid then return end
  194. -- 判断是否被锁
  195. if heroGrid.isLock then
  196. return
  197. end
  198. -- 阵营,星级判断
  199. local heroConfig = HeroExcel.hero[heroGrid.id]
  200. local camp = heroConfig.camp
  201. local star = heroConfig.star
  202. if heroConfig.grade ~= 4 or star ~= 5 then return end
  203. local needItemCnt = xianzhiDelitem_Normal
  204. if camp == 4 or camp == 5 then
  205. needItemCnt = xianzhiDelitem_Special
  206. end
  207. -- 判断消耗
  208. local needItemID = ItemDefine.ITEM_XIANZHI_ZHUFU_ID
  209. local nowItemCnt = BagLogic.getItemCnt(human, needItemID)
  210. if nowItemCnt < needItemCnt then
  211. return Broadcast.sendErr(human, Util.format(Lang.XIANZHI_NO_ITEM,ItemDefine.getValue(needItemID,"name")))
  212. end
  213. -- 扣消耗
  214. BagLogic.delItem(human, needItemID, needItemCnt, "xianzhi_zhihuan")
  215. -- 生成新ID
  216. local nextHero = zhihuanHero(star, camp,heroGrid.id)
  217. if not nextHero then
  218. -- 没有置换目标
  219. print("[zhihuanQuery] 没有获取到置换目标 star = "..star.." camp = "..camp.." id = "..heroGrid.id)
  220. return
  221. end
  222. -- 新ID不等于旧ID
  223. human.tempZhihuanID = nextHero
  224. human.tempZhihuanHeroID = heroID
  225. human.tempZhihuanHeroIndex = heroIndex
  226. -- 通知客户端
  227. local msgRet = Msg.gc.GC_XIANZHI_ZHIHUAN_QUERY
  228. msgRet.heroID = heroID
  229. msgRet.heroIndex = heroIndex
  230. msgRet.skip = human.db.drawCard.skip or 0
  231. HeroGrid.makeHeroSimpleByID(msgRet.heroSimple, nextHero)
  232. HeroGrid.makeHeroStatic(msgRet.heroStatic,nextHero)
  233. Msg.send(msgRet, human.fd)
  234. end
  235. function zhihuanDo(human, heroID, heroIndex)
  236. local heroGrid = HeroLogic.getHeroGrid(human, heroID, heroIndex)
  237. if not heroGrid then return end
  238. -- 判断是否被锁
  239. if heroGrid.isLock then
  240. return
  241. end
  242. if human.tempZhihuanHeroID ~= heroID then return end
  243. if human.tempZhihuanHeroIndex ~= heroIndex then return end
  244. local tempGrid = heroGrid
  245. tempGrid.id = human.tempZhihuanID
  246. -- 先删
  247. HeroLogic.delHeroByIndex(human, heroIndex, "xianzhi_zhihuan")
  248. human.tempZhihuanID = nil
  249. human.tempZhihuanHeroID = nil
  250. human.tempZhihuanHeroIndex = nil
  251. -- 改db
  252. local newIndex = HeroLogic.addHeroByGrid(human, tempGrid, "xianzhi_zhihuan")
  253. -- 通知客户端
  254. local msgRet = Msg.gc.GC_XIANZHI_ZHIHUAN_DO
  255. HeroGrid.makeHeroSimple(msgRet.heroSimple, tempGrid, newIndex, human)
  256. Msg.send(msgRet, human.fd)
  257. end
  258. --------------------------功能函数---------------------------------
  259. function zhaohuanHero(human,camp, cnt)
  260. local xianzhiConfig = Util.copyTable(XianzhiExcel.xianzhi[camp])
  261. if not xianzhiConfig then return end
  262. local totalWeight = 0
  263. local totalWeight1 = 0
  264. for i = 1, #xianzhiConfig.zhaohuan do
  265. totalWeight = totalWeight + xianzhiConfig.zhaohuan[i][3]
  266. end
  267. for i = 1, #xianzhiConfig.zhaohuan1 do
  268. totalWeight1 = totalWeight1 + xianzhiConfig.zhaohuan1[i][3]
  269. end
  270. cnt = cnt * 5
  271. local cjPrivilege = ChengjiuLogic.checkPrivilege(human,ChengjiuDefine.PRIVILEGE_TYPE_4)
  272. if cjPrivilege then
  273. local randNum = math.random(1,100)
  274. if randNum == 1 then
  275. cnt = cnt + 1
  276. end
  277. end
  278. local getID = {}
  279. local getCnt = {}
  280. for i = 1, cnt do
  281. if camp == 4 or camp == 5 then
  282. camp = math.random(4,5)
  283. end
  284. local tWeight = totalWeight
  285. local zhaohuan = xianzhiConfig.zhaohuan
  286. if i % 5 == 0 then
  287. zhaohuan = xianzhiConfig.zhaohuan1
  288. tWeight = totalWeight1
  289. end
  290. local randNum = math.random(1, tWeight)
  291. for j = 1, #zhaohuan do
  292. local tempConfig = zhaohuan[j]
  293. local tempWeight = tempConfig[3]
  294. if randNum <= tempWeight then --抽中
  295. if j == #zhaohuan or j == #zhaohuan - 1 then --抽中五星英雄
  296. local allWeight = 0
  297. for k,v in ipairs(tempConfig[1]) do
  298. allWeight = allWeight + v[2]
  299. end
  300. local nowNum = math.random(1, allWeight)
  301. for k, v in ipairs(tempConfig[1]) do
  302. local nowWeight = v[2]
  303. if nowNum <= nowWeight then
  304. local rate = v[1]
  305. local chipId = {}
  306. local index = 1
  307. for heroID,heroConfig in pairs(HeroExcel.hero) do
  308. if heroConfig.camp == camp and heroConfig.seerLv == rate then
  309. chipId[index] = heroID
  310. index = index + 1
  311. end
  312. end
  313. local randomNum = 0
  314. if index == 1 then
  315. return
  316. else
  317. randomNum = math.random(1,index -1)
  318. end
  319. getID[i] = chipId[randomNum]
  320. local minCnt = tempConfig[2][1]
  321. local maxCnt = tempConfig[2][2]
  322. getCnt[i] = math.random(minCnt,maxCnt)
  323. break
  324. end
  325. nowNum = nowNum - nowWeight
  326. end
  327. break
  328. else -- 抽到其他物品
  329. local allWeight = 0
  330. for k, v in ipairs(tempConfig[1]) do
  331. allWeight = allWeight + v[2]
  332. end
  333. local nowNum = math.random(1, allWeight)
  334. for k, v in ipairs(tempConfig[1]) do
  335. local nowWeight = v[2]
  336. if nowNum <= nowWeight then
  337. getID[i] = v[1]
  338. local minCnt = tempConfig[2][1]
  339. local maxCnt = tempConfig[2][2]
  340. getCnt[i] = math.random(minCnt,maxCnt)
  341. break
  342. end
  343. nowNum = nowNum - nowWeight
  344. end
  345. break
  346. end
  347. end
  348. randNum = randNum - tempWeight
  349. end
  350. end
  351. return getID, getCnt
  352. end
  353. function zhihuanHero(star, camp,heroId)
  354. local xianzhiConfig = XianzhiExcel.xianzhi[camp]
  355. if not xianzhiConfig then return end
  356. local nextHero = nil
  357. local preStarConfig = xianzhiConfig.preStarFive
  358. if not preStarConfig then return end
  359. local totalWeight = 0
  360. for k, v in ipairs(preStarConfig) do
  361. totalWeight = totalWeight + v[2]
  362. end
  363. local randNum = math.random(1, totalWeight)
  364. for k, v in ipairs(preStarConfig) do
  365. tempWeight = v[2]
  366. if randNum <= tempWeight then
  367. local rate = v[1]
  368. local chipId = { }
  369. local index = 0
  370. for heroID, heroConfig in pairs(HeroExcel.hero) do
  371. if heroID ~= heroId and heroConfig.camp == camp and heroConfig.zhihuanLv == rate then
  372. index = index + 1
  373. chipId[index] = heroID
  374. end
  375. end
  376. if index == 0 then
  377. return
  378. end
  379. local randomNum = math.random(1, index)
  380. nextHero = chipId[randomNum]
  381. break
  382. end
  383. randNum = randNum - tempWeight
  384. end
  385. return nextHero
  386. end
  387. function preview(human, camp, star)
  388. local msgRet = Msg.gc.GC_XIANZHI_ZHIHUAN_PREVIEW
  389. if star < 5 then return end
  390. local xianzhiConfig = XianzhiExcel.xianzhi[camp]
  391. if not xianzhiConfig then return end
  392. local nextHero = nil
  393. local len = 0
  394. for k, v in ipairs (xianzhiConfig.preStarFive) do
  395. if len >= 50 then
  396. break
  397. end
  398. local rate = v[1]
  399. for heroID,heroConfig in pairs(HeroExcel.hero) do
  400. if heroConfig.camp == camp and heroConfig.zhihuanLv == rate then
  401. len = len + 1
  402. HeroGrid.makeHeroSimpleByID(msgRet.heroSimple[len], heroID)
  403. end
  404. end
  405. end
  406. msgRet.heroSimple[0] = len
  407. Msg.send(msgRet, human.fd)
  408. end
  409. function shopQuery(human)
  410. local xianzhiShopCfg = XianzhiExcel.xianzhiShop
  411. local msgRet = Msg.gc.GC_XIANZHI_SHOP_QUERY
  412. --配置错误需要通知策划 或者分段传输
  413. local maxLength = 100
  414. if maxLength < #xianzhiShopCfg then
  415. Broadcast.sendErr(human, Lang.SKIN_CONF_ERR)
  416. return
  417. end
  418. msgRet.currency[0] = 1
  419. local totalCoin = BagLogic.getItemCnt(human,xianzhiOutPutId)
  420. Grid.makeItem(msgRet.currency[1],xianzhiOutPutId,totalCoin)
  421. local length = 0
  422. for id,cfg in pairs(xianzhiShopCfg) do
  423. length = length + 1
  424. --local goods = {}
  425. msgRet.production[length].id = id
  426. msgRet.production[length].price = cfg.price
  427. msgRet.production[length].goods[0] = 1
  428. Grid.makeItem(msgRet.production[length].goods[1],cfg.goods[1],cfg.goods[2])
  429. end
  430. msgRet.production[0] = length
  431. Msg.send(msgRet, human.fd)
  432. end
  433. function shopBuy(human,id,count)
  434. -- 判断道具是否足够
  435. local goodsCfg = XianzhiExcel.xianzhiShop[id]
  436. --
  437. if not goodsCfg then
  438. return Broadcast.sendErr(human, Util.format(Lang.XIANZHI_GOODS_NOT_FOUND,id))
  439. end
  440. local cnt = BagLogic.getItemCnt(human,xianzhiOutPutId)
  441. local needCnt = goodsCfg.price * count
  442. if cnt < needCnt then
  443. return Broadcast.sendErr(human, Util.format(Lang.XIANZHI_NO_ITEM,ItemDefine.getValue(xianzhiOutPutId,"name")))
  444. end
  445. -- 先扣款 再发道具
  446. BagLogic.delItem(human,xianzhiOutPutId,needCnt,"xianzhi_buy")
  447. BagLogic.addItemList(human,{{goodsCfg.goods[1],goodsCfg.goods[2] * count}},"xianzhi_buy")
  448. -- 同步客户端最新先知精华数量
  449. local msgRet = Msg.gc.GC_XIANZHI_SHOP_BUY
  450. msgRet.xianzhiCoin = BagLogic.getItemCnt(human,xianzhiOutPutId)
  451. Msg.send(msgRet, human.fd)
  452. end