mirror of
https://github.com/daffyyyy/CS2-SimpleAdmin.git
synced 2026-02-21 19:29:39 +00:00
1.2.9a
- Major changes - Fixed `css_respawn` - Added discord webhook - Refactoring database class
This commit is contained in:
@@ -1,16 +1,15 @@
|
||||
using Dapper;
|
||||
using MySqlConnector;
|
||||
|
||||
namespace CS2_SimpleAdmin
|
||||
{
|
||||
internal class BanManager
|
||||
{
|
||||
private readonly MySqlConnection _dbConnection;
|
||||
private readonly Database _database;
|
||||
private readonly CS2_SimpleAdminConfig _config;
|
||||
|
||||
public BanManager(string connectionString, CS2_SimpleAdminConfig config)
|
||||
public BanManager(Database database, CS2_SimpleAdminConfig config)
|
||||
{
|
||||
_dbConnection = new MySqlConnection(connectionString);
|
||||
_database = database;
|
||||
_config = config;
|
||||
}
|
||||
|
||||
@@ -19,8 +18,7 @@ namespace CS2_SimpleAdmin
|
||||
DateTime now = DateTime.Now;
|
||||
DateTime futureTime = now.AddMinutes(time);
|
||||
|
||||
await using var connection = _dbConnection;
|
||||
await connection.OpenAsync();
|
||||
await using var connection = _database.GetConnection();
|
||||
|
||||
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)";
|
||||
@@ -38,8 +36,6 @@ 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)
|
||||
@@ -49,8 +45,7 @@ namespace CS2_SimpleAdmin
|
||||
DateTime now = DateTime.Now;
|
||||
DateTime futureTime = now.AddMinutes(time);
|
||||
|
||||
await using var connection = _dbConnection;
|
||||
await connection.OpenAsync();
|
||||
await using var connection = _database.GetConnection();
|
||||
|
||||
var sql = "INSERT INTO `sa_bans` (`player_steamid`, `admin_steamid`, `admin_name`, `reason`, `duration`, `ends`, `created`, `server_id`) " +
|
||||
"VALUES (@playerSteamid, @adminSteamid, @adminName, @banReason, @duration, @ends, @created, @serverid)";
|
||||
@@ -66,8 +61,6 @@ 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)
|
||||
@@ -77,8 +70,7 @@ namespace CS2_SimpleAdmin
|
||||
DateTime now = DateTime.Now;
|
||||
DateTime futureTime = now.AddMinutes(time);
|
||||
|
||||
await using var connection = _dbConnection;
|
||||
await connection.OpenAsync();
|
||||
await using var connection = _database.GetConnection();
|
||||
|
||||
var sql = "INSERT INTO `sa_bans` (`player_ip`, `admin_steamid`, `admin_name`, `reason`, `duration`, `ends`, `created`, `server_id`) " +
|
||||
"VALUES (@playerIp, @adminSteamid, @adminName, @banReason, @duration, @ends, @created, @serverid)";
|
||||
@@ -94,8 +86,6 @@ namespace CS2_SimpleAdmin
|
||||
created = now,
|
||||
serverid = CS2_SimpleAdmin.ServerId
|
||||
});
|
||||
|
||||
await connection.CloseAsync();
|
||||
}
|
||||
|
||||
public async Task<bool> IsPlayerBanned(PlayerInfo player)
|
||||
@@ -106,8 +96,7 @@ namespace CS2_SimpleAdmin
|
||||
|
||||
int banCount;
|
||||
|
||||
await using var connection = _dbConnection;
|
||||
await connection.OpenAsync();
|
||||
await using var connection = _database.GetConnection();
|
||||
|
||||
if (!string.IsNullOrEmpty(player.IpAddress))
|
||||
{
|
||||
@@ -118,8 +107,6 @@ namespace CS2_SimpleAdmin
|
||||
banCount = await connection.ExecuteScalarAsync<int>(sql, new { PlayerSteamID = player.SteamId, PlayerIP = DBNull.Value, CurrentTime = now });
|
||||
}
|
||||
|
||||
await connection.CloseAsync();
|
||||
|
||||
return banCount > 0;
|
||||
}
|
||||
|
||||
@@ -128,8 +115,7 @@ namespace CS2_SimpleAdmin
|
||||
string sql = "SELECT COUNT(*) FROM sa_bans WHERE (player_steamid = @PlayerSteamID OR player_ip = @PlayerIP)";
|
||||
int banCount;
|
||||
|
||||
await using var connection = _dbConnection;
|
||||
await connection.OpenAsync();
|
||||
await using var connection = _database.GetConnection();
|
||||
|
||||
if (!string.IsNullOrEmpty(player.IpAddress))
|
||||
{
|
||||
@@ -140,8 +126,6 @@ namespace CS2_SimpleAdmin
|
||||
banCount = await connection.ExecuteScalarAsync<int>(sql, new { PlayerSteamID = player.SteamId, PlayerIP = DBNull.Value });
|
||||
}
|
||||
|
||||
await connection.CloseAsync();
|
||||
|
||||
return banCount;
|
||||
}
|
||||
|
||||
@@ -152,24 +136,18 @@ namespace CS2_SimpleAdmin
|
||||
return;
|
||||
}
|
||||
|
||||
await using var connection = _dbConnection;
|
||||
await connection.OpenAsync();
|
||||
await using var connection = _database.GetConnection();
|
||||
|
||||
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()
|
||||
{
|
||||
await using var connection = _dbConnection;
|
||||
await connection.OpenAsync();
|
||||
await using var connection = _database.GetConnection();
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user