VoucherShopLogic.lua 15 KB

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