Skip to content

Commit

Permalink
Database connection exception fix, additional ip check (problem with …
Browse files Browse the repository at this point in the history
…steam auth)
  • Loading branch information
daffyyyy committed Jan 9, 2024
1 parent f78cd88 commit 71af900
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
5 changes: 3 additions & 2 deletions CS2-SimpleAdmin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig<CS2_SimpleAdmin
public static IStringLocalizer? _localizer;
public static ConcurrentBag<int> gaggedPlayers = new ConcurrentBag<int>();
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 List<int> GodPlayers = new List<int>();
public static bool TagsDetected = false;
Expand All @@ -33,7 +34,7 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig<CS2_SimpleAdmin
public override string ModuleName => "CS2-SimpleAdmin";
public override string ModuleDescription => "Simple admin plugin for Counter-Strike 2 :)";
public override string ModuleAuthor => "daffyy";
public override string ModuleVersion => "1.2.6b";
public override string ModuleVersion => "1.2.6c";

public CS2_SimpleAdminConfig Config { get; set; } = new();

Expand Down Expand Up @@ -123,7 +124,7 @@ PRIMARY KEY (`id`)
command = new MySqlCommand(sql, connection);
command.ExecuteNonQuery();

sql = @"CREATE TABLE `sa_servers` (
sql = @"CREATE TABLE IF NOT EXISTS `sa_servers` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`address` varchar(64) NOT NULL,
`hostname` varchar(64) NOT NULL,
Expand Down
41 changes: 40 additions & 1 deletion Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public partial class CS2_SimpleAdmin
private void registerEvents()
{
RegisterListener<OnClientAuthorized>(OnClientAuthorized);
RegisterListener<OnClientConnected>(OnClientConnected);
//RegisterEventHandler<EventPlayerConnectFull>(OnPlayerFullConnect);
RegisterListener<OnClientDisconnect>(OnClientDisconnect);
RegisterListener<OnMapStart>(OnMapStart);
Expand Down Expand Up @@ -124,6 +125,38 @@ private HookResult OnCommandCallVote(CCSPlayerController? player, CommandInfo in
return HookResult.Continue;
}

private void OnClientConnected(int playerSlot)
{
CCSPlayerController? player = Utilities.GetPlayerFromSlot(playerSlot);

if (player == null || !player.IsValid || player.IpAddress == null || loadedPlayers.Contains((int)player.Index) || player.IsBot || player.IsHLTV)
return;

PlayerInfo playerInfo = new PlayerInfo
{
UserId = player.UserId,
Index = (int)player.Index,
SteamId = player?.AuthorizedSteamID?.SteamId64.ToString(),
Name = player?.PlayerName,
IpAddress = player?.IpAddress.Split(":")[0]
};

Task.Run(async () =>
{
BanManager _banManager = new(dbConnectionString);
bool isBanned = await _banManager.IsPlayerBanned(playerInfo);
Server.NextFrame(() =>
{
if (player == null || !player.IsValid) return;
if (isBanned)
{
Helper.KickPlayer((ushort)player.UserId!, "Banned");
return;
}
});
});
}

private void OnClientAuthorized(int playerSlot, SteamID steamID)
{
CCSPlayerController? player = Utilities.GetPlayerFromSlot(playerSlot);
Expand All @@ -142,7 +175,6 @@ private void OnClientAuthorized(int playerSlot, SteamID steamID)

Task.Run(async () =>
{

BanManager _banManager = new(dbConnectionString);
bool isBanned = await _banManager.IsPlayerBanned(playerInfo);

Expand All @@ -158,6 +190,7 @@ private void OnClientAuthorized(int playerSlot, SteamID steamID)
if (isBanned)
{
Helper.KickPlayer((ushort)player.UserId!, "Banned");
Console.WriteLine("Authorized banned");
return;
}

Expand Down Expand Up @@ -277,6 +310,9 @@ private void OnClientAuthorized(int playerSlot, SteamID steamID)
}
}
*/

if (!loadedPlayers.Contains((int)player.Index))
loadedPlayers.Add((int)player.Index);
});
});
}
Expand Down Expand Up @@ -308,6 +344,9 @@ private void OnClientDisconnect(int playerSlot)
GodPlayers.Remove((int)player.Index);
}

if (loadedPlayers.Contains((int)player.Index))
loadedPlayers.Remove((int)player.Index);

if (player.AuthorizedSteamID != null)
{
//string steamIdString = player.AuthorizedSteamID.SteamId64.ToString();
Expand Down

0 comments on commit 71af900

Please sign in to comment.