BenefitActivity.lua 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. BenefitActivity = {}
  2. local this = {}
  3. ---福利大厅数据
  4. local PLAYER_BENEFIT_INFO = "T$player_benefit_info"
  5. local GLOBAL_BENEFIT_INFO = "R$global_benefit_info"
  6. ---福利大厅常量
  7. this.ActivityType = {
  8. ---无
  9. NONE = 0,
  10. ---在线奖励
  11. ONLINE = 1,
  12. ---登录奖励
  13. LOGIN = 2,
  14. ---等级奖励
  15. LEVELUP = 3,
  16. ---累充奖励
  17. TOTAL_RECHARGE = 4,
  18. }
  19. function BenefitActivity.IsDataKey(varName)
  20. if string.equalsIgnoreCase(varName,PLAYER_BENEFIT_INFO) then
  21. return true
  22. else
  23. return false
  24. end
  25. end
  26. ---获取福利大厅数据
  27. function this.GetBenefitData(actor, type)
  28. local globalBenefitInfo = getplaydef(actor, PLAYER_BENEFIT_INFO)
  29. if globalBenefitInfo == nil then
  30. globalBenefitInfo = {}
  31. end
  32. if type == this.ActivityType.NONE then
  33. return globalBenefitInfo
  34. end
  35. -- 如果type不为NONE则返回对应数据
  36. if globalBenefitInfo[type] == nil then
  37. globalBenefitInfo[type] = {}
  38. end
  39. return globalBenefitInfo[type]
  40. end
  41. ---保存福利大厅数据
  42. function this.SaveBenefitData(actor, type, globalBenefitInfo)
  43. local golbalBenefitAllInfo = this.GetBenefitData(actor, this.ActivityType.NONE)
  44. golbalBenefitAllInfo[type] = globalBenefitInfo
  45. setplaydef(actor, PLAYER_BENEFIT_INFO, golbalBenefitAllInfo)
  46. end
  47. ---获取福利大厅数据
  48. function this.GetGlobalBenefitData(type)
  49. local globalBenefitInfo = getsysvar(GLOBAL_BENEFIT_INFO)
  50. if globalBenefitInfo == nil then
  51. globalBenefitInfo = {}
  52. end
  53. if type == this.ActivityType.NONE then
  54. return globalBenefitInfo
  55. end
  56. -- 如果type不为NONE则返回对应数据
  57. if globalBenefitInfo[type] == nil then
  58. globalBenefitInfo[type] = {}
  59. end
  60. return globalBenefitInfo[type]
  61. end
  62. ---保存福利大厅数据
  63. function this.SaveGlobalBenefitData(type, globalBenefitInfo)
  64. local golbalBenefitAllInfo = this.GetGlobalBenefitData(this.ActivityType.NONE)
  65. golbalBenefitAllInfo[type] = globalBenefitInfo
  66. setsysvar(GLOBAL_BENEFIT_INFO, golbalBenefitAllInfo)
  67. end
  68. -- 记录在线奖励登录时间和登录奖励
  69. function BenefitActivity.login(actor)
  70. -- 记录在线奖励此次登录时间,如果上次登录时间跨天,则清空在线时长
  71. -- local online_data = this.GetBenefitData(actor, this.ActivityType.ONLINE)
  72. -- if online_data == nil then
  73. -- online_data = {}
  74. -- end
  75. -- if online_data.reward_list == nil then
  76. -- online_data.reward_list = {}
  77. -- end
  78. -- online_data.last_login_time = getbaseinfo(actor, "logintime")
  79. -- if online_data.last_reward_time == nil or now - online_data.last_reward_time > 86400000 then
  80. -- online_data.last_reward_time = 0
  81. -- else
  82. -- end
  83. -- this.SaveBenefitData(actor, this.ActivityType.ONLINE, online_data)
  84. -- 登录数据验证,登录奖励记录领奖时间列表,超过7天的数据清零
  85. local now = getbaseinfo("nowsec")
  86. local login_data = this.GetBenefitData(actor, this.ActivityType.LOGIN)
  87. if login_data == nil then
  88. login_data = {}
  89. end
  90. if login_data.reward_list == nil then
  91. login_data.reward_list = {}
  92. end
  93. for i, data in pairs(login_data.reward_list) do
  94. if now - tonumber(data) >= 86400 * 7 then
  95. login_data.reward_list[i] = nil
  96. end
  97. end
  98. this.SaveBenefitData(actor, this.ActivityType.LOGIN, login_data)
  99. end
  100. -- 记录在线奖励在线时间
  101. function BenefitActivity.logout(actor)
  102. end
  103. -- 记录在线奖励在线时间
  104. function BenefitActivity.midNightUpdate(actor)
  105. end
  106. ---构建活动数据回包
  107. function BenefitActivity.SendActivityInfo(actor, msgData)
  108. this.SendActivityInfo(actor, msgData)
  109. end
  110. -- 获取开服天数
  111. function this:GetLoginDays(actor)
  112. -- 获取当前开服天数
  113. local opendays = getbaseinfo(actor, "serveropendays")
  114. return (opendays - 1) % 7 + 1
  115. end
  116. ---构建活动数据回包
  117. function this.SendActivityInfo(actor, msgData)
  118. -- this.SaveBenefitData(actor, this.ActivityType.LEVELUP, {})
  119. local globalBenefitInfo = this.GetBenefitData(actor, this.ActivityType.NONE)
  120. -- 整合升级数据
  121. if globalBenefitInfo[this.ActivityType.LEVELUP] == nil then
  122. globalBenefitInfo[this.ActivityType.LEVELUP] = { record = {} }
  123. end
  124. -- 判断领取数量是否已满
  125. local recordData = this.GetGlobalBenefitData(this.ActivityType.LEVELUP)
  126. local configTable = ConfigDataManager.getList("cfg_benefit_activity")
  127. for i = 1, #configTable do
  128. ---@type cfg_benefit_activity_column
  129. local tmpConfig = configTable[i]
  130. if tonumber(tmpConfig.maingroup) == this.ActivityType.LEVELUP then
  131. local subtype = tonumber(tmpConfig.subtype)
  132. if globalBenefitInfo[this.ActivityType.LEVELUP].record[subtype] == nil then
  133. globalBenefitInfo[this.ActivityType.LEVELUP].record[subtype] = {}
  134. end
  135. if recordData[subtype] == nil then
  136. recordData[subtype] = {}
  137. end
  138. globalBenefitInfo[this.ActivityType.LEVELUP].record[subtype].sub_type = subtype
  139. globalBenefitInfo[this.ActivityType.LEVELUP].record[subtype].reward_num = #recordData[subtype]
  140. end
  141. end
  142. sendluamsg(actor, LuaMessageIdToClient.RES_ACTIVITY_BENEFIT_INFO, globalBenefitInfo)
  143. end
  144. ---领取奖励
  145. function BenefitActivity.ReceiveReward(actor, msgData)
  146. this.ReceiveAward(actor, msgData)
  147. end
  148. ---领取奖励
  149. function this.ReceiveAward(actor, msgData)
  150. -- jprint("玩家请求领取福利大厅奖励",actor,msgData)
  151. local activityType = tonumber(msgData["activityType"])
  152. local activitySubType = tonumber(msgData["activitySubType"])
  153. local activityData = this.GetBenefitData(actor, activityType)
  154. ---@type cfg_benefit_activity_column[]
  155. local config = nil
  156. local configTable = ConfigDataManager.getList("cfg_benefit_activity")
  157. for i = 1, #configTable do
  158. ---@type cfg_benefit_activity_column
  159. local tmpConfig = configTable[i]
  160. if tonumber(tmpConfig.maingroup) == activityType then
  161. if tonumber(tmpConfig.subtype) == activitySubType then
  162. config = tmpConfig
  163. end
  164. end
  165. end
  166. if activityData.record == nil then
  167. activityData.record = {}
  168. end
  169. if config == nil then
  170. tipinfo(actor,"配置可领取数量不足")
  171. return
  172. elseif activityType == this.ActivityType.ONLINE then
  173. return
  174. elseif activityType == this.ActivityType.LOGIN then
  175. -- 获取当前开服天数
  176. local opendays = getbaseinfo(actor, "serveropendays")
  177. local curDays = (opendays - 1) % 7 + 1
  178. if activitySubType ~= curDays then
  179. tipinfo(actor,"不是当前天数")
  180. return
  181. end
  182. local now = getbaseinfo("now")
  183. -- 判断是否已领取过
  184. for _, recordData in pairs(activityData.record) do
  185. if TimeUtil.isSameDay4Millis(now, recordData.time) then
  186. tipinfo(actor,"您已领取过该活动奖励")
  187. -- 发送活动数据刷新显示
  188. this.SendActivityInfo(actor)
  189. return
  190. end
  191. end
  192. elseif activityType == this.ActivityType.LEVELUP then
  193. local level = getplayermaininfo(actor).level
  194. if level < tonumber(config.conditionparam) then
  195. tipinfo(actor, "条件不满足")
  196. return
  197. end
  198. -- 判断是否已领取过
  199. for _, recordData in pairs(activityData.record) do
  200. if activitySubType == tonumber(recordData.type) then
  201. tipinfo(actor,"您已领取过该活动奖励")
  202. -- 发送活动数据刷新显示
  203. this.SendActivityInfo(actor)
  204. return
  205. end
  206. end
  207. -- 判断领取数量是否已满
  208. local recordData = this.GetGlobalBenefitData(activityType)
  209. if recordData[activitySubType] == nil then
  210. recordData[activitySubType] = {}
  211. end
  212. if #recordData[activitySubType] >= tonumber(config.numparam) then
  213. tipinfo(actor,"领取数量已达上限")
  214. return
  215. end
  216. elseif activityType == this.ActivityType.TOTAL_RECHARGE then
  217. local totalChargeNum = RechargeRecord.getTotalMoney(actor)
  218. if totalChargeNum < tonumber(config.conditionparam) then
  219. tipinfo(actor, "条件不满足")
  220. return
  221. end
  222. -- 判断是否已领取过
  223. for _, recordData in pairs(activityData.record) do
  224. if activitySubType == tonumber(recordData.type) then
  225. tipinfo(actor,"您已领取过该活动奖励")
  226. -- 发送活动数据刷新显示
  227. this.SendActivityInfo(actor)
  228. return
  229. end
  230. end
  231. else
  232. tipinfo(actor, "活动不存在")
  233. return
  234. end
  235. -- 发送对应活动奖励
  236. if this.SendReward(actor, config) then
  237. -- 保存个人数据
  238. local recordData = {}
  239. if activityType == this.ActivityType.LOGIN then
  240. local now = getbaseinfo("now")
  241. recordData = {time = now}
  242. else
  243. recordData = {type = activitySubType}
  244. end
  245. if table.count(recordData) > 0 then
  246. activityData.record[activitySubType] = recordData
  247. this.SaveBenefitData(actor, activityType, activityData)
  248. end
  249. -- 保存全局数据
  250. if activityType == this.ActivityType.LEVELUP then
  251. local recordData = this.GetGlobalBenefitData(activityType)
  252. if recordData[activitySubType] == nil then
  253. recordData[activitySubType] = {}
  254. end
  255. table.insert(recordData[activitySubType], {rid = getbaseinfo(actor, "rid")})
  256. this.SaveGlobalBenefitData(activityType, recordData)
  257. end
  258. this.SendActivityInfo(actor)
  259. end
  260. end
  261. -- 发送奖励
  262. function this.SendReward(actor, config)
  263. if config == nil then
  264. print("[BenefitActivity.SendReward] configReward is nil")
  265. return false
  266. end
  267. local rewardsMap = string.toIntIntMap(config.reward, "#", "|")
  268. Bag.sendRewards(actor, rewardsMap, "福利大厅")
  269. print("Benefit.Activity.SendReward send reward success")
  270. return true
  271. end