InvestmentFunds.lua 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. --- 投资基金
  2. --- Generated by EmmyLua(https://github.com/EmmyLua)
  3. --- Created by 無心道(15388152619).
  4. --- DateTime: 2024/11/11 10:44
  5. InvestmentFunds = {}
  6. InvestmentFunds.__index = InvestmentFunds
  7. local this = {}
  8. local function __rechargeType()
  9. return "10"
  10. end
  11. local function __playerDbKey()
  12. return "T$investment_funds_data"
  13. end
  14. function InvestmentFunds.get(actor)
  15. local obj = getplaydef(actor, __playerDbKey())
  16. return setmetatable(obj or {}, InvestmentFunds)
  17. end
  18. function InvestmentFunds:save(actor)
  19. setplaydef(actor, __playerDbKey(), self);
  20. end
  21. ---购买分组
  22. function InvestmentFunds:getGroup()
  23. return self["group"] or 1;
  24. end
  25. ---购买时间
  26. function InvestmentFunds:getBuyTime()
  27. return self["buyTime"] or 0;
  28. end
  29. ---购买天数,尚未购买是0
  30. function InvestmentFunds:getBuyDay(nowSec)
  31. local buyTime = self:getBuyTime()
  32. if buyTime == 0 then
  33. return 0
  34. end
  35. return TimeUtil.diffDays(buyTime, nowSec) + 1
  36. end
  37. ---购买时间
  38. function InvestmentFunds:getRewards()
  39. return self["rewards"] or {};
  40. end
  41. function InvestmentFunds:buy(nowSec)
  42. self["buyTime"] = nowSec --购买时间
  43. self["rewards"] = { } --购买时间
  44. end
  45. ---设置标记下一组
  46. function this.nextGroup(self, actor)
  47. local group = tonumber(self["group"] or 0) + 1
  48. local cfgList = ConfigDataManager.getTable("cfg_investmentfunds", "groupid", group)
  49. if table.isNullOrEmpty(cfgList) then
  50. --说明没有找到下一组
  51. return
  52. end
  53. self["group"] = group;
  54. self["buyTime"] = 0 --更新最好一次购买时间
  55. self["rewards"] = {};
  56. self:save(actor)
  57. info(actor, "玩家", actor, "投资基金切换到下一组:", group)
  58. end
  59. ---充值事件触发
  60. function this.rechargeEvent(actor, cfg_recharge, count, amount, ext, outRewards)
  61. local data = InvestmentFunds.get(actor)
  62. local var = cfg_recharge["type"]
  63. if var ~= __rechargeType() then
  64. --类型不一致不处理
  65. print("类型不一致不处理", var)
  66. return
  67. end
  68. local parameter = cfg_recharge["parameter"]
  69. local group = data:getGroup()
  70. if group ~= tonumber(parameter) then
  71. info(actor, "玩家", actor, "数据错误,无法购买", group)
  72. return
  73. end
  74. local parameter = cfg_recharge["parameter"]
  75. local nowSec = getbaseinfo("nowsec")
  76. gameDebug.assertTrue(data:getBuyTime() == 0, "投资基金已经购买过了", parameter)
  77. data:buy(nowSec)
  78. data:save(actor)
  79. info(actor, "玩家", actor, "购买投资基金", group)
  80. this.sendPanel(actor)
  81. end
  82. function this.reqAction(actor, type, action, reqParameter)
  83. if action == "panel" then
  84. this.sendPanel(actor)
  85. elseif action == "reward" then
  86. this.reward(actor, reqParameter)
  87. elseif action == "buy" then
  88. end
  89. end
  90. function this.reward(actor, reqParameter)
  91. local data = InvestmentFunds.get(actor)
  92. local group = data:getGroup()
  93. local nowSec = getbaseinfo("nowsec")
  94. local id = reqParameter["id"]
  95. local cfg = ConfigDataManager.getById("cfg_investmentfunds", id)
  96. gameDebug.assertNil(cfg, "cfg_investmentfunds 数据错误", id)
  97. gameDebug.assertTrue(ConditionManager.Check(actor, cfg["conditionid"]), "条件未达成")
  98. local cfgGroup = cfg["groupid"]
  99. if tonumber(cfgGroup) ~= group then
  100. tipinfo(actor, "数据错误,无法领取")
  101. return
  102. end
  103. local buyDay = data:getBuyDay(nowSec)
  104. local cfgDay = tonumber(cfg["day"])
  105. if cfgDay > buyDay then
  106. tipinfo(actor, "基金还未到领取时间")
  107. return
  108. end
  109. local rewards = data:getRewards()
  110. local checkRewards = table.contains(rewards, id)
  111. if checkRewards then
  112. tipinfo(actor, "已经领取过该奖励")
  113. return
  114. end
  115. table.insert(rewards, id)
  116. data:save(actor)
  117. info(actor, "玩家", actor, "投资基金", group, "领取奖励:", id)
  118. --发放奖励到背包
  119. Bag.sendRewards4String(actor, cfg["rewards"], "投资基金")
  120. Recharge.resAction(actor, __rechargeType(), "reward", { id = id })
  121. if this.checkNext(data, actor) then
  122. this.sendPanel(actor)
  123. end
  124. end
  125. function this.sendPanel(actor)
  126. local nowSec = getbaseinfo("nowsec")
  127. local data = InvestmentFunds.get(actor)
  128. local group = data:getGroup()
  129. local cfgList = ConfigDataManager.getTable("cfg_investmentfunds", "groupid", group)
  130. if table.isEmpty(cfgList) then
  131. print(actor, "投资基金没有配置数据", group)
  132. return
  133. end
  134. local cfgCount = table.count(cfgList)
  135. local _buyDay = data:getBuyDay(nowSec)
  136. local resInfo = {
  137. group = group,
  138. buyDay = _buyDay > cfgCount and cfgCount or _buyDay, --这个时间跟客户端的小红点有关系
  139. rewards = data:getRewards()
  140. }
  141. Recharge.resAction(actor, __rechargeType(), "panel", resInfo)
  142. end
  143. function this.checkNext(self, actor)
  144. local group = InvestmentFunds.getGroup(self)
  145. local nowSec = getbaseinfo("nowsec")
  146. local cfgList = ConfigDataManager.getTable("cfg_investmentfunds", "groupid", group)
  147. local cfgCount = table.count(cfgList)
  148. local buyDay = InvestmentFunds.getBuyDay(self, nowSec)
  149. if buyDay <= cfgCount then
  150. --根据领奖数量,判断是否可以领取下一组
  151. return
  152. end
  153. local rewards = self:getRewards()
  154. if cfgCount <= table.count(rewards) then
  155. this.nextGroup(self, actor)
  156. return true
  157. end
  158. return false
  159. end
  160. ---登录监听事件
  161. function this.onLogin(actor)
  162. local data = InvestmentFunds.get(actor)
  163. if data["group"] == nil then
  164. this.nextGroup(data, actor)
  165. end
  166. this.checkNext(data, actor)
  167. this.sendPanel(actor)
  168. end
  169. EventListerTable.registerType("投资基金", __rechargeType(), __playerDbKey())
  170. --注册登录事件
  171. LoginEventListerTable:eventLister("0", "投资基金", this.onLogin)
  172. --注册凌晨刷新事件
  173. ZeroEventListerTable:eventLister("0", "投资基金", this.onLogin)
  174. --充值回调,不关心类型
  175. RechargeEventListerTable:eventLister(__rechargeType(), "投资基金", this.rechargeEvent)
  176. --请求事件
  177. RechargeMessageEventListerTable:eventLister(__rechargeType(), "投资基金", this.reqAction)