mirror of
https://github.com/daffyyyy/CS2-SimpleAdmin.git
synced 2026-02-24 04:17:47 +00:00
1.3.9a
**MAJOR UPDATE** - New database schema - Added `css_admins_flags` table - Added `css_unmutes` table - Added `css_unbans` table
This commit is contained in:
@@ -4,20 +4,14 @@ using MySqlConnector;
|
||||
|
||||
namespace CS2_SimpleAdmin;
|
||||
|
||||
internal class MuteManager
|
||||
internal class MuteManager(Database database)
|
||||
{
|
||||
private readonly Database _database;
|
||||
|
||||
public MuteManager(Database database)
|
||||
{
|
||||
_database = database;
|
||||
}
|
||||
private readonly Database _database = database;
|
||||
|
||||
public async Task MutePlayer(PlayerInfo player, PlayerInfo issuer, string reason, int time = 0, int type = 0)
|
||||
{
|
||||
if (player == null || player.SteamId == null) return;
|
||||
|
||||
await using MySqlConnection connection = await _database.GetConnectionAsync();
|
||||
|
||||
DateTime now = DateTime.UtcNow.ToLocalTime();
|
||||
DateTime futureTime = now.AddMinutes(time).ToLocalTime();
|
||||
@@ -28,29 +22,33 @@ internal class MuteManager
|
||||
else if (type == 2)
|
||||
muteType = "SILENCE";
|
||||
|
||||
var sql = "INSERT INTO `sa_mutes` (`player_steamid`, `player_name`, `admin_steamid`, `admin_name`, `reason`, `duration`, `ends`, `created`, `type`, `server_id`) " +
|
||||
"VALUES (@playerSteamid, @playerName, @adminSteamid, @adminName, @banReason, @duration, @ends, @created, @type, @serverid)";
|
||||
|
||||
await connection.ExecuteAsync(sql, new
|
||||
try
|
||||
{
|
||||
playerSteamid = player.SteamId,
|
||||
playerName = player.Name,
|
||||
adminSteamid = issuer.SteamId == null ? "Console" : issuer.SteamId,
|
||||
adminName = issuer.SteamId == null ? "Console" : issuer.Name,
|
||||
banReason = reason,
|
||||
duration = time,
|
||||
ends = futureTime,
|
||||
created = now,
|
||||
type = muteType,
|
||||
serverid = CS2_SimpleAdmin.ServerId
|
||||
});
|
||||
await using MySqlConnection connection = await _database.GetConnectionAsync();
|
||||
var sql = "INSERT INTO `sa_mutes` (`player_steamid`, `player_name`, `admin_steamid`, `admin_name`, `reason`, `duration`, `ends`, `created`, `type`, `server_id`) " +
|
||||
"VALUES (@playerSteamid, @playerName, @adminSteamid, @adminName, @muteReason, @duration, @ends, @created, @type, @serverid)";
|
||||
|
||||
await connection.ExecuteAsync(sql, new
|
||||
{
|
||||
playerSteamid = player.SteamId,
|
||||
playerName = player.Name,
|
||||
adminSteamid = issuer.SteamId ?? "Console",
|
||||
adminName = issuer.SteamId == null ? "Console" : issuer.Name,
|
||||
muteReason = reason,
|
||||
duration = time,
|
||||
ends = futureTime,
|
||||
created = now,
|
||||
type = muteType,
|
||||
serverid = CS2_SimpleAdmin.ServerId
|
||||
});
|
||||
}
|
||||
catch { };
|
||||
}
|
||||
|
||||
public async Task AddMuteBySteamid(string playerSteamId, PlayerInfo issuer, string reason, int time = 0, int type = 0)
|
||||
{
|
||||
if (string.IsNullOrEmpty(playerSteamId)) return;
|
||||
|
||||
await using MySqlConnection connection = await _database.GetConnectionAsync();
|
||||
|
||||
DateTime now = DateTime.UtcNow.ToLocalTime();
|
||||
DateTime futureTime = now.AddMinutes(time).ToLocalTime();
|
||||
@@ -61,28 +59,33 @@ internal class MuteManager
|
||||
else if (type == 2)
|
||||
muteType = "SILENCE";
|
||||
|
||||
var sql = "INSERT INTO `sa_mutes` (`player_steamid`, `admin_steamid`, `admin_name`, `reason`, `duration`, `ends`, `created`, `type`, `server_id`) " +
|
||||
"VALUES (@playerSteamid, @adminSteamid, @adminName, @banReason, @duration, @ends, @created, @type, @serverid)";
|
||||
|
||||
await connection.ExecuteAsync(sql, new
|
||||
try
|
||||
{
|
||||
playerSteamid = playerSteamId,
|
||||
adminSteamid = issuer.SteamId == null ? "Console" : issuer.SteamId,
|
||||
adminName = issuer.Name == null ? "Console" : issuer.Name,
|
||||
banReason = reason,
|
||||
duration = time,
|
||||
ends = futureTime,
|
||||
created = now,
|
||||
type = muteType,
|
||||
serverid = CS2_SimpleAdmin.ServerId
|
||||
});
|
||||
await using MySqlConnection connection = await _database.GetConnectionAsync();
|
||||
var sql = "INSERT INTO `sa_mutes` (`player_steamid`, `admin_steamid`, `admin_name`, `reason`, `duration`, `ends`, `created`, `type`, `server_id`) " +
|
||||
"VALUES (@playerSteamid, @adminSteamid, @adminName, @muteReason, @duration, @ends, @created, @type, @serverid)";
|
||||
|
||||
await connection.ExecuteAsync(sql, new
|
||||
{
|
||||
playerSteamid = playerSteamId,
|
||||
adminSteamid = issuer.SteamId ?? "Console",
|
||||
adminName = issuer.Name ?? "Console",
|
||||
muteReason = reason,
|
||||
duration = time,
|
||||
ends = futureTime,
|
||||
created = now,
|
||||
type = muteType,
|
||||
serverid = CS2_SimpleAdmin.ServerId
|
||||
});
|
||||
}
|
||||
catch { };
|
||||
}
|
||||
|
||||
public async Task<List<dynamic>> IsPlayerMuted(string steamId)
|
||||
{
|
||||
if (string.IsNullOrEmpty(steamId))
|
||||
{
|
||||
return new List<dynamic>();
|
||||
return [];
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
@@ -102,49 +105,84 @@ internal class MuteManager
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return new List<dynamic>();
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<int> GetPlayerMutes(string steamId)
|
||||
{
|
||||
await using MySqlConnection connection = await _database.GetConnectionAsync();
|
||||
try
|
||||
{
|
||||
await using MySqlConnection connection = await _database.GetConnectionAsync();
|
||||
|
||||
int muteCount;
|
||||
string sql = "SELECT COUNT(*) FROM sa_mutes WHERE player_steamid = @PlayerSteamID";
|
||||
int muteCount;
|
||||
string sql = "SELECT COUNT(*) FROM sa_mutes WHERE player_steamid = @PlayerSteamID";
|
||||
|
||||
muteCount = await connection.ExecuteScalarAsync<int>(sql, new { PlayerSteamID = steamId });
|
||||
|
||||
return muteCount;
|
||||
muteCount = await connection.ExecuteScalarAsync<int>(sql, new { PlayerSteamID = steamId });
|
||||
return muteCount;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UnmutePlayer(string playerPattern, int type = 0)
|
||||
public async Task UnmutePlayer(string playerPattern, string adminSteamId, string reason, int type = 0)
|
||||
{
|
||||
if (playerPattern == null || playerPattern.Length <= 1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await using MySqlConnection connection = await _database.GetConnectionAsync();
|
||||
|
||||
if (type == 2)
|
||||
try
|
||||
{
|
||||
string _unbanSql = "UPDATE sa_mutes SET status = 'UNMUTED' WHERE (player_steamid = @pattern OR player_name = @pattern) AND status = 'ACTIVE'";
|
||||
await connection.ExecuteAsync(_unbanSql, new { pattern = playerPattern });
|
||||
await using MySqlConnection connection = await _database.GetConnectionAsync();
|
||||
|
||||
return;
|
||||
string muteType = "GAG";
|
||||
if (type == 1)
|
||||
{
|
||||
muteType = "MUTE";
|
||||
}
|
||||
else if (type == 2)
|
||||
muteType = "SILENCE";
|
||||
|
||||
string sqlRetrieveMutes = "SELECT id FROM sa_mutes WHERE (player_steamid = @pattern OR player_name = @pattern) AND type = @muteType AND status = 'ACTIVE'";
|
||||
var mutes = await connection.QueryAsync(sqlRetrieveMutes, new { pattern = playerPattern, muteType });
|
||||
|
||||
if (!mutes.Any())
|
||||
return;
|
||||
|
||||
string sqlAdmin = "SELECT id FROM sa_admins WHERE player_steamid = @adminSteamId";
|
||||
string sqlInsertUnmute = "INSERT INTO sa_unmutes (mute_id, admin_id, reason) VALUES (@muteId, @adminId, @reason); SELECT LAST_INSERT_ID();";
|
||||
|
||||
int? sqlAdminId = await connection.ExecuteScalarAsync<int?>(sqlAdmin, new { adminSteamId });
|
||||
int adminId = sqlAdminId ?? 0;
|
||||
|
||||
foreach (var mute in mutes)
|
||||
{
|
||||
int muteId = mute.id;
|
||||
int? unmuteId;
|
||||
|
||||
// Insert into sa_unmutes
|
||||
if (reason != null)
|
||||
{
|
||||
unmuteId = await connection.ExecuteScalarAsync<int>(sqlInsertUnmute, new { muteId, adminId, reason });
|
||||
}
|
||||
else
|
||||
{
|
||||
sqlInsertUnmute = "INSERT INTO sa_unmutes (muteId, admin_id) VALUES (@muteId, @adminId); SELECT LAST_INSERT_ID();";
|
||||
unmuteId = await connection.ExecuteScalarAsync<int>(sqlInsertUnmute, new { muteId, adminId });
|
||||
}
|
||||
|
||||
// Update sa_mutes to set unmute_id
|
||||
string sqlUpdateMute = "UPDATE sa_mutes SET status = 'UNMUTED', unmute_id = @unmuteId WHERE id = @muteId";
|
||||
await connection.ExecuteAsync(sqlUpdateMute, new { unmuteId, muteId });
|
||||
}
|
||||
}
|
||||
|
||||
string muteType = "GAG";
|
||||
if (type == 1)
|
||||
catch (Exception ex)
|
||||
{
|
||||
muteType = "MUTE";
|
||||
Console.WriteLine(ex);
|
||||
}
|
||||
else if (type == 2)
|
||||
muteType = "SILENCE";
|
||||
|
||||
string sqlUnban = "UPDATE sa_mutes SET status = 'UNMUTED' WHERE (player_steamid = @pattern OR player_name = @pattern) AND type = @muteType AND status = 'ACTIVE'";
|
||||
await connection.ExecuteAsync(sqlUnban, new { pattern = playerPattern, muteType });
|
||||
}
|
||||
|
||||
public async Task ExpireOldMutes()
|
||||
@@ -158,8 +196,7 @@ internal class MuteManager
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
if (CS2_SimpleAdmin._logger != null)
|
||||
CS2_SimpleAdmin._logger.LogCritical("Unable to remove expired mutes");
|
||||
CS2_SimpleAdmin._logger?.LogCritical("Unable to remove expired mutes");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user