VoucherShopLogic.lua 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. --------------------------------
  2. -- 文件名 : Voucher/Proto.lua
  3. -- 文件说明 : 代金券 - 通用定义
  4. -- 创建时间 : 2024/12/16
  5. -- 创建人 : FC
  6. --------------------------------
  7. local VoucherShopDefine = require("voucher.VoucherShopDefine")
  8. local BuyExcel = require("excel.buy")
  9. local Util = require("common.Util")
  10. local Lang = require("common.Lang")
  11. local Broadcast = require("broadcast.Broadcast")
  12. local MailExcel = require("excel.mail")
  13. local Msg = require("core.Msg")
  14. local ObjHuman = require("core.ObjHuman")
  15. local CommonDB = require("common.CommonDB")
  16. local MailManager = require("mail.MailManager")
  17. local Grid = require("bag.Grid")
  18. local HeroGrid = require("hero.HeroGrid")
  19. local HeroLogic = require("hero.HeroLogic")
  20. local BagLogic = require("bag.BagLogic")
  21. local Log = require("common.Log")
  22. local BuyLogic = require("topup.BuyLogic")
  23. local YunYingLogic = require("yunying.YunYingLogic")
  24. local TopupLogic =require("topup.TopupLogic")
  25. local TriggerDefine = require("trigger.TriggerDefine")
  26. local TriggerLogic = require("trigger.TriggerLogic")
  27. local VouchProConf = require("excel.voucherPro")
  28. -- 商店配置
  29. local tVoucherShopCof = nil
  30. -- 膨胀配置
  31. local tVoucherInflateCof = nil
  32. -- 开放等级
  33. local VOUCHERSHOP_OPENLV = 40
  34. local VOUCHERSHOP_ACTID = 27
  35. ----------------------------------------- 内部处理开始 -------------------------------------
  36. local function VoucherShop_GetConfig()
  37. return BuyExcel.VoucherShop
  38. end
  39. -- 写日志
  40. local function VoucherShop_WriteLog(human, szFuncName, sztext)
  41. Log.write(Log.LOGID_OSS_VOUCHERSHOP, szFuncName..sztext.." _id = "..human.db._id.." name = "..human.db.name)
  42. end
  43. local function VoucherShop_GetPro()
  44. local tProConf = VoucherShopDefine.INFLATIONPROBABILITY
  45. if next(VouchProConf[1].startDate) and next(VouchProConf[1].endDate) then
  46. local tStartDate = VouchProConf.startDate
  47. local tEndDate = VouchProConf.endDate
  48. tStartDate.hour = 0
  49. tStartDate.min = 0
  50. tStartDate.sec = 0
  51. tEndDate.sec = 0
  52. local realStartTime = os.time(tStartDate)
  53. local realEndTime = os.time(tEndDate)
  54. local nNowTime = os.time()
  55. if realStartTime <= nNowTime and realEndTime > nNowTime then
  56. tProConf = {
  57. [1] = {VouchProConf[1].ProOne, 1.2},
  58. [2] = {VouchProConf[1].ProOne + VouchProConf[1].ProTwo, 1.5},
  59. [3] = {VouchProConf[1].ProOne + VouchProConf[1].ProTwo + VouchProConf[1].ProThree, 3},
  60. }
  61. end
  62. end
  63. return tProConf
  64. end
  65. local function VoucherShop_InitShopConfig()
  66. local tAllConfig = VoucherShop_GetConfig()
  67. if not tAllConfig then
  68. return
  69. end
  70. tVoucherShopCof = {}
  71. for nID, v in pairs(tAllConfig) do
  72. if VoucherShopDefine.VOUCHERTYPE_SHOP == v.nType then
  73. tVoucherShopCof[v.buyID] =
  74. {
  75. nID = nID,
  76. tData = v
  77. }
  78. end
  79. end
  80. end
  81. local function VoucherShop_InitInflateConfig()
  82. local tAllConfig = VoucherShop_GetConfig()
  83. if not tAllConfig then
  84. return
  85. end
  86. tVoucherInflateCof = {}
  87. for nID, v in pairs(tAllConfig) do
  88. if VoucherShopDefine.VOUCHERTYPE_INFLATION == v.nType then
  89. tVoucherInflateCof[v.buyID] =
  90. {
  91. nID = nID,
  92. tData = v
  93. }
  94. end
  95. end
  96. end
  97. -- 获取商店配置
  98. local function VoucherShop_GetShopConfig()
  99. if not tVoucherShopCof then
  100. VoucherShop_InitShopConfig()
  101. end
  102. return tVoucherShopCof
  103. end
  104. -- 获取膨胀配置
  105. local function VoucherShop_GetInflateConfig()
  106. if not tVoucherInflateCof then
  107. VoucherShop_InitInflateConfig()
  108. end
  109. return tVoucherInflateCof
  110. end
  111. -- 重置购买状态信息
  112. local function VoucherShop_RestStatus(human)
  113. if not human then
  114. return
  115. end
  116. human.db.tBuyVoucherInflate = {
  117. nStatus = VoucherShopDefine.VOUCHERSTATUS_NOGET,
  118. nTime = 0
  119. }
  120. end
  121. -- 获取购买状态信息
  122. local function VoucherShop_GetStatus(human)
  123. if not human then
  124. return
  125. end
  126. if not human.db.tBuyVoucherInflate then
  127. VoucherShop_RestStatus(human)
  128. end
  129. return human.db.tBuyVoucherInflate
  130. end
  131. -- 设置购买状态
  132. local function VoucherShop_SetStatus(human, nStatus, nTime)
  133. if not human then
  134. return
  135. end
  136. if not human.db.tBuyVoucherInflate then
  137. VoucherShop_RestStatus(human)
  138. end
  139. human.db.tBuyVoucherInflate.nStatus = nStatus
  140. human.db.tBuyVoucherInflate.nTime = nTime
  141. end
  142. local function VoucherShop_GetAddNum(tConfig)
  143. if not tConfig then
  144. return 0, 0
  145. end
  146. table.print_lua_table(tConfig)
  147. local nMultiple = 1
  148. local nNum = tConfig.tData.nGetVoucherNum
  149. if VoucherShopDefine.VOUCHERTYPE_INFLATION == tConfig.tData.nType then
  150. local nRandNum = math.random(1, 100)
  151. local tProConf = VoucherShop_GetPro()
  152. for nID, v in ipairs(tProConf) do
  153. -- if nRandNum <= nID then
  154. -- nMultiple = v
  155. -- break
  156. -- end
  157. if nRandNum <= v[1] then
  158. nMultiple = v[2]
  159. break
  160. end
  161. end
  162. end
  163. nNum = nMultiple * nNum
  164. return nNum, nMultiple
  165. end
  166. ----------------------------------------- 其他模块调用 -------------------------------------
  167. -- 购买回调
  168. function VoucherShop_OnBuyVoucher(human, nBuyID)
  169. if not human or not nBuyID then
  170. return
  171. end
  172. -- 记录一下日志
  173. local sztext = " nBuyID = "..nBuyID
  174. VoucherShop_WriteLog(human, "[VoucherShop_OnBuyVoucher]", "玩家购买代金券回调记录开始 "..sztext)
  175. local tShopCof = VoucherShop_GetShopConfig()
  176. local tInflateConfig = VoucherShop_GetInflateConfig()
  177. if not tShopCof or not tInflateConfig then
  178. print("[VoucherShop_OnBuyVoucher] 获取配置失败 name = "..human.db.name..sztext)
  179. VoucherShop_WriteLog(human, "[VoucherShop_OnBuyVoucher]", "玩家购买代金券回调获取不到配置 "..sztext)
  180. return
  181. end
  182. local nAddNum, nMultiple = VoucherShop_GetAddNum(tShopCof[nBuyID] or tInflateConfig[nBuyID])
  183. if 0 >= nAddNum or 0 >= nMultiple then
  184. sztext = sztext.." nAddNum = "..nAddNum.." nMultiple = "..nMultiple
  185. print("[VoucherShop_OnBuyVoucher] 获取增加的数量和倍率失败 name = "..human.db.name..sztext)
  186. VoucherShop_WriteLog(human, "[VoucherShop_OnBuyVoucher]", "获取增加的数量和倍率失败 nBuyID = "..sztext)
  187. return
  188. end
  189. -- 给物品
  190. BagLogic.addItem(human, VoucherShopDefine.VOUCHERITME_ID, nAddNum, "voucher_use")
  191. -- 弹窗
  192. BagLogic.sendItemGetList(human, {{VoucherShopDefine.VOUCHERITME_ID, nAddNum}}, "voucher_use")
  193. if nMultiple > 1 then
  194. local szInflateText = "恭喜【"..human.db.name.."】通过刮刮乐获得"..nAddNum.."代金券"
  195. CommonDB.setVoucherInflate(szInflateText, os.time())
  196. VoucherShop_SetStatus(human, VoucherShopDefine.VOUCHERSTATUS_GET, os.time())
  197. VoucherShop_QueryInflate(human)
  198. end
  199. VoucherShop_WriteLog(human, "[VoucherShop_OnBuyVoucher]", "玩家购买代金券回调记录结束 "..sztext)
  200. TriggerLogic.PublishEvent(TriggerDefine.VOUCHER_BUY_NUM, human.db._id, nAddNum)
  201. end
  202. -- 定时回调
  203. function VoucherShop_OnZero()
  204. -- 遍历在线玩家
  205. for uuid, human in pairs(ObjHuman.onlineUuid) do
  206. VoucherShop_RestStatus(human)
  207. end
  208. end
  209. -- 玩家登录
  210. function onLogin(human)
  211. if not human then
  212. return
  213. end
  214. local tStatus = VoucherShop_GetStatus(human)
  215. if not tStatus then
  216. return
  217. end
  218. if tStatus.nStatus == VoucherShopDefine.VOUCHERSTATUS_GET then
  219. local nOldTime = tStatus.nTime
  220. local nNowTime = os.time()
  221. local sameDay = Util.isSameDayByTimes(nNowTime, nOldTime)
  222. if true ~= sameDay then
  223. VoucherShop_RestStatus(human)
  224. VoucherShop_WriteLog(human, "[VoucherShop_OnBuyVoucher]", "玩家登录重置购买状态 nOldTime = "..nOldTime.." nNowTime = "..nNowTime)
  225. end
  226. end
  227. end
  228. ----------------------------------------- 客户端请求 -------------------------------------
  229. -- 请求代金券商店信息
  230. function VoucherShop_QueryShop(human)
  231. if not human then
  232. return
  233. end
  234. local tConfig = VoucherShop_GetShopConfig()
  235. if not tConfig then
  236. VoucherShop_WriteLog(human, "[VoucherShop_QueryShop]", "获取商店配置失败")
  237. print("[VoucherShop_QueryShop] 获取商店配置失败")
  238. return
  239. end
  240. local tMsgData = Msg.gc.GC_VOUCHER_QUERY_SHOP
  241. --tMsgData.tVoucherData[0] = #tConfig
  242. local nIndex = 1
  243. for nBuyID, v in pairs(tConfig) do
  244. local tData = tMsgData.tVoucherData[nIndex]
  245. tData.nID = v.nID
  246. --tData.nGetVoucherNum = v.tData.nGetVoucherNum
  247. Grid.makeItem(tData.item, VoucherShopDefine.VOUCHERITME_ID, v.tData.nGetVoucherNum)
  248. print("[VoucherShop_QueryShop] nID = "..tData.nID.." nGetVoucherNum = "..v.tData.nGetVoucherNum)
  249. BuyLogic.fontBuyItem(human, tData.tBuyItem, nBuyID)
  250. nIndex = nIndex +1
  251. end
  252. print("[VoucherShop_QueryShop] nIndex = "..nIndex)
  253. tMsgData.tVoucherData[0] = nIndex - 1
  254. Msg.send(tMsgData, human.fd)
  255. end
  256. -- 请求代金券膨胀信息
  257. function VoucherShop_QueryInflate(human)
  258. if not human then
  259. return
  260. end
  261. local tConfig = VoucherShop_GetInflateConfig()
  262. if not tConfig then
  263. VoucherShop_WriteLog(human, "[VoucherShop_QueryInflate]", "获取膨胀配置失败")
  264. print("[VoucherShop_QueryInflate] 获取膨胀配置失败")
  265. return
  266. end
  267. local tStatus = VoucherShop_GetStatus(human)
  268. if not tStatus then
  269. VoucherShop_WriteLog(human, "[VoucherShop_QueryInflate]", "获取状态信息失败")
  270. print("[VoucherShop_QueryInflate] 获取状态信息失败")
  271. return
  272. end
  273. local tMsgData = Msg.gc.GC_VOUCHER_QUERY_INFLATE
  274. tMsgData.nStatus = tStatus.nStatus
  275. --tMsgData.tVoucherData[0] = #tConfig
  276. local nIndex = 1
  277. for nBuyID, v in pairs(tConfig) do
  278. local tData = tMsgData.tVoucherData[nIndex]
  279. tData.nID = v.nID
  280. Grid.makeItem(tData.item, VoucherShopDefine.VOUCHERITME_ID, v.tData.nGetVoucherNum)
  281. BuyLogic.fontBuyItem(human, tData.tBuyItem, nBuyID)
  282. nIndex = nIndex +1
  283. end
  284. tMsgData.tVoucherData[0] = nIndex - 1
  285. local szTextInfo = CommonDB.getVoucherInflate()
  286. tMsgData.tVoucherText[0] = 0
  287. if szTextInfo then
  288. local nLen = #szTextInfo
  289. tMsgData.tVoucherText[0] = nLen > 20 and 20 or nLen
  290. for i, v in pairs(szTextInfo) do
  291. if i > 20 then
  292. break
  293. end
  294. tMsgData.tVoucherText[i] = v.szText
  295. print("[VoucherShop_QueryInflate] szText = "..v.szText)
  296. end
  297. end
  298. table.print_lua_table(tMsgData.tVoucherText)
  299. print("[VoucherShop_QueryInflate] nIndex = "..nIndex)
  300. Msg.send(tMsgData, human.fd)
  301. end
  302. -- 请求代金券购买商品
  303. function VoucherShop_BuyItem(human, nBuyID)
  304. if not human or 0 >= nBuyID then
  305. return
  306. end
  307. local tBuyConfig = VoucherShop_GetShopConfig()
  308. local tInflateConfig = VoucherShop_GetInflateConfig()
  309. if not tBuyConfig or not tInflateConfig then
  310. return
  311. end
  312. if tBuyConfig[nBuyID] or tInflateConfig[nBuyID] then
  313. print("[VoucherShop_BuyItem] 玩家使用代金券购买代金券礼品,直接返回")
  314. VoucherShop_WriteLog(human, "[VoucherShop_BuyItem]", "玩家使用代金券购买代金券礼品,直接返回")
  315. return
  316. end
  317. local tTrueBuyConfig = BuyExcel.buy[nBuyID]
  318. if not tTrueBuyConfig then
  319. print("[VoucherShop_BuyItem] 不存在对应的商品信息 nBuyID = "..nBuyID)
  320. return
  321. end
  322. local nDelVoucherNum = tTrueBuyConfig.Voucher
  323. if 0 >= nDelVoucherNum then
  324. print("[VoucherShop_BuyItem] 该商品无法用代金券购买,配置的数量为空 nBuyID = "..nBuyID.." nDelVoucherNum = "..nDelVoucherNum)
  325. return
  326. end
  327. -- 删物品
  328. BagLogic.delItem(human, VoucherShopDefine.VOUCHERITME_ID, nDelVoucherNum, "voucher_use")
  329. -- 发道具
  330. local tBuyInfo =
  331. {
  332. buyID = nBuyID,
  333. price = tTrueBuyConfig.CN
  334. }
  335. BuyLogic.buy(human, tBuyInfo)
  336. -- local voucherDenomination = tTrueBuyConfig.CN
  337. -- if voucherDenomination >= 100 then
  338. -- TopupLogic.sendFireworksMail(human, nDelVoucherNum)
  339. -- end
  340. YunYingLogic.sendIconUpdate(VOUCHERSHOP_ACTID, human)
  341. -- 写日志
  342. local szText = "玩家购买使用代金券购买了物品 nBuyID = "..nBuyID.." 消耗代金券数量 nDelVoucherNum = "..nDelVoucherNum
  343. VoucherShop_WriteLog(human, "[VoucherShop_BuyItem]", szText)
  344. end
  345. ----------------------------------------- 活动模板调用 -------------------------------------
  346. -- 是否开启
  347. function isOpen(human, YYInfo, funcConfig)
  348. if human.version and human.version == VoucherShopDefine.VOUCHER_SP_PLATFORM then
  349. return true
  350. end
  351. if human.version and human.version == VoucherShopDefine.VOUCHER_NO_OPEN_TAG then
  352. return false
  353. end
  354. --print("[VoucherShop is open] 1111")
  355. if human.db.lv < VOUCHERSHOP_OPENLV then
  356. -- print("[VoucherShop is open] 22222")
  357. return false
  358. end
  359. if not human.db.nFirstBuy or human.db.nFirstBuy ~= 1 then
  360. --print("[VoucherShop is open] 33333")
  361. return false
  362. end
  363. --print("[VoucherShop is open] 444444")
  364. return true
  365. end
  366. -- 活动剩余时间
  367. function getLeftTime()
  368. return 9999999
  369. end
  370. -- 是否存在红点
  371. function isRed(human)
  372. return false
  373. end
  374. function onCharge(human, price, funcID, buyID)
  375. if human.db.lv >= VOUCHERSHOP_OPENLV and isOpen(human) then
  376. YunYingLogic.sendIconUpdate(VOUCHERSHOP_ACTID, human)
  377. end
  378. end
  379. function onLevelUp(human, oldLv, newLv, funcID)
  380. if true == isOpen(human) then
  381. YunYingLogic.sendIconUpdate(VOUCHERSHOP_ACTID, human)
  382. end
  383. end