Skip to content

Commit

Permalink
Update 1.2.6a
Browse files Browse the repository at this point in the history
- Added server_id
- Added immunity
  • Loading branch information
daffyyyy committed Jan 8, 2024
1 parent cfe7910 commit dd5e72e
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 42 deletions.
39 changes: 33 additions & 6 deletions AdminSQLManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public async Task<List<dynamic>> GetAdminFlags(string steamId)
}
*/

public async Task<List<object>> GetAdminFlags(string steamId)
public async Task<List<(List<string>, int)>> GetAdminFlags(string steamId)
{
/* Unused for now
if (_adminCache.TryGetValue(steamId, out ConcurrentBag<string>? cachedFlags))
Expand All @@ -56,14 +56,17 @@ public async Task<List<object>> GetAdminFlags(string steamId)
await using var connection = _dbConnection;
await connection.OpenAsync();

string sql = "SELECT flags, ends 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();
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();

if (activeFlags == null)
{
return new List<object>();
return new List<(List<string>, int)>();
}

List<(List<string>, int)> filteredFlagsWithImmunity = new List<(List<string>, int)>();

/*
List<string> filteredFlags = new List<string>();
foreach (var flags in activeFlags)
Expand All @@ -76,6 +79,30 @@ public async Task<List<object>> GetAdminFlags(string steamId)
filteredFlags.Add(flag);
}
}
*/

foreach (dynamic flags in activeFlags)
{
if (flags is not IDictionary<string, object> flagsDict)
{
continue;
}

if (!flagsDict.TryGetValue("flags", out var flagsValueObj) || !flagsDict.TryGetValue("immunity", out var immunityValueObj))
{
continue;
}

if (!(flagsValueObj is string flagsValue) || !int.TryParse(immunityValueObj.ToString(), out var immunityValue))
{
continue;
}

//Console.WriteLine($"Flags: {flagsValue}, Immunity: {immunityValue}");

filteredFlagsWithImmunity.Add((flagsValue.Split(',').ToList(), immunityValue));
}


/* Unused for now
bool shouldCache = activeFlags.Any(flags =>
Expand Down Expand Up @@ -117,8 +144,8 @@ public async Task<List<object>> GetAdminFlags(string steamId)
return flagsToCache.Cast<object>().ToList();
}
*/

return filteredFlags.Cast<object>().ToList();
return filteredFlagsWithImmunity;
//return filteredFlags.Cast<object>().ToList();
}

public async Task DeleteAdminBySteamId(string playerSteamId)
Expand Down
21 changes: 12 additions & 9 deletions BanManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public async Task BanPlayer(PlayerInfo player, PlayerInfo issuer, string reason,
await using var connection = _dbConnection;
await connection.OpenAsync();

var sql = "INSERT INTO `sa_bans` (`player_steamid`, `player_name`, `player_ip`, `admin_steamid`, `admin_name`, `reason`, `duration`, `ends`, `created`) " +
"VALUES (@playerSteamid, @playerName, @playerIp, @adminSteamid, @adminName, @banReason, @duration, @ends, @created)";
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)";

await connection.ExecuteAsync(sql, new
{
Expand All @@ -33,7 +33,8 @@ public async Task BanPlayer(PlayerInfo player, PlayerInfo issuer, string reason,
banReason = reason,
duration = time,
ends = futureTime,
created = now
created = now,
serverid = CS2_SimpleAdmin.ServerId
});
}

Expand All @@ -47,8 +48,8 @@ public async Task AddBanBySteamid(string playerSteamId, PlayerInfo issuer, strin
await using var connection = _dbConnection;
await connection.OpenAsync();

var sql = "INSERT INTO `sa_bans` (`player_steamid`, `admin_steamid`, `admin_name`, `reason`, `duration`, `ends`, `created`) " +
"VALUES (@playerSteamid, @adminSteamid, @adminName, @banReason, @duration, @ends, @created)";
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)";

await connection.ExecuteAsync(sql, new
{
Expand All @@ -58,7 +59,8 @@ public async Task AddBanBySteamid(string playerSteamId, PlayerInfo issuer, strin
banReason = reason,
duration = time,
ends = futureTime,
created = now
created = now,
serverid = CS2_SimpleAdmin.ServerId
});
}

Expand All @@ -72,8 +74,8 @@ public async Task AddBanByIp(string playerIp, PlayerInfo issuer, string reason,
await using var connection = _dbConnection;
await connection.OpenAsync();

var sql = "INSERT INTO `sa_bans` (`player_ip`, `admin_steamid`, `admin_name`, `reason`, `duration`, `ends`, `created`) " +
"VALUES (@playerIp, @adminSteamid, @adminName, @banReason, @duration, @ends, @created)";
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)";

await connection.ExecuteAsync(sql, new
{
Expand All @@ -83,7 +85,8 @@ public async Task AddBanByIp(string playerIp, PlayerInfo issuer, string reason,
banReason = reason,
duration = time,
ends = futureTime,
created = now
created = now,
serverid = CS2_SimpleAdmin.ServerId
});
}

Expand Down
10 changes: 7 additions & 3 deletions CS2-SimpleAdmin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig<CS2_SimpleAdmin
public static List<int> GodPlayers = new List<int>();
public static bool TagsDetected = false;
public static bool voteInProgress = false;
public static int? ServerId = null;

internal string dbConnectionString = string.Empty;
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.5a";
public override string ModuleVersion => "1.2.6a";

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

