diff --git a/Commands/basecommands.cs b/Commands/basecommands.cs index 95e18b4..f623a11 100644 --- a/Commands/basecommands.cs +++ b/Commands/basecommands.cs @@ -208,7 +208,97 @@ public void RemoveAdmin(CCSPlayerController? caller, string steamid, bool global Server.PrintToConsole(msg); } - [ConsoleCommand("css_reladmin")] + [ConsoleCommand("css_addgroup")] + [CommandHelper(minArgs: 3, usage: " ", whoCanExecute: CommandUsage.CLIENT_AND_SERVER)] + [RequiresPermissions("@css/root")] + public void OnAddGroup(CCSPlayerController? caller, CommandInfo command) + { + if (_database == null) return; + + if (!command.GetArg(1).StartsWith("#")) + { + command.ReplyToCommand($"Group name must start with #."); + return; + } + + if (!command.GetArg(2).StartsWith("@") && !command.GetArg(2).StartsWith("#")) + { + command.ReplyToCommand($"Invalid flag or group."); + return; + } + + string groupName = command.GetArg(1); + string flags = command.GetArg(2); + int immunity = 0; + int.TryParse(command.GetArg(3), out immunity); + + AddGroup(caller, groupName, flags, immunity, command); + } + + public static void AddGroup(CCSPlayerController? caller, string name, string flags, int immunity, CommandInfo? command = null) + { + if (_database == null) return; + AdminSQLManager _adminManager = new(_database); + + List flagsList = flags.Split(',').Select(flag => flag.Trim()).ToList(); + _ = _adminManager.AddGroup(name, flagsList, immunity); + + if (command != null) + Helper.SendDiscordLogMessage(caller, command, _discordWebhookClientLog, _localizer); + Helper.LogCommand(caller, $"css_addgroup {name} {flags} {immunity}"); + + string msg = $"Created group '{name}' with flags '{flags}'"; + if (command != null) + command.ReplyToCommand(msg); + else if (caller != null && caller.IsValid) + caller.PrintToChat(msg); + else + Server.PrintToConsole(msg); + } + + [ConsoleCommand("css_delgroup")] + [CommandHelper(minArgs: 1, usage: "", whoCanExecute: CommandUsage.CLIENT_AND_SERVER)] + [RequiresPermissions("@css/root")] + public void OnDelGroupCommand(CCSPlayerController? caller, CommandInfo command) + { + if (_database == null) return; + + if (!command.GetArg(1).StartsWith("#")) + { + command.ReplyToCommand($"Group name must start with #."); + return; + } + + string groupName = command.GetArg(1); + + RemoveGroup(caller, groupName, command); + } + + public void RemoveGroup(CCSPlayerController? caller, string name, CommandInfo? command = null) + { + if (_database == null) return; + AdminSQLManager _adminManager = new(_database); + _ = _adminManager.DeleteGroup(name); + + AddTimer(2, () => + { + ReloadAdmins(caller); + }, CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE); + + if (command != null) + Helper.SendDiscordLogMessage(caller, command, _discordWebhookClientLog, _localizer); + Helper.LogCommand(caller, $"css_delgroup {name}"); + + string msg = $"Removed group '{name}'"; + if (command != null) + command.ReplyToCommand(msg); + else if (caller != null && caller.IsValid) + caller.PrintToChat(msg); + else + Server.PrintToConsole(msg); + } + + [ConsoleCommand("css_reloadadmins")] [CommandHelper(whoCanExecute: CommandUsage.CLIENT_AND_SERVER)] [RequiresPermissions("@css/root")] public void OnRelAdminCommand(CCSPlayerController? caller, CommandInfo command) @@ -217,7 +307,7 @@ public void OnRelAdminCommand(CCSPlayerController? caller, CommandInfo command) ReloadAdmins(caller); - command.ReplyToCommand("Reloaded sql admins"); + command.ReplyToCommand("Reloaded sql admins and groups"); } public void ReloadAdmins(CCSPlayerController? caller) @@ -234,6 +324,7 @@ public void ReloadAdmins(CCSPlayerController? caller) } AdminSQLManager _adminManager = new(_database); + _ = _adminManager.GiveAllGroupsFlags(); _ = _adminManager.GiveAllFlags(); } diff --git a/Config.cs b/Config.cs index 595e8cf..4654561 100644 --- a/Config.cs +++ b/Config.cs @@ -22,7 +22,7 @@ public class CustomServerCommandData [JsonPropertyName("Command")] public string Command { get; set; } = ""; - + [JsonPropertyName("ExecuteOnClient")] public bool ExecuteOnClient { get; set; } = false; } @@ -60,6 +60,8 @@ public class CS2_SimpleAdminConfig : BasePluginConfig [JsonPropertyName("BanType")] public int BanType { get; set; } = 1; + [JsonPropertyName("MultiServerMode")] + public bool MultiServerMode { get; set; } = true; [JsonPropertyName("ExpireOldIpBans")] public int ExpireOldIpBans { get; set; } = 0; diff --git a/Database/Migrations/001_CreateTables.sql b/Database/Migrations/001_CreateTables.sql index 26c50b7..2595408 100644 --- a/Database/Migrations/001_CreateTables.sql +++ b/Database/Migrations/001_CreateTables.sql @@ -27,19 +27,19 @@ CREATE TABLE IF NOT EXISTS `sa_mutes` ( `server_id` INT NULL, `status` enum('ACTIVE','UNMUTED','EXPIRED','') NOT NULL DEFAULT 'ACTIVE', PRIMARY KEY (`id`) - ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; CREATE TABLE IF NOT EXISTS `sa_admins` ( `id` int(11) NOT NULL AUTO_INCREMENT, `player_name` varchar(128) NOT NULL, `player_steamid` varchar(64) NOT NULL, `flags` TEXT NOT NULL, - `immunity` varchar(64) NOT NULL DEFAULT '0', + `immunity` int(11) NOT NULL DEFAULT 0, `server_id` INT NULL, `ends` timestamp NULL, `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`) - ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; CREATE TABLE IF NOT EXISTS `sa_servers` ( `id` int(11) NOT NULL AUTO_INCREMENT, @@ -47,4 +47,4 @@ CREATE TABLE IF NOT EXISTS `sa_servers` ( `address` varchar(64) NOT 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 + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; \ No newline at end of file diff --git a/Database/Migrations/006_ServerGroupsFeature.sql b/Database/Migrations/006_ServerGroupsFeature.sql new file mode 100644 index 0000000..4424189 --- /dev/null +++ b/Database/Migrations/006_ServerGroupsFeature.sql @@ -0,0 +1,26 @@ +CREATE TABLE IF NOT EXISTS `sa_groups` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `name` varchar(255) NOT NULL, + `immunity` int(11) NOT NULL DEFAULT 0, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; + +CREATE TABLE IF NOT EXISTS `sa_groups_flags` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `group_id` int(11) NOT NULL, + `flag` varchar(64) NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; + +CREATE TABLE IF NOT EXISTS `sa_groups_servers` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `group_id` int(11) NOT NULL, + `server_id` int(11) NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; + +ALTER TABLE `sa_admins` ADD `group_id` INT NULL AFTER `created`; + +ALTER TABLE `sa_groups_flags` ADD FOREIGN KEY (`group_id`) REFERENCES `sa_groups`(`id`) ON DELETE CASCADE; +ALTER TABLE `sa_groups_servers` ADD FOREIGN KEY (`group_id`) REFERENCES `sa_groups`(`id`) ON DELETE CASCADE; +ALTER TABLE `sa_admins` ADD FOREIGN KEY (`group_id`) REFERENCES `sa_groups`(`id`) ON DELETE SET NULL; \ No newline at end of file diff --git a/Events.cs b/Events.cs index e100d6e..d55efd9 100644 --- a/Events.cs +++ b/Events.cs @@ -422,6 +422,7 @@ await connection.ExecuteAsync( } } + await _adminManager.GiveAllGroupsFlags(); await _adminManager.GiveAllFlags(); }); }, CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE); diff --git a/Managers/AdminSQLManager.cs b/Managers/AdminSQLManager.cs index b26ee30..6400b05 100644 --- a/Managers/AdminSQLManager.cs +++ b/Managers/AdminSQLManager.cs @@ -136,6 +136,101 @@ flagObj is not string flag || } } + public async Task, List>, int>>> GetAllGroupsFlags() + { + try + { + await using MySqlConnection connection = await _database.GetConnectionAsync(); + + string sql = "SELECT group_id FROM sa_groups_servers WHERE server_id = @serverid"; + var groupIds = connection.Query(sql, new { serverid = CS2_SimpleAdmin.ServerId }).ToList(); + + sql = @" + SELECT g.group_id, f.flag + FROM sa_groups_flags f + JOIN sa_groups_servers g ON f.group_id = g.group_id + WHERE g.server_id = @serverid"; + + var groupFlagData = connection.Query(sql, new { serverid = CS2_SimpleAdmin.ServerId }).ToList(); + + if (groupIds.Count == 0 || groupFlagData.Count == 0) + { + return []; + } + + var groupInfoDictionary = new Dictionary, List>, int>>(); + + foreach (var groupId in groupIds) + { + groupInfoDictionary[groupId] = new Tuple, List>, int>([], [], 0); + } + + foreach (var row in groupFlagData) + { + var groupId = (int)row.group_id; + var flag = (string)row.flag; + + groupInfoDictionary[groupId].Item1.Add(flag); + } + + sql = @" + SELECT a.group_id, a.player_steamid, a.ends, g.immunity + FROM sa_admins a + JOIN sa_groups g ON a.group_id = g.id + WHERE a.group_id IN @groupIds"; + + var playerData = (await connection.QueryAsync(sql, new { groupIds })).ToList(); + + foreach (var row in playerData) + { + var groupId = (int)row.group_id; + var playerSteamid = (string)row.player_steamid; + var ends = row.ends as DateTime?; + var immunity = (int)row.immunity; + + groupInfoDictionary[groupId].Item2.Add(new Tuple(playerSteamid, ends)); + groupInfoDictionary[groupId] = new Tuple, List>, int>(groupInfoDictionary[groupId].Item1, groupInfoDictionary[groupId].Item2, immunity); + } + + return groupInfoDictionary; + } + catch { } + + return []; + } + + + + public async Task GiveAllGroupsFlags() + { + Dictionary, List>, int>> groupFlags = await GetAllGroupsFlags(); + + foreach (var kvp in groupFlags) + { + var flags = kvp.Value.Item1; + var players = kvp.Value.Item2; + int immunity = kvp.Value.Item3; + + foreach (var playerTuple in players) + { + var steamIdStr = playerTuple.Item1; + var ends = playerTuple.Item2; + + if (!string.IsNullOrEmpty(steamIdStr) && SteamID.TryParse(steamIdStr, out var steamId) && steamId != null) + { + if (!_adminCache.ContainsKey(steamId)) + { + _adminCache.TryAdd(steamId, ends); + } + + Helper.GivePlayerFlags(steamId, flags, (uint)immunity); + // Often need to call 2 times + Helper.GivePlayerFlags(steamId, flags, (uint)immunity); + } + } + } + } + public async Task GiveAllFlags() { List<(string, List, int, DateTime?)> allPlayers = await GetAllPlayersFlags(); @@ -221,7 +316,22 @@ public async Task AddAdminBySteamId(string playerSteamId, string playerName, Lis // Insert flags into sa_admins_flags table foreach (var flag in flagsList) { - Console.WriteLine(flag); + if (flag.StartsWith("#")) + { + string sql = "SELECT id FROM `sa_groups` WHERE name = @groupName"; + int? groupId = await connection.QuerySingleOrDefaultAsync(sql, new { groupName = flag }); + + if (groupId != null) + { + var updateAdminGroup = "UPDATE `sa_admins` SET group_id = @groupId WHERE id = @adminId"; + await connection.ExecuteAsync(updateAdminGroup, new + { + groupId, + adminId + }); + } + } + var insertFlagsSql = "INSERT INTO `sa_admins_flags` (`admin_id`, `flag`) " + "VALUES (@adminId, @flag)"; @@ -238,6 +348,66 @@ public async Task AddAdminBySteamId(string playerSteamId, string playerName, Lis } } + public async Task AddGroup(string groupName, List flagsList, int immunity = 0) + { + if (string.IsNullOrEmpty(groupName) || flagsList == null || flagsList.Count == 0) return; + + try + { + await using MySqlConnection connection = await _database.GetConnectionAsync(); + + // Insert group into sa_groups table + var insertGroup = "INSERT INTO `sa_groups` (`name`, `immunity`) " + + "VALUES (@groupName, @immunity); SELECT LAST_INSERT_ID();"; + int groupId = await connection.ExecuteScalarAsync(insertGroup, new + { + groupName, + immunity + }); + + // Insert flags into sa_groups_flags table + foreach (var flag in flagsList) + { + var insertFlagsSql = "INSERT INTO `sa_groups_flags` (`group_id`, `flag`) " + + "VALUES (@groupId, @flag)"; + + await connection.ExecuteAsync(insertFlagsSql, new + { + groupId, + flag + }); + } + + if (CS2_SimpleAdmin.ServerId != null) + { + string insertGroupServer = "INSERT INTO `sa_groups_servers` (`group_id`, `server_id`) " + + "VALUES (@groupId, @server_id)"; + await connection.ExecuteAsync(insertGroupServer, new { groupId, server_id = CS2_SimpleAdmin.ServerId }); + } + } + catch (Exception ex) + { + Console.WriteLine(ex.ToString()); + } + } + + public async Task DeleteGroup(string groupName) + { + if (string.IsNullOrEmpty(groupName)) return; + + try + { + await using MySqlConnection connection = await _database.GetConnectionAsync(); + + string sql = "DELETE FROM `sa_groups` WHERE name = @groupName"; + await connection.ExecuteAsync(sql, new { groupName }); + } + catch (Exception ex) + { + Console.WriteLine(ex.ToString()); + } + } + public async Task DeleteOldAdmins() { try diff --git a/Managers/BanManager.cs b/Managers/BanManager.cs index 723c393..3e76346 100644 --- a/Managers/BanManager.cs +++ b/Managers/BanManager.cs @@ -115,7 +115,11 @@ public async Task IsPlayerBanned(PlayerInfo player) try { - string sql = @" + string sql = ""; + + if (_config.MultiServerMode) + { + sql = @" UPDATE sa_bans SET player_ip = CASE WHEN player_ip IS NULL THEN @PlayerIP ELSE player_ip END, player_name = CASE WHEN player_name IS NULL THEN @PlayerName ELSE player_name END @@ -127,6 +131,22 @@ SELECT COUNT(*) FROM sa_bans WHERE (player_steamid = @PlayerSteamID OR player_ip = @PlayerIP) AND status = 'ACTIVE' AND (duration = 0 OR ends > @CurrentTime);"; + } + else + { + sql = @" + UPDATE sa_bans + SET player_ip = CASE WHEN player_ip IS NULL THEN @PlayerIP ELSE player_ip END, + player_name = CASE WHEN player_name IS NULL THEN @PlayerName ELSE player_name END + WHERE (player_steamid = @PlayerSteamID OR player_ip = @PlayerIP) + AND status = 'ACTIVE' + AND (duration = 0 OR ends > @CurrentTime) AND server_id = @serverid; + + 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;"; + } await using MySqlConnection connection = await _database.GetConnectionAsync(); @@ -135,7 +155,8 @@ SELECT COUNT(*) FROM sa_bans PlayerSteamID = player.SteamId, PlayerIP = _config.BanType == 0 || string.IsNullOrEmpty(player.IpAddress) ? null : player.IpAddress, PlayerName = !string.IsNullOrEmpty(player.Name) ? player.Name : string.Empty, - CurrentTime = currentTime + CurrentTime = currentTime, + serverid = CS2_SimpleAdmin.ServerId }; banCount = await connection.ExecuteScalarAsync(sql, parameters); @@ -152,18 +173,28 @@ public async Task GetPlayerBans(PlayerInfo player) { try { - string sql = "SELECT COUNT(*) FROM sa_bans WHERE (player_steamid = @PlayerSteamID OR player_ip = @PlayerIP)"; + string sql = ""; + + if (_config.MultiServerMode) + { + sql = "SELECT COUNT(*) FROM sa_bans WHERE (player_steamid = @PlayerSteamID OR player_ip = @PlayerIP) AND server_id = @serverid"; + } + else + { + sql = "SELECT COUNT(*) FROM sa_bans WHERE (player_steamid = @PlayerSteamID OR player_ip = @PlayerIP)"; + } + int banCount; await using MySqlConnection connection = await _database.GetConnectionAsync(); if (!string.IsNullOrEmpty(player.IpAddress)) { - banCount = await connection.ExecuteScalarAsync(sql, new { PlayerSteamID = player.SteamId, PlayerIP = player.IpAddress }); + banCount = await connection.ExecuteScalarAsync(sql, new { PlayerSteamID = player.SteamId, PlayerIP = player.IpAddress, serverid = CS2_SimpleAdmin.ServerId }); } else { - banCount = await connection.ExecuteScalarAsync(sql, new { PlayerSteamID = player.SteamId, PlayerIP = DBNull.Value }); + banCount = await connection.ExecuteScalarAsync(sql, new { PlayerSteamID = player.SteamId, PlayerIP = DBNull.Value, serverid = CS2_SimpleAdmin.ServerId }); } return banCount; @@ -183,8 +214,17 @@ public async Task UnbanPlayer(string playerPattern, string adminSteamId, string { await using MySqlConnection connection = await _database.GetConnectionAsync(); - string sqlRetrieveBans = "SELECT id FROM sa_bans WHERE (player_steamid = @pattern OR player_name = @pattern OR player_ip = @pattern) AND status = 'ACTIVE'"; - var bans = await connection.QueryAsync(sqlRetrieveBans, new { pattern = playerPattern }); + string sqlRetrieveBans = ""; + if (_config.MultiServerMode) + { + sqlRetrieveBans = "SELECT id FROM sa_bans WHERE (player_steamid = @pattern OR player_name = @pattern OR player_ip = @pattern) AND status = 'ACTIVE' " + + "AND server_id = @serverid"; + } + else + { + sqlRetrieveBans = "SELECT id FROM sa_bans WHERE (player_steamid = @pattern OR player_name = @pattern OR player_ip = @pattern) AND status = 'ACTIVE'"; + } + var bans = await connection.QueryAsync(sqlRetrieveBans, new { pattern = playerPattern, serverid = CS2_SimpleAdmin.ServerId }); if (!bans.Any()) return; @@ -230,7 +270,17 @@ public async Task CheckOnlinePlayers(List<(string? IpAddress, ulong SteamID, int try { await using MySqlConnection connection = await _database.GetConnectionAsync(); - string sql = "SELECT COUNT(*) FROM sa_bans WHERE (player_steamid = @PlayerSteamID OR player_ip = @PlayerIP) AND status = 'ACTIVE'"; + string sql = ""; + + if (_config.MultiServerMode) + { + sql = "SELECT COUNT(*) FROM sa_bans WHERE (player_steamid = @PlayerSteamID OR player_ip = @PlayerIP) AND status = 'ACTIVE' AND" + + " server_id = @serverid"; + } + else + { + sql = "SELECT COUNT(*) FROM sa_bans WHERE (player_steamid = @PlayerSteamID OR player_ip = @PlayerIP) AND status = 'ACTIVE'"; + } foreach (var (IpAddress, SteamID, UserId) in players) { @@ -239,11 +289,11 @@ public async Task CheckOnlinePlayers(List<(string? IpAddress, ulong SteamID, int int banCount = 0; if (!string.IsNullOrEmpty(IpAddress)) { - banCount = await connection.ExecuteScalarAsync(sql, new { PlayerSteamID = SteamID, PlayerIP = IpAddress }); + banCount = await connection.ExecuteScalarAsync(sql, new { PlayerSteamID = SteamID, PlayerIP = IpAddress, serverid = CS2_SimpleAdmin.ServerId }); } else { - banCount = await connection.ExecuteScalarAsync(sql, new { PlayerSteamID = SteamID, PlayerIP = DBNull.Value }); + banCount = await connection.ExecuteScalarAsync(sql, new { PlayerSteamID = SteamID, PlayerIP = DBNull.Value, serverid = CS2_SimpleAdmin.ServerId }); } if (banCount > 0) @@ -274,7 +324,25 @@ public async Task ExpireOldBans() await connection.ExecuteAsync(sql, new { CurrentTime = DateTime.UtcNow.ToLocalTime() }); */ - string sql = @" + string sql = ""; + + if (_config.MultiServerMode) + { + sql = @" + UPDATE sa_bans + SET + status = 'EXPIRED' + WHERE + status = 'ACTIVE' + AND + `duration` > 0 + AND + ends <= @currentTime + AND server_id = @serverid"; + } + else + { + sql = @" UPDATE sa_bans SET status = 'EXPIRED' @@ -284,14 +352,28 @@ UPDATE sa_bans `duration` > 0 AND ends <= @currentTime"; + } - await connection.ExecuteAsync(sql, new { currentTime }); + await connection.ExecuteAsync(sql, new { currentTime, serverid = CS2_SimpleAdmin.ServerId }); if (_config.ExpireOldIpBans > 0) { DateTime ipBansTime = currentTime.AddDays(-_config.ExpireOldIpBans).ToLocalTime(); - - sql = @" + if (_config.MultiServerMode) + { + sql = @" + UPDATE sa_bans + SET + player_ip = NULL + WHERE + status = 'ACTIVE' + AND + ends <= @ipBansTime + AND server_id = @serverid"; + } + else + { + sql = @" UPDATE sa_bans SET player_ip = NULL @@ -299,8 +381,9 @@ UPDATE sa_bans status = 'ACTIVE' AND ends <= @ipBansTime"; + } - await connection.ExecuteAsync(sql, new { ipBansTime }); + await connection.ExecuteAsync(sql, new { ipBansTime, CS2_SimpleAdmin.ServerId }); } } catch (Exception) diff --git a/Managers/MuteManager.cs b/Managers/MuteManager.cs index d9d550d..eb698f0 100644 --- a/Managers/MuteManager.cs +++ b/Managers/MuteManager.cs @@ -97,9 +97,19 @@ public async Task> IsPlayerMuted(string steamId) { await using MySqlConnection connection = await _database.GetConnectionAsync(); DateTime currentTime = DateTime.Now.ToLocalTime(); - string sql = "SELECT * FROM sa_mutes WHERE player_steamid = @PlayerSteamID AND status = 'ACTIVE' AND (duration = 0 OR ends > @CurrentTime)"; + string sql = ""; - var parameters = new { PlayerSteamID = steamId, CurrentTime = currentTime }; + if (CS2_SimpleAdmin.Instance.Config.MultiServerMode) + { + sql = "SELECT * FROM sa_mutes WHERE player_steamid = @PlayerSteamID AND status = 'ACTIVE' AND (duration = 0 OR ends > @CurrentTime) " + + "AND server_id = @serverid"; + } + else + { + sql = "SELECT * FROM sa_mutes WHERE player_steamid = @PlayerSteamID AND status = 'ACTIVE' AND (duration = 0 OR ends > @CurrentTime)"; + } + + var parameters = new { PlayerSteamID = steamId, CurrentTime = currentTime, serverid = CS2_SimpleAdmin.ServerId }; var activeMutes = (await connection.QueryAsync(sql, parameters)).ToList(); return activeMutes; } @@ -116,9 +126,18 @@ public async Task GetPlayerMutes(string steamId) await using MySqlConnection connection = await _database.GetConnectionAsync(); int muteCount; - string sql = "SELECT COUNT(*) FROM sa_mutes WHERE player_steamid = @PlayerSteamID"; + string sql = ""; - muteCount = await connection.ExecuteScalarAsync(sql, new { PlayerSteamID = steamId }); + if (CS2_SimpleAdmin.Instance.Config.MultiServerMode) + { + sql = "SELECT COUNT(*) FROM sa_mutes WHERE player_steamid = @PlayerSteamID AND server_id = @serverid"; + } + else + { + sql = "SELECT COUNT(*) FROM sa_mutes WHERE player_steamid = @PlayerSteamID"; + } + + muteCount = await connection.ExecuteScalarAsync(sql, new { PlayerSteamID = steamId, serverid = CS2_SimpleAdmin.ServerId }); return muteCount; } catch (Exception) @@ -146,8 +165,20 @@ public async Task UnmutePlayer(string playerPattern, string adminSteamId, string else if (type == 2) muteType = "SILENCE"; - string sqlRetrieveMutes = "SELECT id FROM sa_mutes WHERE (player_steamid = @pattern OR player_name = @pattern) AND type = @muteType AND status = 'ACTIVE'"; - var mutes = await connection.QueryAsync(sqlRetrieveMutes, new { pattern = playerPattern, muteType }); + string sqlRetrieveMutes = ""; + + if (CS2_SimpleAdmin.Instance.Config.MultiServerMode) + { + sqlRetrieveMutes = "SELECT id FROM sa_mutes WHERE (player_steamid = @pattern OR player_name = @pattern) AND " + + "type = @muteType AND status = 'ACTIVE' AND server_id = @serverid"; + } + else + { + sqlRetrieveMutes = "SELECT id FROM sa_mutes WHERE (player_steamid = @pattern OR player_name = @pattern) AND " + + "type = @muteType AND status = 'ACTIVE'"; + } + + var mutes = await connection.QueryAsync(sqlRetrieveMutes, new { pattern = playerPattern, muteType, serverid = CS2_SimpleAdmin.ServerId }); if (!mutes.Any()) return; @@ -191,8 +222,17 @@ public async Task ExpireOldMutes() { await using MySqlConnection connection = await _database.GetConnectionAsync(); - string sql = "UPDATE sa_mutes SET status = 'EXPIRED' WHERE status = 'ACTIVE' AND `duration` > 0 AND ends <= @CurrentTime"; - await connection.ExecuteAsync(sql, new { CurrentTime = DateTime.Now.ToLocalTime() }); + string sql = ""; + if (CS2_SimpleAdmin.Instance.Config.MultiServerMode) + { + sql = "UPDATE sa_mutes SET status = 'EXPIRED' WHERE status = 'ACTIVE' AND `duration` > 0 AND ends <= @CurrentTime AND server_id = @serverid"; + } + else + { + sql = "UPDATE sa_mutes SET status = 'EXPIRED' WHERE status = 'ACTIVE' AND `duration` > 0 AND ends <= @CurrentTime"; + } + + await connection.ExecuteAsync(sql, new { CurrentTime = DateTime.Now.ToLocalTime(), serverid = CS2_SimpleAdmin.ServerId }); } catch (Exception) { diff --git a/Menus/AdminMenu.cs b/Menus/AdminMenu.cs index 4384a53..fd7aa1f 100644 --- a/Menus/AdminMenu.cs +++ b/Menus/AdminMenu.cs @@ -35,15 +35,13 @@ public static void OpenMenu(CCSPlayerController admin) return; } - //bool xpRights = AdminManager.PlayerHasPermissions(admin, "@wcs/xp"); - - BaseMenu menu = AdminMenu.CreateMenu("Simple Admin"); - List options = new() - { + BaseMenu menu = CreateMenu("Simple Admin"); + List options = + [ new ChatMenuOptionData("Manage Players", () => ManagePlayersMenu.OpenMenu(admin)), new ChatMenuOptionData("Manage Server", () => ManageServerMenu.OpenMenu(admin)), new ChatMenuOptionData("Fun actions", () => FunActionsMenu.OpenMenu(admin)), - }; + ]; List customCommands = CS2_SimpleAdmin.Instance.Config.CustomServerCommands; if (customCommands.Count > 0)