RichangLibaoLogic.lua 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. ---------------------------------------------------
  2. -- 充值-每周/每月礼包
  3. -- 开服起每x天为1期
  4. -- db.richangLibao[libaoType] = libaoData
  5. -- libaoData.ts 购买时间戳
  6. -- libaoData.list 购买记录 [id] = cnt
  7. ---------------------------------------------------
  8. local Util = require("common.Util")
  9. local PresentExcel = require("excel.present")
  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. local WEEK_GITFT_ACTID = 315 -- 周礼包活动Id
  24. local MONTH_GITFT_ACTID = 316 -- 月礼包活动Id
  25. -- 根据类型获取配置
  26. function getConfigs(libaoType)
  27. if libaoType == LIBAOTYPE_WEEK then
  28. return PresentExcel.weekLibao
  29. elseif libaoType == LIBAOTYPE_MONTH then
  30. return PresentExcel.monthLibao
  31. end
  32. end
  33. -- 获取刷新周期 x天
  34. function getRefreshDay(libaoType)
  35. if libaoType == LIBAOTYPE_WEEK then
  36. return 7
  37. elseif libaoType == LIBAOTYPE_MONTH then
  38. return 30
  39. end
  40. end
  41. --
  42. function getLogType(libaoType)
  43. if libaoType == LIBAOTYPE_WEEK then
  44. return "week_libao"
  45. elseif libaoType == LIBAOTYPE_MONTH then
  46. return "month_libao"
  47. end
  48. end
  49. -- 获取本期礼包开始和结束时间
  50. function getLibaoTime(libaoType)
  51. local openDay = CommonDB.getServerOpenDay()
  52. if not openDay then return end -- 没有设置开服时间 不开活动哦
  53. if openDay < 1 then return end -- 开服时间不对 也不开
  54. local refreshDay = getRefreshDay(libaoType)
  55. if not refreshDay then return end
  56. local day = openDay % refreshDay -- 本期礼包 第x天
  57. if day == 0 then
  58. day = refreshDay
  59. end
  60. local dayStartTime = Util.getDayStartTime(os.time())
  61. local startTime = dayStartTime - (day - 1) * 86400
  62. local endTime = startTime + refreshDay * 86400
  63. return startTime, endTime
  64. end
  65. -- 检查是否需要刷新
  66. function checkDirty(human)
  67. if not human.db.richangLibao then
  68. return
  69. end
  70. for libaoType, libaoData in pairs(human.db.richangLibao) do
  71. local startTime, endTime = getLibaoTime(libaoType)
  72. if (startTime and libaoData.ts < startTime) or
  73. (endTime and libaoData.ts > endTime) then
  74. human.db.richangLibao[libaoType] = nil
  75. end
  76. end
  77. end
  78. -- 获取已购买次数
  79. function getBuyCnt(human, libaoType, id)
  80. if not human.db.richangLibao then
  81. return 0
  82. end
  83. local libaoData = human.db.richangLibao[libaoType]
  84. if not libaoData then
  85. return 0
  86. end
  87. return libaoData.list[id] or 0
  88. end
  89. -- 增加已购买次数
  90. function addBuyCnt(human, libaoType, id, nBuyNum)
  91. nBuyNum = nBuyNum or 1
  92. if not human.db.richangLibao then
  93. human.db.richangLibao = {}
  94. end
  95. local libaoData = human.db.richangLibao[libaoType]
  96. if not libaoData then
  97. libaoData = {}
  98. libaoData.ts = os.time()
  99. libaoData.list = {}
  100. human.db.richangLibao[libaoType] = libaoData
  101. end
  102. libaoData.list[id] = (libaoData.list[id] or 0) + nBuyNum
  103. end
  104. -- 封装礼包结构体
  105. function fontLibaoNet(net, id, config, libaoType, human)
  106. net.id = id
  107. net.items[0] = #config.items
  108. for i = 1, net.items[0] do
  109. local itemID = config.items[i][1]
  110. local itemCnt = config.items[i][2]
  111. Grid.makeItem(net.items[i], itemID, itemCnt)
  112. end
  113. net.name = config.name
  114. net.buyItem[0] = 0
  115. if config.buyID > 0 then
  116. net.buyItem[0] = 1
  117. BuyLogic.fontBuyItem(human, net.buyItem[1], config.buyID)
  118. end
  119. net.cnt = getBuyCnt(human, libaoType, id)
  120. net.maxCnt = config.cnt
  121. end
  122. -- 界面查询
  123. function sendQuery(human, libaoType)
  124. local startTime, endTime = getLibaoTime(libaoType)
  125. if not startTime then -- 没设置开服时间不开
  126. return Broadcast.sendErr(human, Lang.JJC_NOT_SET_OPEN_TIME)
  127. end
  128. local configs = getConfigs(libaoType)
  129. if not configs then return end
  130. checkDirty(human)
  131. local msgRet = Msg.gc.GC_RICHANG_LIBAO_QUERY
  132. msgRet.libaoType = libaoType
  133. msgRet.leftTime = endTime - os.time()
  134. msgRet.list[0] = #configs
  135. for i = 1, msgRet.list[0] do
  136. local config = configs[i]
  137. fontLibaoNet(msgRet.list[i], i, config, libaoType, human)
  138. end
  139. --Msg.trace(msgRet)
  140. Msg.send(msgRet, human.fd)
  141. end
  142. -- 购买礼包
  143. function buyLibao(human, libaoType, id, buyType, nBuyNum)
  144. local startTime, endTime = getLibaoTime(libaoType)
  145. if not startTime then -- 没设置开服时间不开
  146. return Broadcast.sendErr(human, Lang.JJC_NOT_SET_OPEN_TIME)
  147. end
  148. local configs = getConfigs(libaoType)
  149. local config = configs and configs[id]
  150. if not config then return end
  151. if buyType == FREE_BUY and config.buyID > 0 then
  152. return -- rmb礼包不能免费领取
  153. end
  154. checkDirty(human)
  155. local nCnt = getBuyCnt(human, libaoType, id)
  156. if getBuyCnt(human, libaoType, id) >= config.cnt then
  157. return Broadcast.sendErr(human, Lang.YUNYING_BUY_ERR_CNT)
  158. end
  159. if nCnt + nBuyNum > config.cnt then
  160. return Broadcast.sendErr(human, Lang.YUNYING_BUY_ERR_CNT)
  161. end
  162. local logType = getLogType(libaoType)
  163. addBuyCnt(human, libaoType, id, nBuyNum)
  164. local tItemList = {}
  165. for _, v in ipairs(config.items) do
  166. table.insert(tItemList, {v[1], v[2]*nBuyNum})
  167. end
  168. BagLogic.addItemList(human, tItemList, logType)
  169. sendQuery(human, libaoType)
  170. -- 更新界面
  171. local targetPanelID = nil
  172. if libaoType == LIBAOTYPE_WEEK then
  173. targetPanelID = PanelDefine.PANEL_ID_3104
  174. else
  175. targetPanelID = PanelDefine.PANEL_ID_3207
  176. end
  177. -- 通过panelID找到对应的funcID和YYInfo
  178. -- funcID和YYInfo是在YunYingLogic.initYYInfo中动态添加到模块的
  179. -- 直接使用当前模块的 funcID 和 YYInfo,不需要重新 require
  180. local moduleFuncID = funcID or {}
  181. local moduleYYInfo = YYInfo or {}
  182. for k, v in pairs(moduleFuncID) do
  183. local funcConfig = YunYingLogic.getFuncConfig(k)
  184. if funcConfig and funcConfig.panelID == targetPanelID then
  185. YunYingLogic.updateIcon(moduleYYInfo[k], human)
  186. YunYingLogic.sendGroupUpdate(moduleYYInfo[k], human, targetPanelID)
  187. break
  188. end
  189. end
  190. end
  191. -- 红点
  192. function isRed(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 then return end
  200. local configs = getConfigs(libaoType)
  201. if not configs then return end
  202. for id, config in pairs(configs) do
  203. if config.buyID < 1 and
  204. getBuyCnt(human, libaoType, id) < config.cnt then
  205. return true
  206. end
  207. end
  208. end
  209. -- 是否开启
  210. function isOpen(human, YYInfo, funcConfig)
  211. local libaoType = nil
  212. if funcConfig.panelID == PanelDefine.PANEL_ID_3104 then
  213. libaoType = LIBAOTYPE_WEEK
  214. elseif funcConfig.panelID == PanelDefine.PANEL_ID_3207 then
  215. libaoType = LIBAOTYPE_MONTH
  216. end
  217. if not libaoType then
  218. print("[RichangLibaoLogic.isOpen] libaoType is nil for panelID="..funcConfig.panelID)
  219. return
  220. end
  221. local startTime, endTime = getLibaoTime(libaoType)
  222. if not startTime then
  223. print("[RichangLibaoLogic.isOpen] getLibaoTime returned nil for libaoType="..libaoType..", panelID="..funcConfig.panelID)
  224. return
  225. end
  226. -- 没有充值过, 不显示
  227. if not BuyLogic.isChange(human) then
  228. return false
  229. end
  230. return true, endTime, startTime
  231. end
  232. function ShowAct(human)
  233. local YunYingExcel = require("excel.yunying")
  234. local config = YunYingExcel.func[WEEK_GITFT_ACTID]
  235. YunYingLogic.sendGroupUpdate(YYInfo[WEEK_GITFT_ACTID], human, config.panelID)
  236. config = YunYingExcel.func[MONTH_GITFT_ACTID]
  237. YunYingLogic.sendGroupUpdate(YYInfo[MONTH_GITFT_ACTID], human, config.panelID)
  238. end
  239. function updateDaily(human, funcID)
  240. checkDirty(human)
  241. end
  242. local function GetConfIDAndType(tConf, nBuyID, nType)
  243. for nID, v in ipairs(tConf) do
  244. if v.buyID == nBuyID then
  245. return true, nID, nType, v.cnt
  246. end
  247. end
  248. return false, 0, 0, 0
  249. end
  250. function GetRemainNum(human, nBuyID)
  251. -- 这里只有礼包ID,只能遍历
  252. local bHave, nID, nType, nLimitCnt = GetConfIDAndType(PresentExcel.weekLibao, nBuyID, LIBAOTYPE_WEEK)
  253. if false == bHave then
  254. bHave, nID, nType, nLimitCnt = GetConfIDAndType(PresentExcel.monthLibao, nBuyID, LIBAOTYPE_MONTH)
  255. end
  256. if false == bHave then
  257. return 0
  258. end
  259. local nBuyNum = getBuyCnt(human, nType, nID)
  260. return (nLimitCnt > nBuyNum) and (nLimitCnt - nBuyNum) or 0
  261. end