mirror of
https://github.com/daffyyyy/CS2-SimpleAdmin.git
synced 2026-02-19 02:51:51 +00:00
Changed to async, instantly mute in CS2-Tags plugin
- Changed to async - instantly mute in CS2-Tags plugin
This commit is contained in:
130
MuteManager.cs
130
MuteManager.cs
@@ -1,39 +1,37 @@
|
||||
using CounterStrikeSharp.API.Core;
|
||||
using CounterStrikeSharp.API.Modules.Entities;
|
||||
using Dapper;
|
||||
using MySqlConnector;
|
||||
using System.Data;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace CS2_SimpleAdmin
|
||||
{
|
||||
public class MuteManager
|
||||
{
|
||||
private readonly IDbConnection _dbConnection;
|
||||
private readonly MySqlConnection _dbConnection;
|
||||
|
||||
public MuteManager(string connectionString)
|
||||
{
|
||||
_dbConnection = new MySqlConnection(connectionString);
|
||||
}
|
||||
|
||||
public void MutePlayer(CCSPlayerController? player, CCSPlayerController? issuer, string reason, int time = 0, int type = 0)
|
||||
public async Task MutePlayer(CCSPlayerController? player, CCSPlayerController? issuer, string reason, int time = 0, int type = 0)
|
||||
{
|
||||
_dbConnection.Open();
|
||||
|
||||
if (player == null || !player.IsValid || player.AuthorizedSteamID == null) return;
|
||||
|
||||
await using var connection = _dbConnection;
|
||||
await connection.OpenAsync();
|
||||
|
||||
DateTime now = DateTime.Now;
|
||||
DateTime futureTime = now.AddMinutes(time);
|
||||
|
||||
string muteType = "GAG";
|
||||
|
||||
if (type == 1)
|
||||
muteType = "MUTE";
|
||||
|
||||
var sql = "INSERT INTO `sa_mutes` (`player_steamid`, `player_name`, `admin_steamid`, `admin_name`, `reason`, `duration`, `ends`, `created`, `type`) " +
|
||||
"VALUES (@playerSteamid, @playerName, @adminSteamid, @adminName, @banReason, @duration, @ends, @created, @type)";
|
||||
|
||||
_dbConnection.Execute(sql, new
|
||||
await connection.ExecuteAsync(sql, new
|
||||
{
|
||||
playerSteamid = player.AuthorizedSteamID.SteamId64.ToString(),
|
||||
playerName = player.PlayerName,
|
||||
@@ -46,27 +44,30 @@ namespace CS2_SimpleAdmin
|
||||
type = muteType,
|
||||
});
|
||||
|
||||
_dbConnection.Close();
|
||||
if (connection.State != ConnectionState.Closed)
|
||||
{
|
||||
connection.Close();
|
||||
}
|
||||
}
|
||||
|
||||
public void AddMuteBySteamid(string playerSteamId, CCSPlayerController? issuer, string reason, int time = 0, int type = 0)
|
||||
public async Task AddMuteBySteamid(string playerSteamId, CCSPlayerController? issuer, string reason, int time = 0, int type = 0)
|
||||
{
|
||||
if (string.IsNullOrEmpty(playerSteamId)) return;
|
||||
|
||||
_dbConnection.Open();
|
||||
await using var connection = _dbConnection;
|
||||
await connection.OpenAsync();
|
||||
|
||||
DateTime now = DateTime.Now;
|
||||
DateTime futureTime = now.AddMinutes(time);
|
||||
|
||||
string muteType = "GAG";
|
||||
|
||||
if (type == 1)
|
||||
muteType = "MUTE";
|
||||
|
||||
var sql = "INSERT INTO `sa_mutes` (`player_steamid`, `admin_steamid`, `admin_name`, `reason`, `duration`, `ends`, `created`, `type`) " +
|
||||
"VALUES (@playerSteamid, @adminSteamid, @adminName, @banReason, @duration, @ends, @created, @type)";
|
||||
|
||||
_dbConnection.Execute(sql, new
|
||||
await connection.ExecuteAsync(sql, new
|
||||
{
|
||||
playerSteamid = playerSteamId,
|
||||
adminSteamid = issuer == null ? "Console" : issuer?.AuthorizedSteamID?.SteamId64.ToString(),
|
||||
@@ -78,60 +79,125 @@ namespace CS2_SimpleAdmin
|
||||
type = muteType
|
||||
});
|
||||
|
||||
_dbConnection.Close();
|
||||
if (connection.State != ConnectionState.Closed)
|
||||
{
|
||||
connection.Close();
|
||||
}
|
||||
}
|
||||
|
||||
public List<dynamic> IsPlayerMuted(string steamId)
|
||||
public async Task<List<dynamic>> IsPlayerMuted(string steamId)
|
||||
{
|
||||
_dbConnection.Open();
|
||||
await using var connection = _dbConnection;
|
||||
await connection.OpenAsync();
|
||||
|
||||
DateTime now = DateTime.Now;
|
||||
|
||||
string sql = "SELECT * FROM sa_mutes WHERE player_steamid = @PlayerSteamID AND status = 'ACTIVE' AND (duration = 0 OR ends > @CurrentTime)";
|
||||
List<dynamic> activeMutes = _dbConnection.Query<dynamic>(sql, new { PlayerSteamID = steamId, CurrentTime = now }).ToList();
|
||||
var activeMutes = (await connection.QueryAsync(sql, new { PlayerSteamID = steamId, CurrentTime = now })).ToList();
|
||||
|
||||
_dbConnection.Close();
|
||||
if (connection.State != ConnectionState.Closed)
|
||||
{
|
||||
connection.Close();
|
||||
}
|
||||
|
||||
return activeMutes;
|
||||
}
|
||||
|
||||
public void UnmutePlayer(string playerPattern, int type = 0)
|
||||
public async Task UnmutePlayer(string playerPattern, int type = 0)
|
||||
{
|
||||
if (playerPattern == null || playerPattern.Length <= 1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await using var connection = _dbConnection;
|
||||
await connection.OpenAsync();
|
||||
|
||||
if (type == 2)
|
||||
{
|
||||
string _unbanSql = "UPDATE sa_mutes SET status = 'UNMUTED' WHERE (player_steamid = @pattern OR player_name = @pattern) AND status = 'ACTIVE'";
|
||||
_dbConnection.Execute(_unbanSql, new { pattern = playerPattern });
|
||||
_dbConnection.Close();
|
||||
await connection.ExecuteAsync(_unbanSql, new { pattern = playerPattern });
|
||||
|
||||
if (connection.State != ConnectionState.Closed)
|
||||
{
|
||||
connection.Close();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
string muteType = "GAG";
|
||||
|
||||
if (type == 1)
|
||||
{
|
||||
muteType = "MUTE";
|
||||
|
||||
_dbConnection.Open();
|
||||
}
|
||||
|
||||
string sqlUnban = "UPDATE sa_mutes SET status = 'UNMUTED' WHERE (player_steamid = @pattern OR player_name = @pattern) AND type = @muteType AND status = 'ACTIVE'";
|
||||
_dbConnection.Execute(sqlUnban, new { pattern = playerPattern, muteType });
|
||||
_dbConnection.Close();
|
||||
await connection.ExecuteAsync(sqlUnban, new { pattern = playerPattern, muteType });
|
||||
|
||||
if (connection.State != ConnectionState.Closed)
|
||||
{
|
||||
connection.Close();
|
||||
}
|
||||
}
|
||||
|
||||
public void ExpireOldMutes()
|
||||
public async Task ExpireOldMutes()
|
||||
{
|
||||
_dbConnection.Open();
|
||||
string sql = "UPDATE sa_mutes SET status = 'EXPIRED' WHERE status = 'ACTIVE' AND `duration` > 0 AND ends <= @CurrentTime";
|
||||
_dbConnection.Execute(sql, new { CurrentTime = DateTime.Now });
|
||||
//int affectedRows = _dbConnection.Execute(sql, new { CurrentTime = DateTime.Now });
|
||||
await using var connection = _dbConnection;
|
||||
await connection.OpenAsync();
|
||||
|
||||
_dbConnection.Close();
|
||||
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 });
|
||||
|
||||
if (connection.State != ConnectionState.Closed)
|
||||
{
|
||||
connection.Close();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task CheckMute(CCSPlayerController? player)
|
||||
{
|
||||
if (player == null || !player.IsValid || player.AuthorizedSteamID == null) return;
|
||||
|
||||
string steamId = player.AuthorizedSteamID.SteamId64.ToString();
|
||||
List<dynamic> activeMutes = await IsPlayerMuted(steamId);
|
||||
|
||||
if (activeMutes.Count > 0)
|
||||
{
|
||||
foreach (var mute in activeMutes)
|
||||
{
|
||||
string muteType = mute.type;
|
||||
TimeSpan duration = mute.ends - mute.created;
|
||||
int durationInSeconds = (int)duration.TotalSeconds;
|
||||
|
||||
if (muteType == "GAG")
|
||||
{
|
||||
if (!CS2_SimpleAdmin.gaggedPlayers.Contains((int)player.Index))
|
||||
CS2_SimpleAdmin.gaggedPlayers.Add((int)player.Index);
|
||||
|
||||
if (CS2_SimpleAdmin.TagsDetected)
|
||||
NativeAPI.IssueServerCommand($"css_tag_mute {player!.Index.ToString()}");
|
||||
|
||||
CCSPlayerController currentPlayer = player;
|
||||
|
||||
if (mute.duration == 0 || durationInSeconds >= 1800) continue;
|
||||
|
||||
await Task.Delay(TimeSpan.FromSeconds(durationInSeconds));
|
||||
|
||||
if (currentPlayer != null && currentPlayer.IsValid)
|
||||
{
|
||||
NativeAPI.IssueServerCommand($"css_tag_unmute {currentPlayer.Index.ToString()}");
|
||||
await UnmutePlayer(currentPlayer.AuthorizedSteamID.SteamId64.ToString(), 0);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Mic mute
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user