MonthGiftPack.lua 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. MonthGiftPack = {}
  2. MonthGiftPack.__index = MonthGiftPack
  3. local this = {}
  4. this.type = {
  5. once = "1",
  6. batch = "2",
  7. }
  8. local function _rechargeType()
  9. return "14"
  10. end
  11. local function _zeroType()
  12. return "0"
  13. end
  14. local function _playerDbKey()
  15. return PlayerDefKey.monthGift.MONTH_GIFT_KEY
  16. end
  17. local function _dailyDbKey()
  18. return PlayerDefKey.monthGift.MONTH_GIFT_DAILY_KEY
  19. end
  20. function MonthGiftPack.getMonth(actor)
  21. local data = getplaydef(actor, _playerDbKey())
  22. return setmetatable(data or {}, MonthGiftPack)
  23. end
  24. function MonthGiftPack:saveMonth(actor)
  25. setplaydef(actor, _playerDbKey(), self)
  26. end
  27. function MonthGiftPack:addMonthRecord(gift_id)
  28. local month = self.month_data or {}
  29. month[gift_id] = getbaseinfo("now")
  30. self.month_data = month
  31. end
  32. function MonthGiftPack.getDaily(actor)
  33. local data = getplaydef(actor, _dailyDbKey())
  34. return setmetatable(data or {}, MonthGiftPack)
  35. end
  36. function MonthGiftPack:saveDaily(actor)
  37. setplaydef(actor, _dailyDbKey(), self)
  38. end
  39. function MonthGiftPack:addDailyRecord(gift_id)
  40. local daily = self.daily_data or {}
  41. table.insert(daily, gift_id)
  42. self.daily_data = daily
  43. end
  44. -- ------------------------------------------------------------- --
  45. function MonthGiftPack.rechargeEvent(actor, cfg_recharge, count, amount, ext, outRewards)
  46. this.rechargeEvent(actor, cfg_recharge, count, amount, ext, outRewards)
  47. end
  48. function MonthGiftPack.reqRechargeAction(actor, _, action, reqParameter)
  49. if action == "panel" then
  50. this.sendPanelData(actor, reqParameter)
  51. elseif action == "reward" then
  52. this.gainReward(actor, reqParameter)
  53. end
  54. end
  55. function MonthGiftPack.loginEvent(actor)
  56. this.sendPanelData(actor, this.buildParameter())
  57. end
  58. function MonthGiftPack.midnightEvent(actor)
  59. --this.sendPanelData(actor, this.buildParameter())
  60. intervalcalldelay(actor, 10000, 1000, 1, "delayflushmonthgiftpackpanel", this.buildParameter())
  61. end
  62. -- ------------------------------------------------------------- --
  63. function delayflushmonthgiftpackpanel(actor, reqParameter)
  64. this.sendPanelData(actor, reqParameter)
  65. end
  66. function this.sendPanelData(actor, reqParameter)
  67. local month_gift = MonthGiftPack.getMonth(actor)
  68. local daily_gift = MonthGiftPack.getDaily(actor)
  69. this.debug("---- month_gift ----", month_gift)
  70. local resData = {}
  71. local list = ConfigDataManager.getList("cfg_monthlypack")
  72. gameDebug.assertTrue(table.notEmpty(list), "没有配置月卡礼包")
  73. for _, config in pairs(list) do
  74. local count_key = ConfigDataManager.getTableValue(
  75. "cfg_recharge", "countkey", "type", _rechargeType(), "parameter", config.id)
  76. local count_data = CountManager.getCount(actor, count_key)
  77. if count_data and count_data.count < 1 then
  78. if table.notEmpty(month_gift.month_data) then
  79. month_gift.month_data[config.id] = nil
  80. month_gift:saveMonth(actor)
  81. end
  82. if table.notEmpty(daily_gift.daily_data) then
  83. table.removeByValue(daily_gift.daily_data, config.id)
  84. daily_gift:saveDaily(actor)
  85. end
  86. end
  87. :: continue ::
  88. end
  89. resData.month_data = month_gift.month_data
  90. resData.daily_data = daily_gift.daily_data
  91. Recharge.resAction(actor, _rechargeType(), reqParameter.action, resData)
  92. this.debug("--- 请求月卡礼包面板信息", reqParameter, resData)
  93. end
  94. function this.rechargeEvent(actor, cfg_recharge, count, amount, ext, outRewards)
  95. this.debug("--- 购买月卡礼包 ---", cfg_recharge, count, amount, ext, outRewards)
  96. local gift_id = cfg_recharge.parameter
  97. local config = ConfigDataManager.getById("cfg_monthlypack", gift_id)
  98. gameDebug.assertTrue(table.notEmpty(config), "没有配置月卡礼包", gift_id)
  99. local is_pass = ConditionManager.Check(actor, config.condition)
  100. if not is_pass then
  101. tipinfo(actor, "月卡礼包购买条件不满足")
  102. return
  103. end
  104. local month_gift = MonthGiftPack.getMonth(actor)
  105. if config.monthlytype == this.type.once then
  106. this.rechargeOnce(actor, month_gift, config)
  107. else
  108. this.rechargeBatch(actor, month_gift, config)
  109. end
  110. this.jprint("--- 月卡礼包购买完成 ---")
  111. this.sendPanelData(actor, this.buildParameter())
  112. end
  113. function this.rechargeOnce(actor, month_gift, config)
  114. month_gift:addMonthRecord(config.id)
  115. month_gift:saveMonth(actor)
  116. local rewards = {}
  117. local reward_str = config.onerewards
  118. this.addItem2Bag(actor, rewards, reward_str)
  119. this.debug("--- 发放月卡返利礼包奖励 ---", reward_str, rewards)
  120. this.sendRewardPanel(actor, rewards, GameTips.Type.month_gift, config.id)
  121. end
  122. function this.rechargeBatch(actor, month_gift, config)
  123. if table.notEmpty(month_gift.month_data) then
  124. for k, _ in pairs(month_gift.month_data) do
  125. local cfg = ConfigDataManager.getById("cfg_monthlypack", k)
  126. if table.notEmpty(cfg) and cfg ~= config.id then
  127. tipinfo(actor, "您已经购买过其他礼包,无法批量购买")
  128. return
  129. end
  130. end
  131. end
  132. local cfg_list = ConfigDataManager.getTable("cfg_monthlypack", "monthlytype", this.type.once)
  133. gameDebug.assertTrue(table.notEmpty(config), "没有配置单次购买月卡礼包")
  134. local rewards = {}
  135. for _, cfg in pairs(cfg_list) do
  136. month_gift:addMonthRecord(cfg.id)
  137. local count_key = ConfigDataManager.getTableValue(
  138. "cfg_recharge", "countkey", "type", _rechargeType(), "parameter", cfg.id)
  139. CountManager.count(actor, count_key, 1)
  140. local reward_str = cfg.onerewards
  141. if not string.isNullOrEmpty(reward_str) then
  142. string.putIntIntMap(rewards, reward_str, "#", "|")
  143. end
  144. end
  145. month_gift:addMonthRecord(config.id)
  146. month_gift:saveMonth(actor)
  147. if table.notEmpty(rewards) then
  148. additemmaptobag(actor, rewards,0, 9999, '月卡礼包')
  149. this.sendRewardPanel(actor, rewards, GameTips.Type.month_gift, config.id)
  150. end
  151. end
  152. function this.gainReward(actor, reqParameter)
  153. local recharge_id = reqParameter["id"]
  154. this.jprint("-- 请求领取月卡每日礼包 --", recharge_id)
  155. local config = ConfigDataManager.getById("cfg_recharge", recharge_id)
  156. gameDebug.assertTrue(table.notEmpty(config), "没有配置充值档位", recharge_id)
  157. local gift_id = config.parameter
  158. local month_gift = MonthGiftPack.getMonth(actor)
  159. this.debug("---- month_gift ----", month_gift)
  160. local month_ids = month_gift.month_data or {}
  161. if not month_ids[gift_id] then
  162. this.debug("没有购买月卡礼包", config.countkey)
  163. return
  164. end
  165. local daily_gift = MonthGiftPack.getDaily(actor)
  166. local daily_ids = daily_gift.daily_data or {}
  167. if table.contains(daily_ids, gift_id) then
  168. this.debug("已经领取过月卡今日礼包", config.countkey)
  169. return
  170. end
  171. local gift_config = ConfigDataManager.getById("cfg_monthlypack", gift_id)
  172. gameDebug.assertTrue(table.notEmpty(gift_config), "没有配置礼包档位", gift_id)
  173. local rewards = {}
  174. local reward_str = gift_config.rewards
  175. this.addItem2Bag(actor, rewards, reward_str)
  176. this.sendRewardPanel(actor, rewards)
  177. this.debug("--- 发放月卡每日礼包奖励 ---", reward_str, rewards)
  178. daily_gift:addDailyRecord(gift_id)
  179. daily_gift:saveDaily(actor)
  180. this.sendPanelData(actor, this.buildParameter())
  181. this.jprint("--- 已领取月卡每日礼包 ---")
  182. end
  183. function this.buildParameter()
  184. return { type = _rechargeType(), action = "panel" }
  185. end
  186. function this.addItem2Bag(actor, rewards, reward_str)
  187. if not string.isNullOrEmpty(reward_str) then
  188. rewards = string.putIntIntMap(rewards, reward_str, "#", "|")
  189. additemmaptobag(actor, rewards, 0, 9999, '月卡礼包')
  190. end
  191. end
  192. function this.sendRewardPanel(actor, rewards, type, param)
  193. GameTips.sendGetRewardMsgWithParam(actor, rewards, type, param)
  194. end
  195. -- ------------------------------------------------------------- --
  196. this.log_open = true
  197. function this.debug(...)
  198. if not this.log_open then
  199. return
  200. end
  201. gameDebug.print(...)
  202. end
  203. function this.jprint(...)
  204. if not this.log_open then
  205. return
  206. end
  207. jprint(...)
  208. end
  209. -- ------------------------------------------------------------- --
  210. -- 礼包通用事件注册
  211. EventListerTable.registerType("月卡礼包", _rechargeType(), _playerDbKey())
  212. RechargeEventListerTable:eventLister(_rechargeType(), "月卡礼包", MonthGiftPack.rechargeEvent)
  213. RechargeMessageEventListerTable:eventLister(_rechargeType(), "月卡礼包", MonthGiftPack.reqRechargeAction)
  214. -- 系统事件注册
  215. LoginEventListerTable:eventLister(_zeroType(), "月卡礼包", MonthGiftPack.loginEvent)
  216. ZeroEventListerTable:eventLister(_zeroType(), "月卡礼包", MonthGiftPack.midnightEvent)