WeekendFuli.lua 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. --------------------------------------------------
  2. -- 周末福利
  3. -- 周六、周日、周一开启 每天可领礼包
  4. -- db.weekendFuli.ts 开始时间戳
  5. -- db.weekendFuli.list 领取标记 [id]=是否领取
  6. --------------------------------------------------
  7. local WeekendFuliExcel = require("excel.present").weekendFuli
  8. local Util = require("common.Util")
  9. local Lang = require("common.Lang")
  10. local Msg = require("core.Msg")
  11. local Grid = require("bag.Grid")
  12. local BagLogic = require("bag.BagLogic")
  13. local Broadcast = require("broadcast.Broadcast")
  14. local PanelDefine = require("broadcast.PanelDefine")
  15. local YunYingLogic = require("yunying.YunYingLogic")
  16. -- 状态
  17. local STATE_NONE = 0 -- 不可领
  18. local STATE_GET = 1 -- 可领
  19. local STATE_HAD = 2 -- 已领
  20. local STATE_OVER = 3 -- 过期
  21. -- 获取本期活动时间 开始时间 结束时间
  22. local TEMP_TIME = nil
  23. function getActTime()
  24. local nowTime = os.time()
  25. if not TEMP_TIME or nowTime >= TEMP_TIME.endTime then
  26. local time = Util.getWeekStartTime(nowTime)
  27. local sconf = WeekendFuliExcel[1]
  28. local tconf = WeekendFuliExcel[#WeekendFuliExcel]
  29. local startTime = time + (sconf.week - 1) * 86400
  30. local endTime = time + tconf.week * 86400
  31. if startTime > endTime then
  32. if nowTime >= endTime then
  33. endTime = endTime + 7 * 86400
  34. else
  35. startTime = startTime - 7 * 86400
  36. end
  37. end
  38. TEMP_TIME = TEMP_TIME or {}
  39. TEMP_TIME.startTime = startTime
  40. TEMP_TIME.endTime = endTime
  41. end
  42. return TEMP_TIME.startTime, TEMP_TIME.endTime
  43. end
  44. -- 是否开启
  45. function isOpen(human)
  46. local startTime, endTime = getActTime()
  47. local nowTime = os.time()
  48. if startTime <= nowTime and nowTime < endTime then
  49. return true
  50. end
  51. end
  52. -- 是否有红点
  53. function isRed(human)
  54. checkDirty(human)
  55. for id in ipairs(WeekendFuliExcel) do
  56. if getState(human, id) == STATE_GET then
  57. return true
  58. end
  59. end
  60. end
  61. -- 礼包状态
  62. function getState(human, id)
  63. local config = WeekendFuliExcel[id]
  64. if not config then
  65. return STATE_NONE
  66. end
  67. if isGet(human, id) then
  68. return STATE_HAD
  69. end
  70. local nowWeek = Util.getWeekDay() - 1
  71. if nowWeek == 0 then
  72. nowWeek = 7
  73. end
  74. if nowWeek == config.week then
  75. return STATE_GET
  76. end
  77. local startTime = getActTime()
  78. local nowTime = os.time()
  79. local nowID = math.ceil(math.max(nowTime - startTime, 1) / 86400)
  80. if nowID > id then
  81. return STATE_OVER
  82. end
  83. return STATE_NONE
  84. end
  85. -- 检查状态
  86. function checkDirty(human)
  87. if not human.db.weekendFuli then
  88. return
  89. end
  90. local data = human.db.weekendFuli
  91. local startTime = getActTime()
  92. if data.ts ~= startTime then
  93. human.db.weekendFuli = nil
  94. end
  95. end
  96. -- 设置已领取
  97. function setGet(human, id)
  98. if not human.db.weekendFuli then
  99. local startTime = getActTime()
  100. human.db.weekendFuli = {}
  101. human.db.weekendFuli.ts = startTime
  102. end
  103. local data = human.db.weekendFuli
  104. data.list = data.list or {}
  105. data.list[id] = true
  106. end
  107. -- 是否已领取
  108. function isGet(human, id)
  109. if not human.db.weekendFuli then
  110. return
  111. end
  112. if not human.db.weekendFuli.list then
  113. return
  114. end
  115. return human.db.weekendFuli.list[id]
  116. end
  117. -- 封装礼包结构体
  118. function fontNet(net, id, config, human)
  119. net.id = id
  120. net.name = config.name
  121. net.items[0] = #config.items
  122. for i = 1, net.items[0] do
  123. local itemID = config.items[i][1]
  124. local itemCnt = config.items[i][2]
  125. Grid.makeItem(net.items[i], itemID, itemCnt)
  126. end
  127. net.state = getState(human, id)
  128. end
  129. -- 界面查询
  130. function sendQuery(human)
  131. checkDirty(human)
  132. local startTime, endTime = getActTime()
  133. local msgRet = Msg.gc.GC_WEEKEND_FULI_QUERY
  134. msgRet.startTime = startTime
  135. msgRet.endTime = endTime
  136. msgRet.list[0] = #WeekendFuliExcel
  137. for id, config in ipairs(WeekendFuliExcel) do
  138. fontNet(msgRet.list[id], id, config, human)
  139. end
  140. --Msg.trace(msgRet)
  141. Msg.send(msgRet, human.fd)
  142. end
  143. -- 领取奖励
  144. function getItems(human, id)
  145. local state = getState(human, id)
  146. if state == STATE_NONE or state == STATE_OVER then
  147. return Broadcast.sendErr(human, Lang.YUNYING_GET_ERR_CONDITION)
  148. end
  149. if state == STATE_HAD then
  150. return Broadcast.sendErr(human, Lang.YUNYING_GET_ERR_HADGET)
  151. end
  152. local config = WeekendFuliExcel[id]
  153. setGet(human, id)
  154. BagLogic.addItemList(human, config.items, "weekend_fuli")
  155. sendQuery(human)
  156. -- 没有此活动 暂时注释
  157. --YunYingLogic.updateIcon(YYInfo, human)
  158. --YunYingLogic.sendGroupUpdate(YYInfo, human, PanelDefine.PANEL_ID_3306)
  159. end