2024-06-24 18:21:59 -04:00
|
|
|
using HermesSocketLibrary.db;
|
|
|
|
using HermesSocketLibrary.Socket.Data;
|
|
|
|
using Npgsql;
|
|
|
|
using ILogger = Serilog.ILogger;
|
|
|
|
|
|
|
|
namespace HermesSocketServer.Socket.Handlers
|
|
|
|
{
|
|
|
|
public class EmoteUsageHandler : ISocketHandler
|
|
|
|
{
|
2024-08-10 15:36:32 -04:00
|
|
|
public int OperationCode { get; } = 8;
|
2024-06-24 18:21:59 -04:00
|
|
|
|
|
|
|
private readonly Database _database;
|
|
|
|
private readonly HashSet<string> _history;
|
|
|
|
private readonly EmoteUsageMessage[] _array;
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
|
|
private int _index;
|
|
|
|
|
|
|
|
public EmoteUsageHandler(Database database, ILogger logger)
|
|
|
|
{
|
|
|
|
_database = database;
|
|
|
|
_logger = logger;
|
|
|
|
_history = new HashSet<string>(101);
|
|
|
|
_array = new EmoteUsageMessage[100];
|
|
|
|
_index = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task Execute<T>(WebSocketUser sender, T message, HermesSocketManager sockets)
|
|
|
|
{
|
2024-08-10 15:36:32 -04:00
|
|
|
if (message is not EmoteUsageMessage data || sender.Id == null)
|
2024-06-24 18:21:59 -04:00
|
|
|
return;
|
|
|
|
|
|
|
|
lock (_logger)
|
|
|
|
{
|
|
|
|
if (_history.Contains(data.MessageId))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_history.Add(data.MessageId);
|
|
|
|
|
|
|
|
if (_index >= _array.Length - 1)
|
|
|
|
_index = -1;
|
|
|
|
|
|
|
|
_index = (_index + 1) % _array.Length;
|
|
|
|
if (_array[_index] != null)
|
|
|
|
_history.Remove(data.MessageId);
|
|
|
|
|
|
|
|
_array[_index] = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rows = 0;
|
|
|
|
string sql = "INSERT INTO \"EmoteUsageHistory\" (timestamp, \"broadcasterId\", \"emoteId\", \"chatterId\") VALUES (@time, @broadcaster, @emote, @chatter)";
|
|
|
|
using (var connection = await _database.DataSource.OpenConnectionAsync())
|
|
|
|
{
|
|
|
|
using (var command = new NpgsqlCommand(sql, connection))
|
|
|
|
{
|
|
|
|
foreach (var entry in data.Emotes)
|
|
|
|
{
|
|
|
|
command.Parameters.Clear();
|
|
|
|
command.Parameters.AddWithValue("time", data.DateTime);
|
|
|
|
command.Parameters.AddWithValue("broadcaster", data.BroadcasterId);
|
|
|
|
command.Parameters.AddWithValue("emote", entry);
|
|
|
|
command.Parameters.AddWithValue("chatter", data.ChatterId);
|
|
|
|
|
|
|
|
await command.PrepareAsync();
|
|
|
|
rows += await command.ExecuteNonQueryAsync();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-10 15:36:32 -04:00
|
|
|
_logger.Debug($"Tracked {rows} emote(s) to history.");
|
2024-06-24 18:21:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|