ActCycleRechargeLogic.lua 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. -- 循环累计充值活动, 开服第8天开始, 每7天一轮, 活动循环开
  2. -- 为保证合服后玩家不受影响, 将活动时间, 充值等数据绑在玩家身上
  3. --db
  4. --[[
  5. human.db.cycleRechargeData =
  6. {
  7. startTime = nil, 本轮开启时间
  8. totalRecharge = 0, 本轮累计充值
  9. receiveList = {}, 已经领取列表
  10. }
  11. ]]--
  12. local Msg = require("core.Msg")
  13. local Grid = require("bag.Grid")
  14. local BagLogic = require("bag.BagLogic")
  15. local Lang = require("common.Lang")
  16. local Broadcast = require("broadcast.Broadcast")
  17. local CommonDB = require("common.CommonDB")
  18. local Util = require("common.Util")
  19. local cycleRechargeCfg = require("excel.cycleRecharge").cycleRecharge
  20. local MailManager = require("mail.MailManager")
  21. local MailExcel = require("excel.mail")
  22. local YunYingLogic = require("yunying.YunYingLogic")
  23. -- 开服第8天开始
  24. local OPEN_DAYS = 8
  25. -- 每轮活动持续天数
  26. local DURATION_DAYS = 7
  27. local DAY_SECS = 86400
  28. -- 邮件ID
  29. local MAILID = 7019
  30. -- 日志标识
  31. local LOGTAG = "ActCycleRechargeLogic"
  32. --活动ID
  33. local ACTID = 3314
  34. local function initActData(human)
  35. human.db.cycleRechargeData = {}
  36. end
  37. local function getActData(human)
  38. return human.db.cycleRechargeData
  39. end
  40. local function updateActStartTime(human, ti)
  41. local cycleRechargeData = getActData(human)
  42. cycleRechargeData.startTime = ti
  43. end
  44. local function updateActReceiveList(human, newReceiveList)
  45. local cycleRechargeData = getActData(human)
  46. cycleRechargeData.receiveList = cycleRechargeData.receiveList or {}
  47. for k in pairs(newReceiveList) do
  48. cycleRechargeData.receiveList[k] = '1'
  49. end
  50. end
  51. local function updateActTotalRecharge(human, money)
  52. local cycleRechargeData = getActData(human)
  53. cycleRechargeData.totalRecharge = (cycleRechargeData.totalRecharge or 0) + money
  54. end
  55. local function isRecharge(human)
  56. return (human.db.topupAcount and human.db.topupAcount > 0) and true or false
  57. end
  58. -- 开服时间
  59. local function getOpenServerTime()
  60. return CommonDB.getServerOpenTime()
  61. end
  62. -- 计算角色的活动首次开启时间
  63. local function calcRoleFirstOpenTime()
  64. local openServerTime = getOpenServerTime()
  65. local dayStartTime = Util.getDayStartTime(os.time())
  66. local actFirstOpenTime = Util.getDayStartTime(openServerTime + (OPEN_DAYS - 1) * DAY_SECS)
  67. if dayStartTime < actFirstOpenTime then
  68. return
  69. end
  70. -- -- 当天与活动首次开启时间的天数差
  71. -- local days_passed = math.floor((dayStartTime - actFirstOpenTime) / DAY_SECS)
  72. -- -- 当前属于第几轮
  73. -- local crurrent_round = math.floor(days_passed / DURATION_DAYS) + 1
  74. -- -- 本轮的开启时间
  75. -- local crurrent_round_openTime = actFirstOpenTime + (crurrent_round - 1) * DURATION_DAYS * DAY_SECS
  76. -- return crurrent_round_openTime
  77. return dayStartTime
  78. end
  79. -- 获取可领档位的索引
  80. local function getAwardIdx(human)
  81. local cycleRechargeData = getActData(human)
  82. local totalRecharge = cycleRechargeData.totalRecharge
  83. if not totalRecharge or totalRecharge <= 0 then
  84. return
  85. end
  86. local idxList = {}
  87. local receiveList = cycleRechargeData.receiveList or {}
  88. for k,v in ipairs(cycleRechargeCfg) do
  89. if totalRecharge >= v.moneyCond and not receiveList[k] then
  90. idxList[k] = 1
  91. end
  92. end
  93. if not next(idxList) then
  94. return
  95. end
  96. return idxList
  97. end
  98. -- 可领取的奖励
  99. local function getAwardList(human)
  100. local idxList = getAwardIdx(human)
  101. if not idxList then
  102. return
  103. end
  104. local len = 0
  105. local awardList, receiveList = {}, {}
  106. for idx in pairs(idxList) do
  107. local singleCfg = cycleRechargeCfg[idx]
  108. for _,item in ipairs(singleCfg.awardList) do
  109. len = len + 1
  110. awardList[len] = item
  111. receiveList[idx] = 1
  112. end
  113. end
  114. return awardList, receiveList
  115. end
  116. -- 如果还有奖励未领取,通过邮件发送
  117. local function sendAwardByEmail(human)
  118. local awardList, receiveList = getAwardList(human)
  119. if awardList and #awardList > 0 then
  120. updateActReceiveList(human, receiveList)
  121. local mailCfg = MailExcel.mail[MAILID]
  122. MailManager.add(MailManager.SYSTEM, human.db._id, mailCfg.title, mailCfg.content, awardList, mailCfg.senderName)
  123. end
  124. end
  125. -- 检查红点
  126. local function redDotCheck(human, noCheckRed)
  127. -- YunYingLogic.updateIcon(YYInfo[ACTID], human, noCheckRed)
  128. YunYingLogic.sendGroupUpdate(YYInfo[ACTID], human, ACTID)
  129. end
  130. -- 跨天
  131. function updateDaily(human)
  132. local cycleRechargeData = getActData(human)
  133. if not cycleRechargeData then
  134. local firstOpenTime = calcRoleFirstOpenTime()
  135. if not firstOpenTime then
  136. return
  137. end
  138. initActData(human)
  139. updateActStartTime(human, firstOpenTime)
  140. -- redDotCheck(human, true)
  141. if isRecharge(human) then
  142. -- 推送数据,更新界面
  143. Query(human)
  144. end
  145. else
  146. local now = os.time()
  147. if now >= (cycleRechargeData.startTime + DURATION_DAYS * DAY_SECS) then
  148. --检查是否有未领取的奖励
  149. sendAwardByEmail(human)
  150. -- 重置数据
  151. initActData(human)
  152. -- 更新新一轮开启时间
  153. local dayStartTime = Util.getDayStartTime(now)
  154. updateActStartTime(human, dayStartTime)
  155. if isRecharge(human) then
  156. -- 推送数据,更新界面
  157. Query(human)
  158. -- 红点更新
  159. redDotCheck(human, true)
  160. end
  161. end
  162. end
  163. end
  164. function isOpen(human)
  165. local bl = isRecharge(human)
  166. local cycleRechargeData = getActData(human)
  167. if not cycleRechargeData or not bl then
  168. return false
  169. end
  170. return true
  171. end
  172. function isRed(human, YYInfo, funcConfig)
  173. if getAwardIdx(human) then
  174. return true
  175. end
  176. return false
  177. end
  178. --充值
  179. function onCharge(human, price, funcID, buyID)
  180. local cycleRechargeData = getActData(human)
  181. if not cycleRechargeData then
  182. return
  183. end
  184. updateActTotalRecharge(human, price)
  185. redDotCheck(human)
  186. end
  187. function Query(human)
  188. local bl = isRecharge(human)
  189. local cycleRechargeData = getActData(human)
  190. if not cycleRechargeData or not bl then
  191. return
  192. end
  193. local startTag = 1
  194. local len = 0
  195. local msgMaxLen = 5
  196. local cfgCnt = #cycleRechargeCfg
  197. local msgRet = Msg.gc.GC_CYCLERECHARGE_QUERY
  198. msgRet.startTime = cycleRechargeData.startTime
  199. msgRet.endTime = cycleRechargeData.startTime + DURATION_DAYS * DAY_SECS
  200. msgRet.totalRecharge = cycleRechargeData.totalRecharge or 0
  201. local msgAwardList = msgRet.awardList
  202. local receiveList = cycleRechargeData.receiveList or {}
  203. for k, v in ipairs(cycleRechargeCfg) do
  204. len = len + 1
  205. msgAwardList[0] = len
  206. msgAwardList[len].idx = k
  207. msgAwardList[len].moneyCond = v.moneyCond
  208. msgAwardList[len].state = 0
  209. if receiveList[k] then
  210. msgAwardList[len].state = 2
  211. else
  212. if cycleRechargeData.totalRecharge and cycleRechargeData.totalRecharge >= v.moneyCond then
  213. msgAwardList[len].state = 1
  214. end
  215. end
  216. for i, itemInfo in ipairs(v.awardList) do
  217. msgAwardList[len].itemList[0] = i
  218. Grid.makeItem( msgAwardList[len].itemList[i], itemInfo[1], itemInfo[2])
  219. end
  220. if len >= msgMaxLen then
  221. msgRet.startTag = startTag
  222. msgRet.endTag = 0
  223. if cfgCnt - len <= 0 then
  224. msgRet.endTag = 1
  225. return Msg.send(msgRet, human.fd)
  226. end
  227. Msg.send(msgRet, human.fd)
  228. len = 0
  229. startTag = 0
  230. cfgCnt = cfgCnt - msgMaxLen
  231. end
  232. end
  233. msgRet.startTag = startTag
  234. msgRet.endTag = 1
  235. Msg.send(msgRet, human.fd)
  236. end
  237. function GetAward(human)
  238. local cycleRechargeData = getActData(human)
  239. if not cycleRechargeData then
  240. return Broadcast.sendErr(human, Lang.COMMOM_NOT_ENABLED)
  241. end
  242. if not cycleRechargeData.totalRecharge or cycleRechargeData.totalRecharge <= 0 then
  243. return Broadcast.sendErr(human, Lang.COMMON_RECHARGE_NOT_ENOUGH)
  244. end
  245. local awardList, receiveList = getAwardList(human)
  246. if not awardList or not next(awardList) then
  247. return Broadcast.sendErr(human, Lang.SHARE_GROUP_GET_ERR_CNT)
  248. end
  249. updateActReceiveList(human, receiveList)
  250. BagLogic.addItemList(human, awardList, LOGTAG)
  251. Query(human)
  252. redDotCheck(human, true)
  253. end