Skip to content

Commit

Permalink
1.2.8a
Browse files Browse the repository at this point in the history
- Small fixes
  • Loading branch information
daffyyyy committed Jan 25, 2024
1 parent 855f087 commit 450a780
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 33 deletions.
10 changes: 10 additions & 0 deletions AdminSQLManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ public async Task<List<dynamic>> GetAdminFlags(string steamId)
return flagsToCache.Cast<object>().ToList();
}
*/
await connection.CloseAsync();

return filteredFlagsWithImmunity;
//return filteredFlags.Cast<object>().ToList();
}
Expand Down Expand Up @@ -208,6 +210,8 @@ public async Task<List<dynamic>> GetAdminFlags(string steamId)
filteredFlagsWithImmunity.Add((steamId, flagsValue.Split(',').ToList(), immunityValue, ends));
}

await connection.CloseAsync();

return filteredFlagsWithImmunity;
}

Expand Down Expand Up @@ -259,6 +263,8 @@ public async Task DeleteAdminBySteamId(string playerSteamId, bool globalDelete =
}

await connection.ExecuteAsync(sql, new { PlayerSteamID = playerSteamId, ServerId = CS2_SimpleAdmin.ServerId });

await connection.CloseAsync();
}

public async Task AddAdminBySteamId(string playerSteamId, string playerName, string flags, int immunity = 0, int time = 0, bool globalAdmin = false)
Expand Down Expand Up @@ -292,6 +298,8 @@ public async Task AddAdminBySteamId(string playerSteamId, string playerName, str
created = now,
serverid = serverId
});

await connection.CloseAsync();
}

public async Task DeleteOldAdmins()
Expand All @@ -301,6 +309,8 @@ public async Task DeleteOldAdmins()

string sql = "DELETE FROM sa_admins WHERE ends IS NOT NULL AND ends <= @CurrentTime";
await connection.ExecuteAsync(sql, new { CurrentTime = DateTime.Now });

await connection.CloseAsync();
}
}
}
21 changes: 18 additions & 3 deletions BanManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -23,12 +25,11 @@ public async Task BanPlayer(PlayerInfo player, PlayerInfo issuer, string reason,
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,
Expand All @@ -37,6 +38,8 @@ public async Task BanPlayer(PlayerInfo player, PlayerInfo issuer, string reason,
created = now,
serverid = CS2_SimpleAdmin.ServerId
});

await connection.CloseAsync();
}

public async Task AddBanBySteamid(string playerSteamId, PlayerInfo issuer, string reason, int time = 0)
Expand All @@ -63,6 +66,8 @@ public async Task AddBanBySteamid(string playerSteamId, PlayerInfo issuer, strin
created = now,
serverid = CS2_SimpleAdmin.ServerId
});

await connection.CloseAsync();
}

public async Task AddBanByIp(string playerIp, PlayerInfo issuer, string reason, int time = 0)
Expand All @@ -89,6 +94,8 @@ public async Task AddBanByIp(string playerIp, PlayerInfo issuer, string reason,
created = now,
serverid = CS2_SimpleAdmin.ServerId
});

await connection.CloseAsync();
}

public async Task<bool> IsPlayerBanned(PlayerInfo player)
Expand All @@ -111,6 +118,8 @@ public async Task<bool> IsPlayerBanned(PlayerInfo player)
banCount = await connection.ExecuteScalarAsync<int>(sql, new { PlayerSteamID = player.SteamId, PlayerIP = DBNull.Value, CurrentTime = now });
}

await connection.CloseAsync();

return banCount > 0;
}

Expand All @@ -131,6 +140,8 @@ public async Task<int> GetPlayerBans(PlayerInfo player)
banCount = await connection.ExecuteScalarAsync<int>(sql, new { PlayerSteamID = player.SteamId, PlayerIP = DBNull.Value });
}

await connection.CloseAsync();

return banCount;
}

Expand All @@ -146,6 +157,8 @@ public async Task UnbanPlayer(string playerPattern)

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()
Expand All @@ -155,6 +168,8 @@ public async Task ExpireOldBans()

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();
}
}
}
25 changes: 16 additions & 9 deletions CS2-SimpleAdmin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig<CS2_SimpleAdmin
public static IStringLocalizer? _localizer;
public static ConcurrentBag<ushort> gaggedPlayers = new ConcurrentBag<ushort>();
//public static ConcurrentBag<int> mutedPlayers = new ConcurrentBag<int>();
public static List<int> loadedPlayers = new List<int>();
public static Dictionary<string, int> voteAnswers = new Dictionary<string, int>();
public static HashSet<ushort> godPlayers = new HashSet<ushort>();
public static List<ushort> silentPlayers = new List<ushort>();
Expand Down Expand Up @@ -303,7 +302,7 @@ public void OnWhoCommand(CCSPlayerController? caller, CommandInfo command)
if (targets == null) return;
List<CCSPlayerController> playersToTarget = targets!.Players.Where(player => caller!.CanTarget(player) && player != null && player.IsValid && !player.IsBot && !player.IsHLTV).ToList();

