mirror of
https://github.com/RaidMax/IW4M-Admin.git
synced 2025-06-09 23:00:57 -05:00
Refactor Welcome plugin and update project settings
The commit simplifies the WelcomeConfiguration class, removing unnecessary methods and interfaces. It also changes how the Welcome plugin registers dependencies and processes in-game announcements. Additionally, it enables nullable reference type checks in the project settings. These changes generally improve the code structure, readability, and maintainability, while preserving the overall functionality of the plugin.
This commit is contained in:
@ -1,12 +1,11 @@
|
|||||||
using System;
|
using System.Threading.Tasks;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using SharedLibraryCore;
|
using SharedLibraryCore;
|
||||||
using SharedLibraryCore.Interfaces;
|
using SharedLibraryCore.Interfaces;
|
||||||
using SharedLibraryCore.Database.Models;
|
using SharedLibraryCore.Database.Models;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Text.Json;
|
using System.Net.Http.Json;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using Humanizer;
|
using Humanizer;
|
||||||
using Data.Abstractions;
|
using Data.Abstractions;
|
||||||
@ -21,14 +20,9 @@ namespace IW4MAdmin.Plugins.Welcome;
|
|||||||
public class Plugin : IPluginV2
|
public class Plugin : IPluginV2
|
||||||
{
|
{
|
||||||
public string Author => "RaidMax";
|
public string Author => "RaidMax";
|
||||||
public string Version => "1.1";
|
public string Version => Utilities.GetVersionAsString();
|
||||||
public string Name => "Welcome Plugin";
|
public string Name => "Welcome Plugin";
|
||||||
|
|
||||||
public static void RegisterDependencies(IServiceCollection serviceCollection)
|
|
||||||
{
|
|
||||||
serviceCollection.AddConfiguration<WelcomeConfiguration>("WelcomePluginSettings");
|
|
||||||
}
|
|
||||||
|
|
||||||
private readonly WelcomeConfiguration _configuration;
|
private readonly WelcomeConfiguration _configuration;
|
||||||
private readonly IDatabaseContextFactory _contextFactory;
|
private readonly IDatabaseContextFactory _contextFactory;
|
||||||
|
|
||||||
@ -40,86 +34,85 @@ public class Plugin : IPluginV2
|
|||||||
IManagementEventSubscriptions.ClientStateAuthorized += OnClientStateAuthorized;
|
IManagementEventSubscriptions.ClientStateAuthorized += OnClientStateAuthorized;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void RegisterDependencies(IServiceCollection serviceCollection)
|
||||||
|
{
|
||||||
|
serviceCollection.AddConfiguration("WelcomePluginSettings", new WelcomeConfiguration());
|
||||||
|
}
|
||||||
|
|
||||||
private async Task OnClientStateAuthorized(ClientStateEvent clientState, CancellationToken token)
|
private async Task OnClientStateAuthorized(ClientStateEvent clientState, CancellationToken token)
|
||||||
{
|
{
|
||||||
var newPlayer = clientState.Client;
|
var player = clientState.Client;
|
||||||
|
|
||||||
if (newPlayer.Level >= Permission.Trusted && !newPlayer.Masked ||
|
var isPrivileged = player.Level >= Permission.Trusted && !player.Masked ||
|
||||||
!string.IsNullOrEmpty(newPlayer.Tag) &&
|
!string.IsNullOrEmpty(player.Tag) && player.Level != Permission.Flagged &&
|
||||||
newPlayer.Level != Permission.Flagged && newPlayer.Level != Permission.Banned &&
|
player.Level != Permission.Banned && !player.Masked;
|
||||||
!newPlayer.Masked)
|
|
||||||
newPlayer.CurrentServer.Broadcast(
|
|
||||||
await ProcessAnnouncement(_configuration.PrivilegedAnnouncementMessage,
|
|
||||||
newPlayer));
|
|
||||||
|
|
||||||
newPlayer.Tell(await ProcessAnnouncement(_configuration.UserWelcomeMessage, newPlayer));
|
if (isPrivileged)
|
||||||
|
|
||||||
if (newPlayer.Level == Permission.Flagged)
|
|
||||||
{
|
{
|
||||||
string penaltyReason;
|
var announcement = await ProcessAnnouncement(_configuration.PrivilegedAnnouncementMessage, player);
|
||||||
|
player.CurrentServer.Broadcast(announcement);
|
||||||
|
}
|
||||||
|
|
||||||
await using var context = _contextFactory.CreateContext(false);
|
// Send welcome message to the player
|
||||||
{
|
player.Tell(await ProcessAnnouncement(_configuration.UserWelcomeMessage, player));
|
||||||
penaltyReason = await context.Penalties
|
|
||||||
.Where(p => p.OffenderId == newPlayer.ClientId && p.Type == EFPenalty.PenaltyType.Flag)
|
|
||||||
.OrderByDescending(p => p.When)
|
|
||||||
.Select(p => p.AutomatedOffense ?? p.Offense)
|
|
||||||
.FirstOrDefaultAsync(cancellationToken: token);
|
|
||||||
}
|
|
||||||
|
|
||||||
newPlayer.CurrentServer.ToAdmins(Utilities.CurrentLocalization
|
if (player.Level == Permission.Flagged)
|
||||||
.LocalizationIndex["PLUGINS_WELCOME_FLAG_MESSAGE"]
|
{
|
||||||
.FormatExt(newPlayer.Name, penaltyReason));
|
var penaltyReason = await GetPenaltyReason(player.ClientId, token);
|
||||||
|
|
||||||
|
player.CurrentServer.ToAdmins(
|
||||||
|
Utilities.CurrentLocalization.LocalizationIndex["PLUGINS_WELCOME_FLAG_MESSAGE"]
|
||||||
|
.FormatExt(player.Name, penaltyReason)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
newPlayer.CurrentServer.Broadcast(await ProcessAnnouncement(_configuration.UserAnnouncementMessage,
|
var announcement = await ProcessAnnouncement(_configuration.UserAnnouncementMessage, player);
|
||||||
newPlayer));
|
player.CurrentServer.Broadcast(announcement);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<string> ProcessAnnouncement(string msg, EFClient joining)
|
private async Task<string> GetPenaltyReason(int clientId, CancellationToken token)
|
||||||
{
|
{
|
||||||
msg = msg.Replace("{{ClientName}}", joining.Name);
|
await using var context = _contextFactory.CreateContext(false);
|
||||||
msg = msg.Replace("{{ClientLevel}}",
|
|
||||||
$"{Utilities.ConvertLevelToColor(joining.Level, joining.ClientPermission.Name)}{(string.IsNullOrEmpty(joining.Tag) ? "" : $" (Color::White){joining.Tag}(Color::White)")}");
|
|
||||||
// this prevents it from trying to evaluate it every message
|
|
||||||
if (msg.Contains("{{ClientLocation}}"))
|
|
||||||
{
|
|
||||||
msg = msg.Replace("{{ClientLocation}}", await GetCountryName(joining.IPAddressString));
|
|
||||||
}
|
|
||||||
|
|
||||||
msg = msg.Replace("{{TimesConnected}}",
|
return await context.Penalties
|
||||||
joining.Connections.Ordinalize(Utilities.CurrentLocalization.Culture));
|
.Where(p => p.OffenderId == clientId && p.Type == EFPenalty.PenaltyType.Flag)
|
||||||
|
.OrderByDescending(p => p.When)
|
||||||
return msg;
|
.Select(p => p.AutomatedOffense ?? p.Offense)
|
||||||
|
.FirstOrDefaultAsync(token) ?? string.Empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
private static async Task<string> ProcessAnnouncement(string messageTemplate, EFClient player)
|
||||||
/// makes a webrequest to determine IP origin
|
{
|
||||||
/// </summary>
|
var levelWithColor = $"{Utilities.ConvertLevelToColor(player.Level, player.ClientPermission.Name)}{(string.IsNullOrEmpty(player.Tag)
|
||||||
/// <param name="ip">IP address to get location of</param>
|
? string.Empty
|
||||||
/// <returns></returns>
|
: $" (Color::White){player.Tag}(Color::White)")}";
|
||||||
private async Task<string> GetCountryName(string ip)
|
|
||||||
|
return messageTemplate
|
||||||
|
.Replace("{{ClientName}}", player.Name)
|
||||||
|
.Replace("{{ClientLevel}}", levelWithColor)
|
||||||
|
.Replace("{{ClientLocation}}", await GetCountryName(player.IPAddressString))
|
||||||
|
.Replace("{{TimesConnected}}", player.Connections.Ordinalize(Utilities.CurrentLocalization.Culture));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task<string> GetCountryName(string ipAddress)
|
||||||
{
|
{
|
||||||
using var wc = new HttpClient();
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var response =
|
using var httpClient = new HttpClient();
|
||||||
await wc.GetStringAsync(new Uri(
|
var apiUrl =
|
||||||
$"http://ip-api.com/json/{ip}?lang={Utilities.CurrentLocalization.LocalizationName.Split("-").First().ToLower()}"));
|
$"http://ip-api.com/json/{ipAddress}?lang={Utilities.CurrentLocalization.LocalizationName.Split('-').First().ToLower()}";
|
||||||
|
|
||||||
var json = JsonDocument.Parse(response);
|
var response = await httpClient.GetFromJsonAsync<IpApiResponse>(apiUrl);
|
||||||
response = json.RootElement.TryGetProperty("country", out var countryElement) ? countryElement.GetString() : null;
|
|
||||||
|
|
||||||
return string.IsNullOrEmpty(response)
|
return response?.Country ?? Utilities.CurrentLocalization.LocalizationIndex["PLUGINS_WELCOME_UNKNOWN_COUNTRY"];
|
||||||
? Utilities.CurrentLocalization.LocalizationIndex["PLUGINS_WELCOME_UNKNOWN_COUNTRY"]
|
|
||||||
: response;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
return Utilities.CurrentLocalization.LocalizationIndex["PLUGINS_WELCOME_UNKNOWN_IP"];
|
return Utilities.CurrentLocalization.LocalizationIndex["PLUGINS_WELCOME_UNKNOWN_IP"];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private record IpApiResponse(string Country);
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,7 @@
|
|||||||
<Configurations>Debug;Release;Prerelease</Configurations>
|
<Configurations>Debug;Release;Prerelease</Configurations>
|
||||||
<LangVersion>Latest</LangVersion>
|
<LangVersion>Latest</LangVersion>
|
||||||
<RootNamespace>IW4MAdmin.Plugins.Welcome</RootNamespace>
|
<RootNamespace>IW4MAdmin.Plugins.Welcome</RootNamespace>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
@ -1,22 +1,16 @@
|
|||||||
using SharedLibraryCore;
|
using SharedLibraryCore;
|
||||||
using SharedLibraryCore.Interfaces;
|
|
||||||
|
|
||||||
namespace IW4MAdmin.Plugins.Welcome
|
namespace IW4MAdmin.Plugins.Welcome
|
||||||
{
|
{
|
||||||
public class WelcomeConfiguration : IBaseConfiguration
|
public class WelcomeConfiguration
|
||||||
{
|
{
|
||||||
public string UserAnnouncementMessage { get; set; }
|
public string UserAnnouncementMessage { get; set; } =
|
||||||
public string UserWelcomeMessage { get; set; }
|
Utilities.CurrentLocalization.LocalizationIndex["PLUGINS_WELCOME_USERANNOUNCE_V2"];
|
||||||
public string PrivilegedAnnouncementMessage { get; set; }
|
|
||||||
|
|
||||||
public IBaseConfiguration Generate()
|
public string UserWelcomeMessage { get; set; } =
|
||||||
{
|
Utilities.CurrentLocalization.LocalizationIndex["PLUGINS_WELCOME_USERWELCOME_V2"];
|
||||||
UserAnnouncementMessage = Utilities.CurrentLocalization.LocalizationIndex["PLUGINS_WELCOME_USERANNOUNCE_V2"];
|
|
||||||
UserWelcomeMessage = Utilities.CurrentLocalization.LocalizationIndex["PLUGINS_WELCOME_USERWELCOME_V2"];
|
|
||||||
PrivilegedAnnouncementMessage = Utilities.CurrentLocalization.LocalizationIndex["PLUGINS_WELCOME_PRIVANNOUNCE_V2"];
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public string Name() => "WelcomeConfiguration";
|
public string PrivilegedAnnouncementMessage { get; set; } =
|
||||||
|
Utilities.CurrentLocalization.LocalizationIndex["PLUGINS_WELCOME_PRIVANNOUNCE_V2"];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user