Skip to content

Commit

Permalink
1.2.4a
Browse files Browse the repository at this point in the history
- mysql admins
- minor changes
  • Loading branch information
daffyyyy committed Dec 18, 2023
1 parent 270e3bd commit f95031a
Show file tree
Hide file tree
Showing 4 changed files with 251 additions and 26 deletions.
79 changes: 79 additions & 0 deletions AdminSQLManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using Dapper;
using MySqlConnector;
using System.Collections.Generic;

namespace CS2_SimpleAdmin
{
internal class AdminSQLManager
{
private readonly MySqlConnection _dbConnection;

public AdminSQLManager(string connectionString)
{
_dbConnection = new MySqlConnection(connectionString);
}

public async Task<List<dynamic>> GetAdminFlags(string steamId)
{
await using var connection = _dbConnection;
await connection.OpenAsync();

DateTime now = DateTime.Now;

string sql = "SELECT flags FROM sa_admins WHERE player_steamid = @PlayerSteamID AND (ends IS NULL OR ends > @CurrentTime)";
List<dynamic> activeFlags = (await connection.QueryAsync(sql, new { PlayerSteamID = steamId, CurrentTime = now })).ToList();

return activeFlags;
}

public async Task DeleteAdminBySteamId(string playerSteamId)
{
if (string.IsNullOrEmpty(playerSteamId)) return;

await using var connection = _dbConnection;
await connection.OpenAsync();

string sql = "DELETE FROM sa_admins WHERE player_steamid = @PlayerSteamID";
await connection.ExecuteAsync(sql, new { PlayerSteamID = playerSteamId });
}

public async Task AddAdminBySteamId(string playerSteamId, string playerName, string flags, int immunity = 0, int time = 0)
{
if (string.IsNullOrEmpty(playerSteamId)) return;

flags = flags.Replace(" ", "");

DateTime now = DateTime.Now;
DateTime? futureTime;
if (time != 0)
futureTime = now.AddMinutes(time);
else
futureTime = null;

await using var connection = _dbConnection;
await connection.OpenAsync();

var sql = "INSERT INTO `sa_admins` (`player_steamid`, `player_name`, `flags`, `immunity`, `ends`, `created`) " +
"VALUES (@playerSteamid, @playerName, @flags, @immunity, @ends, @created)";

await connection.ExecuteAsync(sql, new
{
playerSteamId,
playerName,
flags,
immunity,
ends = futureTime,
created = now
});
}

public async Task DeleteOldAdmins()
{
await using var connection = _dbConnection;
await connection.OpenAsync();

string sql = "DELETE FROM sa_admins WHERE ends IS NOT NULL AND ends <= @CurrentTime";
await connection.ExecuteAsync(sql, new { CurrentTime = DateTime.Now });
}
}
}
161 changes: 135 additions & 26 deletions CS2-SimpleAdmin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
using System.Text;

namespace CS2_SimpleAdmin;
[MinimumApiVersion(126)]
[MinimumApiVersion(124)]
public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig<CS2_SimpleAdminConfig>
{
public static IStringLocalizer? _localizer;
Expand All @@ -29,9 +29,9 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig<CS2_SimpleAdmin

internal string dbConnectionString = string.Empty;
public override string ModuleName => "CS2-SimpleAdmin";
public override string ModuleDescription => "";
public override string ModuleDescription => "Simple admin plugin for Counter-Strike 2 :)";
public override string ModuleAuthor => "daffyy";
public override string ModuleVersion => "1.2.3a";
public override string ModuleVersion => "1.2.4a";

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

Expand Down Expand Up @@ -104,6 +104,20 @@ PRIMARY KEY (`id`)
command = new MySqlCommand(sql, connection);
command.ExecuteNonQuery();

sql = @"CREATE TABLE IF NOT EXISTS `sa_admins` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`player_steamid` varchar(64) NOT NULL,
`player_name` varchar(128) NOT NULL,
`flags` TEXT,
`immunity` varchar(64) NOT NULL DEFAULT '0',
`ends` timestamp,
`created` timestamp NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci";

command = new MySqlCommand(sql, connection);
command.ExecuteNonQuery();

connection.Close();
}
}
Expand Down Expand Up @@ -132,6 +146,60 @@ public void OnAdminCommand(CCSPlayerController? caller, CommandInfo command)
}
}

