diff --git a/CS2-SimpleAdmin.cs b/CS2-SimpleAdmin.cs index 402517d..9882760 100644 --- a/CS2-SimpleAdmin.cs +++ b/CS2-SimpleAdmin.cs @@ -26,6 +26,8 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig diff --git a/Database/database_setup.sql b/Database/database_setup.sql index 8bda5f7..dbcddb1 100644 --- a/Database/database_setup.sql +++ b/Database/database_setup.sql @@ -36,6 +36,7 @@ CREATE TABLE IF NOT EXISTS `sa_admins` ( `flags` TEXT NOT NULL, `immunity` varchar(64) NOT NULL DEFAULT '0', `server_id` INT NULL, + `group_id` INT NULL, `ends` timestamp NULL, `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`) @@ -45,6 +46,7 @@ CREATE TABLE IF NOT EXISTS `sa_servers` ( `id` int(11) NOT NULL AUTO_INCREMENT, `address` varchar(64) NOT NULL, `hostname` varchar(128) NOT NULL, + `group_id` VARCHAR(255) NULL, PRIMARY KEY (`id`), UNIQUE KEY `address` (`address`) ) ENGINE=InnoDB AUTO_INCREMENT=36 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; \ No newline at end of file diff --git a/Events.cs b/Events.cs index 7c5ea63..6eebb61 100644 --- a/Events.cs +++ b/Events.cs @@ -32,7 +32,7 @@ public HookResult OnClientDisconnect(EventPlayerDisconnect @event, GameEventInfo CCSPlayerController? player = @event.Userid; #if DEBUG - Logger.LogCritical("[OnClientDisconnect] Before"); + Logger.LogCritical("[OnClientDisconnect] Before"); #endif if (player == null || !player.IsValid || string.IsNullOrEmpty(player.IpAddress) || player.IsBot || player.IsHLTV) @@ -46,7 +46,7 @@ public HookResult OnClientDisconnect(EventPlayerDisconnect @event, GameEventInfo } #if DEBUG - Logger.LogCritical("[OnClientDisconnect] After Check"); + Logger.LogCritical("[OnClientDisconnect] After Check"); #endif try { @@ -383,15 +383,26 @@ await connection.ExecuteAsync( new { address, hostname }); } - int? serverId = await connection.ExecuteScalarAsync( - "SELECT `id` FROM `sa_servers` WHERE `address` = @address", + (int? serverId, string? groupId) = await connection.QueryFirstAsync<(int?, string?)>( + "SELECT `id`, `group_id` FROM `sa_servers` WHERE `address` = @address", new { address }); ServerId = serverId; + if (groupId != null && groupId.Length > 0) + { + // Split the group ids by comma and parse them to integers + GroupId = groupId?.Split(',').Select(int.Parse).ToArray(); + } + else + { + // We must set it to null if it's empty to ensure reloads work properly + GroupId = null; + } + } catch (Exception ex) { - _logger?.LogCritical("Unable to create or get server_id" + ex.Message); + _logger?.LogCritical("Unable to create or get server_id " + ex.Message); } if (Config.EnableMetrics) diff --git a/Managers/AdminSQLManager.cs b/Managers/AdminSQLManager.cs index 65e873a..e965ca0 100644 --- a/Managers/AdminSQLManager.cs +++ b/Managers/AdminSQLManager.cs @@ -1,4 +1,5 @@ using CounterStrikeSharp.API.Modules.Entities; +using CounterStrikeSharp.API.Modules.Utils; using Dapper; using Microsoft.Extensions.Logging; using MySqlConnector; @@ -27,8 +28,10 @@ public AdminSQLManager(Database database) await using MySqlConnection connection = await _database.GetConnectionAsync(); - string sql = "SELECT flags, immunity, ends FROM sa_admins WHERE player_steamid = @PlayerSteamID AND (ends IS NULL OR ends > @CurrentTime) AND (server_id IS NULL OR server_id = @serverid)"; - List? activeFlags = (await connection.QueryAsync(sql, new { PlayerSteamID = steamId, CurrentTime = now, serverid = CS2_SimpleAdmin.ServerId }))?.ToList(); + string groupId = CS2_SimpleAdmin.GroupId != null ? string.Join("", CS2_SimpleAdmin.GroupId) : string.Empty; + + string sql = "SELECT flags, immunity, ends FROM sa_admins WHERE player_steamid = @PlayerSteamID AND (ends IS NULL OR ends > @CurrentTime) AND ((server_id IS NULL OR server_id = @serverid) OR ((@groupid IS NOT NULL AND @groupid <> 0) AND group_id LIKE CONCAT('%', @groupid, '%')));"; + List? activeFlags = (await connection.QueryAsync(sql, new { PlayerSteamID = steamId, CurrentTime = now, serverid = CS2_SimpleAdmin.ServerId, groupid = groupId }))?.ToList(); if (activeFlags == null) { @@ -70,8 +73,10 @@ public AdminSQLManager(Database database) { await using MySqlConnection connection = await _database.GetConnectionAsync(); - 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? activeFlags = (await connection.QueryAsync(sql, new { CurrentTime = now, serverid = CS2_SimpleAdmin.ServerId }))?.ToList(); + string groupId = CS2_SimpleAdmin.GroupId != null ? string.Join("", CS2_SimpleAdmin.GroupId) : string.Empty; + + 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) OR ((@groupid IS NOT NULL AND @groupid <> 0) AND group_id LIKE CONCAT('%', @groupid, '%')));"; + List? activeFlags = (await connection.QueryAsync(sql, new { CurrentTime = now, serverid = CS2_SimpleAdmin.ServerId, groupid = groupId }))?.ToList(); if (activeFlags == null) { @@ -116,7 +121,6 @@ public AdminSQLManager(Database database) //Console.WriteLine("Failed to parse one or more values."); continue; } - filteredFlagsWithImmunity.Add((steamId, flagsValue.Split(',').ToList(), immunityValue, ends)); }