Changed to async, instantly mute in CS2-Tags plugin

- Changed to async
- instantly mute in CS2-Tags plugin
This commit is contained in:
daffyyyy
2023-12-03 23:37:44 +01:00
parent 696b8c3877
commit 13e170a9c4
4 changed files with 200 additions and 121 deletions

View File

@@ -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(),
@@ -91,56 +90,76 @@ namespace CS2_SimpleAdmin
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 });
}
_dbConnection.Close();
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];
bool isBanned = false;
if (ipAddress != null)
{
isBanned = await IsPlayerBanned(steamId, ipAddress);
}
else
{
isBanned = await IsPlayerBanned(steamId);
}
if (isBanned)
{
Helper.KickPlayer(player.UserId, "Banned");
}
}
}

View File

@@ -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();
@@ -168,11 +169,15 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig<CS2_SimpleAdmin
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));
@@ -212,7 +217,7 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig<CS2_SimpleAdmin
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)
@@ -231,6 +236,9 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig<CS2_SimpleAdmin
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);
}
@@ -261,7 +269,11 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig<CS2_SimpleAdmin
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()}");
}
}
}
@@ -273,7 +285,13 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig<CS2_SimpleAdmin
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();
}
}
}
@@ -284,16 +302,16 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig<CS2_SimpleAdmin
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}.");
@@ -321,7 +339,7 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig<CS2_SimpleAdmin
if (command.ArgCount >= 3)
reason = command.GetArg(3);
_banManager.BanPlayer(player, caller, reason, time);
_ = _banManager.BanPlayer(player, caller, reason, time);
if (time == 0)
{
@@ -364,7 +382,7 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig<CS2_SimpleAdmin
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)
@@ -418,7 +436,7 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig<CS2_SimpleAdmin
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)
@@ -459,7 +477,7 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig<CS2_SimpleAdmin
string pattern = command.GetArg(1);
BanManager _banManager = new(dbConnectionString);
_banManager.UnbanPlayer(pattern);
_ = _banManager.UnbanPlayer(pattern);
command.ReplyToCommand($"Unbanned player with pattern {pattern}.");
}

View File

@@ -47,42 +47,8 @@ namespace CS2_SimpleAdmin
BanManager _banManager = new(dbConnectionString);
MuteManager _muteManager = new(dbConnectionString);
bool isBanned = false;
if (player.IpAddress != null)
{
isBanned = _banManager.IsPlayerBanned(player.AuthorizedSteamID.SteamId64.ToString(), player.IpAddress.Split(":")[0]);
}
else
{
isBanned = _banManager.IsPlayerBanned(player.AuthorizedSteamID.SteamId64.ToString());
}
List<dynamic> activeMutes = _muteManager.IsPlayerMuted(player.AuthorizedSteamID.SteamId64.ToString());
if (activeMutes.Count > 0)
{
// Player is muted, handle mute
foreach (var mute in activeMutes)
{
string muteType = mute.type;
if (muteType == "GAG")
{
if (!gaggedPlayers.Contains((int)player.Index))
gaggedPlayers.Add((int)player.Index);
}
else
{
continue;
}
}
}
// Player is banned, kick him
if (isBanned)
{
Helper.KickPlayer(player.UserId, "Banned");
}
_ = _banManager.CheckBan(player);
_ = _muteManager.CheckMute(player);
}
private void OnClientDisconnect(int playerSlot)
@@ -91,7 +57,11 @@ namespace CS2_SimpleAdmin
if (player == null || !player.IsValid || player.IsBot || player.IsHLTV) return;
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()}");
}
private void OnMapStart(string mapName)
@@ -99,10 +69,16 @@ namespace CS2_SimpleAdmin
AddTimer(120.0f, () =>
{
BanManager _banManager = new(dbConnectionString);
_banManager.ExpireOldBans();
_ = _banManager.ExpireOldBans();
MuteManager _muteManager = new(dbConnectionString);
_muteManager.ExpireOldMutes();
_ = _muteManager.ExpireOldMutes();
}, CounterStrikeSharp.API.Modules.Timers.TimerFlags.REPEAT | CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE);
string? path = Path.GetDirectoryName(ModuleDirectory);
if (Directory.Exists(path + "/CS2-Tags"))
{
TagsDetected = true;
}
}
}
}