[ConsoleCommand("css_addadmin")]
[CommandHelper(minArgs: 4, usage: "<steamid> <name> <flags/groups> <immunity> <duration>", whoCanExecute: CommandUsage.CLIENT_AND_SERVER)]
[RequiresPermissions("@css/root")]
public void OnAddAdminCommand(CCSPlayerController? caller, CommandInfo command)
{
if (!Helper.IsValidSteamID64(command.GetArg(1)))
{
command.ReplyToCommand($"Invalid SteamID64.");
return;
}
if (command.GetArg(2).Length <= 0)
{
command.ReplyToCommand($"Invalid player name.");
return;
}
if (!command.GetArg(3).Contains("@") && !command.GetArg(3).Contains("#"))
{
command.ReplyToCommand($"Invalid player name.");
return;
}

string steamid = command.GetArg(1);
string name = command.GetArg(2);
string flags = command.GetArg(3);
int immunity = 0;
int.TryParse(command.GetArg(4), out immunity);
int time = 0;
int.TryParse(command.GetArg(5), out time);

AdminSQLManager _adminManager = new(dbConnectionString);
_ = _adminManager.AddAdminBySteamId(steamid, name, flags, immunity, time);

command.ReplyToCommand($"Added '{flags}' flags to '{name}' ({steamid})");
}

[ConsoleCommand("css_deladmin")]
[CommandHelper(minArgs: 1, usage: "<steamid>", whoCanExecute: CommandUsage.CLIENT_AND_SERVER)]
[RequiresPermissions("@css/root")]
public void OnDelAdminCommand(CCSPlayerController? caller, CommandInfo command)
{
if (!Helper.IsValidSteamID64(command.GetArg(1)))
{
command.ReplyToCommand($"Invalid SteamID64.");
return;
}

string steamid = command.GetArg(1);

AdminSQLManager _adminManager = new(dbConnectionString);
_ = _adminManager.DeleteAdminBySteamId(steamid);

command.ReplyToCommand($"Removed flags from '{steamid}'");
}

[ConsoleCommand("css_who")]
[CommandHelper(minArgs: 1, usage: "<#userid or name>", whoCanExecute: CommandUsage.CLIENT_AND_SERVER)]
[RequiresPermissions("@css/generic")]
Expand Down Expand Up @@ -163,30 +231,56 @@ public void OnWhoCommand(CCSPlayerController? caller, CommandInfo command)
totalBans = await _banManager.GetPlayerBans(playerInfo);
totalMutes = await _muteManager.GetPlayerMutes(playerInfo.SteamId!);


