Skip to content

Commit

Permalink
Changed to async, instantly mute in CS2-Tags plugin
Browse files Browse the repository at this point in the history
- Changed to async
- instantly mute in CS2-Tags plugin
  • Loading branch information
daffyyyy committed Dec 3, 2023
1 parent 696b8c3 commit 13e170a
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 120 deletions.
93 changes: 56 additions & 37 deletions BanManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,79 +9,78 @@ namespace CS2_SimpleAdmin
{
internal class BanManager
{
private readonly IDbConnection _dbConnection;
private readonly MySqlConnection _dbConnection;

public BanManager(string connectionString)
{
_dbConnection = new MySqlConnection(connectionString);
}

public void BanPlayer(CCSPlayerController? player, CCSPlayerController? issuer, string reason, int time = 0)
public async Task BanPlayer(CCSPlayerController? player, CCSPlayerController? issuer, string reason, int time = 0)
{
_dbConnection.Open();

if (player == null || !player.IsValid || player.AuthorizedSteamID == null) return;

DateTime now = DateTime.Now;
DateTime futureTime = now.AddMinutes(time);

await using var connection = _dbConnection;
await connection.OpenAsync();

var sql = "INSERT INTO `sa_bans` (`player_steamid`, `player_name`, `player_ip`, `admin_steamid`, `admin_name`, `reason`, `duration`, `ends`, `created`) " +
"VALUES (@playerSteamid, @playerName, @playerIp, @adminSteamid, @adminName, @banReason, @duration, @ends, @created)";
_dbConnection.Execute(sql, new

await connection.ExecuteAsync(sql, new
{
playerSteamid = player.AuthorizedSteamID.SteamId64.ToString(),
playerName = player.PlayerName,
playerIp = player.IpAddress!.Split(":")[0],
adminSteamid = issuer == null ? "Console" : issuer?.AuthorizedSteamID?.SteamId64.ToString(),
adminSteamid = issuer == null ? "Console" : issuer.AuthorizedSteamID?.SteamId64.ToString(),
adminName = issuer == null ? "Console" : issuer.PlayerName,
banReason = reason,
duration = time,
ends = futureTime,
created = now
});

_dbConnection.Close();
}

public void AddBanBySteamid(string playerSteamId, CCSPlayerController? issuer, string reason, int time = 0)
public async Task AddBanBySteamid(string playerSteamId, CCSPlayerController? issuer, string reason, int time = 0)
{
if (string.IsNullOrEmpty(playerSteamId)) return;

_dbConnection.Open();

DateTime now = DateTime.Now;
DateTime futureTime = now.AddMinutes(time);

await using var connection = _dbConnection;
await connection.OpenAsync();

var sql = "INSERT INTO `sa_bans` (`player_steamid`, `admin_steamid`, `admin_name`, `reason`, `duration`, `ends`, `created`) " +
"VALUES (@playerSteamid, @adminSteamid, @adminName, @banReason, @duration, @ends, @created)";

_dbConnection.Execute(sql, new
await connection.ExecuteAsync(sql, new
{
playerSteamid = playerSteamId,
adminSteamid = issuer == null ? "Console" : issuer?.AuthorizedSteamID?.SteamId64.ToString(),
adminSteamid = issuer == null ? "Console" : issuer.AuthorizedSteamID?.SteamId64.ToString(),
adminName = issuer == null ? "Console" : issuer.PlayerName,
banReason = reason,
duration = time,
ends = futureTime,
created = now
});

_dbConnection.Close();
}

public void AddBanByIp(string playerIp, CCSPlayerController? issuer, string reason, int time = 0)
public async Task AddBanByIp(string playerIp, CCSPlayerController? issuer, string reason, int time = 0)
{
if (string.IsNullOrEmpty(playerIp)) return;

_dbConnection.Open();

DateTime now = DateTime.Now;
DateTime futureTime = now.AddMinutes(time);

await using var connection = _dbConnection;
await connection.OpenAsync();

var sql = "INSERT INTO `sa_bans` (`player_ip`, `admin_steamid`, `admin_name`, `reason`, `duration`, `ends`, `created`) " +
"VALUES (@playerIp, @adminSteamid, @adminName, @banReason, @duration, @ends, @created)";

_dbConnection.Execute(sql, new
await connection.ExecuteAsync(sql, new
{
playerIp,
adminSteamid = issuer == null ? "Console" : issuer?.AuthorizedSteamID?.SteamId64.ToString(),
Expand All @@ -91,56 +90,76 @@ public void AddBanByIp(string playerIp, CCSPlayerController? issuer, string reas
ends = futureTime,
created = now
});

_dbConnection.Close();
}

public bool IsPlayerBanned(string steamId, string? ipAddress = null)
public async Task<bool> IsPlayerBanned(string steamId, string? ipAddress = null)
{
_dbConnection.Open();

DateTime now = DateTime.Now;

string sql = "SELECT COUNT(*) FROM sa_bans WHERE (player_steamid = @PlayerSteamID OR player_ip = @PlayerIP) AND status = 'ACTIVE' AND (duration = 0 OR ends > @CurrentTime)";

int banCount;

await using var connection = _dbConnection;
await connection.OpenAsync();

if (!string.IsNullOrEmpty(ipAddress))
{
banCount = _dbConnection.ExecuteScalar<int>(sql, new { PlayerSteamID = steamId, PlayerIP = ipAddress, CurrentTime = now });
banCount = await connection.ExecuteScalarAsync<int>(sql, new { PlayerSteamID = steamId, PlayerIP = ipAddress, CurrentTime = now });
}
else
{
banCount = _dbConnection.ExecuteScalar<int>(sql, new { PlayerSteamID = steamId, PlayerIP = DBNull.Value, CurrentTime = now });
banCount = await connection.ExecuteScalarAsync<int>(sql, new { PlayerSteamID = steamId, PlayerIP = DBNull.Value, CurrentTime = now });
}

return banCount > 0;
}

public void UnbanPlayer(string playerPattern)
public async Task UnbanPlayer(string playerPattern)
{
if (playerPattern == null || playerPattern.Length <= 1)
{
return;
}

_dbConnection.Open();
await using var connection = _dbConnection;
await connection.OpenAsync();

string sqlUnban = "UPDATE sa_bans SET status = 'UNBANNED' WHERE player_steamid = @pattern OR player_name = @pattern OR player_ip = @pattern AND status = 'ACTIVE'";
_dbConnection.Execute(sqlUnban, new { pattern = playerPattern });

_dbConnection.Close();
await connection.ExecuteAsync(sqlUnban, new { pattern = playerPattern });
}

public void ExpireOldBans()
public async Task ExpireOldBans()
{
_dbConnection.Open();
await using var connection = _dbConnection;
await connection.OpenAsync();

string sql = "UPDATE sa_bans SET status = 'EXPIRED' WHERE status = 'ACTIVE' AND `duration` > 0 AND ends <= @CurrentTime";
_dbConnection.Execute(sql, new { CurrentTime = DateTime.Now });
//int affectedRows = _dbConnection.Execute(sql, new { CurrentTime = DateTime.Now });
await connection.ExecuteAsync(sql, new { CurrentTime = DateTime.Now });
}

public async Task CheckBan(CCSPlayerController? player)
{
if (player == null || !player.IsValid || player.AuthorizedSteamID == null) return;

string steamId = player.AuthorizedSteamID.SteamId64.ToString();
string? ipAddress = player.IpAddress?.Split(":")[0];

_dbConnection.Close();
bool isBanned = false;

if (ipAddress != null)
{
isBanned = await IsPlayerBanned(steamId, ipAddress);
}
else
{
isBanned = await IsPlayerBanned(steamId);
}

if (isBanned)
{
Helper.KickPlayer(player.UserId, "Banned");
}
}

}
Expand Down
44 changes: 31 additions & 13 deletions CS2-SimpleAdmin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ namespace CS2_SimpleAdmin;
[MinimumApiVersion(98)]
public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig<CS2_SimpleAdminConfig>
{
public List<int> gaggedPlayers = new List<int>();
public static List<int> gaggedPlayers = new List<int>();
public static bool TagsDetected = false;

internal string dbConnectionString = string.Empty;
public override string ModuleName => "CS2-SimpleAdmin";
public override string ModuleDescription => "";
public override string ModuleAuthor => "daffyy";
public override string ModuleVersion => "1.0.3";
public override string ModuleVersion => "1.0.4";

public CS2_SimpleAdminConfig Config { get; set; } = new();

Expand Down Expand Up @@ -168,11 +169,15 @@ public void OnGagCommand(CCSPlayerController? caller, CommandInfo command)
if (command.ArgCount >= 3)
reason = command.GetArg(3);

_muteManager.MutePlayer(player, caller, reason, time, 0);
_ = _muteManager.MutePlayer(player, caller, reason, time, 0);

if (TagsDetected)
NativeAPI.IssueServerCommand($"css_tag_mute {player!.Index.ToString()}");

if (!gaggedPlayers.Contains((int)player!.Index))
gaggedPlayers.Add((int)player.Index);


if (time == 0)
{
player!.PrintToCenter($"{Config.Messages.PlayerGagMessagePerm}".Replace("{REASON}", reason).Replace("{ADMIN}", caller?.PlayerName == null ? "Console" : caller.PlayerName));
Expand Down Expand Up @@ -212,7 +217,7 @@ public void OnAddGagCommand(CCSPlayerController? caller, CommandInfo command)
if (command.ArgCount >= 3)
reason = command.GetArg(3);

_muteManager.AddMuteBySteamid(steamid, caller, reason, time, 0);
_ = _muteManager.AddMuteBySteamid(steamid, caller, reason, time, 0);

List<CCSPlayerController> matches = Helper.GetPlayerFromSteamid64(steamid);
if (matches.Count == 1)
Expand All @@ -231,6 +236,9 @@ public void OnAddGagCommand(CCSPlayerController? caller, CommandInfo command)
Server.PrintToChatAll(Helper.ReplaceTags($" {Config.Prefix} {Config.Messages.AdminGagMessageTime}".Replace("{REASON}", reason).Replace("{TIME}", time.ToString()).Replace("{ADMIN}", caller?.PlayerName == null ? "Console" : caller.PlayerName).Replace("{PLAYER}", player.PlayerName)));
}

if (TagsDetected)
NativeAPI.IssueServerCommand($"css_tag_mute {player!.Index.ToString()}");

if (!gaggedPlayers.Contains((int)player.Index))
gaggedPlayers.Add((int)player.Index);
}
Expand Down Expand Up @@ -261,7 +269,11 @@ public void OnUnmuteCommand(CCSPlayerController? caller, CommandInfo command)
CCSPlayerController? player = matches.FirstOrDefault();
if (player != null)
{
gaggedPlayers.Remove((int)player.Index);
if (gaggedPlayers.Contains((int)player.Index))
gaggedPlayers.Remove((int)player.Index);

if (TagsDetected)
NativeAPI.IssueServerCommand($"css_tag_unmute {player!.Index.ToString()}");
}
}
}
Expand All @@ -273,7 +285,13 @@ public void OnUnmuteCommand(CCSPlayerController? caller, CommandInfo command)
CCSPlayerController? player = matches.FirstOrDefault();
if (player != null)
{
gaggedPlayers.Remove((int)player.Index);
if (gaggedPlayers.Contains((int)player.Index))
gaggedPlayers.Remove((int)player.Index);

if (TagsDetected)
NativeAPI.IssueServerCommand($"css_tag_unmute {player!.Index.ToString()}");

pattern = player.AuthorizedSteamID!.SteamId64.ToString();
}
}
}
Expand All @@ -284,16 +302,16 @@ public void OnUnmuteCommand(CCSPlayerController? caller, CommandInfo command)

if (action == "gag")
{
_muteManager.UnmutePlayer(pattern, 0); // Unmute by type 0 (gag)
_ = _muteManager.UnmutePlayer(pattern, 0); // Unmute by type 0 (gag)
}
else if (action == "mute")
{
_muteManager.UnmutePlayer(pattern, 1); // Unmute by type 1 (mute)
_ = _muteManager.UnmutePlayer(pattern, 1); // Unmute by type 1 (mute)
}
}
else
{
_muteManager.UnmutePlayer(pattern, 2); // Default unmute (all types)
_ = _muteManager.UnmutePlayer(pattern, 2); // Default unmute (all types)
}

