Recharge.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. ---
  2. --- Generated by EmmyLua(https://github.com/EmmyLua)
  3. --- Created by 無心道(15388152619).
  4. --- DateTime: 2024/10/29 13:50
  5. ---
  6. Recharge = {}
  7. local this = {}
  8. ---相当于前端请求用代金卷充值
  9. function Recharge.request(actor, msgData)
  10. local rechargeId = msgData["rechargeId"]
  11. local count = tonumber(msgData["count"] or 1)
  12. gameDebug.assertTrue(1 <= count and count < 9999, "购买数量异常:", count)
  13. local cfg_recharge = ConfigDataManager.getById("cfg_recharge", rechargeId)
  14. gameDebug.assertNil(cfg_recharge, "充值表找不到配置:", rechargeId)
  15. local cfgAmount = string.tonumber(cfg_recharge.amount)
  16. local costMap = {}
  17. local amount = 0;
  18. local costSource
  19. if cfgAmount > 0 then
  20. --查找代币的道具id
  21. local replaceItemId = ConfigDataManager.getTableValue(
  22. "cfg_item", "id",
  23. "type", ItemType.DRUG,
  24. "subType", ItemSubType.REPLACEMONEY
  25. )
  26. gameDebug.assertNil(replaceItemId, "道具表代币配置查找失败:", "type", ItemType.DRUG, "subType", ItemSubType.REPLACEMONEY)
  27. amount = math.floor(cfgAmount * count)
  28. costMap[replaceItemId] = amount
  29. local name = ConfigDataManager.getTableValue("cfg_item", "name", "id", replaceItemId)
  30. costSource = "购买道具[" .. name .. "]消耗"
  31. else
  32. --扣除消耗道具 无需设置价格,消耗其他道具,价格就是0
  33. local costStr = cfg_recharge["item"]
  34. if not string.isNullOrEmpty(costStr) then
  35. string.putIntIntMap(costMap, costStr, "#", "|")
  36. local itemNames
  37. for itemid, c in pairs(costMap) do
  38. local name = ConfigDataManager.getTableValue("cfg_item", "name", "id", itemid)
  39. itemNames = name .. "|"
  40. end
  41. itemNames = string.sub(itemNames, 1, -2)
  42. costSource = string.format("购买道具[%s]消耗", itemNames)
  43. end
  44. end
  45. if table.notNullOrEmpty(costMap) then
  46. local checkResult = Bag.checkCostMapBind(actor, costMap)
  47. if not checkResult then
  48. --背包道具不足
  49. noticeTip.noticeinfo(actor, StringIdConst.TEXT346)
  50. return
  51. end
  52. Bag.costMapBind(actor, costMap, costSource)
  53. end
  54. local ext = msgData["extraParams"] or "";
  55. this.OnRecharge0(actor, cfg_recharge, count, amount, ext)
  56. end
  57. function Recharge.OnRecharge(actor, rechargeData)
  58. info(actor, "收到充值请求", "actor", actor, "充值事件", rechargeData)
  59. local rechargeId = tonumber(rechargeData["goodsid"])
  60. local count = tonumber(rechargeData["count"] or 1);
  61. gameDebug.assertTrue(1 <= count and count < 9999, "购买数量异常:", rechargeData)
  62. local amount = tonumber(rechargeData["amount"]);
  63. local cfg_recharge = ConfigDataManager.getById("cfg_recharge", rechargeId)
  64. gameDebug.assertNil(cfg_recharge, "充值表找不到配置:", rechargeId)
  65. local cfg_amount = tonumber(cfg_recharge["amount"] or 0)
  66. --if rechargeData["platformId"] == "11" then
  67. --暂时需求是所有渠道都要验证金额
  68. gameDebug.assertTrue(cfg_amount * count == amount, "充值金额异常", rechargeData)
  69. --end
  70. local ext = rechargeData["extraParams"] or "";
  71. this.OnRecharge0(actor, cfg_recharge, count, amount, ext)
  72. end
  73. function this.OnRecharge0(actor, cfg_recharge, count, amount, ext)
  74. local nowScc = getbaseinfo("nowsec")
  75. if count < 1 then
  76. return
  77. end
  78. -- 充值次数限制
  79. if not Recharge.checkCount(actor, cfg_recharge, count) then
  80. return
  81. end
  82. local rechargeRecord = RechargeRecord.get(actor)
  83. --充值记录
  84. rechargeRecord:addRecord(actor, cfg_recharge["id"], amount, count, nowScc)
  85. --保存数据
  86. rechargeRecord:save(actor)
  87. --充值数据更新通知客户端
  88. RechargeRecord.sendMsg(actor)
  89. -- 充值次数计数
  90. CountManager.count(actor, cfg_recharge["countkey"], count)
  91. local reward_item = {}
  92. --触发充值类型事件监听
  93. RechargeEventListerTable:triggerEvent(cfg_recharge["type"], actor, cfg_recharge, count, amount, ext, reward_item)
  94. --触发默认事件监听
  95. RechargeEventListerTable:triggerEvent("0", actor, cfg_recharge, count, amount, ext, reward_item)
  96. local tmp = string.toIntIntMap(cfg_recharge.gain, "#", "|")
  97. for i = 1, count do
  98. table.mergeTable(reward_item, tmp)
  99. end
  100. if table.notNullOrEmpty(reward_item) then
  101. info("充值", actor, cfg_recharge["id"], cfg_recharge["name"], "奖励", reward_item)
  102. --奖励进入背包
  103. Bag.sendRewards(actor, reward_item, "充值奖励")
  104. end
  105. end
  106. function Recharge.checkCount(actor, cfg_recharge, count)
  107. if cfg_recharge["countkey"] == "" then
  108. return true
  109. else
  110. local c = CountManager.getCount(actor, cfg_recharge["countkey"])
  111. if c and c:canAdd(count) then
  112. return true
  113. end
  114. end
  115. info(actor, "玩家", actor, "充值次数已上限", "rechargeId", cfg_recharge["id"], "countKey", cfg_recharge["countkey"], "充值次数", count)
  116. return false
  117. end
  118. ---用于商业化相关的请求处理,统一的消息分发
  119. function Recharge.requestAction(actor, msgData)
  120. local rechargeType = msgData["type"]
  121. local action = msgData["action"]
  122. RechargeMessageEventListerTable:triggerEvent(rechargeType, actor, rechargeType, action, msgData)
  123. end
  124. ---统一回复消息
  125. function Recharge.resAction(actor, rechargeType, action, resData)
  126. local resMsg = {}
  127. resMsg.type = rechargeType
  128. resMsg.action = action
  129. resMsg.data = resData
  130. EventListerTable.print(actor, "商业化统一回复消息", resMsg)
  131. sendluamsg(actor, LuaMessageIdToClient.RES_RECHARGE_ACTION, resMsg)
  132. end