Fixed TTS using StreamElements. Fixed several issues.
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
|
||||
using System.Collections.Concurrent;
|
||||
using HermesSocketLibrary.Requests.Messages;
|
||||
using Serilog;
|
||||
|
||||
@@ -9,109 +7,199 @@ namespace TwitchChatTTS.Chat.Groups
|
||||
{
|
||||
private readonly IDictionary<string, Group> _groups;
|
||||
private readonly IDictionary<long, ICollection<string>> _chatters;
|
||||
private readonly ReaderWriterLockSlim _rwls;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
|
||||
public ChatterGroupManager(ILogger logger)
|
||||
{
|
||||
_logger = logger;
|
||||
_groups = new ConcurrentDictionary<string, Group>();
|
||||
_chatters = new ConcurrentDictionary<long, ICollection<string>>();
|
||||
_groups = new Dictionary<string, Group>();
|
||||
_chatters = new Dictionary<long, ICollection<string>>();
|
||||
_rwls = new ReaderWriterLockSlim();
|
||||
}
|
||||
|
||||
public void Add(Group group)
|
||||
{
|
||||
_groups.Add(group.Id, group);
|
||||
_rwls.EnterWriteLock();
|
||||
try
|
||||
{
|
||||
_groups.Add(group.Id, group);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_rwls.ExitWriteLock();
|
||||
}
|
||||
}
|
||||
|
||||
public void Add(long chatterId, string groupId)
|
||||
{
|
||||
if (_chatters.TryGetValue(chatterId, out var list))
|
||||
_rwls.EnterWriteLock();
|
||||
try
|
||||
{
|
||||
if (!list.Contains(groupId))
|
||||
list.Add(groupId);
|
||||
if (_chatters.TryGetValue(chatterId, out var list))
|
||||
{
|
||||
if (!list.Contains(groupId))
|
||||
list.Add(groupId);
|
||||
}
|
||||
else
|
||||
_chatters.Add(chatterId, new List<string>() { groupId });
|
||||
}
|
||||
finally
|
||||
{
|
||||
_rwls.ExitWriteLock();
|
||||
}
|
||||
else
|
||||
_chatters.Add(chatterId, new List<string>() { groupId });
|
||||
}
|
||||
|
||||
public void Add(long chatter, ICollection<string> groupIds)
|
||||
{
|
||||
if (_chatters.TryGetValue(chatter, out var list))
|
||||
_rwls.EnterWriteLock();
|
||||
try
|
||||
{
|
||||
foreach (var groupId in groupIds)
|
||||
if (!list.Contains(groupId))
|
||||
list.Add(groupId);
|
||||
if (_chatters.TryGetValue(chatter, out var list))
|
||||
{
|
||||
foreach (var groupId in groupIds)
|
||||
if (!list.Contains(groupId))
|
||||
list.Add(groupId);
|
||||
}
|
||||
else
|
||||
_chatters.Add(chatter, groupIds);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_rwls.ExitWriteLock();
|
||||
}
|
||||
else
|
||||
_chatters.Add(chatter, groupIds);
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
_groups.Clear();
|
||||
_chatters.Clear();
|
||||
_rwls.EnterWriteLock();
|
||||
try
|
||||
{
|
||||
_groups.Clear();
|
||||
_chatters.Clear();
|
||||
}
|
||||
finally
|
||||
{
|
||||
_rwls.ExitWriteLock();
|
||||
}
|
||||
}
|
||||
|
||||
public Group? Get(string groupId)
|
||||
{
|
||||
if (_groups.TryGetValue(groupId, out var group))
|
||||
return group;
|
||||
return null;
|
||||
_rwls.EnterReadLock();
|
||||
try
|
||||
{
|
||||
if (_groups.TryGetValue(groupId, out var group))
|
||||
return group;
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_rwls.ExitReadLock();
|
||||
}
|
||||
}
|
||||
|
||||
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>();
|
||||
_rwls.EnterReadLock();
|
||||
try
|
||||
{
|
||||
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>();
|
||||
return Array.Empty<string>();
|
||||
}
|
||||
finally
|
||||
{
|
||||
_rwls.ExitReadLock();
|
||||
}
|
||||
}
|
||||
|
||||
public int GetPriorityFor(long chatter)
|
||||
{
|
||||
if (!_chatters.TryGetValue(chatter, out var groups))
|
||||
return 0;
|
||||
_rwls.EnterReadLock();
|
||||
try
|
||||
{
|
||||
if (!_chatters.TryGetValue(chatter, out var groups))
|
||||
return 0;
|
||||
|
||||
return GetPriorityFor(groups);
|
||||
return GetPriorityFor(groups);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_rwls.ExitReadLock();
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
_rwls.EnterReadLock();
|
||||
try
|
||||
{
|
||||
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;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_rwls.ExitReadLock();
|
||||
}
|
||||
}
|
||||
|
||||
public void Modify(Group group)
|
||||
{
|
||||
_groups[group.Id] = group;
|
||||
_rwls.EnterWriteLock();
|
||||
try
|
||||
{
|
||||
_groups[group.Id] = group;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_rwls.ExitWriteLock();
|
||||
}
|
||||
}
|
||||
|
||||
public bool Remove(string groupId)
|
||||
{
|
||||
if (_groups.Remove(groupId))
|
||||
_rwls.EnterWriteLock();
|
||||
try
|
||||
{
|
||||
foreach (var entry in _chatters)
|
||||
entry.Value.Remove(groupId);
|
||||
return true;
|
||||
if (_groups.Remove(groupId))
|
||||
{
|
||||
foreach (var entry in _chatters)
|
||||
entry.Value.Remove(groupId);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_rwls.ExitReadLock();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool Remove(long chatterId, string groupId)
|
||||
{
|
||||
if (_chatters.TryGetValue(chatterId, out var groups))
|
||||
_rwls.EnterWriteLock();
|
||||
try
|
||||
{
|
||||
groups.Remove(groupId);
|
||||
_logger.Debug($"Removed chatter from group [chatter id: {chatterId}][group name: {_groups[groupId].Name}][group id: {groupId}]");
|
||||
return true;
|
||||
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;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_rwls.ExitReadLock();
|
||||
}
|
||||
_logger.Debug($"Failed to remove chatter from group [chatter id: {chatterId}][group name: {_groups[groupId].Name}][group id: {groupId}]");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user