Skip to content

Commit

Permalink
1.4.4a
Browse files Browse the repository at this point in the history
- MultiServerMode fix
- New feature `TimeMode`
  • Loading branch information
daffyyyy committed May 6, 2024
1 parent d5a6cea commit 4244104
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 70 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v3
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x
- name: Restore
Expand All @@ -38,9 +38,9 @@ jobs:
runs-on: ubuntu-latest
needs: build
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v3
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x
- name: Restore
Expand All @@ -55,13 +55,13 @@ jobs:
${{ env.OUTPUT_PATH }}/Microsoft.DotNet.PlatformAbstractions.dll \
${{ env.OUTPUT_PATH }}/Microsoft.Extensions.DependencyModel.dll \
- name: Zip
uses: thedoctor0/[email protected].5
uses: thedoctor0/[email protected].6
with:
type: 'zip'
filename: '${{ env.PROJECT_NAME }}.zip'
path: ${{ env.OUTPUT_PATH }}
- name: CS2-SimpleAdmin
uses: ncipollo/release-action@v1.12.0
uses: ncipollo/release-action@v1.14.0
with:
artifacts: "${{ env.PROJECT_NAME }}.zip"
name: "Build ${{ env.BUILD_NUMBER }}"
Expand Down
4 changes: 2 additions & 2 deletions CS2-SimpleAdmin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace CS2_SimpleAdmin;

