DailyActivity.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. DailyActivity = {}
  2. --------------------------------- 文件注解 ---------------------------
  3. ---@class DailyActivity
  4. ---@field activity_value number
  5. ---@field reset_time string
  6. ---@field daily_task daily_task[]
  7. ---@field daily_box daily_box[]
  8. ---@class daily_task
  9. ---@field task_id number
  10. ---@field target_id number
  11. ---@field taskgoal_type number
  12. ---@field goal_count number
  13. ---@field state number
  14. ---@field task_finish_count number
  15. ---@class daily_box
  16. ---@field box_id number
  17. ---@field ax_num number
  18. ---@field state number
  19. ---@class DailyActivity.Activity
  20. ---@field activityid string
  21. ---@field open boolean
  22. local this = {}
  23. this.box_accept = 0
  24. this.box_received = 1
  25. this.task_accept = 0
  26. this.task_finish = 1
  27. this.item_bind_id = 11
  28. --- 配置表的红点id
  29. this.RedId = 96
  30. function DailyActivity.login(actor)
  31. this.initPlayerDailyActivity(actor)
  32. end
  33. function DailyActivity.getActivityInfo(actor)
  34. this.sendActivityInfo(actor)
  35. end
  36. function DailyActivity.receiveBoxReward(actor, msgData)
  37. this.receiveBoxReward(actor, msgData)
  38. end
  39. function DailyActivity.taskGoalFlush(actor, taskTargetType, param)
  40. local success, errorInfo = xpcall(this.taskGoalFlush, debug.traceback, actor, taskTargetType, param)
  41. gameDebug.assertPrint(success, "日常任务目标刷新异常:", actor, taskTargetType, param, errorInfo)
  42. end
  43. function DailyActivity.getActivityList(actor)
  44. local data = this.getActivityList(actor)
  45. sendluamsg(actor, LuaMessageIdToClient.GET_DAILY_ACTIVITY_ACTIVITY_LIST, data)
  46. end
  47. function DailyActivity.zeroEvent(actor)
  48. this.initPlayerDailyActivity(actor)
  49. end
  50. --------------------------------------------------------
  51. ---@return DailyActivity
  52. function this.getdata(actor)
  53. return getplaydef(actor, "T$_role_daily_activity_data")
  54. end
  55. function this.savedata(actor, data)
  56. setplaydef(actor, "T$_role_daily_activity_data", data)
  57. end
  58. function this.sendActivityInfo(actor)
  59. local data = this.getdata(actor)
  60. sendluamsg(actor, LuaMessageIdToClient.GET_DAILY_ACTIVITY_INFO, data)
  61. end
  62. --- 玩家上线触发小红点计算
  63. ---@param red_data table 红点数据结果存储器
  64. ---@param actor any 玩家对象
  65. function DailyActivity.hasBoxUnReceived(actor)
  66. local data = this.getdata(actor)
  67. local value = data.activity_value
  68. for _, v in pairs(data.daily_box) do
  69. if value >= tonumber(v.ax_num) and v.state == this.box_accept then
  70. return true
  71. end
  72. end
  73. return false
  74. end
  75. function DailyActivity.loginRed(red_data, actor)
  76. if DailyActivity.hasBoxUnReceived(actor) then
  77. --登陆的时候判定 false 可以不发送的
  78. red_data[this.RedId] = true
  79. end
  80. end
  81. function this.initPlayerDailyActivity(actor)
  82. this.getdata(actor)
  83. local can = this.canReset(actor)
  84. if not can then
  85. this.loginfo(actor, "跳过每日目标初始化")
  86. return
  87. end
  88. this.jprint("初始化每日任务")
  89. local daily_task = {}
  90. local configList = ConfigDataManager.getList("cfg_daily_activity")
  91. for _, config in ipairs(configList) do
  92. this.buildActivityTask(actor, config, daily_task)
  93. end
  94. this.jprint("初始化每日宝箱")
  95. local daily_box = {}
  96. local level = getbaseinfo(actor, "level")
  97. local box_configs = ConfigDataManager.getList("cfg_activeness_reward")
  98. for _, config in ipairs(box_configs) do
  99. local level_limit = config.level
  100. local limit_arr = string.split(level_limit, "#")
  101. if tonumber(limit_arr[1]) <= level and level <= tonumber(limit_arr[2]) then
  102. this.jprint("--- 开始初始化宝箱奖励 --- boxId: " .. config.id)
  103. this.buildActivityBox(config, daily_box)
  104. end
  105. end
  106. local reset_time = this.getTimeStr(actor)
  107. ---@type DailyActivity
  108. local role_daily_task_data = {
  109. daily_task = daily_task,
  110. daily_box = daily_box,
  111. activity_value = 0,
  112. reset_time = reset_time
  113. }
  114. this.savedata(actor, role_daily_task_data)
  115. this.loginfo(actor, "每日任务数据保存完成")
  116. this.jprint(role_daily_task_data)
  117. end
  118. function this.getTimeStr(actor)
  119. local year = getbaseinfo(actor, "year")
  120. local month = getbaseinfo(actor, "month")
  121. local day = getbaseinfo(actor, "day")
  122. local reset_time = year .. month .. day
  123. return reset_time
  124. end
  125. function this.canReset(actor)
  126. local data = this.getdata(actor)
  127. if data == nil then
  128. return true
  129. end
  130. if table.isEmpty(data.daily_task) then
  131. return true
  132. end
  133. local last_time = data.reset_time
  134. if last_time == nil or last_time == "" then
  135. return true
  136. end
  137. local now_time = this.getTimeStr(actor)
  138. local time_log = "last_time:" .. last_time .. " now_time:" .. now_time
  139. this.jprint(time_log)
  140. if last_time == now_time then
  141. return false
  142. end
  143. return true
  144. end
  145. function this.buildActivityTask(actor, config, daily_task)
  146. local task_id = config["id"]
  147. local target_id = config["target"]
  148. local taskInfo = this.buildTaskGlobal(actor, task_id, target_id)
  149. if taskInfo == nil then
  150. return
  151. end
  152. table.insert(daily_task, taskInfo)
  153. end
  154. function this.buildTaskGlobal(actor, task_id, target_id)
  155. local targetConfig = ConfigDataManager.getById("cfg_task_target", target_id)
  156. if table.isEmpty(targetConfig) then
  157. this.loginfo(actor, "没有找到任务配置:" .. target_id)
  158. return
  159. end
  160. local taskGoalType = targetConfig["taskgoaltype"]
  161. ---@type daily_task
  162. local taskData = {
  163. task_id = task_id,
  164. target_id = target_id,
  165. taskgoal_type = taskGoalType,
  166. goal_count = 0,
  167. state = this.task_accept,
  168. task_finish_count = 0
  169. }
  170. return taskData
  171. end
  172. function this.buildActivityBox(config, daily_box)
  173. local box_id = config["id"]
  174. local ax_num = config["axnum"]
  175. ---@type daily_box
  176. local box_data = { box_id = box_id, ax_num = ax_num, state = this.box_accept }
  177. table.insert(daily_box, box_data)
  178. end
  179. function this.receiveBoxReward(actor, param)
  180. this.jprint(param)
  181. local box_id = param
  182. local config = ConfigDataManager.getById("cfg_activeness_reward", box_id)
  183. if table.isEmpty(config) then
  184. this.loginfo(actor, "没有找到宝箱配置:" .. box_id)
  185. return
  186. end
  187. local need_num = config["axnum"]
  188. ---@type DailyActivity
  189. local data = this.getdata(actor)
  190. if tonumber(data.activity_value) < tonumber(need_num) then
  191. this.loginfo(actor, "活跃值不足,无法领取宝箱:" .. box_id)
  192. return
  193. end
  194. local box_list = data.daily_box
  195. for _, box in ipairs(box_list) do
  196. if tonumber(box.box_id) == box_id then
  197. if box.state == this.box_received then
  198. this.jprint("宝箱奖励已经领取")
  199. return
  200. end
  201. box.state = this.box_received
  202. end
  203. end
  204. this.jprint(config)
  205. local reward = config["caxitem"]
  206. local reward_item = string.toIntIntMap(reward, "#", "|")
  207. this.jprint("必得道具")
  208. this.jprint(reward_item)
  209. this.getRandomItem(actor, config, reward_item)
  210. this.savedata(actor, data)
  211. local itemBing = ConfigDataManager.getTableValue("cfg_bind", "bind", "id", this.item_bind_id)
  212. additemmaptobag(actor, reward_item, itemBing, 9999, '每日任务')
  213. this.jprint("发放宝箱奖励:")
  214. this.jprint(reward_item)
  215. this.sendActivityInfo(actor)
  216. GameTips.sendGetRewardMsg(actor, reward_item)
  217. end
  218. function this.getRandomItem(actor, config, reward_item)
  219. -- local count = config["randomreward"]
  220. -- if count == nil or count == "" or tonumber(count) < 1 then
  221. -- this.jprint("randomreward: 随机道具数量错误")
  222. -- return
  223. -- end
  224. local count = 1
  225. local random_item_str = config["randomreward"]
  226. if random_item_str == nil or random_item_str == "" then
  227. this.jprint("randomreward: 随机道具错误")
  228. return
  229. end
  230. local reward_map = {}
  231. local reward_weight = {}
  232. local random_item_str_arr = string.split(random_item_str, "|")
  233. for _, random_item_str in pairs(random_item_str_arr) do
  234. local random_item = string.split(random_item_str, "#")
  235. local item_id = random_item[1]
  236. local item_count = random_item[2]
  237. local weight = random_item[3]
  238. reward_weight[item_id] = weight
  239. reward_map[item_id] = item_count
  240. end
  241. local key_set = randombyweight(actor, reward_weight, count)
  242. this.jprint("key_set:")
  243. this.jprint(key_set)
  244. for _, item_id in pairs(key_set) do
  245. local reward_count = reward_item[item_id]
  246. local random_count = reward_map[item_id]
  247. if reward_count ~= nil then
  248. reward_item[item_id] = reward_count + random_count
  249. end
  250. reward_item[item_id] = random_count
  251. end
  252. end
  253. function this.taskGoalFlush(actor, type, param)
  254. this.jprint(" -------- 每日目标任务刷新 ----------")
  255. ---@type DailyActivity
  256. local data = this.getdata(actor)
  257. if data == nil then
  258. return
  259. end
  260. local haschange = false
  261. for index, task in ipairs(data.daily_task) do
  262. local conditions = ConfigDataManager.getTableValue("cfg_daily_activity", "conditions", "id", task.task_id)
  263. if conditions and conditions ~= "" then
  264. local isPass = checkcondition(actor, conditions)
  265. if tonumber(isPass) ~= 1 then
  266. goto continue
  267. end
  268. end
  269. local ischange = this.doFlush(actor, data, task, type, param)
  270. if ischange then
  271. -- 保存task数据
  272. haschange = true
  273. data.daily_task[index] = task
  274. end
  275. :: continue ::
  276. end
  277. if haschange then
  278. this.savedata(actor, data)
  279. if DailyActivity.hasBoxUnReceived(actor) then
  280. RedPoint.sendOneRedPoint(actor, this.RedId, true)
  281. end
  282. end
  283. this.jprint("-------- 每日任务刷新结束 ----------")
  284. end
  285. function this.doFlush(actor, data, task, type, param)
  286. if tonumber(task.state) == tonumber(this.task_finish) then
  287. this.jprint("任务已完成")
  288. return false
  289. end
  290. if tonumber(task.taskgoal_type) ~= tonumber(type) then
  291. return false
  292. end
  293. local count = TaskGoal.FlushTaskCount(actor, type, task.target_id, task.goal_count, param)
  294. if count == nil or tonumber(task.goal_count) == tonumber(count) then
  295. this.jprint("任务进度没有变化")
  296. return false
  297. end
  298. -- 进度变化
  299. local configs = ConfigDataManager.getTable("cfg_daily_activity", "id", task.task_id)
  300. local target_configs = ConfigDataManager.getTable("cfg_task_target", "id", task.target_id)
  301. local config = configs[1]
  302. local target_config = target_configs[1]
  303. local need_count = target_config["goalcount"]
  304. if tonumber(need_count) < 1 then
  305. need_count = 1
  306. end
  307. if tonumber(need_count) > tonumber(count) then
  308. task.goal_count = count
  309. return true
  310. end
  311. -- 每完成一次 重置任务进度
  312. task.goal_count = 0
  313. local timemax = config["timemax"]
  314. local finish_count = task.task_finish_count + 1
  315. if tonumber(finish_count) >= tonumber(timemax) then
  316. task.state = this.task_finish
  317. task.task_finish_count = timemax
  318. else
  319. task.task_finish_count = finish_count
  320. end
  321. -- 发放活跃值
  322. local act_value = config["activity"]
  323. data.activity_value = data.activity_value + act_value
  324. OpenServerCompetition.addActivityValue(actor, act_value)
  325. return true
  326. end
  327. function this.getActivityList(actor)
  328. local act_data = getallactivities(actor)
  329. local data = {}
  330. for _, act in pairs(act_data) do
  331. local activity_data = {
  332. activityid = act.activityid,
  333. open = act.open
  334. }
  335. table.insert(data, activity_data)
  336. end
  337. return data
  338. end
  339. ---------------------------- 日志打印 -----------------------------
  340. this.log_open = false
  341. function this.jprint(param)
  342. if not this.log_open then
  343. return
  344. end
  345. if param == nil then
  346. param = "error! 输出内容为空. nil"
  347. end
  348. jprint(param)
  349. end
  350. function this.loginfo(actor, param)
  351. if not this.log_open then
  352. return
  353. end
  354. if param == nil then
  355. param = "error! 日志输出内容为空. nil"
  356. end
  357. jprint(param)
  358. info(actor, param)
  359. end
  360. this.gm_open = true
  361. function l_daily_activity_init_role_activity_and_box(actor)
  362. if not this.gm_open then
  363. return
  364. end
  365. this.initPlayerDailyActivity(actor)
  366. end
  367. --- 凌晨时间注册
  368. ZeroEventListerTable:eventLister("0", "每日目标凌晨事件", DailyActivity.zeroEvent)
  369. RedPointEventListerTable:eventLister("0", "每日目标", DailyActivity.loginRed)