Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server groups #99

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CS2-SimpleAdmin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig<CS2_SimpleAdmin
public static bool voteInProgress = false;
public static int? ServerId = null;

public static int[]? GroupId = [];

public static DiscordWebhookClient? _discordWebhookClientLog;
public static DiscordWebhookClient? _discordWebhookClientPenalty;

Expand Down
12 changes: 4 additions & 8 deletions Commands/basecommands.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Core.Translations;
Expand Down Expand Up @@ -27,20 +27,16 @@ public void OnSaUpgradeCommand(CCSPlayerController? caller, CommandInfo command)
try
{
using var connection = await _database.GetConnectionAsync();
var commandText = "ALTER TABLE `sa_mutes` CHANGE `type` `type` ENUM('GAG','MUTE', 'SILENCE', '') CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT 'GAG';";
var commandText = "ALTER TABLE `sa_servers` ADD COLUMN `group_id` VARCHAR(255) NULL AFTER `hostname`";

using var commandSql = connection.CreateCommand();
commandSql.CommandText = commandText;
await commandSql.ExecuteNonQueryAsync();

commandText = "ALTER TABLE `sa_servers` MODIFY COLUMN `hostname` varchar(128);";
using var commandSql1 = connection.CreateCommand();
commandSql1.CommandText = commandText;
await commandSql1.ExecuteNonQueryAsync();
var commandText2 = "ALTER TABLE `sa_admins` ADD COLUMN `group_id` INT NULL AFTER `server_id`";

commandText = "ALTER TABLE `sa_bans` MODIFY `ends` TIMESTAMP NULL DEFAULT NULL;";
using var commandSql2 = connection.CreateCommand();
commandSql2.CommandText = commandText;
commandSql2.CommandText = commandText2;
await commandSql2.ExecuteNonQueryAsync();

Server.NextFrame(() =>
Expand Down
2 changes: 2 additions & 0 deletions Database/database_setup.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand All @@ -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;
21 changes: 16 additions & 5 deletions Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -46,7 +46,7 @@ public HookResult OnClientDisconnect(EventPlayerDisconnect @event, GameEventInfo
}

#if DEBUG
Logger.LogCritical("[OnClientDisconnect] After Check");
Logger.LogCritical("[OnClientDisconnect] After Check");
#endif
try
{
Expand Down Expand Up @@ -383,15 +383,26 @@ await connection.ExecuteAsync(
new { address, hostname });
}

int? serverId = await connection.ExecuteScalarAsync<int>(
"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)
Expand Down
14 changes: 9 additions & 5 deletions Managers/AdminSQLManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CounterStrikeSharp.API.Modules.Entities;
using CounterStrikeSharp.API.Modules.Utils;
using Dapper;
using Microsoft.Extensions.Logging;
using MySqlConnector;
Expand Down Expand Up @@ -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<dynamic>? 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<dynamic>? activeFlags = (await connection.QueryAsync(sql, new { PlayerSteamID = steamId, CurrentTime = now, serverid = CS2_SimpleAdmin.ServerId, groupid = groupId }))?.ToList();

if (activeFlags == null)
{
Expand Down Expand Up @@ -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<dynamic>? 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<dynamic>? activeFlags = (await connection.QueryAsync(sql, new { CurrentTime = now, serverid = CS2_SimpleAdmin.ServerId, groupid = groupId }))?.ToList();

if (activeFlags == null)
{
Expand Down Expand Up @@ -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));
}

Expand Down
Loading