Server.NextFrame(() =>
{
caller!.PrintToConsole($"--------- INFO ABOUT \"{playerInfo.Name}\" ---------");

caller!.PrintToConsole($"• Clan: \"{player!.Clan}\" Name: \"{playerInfo.Name}\"");
caller!.PrintToConsole($"• UserID: \"{playerInfo.UserId}\"");
if (playerInfo.SteamId != null)
caller!.PrintToConsole($"• SteamID64: \"{playerInfo.SteamId}\"");
if (player.AuthorizedSteamID != null)
if (caller != null)
{
caller!.PrintToConsole($"• SteamID2: \"{player.AuthorizedSteamID.SteamId2}\"");
caller!.PrintToConsole($"• Community link: \"{player.AuthorizedSteamID.ToCommunityUrl()}\"");
caller!.PrintToConsole($"--------- INFO ABOUT \"{playerInfo.Name}\" ---------");

caller!.PrintToConsole($"• Clan: \"{player!.Clan}\" Name: \"{playerInfo.Name}\"");
caller!.PrintToConsole($"• UserID: \"{playerInfo.UserId}\"");
if (playerInfo.SteamId != null)
caller!.PrintToConsole($"• SteamID64: \"{playerInfo.SteamId}\"");
if (player.AuthorizedSteamID != null)
{
caller!.PrintToConsole($"• SteamID2: \"{player.AuthorizedSteamID.SteamId2}\"");
caller!.PrintToConsole($"• Community link: \"{player.AuthorizedSteamID.ToCommunityUrl()}\"");
}
if (playerInfo.IpAddress != null)
caller!.PrintToConsole($"• IP Address: \"{playerInfo.IpAddress}\"");
caller!.PrintToConsole($"• Ping: \"{player.Ping}\"");
if (player.AuthorizedSteamID != null)
{
caller!.PrintToConsole($"• Total Bans: \"{totalBans}\"");
caller!.PrintToConsole($"• Total Mutes: \"{totalMutes}\"");
}

caller!.PrintToConsole($"--------- END INFO ABOUT \"{player.PlayerName}\" ---------");
}
if (playerInfo.IpAddress != null)
caller!.PrintToConsole($"• IP Address: \"{playerInfo.IpAddress}\"");
caller!.PrintToConsole($"• Ping: \"{player.Ping}\"");
if (player.AuthorizedSteamID != null)
else
{
caller!.PrintToConsole($"• Total Bans: \"{totalBans}\"");
caller!.PrintToConsole($"• Total Mutes: \"{totalMutes}\"");
}
Server.PrintToConsole($"--------- INFO ABOUT \"{playerInfo.Name}\" ---------");

caller!.PrintToConsole($"--------- END INFO ABOUT \"{player.PlayerName}\" ---------");
Server.PrintToConsole($"• Clan: \"{player!.Clan}\" Name: \"{playerInfo.Name}\"");
Server.PrintToConsole($"• UserID: \"{playerInfo.UserId}\"");
if (playerInfo.SteamId != null)
Server.PrintToConsole($"• SteamID64: \"{playerInfo.SteamId}\"");
if (player.AuthorizedSteamID != null)
{
Server.PrintToConsole($"• SteamID2: \"{player.AuthorizedSteamID.SteamId2}\"");
Server.PrintToConsole($"• Community link: \"{player.AuthorizedSteamID.ToCommunityUrl()}\"");
}
if (playerInfo.IpAddress != null)
Server.PrintToConsole($"• IP Address: \"{playerInfo.IpAddress}\"");
Server.PrintToConsole($"• Ping: \"{player.Ping}\"");
if (player.AuthorizedSteamID != null)
{
Server.PrintToConsole($"• Total Bans: \"{totalBans}\"");
Server.PrintToConsole($"• Total Mutes: \"{totalMutes}\"");
}

Server.PrintToConsole($"--------- END INFO ABOUT \"{player.PlayerName}\" ---------");
}
});
});

Expand All @@ -202,13 +296,26 @@ public void OnPlayersCommand(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();

caller!.PrintToConsole($"--------- PLAYER LIST ---------");
playersToTarget.ForEach(player =>
if (caller != null)
{
caller!.PrintToConsole($"• [#{player.UserId}] \"{player.PlayerName}\" (IP Address: \"{player.IpAddress?.Split(":")[0]}\" SteamID64: \"{player.AuthorizedSteamID?.SteamId64}\")");
caller!.PrintToConsole($"--------- PLAYER LIST ---------");
playersToTarget.ForEach(player =>
{
caller!.PrintToConsole($"• [#{player.UserId}] \"{player.PlayerName}\" (IP Address: \"{player.IpAddress?.Split(":")[0]}\" SteamID64: \"{player.AuthorizedSteamID?.SteamId64}\")");

});
caller!.PrintToConsole($"--------- END PLAYER LIST ---------");
});
caller!.PrintToConsole($"--------- END PLAYER LIST ---------");
}
else
{
Server.PrintToConsole($"--------- PLAYER LIST ---------");
playersToTarget.ForEach(player =>
{
Server.PrintToConsole($"• [#{player.UserId}] \"{player.PlayerName}\" (IP Address: \"{player.IpAddress?.Split(":")[0]}\" SteamID64: \"{player.AuthorizedSteamID?.SteamId64}\")");

});
Server.PrintToConsole($"--------- END PLAYER LIST ---------");
}
}

