ServerCommerceActShop.lua 9.5 KB

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