ServerCommerceActShop.lua 7.7 KB

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