VoucherShopLogic.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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 ipairs(VoucherShopDefine.INFLATIONPROBABILITY) do
  129. -- if nRandNum <= nID then
  130. -- nMultiple = v
  131. -- break
  132. -- end
  133. if nRandNum <= v[1] then
  134. nMultiple = v[2]
  135. break
  136. end
  137. end
  138. end
  139. nNum = nMultiple * nNum
  140. return nNum, nMultiple
  141. end
  142. ----------------------------------------- 其他模块调用 -------------------------------------
  143. -- 购买回调
  144. function VoucherShop_OnBuyVoucher(human, nBuyID)
  145. if not human or not nBuyID then
  146. return
  147. end
  148. -- 记录一下日志
  149. local sztext = " nBuyID = "..nBuyID
  150. VoucherShop_WriteLog(human, "[VoucherShop_OnBuyVoucher]", "玩家购买代金券回调记录开始 "..sztext)
  151. local tShopCof = VoucherShop_GetShopConfig()
  152. local tInflateConfig = VoucherShop_GetInflateConfig()
  153. if not tShopCof or not tInflateConfig then
  154. print("[VoucherShop_OnBuyVoucher] 获取配置失败 name = "..human.db.name..sztext)
  155. VoucherShop_WriteLog(human, "[VoucherShop_OnBuyVoucher]", "玩家购买代金券回调获取不到配置 "..sztext)
  156. return
  157. end
  158. local nAddNum, nMultiple = VoucherShop_GetAddNum(tShopCof[nBuyID] or tInflateConfig[nBuyID])
  159. if 0 >= nAddNum or 0 >= nMultiple then
  160. sztext = sztext.." nAddNum = "..nAddNum.." nMultiple = "..nMultiple
  161. print("[VoucherShop_OnBuyVoucher] 获取增加的数量和倍率失败 name = "..human.db.name..sztext)
  162. VoucherShop_WriteLog(human, "[VoucherShop_OnBuyVoucher]", "获取增加的数量和倍率失败 nBuyID = "..sztext)
  163. return
  164. end
  165. -- 给物品
  166. BagLogic.addItem(human, VoucherShopDefine.VOUCHERITME_ID, nAddNum, "voucher_use")
  167. -- 弹窗
  168. BagLogic.sendItemGetList(human, {{VoucherShopDefine.VOUCHERITME_ID, nAddNum}}, "voucher_use")
  169. if nMultiple > 1 then
  170. local szInflateText = "恭喜【"..human.db.name.."】通过刮刮乐获得"..nAddNum.."代金券"
  171. CommonDB.setVoucherInflate(szInflateText, os.time())
  172. VoucherShop_SetStatus(human, VoucherShopDefine.VOUCHERSTATUS_GET, os.time())
  173. VoucherShop_QueryInflate(human)
  174. end
  175. VoucherShop_WriteLog(human, "[VoucherShop_OnBuyVoucher]", "玩家购买代金券回调记录结束 "..sztext)
  176. TriggerLogic.PublishEvent(TriggerDefine.VOUCHER_BUY_NUM, human.db._id, nAddNum)
  177. end
  178. -- 定时回调
  179. function VoucherShop_OnZero()
  180. -- 遍历在线玩家
  181. for uuid, human in pairs(ObjHuman.onlineUuid) do
  182. VoucherShop_RestStatus(human)
  183. end
  184. end
  185. -- 玩家登录
  186. function onLogin(human)
  187. if not human then
  188. return
  189. end
  190. local tStatus = VoucherShop_GetStatus(human)
  191. if not tStatus then
  192. return
  193. end
  194. if tStatus.nStatus == VoucherShopDefine.VOUCHERSTATUS_GET then
  195. local nOldTime = tStatus.nTime
  196. local nNowTime = os.time()
  197. local sameDay = Util.isSameDayByTimes(nNowTime, nOldTime)
  198. if true ~= sameDay then
  199. VoucherShop_RestStatus(human)
  200. VoucherShop_WriteLog(human, "[VoucherShop_OnBuyVoucher]", "玩家登录重置购买状态 nOldTime = "..nOldTime.." nNowTime = "..nNowTime)
  201. end
  202. end
  203. end
  204. ----------------------------------------- 客户端请求 -------------------------------------
  205. -- 请求代金券商店信息
  206. function VoucherShop_QueryShop(human)
  207. if not human then
  208. return
  209. end
  210. local tConfig = VoucherShop_GetShopConfig()
  211. if not tConfig then
  212. VoucherShop_WriteLog(human, "[VoucherShop_QueryShop]", "获取商店配置失败")
  213. print("[VoucherShop_QueryShop] 获取商店配置失败")
  214. return
  215. end
  216. local tMsgData = Msg.gc.GC_VOUCHER_QUERY_SHOP
  217. --tMsgData.tVoucherData[0] = #tConfig
  218. local nIndex = 1
  219. for nBuyID, v in pairs(tConfig) do
  220. local tData = tMsgData.tVoucherData[nIndex]
  221. tData.nID = v.nID
  222. --tData.nGetVoucherNum = v.tData.nGetVoucherNum
  223. Grid.makeItem(tData.item, VoucherShopDefine.VOUCHERITME_ID, v.tData.nGetVoucherNum)
  224. print("[VoucherShop_QueryShop] nID = "..tData.nID.." nGetVoucherNum = "..v.tData.nGetVoucherNum)
  225. BuyLogic.fontBuyItem(human, tData.tBuyItem, nBuyID)
  226. nIndex = nIndex +1
  227. end
  228. print("[VoucherShop_QueryShop] nIndex = "..nIndex)
  229. tMsgData.tVoucherData[0] = nIndex - 1
  230. Msg.send(tMsgData, human.fd)
  231. end
  232. -- 请求代金券膨胀信息
  233. function VoucherShop_QueryInflate(human)
  234. if not human then
  235. return
  236. end
  237. local tConfig = VoucherShop_GetInflateConfig()
  238. if not tConfig then
  239. VoucherShop_WriteLog(human, "[VoucherShop_QueryInflate]", "获取膨胀配置失败")
  240. print("[VoucherShop_QueryInflate] 获取膨胀配置失败")
  241. return
  242. end
  243. local tStatus = VoucherShop_GetStatus(human)
  244. if not tStatus then
  245. VoucherShop_WriteLog(human, "[VoucherShop_QueryInflate]", "获取状态信息失败")
  246. print("[VoucherShop_QueryInflate] 获取状态信息失败")
  247. return
  248. end
  249. local tMsgData = Msg.gc.GC_VOUCHER_QUERY_INFLATE
  250. tMsgData.nStatus = tStatus.nStatus
  251. --tMsgData.tVoucherData[0] = #tConfig
  252. local nIndex = 1
  253. for nBuyID, v in pairs(tConfig) do
  254. local tData = tMsgData.tVoucherData[nIndex]
  255. tData.nID = v.nID
  256. Grid.makeItem(tData.item, VoucherShopDefine.VOUCHERITME_ID, v.tData.nGetVoucherNum)
  257. BuyLogic.fontBuyItem(human, tData.tBuyItem, nBuyID)
  258. nIndex = nIndex +1
  259. end
  260. tMsgData.tVoucherData[0] = nIndex - 1
  261. local szTextInfo = CommonDB.getVoucherInflate()
  262. tMsgData.tVoucherText[0] = 0
  263. if szTextInfo then
  264. local nLen = #szTextInfo
  265. tMsgData.tVoucherText[0] = nLen > 20 and 20 or nLen
  266. for i, v in pairs(szTextInfo) do
  267. if i > 20 then
  268. break
  269. end
  270. tMsgData.tVoucherText[i] = v.szText
  271. print("[VoucherShop_QueryInflate] szText = "..v.szText)
  272. end
  273. end
  274. table.print_lua_table(tMsgData.tVoucherText)
  275. print("[VoucherShop_QueryInflate] nIndex = "..nIndex)
  276. Msg.send(tMsgData, human.fd)
  277. end
  278. -- 请求代金券购买商品
  279. function VoucherShop_BuyItem(human, nBuyID)
  280. if not human or 0 >= nBuyID then
  281. return
  282. end
  283. local tBuyConfig = VoucherShop_GetShopConfig()
  284. local tInflateConfig = VoucherShop_GetInflateConfig()
  285. if not tBuyConfig or not tInflateConfig then
  286. return
  287. end
  288. if tBuyConfig[nBuyID] or tInflateConfig[nBuyID] then
  289. print("[VoucherShop_BuyItem] 玩家使用代金券购买代金券礼品,直接返回")
  290. VoucherShop_WriteLog(human, "[VoucherShop_BuyItem]", "玩家使用代金券购买代金券礼品,直接返回")
  291. return
  292. end
  293. local tTrueBuyConfig = BuyExcel.buy[nBuyID]
  294. if not tTrueBuyConfig then
  295. print("[VoucherShop_BuyItem] 不存在对应的商品信息 nBuyID = "..nBuyID)
  296. return
  297. end
  298. local nDelVoucherNum = tTrueBuyConfig.Voucher
  299. if 0 >= nDelVoucherNum then
  300. print("[VoucherShop_BuyItem] 该商品无法用代金券购买,配置的数量为空 nBuyID = "..nBuyID.." nDelVoucherNum = "..nDelVoucherNum)
  301. return
  302. end
  303. -- 删物品
  304. BagLogic.delItem(human, VoucherShopDefine.VOUCHERITME_ID, nDelVoucherNum, "voucher_use")
  305. -- 发道具
  306. local tBuyInfo =
  307. {
  308. buyID = nBuyID,
  309. price = tTrueBuyConfig.CN
  310. }
  311. BuyLogic.buy(human, tBuyInfo)
  312. -- local voucherDenomination = tTrueBuyConfig.CN
  313. -- if voucherDenomination >= 100 then
  314. -- TopupLogic.sendFireworksMail(human, nDelVoucherNum)
  315. -- end
  316. YunYingLogic.sendIconUpdate(VOUCHERSHOP_ACTID, human)
  317. -- 写日志
  318. local szText = "玩家购买使用代金券购买了物品 nBuyID = "..nBuyID.." 消耗代金券数量 nDelVoucherNum = "..nDelVoucherNum
  319. VoucherShop_WriteLog(human, "[VoucherShop_BuyItem]", szText)
  320. end
  321. ----------------------------------------- 活动模板调用 -------------------------------------
  322. -- 是否开启
  323. function isOpen(human, YYInfo, funcConfig)
  324. if human.version and human.version == VoucherShopDefine.VOUCHER_SP_PLATFORM then
  325. return true
  326. end
  327. --print("[VoucherShop is open] 1111")
  328. if human.db.lv < VOUCHERSHOP_OPENLV then
  329. -- print("[VoucherShop is open] 22222")
  330. return false
  331. end
  332. if not human.db.nFirstBuy or human.db.nFirstBuy ~= 1 then
  333. --print("[VoucherShop is open] 33333")
  334. return false
  335. end
  336. --print("[VoucherShop is open] 444444")
  337. return true
  338. end
  339. -- 活动剩余时间
  340. function getLeftTime()
  341. return 9999999
  342. end
  343. -- 是否存在红点
  344. function isRed(human)
  345. return false
  346. end
  347. function onCharge(human, price, funcID, buyID)
  348. if human.db.lv >= VOUCHERSHOP_OPENLV then
  349. YunYingLogic.sendIconUpdate(VOUCHERSHOP_ACTID, human)
  350. end
  351. end
  352. function onLevelUp(human, oldLv, newLv, funcID)
  353. if true == isOpen(human) then
  354. YunYingLogic.sendIconUpdate(VOUCHERSHOP_ACTID, human)
  355. end
  356. end