LotteryByDiamondLogic.lua 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. --古玉充能活动
  2. --db
  3. --[=[
  4. human.db.absAct[id] = {
  5. points = nil, --普通抽奖累积的积分
  6. speLotteryTimes = nil, --稀有抽奖次数
  7. lastFreeTime = nil, --上一次免费抽奖时间,如果为nil或者与当前时间不是同一天则可以免费单抽一次
  8. }
  9. ]=]--
  10. local Msg = require("core.Msg")
  11. local Grid = require("bag.Grid")
  12. local Util = require("common.Util")
  13. local BagLogic = require("bag.BagLogic")
  14. local AbsActLogic = require("absAct.AbsActLogic")
  15. local Broadcast = require("broadcast.Broadcast")
  16. local Lang = require("common.Lang")
  17. local LotteryCfg = require("excel.absAct").lotteryByDiamonds
  18. local LOTTERYLOG = "lotteryByDiamonds" --日志标识
  19. local MAXPOINTS = 300 --每三百积分可以获得一次稀有抽奖次数
  20. local RANDMIN = 5 --普通奖池每次抽奖获得积分随机值最小值
  21. local RANDMAX = 15 --普通奖池每次抽奖获得积分随机值最大值
  22. local lotteryCostID = 0 --普通奖池抽奖消耗道具ID
  23. local lottery1CostCnt = 0 --普通奖池单抽消耗道具数量
  24. local lottery10CostCnt = 0 --普通奖池十连消耗道具数量
  25. local comTotalWeight = 0 --普通奖池道具总权重
  26. local speTotalWeight = 0 --稀有奖池道具总权重
  27. local function updateCondValue()
  28. --没热更,所以缓存
  29. local comCfg = LotteryCfg[1]
  30. local speCfg = LotteryCfg[2]
  31. lotteryCostID = comCfg.costItemId
  32. lottery1CostCnt = comCfg.lottery1CostCnt
  33. lottery10CostCnt = comCfg.lottery10CostCnt
  34. for _, v in ipairs(comCfg.prizeData) do
  35. comTotalWeight = comTotalWeight + v[3]
  36. end
  37. for _, v in ipairs(speCfg.prizeData) do
  38. speTotalWeight = speTotalWeight + v[3]
  39. end
  40. end
  41. --查询
  42. function Query(human, id)
  43. local state = AbsActLogic.isStarted(human, id)
  44. if not state then
  45. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  46. end
  47. local actData = human.db.absAct[id]
  48. if not actData then
  49. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  50. end
  51. local msgRet = Msg.gc.GC_LOTTERYBYDIAMONDS_QUERY
  52. if comTotalWeight <= 0 then
  53. updateCondValue()
  54. end
  55. --普通奖池
  56. local nLotteryCostCnt = lottery1CostCnt
  57. if not actData.lastFreeTime or not Util.isSameDay(actData.lastFreeTime) then
  58. nLotteryCostCnt = 0
  59. end
  60. local comLotteryData = msgRet.comLotteryData
  61. Grid.makeItem(comLotteryData.lottery1cost, lotteryCostID, nLotteryCostCnt)
  62. Grid.makeItem(comLotteryData.lottery10cost, lotteryCostID, lottery10CostCnt)
  63. comLotteryData.points = actData.points or 0
  64. comLotteryData.maxPoints = MAXPOINTS
  65. local comPrizeCfg = LotteryCfg[1].prizeData
  66. comLotteryData.prizePoolData[0] = #comPrizeCfg
  67. for i = 1, #comPrizeCfg do
  68. local itemData = comPrizeCfg[i]
  69. Grid.makeItem(comLotteryData.prizePoolData[i], itemData[1], itemData[2])
  70. end
  71. --稀有奖池
  72. local speLotteryData = msgRet.speLotteryData
  73. speLotteryData.lotteryTimes = actData.speLotteryTimes or 0
  74. local spePrizeCfg = LotteryCfg[2].prizeData
  75. speLotteryData.prizePoolData[0] = #spePrizeCfg
  76. for i = 1, #spePrizeCfg do
  77. local itemData = spePrizeCfg[i]
  78. Grid.makeItem(speLotteryData.prizePoolData[i], itemData[1], itemData[2])
  79. end
  80. Msg.send(msgRet, human.fd)
  81. end
  82. --抽奖
  83. function Lottery(human, id, type)
  84. if not id or not type then
  85. return Broadcast.sendErr(human, Lang.SKIN_PARAM_ERR)
  86. end
  87. if type < 1 or type > 3 then
  88. return Broadcast.sendErr(human, Lang.SKIN_PARAM_ERR)
  89. end
  90. local state = AbsActLogic.isStarted(human, id)
  91. if not state then
  92. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  93. end
  94. local actData = human.db.absAct[id]
  95. if not actData then
  96. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  97. end
  98. if comTotalWeight <= 0 then
  99. updateCondValue()
  100. end
  101. local itemCnt, nLotteryTimes = 0, 1
  102. local targetTotalWeight = comTotalWeight
  103. local targetPrizeCfg = LotteryCfg[1].prizeData
  104. if type == 1 then
  105. if not actData.lastFreeTime or not Util.isSameDay(actData.lastFreeTime) then
  106. actData.lastFreeTime = os.time()
  107. else
  108. itemCnt = lottery1CostCnt
  109. end
  110. elseif type == 2 then
  111. itemCnt = lottery10CostCnt
  112. nLotteryTimes = 10
  113. else
  114. if not actData.speLotteryTimes or actData.speLotteryTimes <= 0 then
  115. return
  116. end
  117. actData.speLotteryTimes = actData.speLotteryTimes - 1
  118. targetPrizeCfg = LotteryCfg[2].prizeData
  119. targetTotalWeight = speTotalWeight
  120. end
  121. if itemCnt > 0 then
  122. if BagLogic.getItemCnt(human, lotteryCostID) < itemCnt then
  123. return Broadcast.sendErr(human, Lang.COMMON_NO_ZUANSHI)
  124. end
  125. BagLogic.delItem(human, lotteryCostID, itemCnt, LOTTERYLOG)
  126. end
  127. --抽奖
  128. local awardList = {}
  129. for i=1, nLotteryTimes do
  130. local weight = 0
  131. local randWeight = math.random(0, targetTotalWeight)
  132. for _,v in ipairs(targetPrizeCfg) do
  133. weight = weight + v[3]
  134. if randWeight <= weight then
  135. awardList[#awardList+1] = {v[1], v[2]}
  136. if type < 3 then
  137. actData.points = (actData.points or 0) + math.random(RANDMIN, RANDMAX)
  138. end
  139. break
  140. end
  141. end
  142. end
  143. BagLogic.addItemList(human, awardList, LOTTERYLOG)
  144. --更新数据
  145. local points = actData.points or 0
  146. if points >= MAXPOINTS then
  147. actData.speLotteryTimes = (actData.speLotteryTimes or 0) + math.floor(points / MAXPOINTS)
  148. actData.points = points % MAXPOINTS
  149. end
  150. --下发数据
  151. local msgRet = Msg.gc.GC_LOTTERYBYDIAMONDS_LOTTERY
  152. msgRet.points = actData.points
  153. msgRet.lotteryTimes = actData.speLotteryTimes or 0
  154. local nLotteryCostCnt = lottery1CostCnt
  155. if not actData.lastFreeTime or not Util.isSameDay(actData.lastFreeTime) then
  156. nLotteryCostCnt = 0
  157. end
  158. Grid.makeItem(msgRet.lottery1cost, lotteryCostID, nLotteryCostCnt)
  159. Msg.send(msgRet, human.fd)
  160. end