using System; using System.Collections.Generic; using System.Text.Json; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Hosting; using RainOpsMini.Helpers; using RainOpsMini.Helpers.RCON; namespace RainOpsMini.Plugins { /// /// 循环广播插件 - 定时播放预设的黄字公告 /// public class BroadcastPlugin : BackgroundService, IPlugin { public string Name => "自动广播"; public string Description => "循环广播插件 - 定时播放预设的黄字公告(队列广播)"; public PluginCategory Category => PluginCategory.BasicFunction; public bool IsEnabled => _config.Enabled; private BroadcastConfig _config = new BroadcastConfig(); // 记录当前播放到第几条 private int _currentIndex = 0; /// /// 广播配置项 /// public class BroadcastItem { /// /// 广播内容 /// public string Message { get; set; } = ""; /// /// 本条广播后的等待时间(秒) /// public int IntervalSeconds { get; set; } = 300; } public class BroadcastConfig { /// /// 是否启用 /// public bool Enabled { get; set; } = false; /// /// 本批次全部播放完毕后的等待时间(秒) /// public int BatchIntervalSeconds { get; set; } = 300; /// /// 广播消息队列 /// public List Messages { get; set; } = new List { new BroadcastItem { Message = "欢迎加入本服务器!", IntervalSeconds = 300 } }; } /// /// 获取当前配置 /// /// 配置对象 public object GetConfig() => _config; /// /// 重置配置为默认值 /// public void ResetConfig() { _config = new BroadcastConfig(); _currentIndex = 0; } /// /// 更新配置 /// /// JSON配置元素 public void UpdateConfig(JsonElement config) { try { var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true, ReadCommentHandling = JsonCommentHandling.Skip, AllowTrailingCommas = true }; _config = JsonSerializer.Deserialize(config.GetRawText(), options) ?? new BroadcastConfig(); } catch (Exception ex) { Console.WriteLine($"[BroadcastPlugin] 配置更新失败: {ex.Message}"); } } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { try { if (_config.Enabled && _config.Messages != null && _config.Messages.Count > 0) { // 获取当前消息 if (_currentIndex >= _config.Messages.Count) _currentIndex = 0; var item = _config.Messages[_currentIndex]; string msg = item.Message; // 限制长度 60 字 if (msg.Length > 60) msg = msg.Substring(0, 60); // 发送广播 if (!string.IsNullOrWhiteSpace(msg)) { string cmd = $"AdminBroadcast {msg}"; if (Program.Client != null && Program.Client.IsConnected) { await Program.Client.SendCommandAsync(cmd); RainOpsLog.Log($"[BroadcastPlugin] 发送广播: {msg}"); } } // 指向下一条 _currentIndex++; // 等待间隔 int delay = item.IntervalSeconds; if (delay < 3) delay = 3; // 最小间隔 3 秒 await Task.Delay(delay * 1000, stoppingToken); // 如果本批次播放完毕,等待批次间隔 if (_currentIndex >= _config.Messages.Count) { _currentIndex = 0; int batchDelay = _config.BatchIntervalSeconds; if (batchDelay > 0) { await Task.Delay(batchDelay * 1000, stoppingToken); } } } else { // 如果未启用或无消息,等待较短时间再次检查 await Task.Delay(5000, stoppingToken); } } catch (TaskCanceledException) { break; } catch (Exception ex) { RainOpsLog.Log($"[BroadcastPlugin] 执行异常: {ex.Message}"); await Task.Delay(10000, stoppingToken); // 出错后等待 10 秒 } } } } }