[ConsoleCommand("css_kick")]
Expand Down Expand Up @@ -1130,6 +1237,7 @@ public void OnStripCommand(CCSPlayerController? caller, CommandInfo command)
});
}


[ConsoleCommand("css_hp")]
[RequiresPermissions("@css/slay")]
[CommandHelper(minArgs: 1, usage: "<#userid or name> <health>", whoCanExecute: CommandUsage.CLIENT_AND_SERVER)]
Expand Down Expand Up @@ -1288,6 +1396,7 @@ public void OnTeamCommand(CCSPlayerController? caller, CommandInfo command)
});
}


[ConsoleCommand("css_vote")]
[RequiresPermissions("@css/generic")]
[CommandHelper(minArgs: 2, usage: "<question> [... options ...]", whoCanExecute: CommandUsage.CLIENT_AND_SERVER)]
Expand Down
35 changes: 35 additions & 0 deletions Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ private void OnClientAuthorized(int playerSlot, SteamID steamID)
MuteManager _muteManager = new(dbConnectionString);
List<dynamic> activeMutes = await _muteManager.IsPlayerMuted(playerInfo.SteamId!);

AdminSQLManager _adminManager = new(dbConnectionString);
List<dynamic> activeFlags = await _adminManager.GetAdminFlags(playerInfo.SteamId!);

Server.NextFrame(() =>
{
if (player == null || !player.IsValid) return;
Expand Down Expand Up @@ -218,6 +221,36 @@ private void OnClientAuthorized(int playerSlot, SteamID steamID)
}
}
}

if (activeFlags != null && activeFlags.Count > 0)
{
foreach (var flags in activeFlags)
{
if (flags == null) continue;
string flagsValue = flags.flags.ToString();

if (!string.IsNullOrEmpty(flagsValue))
{
string[] _flags = flagsValue.Split(",");

AddTimer(10, () =>
{
if (player == null) return;
foreach (var _flag in _flags)
{
if (_flag.StartsWith("@"))
{
AdminManager.AddPlayerPermissions(player, _flag);
}
if (_flag.StartsWith("3"))
{
AdminManager.AddPlayerToGroup(player, _flag);
}
}
});
}
}
}
});
});
}
Expand Down Expand Up @@ -261,6 +294,8 @@ private void OnMapStart(string mapName)
_ = _banManager.ExpireOldBans();
MuteManager _muteManager = new(dbConnectionString);
_ = _muteManager.ExpireOldMutes();
AdminSQLManager _adminManager = new(dbConnectionString);
_ = _adminManager.DeleteOldAdmins();
}, CounterStrikeSharp.API.Modules.Timers.TimerFlags.REPEAT | CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE);

string? path = Path.GetDirectoryName(ModuleDirectory);
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Manage your Counter-Strike 2 server by simple commands :)

### Commands
```js
- css_addadmin <steamid> <name> <flags/groups> <immunity> [time in minutes] - Add admin by steamid // @css/root
- css_deladmin <steamid> - Delete admin by steamid // @css/root
- css_admin - Display all admin commands // @css/generic
- css_who <#userid or name> - Display informations about player // @css/generic
- css_players - Display player list // @css/generic
Expand Down

0 comments on commit f95031a

Please sign in to comment.