LotteryByDiamondLogic.lua 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 LOTTERYACTID = 31005 -- 古玉充能活动ID
  23. local lotteryCostID = 0 --普通奖池抽奖消耗道具ID
  24. local lottery1CostCnt = 0 --普通奖池单抽消耗道具数量
  25. local lottery10CostCnt = 0 --普通奖池十连消耗道具数量
  26. local comTotalWeight = 0 --普通奖池道具总权重
  27. local speTotalWeight = 0 --稀有奖池道具总权重
  28. local function updateCondValue()
  29. --没热更,所以缓存
  30. local comCfg = LotteryCfg[1]
  31. local speCfg = LotteryCfg[2]
  32. lotteryCostID = comCfg.costItemId
  33. lottery1CostCnt = comCfg.lottery1CostCnt
  34. lottery10CostCnt = comCfg.lottery10CostCnt
  35. for _, v in ipairs(comCfg.prizeData) do
  36. comTotalWeight = comTotalWeight + v[3]
  37. end
  38. for _, v in ipairs(speCfg.prizeData) do
  39. speTotalWeight = speTotalWeight + v[3]
  40. end
  41. end
  42. --查询
  43. function Query(human, id)
  44. local state = AbsActLogic.isStarted(human, id)
  45. if not state then
  46. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  47. end
  48. local actData = human.db.absAct[id]
  49. if not actData then
  50. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  51. end
  52. local msgRet = Msg.gc.GC_LOTTERYBYDIAMONDS_QUERY
  53. if comTotalWeight <= 0 then
  54. updateCondValue()
  55. end
  56. --普通奖池
  57. local nLotteryCostCnt = lottery1CostCnt
  58. if not actData.lastFreeTime or not Util.isSameDay(actData.lastFreeTime) then
  59. nLotteryCostCnt = 0
  60. end
  61. local comLotteryData = msgRet.comLotteryData
  62. Grid.makeItem(comLotteryData.lottery1cost, lotteryCostID, nLotteryCostCnt)
  63. Grid.makeItem(comLotteryData.lottery10cost, lotteryCostID, lottery10CostCnt)
  64. comLotteryData.points = actData.points or 0
  65. comLotteryData.maxPoints = MAXPOINTS
  66. local comPrizeCfg = LotteryCfg[1].prizeData
  67. comLotteryData.prizePoolData[0] = #comPrizeCfg
  68. for i = 1, #comPrizeCfg do
  69. local itemData = comPrizeCfg[i]
  70. Grid.makeItem(comLotteryData.prizePoolData[i], itemData[1], itemData[2])
  71. end
  72. --稀有奖池
  73. local speLotteryData = msgRet.speLotteryData
  74. speLotteryData.lotteryTimes = actData.speLotteryTimes or 0
  75. local spePrizeCfg = LotteryCfg[2].prizeData
  76. speLotteryData.prizePoolData[0] = #spePrizeCfg
  77. for i = 1, #spePrizeCfg do
  78. local itemData = spePrizeCfg[i]
  79. Grid.makeItem(speLotteryData.prizePoolData[i], itemData[1], itemData[2])
  80. end
  81. Msg.send(msgRet, human.fd)
  82. end
  83. --抽奖
  84. function Lottery(human, id, type)
  85. if not id or not type then
  86. return Broadcast.sendErr(human, Lang.SKIN_PARAM_ERR)
  87. end
  88. if type < 1 or type > 3 then
  89. return Broadcast.sendErr(human, Lang.SKIN_PARAM_ERR)
  90. end
  91. local state = AbsActLogic.isStarted(human, id)
  92. if not state then
  93. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  94. end
  95. local actData = human.db.absAct[id]
  96. if not actData then
  97. return Broadcast.sendErr(human, Lang.YUNYING_ERR_TIME)
  98. end
  99. if comTotalWeight <= 0 then
  100. updateCondValue()
  101. end
  102. local itemCnt, nLotteryTimes = 0, 1
  103. local targetTotalWeight = comTotalWeight
  104. local targetPrizeCfg = LotteryCfg[1].prizeData
  105. if type == 1 then
  106. if not actData.lastFreeTime or not Util.isSameDay(actData.lastFreeTime) then
  107. actData.lastFreeTime = os.time()
  108. else
  109. itemCnt = lottery1CostCnt
  110. end
  111. elseif type == 2 then
  112. itemCnt = lottery10CostCnt
  113. nLotteryTimes = 10
  114. else
  115. if not actData.speLotteryTimes or actData.speLotteryTimes <= 0 then
  116. return
  117. end
  118. actData.speLotteryTimes = actData.speLotteryTimes - 1
  119. targetPrizeCfg = LotteryCfg[2].prizeData
  120. targetTotalWeight = speTotalWeight
  121. end
  122. if itemCnt > 0 then
  123. if BagLogic.getItemCnt(human, lotteryCostID) < itemCnt then
  124. return Broadcast.sendErr(human, Lang.COMMON_NO_ZUANSHI)
  125. end
  126. BagLogic.delItem(human, lotteryCostID, itemCnt, LOTTERYLOG)
  127. end
  128. --抽奖
  129. local awardList = {}
  130. for i=1, nLotteryTimes do
  131. local weight = 0
  132. local randWeight = math.random(0, targetTotalWeight)
  133. for _,v in ipairs(targetPrizeCfg) do
  134. weight = weight + v[3]
  135. if randWeight <= weight then
  136. awardList[#awardList+1] = {v[1], v[2]}
  137. if type < 3 then
  138. actData.points = (actData.points or 0) + math.random(RANDMIN, RANDMAX)
  139. end
  140. break
  141. end
  142. end
  143. end
  144. BagLogic.addItemList(human, awardList, LOTTERYLOG)
  145. --更新数据
  146. local points = actData.points or 0
  147. if points >= MAXPOINTS then
  148. actData.speLotteryTimes = (actData.speLotteryTimes or 0) + math.floor(points / MAXPOINTS)
  149. actData.points = points % MAXPOINTS
  150. end
  151. --下发数据
  152. local msgRet = Msg.gc.GC_LOTTERYBYDIAMONDS_LOTTERY
  153. msgRet.points = actData.points
  154. msgRet.lotteryTimes = actData.speLotteryTimes or 0
  155. local nLotteryCostCnt = lottery1CostCnt
  156. if not actData.lastFreeTime or not Util.isSameDay(actData.lastFreeTime) then
  157. nLotteryCostCnt = 0
  158. end
  159. Grid.makeItem(msgRet.lottery1cost, lotteryCostID, nLotteryCostCnt)
  160. Msg.send(msgRet, human.fd)
  161. end
  162. -- 是否有红点
  163. function isRed(human, YYInfo, funcConfig)
  164. local tActData = human.db.absAct[LOTTERYACTID]
  165. if not tActData then
  166. return false
  167. end
  168. if not tActData.lastFreeTime or not Util.isSameDay(tActData.lastFreeTime) then
  169. return true
  170. end
  171. return false
  172. end