AnotherWordTreasure.lua 6.0 KB

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