ServerCommerceActShop.lua 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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, nType)
  87. local tConf = CommerceActShop_GetCof()
  88. local tGoods = {}
  89. for nID, v in ipairs(tConf) do
  90. local nState = CommerceActShop_GetFreeStatus(human, nID)
  91. if COMMERCEACTPAYID == nType then
  92. nState = CommerceActShop_GetPayStatus(human, nID)
  93. end
  94. if CommonDefine.COMMON_PRIZE_STATE_CANGET == nState then
  95. local tPrize
  96. if COMMERCEACTPAYID == nType then
  97. CommerceActShop_SetPayStatus(human, nID, CommonDefine.COMMON_PRIZE_STATE_GET)
  98. tPrize = v.PayPrize
  99. else
  100. CommerceActShop_SetFreeStatus(human, nID, CommonDefine.COMMON_PRIZE_STATE_GET)
  101. tPrize = v.FreePrize
  102. end
  103. for _, tData in ipairs(tPrize) do
  104. tGoods[tData[1]] = tGoods[tData[1]] or 0
  105. tGoods[tData[1]] = tGoods[tData[1]] + tData[2]
  106. end
  107. end
  108. end
  109. if nil ~= _G.next(tGoods) then
  110. local tItem = {}
  111. for nGoodsID, nGoodsNum in pairs(tGoods) do
  112. table.insert(tItem, {nGoodsID, nGoodsNum})
  113. end
  114. BagLogic.addItemList(human, tItem, "servercommerce")
  115. CommerceActShop_Query(human)
  116. end
  117. end
  118. ----------------------------------------- 外部调用 -------------------------------------
  119. -- 总积分改变
  120. function onAllPointChange()
  121. local nNowPoint = CommonDB.GetCommerceActInfo_Point()
  122. local tConf = CommerceActShop_GetCof()
  123. local tOKID = {}
  124. for nID, v in ipairs(tConf) do
  125. if nNowPoint >= v.nPoint then
  126. table.insert(tOKID, nID)
  127. end
  128. end
  129. for _, human in pairs(ObjHuman.onlineUuid) do
  130. local bSendClient = false
  131. for _, nID in ipairs(tOKID) do
  132. local nStatus = CommerceActShop_GetFreeStatus(human, nID)
  133. if CommonDefine.COMMON_PRIZE_STATE_NOGET == nStatus then
  134. bSendClient = true
  135. CommerceActShop_SetFreeStatus(human, nID, CommonDefine.COMMON_PRIZE_STATE_CANGET)
  136. end
  137. end
  138. if true == bSendClient then
  139. CommerceActShop_Query(human)
  140. end
  141. end
  142. end
  143. function onCharge(human, price, funcID, buyID)
  144. local tConf = CommerceActShop_GetCof()
  145. local nChoseID = 0
  146. for nID, v in ipairs(tConf) do
  147. if v.nBuyID == buyID then
  148. local nPayState = CommerceActShop_GetPayStatus(human, nID)
  149. if CommonDefine.COMMON_PRIZE_STATE_NOGET == nPayState then
  150. nChoseID = nID
  151. else
  152. print("[CommerceActShop_onCharge] 重复购买了 nBuyID = "..v.nBuyID)
  153. end
  154. break
  155. end
  156. end
  157. local tBuyCfg = BuyConf.buy[buyID]
  158. if not tBuyCfg then
  159. print("[onCharge] 不存在对应的购买项配置 buyID = "..buyID)
  160. return
  161. end
  162. if nChoseID == 0 then
  163. return
  164. end
  165. CommerceActShop_SetPayStatus(human, nChoseID, CommonDefine.COMMON_PRIZE_STATE_CANGET)
  166. CommerceActShop_Query(human)
  167. end
  168. -- 登录检测积分
  169. function onLogin(human)
  170. local nNowPoint = CommonDB.GetCommerceActInfo_Point()
  171. local tConf = CommerceActShop_GetCof()
  172. for nID, v in ipairs(tConf) do
  173. if nNowPoint >= v.nPoint then
  174. local nStatus = CommerceActShop_GetFreeStatus(human, nID)
  175. if CommonDefine.COMMON_PRIZE_STATE_NOGET == nStatus then
  176. CommerceActShop_SetFreeStatus(human, nID, CommonDefine.COMMON_PRIZE_STATE_CANGET)
  177. end
  178. end
  179. end
  180. end
  181. function isRed(human)
  182. local tConf = CommerceActShop_GetCof()
  183. for nID, v in ipairs(tConf) do
  184. local nFreeStatus = CommerceActShop_GetFreeStatus(human, nID)
  185. local nPayState = CommerceActShop_GetPayStatus(human, nID)
  186. if CommonDefine.COMMON_PRIZE_STATE_CANGET == nFreeStatus or
  187. CommonDefine.COMMON_PRIZE_STATE_CANGET == nPayState then
  188. return true
  189. end
  190. end
  191. return false
  192. end