VoucherShopLogic.lua 13 KB

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