ActivityManager.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. -- 活动管理器
  2. -- 整合世界BOSS、攻城战、怪物军团等活动系统
  3. local this = {}
  4. -- 导入活动模块
  5. -- 活动类型
  6. local ACTIVITY_TYPE = {
  7. WORLD_BOSS = 1, -- 世界BOSS争夺战
  8. SIEGE_WAR = 2, -- 攻城战
  9. MONSTER_LEGION = 3 -- 怪物军团
  10. }
  11. -- 活动配置
  12. local ACTIVITY_CONFIG = {
  13. [ACTIVITY_TYPE.WORLD_BOSS] = {
  14. name = "世界BOSS争夺战",
  15. description = "每周日晚8点开启,所有玩家均可报名参加",
  16. module = WorldBoss,
  17. enabled = true
  18. },
  19. [ACTIVITY_TYPE.SIEGE_WAR] = {
  20. name = "攻城战",
  21. description = "开服2周后开启,战盟争夺城堡控制权",
  22. module = SiegeWar,
  23. enabled = true
  24. },
  25. [ACTIVITY_TYPE.MONSTER_LEGION] = {
  26. name = "怪物军团",
  27. description = "每周三、周五晚上7点开启,抵御怪物入侵",
  28. module = MonsterLegion,
  29. enabled = true
  30. }
  31. }
  32. -- 初始化所有活动系统
  33. function this.Init()
  34. info("活动管理器初始化开始")
  35. -- 初始化各个活动系统
  36. for activityType, config in pairs(ACTIVITY_CONFIG) do
  37. if config.enabled and config.module and config.module.Init then
  38. local success, err = pcall(config.module.Init)
  39. if success then
  40. info("活动系统初始化成功:", config.name)
  41. else
  42. error("活动系统初始化失败:", config.name, ",错误:", err)
  43. end
  44. end
  45. end
  46. -- 启动活动检查定时器
  47. this.StartActivityCheckTimer()
  48. info("活动管理器初始化完成")
  49. end
  50. -- 启动活动检查定时器
  51. function this.StartActivityCheckTimer()
  52. -- 每30秒检查一次活动状态
  53. setTimer(30000, function()
  54. this.CheckAllActivities()
  55. end)
  56. end
  57. -- 检查所有活动状态
  58. function this.CheckAllActivities()
  59. for activityType, config in pairs(ACTIVITY_CONFIG) do
  60. if config.enabled and config.module then
  61. -- 这里可以添加活动状态检查逻辑
  62. -- 例如:检查活动是否异常、是否需要重启等
  63. end
  64. end
  65. end
  66. -- 获取活动信息
  67. function this.GetActivityInfo(activityType)
  68. local config = ACTIVITY_CONFIG[activityType]
  69. if not config or not config.enabled then
  70. return nil
  71. end
  72. local module = config.module
  73. local info = {
  74. type = activityType,
  75. name = config.name,
  76. description = config.description,
  77. status = "unknown",
  78. phase = "unknown",
  79. timeRemaining = 0,
  80. participants = 0
  81. }
  82. -- 获取活动状态
  83. if module.GetActivityStatus then
  84. info.status = module.GetActivityStatus()
  85. end
  86. -- 获取当前阶段
  87. if module.GetCurrentPhase then
  88. info.phase = module.GetCurrentPhase()
  89. end
  90. -- 获取剩余时间
  91. if module.GetActivityRemainingTime then
  92. info.timeRemaining = module.GetActivityRemainingTime()
  93. end
  94. -- 获取参与人数
  95. if module.GetParticipantCount then
  96. info.participants = module.GetParticipantCount()
  97. end
  98. return info
  99. end
  100. -- 获取所有活动信息
  101. function this.GetAllActivitiesInfo()
  102. local activities = {}
  103. for activityType, config in pairs(ACTIVITY_CONFIG) do
  104. if config.enabled then
  105. local info = this.GetActivityInfo(activityType)
  106. if info then
  107. table.insert(activities, info)
  108. end
  109. end
  110. end
  111. return activities
  112. end
  113. -- 启动指定活动
  114. function this.StartActivity(activityType)
  115. local config = ACTIVITY_CONFIG[activityType]
  116. if not config or not config.enabled then
  117. return false, "活动不存在或已禁用"
  118. end
  119. local module = config.module
  120. if module.StartActivity then
  121. local success, err = pcall(module.StartActivity)
  122. if success then
  123. info("活动启动成功:", config.name)
  124. return true
  125. else
  126. error("活动启动失败:", config.name, ",错误:", err)
  127. return false, "活动启动失败:" .. err
  128. end
  129. else
  130. return false, "活动模块不支持启动功能"
  131. end
  132. end
  133. -- 停止指定活动
  134. function this.StopActivity(activityType)
  135. local config = ACTIVITY_CONFIG[activityType]
  136. if not config or not config.enabled then
  137. return false, "活动不存在或已禁用"
  138. end
  139. local module = config.module
  140. if module.EndActivity then
  141. local success, err = pcall(module.EndActivity)
  142. if success then
  143. info("活动停止成功:", config.name)
  144. return true
  145. else
  146. error("活动停止失败:", config.name, ",错误:", err)
  147. return false, "活动停止失败:" .. err
  148. end
  149. else
  150. return false, "活动模块不支持停止功能"
  151. end
  152. end
  153. -- 玩家参与活动
  154. function this.PlayerJoinActivity(actor, activityType)
  155. local config = ACTIVITY_CONFIG[activityType]
  156. if not config or not config.enabled then
  157. return false, "活动不存在或已禁用"
  158. end
  159. local module = config.module
  160. -- 根据活动类型调用相应的报名函数
  161. if activityType == ACTIVITY_TYPE.WORLD_BOSS then
  162. if module.PlayerRegister then
  163. return module.PlayerRegister(actor)
  164. end
  165. elseif activityType == ACTIVITY_TYPE.SIEGE_WAR then
  166. if module.AllianceRegister then
  167. return module.AllianceRegister(actor)
  168. end
  169. elseif activityType == ACTIVITY_TYPE.MONSTER_LEGION then
  170. if module.PlayerRegister then
  171. return module.PlayerRegister(actor)
  172. end
  173. end
  174. return false, "活动不支持报名功能"
  175. end
  176. -- 检查玩家是否可参与活动
  177. function this.CanPlayerJoinActivity(actor, activityType)
  178. local config = ACTIVITY_CONFIG[activityType]
  179. if not config or not config.enabled then
  180. return false, "活动不存在或已禁用"
  181. end
  182. local module = config.module
  183. if module.CanPlayerParticipate then
  184. return module.CanPlayerParticipate(actor)
  185. end
  186. return false, "活动不支持参与检查"
  187. end
  188. -- 获取活动排行榜
  189. function this.GetActivityRanking(activityType, rankingType)
  190. local config = ACTIVITY_CONFIG[activityType]
  191. if not config or not config.enabled then
  192. return {}
  193. end
  194. local module = config.module
  195. if rankingType == "player" then
  196. if module.GetPlayerScoreRanking then
  197. return module.GetPlayerScoreRanking()
  198. elseif module.GetPlayerDamageRanking then
  199. return module.GetPlayerDamageRanking()
  200. end
  201. elseif rankingType == "alliance" then
  202. if module.GetAllianceScoreRanking then
  203. return module.GetAllianceScoreRanking()
  204. elseif module.GetAllianceDamageRanking then
  205. return module.GetAllianceDamageRanking()
  206. end
  207. end
  208. return {}
  209. end
  210. -- 获取活动详细状态
  211. function this.GetActivityDetailedStatus(activityType)
  212. local config = ACTIVITY_CONFIG[activityType]
  213. if not config or not config.enabled then
  214. return nil
  215. end
  216. local module = config.module
  217. local status = {
  218. type = activityType,
  219. name = config.name,
  220. enabled = config.enabled
  221. }
  222. -- 获取活动状态
  223. if module.GetActivityStatus then
  224. status.status = module.GetActivityStatus()
  225. end
  226. -- 获取当前阶段
  227. if module.GetCurrentPhase then
  228. status.phase = module.GetCurrentPhase()
  229. end
  230. -- 获取剩余时间
  231. if module.GetActivityRemainingTime then
  232. status.timeRemaining = module.GetActivityRemainingTime()
  233. end
  234. -- 获取参与人数
  235. if module.GetParticipantCount then
  236. status.participants = module.GetParticipantCount()
  237. end
  238. -- 根据活动类型获取特定信息
  239. if activityType == ACTIVITY_TYPE.WORLD_BOSS then
  240. if module.GetBossHPInfo then
  241. status.bossHP = module.GetBossHPInfo()
  242. end
  243. if module.GetCurrentBoss then
  244. status.currentBoss = module.GetCurrentBoss()
  245. end
  246. elseif activityType == ACTIVITY_TYPE.SIEGE_WAR then
  247. if module.GetCastleHPInfo then
  248. status.castleHP = module.GetCastleHPInfo()
  249. end
  250. elseif activityType == ACTIVITY_TYPE.MONSTER_LEGION then
  251. if module.GetCurrentWave then
  252. status.currentWave = module.GetCurrentWave()
  253. end
  254. if module.GetActivityStats then
  255. status.stats = module.GetActivityStats()
  256. end
  257. end
  258. return status
  259. end
  260. -- 发送活动公告
  261. function this.SendActivityNotice(activityType, message)
  262. local config = ACTIVITY_CONFIG[activityType]
  263. if not config or not config.enabled then
  264. return false
  265. end
  266. local module = config.module
  267. if module.SendWorldNotice then
  268. module.SendWorldNotice(message)
  269. return true
  270. end
  271. return false
  272. end
  273. -- 获取活动配置
  274. function this.GetActivityConfig(activityType)
  275. local config = ACTIVITY_CONFIG[activityType]
  276. if not config then
  277. return nil
  278. end
  279. return {
  280. type = activityType,
  281. name = config.name,
  282. description = config.description,
  283. enabled = config.enabled
  284. }
  285. end
  286. -- 获取所有活动配置
  287. function this.GetAllActivityConfigs()
  288. local configs = {}
  289. for activityType, config in pairs(ACTIVITY_CONFIG) do
  290. table.insert(configs, this.GetActivityConfig(activityType))
  291. end
  292. return configs
  293. end
  294. -- 启用/禁用活动
  295. function this.SetActivityEnabled(activityType, enabled)
  296. local config = ACTIVITY_CONFIG[activityType]
  297. if not config then
  298. return false, "活动不存在"
  299. end
  300. config.enabled = enabled
  301. if enabled then
  302. info("活动已启用:", config.name)
  303. else
  304. info("活动已禁用:", config.name)
  305. end
  306. return true
  307. end
  308. -- 重启活动系统
  309. function this.RestartActivitySystem(activityType)
  310. local config = ACTIVITY_CONFIG[activityType]
  311. if not config or not config.enabled then
  312. return false, "活动不存在或已禁用"
  313. end
  314. local module = config.module
  315. -- 停止当前活动
  316. if module.EndActivity then
  317. pcall(module.EndActivity)
  318. end
  319. -- 重新初始化
  320. if module.Init then
  321. local success, err = pcall(module.Init)
  322. if success then
  323. info("活动系统重启成功:", config.name)
  324. return true
  325. else
  326. error("活动系统重启失败:", config.name, ",错误:", err)
  327. return false, "活动系统重启失败:" .. err
  328. end
  329. end
  330. return false, "活动模块不支持重启功能"
  331. end
  332. -- 获取活动统计信息
  333. function this.GetActivityStatistics()
  334. local stats = {
  335. totalActivities = 0,
  336. enabledActivities = 0,
  337. activeActivities = 0,
  338. activities = {}
  339. }
  340. for activityType, config in pairs(ACTIVITY_CONFIG) do
  341. stats.totalActivities = stats.totalActivities + 1
  342. if config.enabled then
  343. stats.enabledActivities = stats.enabledActivities + 1
  344. local status = this.GetActivityDetailedStatus(activityType)
  345. if status and status.status and status.status > 0 then
  346. stats.activeActivities = stats.activeActivities + 1
  347. end
  348. table.insert(stats.activities, {
  349. type = activityType,
  350. name = config.name,
  351. status = status
  352. })
  353. end
  354. end
  355. return stats
  356. end
  357. -- 清理活动数据
  358. function this.CleanupActivityData(activityType)
  359. local config = ACTIVITY_CONFIG[activityType]
  360. if not config or not config.enabled then
  361. return false, "活动不存在或已禁用"
  362. end
  363. local module = config.module
  364. if module.CleanupActivityData then
  365. local success, err = pcall(module.CleanupActivityData)
  366. if success then
  367. info("活动数据清理成功:", config.name)
  368. return true
  369. else
  370. error("活动数据清理失败:", config.name, ",错误:", err)
  371. return false, "活动数据清理失败:" .. err
  372. end
  373. end
  374. return false, "活动模块不支持数据清理功能"
  375. end
  376. -- 清理所有活动数据
  377. function this.CleanupAllActivityData()
  378. local results = {}
  379. for activityType, config in pairs(ACTIVITY_CONFIG) do
  380. if config.enabled then
  381. local success, message = this.CleanupActivityData(activityType)
  382. results[activityType] = {
  383. success = success,
  384. message = message
  385. }
  386. end
  387. end
  388. return results
  389. end
  390. -- 导出模块
  391. return this