- Fix for alternative get server ip method
This commit is contained in:
Dawid Bepierszcz
2024-06-18 03:12:00 +02:00
parent 985b1ae61f
commit 8e4724fb3e
3 changed files with 84 additions and 3 deletions

View File

@@ -37,7 +37,7 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig<CS2_SimpleAdmin
public override string ModuleName => "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();

View File

@@ -15,10 +15,88 @@ public partial class CS2_SimpleAdmin
private void RegisterEvents()
{
RegisterListener<Listeners.OnMapStart>(OnMapStart);
RegisterListener<Listeners.OnGameServerSteamAPIActivated>(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 <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<int>()}";
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<bool>(
"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<int>(
"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 <ip>\"");
Logger.LogError($"Using alternative method... Server IP {ipAddress}");

View File

@@ -1 +1 @@
1.4.6a
1.4.6b