ServerCommerceActCharge.lua 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. --------------------------------
  2. -- 文件名 : ServerCommerceActCharge.lua
  3. -- 文件说明 : 跨服商业-连充豪礼
  4. -- 创建时间 : 2025/03/31
  5. -- 创建人 : FC
  6. --------------------------------
  7. local Util = require("common.Util")
  8. local Msg = require("core.Msg")
  9. local Grid = require("bag.Grid")
  10. local BagLogic = require("bag.BagLogic")
  11. local CommonDefine = require("common.CommonDefine")
  12. local ServerCommerceConf = require("excel.ServerCommerce")
  13. local ServerCommerceManager = require("serverCommerce.ServerCommerceManager")
  14. -- 金额映射红点下标
  15. local tChargeMoney2Index =
  16. {
  17. [6] = 1,
  18. [98] = 2,
  19. [198] = 3,
  20. [1] = 1,
  21. [17] = 2,
  22. [33] = 3,
  23. [0.99] = 1,
  24. [14.99] = 2,
  25. [27.99] = 3,
  26. }
  27. ----------------------------------------- 内部处理开始 -------------------------------------
  28. -- 获取配置
  29. local function CommerceActCharge_GetCof()
  30. return ServerCommerceConf.CommerceCharge
  31. end
  32. -- 初始化DB数据()
  33. function CreatDB(human)
  34. if not human.db.ServerCommerce or not human.db.ServerCommerce.Charge then
  35. print("[CommerceActCharge_CreatDB] 初始化数据失败 name = "..human.db.name)
  36. return false
  37. end
  38. human.db.ServerCommerce.Charge =
  39. {
  40. nChargeTime = 0,
  41. tPrizeData = {}
  42. }
  43. local tPrizeData = human.db.ServerCommerce.Charge.tPrizeData
  44. local tConf = CommerceActCharge_GetCof()
  45. for _, v in ipairs(tConf) do
  46. if not tPrizeData[v.nDay] then
  47. tPrizeData[v.nDay] = {
  48. nChargeMoney = 0,
  49. tMoney = {}
  50. }
  51. end
  52. local tNodeData = tPrizeData[v.nDay].tMoney
  53. if not tNodeData[v.nMoney] then
  54. tNodeData[v.nMoney] = CommonDefine.COMMON_PRIZE_STATE_NOGET
  55. else
  56. print("[CommerceActCharge_CreateDB] 配置了重复的数据")
  57. end
  58. end
  59. print("[CommerceActTask_CreatDB] 连充豪礼 初始化数据完成 name = "..human.db.name)
  60. return true
  61. end
  62. -- 获取第一次充值的时间
  63. local function CommerceActCharge_GetChargeTime(human)
  64. return human.db.ServerCommerce.Charge.nChargeTime
  65. end
  66. -- 设置充值时间
  67. local function CommerceActCharge_SetChargeTime(human, nTime)
  68. human.db.ServerCommerce.Charge.nChargeTime = nTime
  69. end
  70. -- 获取DB奖励数据
  71. local function CommerceActCharge_GetDBPrizeData(human)
  72. return human.db.ServerCommerce.Charge.tPrizeData
  73. end
  74. -- 获取DB奖励状态
  75. local function CommerceActCharge_GetDBPrizeStatus(human, nDay, nMoney)
  76. local tPrizeData = CommerceActCharge_GetDBPrizeData(human)
  77. if not tPrizeData[nDay] or not tPrizeData[nDay].tMoney[nMoney] then
  78. return CommonDefine.COMMON_PRIZE_STATE_NOGET
  79. end
  80. return tPrizeData[nDay].tMoney[nMoney]
  81. end
  82. -- 设置DB奖励状态
  83. local function CommerceActCharge_SetDBPrizeStatus(human, nDay, nMoney, nStatus)
  84. local tPrizeData = CommerceActCharge_GetDBPrizeData(human)
  85. tPrizeData[nDay].tMoney[nMoney] = nStatus
  86. end
  87. -- 获取DB天数充值金额
  88. local function CommerceActCharge_GetDBDayCharge(human, nDay)
  89. local tPrizeData = CommerceActCharge_GetDBPrizeData(human)
  90. if not tPrizeData[nDay] then
  91. return 0
  92. end
  93. return tPrizeData[nDay].nChargeMoney
  94. end
  95. -- 设置DB天数充值金额
  96. local function CommerceActCharge_SetDBDayCharge(human, nDay, nMoney)
  97. local tPrizeData = CommerceActCharge_GetDBPrizeData(human)
  98. tPrizeData[nDay].nChargeMoney = tPrizeData[nDay].nChargeMoney + nMoney
  99. end
  100. ----------------------------------------- 客户端请求处理开始 -------------------------------------
  101. -- 请求数据
  102. function CommerceActCharge_Query(human, nMoney)
  103. local tConf = CommerceActCharge_GetCof()
  104. local tMsgData = Msg.gc.GC_SERVEERCOMMERCE_ACT_CHARGEQUERY
  105. local tMoney = {}
  106. local tRed = tMsgData.tRed
  107. tRed[0] = 0
  108. tMsgData.list[0] = 0
  109. tMsgData.nMoney = nMoney
  110. local nChargeDay, nLen = 0, 0
  111. for nID, v in ipairs(tConf) do
  112. if v.nMoney == nMoney then
  113. nLen = nLen + 1
  114. tMsgData.list[0] = nLen
  115. local tPrizeData = tMsgData.list[nLen]
  116. tPrizeData.nID = nID
  117. tPrizeData.nState = CommerceActCharge_GetDBPrizeStatus(human, v.nDay, nMoney)
  118. if tPrizeData.nState ~= CommonDefine.COMMON_PRIZE_STATE_NOGET then
  119. nChargeDay = nChargeDay + 1
  120. end
  121. tPrizeData.nDay = v.nDay
  122. tPrizeData.nAllMoney = CommerceActCharge_GetDBDayCharge(human, v.nDay)
  123. tPrizeData.item[0] = #v.Prize
  124. for nIndex, tItem in ipairs(v.Prize) do
  125. local nGoodsID = tItem[1]
  126. local nGoodsNum = tItem[2]
  127. Grid.makeItem(tPrizeData.item[nIndex], nGoodsID, nGoodsNum)
  128. end
  129. end
  130. if not tMoney[v.nMoney] then
  131. tMoney[v.nMoney] = 1
  132. tRed[0] = tRed[0] + 1
  133. tRed[tChargeMoney2Index[v.nMoney]] = 0
  134. end
  135. if tRed[tChargeMoney2Index[v.nMoney]] == 0 then
  136. local nState = CommerceActCharge_GetDBPrizeStatus(human, v.nDay, v.nMoney)
  137. if nState == CommonDefine.COMMON_PRIZE_STATE_CANGET then
  138. tRed[tChargeMoney2Index[v.nMoney]] = 1
  139. end
  140. end
  141. end
  142. tMsgData.nChargeDay = nChargeDay
  143. Msg.send(tMsgData, human.fd)
  144. end
  145. -- 请求领取奖励
  146. function CommerceActCharge_GetPrize(human, nMoney)
  147. local tConf = CommerceActCharge_GetCof()
  148. local tItem = {}
  149. for nID, v in ipairs(tConf) do
  150. if v.nMoney == nMoney then
  151. local nState = CommerceActCharge_GetDBPrizeStatus(human, v.nDay, v.nMoney)
  152. if CommonDefine.COMMON_PRIZE_STATE_CANGET == nState then
  153. CommerceActCharge_SetDBPrizeStatus(human, v.nDay, nMoney, CommonDefine.COMMON_PRIZE_STATE_GET)
  154. for _, tPrize in ipairs(v.Prize) do
  155. if not tItem[tPrize[1]] then
  156. tItem[tPrize[1]] = 0
  157. end
  158. tItem[tPrize[1]] = tItem[tPrize[1]] + tPrize[2]
  159. end
  160. end
  161. end
  162. end
  163. local tPrize = {}
  164. for nID, nNum in pairs(tItem) do
  165. table.insert(tPrize, {nID, nNum})
  166. end
  167. BagLogic.addItemList(human, tPrize, "servercommerce")
  168. CommerceActCharge_Query(human, nMoney)
  169. ServerCommerceManager.CommerceAct_SendActInfo(human)
  170. end
  171. ----------------------------------------- 外部调用 -------------------------------------
  172. function isRed(human)
  173. if not human.db.ServerCommerce or not human.db.ServerCommerce.Charge then
  174. return false
  175. end
  176. local tConf = CommerceActCharge_GetCof()
  177. for _, v in ipairs(tConf) do
  178. local nState = CommerceActCharge_GetDBPrizeStatus(human, v.nDay, v.nMoney)
  179. if nState == CommonDefine.COMMON_PRIZE_STATE_CANGET then
  180. return true
  181. end
  182. end
  183. return false
  184. end
  185. function onCharge(human, price, funcID, buyID)
  186. local nBuyTime = CommerceActCharge_GetChargeTime(human)
  187. if 0 == nBuyTime then
  188. nBuyTime = os.time()
  189. CommerceActCharge_SetChargeTime(human, nBuyTime)
  190. end
  191. local nDiffDay = Util.diffDay(nBuyTime) + 1
  192. local tConf = CommerceActCharge_GetCof()
  193. local bQuery = false
  194. local nNewChareNum = CommerceActCharge_GetDBDayCharge(human, nDiffDay)
  195. nNewChareNum = nNewChareNum + price
  196. for _, v in ipairs(tConf) do
  197. if v.nDay == nDiffDay then
  198. local nStatus = CommerceActCharge_GetDBPrizeStatus(human, v.nDay, v.nMoney)
  199. if nStatus == CommonDefine.COMMON_PRIZE_STATE_NOGET and nNewChareNum >= v.nMoney then
  200. CommerceActCharge_SetDBPrizeStatus(human, v.nDay, v.nMoney, CommonDefine.COMMON_PRIZE_STATE_CANGET)
  201. bQuery = true
  202. end
  203. end
  204. end
  205. CommerceActCharge_SetDBDayCharge(human, nDiffDay, price)
  206. if true == bQuery then
  207. CommerceActCharge_Query(human, price)
  208. ServerCommerceManager.CommerceAct_SendActInfo(human)
  209. end
  210. end