feat: setting group id internally and migration

This commit is contained in:
Dollan 2024-04-11 21:49:43 +02:00
parent 0ade5a1371
commit d4340d69cf
No known key found for this signature in database
3 changed files with 49 additions and 14 deletions

View file

@ -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[]? GroupIds = [];
public static DiscordWebhookClient? _discordWebhookClientLog;
public static DiscordWebhookClient? _discordWebhookClientPenalty;

View file

@ -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;
@ -27,22 +27,26 @@ namespace CS2_SimpleAdmin
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_ids` 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();
var commandText3 = "CREATE TABLE IF NOT EXISTS `sa_server_groups` ( `id` int(11) NOT NULL AUTO_INCREMENT, `group_name` varchar(128) NOT NULL, PRIMARY KEY(`id`) ) ENGINE = InnoDB AUTO_INCREMENT = 2 DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci;`";
using var commandSql3 = connection.CreateCommand();
commandSql3.CommandText = commandText3;
await commandSql3.ExecuteNonQueryAsync();
Server.NextFrame(() =>
{
command.ReplyToCommand($"Successfully updated the database - {ModuleVersion}");

View file

@ -17,6 +17,27 @@ public partial class CS2_SimpleAdmin
{
public static HashSet<int> loadedPlayers = new HashSet<int>();
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<Listeners.OnMapStart>(OnMapStart);
@ -383,11 +404,19 @@ public partial class CS2_SimpleAdmin
new { address, hostname });
}
int? serverId = await connection.ExecuteScalarAsync<int>(
"SELECT `id` FROM `sa_servers` WHERE `address` = @address",
ServerData serverData = await connection.ExecuteScalarAsync<ServerData>(
"SELECT `id`, `group_ids` FROM `sa_servers` WHERE `address` = @address",
new { address });
ServerId = serverId;
ServerId = serverData.Id;
GroupIds = serverData.GroupIds;
// For testing only
_logger?.LogInformation($"Server ID: {ServerId}");
_logger?.LogInformation($"Group IDs: {GroupIds}");
}
catch (Exception ex)
{