mirror of
https://github.com/daffyyyy/CS2-SimpleAdmin.git
synced 2026-03-06 07:16:42 +00:00
1.2.6d
- Probably workaround for sql admins
This commit is contained in:
@@ -1,4 +1,6 @@
|
|||||||
using Dapper;
|
using CounterStrikeSharp.API.Modules.Admin;
|
||||||
|
using CounterStrikeSharp.API.Modules.Entities;
|
||||||
|
using Dapper;
|
||||||
using MySqlConnector;
|
using MySqlConnector;
|
||||||
|
|
||||||
namespace CS2_SimpleAdmin
|
namespace CS2_SimpleAdmin
|
||||||
@@ -8,6 +10,8 @@ namespace CS2_SimpleAdmin
|
|||||||
private readonly MySqlConnection _dbConnection;
|
private readonly MySqlConnection _dbConnection;
|
||||||
// Unused for now
|
// Unused for now
|
||||||
//public static readonly ConcurrentDictionary<string, ConcurrentBag<string>> _adminCache = new ConcurrentDictionary<string, ConcurrentBag<string>>();
|
//public static readonly ConcurrentDictionary<string, ConcurrentBag<string>> _adminCache = new ConcurrentDictionary<string, ConcurrentBag<string>>();
|
||||||
|
public static readonly HashSet<SteamID> _adminCacheSet = new HashSet<SteamID>();
|
||||||
|
public static readonly Dictionary<SteamID, DateTime?> _adminCacheTimestamps = new Dictionary<SteamID, DateTime?>();
|
||||||
|
|
||||||
public AdminSQLManager(string connectionString)
|
public AdminSQLManager(string connectionString)
|
||||||
{
|
{
|
||||||
@@ -148,6 +152,93 @@ namespace CS2_SimpleAdmin
|
|||||||
//return filteredFlags.Cast<object>().ToList();
|
//return filteredFlags.Cast<object>().ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<List<(string, List<string>, int, DateTime?)>> GetAllPlayersFlags()
|
||||||
|
{
|
||||||
|
DateTime now = DateTime.Now;
|
||||||
|
|
||||||
|
await using var connection = _dbConnection;
|
||||||
|
await connection.OpenAsync();
|
||||||
|
|
||||||
|
string sql = "SELECT player_steamid, flags, immunity, ends FROM sa_admins WHERE (ends IS NULL OR ends > @CurrentTime) AND (server_id IS NULL OR server_id = @serverid)";
|
||||||
|
List<dynamic>? activeFlags = (await connection.QueryAsync(sql, new { CurrentTime = now, serverid = CS2_SimpleAdmin.ServerId }))?.ToList();
|
||||||
|
|
||||||
|
if (activeFlags == null)
|
||||||
|
{
|
||||||
|
return new List<(string, List<string>, int, DateTime?)>();
|
||||||
|
}
|
||||||
|
|
||||||
|
List<(string, List<string>, int, DateTime?)> filteredFlagsWithImmunity = new List<(string, List<string>, int, DateTime?)>();
|
||||||
|
|
||||||
|
foreach (dynamic flags in activeFlags)
|
||||||
|
{
|
||||||
|
if (flags is not IDictionary<string, object> flagsDict)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!flagsDict.TryGetValue("player_steamid", out var steamIdObj) ||
|
||||||
|
!flagsDict.TryGetValue("flags", out var flagsValueObj) ||
|
||||||
|
!flagsDict.TryGetValue("immunity", out var immunityValueObj) ||
|
||||||
|
!flagsDict.TryGetValue("ends", out var endsObj))
|
||||||
|
{
|
||||||
|
//Console.WriteLine("One or more required keys are missing.");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
DateTime? ends = null;
|
||||||
|
|
||||||
|
if (endsObj != null) // Check if "ends" is not null
|
||||||
|
{
|
||||||
|
if (!DateTime.TryParse(endsObj.ToString(), out var parsedEnds))
|
||||||
|
{
|
||||||
|
//Console.WriteLine("Failed to parse 'ends' value.");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
ends = parsedEnds;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(steamIdObj is string steamId) ||
|
||||||
|
!(flagsValueObj is string flagsValue) ||
|
||||||
|
!int.TryParse(immunityValueObj.ToString(), out var immunityValue))
|
||||||
|
{
|
||||||
|
//Console.WriteLine("Failed to parse one or more values.");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
filteredFlagsWithImmunity.Add((steamId, flagsValue.Split(',').ToList(), immunityValue, ends));
|
||||||
|
}
|
||||||
|
|
||||||
|
return filteredFlagsWithImmunity;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task GiveAllFlags()
|
||||||
|
{
|
||||||
|
List<(string, List<string>, int, DateTime?)> allPlayers = await GetAllPlayersFlags();
|
||||||
|
|
||||||
|
foreach (var record in allPlayers)
|
||||||
|
{
|
||||||
|
string steamIdStr = record.Item1;
|
||||||
|
List<string> flags = record.Item2;
|
||||||
|
int immunity = record.Item3;
|
||||||
|
|
||||||
|
DateTime? ends = record.Item4;
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(steamIdStr) && SteamID.TryParse(steamIdStr, out var steamId) && steamId != null)
|
||||||
|
{
|
||||||
|
if (!_adminCacheSet.Contains(steamId))
|
||||||
|
{
|
||||||
|
_adminCacheSet.Add(steamId);
|
||||||
|
_adminCacheTimestamps.Add(steamId, ends);
|
||||||
|
}
|
||||||
|
|
||||||
|
Helper.GivePlayerFlags(steamId, flags, (uint)immunity);
|
||||||
|
// Often need to call 2 times
|
||||||
|
Helper.GivePlayerFlags(steamId, flags, (uint)immunity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public async Task DeleteAdminBySteamId(string playerSteamId)
|
public async Task DeleteAdminBySteamId(string playerSteamId)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(playerSteamId)) return;
|
if (string.IsNullOrEmpty(playerSteamId)) return;
|
||||||
@@ -159,6 +250,18 @@ namespace CS2_SimpleAdmin
|
|||||||
|
|
||||||
string sql = "DELETE FROM sa_admins WHERE player_steamid = @PlayerSteamID";
|
string sql = "DELETE FROM sa_admins WHERE player_steamid = @PlayerSteamID";
|
||||||
await connection.ExecuteAsync(sql, new { PlayerSteamID = playerSteamId });
|
await connection.ExecuteAsync(sql, new { PlayerSteamID = playerSteamId });
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(playerSteamId) && SteamID.TryParse(playerSteamId, out var steamId) && steamId != null)
|
||||||
|
{
|
||||||
|
if (_adminCacheSet.Contains(steamId))
|
||||||
|
{
|
||||||
|
_adminCacheSet.Remove(steamId);
|
||||||
|
_adminCacheTimestamps.Remove(steamId);
|
||||||
|
}
|
||||||
|
|
||||||
|
AdminManager.ClearPlayerPermissions(steamId);
|
||||||
|
AdminManager.RemovePlayerAdminData(steamId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task AddAdminBySteamId(string playerSteamId, string playerName, string flags, int immunity = 0, int time = 0)
|
public async Task AddAdminBySteamId(string playerSteamId, string playerName, string flags, int immunity = 0, int time = 0)
|
||||||
@@ -190,6 +293,8 @@ namespace CS2_SimpleAdmin
|
|||||||
created = now,
|
created = now,
|
||||||
serverid = CS2_SimpleAdmin.ServerId
|
serverid = CS2_SimpleAdmin.ServerId
|
||||||
});
|
});
|
||||||
|
|
||||||
|
_ = GiveAllFlags();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task DeleteOldAdmins()
|
public async Task DeleteOldAdmins()
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig<CS2_SimpleAdmin
|
|||||||
public override string ModuleName => "CS2-SimpleAdmin";
|
public override string ModuleName => "CS2-SimpleAdmin";
|
||||||
public override string ModuleDescription => "Simple admin plugin for Counter-Strike 2 :)";
|
public override string ModuleDescription => "Simple admin plugin for Counter-Strike 2 :)";
|
||||||
public override string ModuleAuthor => "daffyy";
|
public override string ModuleAuthor => "daffyy";
|
||||||
public override string ModuleVersion => "1.2.6c";
|
public override string ModuleVersion => "1.2.6d";
|
||||||
|
|
||||||
public CS2_SimpleAdminConfig Config { get; set; } = new();
|
public CS2_SimpleAdminConfig Config { get; set; } = new();
|
||||||
|
|
||||||
|
|||||||
23
Events.cs
23
Events.cs
@@ -190,10 +190,11 @@ public partial class CS2_SimpleAdmin
|
|||||||
if (isBanned)
|
if (isBanned)
|
||||||
{
|
{
|
||||||
Helper.KickPlayer((ushort)player.UserId!, "Banned");
|
Helper.KickPlayer((ushort)player.UserId!, "Banned");
|
||||||
Console.WriteLine("Authorized banned");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Helper.GivePlayerFlags(player, activeFlags);
|
||||||
|
|
||||||
if (activeMutes.Count > 0)
|
if (activeMutes.Count > 0)
|
||||||
{
|
{
|
||||||
foreach (var mute in activeMutes)
|
foreach (var mute in activeMutes)
|
||||||
@@ -276,8 +277,6 @@ public partial class CS2_SimpleAdmin
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
AddTimer(6, () => Helper.GivePlayerFlags(player, activeFlags));
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
||||||
if (_adminManager._adminCache != null && _adminManager._adminCache.Count > 0)
|
if (_adminManager._adminCache != null && _adminManager._adminCache.Count > 0)
|
||||||
@@ -347,12 +346,13 @@ public partial class CS2_SimpleAdmin
|
|||||||
if (loadedPlayers.Contains((int)player.Index))
|
if (loadedPlayers.Contains((int)player.Index))
|
||||||
loadedPlayers.Remove((int)player.Index);
|
loadedPlayers.Remove((int)player.Index);
|
||||||
|
|
||||||
if (player.AuthorizedSteamID != null)
|
if (player.AuthorizedSteamID != null && AdminSQLManager._adminCacheSet.Contains(player.AuthorizedSteamID))
|
||||||
{
|
{
|
||||||
//string steamIdString = player.AuthorizedSteamID.SteamId64.ToString();
|
if (AdminSQLManager._adminCacheTimestamps != null && AdminSQLManager._adminCacheTimestamps.TryGetValue(player.AuthorizedSteamID, out DateTime? expirationTime) && expirationTime.HasValue && expirationTime.Value <= DateTime.Now)
|
||||||
|
{
|
||||||
//AdminSQLManager._adminCache.TryRemove(steamIdString, out _);
|
AdminManager.ClearPlayerPermissions(player.AuthorizedSteamID);
|
||||||
AdminManager.RemovePlayerPermissions(player);
|
AdminManager.RemovePlayerAdminData(player.AuthorizedSteamID);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (TagsDetected)
|
if (TagsDetected)
|
||||||
@@ -361,13 +361,14 @@ public partial class CS2_SimpleAdmin
|
|||||||
|
|
||||||
private void OnMapStart(string mapName)
|
private void OnMapStart(string mapName)
|
||||||
{
|
{
|
||||||
|
AdminSQLManager _adminManager = new(dbConnectionString);
|
||||||
|
|
||||||
AddTimer(120.0f, () =>
|
AddTimer(120.0f, () =>
|
||||||
{
|
{
|
||||||
BanManager _banManager = new(dbConnectionString);
|
BanManager _banManager = new(dbConnectionString);
|
||||||
_ = _banManager.ExpireOldBans();
|
|
||||||
MuteManager _muteManager = new(dbConnectionString);
|
MuteManager _muteManager = new(dbConnectionString);
|
||||||
|
_ = _banManager.ExpireOldBans();
|
||||||
_ = _muteManager.ExpireOldMutes();
|
_ = _muteManager.ExpireOldMutes();
|
||||||
AdminSQLManager _adminManager = new(dbConnectionString);
|
|
||||||
_ = _adminManager.DeleteOldAdmins();
|
_ = _adminManager.DeleteOldAdmins();
|
||||||
}, CounterStrikeSharp.API.Modules.Timers.TimerFlags.REPEAT | CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE);
|
}, CounterStrikeSharp.API.Modules.Timers.TimerFlags.REPEAT | CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE);
|
||||||
|
|
||||||
@@ -394,6 +395,8 @@ public partial class CS2_SimpleAdmin
|
|||||||
|
|
||||||
connection.Close();
|
connection.Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_ = _adminManager.GiveAllFlags();
|
||||||
}
|
}
|
||||||
|
|
||||||
private HookResult OnPlayerHurt(EventPlayerHurt @event, GameEventInfo info)
|
private HookResult OnPlayerHurt(EventPlayerHurt @event, GameEventInfo info)
|
||||||
|
|||||||
48
Helper.cs
48
Helper.cs
@@ -1,6 +1,7 @@
|
|||||||
using CounterStrikeSharp.API;
|
using CounterStrikeSharp.API;
|
||||||
using CounterStrikeSharp.API.Core;
|
using CounterStrikeSharp.API.Core;
|
||||||
using CounterStrikeSharp.API.Modules.Admin;
|
using CounterStrikeSharp.API.Modules.Admin;
|
||||||
|
using CounterStrikeSharp.API.Modules.Entities;
|
||||||
using CounterStrikeSharp.API.Modules.Menu;
|
using CounterStrikeSharp.API.Modules.Menu;
|
||||||
using CounterStrikeSharp.API.Modules.Utils;
|
using CounterStrikeSharp.API.Modules.Utils;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
@@ -47,32 +48,49 @@ namespace CS2_SimpleAdmin
|
|||||||
return Regex.IsMatch(input, pattern);
|
return Regex.IsMatch(input, pattern);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void GivePlayerFlags(CCSPlayerController player, List<(List<string>, int)> flagsWithImmunity)
|
public static void GivePlayerFlags(SteamID? steamid, List<string>? flags = null, uint immunity = 0)
|
||||||
{
|
{
|
||||||
if (player == null) return;
|
try
|
||||||
|
|
||||||
foreach (var (flags, immunity) in flagsWithImmunity)
|
|
||||||
{
|
{
|
||||||
AdminManager.SetPlayerImmunity(player, (uint)immunity);
|
if (steamid == null || (flags == null && immunity == 0))
|
||||||
|
|
||||||
foreach (var flag in flags)
|
|
||||||
{
|
{
|
||||||
if (!string.IsNullOrEmpty(flag))
|
//Console.WriteLine("Invalid input: steamid is null or both flags and immunity are not provided.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Console.WriteLine($"Setting immunity for SteamID {steamid} to {immunity}");
|
||||||
|
AdminManager.SetPlayerImmunity(steamid, (uint)immunity);
|
||||||
|
|
||||||
|
if (flags != null)
|
||||||
|
{
|
||||||
|
//Console.WriteLine($"Applying flags to SteamID {steamid}:");
|
||||||
|
|
||||||
|
foreach (var flag in flags)
|
||||||
{
|
{
|
||||||
if (flag.StartsWith("@"))
|
if (!string.IsNullOrEmpty(flag))
|
||||||
{
|
{
|
||||||
AdminManager.AddPlayerPermissions(player, flag);
|
if (flag.StartsWith("@"))
|
||||||
}
|
{
|
||||||
else if (flag.StartsWith("#"))
|
//Console.WriteLine($"Adding permission {flag} to SteamID {steamid}");
|
||||||
{
|
AdminManager.AddPlayerPermissions(steamid, flag);
|
||||||
AdminManager.AddPlayerToGroup(player, flag);
|
}
|
||||||
|
else if (flag.StartsWith("#"))
|
||||||
|
{
|
||||||
|
//Console.WriteLine($"Adding SteamID {steamid} to group {flag}");
|
||||||
|
AdminManager.AddPlayerToGroup(steamid, flag);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
//Console.WriteLine($"An error occurred: {ex}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
public static TargetResult GetTarget(string target, out CCSPlayerController? player)
|
public static TargetResult GetTarget(string target, out CCSPlayerController? player)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user