VoucherShopLogic.lua 12 KB

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