Files
squad-rain-ops-mini/Plugins/BroadcastPlugin.cs

169 lines
5.9 KiB
C#

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
{
/// <summary>
/// 循环广播插件 - 定时播放预设的黄字公告
/// </summary>
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;
/// <summary>
/// 广播配置项
/// </summary>
public class BroadcastItem
{
/// <summary>
/// 广播内容
/// </summary>
public string Message { get; set; } = "";
/// <summary>
/// 本条广播后的等待时间(秒)
/// </summary>
public int IntervalSeconds { get; set; } = 300;
}
public class BroadcastConfig
{
/// <summary>
/// 是否启用
/// </summary>
public bool Enabled { get; set; } = false;
/// <summary>
/// 本批次全部播放完毕后的等待时间(秒)
/// </summary>
public int BatchIntervalSeconds { get; set; } = 300;
/// <summary>
/// 广播消息队列
/// </summary>
public List<BroadcastItem> Messages { get; set; } = new List<BroadcastItem>
{
new BroadcastItem { Message = "欢迎加入本服务器!", IntervalSeconds = 300 }
};
}
/// <summary>
/// 获取当前配置
/// </summary>
/// <returns>配置对象</returns>
public object GetConfig() => _config;
/// <summary>
/// 重置配置为默认值
/// </summary>
public void ResetConfig()
{
_config = new BroadcastConfig();
_currentIndex = 0;
}
/// <summary>
/// 更新配置
/// </summary>
/// <param name="config">JSON配置元素</param>
public void UpdateConfig(JsonElement config)
{
try
{
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
ReadCommentHandling = JsonCommentHandling.Skip,
AllowTrailingCommas = true
};
_config = JsonSerializer.Deserialize<BroadcastConfig>(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 秒
}
}
}
}
}