namespace XFEExtension.NetCore.CyberComm;
///
/// 通信群组控制器
///
public class CyberCommGroupController
{
private readonly List commGroups;
///
/// 刷新
///
public void Refresh()
{
for (int i = commGroups.Count - 1; i >= 0; i--)
{
if (commGroups[i].Count == 0)
{
commGroups.Remove(commGroups[i]);
}
}
}
///
/// 添加群组
///
///
public void AddGroup(CyberCommGroup commGroup)
{
commGroups.Add(commGroup);
}
///
/// 移除群组
///
///
public void RemoveGroup(CyberCommGroup commGroup)
{
commGroups.Remove(commGroup);
}
///
/// 移除指定索引的群组
///
///
public void RemoveGroupAt(int index)
{
commGroups.RemoveAt(index);
}
///
/// 清空群组
///
public void Clear()
{
commGroups.Clear();
}
///
/// 群组数量
///
public int Count
{
get
{
return commGroups.Count;
}
}
///
/// 索引器
///
///
///
public CyberCommGroup this[int index]
{
get
{
return commGroups[index];
}
}
///
/// 索引器
///
///
///
public CyberCommGroup? this[string groupId]
{
get
{
foreach (CyberCommGroup commGroup in commGroups)
{
if (commGroup.GroupId == groupId)
{
return commGroup;
}
}
return null;
}
}
///
/// 通信群组控制器发送文本消息
///
/// 目标群组的ID
/// 发送的文本消息
public async Task SendGroupTextMessage(string groupId, string message)
{
var commGroup = this[groupId];
if (commGroup is not null)
{
await commGroup.SendGroupTextMessage(message);
}
}
///
/// 通信群组控制器发送二进制消息
///
/// 目标群组的ID
/// 发送的二进制消息
public async Task SendGroupBinaryMessage(string groupId, byte[] bytes)
{
var commGroup = this[groupId];
if (commGroup is not null)
{
await commGroup.SendGroupBinaryMessage(bytes);
}
}
///
/// 通信群组控制器
///
public CyberCommGroupController()
{
commGroups = [];
}
}