VoucherShopLogic.lua 16 KB

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