Expand Down Expand Up @@ -81,6 +82,7 @@ public void OnConfigParsed(CS2_SimpleAdminConfig config)
`duration` INT NOT NULL,
`ends` TIMESTAMP NOT NULL,
`created` TIMESTAMP NOT NULL,
`server_id` INT NULL,
`status` ENUM('ACTIVE', 'UNBANNED', 'EXPIRED', '') NOT NULL DEFAULT 'ACTIVE'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;";

Expand All @@ -98,6 +100,7 @@ public void OnConfigParsed(CS2_SimpleAdminConfig config)
`ends` timestamp NOT NULL,
`created` timestamp NOT NULL,
`type` enum('GAG','MUTE','') NOT NULL DEFAULT 'GAG',
`server_id` INT NULL,
`status` enum('ACTIVE','UNMUTED','EXPIRED','') NOT NULL DEFAULT 'ACTIVE',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci";
Expand All @@ -109,9 +112,10 @@ PRIMARY KEY (`id`)
`id` int(11) NOT NULL AUTO_INCREMENT,
`player_steamid` varchar(64) NOT NULL,
`player_name` varchar(128) NOT NULL,
`flags` TEXT,
`flags` TEXT NOT NULL,
`immunity` varchar(64) NOT NULL DEFAULT '0',
`ends` timestamp,
`server_id` INT NULL,
`ends` timestamp NULL,
`created` timestamp NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci";
Expand Down
26 changes: 24 additions & 2 deletions Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Admin;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Cvars;
using CounterStrikeSharp.API.Modules.Entities;
using Dapper;
using MySqlConnector;
using System.Text;
using static CounterStrikeSharp.API.Core.Listeners;

Expand Down Expand Up @@ -139,14 +142,15 @@ private void OnClientAuthorized(int playerSlot, SteamID steamID)

Task.Run(async () =>
{

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

MuteManager _muteManager = new(dbConnectionString);
List<dynamic> activeMutes = await _muteManager.IsPlayerMuted(playerInfo.SteamId!);

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

Server.NextFrame(() =>
{
Expand Down Expand Up @@ -239,7 +243,7 @@ private void OnClientAuthorized(int playerSlot, SteamID steamID)
}
}

AddTimer(14, () => Helper.GivePlayerFlags(player, activeFlags));
AddTimer(6, () => Helper.GivePlayerFlags(player, activeFlags));

/*
Expand Down Expand Up @@ -333,6 +337,24 @@ private void OnMapStart(string mapName)
{
TagsDetected = true;
}

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 });

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();
}
}

private HookResult OnPlayerHurt(EventPlayerHurt @event, GameEventInfo info)
Expand Down
31 changes: 14 additions & 17 deletions Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,33 +47,30 @@ public static bool IsValidIP(string input)
return Regex.IsMatch(input, pattern);
}

public static void GivePlayerFlags(CCSPlayerController player, dynamic flags)
public static void GivePlayerFlags(CCSPlayerController player, List<(List<string>, int)> flagsWithImmunity)
{
if (player == null) return;

foreach (var flagsValue in flags)
foreach (var (flags, immunity) in flagsWithImmunity)
{
if (!string.IsNullOrEmpty(flagsValue))
{
string[] _flags = flagsValue.Split(',');
Console.WriteLine(flagsValue);
AdminManager.SetPlayerImmunity(player, (uint)immunity);

foreach (var _flag in _flags)
foreach (var flag in flags)
{
if (!string.IsNullOrEmpty(flag))
{
Server.NextFrame(() =>
if (flag.StartsWith("@"))
{
if (_flag.StartsWith("@"))
{
AdminManager.AddPlayerPermissions(player, _flag);
}
if (_flag.StartsWith("#"))
{
AdminManager.AddPlayerToGroup(player, _flag);
}
});
AdminManager.AddPlayerPermissions(player, flag);
}
else if (flag.StartsWith("#"))
{
AdminManager.AddPlayerToGroup(player, flag);
}
}
}
}

}

/*
Expand Down
12 changes: 7 additions & 5 deletions MuteManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public async Task MutePlayer(PlayerInfo player, PlayerInfo issuer, string reason
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)";
var sql = "INSERT INTO `sa_mutes` (`player_steamid`, `player_name`, `admin_steamid`, `admin_name`, `reason`, `duration`, `ends`, `created`, `type`, `server_id`) " +
"VALUES (@playerSteamid, @playerName, @adminSteamid, @adminName, @banReason, @duration, @ends, @created, @type, @serverid)";

await connection.ExecuteAsync(sql, new
{
Expand All @@ -42,6 +42,7 @@ public async Task MutePlayer(PlayerInfo player, PlayerInfo issuer, string reason
ends = futureTime,
created = now,
type = muteType,
serverid = CS2_SimpleAdmin.ServerId
});
}

Expand All @@ -59,8 +60,8 @@ public async Task AddMuteBySteamid(string playerSteamId, PlayerInfo issuer, stri
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)";
var sql = "INSERT INTO `sa_mutes` (`player_steamid`, `admin_steamid`, `admin_name`, `reason`, `duration`, `ends`, `created`, `type`, `server_id`) " +
"VALUES (@playerSteamid, @adminSteamid, @adminName, @banReason, @duration, @ends, @created, @type, @serverid)";

await connection.ExecuteAsync(sql, new
{
Expand All @@ -71,7 +72,8 @@ public async Task AddMuteBySteamid(string playerSteamId, PlayerInfo issuer, stri
duration = time,
ends = futureTime,
created = now,
type = muteType
type = muteType,
serverid = CS2_SimpleAdmin.ServerId
});
}

Expand Down

0 comments on commit dd5e72e

Please sign in to comment.