FirstRecharge.lua 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. FirstRecharge = {}
  2. local FirstRechargeConst = {
  3. RECHARGE_STAGE = "T$RechargeStage",
  4. RECHARGE_RECORD = "T$RechargeRecord",
  5. }
  6. -- 玩家充值
  7. function FirstRecharge.Recharge(actor, msg)
  8. local money = msg.money -- 本次充值金额
  9. local tables = ConfigDataManager.getList("cfg_first_recharge")
  10. if not tables or next(tables) == nil then
  11. error("首充配置表不存在")
  12. end
  13. local preTotalMoney = FirstRecharge.GetTotalMoney(actor,false)
  14. local totalMoney = preTotalMoney + money
  15. local num = tonumber(0)
  16. local tableIds = {}
  17. for _, cfg in ipairs(tables) do
  18. local amount = tonumber(cfg.amount)
  19. local id = tonumber(cfg.id)
  20. if amount >= num and amount <= totalMoney then
  21. num = amount
  22. end
  23. table.insert(tableIds, id)
  24. end
  25. if num == 0 then
  26. -- 没有达到首充条件
  27. FirstRecharge.RecordHistory(actor, money)
  28. return
  29. end
  30. local cfg = ConfigDataManager.getTable("cfg_first_recharge", "amount", num)[1]
  31. local tableId = tonumber(cfg.id)
  32. local runDay = tonumber(cfg.runday)
  33. local severOpenDays = getbaseinfo(actor, "serveropendays")
  34. if runDay > severOpenDays then
  35. error("开服天数不足")
  36. return
  37. end
  38. -- 过滤掉发过的和达不到的,发奖励
  39. local stage = FirstRecharge.GetRechargeStage(actor, false)
  40. stage = tonumber(stage)
  41. for _, id in ipairs(tableIds) do
  42. if id > stage and id <= tableId then
  43. -- 发放奖励
  44. local reward = ConfigDataManager.getTableValue("cfg_first_recharge", "reward", "id", id)
  45. if reward and reward ~= "" then
  46. local rewardStrList = string.splitByAll(reward, "|")
  47. for _, value in ipairs(rewardStrList) do
  48. local parts = string.splitByAll(value, "#")
  49. local itemId = parts[1]
  50. local count = parts[2]
  51. additemtobag(actor, tonumber(itemId), tonumber(count))
  52. end
  53. end
  54. end
  55. end
  56. if stage == 0 then
  57. -- todo之前没有充值过,首充武器奖励,item要提前生成,如何放置?
  58. end
  59. FirstRecharge.RecordStage(actor, tableId)
  60. FirstRecharge.RecordHistory(actor, money)
  61. sendluamsg(actor, 00000, { tableId })
  62. end
  63. -- 记录充值阶段
  64. function FirstRecharge.RecordStage(actor, tableId)
  65. local stage = getplaydef(actor, FirstRechargeConst.RECHARGE_STAGE) or 0
  66. if tonumber(stage) < tableId then
  67. setplaydef(actor, FirstRechargeConst.RECHARGE_STAGE, tableId)
  68. end
  69. end
  70. -- 记录充值金额记录
  71. function FirstRecharge.RecordHistory(actor, money)
  72. local records = getplaydef(actor, FirstRechargeConst.RECHARGE_RECORD) or {}
  73. local tamp = getbaseinfo(actor, "now")
  74. records[tamp] = money
  75. setplaydef(actor, FirstRechargeConst.RECHARGE_RECORD, records)
  76. end
  77. -- 获取总充值金额
  78. function FirstRecharge.GetTotalMoney(actor, isClient)
  79. local record = getplaydef(actor, FirstRechargeConst.RECHARGE_RECORD) or {}
  80. if next(record) ~= nil then
  81. local total = 0
  82. for tamp, money in pairs(record) do
  83. total = total + money
  84. end
  85. if isClient then
  86. sendluamsg(actor, 00000, { total })
  87. else
  88. return total
  89. end
  90. end
  91. if isClient then
  92. sendluamsg(actor, 00000, { 0 })
  93. else
  94. return 0
  95. end
  96. end
  97. -- 获取最高充值档次
  98. function FirstRecharge.GetRechargeStage(actor, isClient)
  99. local maxRecord = getplaydef(actor, FirstRechargeConst.RECHARGE_STAGE)
  100. if not maxRecord then
  101. if isClient then
  102. sendluamsg(actor, 00000, { 0 })
  103. else
  104. return 0
  105. end
  106. end
  107. if isClient then
  108. sendluamsg(actor, 00000, { maxRecord })
  109. else
  110. return maxRecord
  111. end
  112. end
  113. -- =================测试===================
  114. function testrecharge(actor, money)
  115. local msg = {
  116. money = money
  117. }
  118. FirstRecharge.Recharge(actor, msg)
  119. end
  120. function clearracharge(actor)
  121. setplaydef(actor, FirstRechargeConst.RECHARGE_STAGE, 0)
  122. setplaydef(actor, FirstRechargeConst.RECHARGE_RECORD, {})
  123. end