FirstChargeGift.lua 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. FirstChargeGift = {}
  2. FirstChargeGift.__index = FirstChargeGift
  3. local function _rechargeType()
  4. return "3"
  5. end
  6. local function _playerDbKey()
  7. return "T$recharge_first_gift_data"
  8. end
  9. function FirstChargeGift.getData(actor)
  10. local var = getplaydef(actor, _playerDbKey())
  11. local data = setmetatable(var or {}, FirstChargeGift)
  12. if data.rewards == nil then
  13. data.rewards = {}
  14. end
  15. if not data.totalMoney then
  16. data.totalMoney = 0
  17. end
  18. return data
  19. end
  20. function FirstChargeGift:saveData(actor)
  21. setplaydef(actor, _playerDbKey(), self)
  22. end
  23. ---充值回调触发事件
  24. function FirstChargeGift.rechargeEvent(actor, cfg_recharge, count, amount, ext, outRewards)
  25. local data = FirstChargeGift.getData(actor)
  26. if tonumber(data[cfg_recharge["parameter"]] or 0) > 0 then
  27. return
  28. end
  29. data.totalMoney = data.totalMoney + amount
  30. local configs = ConfigDataManager.getTable("cfg_first_charge")
  31. gameDebug.assertTrue(not table.isNullOrEmpty(configs), "首充礼包配置为空")
  32. for _, config in ipairs(configs) do
  33. if string.isNullOrEmpty(config["showswitch"]) then
  34. local cfg_recharge_amount = ConfigDataManager.getTableValue("cfg_recharge", "amount", "type", _rechargeType(), "parameter", config["id"])
  35. gameDebug.assertNil(cfg_recharge_amount, "查找充值配置异常", "type", _rechargeType(), "parameter", config["id"])
  36. if data.totalMoney >= tonumber(cfg_recharge_amount) then
  37. if tonumber(data.rewards[config["id"]] or 0) < 1 then
  38. data.rewards[config["id"]] = 1
  39. end
  40. end
  41. end
  42. end
  43. FirstChargeGift.saveData(data, actor)
  44. FirstChargeGift.sendPanel(actor, data)
  45. -- jprint("更新豪礼数据",data,config)
  46. end
  47. ---统一的请求消息处理
  48. function FirstChargeGift.reqAction(actor, type, action, reqParameter)
  49. if action == "panel" then
  50. local data = FirstChargeGift.getData(actor)
  51. FirstChargeGift.sendPanel(actor, data)
  52. elseif action == "reward" then
  53. FirstChargeGift.gainReward(actor, reqParameter)
  54. end
  55. end
  56. function FirstChargeGift.gainReward(actor, reqParameter)
  57. local id = tostring(reqParameter["id"])
  58. local rechargeCfg = ConfigDataManager.getById("cfg_first_charge", id)
  59. gameDebug.assertTrue(table.notNullOrEmpty(rechargeCfg), "首充配置查找失败", id)
  60. local data = FirstChargeGift.getData(actor)
  61. local reward = data.rewards[id] or 0
  62. if reward < 1 then
  63. tipinfo(actor, "未达到领取条件")
  64. return
  65. end
  66. if reward > 1 then
  67. tipinfo(actor, "已经领取条件")
  68. return
  69. end
  70. -- 改变状态
  71. data.rewards[id] = 2
  72. FirstChargeGift.saveData(data, actor)
  73. info(actor, "领取首充礼包", actor, id)
  74. -- 发送奖励
  75. Bag.sendRewards4CareerString(actor, rechargeCfg['firstreward'], "首充礼包")
  76. FirstChargeGift.sendPanel(actor, data)
  77. -- jprint("领奖豪礼数据",data)
  78. end
  79. function FirstChargeGift.sendPanel(actor, data)
  80. Recharge.resAction(actor, _rechargeType(), "panel", data)
  81. end
  82. --TODO 一定要放到文件最后
  83. EventListerTable.registerType("首充礼包", _rechargeType(), _playerDbKey())
  84. --注册充值事件
  85. RechargeEventListerTable:eventLister(_rechargeType(), "首充礼包", FirstChargeGift.rechargeEvent)
  86. -- 注册请求消息监听
  87. RechargeMessageEventListerTable:eventLister(_rechargeType(), "首充礼包", FirstChargeGift.reqAction)