Compare commits

...

4 Commits

Author SHA1 Message Date
Tom
c373af5281 Fixed some minor things. 2025-01-18 17:34:02 +00:00
Tom
9f884f71ae Added group chatters support to websocket. 2025-01-18 17:33:15 +00:00
Tom
a49e52a6bb Added group support for websockets. 2025-01-18 17:31:51 +00:00
Tom
aed0421843 Fixed getting scene item id from OBS. 2025-01-18 16:37:04 +00:00
14 changed files with 322 additions and 9 deletions

View File

@@ -12,7 +12,7 @@ namespace TwitchChatTTS.Chat.Commands.Limits
public UsagePolicy(ILogger logger)
{
_logger = logger;
_root = new UsagePolicyNode<K>(string.Empty, null, null, logger);
_root = new UsagePolicyNode<K>(string.Empty, null, null, logger, root: true);
}
@@ -104,9 +104,10 @@ namespace TwitchChatTTS.Chat.Commands.Limits
private ILogger _logger;
private object _lock { get; }
public UsagePolicyNode(string name, UsagePolicyLimit? data, UsagePolicyNode<T>? parent, ILogger logger)
public UsagePolicyNode(string name, UsagePolicyLimit? data, UsagePolicyNode<T>? parent, ILogger logger, bool root = false)
{
//ArgumentException.ThrowIfNullOrWhiteSpace(name, nameof(name));
if (!root)
ArgumentException.ThrowIfNullOrWhiteSpace(name, nameof(name));
Name = name;
Limit = data;
_parent = parent;

View File

@@ -24,9 +24,12 @@ namespace TwitchChatTTS.Chat.Groups
_groups.Add(group.Name, group);
}
public void Add(long chatter, string groupName)
public void Add(long chatter, string groupId)
{
_chatters.Add(chatter, new List<string>() { groupName });
if (_chatters.TryGetValue(chatter, out var list))
list.Add(groupId);
else
_chatters.Add(chatter, new List<string>() { groupId });
}
public void Add(long chatter, ICollection<string> groupNames)
@@ -77,6 +80,16 @@ namespace TwitchChatTTS.Chat.Groups
return 0;
}
public void Modify(Group group)
{
_groups[group.Name] = group;
}
public bool Remove(string groupId)
{
return _groups.Remove(groupId);
}
public bool Remove(long chatterId, string groupId)
{
if (_chatters.TryGetValue(chatterId, out var groups))

View File

@@ -12,6 +12,8 @@ namespace TwitchChatTTS.Chat.Groups
IEnumerable<string> GetGroupNamesFor(long chatter);
int GetPriorityFor(long chatter);
int GetPriorityFor(IEnumerable<string> groupIds);
void Modify(Group group);
bool Remove(string groupId);
bool Remove(long chatter, string groupId);
}
}

View File

@@ -0,0 +1,49 @@
using System.Text.Json;
using HermesSocketLibrary.Requests.Messages;
using Serilog;
using TwitchChatTTS.Chat.Groups;
namespace TwitchChatTTS.Hermes.Socket.Requests
{
public class CreateGroupAck : IRequestAck
{
public string Name => "create_group";
private readonly IChatterGroupManager _groups;
private readonly JsonSerializerOptions _options;
private readonly ILogger _logger;
public CreateGroupAck(IChatterGroupManager groups, JsonSerializerOptions options, ILogger logger)
{
_groups = groups;
_options = options;
_logger = logger;
}
public void Acknowledge(string requestId, string? json, IDictionary<string, object>? requestData)
{
if (string.IsNullOrWhiteSpace(json))
{
_logger.Warning($"Group JSON data is null.");
return;
}
var group = JsonSerializer.Deserialize<Group>(json, _options);
if (group == null)
{
_logger.Warning($"Group data is null.");
return;
}
var exists = _groups.Get(group.Id);
if (exists != null)
{
_logger.Warning($"Group id already exists [group id: {exists.Id}][group name: {exists.Name} / {group.Name}][group priority: {exists.Priority} / {group.Priority}]");
return;
}
_logger.Debug($"Adding group [group id: {group.Id}][group name: {group.Name}][group priority: {group.Priority}]");
_groups.Add(group);
_logger.Information($"Group has been created [group id: {group.Id}]");
}
}
}

View File

