RichangLibaoLogic.lua 8.8 KB

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