DailyLotteryDraw.lua 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. -- 每日抽奖
  2. DailyLotteryDraw = {}
  3. local this = {}
  4. DAILY_LOTTERY_DRAW = "T$dailyLotteryDraw"
  5. ---@class DailyLotteryDraw.Info 每日抽奖信息
  6. ---@field currentCount number 当前已经抽奖次数
  7. ---@field lastResetTime number 上一次重置事件
  8. ---@field phone number 手机号
  9. ---@field dayCount number 当天剩余抽奖次数
  10. -- 抽奖
  11. function DailyLotteryDraw.luckDraw(actor)
  12. -- 校验是否绑定了手机号
  13. local dailyLotteryDraw = getplaydef(actor, DAILY_LOTTERY_DRAW)
  14. if table.isEmpty(dailyLotteryDraw) then
  15. -- 请先绑定手机号
  16. noticeTip.noticeinfo(actor, StringIdConst.TEXT467)
  17. -- tipinfo(actor,"请先绑定手机号")
  18. return
  19. end
  20. local dayCount = dailyLotteryDraw.dayCount
  21. if dayCount <= 0 then
  22. -- 当天抽奖次数已经用完,请明天再来
  23. noticeTip.noticeinfo(actor, StringIdConst.TEXT471)
  24. -- tipinfo(actor,"当天抽奖次数已经用完,请明天再来")
  25. return
  26. end
  27. local currentCount = dailyLotteryDraw.currentCount
  28. local value = ConfigDataManager.getTableValue("cfg_global","value","id",19001)
  29. local valueTable = string.split(value,"#")
  30. if tonumber(valueTable[1]) <= currentCount then
  31. -- 全部的抽奖次数已经用完
  32. noticeTip.noticeinfo(actor, StringIdConst.TEXT469)
  33. -- tipinfo(actor,"全部的抽奖次数已经用完")
  34. return
  35. end
  36. local dailyPrizes = ConfigDataManager.getList("cfg_daily_prizes")
  37. if table.isEmpty(dailyPrizes) then
  38. error("每日抽奖配置错误")
  39. return
  40. end
  41. local totalChance = 0
  42. for index, dailyPrize in pairs(dailyPrizes) do
  43. totalChance = totalChance + dailyPrize.chance
  44. end
  45. local rate = math.random(1, totalChance)
  46. local rateChance = 0
  47. local reward = ""
  48. local rewardId = ""
  49. for index, dailyPrize in pairs(dailyPrizes) do
  50. local upChance = rateChance + dailyPrize.chance
  51. if rateChance < rate and rate <= upChance then
  52. -- 发送奖励信息
  53. reward = dailyPrize.reward
  54. rewardId = dailyPrize.id
  55. end
  56. rateChance = upChance
  57. end
  58. sendluamsg(actor, LuaMessageIdToClient.RES_LUCK_DRAW, rewardId)
  59. dailyLotteryDraw.dayCount = dailyLotteryDraw.dayCount - 1
  60. dailyLotteryDraw.currentCount = dailyLotteryDraw.currentCount + 1
  61. setplaydef(actor, DAILY_LOTTERY_DRAW,dailyLotteryDraw)
  62. intervalcalldelay(actor, 7000, 1000, 1,"senddailylotterydrawreward",reward)
  63. end
  64. -- 发送奖励
  65. function senddailylotterydrawreward(actor,reward)
  66. if not reward or reward == "" then
  67. return
  68. end
  69. local rewardInfo = string.split(reward,"#")
  70. additemtobag(actor,rewardInfo[1],rewardInfo[2],0,9999,'每日抽奖')
  71. end
  72. function DailyLotteryDraw.sendInfo(actor)
  73. local dailyLotteryDraw = getplaydef(actor, DAILY_LOTTERY_DRAW)
  74. sendluamsg(actor, LuaMessageIdToClient.RES_LUCK_DRAW_INFO, dailyLotteryDraw)
  75. end
  76. function this.isValidPhoneNumber(phone)
  77. -- 模式解释:
  78. -- ^1:手机号必须以1开头
  79. -- [3-9]:第二位可以是3到9中的任何一个数字
  80. -- %d{9}:后面必须跟着9个数字
  81. -- $:结束符,表示字符串结束
  82. local pattern = "^1[3-9]%d%d%d%d%d%d%d%d%d$"
  83. return string.match(phone, pattern) ~= nil
  84. end
  85. -- 绑定手机号
  86. function DailyLotteryDraw.bindPhone(actor,msgData)
  87. local dailyLotteryDraw = getplaydef(actor, DAILY_LOTTERY_DRAW)
  88. if not table.isEmpty(dailyLotteryDraw) then
  89. -- 请先输入手机号
  90. noticeTip.noticeinfo(actor, StringIdConst.TEXT468)
  91. -- tipinfo(actor,"已经填写过问卷,无需重复填写")
  92. return
  93. end
  94. local phone = msgData.phone
  95. if not this.isValidPhoneNumber(phone) then
  96. noticeTip.noticeinfo(actor, StringIdConst.TEXT467)
  97. -- tipinfo(actor,"请输入正确的手机号")
  98. return
  99. end
  100. local value = ConfigDataManager.getTableValue("cfg_global","value","id",19001)
  101. local valueTable = string.split(value,"#")
  102. msgData.currentCount = 0
  103. msgData.lastResetTime = getbaseinfo(actor,"nowsec")
  104. msgData.dayCount = tonumber(valueTable[2])
  105. setplaydef(actor, DAILY_LOTTERY_DRAW,msgData)
  106. sendluamsg(actor, LuaMessageIdToClient.RES_BIND_PHONE, true)
  107. end
  108. -- 定时刷新抽奖信息
  109. function DailyLotteryDraw.flushDailyLotteryDraw(actor)
  110. local nowSec = getbaseinfo(actor,"nowsec")
  111. local time = TimeUtil.timeToDate(nowSec)
  112. if time.hour ~= 9 then
  113. return
  114. end
  115. local dailyLotteryDraw = getplaydef(actor, DAILY_LOTTERY_DRAW)
  116. if table.isEmpty(dailyLotteryDraw) then
  117. return
  118. end
  119. local value = ConfigDataManager.getTableValue("cfg_global","value","id",19001)
  120. local valueTable = string.split(value,"#")
  121. if dailyLotteryDraw.currentCount == tonumber(valueTable[1]) then
  122. return
  123. end
  124. dailyLotteryDraw.lastResetTime = getbaseinfo(actor,"nowsec")
  125. dailyLotteryDraw.dayCount = tonumber(valueTable[2])
  126. setplaydef(actor, DAILY_LOTTERY_DRAW,dailyLotteryDraw)
  127. end
  128. -- 登录时检查是否刷新数据
  129. function DailyLotteryDraw.login(actor)
  130. local dailyLotteryDraw = getplaydef(actor, DAILY_LOTTERY_DRAW)
  131. if table.isEmpty(dailyLotteryDraw) then
  132. return
  133. end
  134. local lastResetTime = dailyLotteryDraw.lastResetTime
  135. local nowSec = getbaseinfo(actor,"nowsec")
  136. local value = ConfigDataManager.getTableValue("cfg_global","value","id",19001)
  137. local valueTable = string.split(value,"#")
  138. local flush = TimeUtil.dayFlush(nowSec,lastResetTime,tonumber(valueTable[3]))
  139. if flush and dailyLotteryDraw.currentCount ~= tonumber(valueTable[1]) then
  140. dailyLotteryDraw.lastResetTime = nowSec
  141. dailyLotteryDraw.dayCount = tonumber(valueTable[2])
  142. setplaydef(actor, DAILY_LOTTERY_DRAW,dailyLotteryDraw)
  143. return
  144. end
  145. end
  146. function testgenerateverificationcode(actor)
  147. DailyLotteryDraw.generateVerificationCode(actor,111111111)
  148. end
  149. -- 生成验证码
  150. function DailyLotteryDraw.generateVerificationCode(actor,phone)
  151. if not this.isValidPhoneNumber(phone) then
  152. tipinfo(actor,"请输入正确的手机号")
  153. return
  154. end
  155. local dailyLotteryDraw = getplaydef(actor, DAILY_LOTTERY_DRAW)
  156. if not table.isEmpty(dailyLotteryDraw) and dailyLotteryDraw.bind then
  157. -- 已经绑定过手机号
  158. tipinfo(actor,"已经绑定过手机号")
  159. return
  160. end
  161. -- 没有申请过验证码
  162. if table.isEmpty(dailyLotteryDraw) then
  163. dailyLotteryDraw = {
  164. phone = phone,
  165. verificationCode = this.generateCaptcha(4),
  166. bind = false,
  167. verificationCodeTime = getbaseinfo(actor,"nowsec")
  168. }
  169. setplaydef(actor, DAILY_LOTTERY_DRAW,dailyLotteryDraw)
  170. sendluamsg(actor, LuaMessageIdToClient.RES_GENERATE_VERIFICATION_CODE, dailyLotteryDraw)
  171. jprint(dailyLotteryDraw)
  172. return
  173. end
  174. jprint(dailyLotteryDraw)
  175. local verificationCodeTime = dailyLotteryDraw.verificationCodeTime
  176. local nowSec = getbaseinfo(actor,"nowsec")
  177. local diffTime = nowSec - verificationCodeTime
  178. if diffTime < 60 then
  179. tipinfo(actor,"验证码还在有效期内,请输入")
  180. return
  181. end
  182. dailyLotteryDraw.verificationCode = this.generateCaptcha(4)
  183. dailyLotteryDraw.verificationCodeTime = nowSec
  184. setplaydef(actor, DAILY_LOTTERY_DRAW,dailyLotteryDraw)
  185. sendluamsg(actor, LuaMessageIdToClient.RES_GENERATE_VERIFICATION_CODE, dailyLotteryDraw)
  186. jprint(dailyLotteryDraw)
  187. -- 发送验证码
  188. end
  189. -- 生成验证码
  190. function this.generateCaptcha(length)
  191. local captcha = ""
  192. local characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" -- 可选字符集
  193. for i = 1, length do
  194. local randomIndex = math.random(1, #characters)
  195. local randomChar = characters:sub(randomIndex, randomIndex)
  196. captcha = captcha .. randomChar
  197. end
  198. return captcha
  199. end