@@ -0,0 +1,49 @@
using System.Text.Json;
using HermesSocketLibrary.Requests.Messages;
using Serilog;
using TwitchChatTTS.Chat.Groups;
namespace TwitchChatTTS.Hermes.Socket.Requests
{
public class CreateGroupChatterAck : IRequestAck
{
public string Name => "create_group_chatter";
private readonly IChatterGroupManager _groups;
private readonly JsonSerializerOptions _options;
private readonly ILogger _logger;
public CreateGroupChatterAck(IChatterGroupManager groups, JsonSerializerOptions options, ILogger logger)
{
_groups = groups;
_options = options;
_logger = logger;
}
public void Acknowledge(string requestId, string? json, IDictionary<string, object>? requestData)
{
if (string.IsNullOrWhiteSpace(json))
{
_logger.Warning($"Group Chatter JSON data is null.");
return;
}
var groupChatter = JsonSerializer.Deserialize<GroupChatter>(json, _options);
if (groupChatter == null)
{
_logger.Warning($"Group Chatter data is null.");
return;
}
var group = _groups.Get(groupChatter.GroupId);
if (group == null)
{
_logger.Warning($"Group id for chatter does not exists [group id: {groupChatter.GroupId}]");
return;
}
_logger.Debug($"Adding chatter to group [group id: {groupChatter.GroupId}][group name: {group.Name}][chatter id: {groupChatter.ChatterId}][chatter label: {groupChatter.ChatterLabel}]");
_groups.Add(groupChatter.ChatterId, groupChatter.GroupId);
_logger.Information($"Chatter has been added to group [group id: {groupChatter.GroupId}][group name: {group.Name}][chatter id: {groupChatter.ChatterId}][chatter label: {groupChatter.ChatterLabel}]");
}
}
}

View File

