Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Load banned players from bans.json file #247

Merged
merged 2 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="System.Text.Json" Version="8.0.5" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
Expand Down
1 change: 1 addition & 0 deletions src/Application/CTF.Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Text.Json" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" />
<PackageReference Include="SampSharp.CTF.Entities" />
<PackageReference Include="SampSharp.CTF.Streamer.Entities" />
Expand Down
32 changes: 26 additions & 6 deletions src/Application/Players/GeneralCommands/AdminCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,20 +106,40 @@ public void ShowBannedIPs(Player currentPlayer)
if (currentPlayer.HasLowerRoleThan(RoleId.Admin))
return;

var path = Path.Combine(Directory.GetCurrentDirectory(), "samp.ban");
var bannedIPs = File.ReadAllLines(path);
if (bannedIPs.Length == 0)
var path = Path.Combine(Directory.GetCurrentDirectory(), "bans.json");
var content = File.ReadAllText(path);
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};
var bannedPlayers = JsonSerializer.Deserialize<BannedPlayer[]>(content, options);
if (bannedPlayers.Length == 0)
{
currentPlayer.SendClientMessage(Color.Red, Messages.NoMatchFound);
return;
}

var dialog = new ListDialog(caption: $"Banned IPs: {bannedIPs.Length}", "Close");
foreach (string bannedIP in bannedIPs)
var dialog = new ListDialog(caption: $"Banned Players: {bannedPlayers.Length}", "Close");
foreach (BannedPlayer bannedPlayer in bannedPlayers)
{
dialog.Add(bannedIP);
dialog.Add(bannedPlayer.ToString());
}

dialogService.ShowAsync(currentPlayer, dialog);
}

private class BannedPlayer
{
public string Address { get; set; } = string.Empty;
public string Player { get; set; } = string.Empty;
public string Reason { get; set; } = string.Empty;
public string Time { get; set; } = "2023-12-07T16:05:21-0500";
public override string ToString()
{
var dt = DateTimeOffset.Parse(Time).DateTime;
var date = dt.ToString("yyyy/MM/dd");
var time = dt.ToString("HH:mm:ss");
return $"{Address} [{date} | {time}] {Player} - {Reason}";
}
}
}
3 changes: 2 additions & 1 deletion src/Application/Usings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
global using System.Collections;
global using System.Text.Json;
global using System.Collections;
global using System.Globalization;
global using System.Reflection;
global using System.Text.RegularExpressions;
Expand Down
Loading