BanManager _banManager = new(dbConnectionString);
BanManager _banManager = new(dbConnectionString, Config);
MuteManager _muteManager = new(dbConnectionString);

playersToTarget.ForEach(player =>
Expand Down Expand Up @@ -435,7 +434,11 @@ public void OnKickCommand(CCSPlayerController? caller, CommandInfo command)
{
if (caller!.CanTarget(player))
{
player.Pawn.Value!.Freeze();
if (player.PawnIsAlive)
{
player.Pawn.Value!.Freeze();
player.CommitSuicide(true, true);
}

if (command.ArgCount >= 2)
{
Expand Down Expand Up @@ -1091,7 +1094,7 @@ public void OnBanCommand(CCSPlayerController? caller, CommandInfo command)
return;
}

BanManager _banManager = new(dbConnectionString);
BanManager _banManager = new(dbConnectionString, Config);

int.TryParse(command.GetArg(2), out time);

Expand All @@ -1102,7 +1105,11 @@ public void OnBanCommand(CCSPlayerController? caller, CommandInfo command)
{
if (caller!.CanTarget(player))
{
player.Pawn.Value!.Freeze();
if (player.PawnIsAlive)
{
player.Pawn.Value!.Freeze();
player.CommitSuicide(true, true);
}

PlayerInfo playerInfo = new PlayerInfo
{
Expand Down Expand Up @@ -1171,7 +1178,7 @@ public void OnAddBanCommand(CCSPlayerController? caller, CommandInfo command)
int time = 0;
string reason = "Unknown";

BanManager _banManager = new(dbConnectionString);
BanManager _banManager = new(dbConnectionString, Config);

int.TryParse(command.GetArg(2), out time);

Expand Down Expand Up @@ -1227,7 +1234,7 @@ public void OnAddBanCommand(CCSPlayerController? caller, CommandInfo command)

Task.Run(async () =>
{
BanManager _banManager = new(dbConnectionString);
BanManager _banManager = new(dbConnectionString, Config);
await _banManager.AddBanBySteamid(steamid, adminInfo, reason, time);
});

Expand Down Expand Up @@ -1309,7 +1316,7 @@ public void OnBanIp(CCSPlayerController? caller, CommandInfo command)

Task.Run(async () =>
{
BanManager _banManager = new(dbConnectionString);
BanManager _banManager = new(dbConnectionString, Config);
await _banManager.AddBanByIp(ipAddress, adminInfo, reason, time);
});

Expand All @@ -1328,7 +1335,7 @@ public void OnUnbanCommand(CCSPlayerController? caller, CommandInfo command)
}

string pattern = command.GetArg(1);
BanManager _banManager = new(dbConnectionString);
BanManager _banManager = new(dbConnectionString, Config);

_ = _banManager.UnbanPlayer(pattern);

Expand Down
39 changes: 18 additions & 21 deletions Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private void OnClientConnect(int playerSlot, string name, string ipAddress)

Task.Run(async () =>
{
BanManager _banManager = new(dbConnectionString);
BanManager _banManager = new(dbConnectionString, Config);
bool isBanned = await _banManager.IsPlayerBanned(playerInfo);

MuteManager _muteManager = new(dbConnectionString);
Expand Down Expand Up @@ -360,7 +360,7 @@ private void OnClientAuthorized(int playerSlot, SteamID steamID)

Task.Run(async () =>
{
BanManager _banManager = new(dbConnectionString);
BanManager _banManager = new(dbConnectionString, Config);
bool isBanned = await _banManager.IsPlayerBanned(playerInfo);

MuteManager _muteManager = new(dbConnectionString);
Expand Down Expand Up @@ -501,9 +501,6 @@ private void OnClientAuthorized(int playerSlot, SteamID steamID)
}
}
*/

if (player.UserId != null && !loadedPlayers.Contains((ushort)player.UserId))
loadedPlayers.Add((ushort)player.UserId);
});
});
}
Expand Down Expand Up @@ -542,9 +539,6 @@ private void OnClientDisconnect(int playerSlot)
godPlayers.Remove((ushort)player.UserId);
}

if (player.UserId != null && loadedPlayers.Contains((ushort)player.UserId))
loadedPlayers.Remove((ushort)player.UserId);

if (player.AuthorizedSteamID != null && AdminSQLManager._adminCacheSet.Contains(player.AuthorizedSteamID))
{
if (AdminSQLManager._adminCacheTimestamps != null && AdminSQLManager._adminCacheTimestamps.TryGetValue(player.AuthorizedSteamID, out DateTime? expirationTime) && expirationTime.HasValue && expirationTime.Value <= DateTime.Now)
Expand All @@ -565,7 +559,7 @@ private void OnMapStart(string mapName)
AddTimer(60.0f, bannedPlayers.Clear, CounterStrikeSharp.API.Modules.Timers.TimerFlags.REPEAT | CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE);
AddTimer(120.0f, () =>
{
BanManager _banManager = new(dbConnectionString);
BanManager _banManager = new(dbConnectionString, Config);
MuteManager _muteManager = new(dbConnectionString);
_ = _banManager.ExpireOldBans();
_ = _muteManager.ExpireOldMutes();
Expand All @@ -578,23 +572,26 @@ private void OnMapStart(string mapName)
TagsDetected = true;
}

using (var connection = new MySqlConnection(dbConnectionString))
AddTimer(1.0f, () =>
{
connection.Open();
using (var connection = new MySqlConnection(dbConnectionString))
{
connection.Open();

connection.Execute(
"INSERT INTO `sa_servers` (address, hostname) VALUES (@address, @hostname) " +
"ON DUPLICATE KEY UPDATE hostname = @hostname",
new { address = $"{ConVar.Find("ip")!.StringValue}:{ConVar.Find("hostport")!.GetPrimitiveValue<int>()}", hostname = ConVar.Find("hostname")!.StringValue });
connection.Execute(
"INSERT INTO `sa_servers` (address, hostname) VALUES (@address, @hostname) " +
"ON DUPLICATE KEY UPDATE hostname = @hostname",
new { address = $"{ConVar.Find("ip")!.StringValue}:{ConVar.Find("hostport")!.GetPrimitiveValue<int>()}", hostname = ConVar.Find("hostname")!.StringValue });

int? serverId = connection.ExecuteScalar<int>(
"SELECT `id` FROM `sa_servers` WHERE `address` = @address",
new { address = $"{ConVar.Find("ip")!.StringValue}:{ConVar.Find("hostport")!.GetPrimitiveValue<int>()}" });
int? serverId = connection.ExecuteScalar<int>(
"SELECT `id` FROM `sa_servers` WHERE `address` = @address",
new { address = $"{ConVar.Find("ip")!.StringValue}:{ConVar.Find("hostport")!.GetPrimitiveValue<int>()}" });

ServerId = serverId;
ServerId = serverId;

connection.Close();
}
connection.Close();
}
});

_ = _adminManager.GiveAllFlags();
}
Expand Down
12 changes: 12 additions & 0 deletions MuteManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public async Task MutePlayer(PlayerInfo player, PlayerInfo issuer, string reason
type = muteType,
serverid = CS2_SimpleAdmin.ServerId
});

