RichangLibaoLogic.lua 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. -- 根据类型获取配置
  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, nBuyNum)
  89. nBuyNum = nBuyNum or 1
  90. if not human.db.richangLibao then
  91. human.db.richangLibao = {}
  92. end
  93. local libaoData = human.db.richangLibao[libaoType]
  94. if not libaoData then
  95. libaoData = {}
  96. libaoData.ts = os.time()
  97. libaoData.list = {}
  98. human.db.richangLibao[libaoType] = libaoData
  99. end
  100. libaoData.list[id] = (libaoData.list[id] or 0) + nBuyNum
  101. end
  102. -- 封装礼包结构体
  103. function fontLibaoNet(net, id, config, libaoType, human)
  104. net.id = id
  105. net.items[0] = #config.items
  106. for i = 1, net.items[0] do
  107. local itemID = config.items[i][1]
  108. local itemCnt = config.items[i][2]
  109. Grid.makeItem(net.items[i], itemID, itemCnt)
  110. end
  111. net.name = config.name
  112. net.buyItem[0] = 0
  113. if config.buyID > 0 then
  114. net.buyItem[0] = 1
  115. BuyLogic.fontBuyItem(human, net.buyItem[1], config.buyID)
  116. end
  117. net.cnt = getBuyCnt(human, libaoType, id)
  118. net.maxCnt = config.cnt
  119. end
  120. -- 界面查询
  121. function sendQuery(human, libaoType)
  122. local startTime, endTime = getLibaoTime(libaoType)
  123. if not startTime then -- 没设置开服时间不开
  124. return Broadcast.sendErr(human, Lang.JJC_NOT_SET_OPEN_TIME)
  125. end
  126. local configs = getConfigs(libaoType)
  127. if not configs then return end
  128. checkDirty(human)
  129. local msgRet = Msg.gc.GC_RICHANG_LIBAO_QUERY
  130. msgRet.libaoType = libaoType
  131. msgRet.leftTime = endTime - os.time()
  132. msgRet.list[0] = #configs
  133. for i = 1, msgRet.list[0] do
  134. local config = configs[i]
  135. fontLibaoNet(msgRet.list[i], i, config, libaoType, human)
  136. end
  137. --Msg.trace(msgRet)
  138. Msg.send(msgRet, human.fd)
  139. end
  140. -- 购买礼包
  141. function buyLibao(human, libaoType, id, buyType, nBuyNum)
  142. local startTime, endTime = getLibaoTime(libaoType)
  143. if not startTime then -- 没设置开服时间不开
  144. return Broadcast.sendErr(human, Lang.JJC_NOT_SET_OPEN_TIME)
  145. end
  146. local configs = getConfigs(libaoType)
  147. local config = configs and configs[id]
  148. if not config then return end
  149. if buyType == FREE_BUY and config.buyID > 0 then
  150. return -- rmb礼包不能免费领取
  151. end
  152. checkDirty(human)
  153. local nCnt = getBuyCnt(human, libaoType, id)
  154. if getBuyCnt(human, libaoType, id) >= config.cnt then
  155. return Broadcast.sendErr(human, Lang.YUNYING_BUY_ERR_CNT)
  156. end
  157. if nCnt + nBuyNum > config.cnt then
  158. return Broadcast.sendErr(human, Lang.YUNYING_BUY_ERR_CNT)
  159. end
  160. local logType = getLogType(libaoType)
  161. addBuyCnt(human, libaoType, id, nBuyNum)
  162. local tItemList = {}
  163. for _, v in ipairs(config.items) do
  164. table.insert(tItemList, {v[1], v[2]*nBuyNum})
  165. end
  166. BagLogic.addItemList(human, tItemList, logType)
  167. sendQuery(human, libaoType)
  168. -- 更新界面
  169. local targetPanelID = nil
  170. if libaoType == LIBAOTYPE_WEEK then
  171. targetPanelID = PanelDefine.PANEL_ID_3104
  172. else
  173. targetPanelID = PanelDefine.PANEL_ID_3207
  174. end
  175. -- 通过panelID找到对应的funcID和YYInfo
  176. -- funcID和YYInfo是在YunYingLogic.initYYInfo中动态添加到模块的
  177. local RichangLibaoLogic = require("present.RichangLibaoLogic")
  178. local moduleFuncID = RichangLibaoLogic.funcID or {}
  179. local moduleYYInfo = RichangLibaoLogic.YYInfo or {}
  180. for k, v in pairs(moduleFuncID) do
  181. local funcConfig = YunYingLogic.getFuncConfig(k)
  182. if funcConfig and funcConfig.panelID == targetPanelID then
  183. YunYingLogic.updateIcon(moduleYYInfo[k], human)
  184. YunYingLogic.sendGroupUpdate(moduleYYInfo[k], human, targetPanelID)
  185. break
  186. end
  187. end
  188. end
  189. -- 红点
  190. function isRed(human, YYInfo, funcConfig)
  191. local libaoType = nil
  192. if funcConfig.panelID == PanelDefine.PANEL_ID_3104 then
  193. libaoType = LIBAOTYPE_WEEK
  194. elseif funcConfig.panelID == PanelDefine.PANEL_ID_3207 then
  195. libaoType = LIBAOTYPE_MONTH
  196. end
  197. if not libaoType then return end
  198. local configs = getConfigs(libaoType)
  199. if not configs then return end
  200. for id, config in pairs(configs) do
  201. if config.buyID < 1 and
  202. getBuyCnt(human, libaoType, id) < config.cnt then
  203. return true
  204. end
  205. end
  206. end
  207. -- 是否开启
  208. function isOpen(human, YYInfo, funcConfig)
  209. local libaoType = nil
  210. if funcConfig.panelID == PanelDefine.PANEL_ID_3104 then
  211. libaoType = LIBAOTYPE_WEEK
  212. elseif funcConfig.panelID == PanelDefine.PANEL_ID_3207 then
  213. libaoType = LIBAOTYPE_MONTH
  214. end
  215. if not libaoType or not getLibaoTime(libaoType) then
  216. return
  217. end
  218. local startTime, endTime = getLibaoTime(libaoType)
  219. return true, endTime,startTime
  220. end
  221. function updateDaily(human, funcID)
  222. checkDirty(human)
  223. end
  224. local function GetConfIDAndType(tConf, nBuyID, nType)
  225. for nID, v in ipairs(tConf) do
  226. if v.buyID == nBuyID then
  227. return true, nID, nType, v.cnt
  228. end
  229. end
  230. return false, 0, 0, 0
  231. end
  232. function GetRemainNum(human, nBuyID)
  233. -- 这里只有礼包ID,只能遍历
  234. local bHave, nID, nType, nLimitCnt = GetConfIDAndType(PresentExcel.weekLibao, nBuyID, LIBAOTYPE_WEEK)
  235. if false == bHave then
  236. bHave, nID, nType, nLimitCnt = GetConfIDAndType(PresentExcel.monthLibao, nBuyID, LIBAOTYPE_MONTH)
  237. end
  238. if false == bHave then
  239. return 0
  240. end
  241. local nBuyNum = getBuyCnt(human, nType, nID)
  242. return (nLimitCnt > nBuyNum) and (nLimitCnt - nBuyNum) or 0
  243. end