From 8e4724fb3e60b25c80b3f5461922ba20c48176ff Mon Sep 17 00:00:00 2001 From: Dawid Bepierszcz <41084667+daffyyyy@users.noreply.github.com> Date: Tue, 18 Jun 2024 03:12:00 +0200 Subject: [PATCH] 1.4.6b - Fix for alternative get server ip method --- CS2-SimpleAdmin.cs | 2 +- Events.cs | 83 +++++++++++++++++++++++++++++++++++++++++++++- VERSION | 2 +- 3 files changed, 84 insertions(+), 3 deletions(-) diff --git a/CS2-SimpleAdmin.cs b/CS2-SimpleAdmin.cs index 20f7999..de87c99 100644 --- a/CS2-SimpleAdmin.cs +++ b/CS2-SimpleAdmin.cs @@ -37,7 +37,7 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig "CS2-SimpleAdmin" + (Helper.IsDebugBuild ? " (DEBUG)" : " (RELEASE)"); public override string ModuleDescription => "Simple admin plugin for Counter-Strike 2 :)"; public override string ModuleAuthor => "daffyy & Dliix66"; - public override string ModuleVersion => "1.4.6a"; + public override string ModuleVersion => "1.4.6b"; public CS2_SimpleAdminConfig Config { get; set; } = new(); diff --git a/Events.cs b/Events.cs index f497db4..9e72b62 100644 --- a/Events.cs +++ b/Events.cs @@ -15,10 +15,88 @@ public partial class CS2_SimpleAdmin private void RegisterEvents() { RegisterListener(OnMapStart); + RegisterListener(OnGameServerSteamAPIActivated); AddCommandListener("say", OnCommandSay); AddCommandListener("say_team", OnCommandTeamSay); } + private void OnGameServerSteamAPIActivated() + { + AddTimer(8.5f, () => + { + if (ServerId != null) return; + + var ipAddress = ConVar.Find("ip")?.StringValue; + + if (string.IsNullOrEmpty(ipAddress) || ipAddress.StartsWith("0.0.0")) + { + ipAddress = Helper.GetServerIp(); + Logger.LogError("Unable to get server ip, Check that you have added the correct start parameter \"-ip \""); + Logger.LogError($"Using alternative method... Server IP {ipAddress}"); + } + + if (string.IsNullOrEmpty(ipAddress) || ipAddress.StartsWith("0.0.0")) + { + OnGameServerSteamAPIActivated(); + return; + } + + var address = $"{ipAddress}:{ConVar.Find("hostport")?.GetPrimitiveValue()}"; + var hostname = ConVar.Find("hostname")!.StringValue; + + Task.Run(async () => + { + PermissionManager adminManager = new(_database); + + try + { + await using var connection = await _database.GetConnectionAsync(); + var addressExists = await connection.ExecuteScalarAsync( + "SELECT COUNT(*) FROM sa_servers WHERE address = @address", + new { address }); + + if (!addressExists) + { + await connection.ExecuteAsync( + "INSERT INTO sa_servers (address, hostname) VALUES (@address, @hostname)", + new { address, hostname }); + } + else + { + await connection.ExecuteAsync( + "UPDATE `sa_servers` SET `hostname` = @hostname, `id` = `id` WHERE `address` = @address", + new { address, hostname }); + } + + int? serverId = await connection.ExecuteScalarAsync( + "SELECT `id` FROM `sa_servers` WHERE `address` = @address", + new { address }); + + ServerId = serverId; + } + catch (Exception ex) + { + _logger?.LogCritical("Unable to create or get server_id" + ex.Message); + } + + if (Config.EnableMetrics) + { + var queryString = $"?address={address}&hostname={hostname}"; + using HttpClient client = new(); + + try + { + await client.GetAsync($"https://api.daffyy.love/index.php{queryString}"); + } + catch (HttpRequestException ex) + { + Logger.LogWarning($"Unable to make metrics call: {ex.Message}"); + } + } + }); + }); + } + [GameEventHandler] public HookResult OnClientDisconnect(EventPlayerDisconnect @event, GameEventInfo info) { @@ -277,12 +355,15 @@ public partial class CS2_SimpleAdmin _database = new Database.Database(_dbConnectionString); - AddTimer(2.5f, () => + AddTimer(2f, () => { + if (ServerId != null) return; + var ipAddress = ConVar.Find("ip")?.StringValue; if (string.IsNullOrEmpty(ipAddress) || ipAddress.StartsWith("0.0.0")) { + return; ipAddress = Helper.GetServerIp(); Logger.LogError("Unable to get server ip, Check that you have added the correct start parameter \"-ip \""); Logger.LogError($"Using alternative method... Server IP {ipAddress}"); diff --git a/VERSION b/VERSION index a87f057..e88f0eb 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.4.6a \ No newline at end of file +1.4.6b \ No newline at end of file