RichangLibaoLogic.lua 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. ---------------------------------------------------
  2. -- 充值-每周/每月礼包
  3. -- 开服起每x天为1期
  4. -- db.richangLibao[libaoType] = libaoData
  5. -- libaoData.ts 购买时间戳
  6. -- libaoData.list 购买记录 [id] = cnt
  7. ---------------------------------------------------
  8. local PresentExcel = require("excel.present")
  9. local Util = require("common.Util")
  10. local Lang = require("common.Lang")
  11. local CommonDB = require("common.CommonDB")
  12. local Msg = require("core.Msg")
  13. local Broadcast = require("broadcast.Broadcast")
  14. local PanelDefine = require("broadcast.PanelDefine")
  15. local Grid = require("bag.Grid")
  16. local BagLogic = require("bag.BagLogic")
  17. local BuyLogic = require("topup.BuyLogic")
  18. local YunYingLogic = require("yunying.YunYingLogic")
  19. LIBAOTYPE_WEEK = 1 -- 每周礼包
  20. LIBAOTYPE_MONTH = 2 -- 每月礼包
  21. FREE_BUY = 1 -- 免费领取
  22. RMB_BUY = 2 -- rmb购买
  23. -- 根据类型获取配置
  24. function getConfigs(libaoType)
  25. if libaoType == LIBAOTYPE_WEEK then
  26. return PresentExcel.weekLibao
  27. elseif libaoType == LIBAOTYPE_MONTH then
  28. return PresentExcel.monthLibao
  29. end
  30. end
  31. -- 获取刷新周期 x天
  32. function getRefreshDay(libaoType)
  33. if libaoType == LIBAOTYPE_WEEK then
  34. return 7
  35. elseif libaoType == LIBAOTYPE_MONTH then
  36. return 30
  37. end
  38. end
  39. --
  40. function getLogType(libaoType)
  41. if libaoType == LIBAOTYPE_WEEK then
  42. return "week_libao"
  43. elseif libaoType == LIBAOTYPE_MONTH then
  44. return "month_libao"
  45. end
  46. end
  47. -- 获取本期礼包开始和结束时间
  48. function getLibaoTime(libaoType)
  49. local openDay = CommonDB.getServerOpenDay()
  50. if not openDay then return end -- 没有设置开服时间 不开活动哦
  51. if openDay < 1 then return end -- 开服时间不对 也不开
  52. local refreshDay = getRefreshDay(libaoType)
  53. if not refreshDay then return end
  54. local day = openDay % refreshDay -- 本期礼包 第x天
  55. if day == 0 then
  56. day = refreshDay
  57. end
  58. local dayStartTime = Util.getDayStartTime(os.time())
  59. local startTime = dayStartTime - (day - 1) * 86400
  60. local endTime = startTime + refreshDay * 86400
  61. return startTime, endTime
  62. end
  63. -- 检查是否需要刷新
  64. function checkDirty(human)
  65. if not human.db.richangLibao then
  66. return
  67. end
  68. for libaoType, libaoData in pairs(human.db.richangLibao) do
  69. local startTime, endTime = getLibaoTime(libaoType)
  70. if (startTime and libaoData.ts < startTime) or
  71. (endTime and libaoData.ts > endTime) then
  72. human.db.richangLibao[libaoType] = nil
  73. end
  74. end
  75. end
  76. -- 获取已购买次数
  77. function getBuyCnt(human, libaoType, id)
  78. if not human.db.richangLibao then
  79. return 0
  80. end
  81. local libaoData = human.db.richangLibao[libaoType]
  82. if not libaoData then
  83. return 0
  84. end
  85. return libaoData.list[id] or 0
  86. end
  87. -- 增加已购买次数
  88. function addBuyCnt(human, libaoType, id)
  89. if not human.db.richangLibao then
  90. human.db.richangLibao = {}
  91. end
  92. local libaoData = human.db.richangLibao[libaoType]
  93. if not libaoData then
  94. libaoData = {}
  95. libaoData.ts = os.time()
  96. libaoData.list = {}
  97. human.db.richangLibao[libaoType] = libaoData
  98. end
  99. libaoData.list[id] = (libaoData.list[id] or 0) + 1
  100. end
  101. -- 封装礼包结构体
  102. function fontLibaoNet(net, id, config, libaoType, human)
  103. net.id = id
  104. net.items[0] = #config.items
  105. for i = 1, net.items[0] do
  106. local itemID = config.items[i][1]
  107. local itemCnt = config.items[i][2]
  108. Grid.makeItem(net.items[i], itemID, itemCnt)
  109. end
  110. net.name = config.name
  111. net.buyItem[0] = 0
  112. if config.buyID > 0 then
  113. net.buyItem[0] = 1
  114. BuyLogic.fontBuyItem(human, net.buyItem[1], config.buyID)
  115. end
  116. net.cnt = getBuyCnt(human, libaoType, id)
  117. net.maxCnt = config.cnt
  118. end
  119. -- 界面查询
  120. function sendQuery(human, libaoType)
  121. local startTime, endTime = getLibaoTime(libaoType)
  122. if not startTime then -- 没设置开服时间不开
  123. return Broadcast.sendErr(human, Lang.JJC_NOT_SET_OPEN_TIME)
  124. end
  125. local configs = getConfigs(libaoType)
  126. if not configs then return end
  127. checkDirty(human)
  128. local msgRet = Msg.gc.GC_RICHANG_LIBAO_QUERY
  129. msgRet.libaoType = libaoType
  130. msgRet.leftTime = endTime - os.time()
  131. msgRet.list[0] = #configs
  132. for i = 1, msgRet.list[0] do
  133. local config = configs[i]
  134. fontLibaoNet(msgRet.list[i], i, config, libaoType, human)
  135. end
  136. --Msg.trace(msgRet)
  137. Msg.send(msgRet, human.fd)
  138. end
  139. -- 购买礼包
  140. function buyLibao(human, libaoType, id, buyType)
  141. local startTime, endTime = getLibaoTime(libaoType)
  142. if not startTime then -- 没设置开服时间不开
  143. return Broadcast.sendErr(human, Lang.JJC_NOT_SET_OPEN_TIME)
  144. end
  145. local configs = getConfigs(libaoType)
  146. local config = configs and configs[id]
  147. if not config then return end
  148. if buyType == FREE_BUY and config.buyID > 0 then
  149. return -- rmb礼包不能免费领取
  150. end
  151. checkDirty(human)
  152. if getBuyCnt(human, libaoType, id) >= config.cnt then
  153. return Broadcast.sendErr(human, Lang.YUNYING_BUY_ERR_CNT)
  154. end
  155. local logType = getLogType(libaoType)
  156. addBuyCnt(human, libaoType, id)
  157. BagLogic.addItemList(human, config.items, logType)
  158. sendQuery(human, libaoType)
  159. if libaoType == LIBAOTYPE_WEEK then
  160. for k, v in pairs(funcID) do
  161. YunYingLogic.updateIcon(YYInfo[k], human)
  162. YunYingLogic.sendGroupUpdate(YYInfo[k], human, PanelDefine.PANEL_ID_3104)
  163. break
  164. end
  165. else
  166. for k, v in pairs(funcID) do
  167. YunYingLogic.updateIcon(YYInfo[k], human)
  168. YunYingLogic.sendGroupUpdate(YYInfo[k], human, PanelDefine.PANEL_ID_3207)
  169. break
  170. end
  171. end
  172. end
  173. -- 红点
  174. function isRed(human, YYInfo, funcConfig)
  175. local libaoType = nil
  176. if funcConfig.panelID == PanelDefine.PANEL_ID_3104 then
  177. libaoType = LIBAOTYPE_WEEK
  178. elseif funcConfig.panelID == PanelDefine.PANEL_ID_3207 then
  179. libaoType = LIBAOTYPE_MONTH
  180. end
  181. if not libaoType then return end
  182. local configs = getConfigs(libaoType)
  183. if not configs then return end
  184. for id, config in pairs(configs) do
  185. if config.buyID < 1 and
  186. getBuyCnt(human, libaoType, id) < config.cnt then
  187. return true
  188. end
  189. end
  190. end
  191. -- 是否开启
  192. function isOpen(human, YYInfo, funcConfig)
  193. local libaoType = nil
  194. if funcConfig.panelID == PanelDefine.PANEL_ID_3104 then
  195. libaoType = LIBAOTYPE_WEEK
  196. elseif funcConfig.panelID == PanelDefine.PANEL_ID_3207 then
  197. libaoType = LIBAOTYPE_MONTH
  198. end
  199. if not libaoType or not getLibaoTime(libaoType) then
  200. return
  201. end
  202. local startTime, endTime = getLibaoTime(libaoType)
  203. return true, endTime,startTime
  204. end
  205. function updateDaily(human, funcID)
  206. checkDirty(human)
  207. end