command.ReplyToCommand($"Unmuted player with pattern {pattern}.");
Expand Down Expand Up @@ -321,7 +339,7 @@ public void OnBanCommand(CCSPlayerController? caller, CommandInfo command)
if (command.ArgCount >= 3)
reason = command.GetArg(3);

_banManager.BanPlayer(player, caller, reason, time);
_ = _banManager.BanPlayer(player, caller, reason, time);

if (time == 0)
{
Expand Down Expand Up @@ -364,7 +382,7 @@ public void OnAddBanCommand(CCSPlayerController? caller, CommandInfo command)
if (command.ArgCount >= 3)
reason = command.GetArg(3);

_banManager.AddBanBySteamid(steamid, caller, reason, time);
_ = _banManager.AddBanBySteamid(steamid, caller, reason, time);

List<CCSPlayerController> matches = Helper.GetPlayerFromSteamid64(steamid);
if (matches.Count == 1)
Expand Down Expand Up @@ -418,7 +436,7 @@ public void OnBanIp(CCSPlayerController? caller, CommandInfo command)
if (command.ArgCount >= 3)
reason = command.GetArg(3);

_banManager.AddBanByIp(ipAddress, caller, reason, time);
_ = _banManager.AddBanByIp(ipAddress, caller, reason, time);

List<CCSPlayerController> matches = Helper.GetPlayerFromIp(ipAddress);
if (matches.Count == 1)
Expand Down Expand Up @@ -459,7 +477,7 @@ public void OnUnbanCommand(CCSPlayerController? caller, CommandInfo command)
string pattern = command.GetArg(1);
BanManager _banManager = new(dbConnectionString);

_banManager.UnbanPlayer(pattern);
_ = _banManager.UnbanPlayer(pattern);

command.ReplyToCommand($"Unbanned player with pattern {pattern}.");
}
Expand Down
Loading

0 comments on commit 13e170a

Please sign in to comment.