await connection.CloseAsync();
}

public async Task AddMuteBySteamid(string playerSteamId, PlayerInfo issuer, string reason, int time = 0, int type = 0)
Expand Down Expand Up @@ -75,6 +77,8 @@ public async Task AddMuteBySteamid(string playerSteamId, PlayerInfo issuer, stri
type = muteType,
serverid = CS2_SimpleAdmin.ServerId
});

await connection.CloseAsync();
}

public async Task<List<dynamic>> IsPlayerMuted(string steamId)
Expand All @@ -87,6 +91,8 @@ public async Task<List<dynamic>> IsPlayerMuted(string steamId)
string sql = "SELECT * FROM sa_mutes WHERE player_steamid = @PlayerSteamID AND status = 'ACTIVE' AND (duration = 0 OR ends > @CurrentTime)";
var activeMutes = (await connection.QueryAsync(sql, new { PlayerSteamID = steamId, CurrentTime = now })).ToList();

await connection.CloseAsync();

return activeMutes;
}

Expand All @@ -100,6 +106,8 @@ public async Task<int> GetPlayerMutes(string steamId)

muteCount = await connection.ExecuteScalarAsync<int>(sql, new { PlayerSteamID = steamId });

await connection.CloseAsync();

return muteCount;
}

Expand Down Expand Up @@ -134,6 +142,8 @@ public async Task UnmutePlayer(string playerPattern, int type = 0)

string sqlUnban = "UPDATE sa_mutes SET status = 'UNMUTED' WHERE (player_steamid = @pattern OR player_name = @pattern) AND type = @muteType AND status = 'ACTIVE'";
await connection.ExecuteAsync(sqlUnban, new { pattern = playerPattern, muteType });

await connection.CloseAsync();
}

public async Task ExpireOldMutes()
Expand All @@ -143,6 +153,8 @@ public async Task ExpireOldMutes()

string sql = "UPDATE sa_mutes SET status = 'EXPIRED' WHERE status = 'ACTIVE' AND `duration` > 0 AND ends <= @CurrentTime";
await connection.ExecuteAsync(sql, new { CurrentTime = DateTime.Now });

await connection.CloseAsync();
}

public async Task CheckMute(PlayerInfo player)
Expand Down

0 comments on commit 450a780

Please sign in to comment.