diff --git a/AdminSQLManager.cs b/AdminSQLManager.cs index f10d9a0..8b46e32 100644 --- a/AdminSQLManager.cs +++ b/AdminSQLManager.cs @@ -6,13 +6,15 @@ namespace CS2_SimpleAdmin internal class AdminSQLManager { private readonly MySqlConnection _dbConnection; - public static readonly Dictionary> _adminCache = new Dictionary>(); + // Unused for now + //public static readonly ConcurrentDictionary> _adminCache = new ConcurrentDictionary>(); public AdminSQLManager(string connectionString) { _dbConnection = new MySqlConnection(connectionString); } + /* public async Task> GetAdminFlags(string steamId) { if (_adminCache.ContainsKey(steamId)) @@ -26,7 +28,7 @@ namespace CS2_SimpleAdmin DateTime now = DateTime.Now; - string sql = "SELECT flags FROM sa_admins WHERE player_steamid = @PlayerSteamID AND (ends IS NULL OR ends > @CurrentTime)"; + string sql = "SELECT flags, ends 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(); _adminCache[steamId] = new List(); @@ -39,13 +41,91 @@ namespace CS2_SimpleAdmin } return _adminCache[steamId].Select(flag => (dynamic)flag).ToList(); } + */ + + public async Task> GetAdminFlags(string steamId) + { + /* Unused for now + if (_adminCache.TryGetValue(steamId, out ConcurrentBag? cachedFlags)) + { + return cachedFlags.ToList(); + } + */ + DateTime now = DateTime.Now; + + await using var connection = _dbConnection; + await connection.OpenAsync(); + + string sql = "SELECT flags, ends 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(); + + if (activeFlags == null) + { + return new List(); + } + + List filteredFlags = new List(); + + foreach (var flags in activeFlags) + { + if (flags == null) continue; + + string flag = flags.flags.ToString(); + if (flag != null) + { + filteredFlags.Add(flag); + } + } + + /* Unused for now + bool shouldCache = activeFlags.Any(flags => + { + if (flags?.ends == null) + { + return true; + } + + if (flags.ends is DateTime endsTime) + { + return (endsTime - now).TotalHours > 1; + } + + return false; + }); + + if (shouldCache) + { + List flagsToCache = new List(); + + foreach (var flags in activeFlags) + { + if (flags.ends == null || (DateTime.Now - (DateTime)flags.ends).TotalHours > 6) + { + if (flags == null) continue; + flagsToCache.Add(flags.flags.ToString()); + } + } + + _adminCache.AddOrUpdate(steamId, new ConcurrentBag(flagsToCache), (_, existingBag) => + { + foreach (var flag in flagsToCache) + { + existingBag.Add(flag); + } + return existingBag; + }); + return flagsToCache.Cast().ToList(); + } + */ + + return filteredFlags.Cast().ToList(); + } public async Task DeleteAdminBySteamId(string playerSteamId) { if (string.IsNullOrEmpty(playerSteamId)) return; - if (_adminCache.ContainsKey(playerSteamId)) - _adminCache.Remove(playerSteamId); + //_adminCache.TryRemove(playerSteamId, out _); await using var connection = _dbConnection; await connection.OpenAsync(); @@ -93,4 +173,4 @@ namespace CS2_SimpleAdmin await connection.ExecuteAsync(sql, new { CurrentTime = DateTime.Now }); } } -} +} \ No newline at end of file diff --git a/BanManager.cs b/BanManager.cs index 2f37d71..e327cb2 100644 --- a/BanManager.cs +++ b/BanManager.cs @@ -11,6 +11,7 @@ namespace CS2_SimpleAdmin { _dbConnection = new MySqlConnection(connectionString); } + public async Task BanPlayer(PlayerInfo player, PlayerInfo issuer, string reason, int time = 0) { DateTime now = DateTime.Now; @@ -152,4 +153,4 @@ namespace CS2_SimpleAdmin await connection.ExecuteAsync(sql, new { CurrentTime = DateTime.Now }); } } -} +} \ No newline at end of file diff --git a/CS2-SimpleAdmin.cs b/CS2-SimpleAdmin.cs index 9dc75f4..54429af 100644 --- a/CS2-SimpleAdmin.cs +++ b/CS2-SimpleAdmin.cs @@ -16,7 +16,8 @@ using System.Collections.Concurrent; using System.Text; namespace CS2_SimpleAdmin; -[MinimumApiVersion(124)] + +[MinimumApiVersion(142)] public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig { public static IStringLocalizer? _localizer; @@ -31,7 +32,7 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig "CS2-SimpleAdmin"; public override string ModuleDescription => "Simple admin plugin for Counter-Strike 2 :)"; public override string ModuleAuthor => "daffyy"; - public override string ModuleVersion => "1.2.4b"; + public override string ModuleVersion => "1.2.5a"; public CS2_SimpleAdminConfig Config { get; set; } = new(); @@ -125,7 +126,7 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig playersToTarget = targets!.Players.Where(player => caller!.CanTarget(player) && player != null && player.IsValid && !player.IsBot && !player.IsHLTV).ToList(); + List playersToTarget = targets!.Players.Where(player => caller!.CanTarget(player) && player != null && player.IsValid && player.Connected == PlayerConnectedState.PlayerConnected && !player.IsHLTV).ToList(); if (caller != null) { @@ -302,7 +302,6 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig { caller!.PrintToConsole($"• [#{player.UserId}] \"{player.PlayerName}\" (IP Address: \"{player.IpAddress?.Split(":")[0]}\" SteamID64: \"{player.AuthorizedSteamID?.SteamId64}\")"); - }); caller!.PrintToConsole($"--------- END PLAYER LIST ---------"); } @@ -312,7 +311,6 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig { Server.PrintToConsole($"• [#{player.UserId}] \"{player.PlayerName}\" (IP Address: \"{player.IpAddress?.Split(":")[0]}\" SteamID64: \"{player.AuthorizedSteamID?.SteamId64}\")"); - }); Server.PrintToConsole($"--------- END PLAYER LIST ---------"); } @@ -1020,7 +1018,6 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig matches = Helper.GetPlayerFromSteamid64(steamid); if (matches.Count == 1) { @@ -1237,7 +1234,6 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig ", whoCanExecute: CommandUsage.CLIENT_AND_SERVER)] @@ -1274,8 +1270,11 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig { + /* player.Speed = (float)speed; player.PlayerPawn.Value!.Speed = (float)speed; + */ + player.SetSpeed((float)speed); StringBuilder sb = new(_localizer!["sa_prefix"]); sb.Append(_localizer["sa_admin_speed_message", caller == null ? "Console" : caller.PlayerName, player.PlayerName]); @@ -1350,15 +1349,18 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig [... options ...]", whoCanExecute: CommandUsage.CLIENT_AND_SERVER)] @@ -1445,7 +1446,6 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig p.IsValid && !p.IsBot && !p.IsHLTV && AdminManager.PlayerHasPermissions(p, "@css/chat"))) { @@ -1516,8 +1519,13 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig { - player.PrintToChat(Helper.ReplaceTags($"({caller!.PlayerName}) {message}")); + player.PrintToChat(Helper.ReplaceTags($"({caller!.PlayerName}) {utf8String}")); }); command.ReplyToCommand(Helper.ReplaceTags($" Private message sent!")); @@ -1546,7 +1557,10 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig - + diff --git a/Config.cs b/Config.cs index f92fcd2..6875113 100644 --- a/Config.cs +++ b/Config.cs @@ -24,6 +24,7 @@ namespace CS2_SimpleAdmin [JsonPropertyName("KickTime")] public int KickTime { get; set; } = 10; + [JsonPropertyName("DisableDangerousCommands")] public bool DisableDangerousCommands { get; set; } = true; } diff --git a/Events.cs b/Events.cs index f1aea34..71172f7 100644 --- a/Events.cs +++ b/Events.cs @@ -7,11 +7,13 @@ using System.Text; using static CounterStrikeSharp.API.Core.Listeners; namespace CS2_SimpleAdmin; + public partial class CS2_SimpleAdmin { private void registerEvents() { RegisterListener(OnClientAuthorized); + //RegisterEventHandler(OnPlayerFullConnect); RegisterListener(OnClientDisconnect); RegisterListener(OnMapStart); RegisterEventHandler(OnPlayerHurt); @@ -21,6 +23,32 @@ public partial class CS2_SimpleAdmin AddCommandListener("callvote", OnCommandCallVote); } + /*private HookResult OnPlayerFullConnect(EventPlayerConnectFull @event, GameEventInfo info) + { + CCSPlayerController? player = @event.Userid; + + if (player == null || player.IsBot || player.IsHLTV) return HookResult.Continue; + + PlayerInfo playerInfo = new PlayerInfo + { + UserId = player.UserId, + Index = (int)player.Index, + SteamId = player?.AuthorizedSteamID?.SteamId64.ToString(), + Name = player?.PlayerName, + IpAddress = player?.IpAddress?.Split(":")[0] + }; + + Task.Run(async () => + { + Server.NextFrame(() => + { + if (player == null) return; + }); + }); + return HookResult.Continue; + } + */ + private HookResult OnRoundStart(EventRoundStart @event, GameEventInfo info) { GodPlayers.Clear(); @@ -100,15 +128,6 @@ public partial class CS2_SimpleAdmin if (player == null || !player.IsValid || player.IsBot || player.IsHLTV) return; - if (player.AuthorizedSteamID == null) - { - AddTimer(3.0f, () => - { - OnClientAuthorized(playerSlot, steamID); - }); - return; - } - PlayerInfo playerInfo = new PlayerInfo { UserId = player.UserId, @@ -120,7 +139,6 @@ public partial class CS2_SimpleAdmin Task.Run(async () => { - if (player == null || !player.IsValid) return; BanManager _banManager = new(dbConnectionString); bool isBanned = await _banManager.IsPlayerBanned(playerInfo); @@ -178,7 +196,6 @@ public partial class CS2_SimpleAdmin }, CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE); } - /* CCSPlayerController currentPlayer = player; @@ -222,33 +239,7 @@ public partial class CS2_SimpleAdmin } } - if (AdminSQLManager._adminCache.ContainsKey(playerInfo.SteamId!)) - { - AddTimer(10, () => - { - foreach (var flagsValue in AdminSQLManager._adminCache[playerInfo.SteamId!]) - { - if (!string.IsNullOrEmpty(flagsValue)) - { - string[] _flags = flagsValue.Split(','); - - if (player == null) return; - foreach (var _flag in _flags) - { - if (_flag.StartsWith("@")) - { - AdminManager.AddPlayerPermissions(player, _flag); - } - if (_flag.StartsWith("#")) - { - AdminManager.AddPlayerToGroup(player, _flag); - } - } - } - } - }); - } - + AddTimer(14, () => Helper.GivePlayerFlags(player, activeFlags)); /* @@ -315,10 +306,10 @@ public partial class CS2_SimpleAdmin if (player.AuthorizedSteamID != null) { - if (AdminSQLManager._adminCache.ContainsKey(player.AuthorizedSteamID.SteamId64.ToString())) - { - AdminSQLManager._adminCache.Remove(player.AuthorizedSteamID.SteamId64.ToString()); - } + //string steamIdString = player.AuthorizedSteamID.SteamId64.ToString(); + + //AdminSQLManager._adminCache.TryRemove(steamIdString, out _); + AdminManager.RemovePlayerPermissions(player); } if (TagsDetected) @@ -359,4 +350,4 @@ public partial class CS2_SimpleAdmin return HookResult.Continue; } -} +} \ No newline at end of file diff --git a/Helper.cs b/Helper.cs index 1f7aae8..885d9db 100644 --- a/Helper.cs +++ b/Helper.cs @@ -1,9 +1,10 @@ -using CounterStrikeSharp.API.Core; -using CounterStrikeSharp.API; +using CounterStrikeSharp.API; +using CounterStrikeSharp.API.Core; +using CounterStrikeSharp.API.Modules.Admin; +using CounterStrikeSharp.API.Modules.Menu; using CounterStrikeSharp.API.Modules.Utils; using System.Reflection; using System.Text.RegularExpressions; -using CounterStrikeSharp.API.Modules.Menu; namespace CS2_SimpleAdmin { @@ -46,6 +47,35 @@ namespace CS2_SimpleAdmin return Regex.IsMatch(input, pattern); } + public static void GivePlayerFlags(CCSPlayerController player, dynamic flags) + { + if (player == null) return; + + foreach (var flagsValue in flags) + { + if (!string.IsNullOrEmpty(flagsValue)) + { + string[] _flags = flagsValue.Split(','); + Console.WriteLine(flagsValue); + + foreach (var _flag in _flags) + { + Server.NextFrame(() => + { + if (_flag.StartsWith("@")) + { + AdminManager.AddPlayerPermissions(player, _flag); + } + if (_flag.StartsWith("#")) + { + AdminManager.AddPlayerToGroup(player, _flag); + } + }); + } + } + } + } + /* public static TargetResult GetTarget(string target, out CCSPlayerController? player) { @@ -105,6 +135,5 @@ namespace CS2_SimpleAdmin if (CS2_SimpleAdmin.voteInProgress) CS2_SimpleAdmin.voteAnswers[option.Text]++; } - } -} +} \ No newline at end of file diff --git a/MuteManager.cs b/MuteManager.cs index 50f6267..e32f972 100644 --- a/MuteManager.cs +++ b/MuteManager.cs @@ -1,5 +1,4 @@ using CounterStrikeSharp.API.Core; -using CounterStrikeSharp.API.Modules.Entities; using Dapper; using MySqlConnector; using System.Data; @@ -188,7 +187,5 @@ namespace CS2_SimpleAdmin } } } - - } -} +} \ No newline at end of file diff --git a/PlayerInfo.cs b/PlayerInfo.cs index 13e55c2..2173ce7 100644 --- a/PlayerInfo.cs +++ b/PlayerInfo.cs @@ -8,4 +8,4 @@ public string? Name { get; set; } public string? IpAddress { get; set; } } -} +} \ No newline at end of file diff --git a/PlayerUtils.cs b/PlayerUtils.cs index e493de8..539512e 100644 --- a/PlayerUtils.cs +++ b/PlayerUtils.cs @@ -1,6 +1,5 @@ using CounterStrikeSharp.API.Core; using CounterStrikeSharp.API.Modules.Admin; -using CounterStrikeSharp.API.Modules.Menu; using CounterStrikeSharp.API.Modules.Utils; using System.Text; @@ -12,6 +11,7 @@ public static class PlayerUtils { PerformSlap(pawn, damage); } + public static void Print(this CCSPlayerController controller, string message = "") { StringBuilder _message = new(CS2_SimpleAdmin._localizer!["sa_prefix"]); @@ -25,6 +25,14 @@ public static class PlayerUtils return AdminManager.CanPlayerTarget(controller, target); } + public static void SetSpeed(this CCSPlayerController controller, float speed) + { + CCSPlayerPawn? playerPawnValue = controller.PlayerPawn.Value; + if (playerPawnValue == null) return; + + playerPawnValue.VelocityModifier = speed; + } + public static void SetHp(this CCSPlayerController controller, int health = 100) { if (health <= 0 || !controller.PawnIsAlive || controller.PlayerPawn.Value == null) return; diff --git a/README.md b/README.md index 7a82fc5..e2df2c0 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ Manage your Counter-Strike 2 server by simple commands :) ``` ### Requirments -[CounterStrikeSharp](https://github.com/roflmuffin/CounterStrikeSharp/) **tested on v90** +[CounterStrikeSharp](https://github.com/roflmuffin/CounterStrikeSharp/) **tested on v142** ### Configuration After first launch, u need to configure plugin in addons/counterstrikesharp/configs/plugins/CS2-SimpleAdmin/CS2-SimpleAdmin.json diff --git a/lang/lv.json b/lang/lv.json index 859a4f3..1a697c6 100644 --- a/lang/lv.json +++ b/lang/lv.json @@ -35,4 +35,4 @@ "sa_adminsay_prefix": "{RED}ADMINS: {lightred}{0}{default}", "sa_adminchat_template_admin": "{LIME}(ADMINS) {lightred}{0}{default}: {lightred}{1}{default}", "sa_adminchat_template_player": "{SILVER}(SPĒLĒTĀJS) {lightred}{0}{default}: {lightred}{1}{default}" -} +} \ No newline at end of file