[MinimumApiVersion(220)]
[MinimumApiVersion(225)]
public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig<CS2_SimpleAdminConfig>
{
public static CS2_SimpleAdmin Instance { get; private set; } = new();
Expand All @@ -37,7 +37,7 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig<CS2_SimpleAdmin
public override string ModuleName => "CS2-SimpleAdmin" + (Helper.IsDebugBuild ? " (DEBUG)" : " (RELEASE)");
public override string ModuleDescription => "Simple admin plugin for Counter-Strike 2 :)";
public override string ModuleAuthor => "daffyy & Dliix66";
public override string ModuleVersion => "1.4.3d";
public override string ModuleVersion => "1.4.4a";

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

Expand Down
2 changes: 1 addition & 1 deletion CS2-SimpleAdmin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.225" />
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.228" />
<PackageReference Include="Dapper" Version="2.1.35" />
<PackageReference Include="Discord.Net.Webhook" Version="3.14.1" />
<PackageReference Include="MySqlConnector" Version="2.3.7" />
Expand Down
3 changes: 3 additions & 0 deletions Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ public class CS2_SimpleAdminConfig : BasePluginConfig
[JsonPropertyName("BanType")]
public int BanType { get; set; } = 1;

[JsonPropertyName("TimeMode")]
public int TimeMode { get; set; } = 1;

[JsonPropertyName("MaxBanDuration")]
public int MaxBanDuration { get; set; } = 60 * 24 * 7; // 7 days
[JsonPropertyName("MultiServerMode")]
Expand Down
16 changes: 11 additions & 5 deletions Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ await Server.NextFrameAsync(() =>
string muteType = mute.type;
DateTime ends = mute.ends;
int duration = mute.duration;

switch (muteType)
{
// Apply mute penalty based on mute type
Expand Down Expand Up @@ -275,7 +274,7 @@ public void OnMapStart(string mapName)
{
var ipAddress = ConVar.Find("ip")?.StringValue;

if (string.IsNullOrEmpty(ipAddress))
if (string.IsNullOrEmpty(ipAddress) || ipAddress.StartsWith("0.0.0"))
{
Logger.LogError("Unable to get server ip, Check that you have added the correct start parameter \"-ip <ip>\"");
}
Expand Down Expand Up @@ -352,8 +351,8 @@ await Server.NextFrameAsync(() =>

var players = Helper.GetValidPlayers();
var onlinePlayers = players
.Where(player => player.IpAddress != null && player.SteamID.ToString().Length == 17)
.Select(player => (player.IpAddress, player.SteamID, player.UserId))
.Where(player => player.IpAddress != null)
.Select(player => (player.IpAddress, player.SteamID, player.UserId, player.Slot))
.ToList();

Task.Run(async () =>
Expand All @@ -363,18 +362,25 @@ await Server.NextFrameAsync(() =>
MuteManager muteManager = new(_database);

await banManager.ExpireOldBans();
await muteManager.ExpireOldMutes();
await adminManager.DeleteOldAdmins();

BannedPlayers.Clear();

if (onlinePlayers.Count > 0)
{
try
{
await banManager.CheckOnlinePlayers(onlinePlayers);
if (Config.TimeMode == 0)
{
await muteManager.CheckOnlineModeMutes(onlinePlayers);
}
}
catch { }
}

await muteManager.ExpireOldMutes();

await Server.NextFrameAsync(() =>
{
try
Expand Down
41 changes: 14 additions & 27 deletions Managers/BanManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ public async Task<int> GetPlayerBans(PlayerInfo player)
var sql = "";

sql = config.MultiServerMode
? "SELECT COUNT(*) FROM sa_bans WHERE (player_steamid = @PlayerSteamID OR player_ip = @PlayerIP) AND server_id = @serverid"
: "SELECT COUNT(*) FROM sa_bans WHERE (player_steamid = @PlayerSteamID OR player_ip = @PlayerIP)";
? "SELECT COUNT(*) FROM sa_bans WHERE (player_steamid = @PlayerSteamID OR player_ip = @PlayerIP)"
: "SELECT COUNT(*) FROM sa_bans WHERE (player_steamid = @PlayerSteamID OR player_ip = @PlayerIP) AND server_id = @serverid";

int banCount;

Expand Down Expand Up @@ -210,18 +210,12 @@ public async Task UnbanPlayer(string playerPattern, string adminSteamId, string
{
await using var connection = await database.GetConnectionAsync();

string sqlRetrieveBans;
if (config.MultiServerMode)
{
sqlRetrieveBans = "SELECT id FROM sa_bans WHERE (player_steamid = @pattern OR player_name = @pattern OR player_ip = @pattern) AND status = 'ACTIVE' " +
"AND server_id = @serverid";
}
else
{
sqlRetrieveBans = "SELECT id FROM sa_bans WHERE (player_steamid = @pattern OR player_name = @pattern OR player_ip = @pattern) AND status = 'ACTIVE'";
}
var sqlRetrieveBans = config.MultiServerMode
? "SELECT id FROM sa_bans WHERE (player_steamid = @pattern OR player_name = @pattern OR player_ip = @pattern) AND status = 'ACTIVE'"
: "SELECT id FROM sa_bans WHERE (player_steamid = @pattern OR player_name = @pattern OR player_ip = @pattern) AND status = 'ACTIVE' AND server_id = @serverid";

var bans = await connection.QueryAsync(sqlRetrieveBans, new { pattern = playerPattern, serverid = CS2_SimpleAdmin.ServerId });

var bansList = bans as dynamic[] ?? bans.ToArray();
if (bansList.Length == 0)
return;
Expand Down Expand Up @@ -262,25 +256,18 @@ public async Task UnbanPlayer(string playerPattern, string adminSteamId, string
catch { }
}

public async Task CheckOnlinePlayers(List<(string? IpAddress, ulong SteamID, int? UserId)> players)
public async Task CheckOnlinePlayers(List<(string? IpAddress, ulong SteamID, int? UserId, int Slot)> players)
{
try
{
await using var connection = await database.GetConnectionAsync();
string sql;
bool checkIpBans = config.BanType > 0;

if (config.MultiServerMode)
{
sql = "SELECT COUNT(*) FROM sa_bans WHERE (player_steamid = @PlayerSteamID OR player_ip = @PlayerIP) AND status = 'ACTIVE' AND" +
" server_id = @serverid";
}
else
{
sql = "SELECT COUNT(*) FROM sa_bans WHERE (player_steamid = @PlayerSteamID OR player_ip = @PlayerIP) AND status = 'ACTIVE'";
}
var sql = config.MultiServerMode
? "SELECT COUNT(*) FROM sa_bans WHERE (player_steamid = @PlayerSteamID OR player_ip = @PlayerIP) AND status = 'ACTIVE'"
: "SELECT COUNT(*) FROM sa_bans WHERE (player_steamid = @PlayerSteamID OR player_ip = @PlayerIP) AND status = 'ACTIVE' AND server_id = @serverid";

foreach (var (IpAddress, SteamID, UserId) in players)
foreach (var (IpAddress, SteamID, UserId, Slot) in players)
{
if (!UserId.HasValue) continue;

Expand Down Expand Up @@ -336,7 +323,6 @@ UPDATE sa_bans
`duration` > 0
AND
ends <= @currentTime
AND server_id = @serverid
""" : """
UPDATE sa_bans
Expand All @@ -348,6 +334,7 @@ UPDATE sa_bans
`duration` > 0
AND
ends <= @currentTime
AND server_id = @serverid
""";

await connection.ExecuteAsync(sql, new { currentTime, serverid = CS2_SimpleAdmin.ServerId });
Expand All @@ -364,7 +351,6 @@ UPDATE sa_bans
status = 'ACTIVE'
AND
ends <= @ipBansTime
AND server_id = @serverid
""" : """
UPDATE sa_bans
Expand All @@ -374,6 +360,7 @@ UPDATE sa_bans
status = 'ACTIVE'
AND
ends <= @ipBansTime
AND server_id = @serverid
""";

await connection.ExecuteAsync(sql, new { ipBansTime, CS2_SimpleAdmin.ServerId });
Expand Down
93 changes: 79 additions & 14 deletions Managers/MuteManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Dapper;
using CounterStrikeSharp.API.Core;
using Dapper;
using Microsoft.Extensions.Logging;

namespace CS2_SimpleAdmin;
Expand Down Expand Up @@ -96,16 +97,20 @@ public async Task<List<dynamic>> IsPlayerMuted(string steamId)
{
await using var connection = await database.GetConnectionAsync();
var currentTime = DateTime.UtcNow.ToLocalTime();
string sql;

var sql = "";
if (CS2_SimpleAdmin.Instance.Config.MultiServerMode)
{
sql = "SELECT * FROM sa_mutes WHERE player_steamid = @PlayerSteamID AND status = 'ACTIVE' AND (duration = 0 OR ends > @CurrentTime) " +
"AND server_id = @serverid";
sql = CS2_SimpleAdmin.Instance.Config.TimeMode == 1
? "SELECT * FROM sa_mutes WHERE player_steamid = @PlayerSteamID AND status = 'ACTIVE' AND (duration = 0 OR ends > @CurrentTime)"
: "SELECT * FROM sa_mutes WHERE player_steamid = @PlayerSteamID AND status = 'ACTIVE' AND (duration = 0 OR duration > COALESCE(passed, 0))";
}
else
{
sql = "SELECT * FROM sa_mutes WHERE player_steamid = @PlayerSteamID AND status = 'ACTIVE' AND (duration = 0 OR ends > @CurrentTime)";
sql = CS2_SimpleAdmin.Instance.Config.TimeMode == 1
? "SELECT * FROM sa_mutes WHERE player_steamid = @PlayerSteamID AND status = 'ACTIVE' AND (duration = 0 OR ends > @CurrentTime) AND server_id = @serverid"
: "SELECT * FROM sa_mutes WHERE player_steamid = @PlayerSteamID AND status = 'ACTIVE' AND (duration = 0 OR duration > COALESCE(passed, 0)) AND server_id = @serverid";

}

var parameters = new { PlayerSteamID = steamId, CurrentTime = currentTime, serverid = CS2_SimpleAdmin.ServerId };
Expand All @@ -125,8 +130,8 @@ public async Task<int> GetPlayerMutes(string steamId)
await using var connection = await database.GetConnectionAsync();

var sql = CS2_SimpleAdmin.Instance.Config.MultiServerMode
? "SELECT COUNT(*) FROM sa_mutes WHERE player_steamid = @PlayerSteamID AND server_id = @serverid"
: "SELECT COUNT(*) FROM sa_mutes WHERE player_steamid = @PlayerSteamID";
? "SELECT COUNT(*) FROM sa_mutes WHERE player_steamid = @PlayerSteamID"
: "SELECT COUNT(*) FROM sa_mutes WHERE player_steamid = @PlayerSteamID AND server_id = @serverid";

var muteCount = await connection.ExecuteScalarAsync<int>(sql, new { PlayerSteamID = steamId, serverid = CS2_SimpleAdmin.ServerId });
return muteCount;
Expand All @@ -136,6 +141,56 @@ public async Task<int> GetPlayerMutes(string steamId)
return 0;
}
}

public async Task CheckOnlineModeMutes(List<(string? IpAddress, ulong SteamID, int? UserId, int Slot)> players)
{
try
{
int batchSize = 10;
await using var connection = await database.GetConnectionAsync();

var sql = CS2_SimpleAdmin.Instance.Config.MultiServerMode
? "UPDATE `sa_mutes` SET passed = COALESCE(passed, 0) + 1 WHERE (player_steamid = @PlayerSteamID) AND duration > 0 AND status = 'ACTIVE'"
: "UPDATE `sa_mutes` SET passed = COALESCE(passed, 0) + 1 WHERE (player_steamid = @PlayerSteamID) AND duration > 0 AND status = 'ACTIVE' AND server_id = @serverid";
/*
foreach (var (IpAddress, SteamID, UserId, Slot) in players)
{
await connection.ExecuteAsync(sql,
new { PlayerSteamID = SteamID, serverid = CS2_SimpleAdmin.ServerId });
}*/

for (var i = 0; i < players.Count; i += batchSize)
{
var batch = players.Skip(i).Take(batchSize);
var parametersList = new List<object>();

foreach (var (IpAddress, SteamID, UserId, Slot) in batch)
{
parametersList.Add(new { PlayerSteamID = SteamID, serverid = CS2_SimpleAdmin.ServerId });
}

await connection.ExecuteAsync(sql, parametersList);
}

sql = CS2_SimpleAdmin.Instance.Config.MultiServerMode
? "SELECT * FROM `sa_mutes` WHERE player_steamid = @PlayerSteamID AND passed >= duration AND duration > 0 AND status = 'ACTIVE'"
: "SELECT * FROM `sa_mutes` WHERE player_steamid = @PlayerSteamID AND passed >= duration AND duration > 0 AND status = 'ACTIVE' AND server_id = @serverid";


foreach (var (IpAddress, SteamID, UserId, Slot) in players)
{
var muteRecords = await connection.QueryAsync(sql, new { PlayerSteamID = SteamID, serverid = CS2_SimpleAdmin.ServerId });

foreach (var muteRecord in muteRecords)
{
DateTime endDateTime = muteRecord.ends;
PlayerPenaltyManager.RemovePenaltiesByDateTime(Slot, endDateTime);
}

}
}
catch { }
}

public async Task UnmutePlayer(string playerPattern, string adminSteamId, string reason, int type = 0)
{
Expand All @@ -160,12 +215,12 @@ public async Task UnmutePlayer(string playerPattern, string adminSteamId, string
if (CS2_SimpleAdmin.Instance.Config.MultiServerMode)
{
sqlRetrieveMutes = "SELECT id FROM sa_mutes WHERE (player_steamid = @pattern OR player_name = @pattern) AND " +
"type = @muteType AND status = 'ACTIVE' AND server_id = @serverid";
"type = @muteType AND status = 'ACTIVE'";
}
else
{
sqlRetrieveMutes = "SELECT id FROM sa_mutes WHERE (player_steamid = @pattern OR player_name = @pattern) AND " +
"type = @muteType AND status = 'ACTIVE'";
"type = @muteType AND status = 'ACTIVE' AND server_id = @serverid";
}

var mutes = await connection.QueryAsync(sqlRetrieveMutes, new { pattern = playerPattern, muteType, serverid = CS2_SimpleAdmin.ServerId });
Expand Down Expand Up @@ -212,10 +267,20 @@ public async Task ExpireOldMutes()
try
{
await using var connection = await database.GetConnectionAsync();

var sql = CS2_SimpleAdmin.Instance.Config.MultiServerMode
? "UPDATE sa_mutes SET status = 'EXPIRED' WHERE status = 'ACTIVE' AND `duration` > 0 AND ends <= @CurrentTime AND server_id = @serverid"
: "UPDATE sa_mutes SET status = 'EXPIRED' WHERE status = 'ACTIVE' AND `duration` > 0 AND ends <= @CurrentTime";
var sql = "";

if (CS2_SimpleAdmin.Instance.Config.MultiServerMode)
{
sql = CS2_SimpleAdmin.Instance.Config.TimeMode == 1
? "UPDATE sa_mutes SET status = 'EXPIRED' WHERE status = 'ACTIVE' AND `duration` > 0 AND ends <= @CurrentTime"
: "UPDATE sa_mutes SET status = 'EXPIRED' WHERE status = 'ACTIVE' AND `duration` > 0 AND `passed` >= `duration`";
}
else
{
sql = CS2_SimpleAdmin.Instance.Config.TimeMode == 1
? "UPDATE sa_mutes SET status = 'EXPIRED' WHERE status = 'ACTIVE' AND `duration` > 0 AND ends <= @CurrentTime AND server_id = @serverid"
: "UPDATE sa_mutes SET status = 'EXPIRED' WHERE status = 'ACTIVE' AND `duration` > 0 AND `passed` >= `duration` AND server_id = @serverid";
}

await connection.ExecuteAsync(sql, new { CurrentTime = DateTime.UtcNow.ToLocalTime(), serverid = CS2_SimpleAdmin.ServerId });
}
Expand Down
Loading

0 comments on commit 4244104

Please sign in to comment.