@@ -24,10 +24,15 @@ namespace TwitchChatTTS.Hermes.Socket.Requests
public void Acknowledge(string requestId, string? json, IDictionary<string, object>? requestData)
{
if (string.IsNullOrWhiteSpace(json)) {
_logger.Warning($"Policy JSON data is null.");
return;
}
var policy = JsonSerializer.Deserialize<Policy>(json, _options);
if (policy == null)
{
_logger.Warning($"Policy JSON data is null.");
_logger.Warning($"Policy data is null.");
return;
}

View File

@@ -0,0 +1,48 @@
using System.Text.Json;
using Serilog;
using TwitchChatTTS.Chat.Groups;
namespace TwitchChatTTS.Hermes.Socket.Requests
{
public class DeleteGroupAck : IRequestAck
{
public string Name => "delete_group";
private readonly IChatterGroupManager _groups;
private readonly JsonSerializerOptions _options;
private readonly ILogger _logger;
public DeleteGroupAck(IChatterGroupManager groups, JsonSerializerOptions options, ILogger logger)
{
_groups = groups;
_options = options;
_logger = logger;
}
public void Acknowledge(string requestId, string? json, IDictionary<string, object>? requestData)
{
if (requestData == null)
{
_logger.Warning("Request data is null.");
return;
}
var groupId = requestData["group"].ToString();
if (string.IsNullOrEmpty(groupId))
{
_logger.Warning($"Action name is invalid [action name: {groupId}]");
return;
}
var exists = _groups.Get(groupId);
if (exists == null)
{
_logger.Warning($"Group id does not exist [group id: {exists}]");
return;
}
_logger.Debug($"Removing group [group id: {exists.Id}][group name: {exists.Name}][group priority: {exists.Priority}]");
_groups.Remove(exists.Id);
_logger.Information($"Group has been updated [group id: {exists.Id}]");
}
}
}

View File

@@ -0,0 +1,51 @@
using Serilog;
using TwitchChatTTS.Chat.Groups;
namespace TwitchChatTTS.Hermes.Socket.Requests
{
public class DeleteGroupChatterAck : IRequestAck
{
public string Name => "delete_group_chatter";
private readonly IChatterGroupManager _groups;
private readonly ILogger _logger;
public DeleteGroupChatterAck(IChatterGroupManager groups, ILogger logger)
{
_groups = groups;
_logger = logger;
}
public void Acknowledge(string requestId, string? json, IDictionary<string, object>? requestData)
{
if (requestData == null)
{
_logger.Warning("Request data is null.");
return;
}
if (long.TryParse(requestData["chatter"].ToString(), out var chatterId))
{
_logger.Warning($"Chatter Id is invalid [chatter id: {chatterId}]");
return;
}
var groupId = requestData["group"].ToString();
if (string.IsNullOrWhiteSpace(groupId))
{
_logger.Warning($"Group Id is invalid [group id: {groupId}]");
return;
}
var group = _groups.Get(groupId);
if (group == null)
{
_logger.Warning($"Group id does not exist [group id: {groupId}]");
return;
}
_logger.Debug($"Deleting chatter from group [group id: {group.Id}][chatter id: {chatterId}][group name: {group.Name}][group priority: {group.Priority}]");
_groups.Remove(chatterId, groupId);
_logger.Information($"Chatter has been deleted from group [group id: {group.Id}][chatter id: {chatterId}]");
}
}
}

View File

@@ -20,6 +20,12 @@ namespace TwitchChatTTS.Hermes.Socket.Requests
public void Acknowledge(string requestId, string? json, IDictionary<string, object>? requestData)
{
if (json == null)
{
_logger.Warning("Policy JSON data is null.");
return;
}
var data = json.Split('/');
if (data.Length != 2)
{

View File

@@ -0,0 +1,49 @@
using System.Text.Json;
using HermesSocketLibrary.Requests.Messages;
using Serilog;
using TwitchChatTTS.Chat.Groups;
namespace TwitchChatTTS.Hermes.Socket.Requests
{
public class UpdateGroupAck : IRequestAck
{
public string Name => "create_group";
private readonly IChatterGroupManager _groups;
private readonly JsonSerializerOptions _options;
private readonly ILogger _logger;
public UpdateGroupAck(IChatterGroupManager groups, JsonSerializerOptions options, ILogger logger)
{
_groups = groups;
_options = options;
_logger = logger;
}
public void Acknowledge(string requestId, string? json, IDictionary<string, object>? requestData)
{
if (string.IsNullOrWhiteSpace(json))
{
_logger.Warning($"Group JSON data is null.");
return;
}
var group = JsonSerializer.Deserialize<Group>(json, _options);
if (group == null)
{
_logger.Warning($"Group data is null.");
return;
}
var exists = _groups.Get(group.Id);
if (exists == null)
{
_logger.Warning($"Group id does not exist [group id: {group.Id}][group name: {group.Name}][group priority: {group.Priority}]");
return;
}
_logger.Debug($"Updating group [group id: {group.Id}][group name: {group.Name}][group priority: {group.Priority}]");
_groups.Modify(group);
_logger.Information($"Group has been updated [group id: {group.Id}]");
}
}
}

View File

@@ -0,0 +1,40 @@
using System.Text.Json;
using HermesSocketLibrary.Requests.Messages;
using Serilog;
using TwitchChatTTS.Chat.Groups;
namespace TwitchChatTTS.Hermes.Socket.Requests
{
public class UpdateGroupChatterAck : IRequestAck
{
public string Name => "update_group_chatter";
private readonly IChatterGroupManager _groups;
private readonly JsonSerializerOptions _options;
private readonly ILogger _logger;
public UpdateGroupChatterAck(IChatterGroupManager groups, JsonSerializerOptions options, ILogger logger)
{
_groups = groups;
_options = options;
_logger = logger;
}
public void Acknowledge(string requestId, string? json, IDictionary<string, object>? requestData)
{
if (string.IsNullOrWhiteSpace(json)) {
_logger.Warning($"Group Chatter JSON data is null.");
return;
}
var groupChatter = JsonSerializer.Deserialize<GroupChatter>(json, _options);
if (groupChatter == null)
{
_logger.Warning($"Group Chatter data is null.");
return;
}
_groups.Add(groupChatter.ChatterId, groupChatter.GroupId);
_logger.Information($"Chatter has been updated [chatter label: {groupChatter.ChatterLabel}]");
}
}
}

View File

@@ -4,6 +4,6 @@ namespace TwitchChatTTS.OBS.Socket.Data
{
public required string EventType { get; set; }
public int EventIntent { get; set; }
public required Dictionary<string, object> EventData { get; set; }
public Dictionary<string, object>? EventData { get; set; }
}
}

View File

@@ -5,6 +5,6 @@ namespace TwitchChatTTS.OBS.Socket.Data
public required string RequestType { get; set; }
public required string RequestId { get; set; }
public required object RequestStatus { get; set; }
public required Dictionary<string, object> ResponseData { get; set; }
public Dictionary<string, object>? ResponseData { get; set; }
}
}

View File

@@ -68,7 +68,7 @@ namespace TwitchChatTTS.OBS.Socket.Handlers
}
_logger.Debug($"Found the scene item by name [scene: {sceneName}][source: {sourceName}][id: {sceneItemId}][obs request id: {message.RequestId}].");
obs.AddSourceId(sourceName.ToString()!, (long)sceneItemId);
obs.AddSourceId(sourceName.ToString()!, long.Parse(sceneItemId.ToString()!));
requestData.ResponseValues = message.ResponseData;
break;