VoucherShopLogic.lua 12 KB

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