mirror of
https://github.com/daffyyyy/CS2-SimpleAdmin.git
synced 2026-02-18 10:43:23 +00:00
Merge branch 'main' into feature/menu
# Conflicts: # CS2-SimpleAdmin.cs
This commit is contained in:
@@ -1,20 +1,19 @@
|
||||
using CounterStrikeSharp.API.Modules.Entities;
|
||||
using Dapper;
|
||||
using MySqlConnector;
|
||||
|
||||
namespace CS2_SimpleAdmin
|
||||
{
|
||||
internal class AdminSQLManager
|
||||
public class AdminSQLManager
|
||||
{
|
||||
private readonly MySqlConnection _dbConnection;
|
||||
private readonly Database _database;
|
||||
// Unused for now
|
||||
//public static readonly ConcurrentDictionary<string, ConcurrentBag<string>> _adminCache = new ConcurrentDictionary<string, ConcurrentBag<string>>();
|
||||
public static readonly HashSet<SteamID> _adminCacheSet = new HashSet<SteamID>();
|
||||
public static readonly Dictionary<SteamID, DateTime?> _adminCacheTimestamps = new Dictionary<SteamID, DateTime?>();
|
||||
|
||||
public AdminSQLManager(string connectionString)
|
||||
public AdminSQLManager(Database database)
|
||||
{
|
||||
_dbConnection = new MySqlConnection(connectionString);
|
||||
_database = database;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -26,7 +25,7 @@ namespace CS2_SimpleAdmin
|
||||
}
|
||||
else
|
||||
{
|
||||
await using var connection = _dbConnection;
|
||||
await using var connection = _database.GetConnection();
|
||||
await connection.OpenAsync();
|
||||
|
||||
DateTime now = DateTime.Now;
|
||||
@@ -48,16 +47,9 @@ namespace CS2_SimpleAdmin
|
||||
|
||||
public async Task<List<(List<string>, int)>> GetAdminFlags(string steamId)
|
||||
{
|
||||
/* Unused for now
|
||||
if (_adminCache.TryGetValue(steamId, out ConcurrentBag<string>? cachedFlags))
|
||||
{
|
||||
return cachedFlags.ToList<object>();
|
||||
}
|
||||
*/
|
||||
DateTime now = DateTime.Now;
|
||||
|
||||
await using var connection = _dbConnection;
|
||||
await connection.OpenAsync();
|
||||
await using var connection = _database.GetConnection();
|
||||
|
||||
string sql = "SELECT flags, immunity, ends FROM sa_admins WHERE player_steamid = @PlayerSteamID AND (ends IS NULL OR ends > @CurrentTime) AND (server_id IS NULL OR server_id = @serverid)";
|
||||
List<dynamic>? activeFlags = (await connection.QueryAsync(sql, new { PlayerSteamID = steamId, CurrentTime = now, serverid = CS2_SimpleAdmin.ServerId }))?.ToList();
|
||||
@@ -147,8 +139,6 @@ namespace CS2_SimpleAdmin
|
||||
return flagsToCache.Cast<object>().ToList();
|
||||
}
|
||||
*/
|
||||
await connection.CloseAsync();
|
||||
|
||||
return filteredFlagsWithImmunity;
|
||||
//return filteredFlags.Cast<object>().ToList();
|
||||
}
|
||||
@@ -157,8 +147,7 @@ namespace CS2_SimpleAdmin
|
||||
{
|
||||
DateTime now = DateTime.Now;
|
||||
|
||||
await using var connection = _dbConnection;
|
||||
await connection.OpenAsync();
|
||||
await using var connection = _database.GetConnection();
|
||||
|
||||
string sql = "SELECT player_steamid, flags, immunity, ends FROM sa_admins WHERE (ends IS NULL OR ends > @CurrentTime) AND (server_id IS NULL OR server_id = @serverid)";
|
||||
List<dynamic>? activeFlags = (await connection.QueryAsync(sql, new { CurrentTime = now, serverid = CS2_SimpleAdmin.ServerId }))?.ToList();
|
||||
@@ -210,8 +199,6 @@ namespace CS2_SimpleAdmin
|
||||
filteredFlagsWithImmunity.Add((steamId, flagsValue.Split(',').ToList(), immunityValue, ends));
|
||||
}
|
||||
|
||||
await connection.CloseAsync();
|
||||
|
||||
return filteredFlagsWithImmunity;
|
||||
}
|
||||
|
||||
@@ -248,8 +235,7 @@ namespace CS2_SimpleAdmin
|
||||
|
||||
//_adminCache.TryRemove(playerSteamId, out _);
|
||||
|
||||
await using var connection = _dbConnection;
|
||||
await connection.OpenAsync();
|
||||
await using var connection = _database.GetConnection();
|
||||
|
||||
string sql = "";
|
||||
|
||||
@@ -263,8 +249,6 @@ namespace CS2_SimpleAdmin
|
||||
}
|
||||
|
||||
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)
|
||||
@@ -280,8 +264,7 @@ namespace CS2_SimpleAdmin
|
||||
else
|
||||
futureTime = null;
|
||||
|
||||
await using var connection = _dbConnection;
|
||||
await connection.OpenAsync();
|
||||
await using var connection = _database.GetConnection();
|
||||
|
||||
var sql = "INSERT INTO `sa_admins` (`player_steamid`, `player_name`, `flags`, `immunity`, `ends`, `created`, `server_id`) " +
|
||||
"VALUES (@playerSteamid, @playerName, @flags, @immunity, @ends, @created, @serverid)";
|
||||
@@ -298,19 +281,14 @@ namespace CS2_SimpleAdmin
|
||||
created = now,
|
||||
serverid = serverId
|
||||
});
|
||||
|
||||
await connection.CloseAsync();
|
||||
}
|
||||
|
||||
public async Task DeleteOldAdmins()
|
||||
{
|
||||
await using var connection = _dbConnection;
|
||||
await connection.OpenAsync();
|
||||
await using var connection = _database.GetConnection();
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -12,6 +12,7 @@
|
||||
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.159" />
|
||||
<PackageReference Include="Dapper" Version="*" />
|
||||
<PackageReference Include="MySqlConnector" Version="2.3.5" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="*" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -5,7 +5,7 @@ namespace CS2_SimpleAdmin
|
||||
{
|
||||
public class CS2_SimpleAdminConfig : BasePluginConfig
|
||||
{
|
||||
public override int Version { get; set; } = 3;
|
||||
public override int Version { get; set; } = 4;
|
||||
|
||||
[JsonPropertyName("DatabaseHost")]
|
||||
public string DatabaseHost { get; set; } = "";
|
||||
@@ -31,5 +31,8 @@ namespace CS2_SimpleAdmin
|
||||
[JsonPropertyName("BanType")]
|
||||
public int BanType { get; set; } = 1;
|
||||
|
||||
[JsonPropertyName("DiscordWebhook")]
|
||||
public string DiscordWebhook { get; set; } = "";
|
||||
|
||||
}
|
||||
}
|
||||
27
Database.cs
Normal file
27
Database.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using MySqlConnector;
|
||||
|
||||
namespace CS2_SimpleAdmin;
|
||||
public class Database
|
||||
{
|
||||
private readonly string _dbConnectionString;
|
||||
|
||||
public Database(string dbConnectionString)
|
||||
{
|
||||
_dbConnectionString = dbConnectionString;
|
||||
}
|
||||
|
||||
public MySqlConnection GetConnection()
|
||||
{
|
||||
var connection = new MySqlConnection(_dbConnectionString);
|
||||
connection.Open();
|
||||
return connection;
|
||||
}
|
||||
}
|
||||
|
||||
public static class DatabaseExtension
|
||||
{
|
||||
public static string WithConnectionPooling(this string connectionString)
|
||||
{
|
||||
return $"{connectionString};Pooling=true;MinimumPoolSize=1;MaximumPoolSize=15";
|
||||
}
|
||||
}
|
||||
115
Events.cs
115
Events.cs
@@ -4,7 +4,8 @@ using CounterStrikeSharp.API.Modules.Admin;
|
||||
using CounterStrikeSharp.API.Modules.Commands;
|
||||
using CounterStrikeSharp.API.Modules.Cvars;
|
||||
using Dapper;
|
||||
using MySqlConnector;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Data;
|
||||
using System.Text;
|
||||
using static CounterStrikeSharp.API.Core.Listeners;
|
||||
|
||||
@@ -24,7 +25,6 @@ public partial class CS2_SimpleAdmin
|
||||
RegisterEventHandler<EventRoundStart>(OnRoundStart);
|
||||
AddCommandListener("say", OnCommandSay);
|
||||
AddCommandListener("say_team", OnCommandTeamSay);
|
||||
//AddCommandListener("callvote", OnCommandCallVote);
|
||||
}
|
||||
|
||||
/*private HookResult OnPlayerFullConnect(EventPlayerConnectFull @event, GameEventInfo info)
|
||||
@@ -56,12 +56,15 @@ public partial class CS2_SimpleAdmin
|
||||
private HookResult OnRoundStart(EventRoundStart @event, GameEventInfo info)
|
||||
{
|
||||
godPlayers.Clear();
|
||||
#if DEBUG
|
||||
Logger.LogCritical("[OnRoundStart]");
|
||||
#endif
|
||||
return HookResult.Continue;
|
||||
}
|
||||
|
||||
private HookResult OnCommandSay(CCSPlayerController? player, CommandInfo info)
|
||||
{
|
||||
if (player == null || !player.IsValid || info.GetArg(1).Length == 0) return HookResult.Continue;
|
||||
if (player == null || !player.IsValid || player.IsBot || player.IsHLTV || info.GetArg(1).Length == 0) return HookResult.Continue;
|
||||
|
||||
if (player != null && player.SteamID.ToString() != "" && gaggedPlayers.Contains(player.SteamID.ToString()))
|
||||
{
|
||||
@@ -73,7 +76,7 @@ public partial class CS2_SimpleAdmin
|
||||
|
||||
private HookResult OnCommandTeamSay(CCSPlayerController? player, CommandInfo info)
|
||||
{
|
||||
if (player == null || !player.IsValid || info.GetArg(1).Length == 0) return HookResult.Continue;
|
||||
if (player == null || !player.IsValid || player.IsBot || player.IsHLTV || info.GetArg(1).Length == 0) return HookResult.Continue;
|
||||
|
||||
if (player != null && player.SteamID.ToString() != "" && gaggedPlayers.Contains(player.SteamID.ToString()))
|
||||
{
|
||||
@@ -108,31 +111,18 @@ public partial class CS2_SimpleAdmin
|
||||
return HookResult.Continue;
|
||||
}
|
||||
|
||||
private HookResult OnCommandCallVote(CCSPlayerController? player, CommandInfo info)
|
||||
{
|
||||
string reason = info.GetArg(1);
|
||||
|
||||
if (reason == "kick" || reason == "ban")
|
||||
{
|
||||
int.TryParse(info.GetArg(2), out int target);
|
||||
if (target > 0)
|
||||
{
|
||||
if (!player!.CanTarget(Utilities.GetPlayerFromUserid(target)))
|
||||
return HookResult.Handled;
|
||||
}
|
||||
}
|
||||
|
||||
return HookResult.Continue;
|
||||
}
|
||||
|
||||
private void OnClientPutInServer(int playerSlot)
|
||||
{
|
||||
CCSPlayerController? player = Utilities.GetPlayerFromSlot(playerSlot);
|
||||
#if DEBUG
|
||||
Logger.LogCritical("[OnClientPutInServer] Before check");
|
||||
#endif
|
||||
|
||||
if (player == null || !player.IsValid || player.IsBot || player.IsHLTV)
|
||||
if (player == null || !player.IsValid || player.IsBot || player.IsHLTV || player.Connected == PlayerConnectedState.PlayerDisconnecting)
|
||||
return;
|
||||
|
||||
Console.WriteLine("test");
|
||||
#if DEBUG
|
||||
Logger.LogCritical("[OnClientPutInServer] After Check");
|
||||
#endif
|
||||
|
||||
string? ipAddress = !string.IsNullOrEmpty(player.IpAddress) ? player.IpAddress.Split(":")[0] : null;
|
||||
|
||||
@@ -153,13 +143,15 @@ public partial class CS2_SimpleAdmin
|
||||
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
BanManager _banManager = new(dbConnectionString, Config);
|
||||
if (_database == null) return;
|
||||
|
||||
BanManager _banManager = new(_database, Config);
|
||||
bool isBanned = await _banManager.IsPlayerBanned(playerInfo);
|
||||
|
||||
MuteManager _muteManager = new(dbConnectionString);
|
||||
MuteManager _muteManager = new(_database);
|
||||
List<dynamic> activeMutes = await _muteManager.IsPlayerMuted(playerInfo.SteamId!);
|
||||
|
||||
AdminSQLManager _adminManager = new(dbConnectionString);
|
||||
AdminSQLManager _adminManager = new(_database);
|
||||
List<(List<string>, int)> activeFlags = await _adminManager.GetAdminFlags(playerInfo.SteamId!);
|
||||
|
||||
Server.NextFrame(() =>
|
||||
@@ -212,7 +204,7 @@ public partial class CS2_SimpleAdmin
|
||||
if (TagsDetected)
|
||||
NativeAPI.IssueServerCommand($"css_tag_unmute {player!.SteamID.ToString()}");
|
||||
|
||||
MuteManager _muteManager = new(dbConnectionString);
|
||||
MuteManager _muteManager = new(_database);
|
||||
_ = _muteManager.UnmutePlayer(player!.SteamID.ToString(), 0);
|
||||
}, CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE);
|
||||
}
|
||||
@@ -254,7 +246,7 @@ public partial class CS2_SimpleAdmin
|
||||
|
||||
player.VoiceFlags = VoiceFlags.Normal;
|
||||
|
||||
//MuteManager _muteManager = new(dbConnectionString);
|
||||
//MuteManager _muteManager = new(_database);
|
||||
//_ = _muteManager.UnmutePlayer(player.AuthorizedSteamID.SteamId64.ToString(), 1);
|
||||
}, CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE);
|
||||
}
|
||||
@@ -338,8 +330,14 @@ public partial class CS2_SimpleAdmin
|
||||
private void OnClientDisconnect(int playerSlot)
|
||||
{
|
||||
CCSPlayerController? player = Utilities.GetPlayerFromSlot(playerSlot);
|
||||
#if DEBUG
|
||||
Logger.LogCritical("[OnClientDisconnect] Before");
|
||||
#endif
|
||||
|
||||
if (player == null || !player.IsValid || player.IsBot || player.IsHLTV) return;
|
||||
#if DEBUG
|
||||
Logger.LogCritical("[OnClientDisconnect] After Check");
|
||||
#endif
|
||||
|
||||
if (player != null && player.SteamID.ToString() != "" && gaggedPlayers.Contains(player.SteamID.ToString()))
|
||||
{
|
||||
@@ -386,16 +384,22 @@ public partial class CS2_SimpleAdmin
|
||||
{
|
||||
gaggedPlayers.Clear();
|
||||
|
||||
AdminSQLManager _adminManager = new(dbConnectionString);
|
||||
if (_database == null) return;
|
||||
|
||||
AdminSQLManager _adminManager = new(_database);
|
||||
|
||||
AddTimer(60.0f, bannedPlayers.Clear, CounterStrikeSharp.API.Modules.Timers.TimerFlags.REPEAT | CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE);
|
||||
AddTimer(120.0f, () =>
|
||||
AddTimer(120.0f, async () =>
|
||||
{
|
||||
BanManager _banManager = new(dbConnectionString, Config);
|
||||
MuteManager _muteManager = new(dbConnectionString);
|
||||
_ = _banManager.ExpireOldBans();
|
||||
_ = _muteManager.ExpireOldMutes();
|
||||
_ = _adminManager.DeleteOldAdmins();
|
||||
BanManager _banManager = new(_database, Config);
|
||||
MuteManager _muteManager = new(_database);
|
||||
await _banManager.ExpireOldBans();
|
||||
await _muteManager.ExpireOldMutes();
|
||||
await _adminManager.DeleteOldAdmins();
|
||||
#if DEBUG
|
||||
Logger.LogCritical("[OnMapStart] Expired check");
|
||||
#endif
|
||||
|
||||
}, CounterStrikeSharp.API.Modules.Timers.TimerFlags.REPEAT | CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE);
|
||||
|
||||
string? path = Path.GetDirectoryName(ModuleDirectory);
|
||||
@@ -404,36 +408,37 @@ public partial class CS2_SimpleAdmin
|
||||
TagsDetected = true;
|
||||
}
|
||||
|
||||
AddTimer(2.0f, () =>
|
||||
AddTimer(2.0f, async () =>
|
||||
{
|
||||
using (var connection = new MySqlConnection(dbConnectionString))
|
||||
string? address = $"{ConVar.Find("ip")!.StringValue}:{ConVar.Find("hostport")!.GetPrimitiveValue<int>()}";
|
||||
string? hostname = ConVar.Find("hostname")!.StringValue;
|
||||
|
||||
await Task.Run(async () =>
|
||||
{
|
||||
connection.Open();
|
||||
using (var connection = _database.GetConnection())
|
||||
{
|
||||
await connection.ExecuteAsync(
|
||||
"INSERT INTO `sa_servers` (address, hostname) VALUES (@address, @hostname) " +
|
||||
"ON DUPLICATE KEY UPDATE hostname = @hostname",
|
||||
new { address = $"{address}", hostname });
|
||||
|
||||
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 = await connection.ExecuteScalarAsync<int>(
|
||||
"SELECT `id` FROM `sa_servers` WHERE `address` = @address",
|
||||
new { address = $"{address}" });
|
||||
|
||||
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;
|
||||
|
||||
connection.Close();
|
||||
|
||||
_ = _adminManager.GiveAllFlags();
|
||||
}
|
||||
});
|
||||
ServerId = serverId;
|
||||
|
||||
await _adminManager.GiveAllFlags();
|
||||
}
|
||||
});
|
||||
}, CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE);
|
||||
}
|
||||
|
||||
private HookResult OnPlayerHurt(EventPlayerHurt @event, GameEventInfo info)
|
||||
{
|
||||
CCSPlayerController? player = @event.Userid;
|
||||
|
||||
if (player == null || !player.IsValid)
|
||||
if (player == null || !player.IsValid || player.IsBot || player.IsHLTV || player.Connected == PlayerConnectedState.PlayerDisconnecting)
|
||||
return HookResult.Continue;
|
||||
|
||||
if (player.UserId != null && godPlayers.Contains((ushort)player.UserId) && player.PawnIsAlive)
|
||||
|
||||
@@ -59,7 +59,7 @@ namespace CS2_SimpleAdmin
|
||||
}
|
||||
|
||||
//Console.WriteLine($"Setting immunity for SteamID {steamid} to {immunity}");
|
||||
AdminManager.SetPlayerImmunity(steamid, (uint)immunity);
|
||||
|
||||
|
||||
if (flags != null)
|
||||
{
|
||||
@@ -81,6 +81,7 @@ namespace CS2_SimpleAdmin
|
||||
}
|
||||
}
|
||||
}
|
||||
AdminManager.SetPlayerImmunity(steamid, (uint)immunity);
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
|
||||
@@ -1,25 +1,22 @@
|
||||
using CounterStrikeSharp.API.Core;
|
||||
using Dapper;
|
||||
using MySqlConnector;
|
||||
using System.Data;
|
||||
|
||||
namespace CS2_SimpleAdmin
|
||||
{
|
||||
internal class MuteManager
|
||||
{
|
||||
private readonly MySqlConnection _dbConnection;
|
||||
private readonly Database _database;
|
||||
|
||||
public MuteManager(string connectionString)
|
||||
public MuteManager(Database database)
|
||||
{
|
||||
_dbConnection = new MySqlConnection(connectionString);
|
||||
_database = database;
|
||||
}
|
||||
|
||||
public async Task MutePlayer(PlayerInfo player, PlayerInfo issuer, string reason, int time = 0, int type = 0)
|
||||
{
|
||||
if (player == null || player.SteamId == null) return;
|
||||
|
||||
await using var connection = _dbConnection;
|
||||
await connection.OpenAsync();
|
||||
await using var connection = _database.GetConnection();
|
||||
|
||||
DateTime now = DateTime.Now;
|
||||
DateTime futureTime = now.AddMinutes(time);
|
||||
@@ -44,16 +41,13 @@ namespace CS2_SimpleAdmin
|
||||
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)
|
||||
{
|
||||
if (string.IsNullOrEmpty(playerSteamId)) return;
|
||||
|
||||
await using var connection = _dbConnection;
|
||||
await connection.OpenAsync();
|
||||
await using var connection = _database.GetConnection();
|
||||
|
||||
DateTime now = DateTime.Now;
|
||||
DateTime futureTime = now.AddMinutes(time);
|
||||
@@ -77,37 +71,29 @@ namespace CS2_SimpleAdmin
|
||||
type = muteType,
|
||||
serverid = CS2_SimpleAdmin.ServerId
|
||||
});
|
||||
|
||||
await connection.CloseAsync();
|
||||
}
|
||||
|
||||
public async Task<List<dynamic>> IsPlayerMuted(string steamId)
|
||||
{
|
||||
await using var connection = _dbConnection;
|
||||
await connection.OpenAsync();
|
||||
await using var connection = _database.GetConnection();
|
||||
|
||||
DateTime now = DateTime.Now;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public async Task<int> GetPlayerMutes(string steamId)
|
||||
{
|
||||
await using var connection = _dbConnection;
|
||||
await connection.OpenAsync();
|
||||
await using var connection = _database.GetConnection();
|
||||
|
||||
int muteCount;
|
||||
string sql = "SELECT COUNT(*) FROM sa_mutes WHERE player_steamid = @PlayerSteamID";
|
||||
|
||||
muteCount = await connection.ExecuteScalarAsync<int>(sql, new { PlayerSteamID = steamId });
|
||||
|
||||
await connection.CloseAsync();
|
||||
|
||||
return muteCount;
|
||||
}
|
||||
|
||||
@@ -118,19 +104,13 @@ namespace CS2_SimpleAdmin
|
||||
return;
|
||||
}
|
||||
|
||||
await using var connection = _dbConnection;
|
||||
await connection.OpenAsync();
|
||||
await using var connection = _database.GetConnection();
|
||||
|
||||
if (type == 2)
|
||||
{
|
||||
string _unbanSql = "UPDATE sa_mutes SET status = 'UNMUTED' WHERE (player_steamid = @pattern OR player_name = @pattern) AND status = 'ACTIVE'";
|
||||
await connection.ExecuteAsync(_unbanSql, new { pattern = playerPattern });
|
||||
|
||||
if (connection.State != ConnectionState.Closed)
|
||||
{
|
||||
connection.Close();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -142,19 +122,14 @@ namespace CS2_SimpleAdmin
|
||||
|
||||
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()
|
||||
{
|
||||
await using var connection = _dbConnection;
|
||||
await connection.OpenAsync();
|
||||
await using var connection = _database.GetConnection();
|
||||
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user