View File

@@ -1,39 +1,37 @@
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Entities;
using Dapper;
using MySqlConnector;
using System.Data;
using System.Xml.Linq;
namespace CS2_SimpleAdmin
{
public class MuteManager
{
private readonly IDbConnection _dbConnection;
private readonly MySqlConnection _dbConnection;
public MuteManager(string connectionString)
{
_dbConnection = new MySqlConnection(connectionString);
}
public void MutePlayer(CCSPlayerController? player, CCSPlayerController? issuer, string reason, int time = 0, int type = 0)
public async Task MutePlayer(CCSPlayerController? player, CCSPlayerController? issuer, string reason, int time = 0, int type = 0)
{
_dbConnection.Open();
if (player == null || !player.IsValid || player.AuthorizedSteamID == null) return;
await using var connection = _dbConnection;
await connection.OpenAsync();
DateTime now = DateTime.Now;
DateTime futureTime = now.AddMinutes(time);
string muteType = "GAG";
if (type == 1)
muteType = "MUTE";
var sql = "INSERT INTO `sa_mutes` (`player_steamid`, `player_name`, `admin_steamid`, `admin_name`, `reason`, `duration`, `ends`, `created`, `type`) " +
"VALUES (@playerSteamid, @playerName, @adminSteamid, @adminName, @banReason, @duration, @ends, @created, @type)";
_dbConnection.Execute(sql, new
await connection.ExecuteAsync(sql, new
{
playerSteamid = player.AuthorizedSteamID.SteamId64.ToString(),
playerName = player.PlayerName,
@@ -46,27 +44,30 @@ namespace CS2_SimpleAdmin
type = muteType,
});
_dbConnection.Close();
if (connection.State != ConnectionState.Closed)
{
connection.Close();
}
}
public void AddMuteBySteamid(string playerSteamId, CCSPlayerController? issuer, string reason, int time = 0, int type = 0)
public async Task AddMuteBySteamid(string playerSteamId, CCSPlayerController? issuer, string reason, int time = 0, int type = 0)
{
if (string.IsNullOrEmpty(playerSteamId)) return;
_dbConnection.Open();
await using var connection = _dbConnection;
await connection.OpenAsync();
DateTime now = DateTime.Now;
DateTime futureTime = now.AddMinutes(time);
string muteType = "GAG";
if (type == 1)
muteType = "MUTE";
var sql = "INSERT INTO `sa_mutes` (`player_steamid`, `admin_steamid`, `admin_name`, `reason`, `duration`, `ends`, `created`, `type`) " +
"VALUES (@playerSteamid, @adminSteamid, @adminName, @banReason, @duration, @ends, @created, @type)";
_dbConnection.Execute(sql, new
await connection.ExecuteAsync(sql, new
{
playerSteamid = playerSteamId,
adminSteamid = issuer == null ? "Console" : issuer?.AuthorizedSteamID?.SteamId64.ToString(),
@@ -78,60 +79,125 @@ namespace CS2_SimpleAdmin
type = muteType
});
_dbConnection.Close();
if (connection.State != ConnectionState.Closed)
{
connection.Close();
}
}
public List<dynamic> IsPlayerMuted(string steamId)
public async Task<List<dynamic>> IsPlayerMuted(string steamId)
{
_dbConnection.Open();
await using var connection = _dbConnection;
await connection.OpenAsync();
DateTime now = DateTime.Now;
string sql = "SELECT * FROM sa_mutes WHERE player_steamid = @PlayerSteamID AND status = 'ACTIVE' AND (duration = 0 OR ends > @CurrentTime)";
List<dynamic> activeMutes = _dbConnection.Query<dynamic>(sql, new { PlayerSteamID = steamId, CurrentTime = now }).ToList();
var activeMutes = (await connection.QueryAsync(sql, new { PlayerSteamID = steamId, CurrentTime = now })).ToList();
_dbConnection.Close();
if (connection.State != ConnectionState.Closed)
{
connection.Close();
}
return activeMutes;
}
public void UnmutePlayer(string playerPattern, int type = 0)
public async Task UnmutePlayer(string playerPattern, int type = 0)
{
if (playerPattern == null || playerPattern.Length <= 1)
{
return;
}
await using var connection = _dbConnection;
await connection.OpenAsync();
if (type == 2)
{
string _unbanSql = "UPDATE sa_mutes SET status = 'UNMUTED' WHERE (player_steamid = @pattern OR player_name = @pattern) AND status = 'ACTIVE'";
_dbConnection.Execute(_unbanSql, new { pattern = playerPattern });
_dbConnection.Close();
await connection.ExecuteAsync(_unbanSql, new { pattern = playerPattern });
if (connection.State != ConnectionState.Closed)
{
connection.Close();
}
return;
}
string muteType = "GAG";
if (type == 1)
{
muteType = "MUTE";
_dbConnection.Open();
}
string sqlUnban = "UPDATE sa_mutes SET status = 'UNMUTED' WHERE (player_steamid = @pattern OR player_name = @pattern) AND type = @muteType AND status = 'ACTIVE'";
_dbConnection.Execute(sqlUnban, new { pattern = playerPattern, muteType });
_dbConnection.Close();
await connection.ExecuteAsync(sqlUnban, new { pattern = playerPattern, muteType });
if (connection.State != ConnectionState.Closed)
{
connection.Close();
}
}
public void ExpireOldMutes()
public async Task ExpireOldMutes()
{
_dbConnection.Open();
string sql = "UPDATE sa_mutes 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 using var connection = _dbConnection;
await connection.OpenAsync();
_dbConnection.Close();
string sql = "UPDATE sa_mutes SET status = 'EXPIRED' WHERE status = 'ACTIVE' AND `duration` > 0 AND ends <= @CurrentTime";
await connection.ExecuteAsync(sql, new { CurrentTime = DateTime.Now });
if (connection.State != ConnectionState.Closed)
{
connection.Close();
}
}
public async Task CheckMute(CCSPlayerController? player)
{
if (player == null || !player.IsValid || player.AuthorizedSteamID == null) return;
string steamId = player.AuthorizedSteamID.SteamId64.ToString();
List<dynamic> activeMutes = await IsPlayerMuted(steamId);
if (activeMutes.Count > 0)
{
foreach (var mute in activeMutes)
{
string muteType = mute.type;
TimeSpan duration = mute.ends - mute.created;
int durationInSeconds = (int)duration.TotalSeconds;
if (muteType == "GAG")
{
if (!CS2_SimpleAdmin.gaggedPlayers.Contains((int)player.Index))
CS2_SimpleAdmin.gaggedPlayers.Add((int)player.Index);
if (CS2_SimpleAdmin.TagsDetected)
NativeAPI.IssueServerCommand($"css_tag_mute {player!.Index.ToString()}");
CCSPlayerController currentPlayer = player;
if (mute.duration == 0 || durationInSeconds >= 1800) continue;
await Task.Delay(TimeSpan.FromSeconds(durationInSeconds));
if (currentPlayer != null && currentPlayer.IsValid)
{
NativeAPI.IssueServerCommand($"css_tag_unmute {currentPlayer.Index.ToString()}");
await UnmutePlayer(currentPlayer.AuthorizedSteamID.SteamId64.ToString(), 0);
}
}
else
{
// Mic mute
}
}
}
}
}
}