From 9820d7409510bea7873b7fe72b535f555956bac6 Mon Sep 17 00:00:00 2001 From: Dawid Bepierszcz <41084667+daffyyyy@users.noreply.github.com> Date: Tue, 8 Oct 2024 00:54:20 +0200 Subject: [PATCH] 1.6.3b ```diff + Small code cleanup + FIXED ban check on connect ``` --- CS2-SimpleAdmin/CS2-SimpleAdmin.cs | 21 +++--- CS2-SimpleAdmin/Commands/basebans.cs | 2 - CS2-SimpleAdmin/Commands/basecommands.cs | 14 ++-- CS2-SimpleAdmin/Commands/basecomms.cs | 48 ++++++------- CS2-SimpleAdmin/Events.cs | 16 ++--- CS2-SimpleAdmin/Managers/BanManager.cs | 85 +++++++++++++---------- CS2-SimpleAdmin/Managers/MuteManager.cs | 14 ++-- CS2-SimpleAdmin/Managers/PlayerManager.cs | 12 ++-- 8 files changed, 109 insertions(+), 103 deletions(-) diff --git a/CS2-SimpleAdmin/CS2-SimpleAdmin.cs b/CS2-SimpleAdmin/CS2-SimpleAdmin.cs index e26be56..e67a1b4 100644 --- a/CS2-SimpleAdmin/CS2-SimpleAdmin.cs +++ b/CS2-SimpleAdmin/CS2-SimpleAdmin.cs @@ -3,6 +3,7 @@ using CounterStrikeSharp.API.Core.Capabilities; using CounterStrikeSharp.API.Modules.Commands; using CounterStrikeSharp.API.Modules.Commands.Targeting; +using CounterStrikeSharp.API.Modules.Entities; using CounterStrikeSharp.API.Modules.Memory.DynamicFunctions; using CS2_SimpleAdmin.Managers; using CS2_SimpleAdminApi; @@ -19,7 +20,7 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig "CS2-SimpleAdmin" + (Helper.IsDebugBuild ? " (DEBUG)" : " (RELEASE)"); public override string ModuleDescription => "Simple admin plugin for Counter-Strike 2 :)"; public override string ModuleAuthor => "daffyy & Dliix66"; - public override string ModuleVersion => "1.6.3a"; + public override string ModuleVersion => "1.6.3b"; public override void Load(bool hotReload) { @@ -48,6 +49,8 @@ public override void Load(bool hotReload) SimpleAdminApi = new Api.CS2_SimpleAdminApi(); Capabilities.RegisterPluginCapability(ICS2_SimpleAdminApi.PluginCapability, () => SimpleAdminApi); + + new PlayerManager().CheckPlayersTimer(); } public override void OnAllPluginsLoaded(bool hotReload) @@ -63,13 +66,13 @@ public override void OnAllPluginsLoaded(bool hotReload) public void OnConfigParsed(CS2_SimpleAdminConfig config) { + Instance = this; + _logger = Logger; + if (config.DatabaseHost.Length < 1 || config.DatabaseName.Length < 1 || config.DatabaseUser.Length < 1) { throw new Exception("[CS2-SimpleAdmin] You need to setup Database credentials in config!"); } - - Instance = this; - _logger = Logger; MySqlConnectionStringBuilder builder = new() { @@ -95,11 +98,6 @@ public void OnConfigParsed(CS2_SimpleAdminConfig config) Task.Run(() => Database.DatabaseMigration()); - PermissionManager = new PermissionManager(Database); - BanManager = new BanManager(Database); - MuteManager = new MuteManager(Database); - WarnManager = new WarnManager(Database); - Config = config; Helper.UpdateConfig(config); @@ -116,6 +114,11 @@ public void OnConfigParsed(CS2_SimpleAdminConfig config) PluginInfo.ShowAd(ModuleVersion); if (Config.EnableUpdateCheck) Task.Run(async () => await PluginInfo.CheckVersion(ModuleVersion, _logger)); + + PermissionManager = new PermissionManager(Database); + BanManager = new BanManager(Database); + MuteManager = new MuteManager(Database); + WarnManager = new WarnManager(Database); } private static TargetResult? GetTarget(CommandInfo command) diff --git a/CS2-SimpleAdmin/Commands/basebans.cs b/CS2-SimpleAdmin/Commands/basebans.cs index 6861346..7c2f6cd 100644 --- a/CS2-SimpleAdmin/Commands/basebans.cs +++ b/CS2-SimpleAdmin/Commands/basebans.cs @@ -394,8 +394,6 @@ public void OnUnwarnCommand(CCSPlayerController? caller, CommandInfo command) { if (Database == null) return; - var callerSteamId = caller?.SteamID.ToString() ?? _localizer?["sa_console"] ?? "Console"; - if (command.GetArg(1).Length <= 1) { command.ReplyToCommand($"Too short pattern to search."); diff --git a/CS2-SimpleAdmin/Commands/basecommands.cs b/CS2-SimpleAdmin/Commands/basecommands.cs index 2797fdc..7447c3e 100644 --- a/CS2-SimpleAdmin/Commands/basecommands.cs +++ b/CS2-SimpleAdmin/Commands/basecommands.cs @@ -198,10 +198,9 @@ public void OnAddAdminCommand(CCSPlayerController? caller, CommandInfo command) public static void AddAdmin(CCSPlayerController? caller, string steamid, string name, string flags, int immunity, int time = 0, bool globalAdmin = false, CommandInfo? command = null) { if (Database == null) return; - PermissionManager adminManager = new(Database); - + var flagsList = flags.Split(',').Select(flag => flag.Trim()).ToList(); - _ = adminManager.AddAdminBySteamId(steamid, name, flagsList, immunity, time, globalAdmin); + _ = Instance.PermissionManager.AddAdminBySteamId(steamid, name, flagsList, immunity, time, globalAdmin); Helper.LogCommand(caller, $"css_addadmin {steamid} {name} {flags} {immunity} {time}"); @@ -289,10 +288,9 @@ public void OnAddGroup(CCSPlayerController? caller, CommandInfo command) private static void AddGroup(CCSPlayerController? caller, string name, string flags, int immunity, bool globalGroup, CommandInfo? command = null) { if (Database == null) return; - PermissionManager adminManager = new(Database); var flagsList = flags.Split(',').Select(flag => flag.Trim()).ToList(); - _ = adminManager.AddGroup(name, flagsList, immunity, globalGroup); + _ = Instance.PermissionManager.AddGroup(name, flagsList, immunity, globalGroup); Helper.LogCommand(caller, $"css_addgroup {name} {flags} {immunity}"); @@ -367,12 +365,10 @@ public void ReloadAdmins(CCSPlayerController? caller) AdminManager.RemovePlayerAdminData(steamId); } - PermissionManager adminManager = new(Database); - Task.Run(async () => { - await adminManager.CrateGroupsJsonFile(); - await adminManager.CreateAdminsJsonFile(); + await PermissionManager.CrateGroupsJsonFile(); + await PermissionManager.CreateAdminsJsonFile(); var adminsFile = await File.ReadAllTextAsync(Instance.ModuleDirectory + "/data/admins.json"); var groupsFile = await File.ReadAllTextAsync(Instance.ModuleDirectory + "/data/groups.json"); diff --git a/CS2-SimpleAdmin/Commands/basecomms.cs b/CS2-SimpleAdmin/Commands/basecomms.cs index 97011c7..4aa3f3d 100644 --- a/CS2-SimpleAdmin/Commands/basecomms.cs +++ b/CS2-SimpleAdmin/Commands/basecomms.cs @@ -42,18 +42,17 @@ public void OnGagCommand(CCSPlayerController? caller, CommandInfo command) return; } - Gag(caller, player, time, reason, callerName, MuteManager, command); + Gag(caller, player, time, reason, callerName, command); }); } - internal void Gag(CCSPlayerController? caller, CCSPlayerController player, int time, string reason, string? callerName = null, MuteManager? muteManager = null, CommandInfo? command = null, bool silent = false) + internal void Gag(CCSPlayerController? caller, CCSPlayerController player, int time, string reason, string? callerName = null, CommandInfo? command = null, bool silent = false) { if (Database == null || !player.IsValid || !player.UserId.HasValue) return; if (!caller.CanTarget(player)) return; // Set default caller name if not provided callerName ??= caller == null ? _localizer?["sa_console"] ?? "Console" : caller.PlayerName; - muteManager ??= new MuteManager(Database); // Get player and admin information var playerInfo = PlayersInfo[player.UserId.Value]; @@ -62,7 +61,7 @@ internal void Gag(CCSPlayerController? caller, CCSPlayerController player, int t // Asynchronously handle gag logic Task.Run(async () => { - await muteManager.MutePlayer(playerInfo, adminInfo, reason, time); + await MuteManager.MutePlayer(playerInfo, adminInfo, reason, time); }); // Add penalty to the player's penalty manager @@ -141,7 +140,7 @@ public void OnAddGagCommand(CCSPlayerController? caller, CommandInfo command) if (!caller.CanTarget(player)) return; // Perform the gag for an online player - Gag(caller, player, time, reason, callerName, MuteManager, silent: true); + Gag(caller, player, time, reason, callerName, silent: true); } else { @@ -176,7 +175,6 @@ public void OnUngagCommand(CCSPlayerController? caller, CommandInfo command) } Helper.LogCommand(caller, command); - var muteManager = new MuteManager(Database); // Check if pattern is a valid SteamID64 if (Helper.ValidateSteamId(pattern, out var steamId) && steamId != null) @@ -190,7 +188,7 @@ public void OnUngagCommand(CCSPlayerController? caller, CommandInfo command) Task.Run(async () => { - await muteManager.UnmutePlayer(player.SteamID.ToString(), callerSteamId, reason); + await MuteManager.UnmutePlayer(player.SteamID.ToString(), callerSteamId, reason); }); command.ReplyToCommand($"Ungaged player {player.PlayerName}."); @@ -211,7 +209,7 @@ public void OnUngagCommand(CCSPlayerController? caller, CommandInfo command) Task.Run(async () => { - await muteManager.UnmutePlayer(namePlayer.SteamID.ToString(), callerSteamId, reason); + await MuteManager.UnmutePlayer(namePlayer.SteamID.ToString(), callerSteamId, reason); }); command.ReplyToCommand($"Ungaged player {namePlayer.PlayerName}."); @@ -220,7 +218,7 @@ public void OnUngagCommand(CCSPlayerController? caller, CommandInfo command) { Task.Run(async () => { - await muteManager.UnmutePlayer(pattern, callerSteamId, reason); + await MuteManager.UnmutePlayer(pattern, callerSteamId, reason); }); command.ReplyToCommand($"Ungaged offline player with pattern {pattern}."); @@ -258,18 +256,17 @@ public void OnMuteCommand(CCSPlayerController? caller, CommandInfo command) return; } - Mute(caller, player, time, reason, callerName, MuteManager, command); + Mute(caller, player, time, reason, callerName, command); }); } - internal void Mute(CCSPlayerController? caller, CCSPlayerController player, int time, string reason, string? callerName = null, MuteManager? muteManager = null, CommandInfo? command = null, bool silent = false) + internal void Mute(CCSPlayerController? caller, CCSPlayerController player, int time, string reason, string? callerName = null, CommandInfo? command = null, bool silent = false) { if (Database == null || !player.IsValid || !player.UserId.HasValue) return; if (!caller.CanTarget(player)) return; // Set default caller name if not provided callerName ??= caller == null ? _localizer?["sa_console"] ?? "Console" : caller.PlayerName; - muteManager ??= new MuteManager(Database); // Get player and admin information var playerInfo = PlayersInfo[player.UserId.Value]; @@ -281,7 +278,7 @@ internal void Mute(CCSPlayerController? caller, CCSPlayerController player, int // Asynchronously handle mute logic Task.Run(async () => { - await muteManager.MutePlayer(playerInfo, adminInfo, reason, time, 1); + await MuteManager.MutePlayer(playerInfo, adminInfo, reason, time, 1); }); // Add penalty to the player's penalty manager @@ -360,7 +357,7 @@ public void OnAddMuteCommand(CCSPlayerController? caller, CommandInfo command) if (!caller.CanTarget(player)) return; // Perform the mute for an online player - Mute(caller, player, time, reason, callerName, MuteManager, silent: true); + Mute(caller, player, time, reason, callerName, silent: true); } else { @@ -395,7 +392,6 @@ public void OnUnmuteCommand(CCSPlayerController? caller, CommandInfo command) } Helper.LogCommand(caller, command); - var muteManager = new MuteManager(Database); // Check if pattern is a valid SteamID64 if (Helper.ValidateSteamId(pattern, out var steamId) && steamId != null) @@ -410,7 +406,7 @@ public void OnUnmuteCommand(CCSPlayerController? caller, CommandInfo command) Task.Run(async () => { - await muteManager.UnmutePlayer(player.SteamID.ToString(), callerSteamId, reason, 1); + await MuteManager.UnmutePlayer(player.SteamID.ToString(), callerSteamId, reason, 1); }); command.ReplyToCommand($"Unmuted player {player.PlayerName}."); @@ -432,7 +428,7 @@ public void OnUnmuteCommand(CCSPlayerController? caller, CommandInfo command) Task.Run(async () => { - await muteManager.UnmutePlayer(namePlayer.SteamID.ToString(), callerSteamId, reason, 1); + await MuteManager.UnmutePlayer(namePlayer.SteamID.ToString(), callerSteamId, reason, 1); }); command.ReplyToCommand($"Unmuted player {namePlayer.PlayerName}."); @@ -441,7 +437,7 @@ public void OnUnmuteCommand(CCSPlayerController? caller, CommandInfo command) { Task.Run(async () => { - await muteManager.UnmutePlayer(pattern, callerSteamId, reason, 1); + await MuteManager.UnmutePlayer(pattern, callerSteamId, reason, 1); }); command.ReplyToCommand($"Unmuted offline player with pattern {pattern}."); @@ -479,18 +475,17 @@ public void OnSilenceCommand(CCSPlayerController? caller, CommandInfo command) return; } - Silence(caller, player, time, reason, callerName, MuteManager, command); + Silence(caller, player, time, reason, callerName, command); }); } - internal void Silence(CCSPlayerController? caller, CCSPlayerController player, int time, string reason, string? callerName = null, MuteManager? muteManager = null, CommandInfo? command = null, bool silent = false) + internal void Silence(CCSPlayerController? caller, CCSPlayerController player, int time, string reason, string? callerName = null, CommandInfo? command = null, bool silent = false) { if (Database == null || !player.IsValid || !player.UserId.HasValue) return; if (!caller.CanTarget(player)) return; // Set default caller name if not provided callerName ??= caller == null ? _localizer?["sa_console"] ?? "Console" : caller.PlayerName; - muteManager ??= new MuteManager(Database); // Get player and admin information var playerInfo = PlayersInfo[player.UserId.Value]; @@ -499,7 +494,7 @@ internal void Silence(CCSPlayerController? caller, CCSPlayerController player, i // Asynchronously handle silence logic Task.Run(async () => { - await muteManager.MutePlayer(playerInfo, adminInfo, reason, time, 2); // Assuming 2 is the type for silence + await MuteManager.MutePlayer(playerInfo, adminInfo, reason, time, 2); // Assuming 2 is the type for silence }); // Add penalty to the player's penalty manager @@ -578,7 +573,7 @@ public void OnAddSilenceCommand(CCSPlayerController? caller, CommandInfo command if (!caller.CanTarget(player)) return; // Perform the silence for an online player - Silence(caller, player, time, reason, callerName, MuteManager, silent: true); + Silence(caller, player, time, reason, callerName, silent: true); } else { @@ -613,7 +608,6 @@ public void OnUnsilenceCommand(CCSPlayerController? caller, CommandInfo command) } Helper.LogCommand(caller, command); - var muteManager = new MuteManager(Database); // Check if pattern is a valid SteamID64 if (Helper.ValidateSteamId(pattern, out var steamId) && steamId != null) @@ -630,7 +624,7 @@ public void OnUnsilenceCommand(CCSPlayerController? caller, CommandInfo command) Task.Run(async () => { - await muteManager.UnmutePlayer(player.SteamID.ToString(), callerSteamId, reason, 2); // Unmute by type 2 (silence) + await MuteManager.UnmutePlayer(player.SteamID.ToString(), callerSteamId, reason, 2); // Unmute by type 2 (silence) }); command.ReplyToCommand($"Unsilenced player {player.PlayerName}."); @@ -654,7 +648,7 @@ public void OnUnsilenceCommand(CCSPlayerController? caller, CommandInfo command) Task.Run(async () => { - await muteManager.UnmutePlayer(namePlayer.SteamID.ToString(), callerSteamId, reason, 2); // Unmute by type 2 (silence) + await MuteManager.UnmutePlayer(namePlayer.SteamID.ToString(), callerSteamId, reason, 2); // Unmute by type 2 (silence) }); command.ReplyToCommand($"Unsilenced player {namePlayer.PlayerName}."); @@ -663,7 +657,7 @@ public void OnUnsilenceCommand(CCSPlayerController? caller, CommandInfo command) { Task.Run(async () => { - await muteManager.UnmutePlayer(pattern, callerSteamId, reason, 2); // Unmute by type 2 (silence) + await MuteManager.UnmutePlayer(pattern, callerSteamId, reason, 2); // Unmute by type 2 (silence) }); command.ReplyToCommand($"Unsilenced offline player with pattern {pattern}."); diff --git a/CS2-SimpleAdmin/Events.cs b/CS2-SimpleAdmin/Events.cs index a1aa247..e92329f 100644 --- a/CS2-SimpleAdmin/Events.cs +++ b/CS2-SimpleAdmin/Events.cs @@ -29,6 +29,7 @@ private void RegisterEvents() private HookResult OnCommandCallVote(CCSPlayerController? caller, CommandInfo info) { var voteType = info.GetArg(1).ToLower(); + if (voteType != "kick") return HookResult.Continue; @@ -96,8 +97,8 @@ public HookResult OnClientDisconnect(EventPlayerDisconnect @event, GameEventInfo out var expirationTime) || !(expirationTime <= Time.ActualDateTime())) return HookResult.Continue; - CounterStrikeSharp.API.Modules.Admin.AdminManager.ClearPlayerPermissions(authorizedSteamId); - CounterStrikeSharp.API.Modules.Admin.AdminManager.RemovePlayerAdminData(authorizedSteamId); + AdminManager.ClearPlayerPermissions(authorizedSteamId); + AdminManager.RemovePlayerAdminData(authorizedSteamId); return HookResult.Continue; } @@ -186,7 +187,7 @@ private HookResult OnCommandSayNew(CCSPlayerController? player, CommandInfo info if (AdminManager.PlayerHasPermissions(player, "@css/chat")) { sb.Append(_localizer!["sa_adminchat_template_admin", player.PlayerName, info.GetArg(1).Remove(0, 1)]); - foreach (var p in Utilities.GetPlayers().Where(p => p.IsValid && p is { IsBot: false, IsHLTV: false } && CounterStrikeSharp.API.Modules.Admin.AdminManager.PlayerHasPermissions(p, "@css/chat"))) + foreach (var p in Utilities.GetPlayers().Where(p => p.IsValid && p is { IsBot: false, IsHLTV: false } && AdminManager.PlayerHasPermissions(p, "@css/chat"))) { p.PrintToChat(sb.ToString()); } @@ -195,7 +196,7 @@ private HookResult OnCommandSayNew(CCSPlayerController? player, CommandInfo info { sb.Append(_localizer!["sa_adminchat_template_player", player.PlayerName, info.GetArg(1).Remove(0, 1)]); player.PrintToChat(sb.ToString()); - foreach (var p in Utilities.GetPlayers().Where(p => p is { IsValid: true, IsBot: false, IsHLTV: false } && CounterStrikeSharp.API.Modules.Admin.AdminManager.PlayerHasPermissions(p, "@css/chat"))) + foreach (var p in Utilities.GetPlayers().Where(p => p is { IsValid: true, IsBot: false, IsHLTV: false } && AdminManager.PlayerHasPermissions(p, "@css/chat"))) { p.PrintToChat(sb.ToString()); } @@ -241,10 +242,10 @@ public HookResult OnCommandTeamSay(CCSPlayerController? player, CommandInfo info StringBuilder sb = new(); - if (CounterStrikeSharp.API.Modules.Admin.AdminManager.PlayerHasPermissions(player, "@css/chat")) + if (AdminManager.PlayerHasPermissions(player, "@css/chat")) { sb.Append(_localizer!["sa_adminchat_template_admin", player.PlayerName, info.GetArg(1).Remove(0, 1)]); - foreach (var p in Utilities.GetPlayers().Where(p => p.IsValid && p is { IsBot: false, IsHLTV: false } && CounterStrikeSharp.API.Modules.Admin.AdminManager.PlayerHasPermissions(p, "@css/chat"))) + foreach (var p in Utilities.GetPlayers().Where(p => p.IsValid && p is { IsBot: false, IsHLTV: false } && AdminManager.PlayerHasPermissions(p, "@css/chat"))) { p.PrintToChat(sb.ToString()); } @@ -253,7 +254,7 @@ public HookResult OnCommandTeamSay(CCSPlayerController? player, CommandInfo info { sb.Append(_localizer!["sa_adminchat_template_player", player.PlayerName, info.GetArg(1).Remove(0, 1)]); player.PrintToChat(sb.ToString()); - foreach (var p in Utilities.GetPlayers().Where(p => p is { IsValid: true, IsBot: false, IsHLTV: false } && CounterStrikeSharp.API.Modules.Admin.AdminManager.PlayerHasPermissions(p, "@css/chat"))) + foreach (var p in Utilities.GetPlayers().Where(p => p is { IsValid: true, IsBot: false, IsHLTV: false } && AdminManager.PlayerHasPermissions(p, "@css/chat"))) { p.PrintToChat(sb.ToString()); } @@ -277,7 +278,6 @@ private void OnMapStart(string mapName) SilentPlayers.Clear(); PlayerPenaltyManager.RemoveAllPenalties(); - new PlayerManager().CheckPlayersTimer(); } [GameEventHandler] diff --git a/CS2-SimpleAdmin/Managers/BanManager.cs b/CS2-SimpleAdmin/Managers/BanManager.cs index a9854bd..6cd0013 100644 --- a/CS2-SimpleAdmin/Managers/BanManager.cs +++ b/CS2-SimpleAdmin/Managers/BanManager.cs @@ -111,7 +111,7 @@ public async Task IsPlayerBanned(PlayerInfo player) { return false; } - + #if DEBUG if (CS2_SimpleAdmin._logger != null) CS2_SimpleAdmin._logger.LogCritical($"IsPlayerBanned for {player.Name}"); @@ -124,40 +124,53 @@ public async Task IsPlayerBanned(PlayerInfo player) try { var sql = CS2_SimpleAdmin.Instance.Config.MultiServerMode ? """ - SELECT COALESCE(( - SELECT COUNT(*) - FROM sa_bans - WHERE (player_steamid = @PlayerSteamID OR player_ip = @PlayerIP) - AND status = 'ACTIVE' - AND (duration = 0 OR ends > @CurrentTime) - ), 0) AS BanCount - + COALESCE(( - SELECT COUNT(*) - FROM sa_bans - JOIN sa_players_ips ON sa_bans.player_steamid = sa_players_ips.steamid - WHERE sa_bans.status = 'ACTIVE' - AND sa_players_ips.address = @PlayerIP - AND (SELECT COUNT(*) FROM sa_bans WHERE (player_steamid = @PlayerSteamID OR player_ip = @PlayerIP) - AND status = 'ACTIVE' AND (duration = 0 OR ends > @CurrentTime)) = 0 - ), 0) AS TotalBanCount; + SELECT COALESCE(( + SELECT COUNT(*) + FROM sa_bans + WHERE (player_steamid = @PlayerSteamID OR player_ip = @PlayerIP) + AND status = 'ACTIVE' + AND (duration = 0 OR ends > @CurrentTime) + ), 0) + + + COALESCE(( + SELECT COUNT(*) + FROM sa_bans + JOIN sa_players_ips ON sa_bans.player_steamid = sa_players_ips.steamid + WHERE sa_bans.status = 'ACTIVE' + AND sa_players_ips.address = @PlayerIP + AND NOT EXISTS ( + SELECT 1 + FROM sa_bans + WHERE (player_steamid = @PlayerSteamID OR player_ip = @PlayerIP) + AND status = 'ACTIVE' + AND (duration = 0 OR ends > @CurrentTime) + ) + ), 0) AS TotalBanCount; """ : """ - SELECT COALESCE(( - SELECT COUNT(*) - FROM sa_bans - WHERE (player_steamid = @PlayerSteamID OR player_ip = @PlayerIP) - AND status = 'ACTIVE' - AND (duration = 0 OR ends > @CurrentTime) - AND server_id = @ServerId - ), 0) AS BanCount - + COALESCE(( - SELECT COUNT(*) - FROM sa_bans - JOIN sa_players_ips ON sa_bans.player_steamid = sa_players_ips.steamid - WHERE sa_bans.status = 'ACTIVE' - AND sa_players_ips.address = @PlayerIP - AND (SELECT COUNT(*) FROM sa_bans WHERE (player_steamid = @PlayerSteamID OR player_ip = @PlayerIP) - AND status = 'ACTIVE' AND (duration = 0 OR ends > @CurrentTime) AND server_id = @ServerId) = 0 - ), 0) AS TotalBanCount; + SELECT COALESCE(( + SELECT COUNT(*) + FROM sa_bans + WHERE (player_steamid = @PlayerSteamID OR player_ip = @PlayerIP) + AND status = 'ACTIVE' + AND (duration = 0 OR ends > @CurrentTime) + AND server_id = @ServerId + ), 0) + + + COALESCE(( + SELECT COUNT(*) + FROM sa_bans + JOIN sa_players_ips ON sa_bans.player_steamid = sa_players_ips.steamid + WHERE sa_bans.status = 'ACTIVE' + AND sa_players_ips.address = @PlayerIP + AND NOT EXISTS ( + SELECT 1 + FROM sa_bans + WHERE (player_steamid = @PlayerSteamID OR player_ip = @PlayerIP) + AND status = 'ACTIVE' + AND (duration = 0 OR ends > @CurrentTime) + AND server_id = @ServerId + ) + ), 0) AS TotalBanCount; """; await using var connection = await database.GetConnectionAsync(); @@ -176,8 +189,10 @@ FROM sa_bans banCount = await connection.ExecuteScalarAsync(sql, parameters); } - catch (Exception) + catch (Exception ex) { + CS2_SimpleAdmin._logger?.LogError("Unable to check ban status for {PlayerName} ({ExceptionMessage})", + player.Name, ex.Message); return false; } diff --git a/CS2-SimpleAdmin/Managers/MuteManager.cs b/CS2-SimpleAdmin/Managers/MuteManager.cs index ef907eb..3a5637a 100644 --- a/CS2-SimpleAdmin/Managers/MuteManager.cs +++ b/CS2-SimpleAdmin/Managers/MuteManager.cs @@ -176,7 +176,7 @@ public async Task CheckOnlineModeMutes(List<(string? IpAddress, ulong SteamID, i try { - int batchSize = 10; + var batchSize = 10; await using var connection = await database.GetConnectionAsync(); var sql = CS2_SimpleAdmin.Instance.Config.MultiServerMode @@ -188,9 +188,9 @@ public async Task CheckOnlineModeMutes(List<(string? IpAddress, ulong SteamID, i var batch = players.Skip(i).Take(batchSize); var parametersList = new List(); - foreach (var (IpAddress, SteamID, UserId, Slot) in batch) + foreach (var (_, steamId, _, _) in batch) { - parametersList.Add(new { PlayerSteamID = SteamID, serverid = CS2_SimpleAdmin.ServerId }); + parametersList.Add(new { PlayerSteamID = steamId, serverid = CS2_SimpleAdmin.ServerId }); } await connection.ExecuteAsync(sql, parametersList); @@ -201,14 +201,14 @@ public async Task CheckOnlineModeMutes(List<(string? IpAddress, ulong SteamID, i : "SELECT * FROM `sa_mutes` WHERE player_steamid = @PlayerSteamID AND passed >= duration AND duration > 0 AND status = 'ACTIVE' AND server_id = @serverid"; - foreach (var (IpAddress, SteamID, UserId, Slot) in players) + foreach (var (_, steamId, _, slot) in players) { - var muteRecords = await connection.QueryAsync(sql, new { PlayerSteamID = SteamID, serverid = CS2_SimpleAdmin.ServerId }); + var muteRecords = await connection.QueryAsync(sql, new { PlayerSteamID = steamId, serverid = CS2_SimpleAdmin.ServerId }); foreach (var muteRecord in muteRecords) { DateTime endDateTime = muteRecord.ends; - PlayerPenaltyManager.RemovePenaltiesByDateTime(Slot, endDateTime); + PlayerPenaltyManager.RemovePenaltiesByDateTime(slot, endDateTime); } } @@ -295,7 +295,7 @@ public async Task ExpireOldMutes() try { await using var connection = await database.GetConnectionAsync(); - var sql = ""; + string sql; if (CS2_SimpleAdmin.Instance.Config.MultiServerMode) { diff --git a/CS2-SimpleAdmin/Managers/PlayerManager.cs b/CS2-SimpleAdmin/Managers/PlayerManager.cs index cb19d2d..d4138ed 100644 --- a/CS2-SimpleAdmin/Managers/PlayerManager.cs +++ b/CS2-SimpleAdmin/Managers/PlayerManager.cs @@ -86,8 +86,8 @@ public void LoadPlayerData(CCSPlayerController player) try { // Check if the player is banned - bool isBanned = await CS2_SimpleAdmin.Instance.BanManager.IsPlayerBanned(CS2_SimpleAdmin.PlayersInfo[userId]); - + var isBanned = await CS2_SimpleAdmin.Instance.BanManager.IsPlayerBanned(CS2_SimpleAdmin.PlayersInfo[userId]); + if (isBanned) { // Add player's IP and SteamID to bannedPlayers list if not already present @@ -205,14 +205,14 @@ await Server.NextFrameAsync(() => public void CheckPlayersTimer() { - CS2_SimpleAdmin.Database = new Database.Database(CS2_SimpleAdmin.Instance.DbConnectionString); - CS2_SimpleAdmin.Instance.AddTimer(61.0f, () => { #if DEBUG CS2_SimpleAdmin._logger?.LogCritical("[OnMapStart] Expired check"); #endif - + if (CS2_SimpleAdmin.Database == null) + return; + var players = Helper.GetValidPlayers(); var onlinePlayers = players .Where(player => player.IpAddress != null) @@ -271,6 +271,6 @@ await Server.NextFrameAsync(() => } }); }); - }, CounterStrikeSharp.API.Modules.Timers.TimerFlags.REPEAT | CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE); + }, CounterStrikeSharp.API.Modules.Timers.TimerFlags.REPEAT); } } \ No newline at end of file