DailyGiftPack.lua 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. ---
  2. --- Created by zhouzhipeng.
  3. --- DateTime: 2024/11/5 上午11:13
  4. --- Desc:每日礼包
  5. ---
  6. DailyGiftPack = {}
  7. DailyGiftPack.__index = DailyGiftPack
  8. local this = {}
  9. --local DAILY_GIFT = 4
  10. --- 每日礼包类型
  11. local function _rechargeType()
  12. return "4"
  13. end
  14. local function _playerDbKey()
  15. return "J$daily_gift_pack_data"
  16. end
  17. function DailyGiftPack.get(actor)
  18. local obj = getplaydef(actor, _playerDbKey())
  19. return setmetatable(obj or {}, DailyGiftPack)
  20. end
  21. function DailyGiftPack:save(actor)
  22. setplaydef(actor, _playerDbKey(), self);
  23. end
  24. function DailyGiftPack:setRecordIndex(rechargeId, index)
  25. if not self.dailyGiftInfo then
  26. self.dailyGiftInfo = {}
  27. end
  28. self.dailyGiftInfo[rechargeId] = index
  29. end
  30. function DailyGiftPack:getRecordIndex(rechargeId)
  31. if not self.dailyGiftInfo then
  32. self.dailyGiftInfo = {}
  33. end
  34. return self.dailyGiftInfo[rechargeId] or 0
  35. end
  36. ---充值事件触发
  37. function DailyGiftPack.rechargeEvent(actor, cfg_recharge, count, amount, ext, outRewards)
  38. DailyGiftPack.print(actor, "购买每日礼包", cfg_recharge, "购买数量", count, ext, outRewards)
  39. if ext == "" then
  40. ext = {}
  41. end
  42. local dailyGiftId = cfg_recharge.parameter
  43. local rechargeType = cfg_recharge.type
  44. local cfg_DailyGiftPack = ConfigDataManager.getById("cfg_DailyGiftPack", dailyGiftId)
  45. gameDebug.assertTrue(table.notNullOrEmpty(cfg_DailyGiftPack), "没有配置每日礼包", dailyGiftId)
  46. if not ConditionManager.Check(actor, cfg_DailyGiftPack.conditionid) then
  47. tipinfo(actor, "购买条件不满足")
  48. return
  49. end
  50. local purchase = cfg_DailyGiftPack.purchase
  51. local dailyGiftPack = DailyGiftPack.get(actor)
  52. if string.isNullOrEmpty(purchase) then
  53. local cfgs = ConfigDataManager.getTable("cfg_DailyGiftPack", "purchase", dailyGiftId)
  54. if not cfgs then
  55. tipinfo(actor, "一键购买配置错误")
  56. end
  57. -- 判断是否购买过单个礼包
  58. for _, cfg in pairs(cfgs) do
  59. if tonumber(cfg.price) == 0 then
  60. goto continue
  61. end
  62. local c = CountManager.getCount(actor, cfg["limited"])
  63. local buyCount = c.count
  64. if buyCount > 0 then
  65. tipinfo(actor, "购买过单个礼包")
  66. return
  67. end
  68. :: continue ::
  69. end
  70. gameDebug.assertTrue(table.notNullOrEmpty(cfgs), "没有一键购买每日礼包配置", dailyGiftId)
  71. DailyGiftPack.print("读取的配置", cfgs)
  72. -- 购买一键礼包
  73. for i = 1, #cfgs do
  74. local cfg = cfgs[i]
  75. if tonumber(cfg.price) == 0 then
  76. goto continue
  77. end
  78. local limited = cfg["limited"]
  79. local total = tonumber(ConfigDataManager.getTableValue("Count_count", "total", "id", limited))
  80. local index = ext[tonumber(cfg.id)] or 0
  81. for j = 1, total do
  82. DailyGiftPack.print(actor, "购买一键礼包", cfg.id, "购买次数", j, "i", i, "ext", ext, "ext[e]", index)
  83. this.mergeItem(outRewards, cfg, index)
  84. end
  85. local recId = ConfigDataManager.getTableValue("cfg_recharge", "id", "parameter", cfg.id, "type", rechargeType)
  86. if index ~= 0 then
  87. dailyGiftPack:setRecordIndex(recId, index)
  88. end
  89. CountManager.count(actor, limited, total)
  90. :: continue ::
  91. end
  92. else
  93. -- 购买单个礼包
  94. local index = ext[tonumber(cfg_DailyGiftPack.id)] or 0
  95. this.mergeItem(outRewards, cfg_DailyGiftPack, index)
  96. if index ~= 0 then
  97. dailyGiftPack:setRecordIndex(cfg_recharge.id, index)
  98. end
  99. end
  100. dailyGiftPack:save(actor)
  101. -- 发送界面信息数据
  102. DailyGiftPack.queryData(actor, { type = rechargeType, action = "panel" })
  103. DailyGiftPack.print(actor, "购买每日礼包成功", cfg_recharge, "购买数量", count, ext, outRewards)
  104. end
  105. --- 请求充值档位信息
  106. ---@param msgData table 请求参数
  107. function DailyGiftPack.reqRechargeAction(actor, rechargeType, action, reqParameter)
  108. if action == "panel" then
  109. DailyGiftPack.queryData(actor, reqParameter)
  110. elseif action == "reward" then
  111. DailyGiftPack.gainReward(actor, reqParameter)
  112. end
  113. end
  114. --- 每日礼包信息
  115. function DailyGiftPack.queryData(actor, reqParameter)
  116. DailyGiftPack.print(actor, "请求每日礼包信息", reqParameter)
  117. local rechargeType = reqParameter["type"]
  118. local dailyGiftPacks = ConfigDataManager.getList("cfg_DailyGiftPack")
  119. gameDebug.assertTrue(table.notNullOrEmpty(dailyGiftPacks), "没有配置充值档位")
  120. local resData = {}
  121. local group = 0
  122. local countInfo = {}
  123. local dailyGiftPack = DailyGiftPack.get(actor)
  124. DailyGiftPack.print("配置表数据", dailyGiftPacks)
  125. for _, cfg in pairs(dailyGiftPacks) do
  126. if ConditionManager.Check(actor, cfg.conditionid) then
  127. local id = cfg.id
  128. local rechargeId = ConfigDataManager.getTableValue("cfg_recharge", "id", "parameter", id, "type", rechargeType)
  129. --local c = CountManager.getCount(actor, cfg["limited"])
  130. --local count = c.count
  131. local index = dailyGiftPack:getRecordIndex(rechargeId)
  132. countInfo[rechargeId] = index
  133. group = cfg.packagetype
  134. end
  135. end
  136. resData['group'] = group
  137. resData['countInfo'] = countInfo
  138. Recharge.resAction(actor, rechargeType, reqParameter.action, resData)
  139. end
  140. function DailyGiftPack.gainReward(actor, reqParameter)
  141. end
  142. function this.mergeItem(outRewards, cfg, index)
  143. string.putIntIntMap(outRewards, cfg['rewards'], "#", "|")
  144. -- todo 待优化,optionalreward 分隔为 {1={itemId=count},2={itemId=count},3={itemId=count}}
  145. local optionalReward = cfg['optionalreward']
  146. if not string.isNullOrEmpty(optionalReward) then
  147. local strShuXian = string.split(optionalReward, "|")
  148. local strJinHao = strShuXian[index]
  149. DailyGiftPack.print("合并奖励", cfg, "分隔好的", strShuXian, "根据索引选的", strJinHao, "索引", index)
  150. local str = string.split(strJinHao, "#")
  151. local itemCfgId = tonumber(str[1])
  152. local oldCount = outRewards[itemCfgId] or 0
  153. outRewards[itemCfgId] = oldCount + tonumber(str[2])
  154. end
  155. end
  156. --- 登录处理
  157. function DailyGiftPack.login(play)
  158. DailyGiftPack.queryData(play, { type = _rechargeType(), action = "panel" })
  159. end
  160. --- 零点处理
  161. function DailyGiftPack.zero(play)
  162. DailyGiftPack.queryData(play, { type = _rechargeType(), action = "panel" })
  163. end
  164. --- 打印日志
  165. local isPrintLog = true
  166. function DailyGiftPack.print(...)
  167. if isPrintLog then
  168. return
  169. end
  170. gameDebug.print(...)
  171. end
  172. function dailygiftpacktest(actor, id, type)
  173. gameDebug.print(actor, "请求每日礼包", id)
  174. local tab
  175. if tonumber(type == 1) then
  176. tab = { [1] = 1 }
  177. else
  178. tab = { [1] = 1, [2] = 1, [3] = 1, [4] = 1 }
  179. end
  180. Recharge.request(actor, { rechargeId = id, count = 1, extraParams = tab })
  181. end
  182. EventListerTable.registerType("每日礼包", _rechargeType(), _playerDbKey())
  183. -- 凌晨刷新事件
  184. ZeroEventListerTable:eventLister("0", "每日礼包", DailyGiftPack.zero)
  185. -- 注册登录事件
  186. LoginEventListerTable:eventLister("0", "每日礼包", DailyGiftPack.login)
  187. -- 注册充值回调事件
  188. RechargeEventListerTable:eventLister(_rechargeType(), "每日礼包", DailyGiftPack.rechargeEvent)
  189. -- 注册请求消息监听
  190. RechargeMessageEventListerTable:eventLister(_rechargeType(), "每日礼包", DailyGiftPack.reqRechargeAction)