RebateLogic.lua 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. --渠道返利逻辑
  2. local Config = require("Config")
  3. local InnerMsg = require("core.InnerMsg")
  4. local ObjHuman = require("core.ObjHuman")
  5. local RebateCfg = require("excel.rebate")
  6. local CommonDB = require("common.CommonDB")
  7. local Util = require("common.Util")
  8. local MiddleManager = require("middle.MiddleManager")
  9. local RoleDBLogic = require("role.RoleDBLogic")
  10. local MailManager = require("mail.MailManager")
  11. local MailExcel = require("excel.mail")
  12. local Log = require("common.Log")
  13. -- 返利邮件ID
  14. local REBATE_MAIL_ID = 7016
  15. -- 获取玩家总充值金额
  16. local function getRoleTotalRecharge(account)
  17. account = tostring(account)
  18. for _, v in ipairs(RebateCfg.rebate) do
  19. if v.account == account then
  20. return v.totalRecharge1
  21. end
  22. end
  23. end
  24. -- 检查玩家是否符合返利条件
  25. local function check(channelId, account)
  26. local mainCfg = RebateCfg.main[1]
  27. if channelId ~= mainCfg.channelId then
  28. return false
  29. end
  30. if getRoleTotalRecharge(account) then
  31. return true
  32. end
  33. return false
  34. end
  35. ----------------------------------------------------普通服-----------------------------------------------------
  36. --计算返利道具数量
  37. local function calcRebateItemCnt(account)
  38. local roleTotalRecharge = getRoleTotalRecharge(account)
  39. local rebateDays = RebateCfg.main[1].rebateDays
  40. return math.ceil(roleTotalRecharge / rebateDays)
  41. end
  42. --玩家登录时
  43. function NS_RebateQuery(channelId, serverId, account)
  44. if not check(channelId, account) then
  45. return
  46. end
  47. -- 向跨服查询是否返利
  48. local tMsgData = InnerMsg.lw.LW_REBATE_QUERY
  49. tMsgData.nSrcServerID = Config.SVR_INDEX
  50. tMsgData.channelId = channelId
  51. tMsgData.serverId = serverId
  52. tMsgData.account = account
  53. InnerMsg.sendMsg(0, tMsgData)
  54. end
  55. --收到跨服通知给玩家返利
  56. function NS_Rebate_OK(channelId, serverId, account)
  57. local uTag = RoleDBLogic.Generateuuid(channelId, account, serverId)
  58. local human = ObjHuman.onlineNewUniqueTag[uTag]
  59. if not human or not human.db or not human.db._id then
  60. local logInfo = string.format("channelId: %s, serverId: %s, account: %s, result: %s, time: %s",
  61. channelId, serverId, account, "defeat", os.time())
  62. Log.write(Log.LOGID_OSS_REBATE, logInfo)
  63. return
  64. end
  65. -- 发邮件
  66. local mailCfg = MailExcel.mail[REBATE_MAIL_ID]
  67. local itemId = RebateCfg.main[1].rebateItemId
  68. local itemCnt = calcRebateItemCnt(account)
  69. local itemList = { {itemId, itemCnt } }
  70. MailManager.add(MailManager.SYSTEM, human.db._id, mailCfg.title, mailCfg.content, itemList, mailCfg.senderName or "GM")
  71. end
  72. ----------------------------------------------------------------------------------------------------------------
  73. ----------------------------------------------------跨服-----------------------------------------------------
  74. local function getRebateData()
  75. return CommonDB.getValueByKey(CommonDB.KEY_REBATE_DATA)
  76. end
  77. local function updateRebateData(newData)
  78. CommonDB.updateValue(CommonDB.KEY_REBATE_DATA, newData)
  79. end
  80. -- 收到普通服查询是否给玩家返利
  81. function CS_RebateQuery(msg)
  82. local channelId = msg.channelId
  83. local serverId = msg.serverId
  84. local account = msg.account
  85. if not check(channelId, account) then
  86. return
  87. end
  88. local now = os.time()
  89. local rebateData = getRebateData()
  90. local mainCfg = RebateCfg.main[1]
  91. if rebateData and rebateData[account] then
  92. local roleRebateData = rebateData[account]
  93. --同账号值返一个区
  94. if roleRebateData.serverId ~= serverId then
  95. return
  96. end
  97. if roleRebateData.rebateDays >= mainCfg.rebateDays then
  98. return
  99. end
  100. -- 一天返一次
  101. if Util.isSameDayByTimes(roleRebateData.lastRebateTime, now) then
  102. return
  103. end
  104. end
  105. rebateData = rebateData or {}
  106. rebateData[account] = rebateData[account] or {}
  107. local roleRebateData = rebateData[account]
  108. roleRebateData.serverId = serverId
  109. roleRebateData.rebateDays = (roleRebateData.rebateDays or 0) + 1
  110. roleRebateData.lastRebateTime = now
  111. -- 更新数据库
  112. updateRebateData(rebateData)
  113. -- 通知游戏服当天可以返利
  114. local tMsgData = InnerMsg.wl.WL_REBATE_OK
  115. tMsgData.channelId = channelId
  116. tMsgData.serverId = serverId
  117. tMsgData.account = account
  118. local fd = MiddleManager.getFDBySvrIndex(msg.nSrcServerID)
  119. InnerMsg.sendMsg(fd, tMsgData)
  120. end
  121. ----------------------------------------------------------------------------------------------------------------