117 lines
3.5 KiB
C#
117 lines
3.5 KiB
C#
|
|
using System.Collections.Concurrent;
|
|
using HermesSocketLibrary.Requests.Messages;
|
|
using Serilog;
|
|
|
|
namespace TwitchChatTTS.Chat.Groups
|
|
{
|
|
public class ChatterGroupManager : IChatterGroupManager
|
|
{
|
|
private readonly IDictionary<string, Group> _groups;
|
|
private readonly IDictionary<long, ICollection<string>> _chatters;
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
public ChatterGroupManager(ILogger logger)
|
|
{
|
|
_logger = logger;
|
|
_groups = new ConcurrentDictionary<string, Group>();
|
|
_chatters = new ConcurrentDictionary<long, ICollection<string>>();
|
|
}
|
|
|
|
public void Add(Group group)
|
|
{
|
|
_groups.Add(group.Id, group);
|
|
}
|
|
|
|
public void Add(long chatterId, string groupId)
|
|
{
|
|
if (_chatters.TryGetValue(chatterId, out var list))
|
|
{
|
|
if (!list.Contains(groupId))
|
|
list.Add(groupId);
|
|
}
|
|
else
|
|
_chatters.Add(chatterId, new List<string>() { groupId });
|
|
}
|
|
|
|
public void Add(long chatter, ICollection<string> groupIds)
|
|
{
|
|
if (_chatters.TryGetValue(chatter, out var list))
|
|
{
|
|
foreach (var groupId in groupIds)
|
|
if (!list.Contains(groupId))
|
|
list.Add(groupId);
|
|
}
|
|
else
|
|
_chatters.Add(chatter, groupIds);
|
|
}
|
|
|
|
public void Clear()
|
|
{
|
|
_groups.Clear();
|
|
_chatters.Clear();
|
|
}
|
|
|
|
public Group? Get(string groupId)
|
|
{
|
|
if (_groups.TryGetValue(groupId, out var group))
|
|
return group;
|
|
return null;
|
|
}
|
|
|
|
public IEnumerable<string> GetGroupNamesFor(long chatter)
|
|
{
|
|
if (_chatters.TryGetValue(chatter, out var groups))
|
|
return groups.Select(g => _groups.TryGetValue(g, out var group) ? group.Name : null)
|
|
.Where(g => g != null)
|
|
.Cast<string>();
|
|
|
|
return Array.Empty<string>();
|
|
}
|
|
|
|
public int GetPriorityFor(long chatter)
|
|
{
|
|
if (!_chatters.TryGetValue(chatter, out var groups))
|
|
return 0;
|
|
|
|
return GetPriorityFor(groups);
|
|
}
|
|
|
|
public int GetPriorityFor(IEnumerable<string> groupIds)
|
|
{
|
|
var values = groupIds.Select(g => _groups.TryGetValue(g, out var group) ? group : null).Where(g => g != null);
|
|
if (values.Any())
|
|
return values.Max(g => g!.Priority);
|
|
return 0;
|
|
}
|
|
|
|
public void Modify(Group group)
|
|
{
|
|
_groups[group.Id] = group;
|
|
}
|
|
|
|
public bool Remove(string groupId)
|
|
{
|
|
if (_groups.Remove(groupId))
|
|
{
|
|
foreach (var entry in _chatters)
|
|
entry.Value.Remove(groupId);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public bool Remove(long chatterId, string groupId)
|
|
{
|
|
if (_chatters.TryGetValue(chatterId, out var groups))
|
|
{
|
|
groups.Remove(groupId);
|
|
_logger.Debug($"Removed chatter from group [chatter id: {chatterId}][group name: {_groups[groupId].Name}][group id: {groupId}]");
|
|
return true;
|
|
}
|
|
_logger.Debug($"Failed to remove chatter from group [chatter id: {chatterId}][group name: {_groups[groupId].Name}][group id: {groupId}]");
|
|
return false;
|
|
}
|
|
}
|
|
} |