using CS2_SimpleAdmin.Database;
using CS2_SimpleAdminApi;
using Dapper;
using Microsoft.Extensions.Logging;
namespace CS2_SimpleAdmin.Managers;
internal class MuteManager(IDatabaseProvider? databaseProvider)
{
///
/// Adds a mute entry for a specified player with detailed information.
///
/// Player to be muted.
/// Admin issuing the mute; null if issued from console.
/// Reason for muting the player.
/// Duration of the mute in minutes. Zero means permanent mute.
/// Mute type: 0 = GAG, 1 = MUTE, 2 = SILENCE.
/// Mute ID if successfully added, otherwise null.
public async Task MutePlayer(PlayerInfo player, PlayerInfo? issuer, string reason, int time = 0, int type = 0)
{
if (databaseProvider == null) return null;
var now = Time.ActualDateTime();
var futureTime = now.AddMinutes(time);
var muteType = type switch
{
1 => "MUTE",
2 => "SILENCE",
_ => "GAG"
};
try
{
await using var connection = await databaseProvider.CreateConnectionAsync();
var sql = databaseProvider.GetAddMuteQuery(true);
var muteId = await connection.ExecuteScalarAsync(sql, new
{
playerSteamid = player.SteamId.SteamId64,
playerName = player.Name,
adminSteamid = issuer?.SteamId.SteamId64 ?? 0,
adminName = issuer?.Name ?? CS2_SimpleAdmin._localizer?["sa_console"] ?? "Console",
muteReason = reason,
duration = time,
ends = futureTime,
created = now,
type = muteType,
serverid = CS2_SimpleAdmin.ServerId
});
return muteId;
}
catch (Exception ex)
{
CS2_SimpleAdmin._logger?.LogError(ex.Message);
return null;
}
}
///
/// Adds a mute entry for a offline player identified by their SteamID.
///
/// SteamID64 of the player to mute.
/// Admin issuing the mute; can be null if from console.
/// Reason for the mute.
/// Mute duration in minutes; 0 for permanent.
/// Mute type: 0 = GAG, 1 = MUTE, 2 = SILENCE.
/// Mute ID if successful, otherwise null.
public async Task AddMuteBySteamid(ulong playerSteamId, PlayerInfo? issuer, string reason, int time = 0, int type = 0)
{
if (databaseProvider == null) return null;
var now = Time.ActualDateTime();
var futureTime = now.AddMinutes(time);
var muteType = type switch
{
1 => "MUTE",
2 => "SILENCE",
_ => "GAG"
};
try
{
await using var connection = await databaseProvider.CreateConnectionAsync();
var sql = databaseProvider.GetAddMuteQuery(false);
var muteId = await connection.ExecuteScalarAsync(sql, new
{
playerSteamid = playerSteamId,
adminSteamid = issuer?.SteamId.SteamId64 ?? 0,
adminName = issuer?.Name ?? CS2_SimpleAdmin._localizer?["sa_console"] ?? "Console",
muteReason = reason,
duration = time,
ends = futureTime,
created = now,
type = muteType,
serverid = CS2_SimpleAdmin.ServerId
});
return muteId;
}
catch
{
return null;
}
}
///
/// Checks if a player with the given SteamID currently has any active mutes.
///
/// SteamID64 of the player to check.
/// List of active mute records; empty list if none or on error.
public async Task> IsPlayerMuted(string steamId)
{
if (databaseProvider == null) return [];
if (string.IsNullOrEmpty(steamId))
{
return [];
}
#if DEBUG
if (CS2_SimpleAdmin._logger != null)
CS2_SimpleAdmin._logger.LogCritical($"IsPlayerMuted for {steamId}");
#endif
try
{
await using var connection = await databaseProvider.CreateConnectionAsync();
var currentTime = Time.ActualDateTime();
var sql = databaseProvider.GetIsMutedQuery(CS2_SimpleAdmin.Instance.Config.MultiServerMode, CS2_SimpleAdmin.Instance.Config.OtherSettings.TimeMode);
var parameters = new { PlayerSteamID = steamId, CurrentTime = currentTime, serverid = CS2_SimpleAdmin.ServerId };
var activeMutes = (await connection.QueryAsync(sql, parameters)).ToList();
return activeMutes;
}
catch (Exception)
{
return [];
}
}
///
/// Retrieves counts of total mutes, gags, and silences for a given player.
///
/// Information about the player.
///
/// Tuple containing total mutes, total gags, and total silences respectively.
/// Returns zeros if no data or on error.
///
public async Task<(int TotalMutes, int TotalGags, int TotalSilences)> GetPlayerMutes(PlayerInfo playerInfo)
{
if (databaseProvider == null) return (0,0,0);
try
{
await using var connection = await databaseProvider.CreateConnectionAsync();
var sql = databaseProvider.GetRetrieveMutesQuery(CS2_SimpleAdmin.Instance.Config.MultiServerMode);
var result = await connection.QuerySingleAsync<(int TotalMutes, int TotalGags, int TotalSilences)>(sql, new
{
PlayerSteamID = playerInfo.SteamId.SteamId64,
CS2_SimpleAdmin.ServerId
});
return result;
}
catch (Exception)
{
return (0, 0, 0);
}
}
///
/// Processes a batch of online players to update their mute status and remove expired penalties.
///
/// List of tuples containing player SteamID, optional UserID, and slot index.
/// Task representing the asynchronous operation.
public async Task CheckOnlineModeMutes(List<(ulong SteamID, int? UserId, int Slot)> players)
{
if (databaseProvider == null) return;
try
{
const int batchSize = 20;
await using var connection = await databaseProvider.CreateConnectionAsync();
var sql = databaseProvider.GetUpdateMutePassedQuery(CS2_SimpleAdmin.Instance.Config.MultiServerMode);
for (var i = 0; i < players.Count; i += batchSize)
{
var batch = players.Skip(i).Take(batchSize);
var parametersList = new List