mirror of
https://github.com/daffyyyy/CS2-SimpleAdmin.git
synced 2026-10-18 04:47:04 +02:00
【UPDATE 1.7.1a】 🆕 What's new and what's changed: - Fixed 2x player lock text - Added immunity check when using css_addX - MenuManagerCS2 has been updated - Added return of lock ID by api (check example_module) - Fixed reasons for locks, no longer need to use `“”` if there is a space in them - Improved handling of lock times, from now on you can use: -- 1mo - 1 month -- 1w - 1 week -- 1d - 1 day -- 1h - 1 hour -- 1m - 1 minute -- 1 - 1 minute You can also combine it, e.g: 10d5h - 10 days and 5 hours 2w3d - 2 weeks and 3 days 1w1d1h1 - 1 week, 1 day and 1 minute So for example css_ban player 10d5h Super reason for my ban - Added a `rcon_password` column in the `sa_servers` table that populates with the rcon password - Fixed `banid` and banning - Fixed too fast kick (it was caused by checking players online) ⚠️ **Remember to update all files + api**
104 lines
No EOL
4 KiB
C#
104 lines
No EOL
4 KiB
C#
using CounterStrikeSharp.API;
|
|
using CounterStrikeSharp.API.Modules.Cvars;
|
|
using Dapper;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace CS2_SimpleAdmin.Managers;
|
|
|
|
public class ServerManager
|
|
{
|
|
private int _getIpTryCount;
|
|
|
|
public void LoadServerData()
|
|
{
|
|
CS2_SimpleAdmin.Instance.AddTimer(1.2f, () =>
|
|
{
|
|
if (CS2_SimpleAdmin.ServerLoaded || CS2_SimpleAdmin.ServerId != null || CS2_SimpleAdmin.Database == null) return;
|
|
|
|
if (_getIpTryCount > 32 && Helper.GetServerIp().StartsWith("0.0.0.0") || string.IsNullOrEmpty(Helper.GetServerIp()))
|
|
{
|
|
CS2_SimpleAdmin._logger?.LogError("Unable to load server data - can't fetch ip address!");
|
|
return;
|
|
}
|
|
|
|
var ipAddress = ConVar.Find("ip")?.StringValue;
|
|
|
|
if (string.IsNullOrEmpty(ipAddress) || ipAddress.StartsWith("0.0.0"))
|
|
{
|
|
ipAddress = Helper.GetServerIp();
|
|
|
|
if (_getIpTryCount <= 32 && (string.IsNullOrEmpty(ipAddress) || ipAddress.StartsWith("0.0.0")))
|
|
{
|
|
_getIpTryCount++;
|
|
|
|
LoadServerData();
|
|
return;
|
|
}
|
|
}
|
|
|
|
var address = $"{ipAddress}:{ConVar.Find("hostport")?.GetPrimitiveValue<int>()}";
|
|
var hostname = ConVar.Find("hostname")!.StringValue;
|
|
var rconPassword = ConVar.Find("rcon_password")!.StringValue;
|
|
CS2_SimpleAdmin.IpAddress = address;
|
|
|
|
CS2_SimpleAdmin._logger?.LogInformation("Loaded server with ip {ip}", ipAddress);
|
|
|
|
Task.Run(async () =>
|
|
{
|
|
try
|
|
{
|
|
await using var connection = await CS2_SimpleAdmin.Database.GetConnectionAsync();
|
|
|
|
int? serverId = await connection.ExecuteScalarAsync<int?>(
|
|
"SELECT id FROM sa_servers WHERE address = @address",
|
|
new { address });
|
|
|
|
if (serverId == null)
|
|
{
|
|
await connection.ExecuteAsync(
|
|
"INSERT INTO sa_servers (address, hostname, rcon_password) VALUES (@address, @hostname, @rconPassword)",
|
|
new { address, hostname, rconPassword });
|
|
|
|
serverId = await connection.ExecuteScalarAsync<int>(
|
|
"SELECT id FROM sa_servers WHERE address = @address",
|
|
new { address });
|
|
}
|
|
else
|
|
{
|
|
await connection.ExecuteAsync(
|
|
"UPDATE sa_servers SET hostname = @hostname, rcon_password = @rconPassword WHERE address = @address",
|
|
new { address, hostname, rconPassword });
|
|
}
|
|
|
|
CS2_SimpleAdmin.ServerId = serverId;
|
|
|
|
if (CS2_SimpleAdmin.ServerId != null)
|
|
{
|
|
await Server.NextWorldUpdateAsync(() => CS2_SimpleAdmin.Instance.ReloadAdmins(null));
|
|
}
|
|
|
|
CS2_SimpleAdmin.ServerLoaded = true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
CS2_SimpleAdmin._logger?.LogCritical("Unable to create or get server_id: " + ex.Message);
|
|
}
|
|
|
|
if (CS2_SimpleAdmin.Instance.Config.EnableMetrics)
|
|
{
|
|
var queryString = $"?address={address}&hostname={hostname}";
|
|
var client = CS2_SimpleAdmin.HttpClient;
|
|
|
|
try
|
|
{
|
|
await client.GetAsync($"https://api.daffyy.love/index.php{queryString}");
|
|
}
|
|
catch (HttpRequestException ex)
|
|
{
|
|
CS2_SimpleAdmin._logger?.LogWarning($"Unable to make metrics call: {ex.Message}");
|
|
}
|
|
}
|
|
});
|
|
});
|
|
}
|
|
} |