AnotherWordTreasure.lua 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. -- 异界寻宝
  2. local Msg = require("core.Msg")
  3. local Lang = require("common.Lang")
  4. local Grid = require("bag.Grid")
  5. local BagLogic = require("bag.BagLogic")
  6. local Broadcast = require("broadcast.Broadcast")
  7. local AnotherWorldBattleConfig = require("excel.anotherWorldBattle")
  8. local TREASURELOG = "anotherWorldBattleTreasure"
  9. local ITEMID_2_TIP = {
  10. [185] = Lang.AB_MIYAO_NOT_ENOUG
  11. }
  12. local function initData(human)
  13. human.db.anotherWordTreasureTimes = 0
  14. end
  15. local function getData(human)
  16. if not human.db.anotherWordTreasureTimes then
  17. initData(human)
  18. end
  19. return human.db.anotherWordTreasureTimes
  20. end
  21. local function updateData(human, lotteryTimes)
  22. human.db.anotherWordTreasureTimes = (human.db.anotherWordTreasureTimes or 0) + lotteryTimes
  23. end
  24. -- 根据当前寻宝次数计算出寻宝等级
  25. local function getTreasureLv(human)
  26. local lotteryTimes = getData(human)
  27. local treasureLv = 0
  28. if not lotteryTimes or lotteryTimes <= 0 then
  29. return treasureLv
  30. end
  31. for _, treasureCfg in ipairs(AnotherWorldBattleConfig.treasureLv) do
  32. if lotteryTimes >= treasureCfg.condLotteryNum then
  33. lotteryTimes = treasureCfg.treasureLv
  34. end
  35. end
  36. return treasureLv
  37. end
  38. -- 获取当前寻宝等级的展示道具
  39. local function getShowItemByLv(targetTreasureLv)
  40. local tb = {}
  41. for _, treasureCfg in pairs(AnotherWorldBattleConfig.treasureAwardPool) do
  42. if targetTreasureLv == treasureCfg.treasureLv and treasureCfg.isShow == 1 then
  43. tb[#tb+1] = treasureCfg.item
  44. end
  45. end
  46. return tb
  47. end
  48. -- 填充协议结构
  49. local function populateMsg(net, itemArr)
  50. net[0] = 0
  51. for i, itemInfo in ipairs(itemArr) do
  52. net[0] = i
  53. Grid.makeItem(net[i], itemInfo[1], itemInfo[2])
  54. end
  55. end
  56. -- 填充协议结构2
  57. local function populateMsg2(net, weightArr)
  58. local len = 0
  59. for _, weightTb in ipairs(weightArr) do
  60. for _, weight in ipairs(weightTb) do
  61. len = len + 1
  62. net[len] = weight
  63. end
  64. end
  65. net[0] = len
  66. end
  67. -- 生成当前等级的抽奖数据
  68. local function genLotteryData(targetTreasureLv)
  69. local totalWeight = 0
  70. local itemPool = {}
  71. for _, treasureCfg in pairs(AnotherWorldBattleConfig.treasureAwardPool) do
  72. if targetTreasureLv == treasureCfg.treasureLv and treasureCfg.weight > 0 then
  73. totalWeight = totalWeight + treasureCfg.weight
  74. itemPool[#itemPool+1] = { treasureCfg.weight, treasureCfg.item[1], treasureCfg.item[2] }
  75. end
  76. end
  77. return totalWeight, itemPool
  78. end
  79. -- 查询
  80. function AB_Treasure_Query(human)
  81. local nowLv = getTreasureLv(human)
  82. local msgRet = Msg.gc.GC_AB_TREASURE_QUERY
  83. msgRet.treasureLv = nowLv
  84. msgRet.lotteryTimes = getData(human)
  85. msgRet.nextLvCondTimes = 0
  86. local maxLv = #AnotherWorldBattleConfig.treasureLv
  87. local nextLv = nowLv + 1
  88. if nextLv >= maxLv then
  89. nextLv = maxLv
  90. end
  91. local nextLvCfg = AnotherWorldBattleConfig.treasureLv[nextLv]
  92. msgRet.nextLvCondTimes = nextLvCfg.condLotteryNum
  93. local awardPoolMsg = msgRet.awardPool
  94. local showItemArr = getShowItemByLv(nowLv)
  95. awardPoolMsg[0] = #showItemArr
  96. for i, itemInfo in ipairs(showItemArr) do
  97. Grid.makeItem(awardPoolMsg[i], itemInfo[1], itemInfo[2])
  98. end
  99. local costCfg = AnotherWorldBattleConfig.var[1]
  100. Grid.makeItem(msgRet.lottery1Cost, costCfg.lottery1Cost[1][1], costCfg.lottery1Cost[1][2])
  101. Grid.makeItem(msgRet.lottery10Cost, costCfg.lottery10Cost[1][1], costCfg.lottery10Cost[1][2])
  102. populateMsg(msgRet.lottery2Cost, costCfg.lottery2Cost)
  103. populateMsg(msgRet.lottery20Cost, costCfg.lottery20Cost)
  104. populateMsg2(msgRet.nextLvProArr, nextLvCfg.probabilityArr)
  105. Msg.send(msgRet, human.fd)
  106. end
  107. -- 抽奖
  108. function AB_Treasure_Lottery(human, lotteryNum)
  109. local costItem
  110. local costCfg = AnotherWorldBattleConfig.var[1]
  111. if lotteryNum == 1 then
  112. costItem = costCfg.lottery1Cost
  113. elseif lotteryNum == 2 then
  114. costItem = costCfg.lottery2Cost
  115. elseif lotteryNum == 10 then
  116. costItem = costCfg.lottery10Cost
  117. elseif lotteryNum == 20 then
  118. costItem = costCfg.lottery20Cost
  119. end
  120. if not costItem then
  121. return Broadcast.sendErr(human, Lang.COMMON_ARGUMENT_ERROR)
  122. end
  123. -- 道具判断
  124. for _, itemTb in ipairs(costItem) do
  125. local itemId = itemTb[1]
  126. local itemNum = itemTb[2]
  127. if BagLogic.getItemCnt(human, itemId) < itemNum then
  128. local tips = ITEMID_2_TIP[itemId] or Lang.COMMON_NO_ZUANSHI
  129. return Broadcast.sendErr(human, tips)
  130. end
  131. BagLogic.delItem(human, itemId, itemNum, TREASURELOG)
  132. end
  133. -- 开始抽奖
  134. local weight = 0
  135. local itemArr = {}
  136. local nowLv = getTreasureLv(human)
  137. local totalWeight, itemPool = genLotteryData(nowLv)
  138. for i=1, lotteryNum do
  139. weight = 0
  140. local randWeight = math.random(0, totalWeight)
  141. for _, v in ipairs(itemPool) do
  142. weight = weight + v[1]
  143. if randWeight <= weight then
  144. itemArr[#itemArr+1] = {v[2], v[3]}
  145. break
  146. end
  147. end
  148. end
  149. -- 更新抽奖次数
  150. updateData(human, lotteryNum)
  151. -- 发抽奖所得奖励
  152. BagLogic.addItemList(human, itemArr, TREASURELOG)
  153. -- 推送最新数据到客户端
  154. local newLv = getTreasureLv(human)
  155. if newLv == nowLv then
  156. local msgRet = Msg.gc.GC_AB_TREASURE_LOTTERY
  157. msgRet.lotteryTimes = getData(human)
  158. return Msg.send(msgRet, human.fd)
  159. end
  160. AB_Treasure_Query(human)
  161. end