From f95031a3f5c919639ce92c3d2754be57edaae50e Mon Sep 17 00:00:00 2001 From: daffyyyy Date: Mon, 18 Dec 2023 13:46:58 +0100 Subject: [PATCH] 1.2.4a - mysql admins - minor changes --- AdminSQLManager.cs | 79 ++++++++++++++++++++++ CS2-SimpleAdmin.cs | 165 +++++++++++++++++++++++++++++++++++++-------- Events.cs | 35 ++++++++++ README.md | 2 + 4 files changed, 253 insertions(+), 28 deletions(-) create mode 100644 AdminSQLManager.cs diff --git a/AdminSQLManager.cs b/AdminSQLManager.cs new file mode 100644 index 0000000..5ebe320 --- /dev/null +++ b/AdminSQLManager.cs @@ -0,0 +1,79 @@ +using Dapper; +using MySqlConnector; +using System.Collections.Generic; + +namespace CS2_SimpleAdmin +{ + internal class AdminSQLManager + { + private readonly MySqlConnection _dbConnection; + + public AdminSQLManager(string connectionString) + { + _dbConnection = new MySqlConnection(connectionString); + } + + public async Task> GetAdminFlags(string steamId) + { + await using var connection = _dbConnection; + await connection.OpenAsync(); + + DateTime now = DateTime.Now; + + string sql = "SELECT flags FROM sa_admins WHERE player_steamid = @PlayerSteamID AND (ends IS NULL OR ends > @CurrentTime)"; + List activeFlags = (await connection.QueryAsync(sql, new { PlayerSteamID = steamId, CurrentTime = now })).ToList(); + + return activeFlags; + } + + public async Task DeleteAdminBySteamId(string playerSteamId) + { + if (string.IsNullOrEmpty(playerSteamId)) return; + + await using var connection = _dbConnection; + await connection.OpenAsync(); + + string sql = "DELETE FROM sa_admins WHERE player_steamid = @PlayerSteamID"; + await connection.ExecuteAsync(sql, new { PlayerSteamID = playerSteamId }); + } + + public async Task AddAdminBySteamId(string playerSteamId, string playerName, string flags, int immunity = 0, int time = 0) + { + if (string.IsNullOrEmpty(playerSteamId)) return; + + flags = flags.Replace(" ", ""); + + DateTime now = DateTime.Now; + DateTime? futureTime; + if (time != 0) + futureTime = now.AddMinutes(time); + else + futureTime = null; + + await using var connection = _dbConnection; + await connection.OpenAsync(); + + var sql = "INSERT INTO `sa_admins` (`player_steamid`, `player_name`, `flags`, `immunity`, `ends`, `created`) " + + "VALUES (@playerSteamid, @playerName, @flags, @immunity, @ends, @created)"; + + await connection.ExecuteAsync(sql, new + { + playerSteamId, + playerName, + flags, + immunity, + ends = futureTime, + created = now + }); + } + + public async Task DeleteOldAdmins() + { + await using var connection = _dbConnection; + await connection.OpenAsync(); + + string sql = "DELETE FROM sa_admins WHERE ends IS NOT NULL AND ends <= @CurrentTime"; + await connection.ExecuteAsync(sql, new { CurrentTime = DateTime.Now }); + } + } +} diff --git a/CS2-SimpleAdmin.cs b/CS2-SimpleAdmin.cs index 47fef8f..6c9ab63 100644 --- a/CS2-SimpleAdmin.cs +++ b/CS2-SimpleAdmin.cs @@ -16,7 +16,7 @@ using System.Collections.Concurrent; using System.Text; namespace CS2_SimpleAdmin; -[MinimumApiVersion(126)] +[MinimumApiVersion(124)] public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig { public static IStringLocalizer? _localizer; @@ -29,9 +29,9 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig "CS2-SimpleAdmin"; - public override string ModuleDescription => ""; + public override string ModuleDescription => "Simple admin plugin for Counter-Strike 2 :)"; public override string ModuleAuthor => "daffyy"; - public override string ModuleVersion => "1.2.3a"; + public override string ModuleVersion => "1.2.4a"; public CS2_SimpleAdminConfig Config { get; set; } = new(); @@ -104,6 +104,20 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig ", whoCanExecute: CommandUsage.CLIENT_AND_SERVER)] + [RequiresPermissions("@css/root")] + public void OnAddAdminCommand(CCSPlayerController? caller, CommandInfo command) + { + if (!Helper.IsValidSteamID64(command.GetArg(1))) + { + command.ReplyToCommand($"Invalid SteamID64."); + return; + } + if (command.GetArg(2).Length <= 0) + { + command.ReplyToCommand($"Invalid player name."); + return; + } + if (!command.GetArg(3).Contains("@") && !command.GetArg(3).Contains("#")) + { + command.ReplyToCommand($"Invalid player name."); + return; + } + + string steamid = command.GetArg(1); + string name = command.GetArg(2); + string flags = command.GetArg(3); + int immunity = 0; + int.TryParse(command.GetArg(4), out immunity); + int time = 0; + int.TryParse(command.GetArg(5), out time); + + AdminSQLManager _adminManager = new(dbConnectionString); + _ = _adminManager.AddAdminBySteamId(steamid, name, flags, immunity, time); + + command.ReplyToCommand($"Added '{flags}' flags to '{name}' ({steamid})"); + } + + [ConsoleCommand("css_deladmin")] + [CommandHelper(minArgs: 1, usage: "", whoCanExecute: CommandUsage.CLIENT_AND_SERVER)] + [RequiresPermissions("@css/root")] + public void OnDelAdminCommand(CCSPlayerController? caller, CommandInfo command) + { + if (!Helper.IsValidSteamID64(command.GetArg(1))) + { + command.ReplyToCommand($"Invalid SteamID64."); + return; + } + + string steamid = command.GetArg(1); + + AdminSQLManager _adminManager = new(dbConnectionString); + _ = _adminManager.DeleteAdminBySteamId(steamid); + + command.ReplyToCommand($"Removed flags from '{steamid}'"); + } + [ConsoleCommand("css_who")] [CommandHelper(minArgs: 1, usage: "<#userid or name>", whoCanExecute: CommandUsage.CLIENT_AND_SERVER)] [RequiresPermissions("@css/generic")] @@ -163,30 +231,56 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig { - caller!.PrintToConsole($"--------- INFO ABOUT \"{playerInfo.Name}\" ---------"); - - caller!.PrintToConsole($"• Clan: \"{player!.Clan}\" Name: \"{playerInfo.Name}\""); - caller!.PrintToConsole($"• UserID: \"{playerInfo.UserId}\""); - if (playerInfo.SteamId != null) - caller!.PrintToConsole($"• SteamID64: \"{playerInfo.SteamId}\""); - if (player.AuthorizedSteamID != null) + if (caller != null) { - caller!.PrintToConsole($"• SteamID2: \"{player.AuthorizedSteamID.SteamId2}\""); - caller!.PrintToConsole($"• Community link: \"{player.AuthorizedSteamID.ToCommunityUrl()}\""); - } - if (playerInfo.IpAddress != null) - caller!.PrintToConsole($"• IP Address: \"{playerInfo.IpAddress}\""); - caller!.PrintToConsole($"• Ping: \"{player.Ping}\""); - if (player.AuthorizedSteamID != null) - { - caller!.PrintToConsole($"• Total Bans: \"{totalBans}\""); - caller!.PrintToConsole($"• Total Mutes: \"{totalMutes}\""); - } + caller!.PrintToConsole($"--------- INFO ABOUT \"{playerInfo.Name}\" ---------"); - caller!.PrintToConsole($"--------- END INFO ABOUT \"{player.PlayerName}\" ---------"); + caller!.PrintToConsole($"• Clan: \"{player!.Clan}\" Name: \"{playerInfo.Name}\""); + caller!.PrintToConsole($"• UserID: \"{playerInfo.UserId}\""); + if (playerInfo.SteamId != null) + caller!.PrintToConsole($"• SteamID64: \"{playerInfo.SteamId}\""); + if (player.AuthorizedSteamID != null) + { + caller!.PrintToConsole($"• SteamID2: \"{player.AuthorizedSteamID.SteamId2}\""); + caller!.PrintToConsole($"• Community link: \"{player.AuthorizedSteamID.ToCommunityUrl()}\""); + } + if (playerInfo.IpAddress != null) + caller!.PrintToConsole($"• IP Address: \"{playerInfo.IpAddress}\""); + caller!.PrintToConsole($"• Ping: \"{player.Ping}\""); + if (player.AuthorizedSteamID != null) + { + caller!.PrintToConsole($"• Total Bans: \"{totalBans}\""); + caller!.PrintToConsole($"• Total Mutes: \"{totalMutes}\""); + } + + caller!.PrintToConsole($"--------- END INFO ABOUT \"{player.PlayerName}\" ---------"); + } + else + { + Server.PrintToConsole($"--------- INFO ABOUT \"{playerInfo.Name}\" ---------"); + + Server.PrintToConsole($"• Clan: \"{player!.Clan}\" Name: \"{playerInfo.Name}\""); + Server.PrintToConsole($"• UserID: \"{playerInfo.UserId}\""); + if (playerInfo.SteamId != null) + Server.PrintToConsole($"• SteamID64: \"{playerInfo.SteamId}\""); + if (player.AuthorizedSteamID != null) + { + Server.PrintToConsole($"• SteamID2: \"{player.AuthorizedSteamID.SteamId2}\""); + Server.PrintToConsole($"• Community link: \"{player.AuthorizedSteamID.ToCommunityUrl()}\""); + } + if (playerInfo.IpAddress != null) + Server.PrintToConsole($"• IP Address: \"{playerInfo.IpAddress}\""); + Server.PrintToConsole($"• Ping: \"{player.Ping}\""); + if (player.AuthorizedSteamID != null) + { + Server.PrintToConsole($"• Total Bans: \"{totalBans}\""); + Server.PrintToConsole($"• Total Mutes: \"{totalMutes}\""); + } + + Server.PrintToConsole($"--------- END INFO ABOUT \"{player.PlayerName}\" ---------"); + } }); }); @@ -202,13 +296,26 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig playersToTarget = targets!.Players.Where(player => caller!.CanTarget(player) && player != null && player.IsValid && !player.IsBot && !player.IsHLTV).ToList(); - caller!.PrintToConsole($"--------- PLAYER LIST ---------"); - playersToTarget.ForEach(player => + if (caller != null) { - caller!.PrintToConsole($"• [#{player.UserId}] \"{player.PlayerName}\" (IP Address: \"{player.IpAddress?.Split(":")[0]}\" SteamID64: \"{player.AuthorizedSteamID?.SteamId64}\")"); + caller!.PrintToConsole($"--------- PLAYER LIST ---------"); + playersToTarget.ForEach(player => + { + caller!.PrintToConsole($"• [#{player.UserId}] \"{player.PlayerName}\" (IP Address: \"{player.IpAddress?.Split(":")[0]}\" SteamID64: \"{player.AuthorizedSteamID?.SteamId64}\")"); - }); - caller!.PrintToConsole($"--------- END PLAYER LIST ---------"); + }); + caller!.PrintToConsole($"--------- END PLAYER LIST ---------"); + } + else + { + Server.PrintToConsole($"--------- PLAYER LIST ---------"); + playersToTarget.ForEach(player => + { + Server.PrintToConsole($"• [#{player.UserId}] \"{player.PlayerName}\" (IP Address: \"{player.IpAddress?.Split(":")[0]}\" SteamID64: \"{player.AuthorizedSteamID?.SteamId64}\")"); + + }); + Server.PrintToConsole($"--------- END PLAYER LIST ---------"); + } } [ConsoleCommand("css_kick")] @@ -1130,6 +1237,7 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig ", whoCanExecute: CommandUsage.CLIENT_AND_SERVER)] @@ -1288,6 +1396,7 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig [... options ...]", whoCanExecute: CommandUsage.CLIENT_AND_SERVER)] diff --git a/Events.cs b/Events.cs index f39f87e..4d98187 100644 --- a/Events.cs +++ b/Events.cs @@ -127,6 +127,9 @@ public partial class CS2_SimpleAdmin MuteManager _muteManager = new(dbConnectionString); List activeMutes = await _muteManager.IsPlayerMuted(playerInfo.SteamId!); + AdminSQLManager _adminManager = new(dbConnectionString); + List activeFlags = await _adminManager.GetAdminFlags(playerInfo.SteamId!); + Server.NextFrame(() => { if (player == null || !player.IsValid) return; @@ -218,6 +221,36 @@ public partial class CS2_SimpleAdmin } } } + + if (activeFlags != null && activeFlags.Count > 0) + { + foreach (var flags in activeFlags) + { + if (flags == null) continue; + string flagsValue = flags.flags.ToString(); + + if (!string.IsNullOrEmpty(flagsValue)) + { + string[] _flags = flagsValue.Split(","); + + AddTimer(10, () => + { + if (player == null) return; + foreach (var _flag in _flags) + { + if (_flag.StartsWith("@")) + { + AdminManager.AddPlayerPermissions(player, _flag); + } + if (_flag.StartsWith("3")) + { + AdminManager.AddPlayerToGroup(player, _flag); + } + } + }); + } + } + } }); }); } @@ -261,6 +294,8 @@ public partial class CS2_SimpleAdmin _ = _banManager.ExpireOldBans(); MuteManager _muteManager = new(dbConnectionString); _ = _muteManager.ExpireOldMutes(); + AdminSQLManager _adminManager = new(dbConnectionString); + _ = _adminManager.DeleteOldAdmins(); }, CounterStrikeSharp.API.Modules.Timers.TimerFlags.REPEAT | CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE); string? path = Path.GetDirectoryName(ModuleDirectory); diff --git a/README.md b/README.md index 229cc65..7a82fc5 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,8 @@ Manage your Counter-Strike 2 server by simple commands :) ### Commands ```js +- css_addadmin [time in minutes] - Add admin by steamid // @css/root +- css_deladmin - Delete admin by steamid // @css/root - css_admin - Display all admin commands // @css/generic - css_who <#userid or name> - Display informations about player // @css/generic - css_players - Display player list // @css/generic