VoucherShopLogic.lua 13 KB

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