ServerCommerceActShop.lua 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. --------------------------------
  2. -- 文件名 : ServerCommerceActShop.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 BuyLogic = require("topup.BuyLogic")
  12. local CommonDefine = require("common.CommonDefine")
  13. local ServerCommerceConf = require("excel.ServerCommerce")
  14. local BuyConf = require("excel.buy")
  15. local CommonDB = require("common.CommonDB")
  16. local ObjHuman = require("core.ObjHuman")
  17. local ServerCommerceManager = require("serverCommerce.ServerCommerceManager")
  18. local ServerCommerceDefine = require("serverCommerce.ServerCommerceActDefine")
  19. local COMMERCEACTFREEID = 1 -- 免费
  20. local COMMERCEACTPAYID = 2 -- 付费
  21. ----------------------------------------- 内部处理开始 -------------------------------------
  22. -- 获取配置
  23. local function CommerceActShop_GetCof()
  24. return ServerCommerceConf.CommerceShop
  25. end
  26. function CreatDB(human)
  27. if not human.db.ServerCommerce or not human.db.ServerCommerce.Shop then
  28. print("[CommerceActShop_CreatDB] 初始化数据失败 name = "..human.db.name)
  29. return false
  30. end
  31. local tConf = CommerceActShop_GetCof()
  32. for nID, v in ipairs(tConf) do
  33. human.db.ServerCommerce.Shop[nID] =
  34. {
  35. [COMMERCEACTFREEID] = CommonDefine.COMMON_PRIZE_STATE_NOGET,
  36. [COMMERCEACTPAYID] = ServerCommerceDefine.COMMERCEACT_SHOP_PAY_STATE0
  37. }
  38. end
  39. return true
  40. end
  41. -- 获取免费奖励状态
  42. local function CommerceActShop_GetFreeStatus(human, nID)
  43. return human.db.ServerCommerce.Shop[nID][COMMERCEACTFREEID]
  44. end
  45. -- 设置免费奖励状态
  46. local function CommerceActShop_SetFreeStatus(human, nID, nStatus)
  47. human.db.ServerCommerce.Shop[nID][COMMERCEACTFREEID] = nStatus
  48. end
  49. -- 获取付费奖励状态
  50. local function CommerceActShop_GetPayStatus(human, nID)
  51. return human.db.ServerCommerce.Shop[nID][COMMERCEACTPAYID]
  52. end
  53. -- 设置付费奖励状态
  54. local function CommerceActShop_SetPayStatus(human, nID, nStatus)
  55. human.db.ServerCommerce.Shop[nID][COMMERCEACTPAYID] = nStatus
  56. end
  57. ----------------------------------------- 客户端请求处理开始 -------------------------------------
  58. -- 查询数据
  59. function CommerceActShop_Query(human)
  60. local tConf = CommerceActShop_GetCof()
  61. local tMsgData = Msg.gc.GC_SERVEERCOMMERCE_ACT_SHOPQUERY
  62. tMsgData.nNowPoint = CommonDB.GetCommerceActInfo_Point()
  63. tMsgData.list[0] = #tConf
  64. local nLen = 1
  65. for nID, v in ipairs(tConf) do
  66. local tNodeData = tMsgData.list[nLen]
  67. nLen = nLen + 1
  68. tNodeData.nID = nID
  69. tNodeData.nNeedPoint = v.nPoint
  70. tNodeData.nFreeState = CommerceActShop_GetFreeStatus(human, nID)
  71. tNodeData.nPayState = CommerceActShop_GetPayStatus(human, nID)
  72. -- 免费物品
  73. tNodeData.tFreeItem[0] = #v.FreePrize
  74. for i, tData in ipairs(v.FreePrize) do
  75. Grid.makeItem(tNodeData.tFreeItem[i], tData[1], tData[2])
  76. end
  77. tNodeData.tPayItem[0] = #v.PayPrize
  78. for i, tData in ipairs(v.PayPrize) do
  79. Grid.makeItem(tNodeData.tPayItem[i], tData[1], tData[2])
  80. end
  81. -- 封装购买项物品
  82. BuyLogic.fontBuyItem(human, tNodeData.buyItem, v.nBuyID)
  83. --Grid.makeItem(tNodeData.pointItem, ServerCommerceDefine.COMERCEACT_POINT_GOODSID, v.nPoint)
  84. end
  85. Msg.send(tMsgData, human.fd)
  86. end
  87. -- 领取奖励
  88. function CommerceActShop_GetPrize(human, nType)
  89. local tConf = CommerceActShop_GetCof()
  90. local tGoods = {}
  91. for nID, v in ipairs(tConf) do
  92. local nState = CommerceActShop_GetFreeStatus(human, nID)
  93. local bCanGet = false
  94. if COMMERCEACTPAYID == nType then
  95. nState = CommerceActShop_GetPayStatus(human, nID)
  96. bCanGet = ServerCommerceDefine.COMMERCEACT_SHOP_PAY_STATE3 == nState
  97. else
  98. bCanGet = CommonDefine.COMMON_PRIZE_STATE_CANGET == nState
  99. end
  100. if true == bCanGet then
  101. local tPrize
  102. if COMMERCEACTPAYID == nType then
  103. CommerceActShop_SetPayStatus(human, nID, ServerCommerceDefine.COMMERCEACT_SHOP_PAY_STATE4)
  104. tPrize = v.PayPrize
  105. else
  106. CommerceActShop_SetFreeStatus(human, nID, CommonDefine.COMMON_PRIZE_STATE_GET)
  107. tPrize = v.FreePrize
  108. end
  109. for _, tData in ipairs(tPrize) do
  110. tGoods[tData[1]] = tGoods[tData[1]] or 0
  111. tGoods[tData[1]] = tGoods[tData[1]] + tData[2]
  112. end
  113. end
  114. end
  115. if nil ~= _G.next(tGoods) then
  116. local tItem = {}
  117. for nGoodsID, nGoodsNum in pairs(tGoods) do
  118. table.insert(tItem, {nGoodsID, nGoodsNum})
  119. end
  120. BagLogic.addItemList(human, tItem, "servercommerce")
  121. CommerceActShop_Query(human)
  122. ServerCommerceManager.CommerceAct_SendActInfo(human)
  123. end
  124. end
  125. ----------------------------------------- 外部调用 -------------------------------------
  126. -- 总积分改变
  127. function onAllPointChange()
  128. local nNowPoint = CommonDB.GetCommerceActInfo_Point()
  129. local tConf = CommerceActShop_GetCof()
  130. local tOKID = {}
  131. for nID, v in ipairs(tConf) do
  132. if nNowPoint >= v.nPoint then
  133. table.insert(tOKID, nID)
  134. end
  135. end
  136. for _, human in pairs(ObjHuman.onlineUuid) do
  137. local bSendClient = false
  138. for _, nID in ipairs(tOKID) do
  139. local nStatus = CommerceActShop_GetFreeStatus(human, nID)
  140. if CommonDefine.COMMON_PRIZE_STATE_NOGET == nStatus then
  141. bSendClient = true
  142. CommerceActShop_SetFreeStatus(human, nID, CommonDefine.COMMON_PRIZE_STATE_CANGET)
  143. end
  144. nStatus = CommerceActShop_GetPayStatus(human, nID)
  145. if ServerCommerceDefine.COMMERCEACT_SHOP_PAY_STATE0 == nStatus then
  146. -- 变成积分足够,未充值状态
  147. CommerceActShop_SetPayStatus(human, nID, ServerCommerceDefine.COMMERCEACT_SHOP_PAY_STATE1)
  148. elseif ServerCommerceDefine.COMMERCEACT_SHOP_PAY_STATE2 == nStatus then
  149. -- 已经是购买状态,积分足够,直接变成可领取
  150. CommerceActShop_SetPayStatus(human, nID, ServerCommerceDefine.COMMERCEACT_SHOP_PAY_STATE3)
  151. end
  152. end
  153. if true == bSendClient then
  154. CommerceActShop_Query(human)
  155. end
  156. end
  157. end
  158. function onCharge(human, price, funcID, buyID)
  159. local tConf = CommerceActShop_GetCof()
  160. local nNowPoint = CommonDB.GetCommerceActInfo_Point()
  161. print("[CommerceActShop_onCharge] nNowPoint = "..nNowPoint)
  162. local nChoseID = 0
  163. local tConfData = nil
  164. for nID, v in ipairs(tConf) do
  165. if v.nBuyID == buyID then
  166. local nPayState = CommerceActShop_GetPayStatus(human, nID)
  167. -- 状态为0 或者状态为积分达成未购买
  168. if ServerCommerceDefine.COMMERCEACT_SHOP_PAY_STATE0 == nPayState or ServerCommerceDefine.COMMERCEACT_SHOP_PAY_STATE1 == nPayState then
  169. nChoseID = nID
  170. tConfData = v
  171. else
  172. print("[CommerceActShop_onCharge] 重复购买了 nBuyID = "..v.nBuyID)
  173. end
  174. break
  175. end
  176. end
  177. local tBuyCfg = BuyConf.buy[buyID]
  178. if not tBuyCfg then
  179. print("[onCharge] 不存在对应的购买项配置 buyID = "..buyID)
  180. return
  181. end
  182. if nChoseID == 0 or nil == tConfData then
  183. return
  184. end
  185. if nNowPoint >= tConfData.nPoint then
  186. CommerceActShop_SetPayStatus(human, nChoseID, ServerCommerceDefine.COMMERCEACT_SHOP_PAY_STATE3)
  187. else
  188. CommerceActShop_SetPayStatus(human, nChoseID, ServerCommerceDefine.COMMERCEACT_SHOP_PAY_STATE2)
  189. end
  190. CommerceActShop_Query(human)
  191. ServerCommerceManager.CommerceAct_SendActInfo(human)
  192. end
  193. -- 登录检测积分
  194. function onLogin(human)
  195. local nNowPoint = CommonDB.GetCommerceActInfo_Point()
  196. local tConf = CommerceActShop_GetCof()
  197. for nID, v in ipairs(tConf) do
  198. if nNowPoint >= v.nPoint then
  199. local nStatus = CommerceActShop_GetFreeStatus(human, nID)
  200. if CommonDefine.COMMON_PRIZE_STATE_NOGET == nStatus then
  201. CommerceActShop_SetFreeStatus(human, nID, CommonDefine.COMMON_PRIZE_STATE_CANGET)
  202. end
  203. end
  204. end
  205. end
  206. function isRed(human)
  207. local tConf = CommerceActShop_GetCof()
  208. for nID, v in ipairs(tConf) do
  209. local nFreeStatus = CommerceActShop_GetFreeStatus(human, nID)
  210. local nPayState = CommerceActShop_GetPayStatus(human, nID)
  211. if CommonDefine.COMMON_PRIZE_STATE_CANGET == nFreeStatus or
  212. ServerCommerceDefine.COMMERCEACT_SHOP_PAY_STATE3 == nPayState then
  213. return true
  214. end
  215. end
  216. return false
  217. end