RichangLibaoLogic.lua 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. if libaoType == LIBAOTYPE_WEEK then
  169. for k, v in pairs(funcID) do
  170. YunYingLogic.updateIcon(YYInfo[k], human)
  171. YunYingLogic.sendGroupUpdate(YYInfo[k], human, PanelDefine.PANEL_ID_3104)
  172. break
  173. end
  174. else
  175. for k, v in pairs(funcID) do
  176. YunYingLogic.updateIcon(YYInfo[k], human)
  177. YunYingLogic.sendGroupUpdate(YYInfo[k], human, PanelDefine.PANEL_ID_3207)
  178. break
  179. end
  180. end
  181. end
  182. -- 红点
  183. function isRed(human, YYInfo, funcConfig)
  184. local libaoType = nil
  185. if funcConfig.panelID == PanelDefine.PANEL_ID_3104 then
  186. libaoType = LIBAOTYPE_WEEK
  187. elseif funcConfig.panelID == PanelDefine.PANEL_ID_3207 then
  188. libaoType = LIBAOTYPE_MONTH
  189. end
  190. if not libaoType then return end
  191. local configs = getConfigs(libaoType)
  192. if not configs then return end
  193. for id, config in pairs(configs) do
  194. if config.buyID < 1 and
  195. getBuyCnt(human, libaoType, id) < config.cnt then
  196. return true
  197. end
  198. end
  199. end
  200. -- 是否开启
  201. function isOpen(human, YYInfo, funcConfig)
  202. local libaoType = nil
  203. if funcConfig.panelID == PanelDefine.PANEL_ID_3104 then
  204. libaoType = LIBAOTYPE_WEEK
  205. elseif funcConfig.panelID == PanelDefine.PANEL_ID_3207 then
  206. libaoType = LIBAOTYPE_MONTH
  207. end
  208. if not libaoType or not getLibaoTime(libaoType) then
  209. return
  210. end
  211. local startTime, endTime = getLibaoTime(libaoType)
  212. return true, endTime,startTime
  213. end
  214. function updateDaily(human, funcID)
  215. checkDirty(human)
  216. end
  217. local function GetConfIDAndType(tConf, nBuyID, nType)
  218. for nID, v in ipairs(tConf) do
  219. if v.buyID == nBuyID then
  220. return true, nID, nType, v.cnt
  221. end
  222. end
  223. return false, 0, 0, 0
  224. end
  225. function GetRemainNum(human, nBuyID)
  226. -- 这里只有礼包ID,只能遍历
  227. local bHave, nID, nType, nLimitCnt = GetConfIDAndType(PresentExcel.weekLibao, nBuyID, LIBAOTYPE_WEEK)
  228. if false == bHave then
  229. bHave, nID, nType, nLimitCnt = GetConfIDAndType(PresentExcel.monthLibao, nBuyID, LIBAOTYPE_MONTH)
  230. end
  231. if false == bHave then
  232. return 0
  233. end
  234. local nBuyNum = getBuyCnt(human, nType, nID)
  235. return (nLimitCnt > nBuyNum) and (nLimitCnt - nBuyNum) or 0
  236. end