fix: major simplication of the server query and use correct dapper query

This commit is contained in:
Dollan 2024-04-11 22:34:23 +02:00
parent cab74dcb0c
commit 8f1702bb75
No known key found for this signature in database

View file

@ -17,27 +17,6 @@ public partial class CS2_SimpleAdmin
{ {
public static HashSet<int> loadedPlayers = new HashSet<int>(); 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() private void RegisterEvents()
{ {
RegisterListener<Listeners.OnMapStart>(OnMapStart); RegisterListener<Listeners.OnMapStart>(OnMapStart);
@ -406,16 +385,18 @@ public partial class CS2_SimpleAdmin
ServerData serverData = await connection.ExecuteScalarAsync<ServerData>(
(int? serverId, string? groupIds) = await connection.QueryFirstAsync<(int?, string?)>(
"SELECT `id`, `group_ids` FROM `sa_servers` WHERE `address` = @address", "SELECT `id`, `group_ids` FROM `sa_servers` WHERE `address` = @address",
new { address }); new { address });
ServerId = serverData.Id; ServerId = serverId;
GroupIds = serverData.GroupIds; GroupIds = groupIds?.Split(',').Select(int.Parse).ToArray();
// For testing only // For testing only
_logger?.LogInformation($"Server ID: {ServerId}"); _logger?.LogInformation($"Server ID: {ServerId}");
_logger?.LogInformation($"Group IDs: {GroupIds}"); _logger?.LogInformation($"Group IDs: {string.Join(", ", GroupIds ?? Array.Empty<int>())}");
} }
catch (Exception ex) catch (Exception ex)