- Small fixes
This commit is contained in:
Dawid Bepierszcz
2024-01-25 01:00:31 +01:00
parent 855f087a7b
commit 450a7804c6
5 changed files with 74 additions and 33 deletions

View File

@@ -6,10 +6,12 @@ namespace CS2_SimpleAdmin
internal class BanManager
{
private readonly MySqlConnection _dbConnection;
private readonly CS2_SimpleAdminConfig _config;
public BanManager(string connectionString)
public BanManager(string connectionString, CS2_SimpleAdminConfig config)
{
_dbConnection = new MySqlConnection(connectionString);
_config = config;
}
public async Task BanPlayer(PlayerInfo player, PlayerInfo issuer, string reason, int time = 0)
@@ -23,12 +25,11 @@ namespace CS2_SimpleAdmin
var sql = "INSERT INTO `sa_bans` (`player_steamid`, `player_name`, `player_ip`, `admin_steamid`, `admin_name`, `reason`, `duration`, `ends`, `created`, `server_id`) " +
"VALUES (@playerSteamid, @playerName, @playerIp, @adminSteamid, @adminName, @banReason, @duration, @ends, @created, @serverid)";
CS2_SimpleAdminConfig config = new CS2_SimpleAdminConfig();
await connection.ExecuteAsync(sql, new
{
playerSteamid = player.SteamId,
playerName = player.Name,
playerIp = config.BanType == 1 ? player.IpAddress : null,
playerIp = _config.BanType == 1 ? player.IpAddress : null,
adminSteamid = issuer.SteamId == null ? "Console" : issuer.SteamId,
adminName = issuer.Name == null ? "Console" : issuer.Name,
banReason = reason,
@@ -37,6 +38,8 @@ namespace CS2_SimpleAdmin
created = now,
serverid = CS2_SimpleAdmin.ServerId
});
await connection.CloseAsync();
}
public async Task AddBanBySteamid(string playerSteamId, PlayerInfo issuer, string reason, int time = 0)
@@ -63,6 +66,8 @@ namespace CS2_SimpleAdmin
created = now,
serverid = CS2_SimpleAdmin.ServerId
});
await connection.CloseAsync();
}
public async Task AddBanByIp(string playerIp, PlayerInfo issuer, string reason, int time = 0)
@@ -89,6 +94,8 @@ namespace CS2_SimpleAdmin
created = now,
serverid = CS2_SimpleAdmin.ServerId
});
await connection.CloseAsync();
}
public async Task<bool> IsPlayerBanned(PlayerInfo player)
@@ -111,6 +118,8 @@ namespace CS2_SimpleAdmin
banCount = await connection.ExecuteScalarAsync<int>(sql, new { PlayerSteamID = player.SteamId, PlayerIP = DBNull.Value, CurrentTime = now });
}
await connection.CloseAsync();
return banCount > 0;
}
@@ -131,6 +140,8 @@ namespace CS2_SimpleAdmin
banCount = await connection.ExecuteScalarAsync<int>(sql, new { PlayerSteamID = player.SteamId, PlayerIP = DBNull.Value });
}
await connection.CloseAsync();
return banCount;
}
@@ -146,6 +157,8 @@ namespace CS2_SimpleAdmin
string sqlUnban = "UPDATE sa_bans SET status = 'UNBANNED' WHERE player_steamid = @pattern OR player_name = @pattern OR player_ip = @pattern AND status = 'ACTIVE'";
await connection.ExecuteAsync(sqlUnban, new { pattern = playerPattern });
await connection.CloseAsync();
}
public async Task ExpireOldBans()
@@ -155,6 +168,8 @@ namespace CS2_SimpleAdmin
string sql = "UPDATE sa_bans SET status = 'EXPIRED' WHERE status = 'ACTIVE' AND `duration` > 0 AND ends <= @CurrentTime";
await connection.ExecuteAsync(sql, new { CurrentTime = DateTime.Now });
await connection.CloseAsync();
}
}
}