From d4340d69cf0ddc5914375870e62d54027bcacfcb Mon Sep 17 00:00:00 2001 From: Dollan Date: Thu, 11 Apr 2024 21:49:43 +0200 Subject: [PATCH] feat: setting group id internally and migration --- CS2-SimpleAdmin.cs | 2 ++ Commands/basecommands.cs | 20 ++++++++++++-------- Events.cs | 41 ++++++++++++++++++++++++++++++++++------ 3 files changed, 49 insertions(+), 14 deletions(-) diff --git a/CS2-SimpleAdmin.cs b/CS2-SimpleAdmin.cs index 402517d..e7fbd9e 100644 --- a/CS2-SimpleAdmin.cs +++ b/CS2-SimpleAdmin.cs @@ -26,6 +26,8 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig { command.ReplyToCommand($"Successfully updated the database - {ModuleVersion}"); diff --git a/Events.cs b/Events.cs index 7c5ea63..e0906e0 100644 --- a/Events.cs +++ b/Events.cs @@ -17,6 +17,27 @@ public partial class CS2_SimpleAdmin { public static HashSet loadedPlayers = new HashSet(); + struct ServerData + { + public int? Id { get; } + public int[]? GroupIds { get; } + + public ServerData(int? id, string? groupIds) + { + Id = id; + GroupIds = ParseGroupIds(groupIds); + } + + private int[]? ParseGroupIds(string? groupIds) + { + if (string.IsNullOrWhiteSpace(groupIds)) + return null; + + return groupIds.Split(',') + .Select(id => int.TryParse(id, out int result) ? result : 0) + .ToArray(); + } + } private void RegisterEvents() { RegisterListener(OnMapStart); @@ -32,7 +53,7 @@ public partial class CS2_SimpleAdmin 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 +67,7 @@ public partial class CS2_SimpleAdmin } #if DEBUG - Logger.LogCritical("[OnClientDisconnect] After Check"); + Logger.LogCritical("[OnClientDisconnect] After Check"); #endif try { @@ -383,11 +404,19 @@ public partial class CS2_SimpleAdmin new { address, hostname }); } - int? serverId = await connection.ExecuteScalarAsync( - "SELECT `id` FROM `sa_servers` WHERE `address` = @address", - new { address }); - ServerId = serverId; + + ServerData serverData = await connection.ExecuteScalarAsync( + "SELECT `id`, `group_ids` FROM `sa_servers` WHERE `address` = @address", + new { address }); + + ServerId = serverData.Id; + GroupIds = serverData.GroupIds; + + // For testing only + _logger?.LogInformation($"Server ID: {ServerId}"); + _logger?.LogInformation($"Group IDs: {GroupIds}"); + } catch (Exception ex) {