VoucherShopLogic.lua 16 KB

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