mirror of
https://github.com/RaidMax/IW4M-Admin.git
synced 2025-06-10 07:13:58 -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task OnClientStateAuthorized(ClientStateEvent clientState, CancellationToken token)
|
public static void RegisterDependencies(IServiceCollection serviceCollection)
|
||||||
{
|
{
|
||||||
var newPlayer = clientState.Client;
|
serviceCollection.AddConfiguration("WelcomePluginSettings", new WelcomeConfiguration());
|
||||||
|
|
||||||
if (newPlayer.Level >= Permission.Trusted && !newPlayer.Masked ||
|
|
||||||
!string.IsNullOrEmpty(newPlayer.Tag) &&
|
|
||||||
newPlayer.Level != Permission.Flagged && newPlayer.Level != Permission.Banned &&
|
|
||||||
!newPlayer.Masked)
|
|
||||||
newPlayer.CurrentServer.Broadcast(
|
|
||||||
await ProcessAnnouncement(_configuration.PrivilegedAnnouncementMessage,
|
|
||||||
newPlayer));
|
|
||||||
|
|
||||||
newPlayer.Tell(await ProcessAnnouncement(_configuration.UserWelcomeMessage, newPlayer));
|
|
||||||
|
|
||||||
if (newPlayer.Level == Permission.Flagged)
|
|
||||||
{
|
|
||||||
string penaltyReason;
|
|
||||||
|
|
||||||
await using var context = _contextFactory.CreateContext(false);
|
|
||||||
{
|
|
||||||
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
|
private async Task OnClientStateAuthorized(ClientStateEvent clientState, CancellationToken token)
|
||||||
.LocalizationIndex["PLUGINS_WELCOME_FLAG_MESSAGE"]
|
{
|
||||||
.FormatExt(newPlayer.Name, penaltyReason));
|
var player = clientState.Client;
|
||||||
|
|
||||||
|
var isPrivileged = player.Level >= Permission.Trusted && !player.Masked ||
|
||||||
|
!string.IsNullOrEmpty(player.Tag) && player.Level != Permission.Flagged &&
|
||||||
|
player.Level != Permission.Banned && !player.Masked;
|
||||||
|
|
||||||
|
if (isPrivileged)
|
||||||
|
{
|
||||||
|
var announcement = await ProcessAnnouncement(_configuration.PrivilegedAnnouncementMessage, player);
|
||||||
|
player.CurrentServer.Broadcast(announcement);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send welcome message to the player
|
||||||
|
player.Tell(await ProcessAnnouncement(_configuration.UserWelcomeMessage, player));
|
||||||
|
|
||||||
|
if (player.Level == Permission.Flagged)
|
||||||
|
{
|
||||||
|
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)")}");
|
return await context.Penalties
|
||||||
// this prevents it from trying to evaluate it every message
|
.Where(p => p.OffenderId == clientId && p.Type == EFPenalty.PenaltyType.Flag)
|
||||||
if (msg.Contains("{{ClientLocation}}"))
|
.OrderByDescending(p => p.When)
|
||||||
{
|
.Select(p => p.AutomatedOffense ?? p.Offense)
|
||||||
msg = msg.Replace("{{ClientLocation}}", await GetCountryName(joining.IPAddressString));
|
.FirstOrDefaultAsync(token) ?? string.Empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
msg = msg.Replace("{{TimesConnected}}",
|
private static async Task<string> ProcessAnnouncement(string messageTemplate, EFClient player)
|
||||||
joining.Connections.Ordinalize(Utilities.CurrentLocalization.Culture));
|
{
|
||||||
|
var levelWithColor = $"{Utilities.ConvertLevelToColor(player.Level, player.ClientPermission.Name)}{(string.IsNullOrEmpty(player.Tag)
|
||||||
|
? string.Empty
|
||||||
|
: $" (Color::White){player.Tag}(Color::White)")}";
|
||||||
|
|
||||||
return msg;
|
return messageTemplate
|
||||||
|
.Replace("{{ClientName}}", player.Name)
|
||||||
|
.Replace("{{ClientLevel}}", levelWithColor)
|
||||||
|
.Replace("{{ClientLocation}}", await GetCountryName(player.IPAddressString))
|
||||||
|
.Replace("{{TimesConnected}}", player.Connections.Ordinalize(Utilities.CurrentLocalization.Culture));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
private static async Task<string> GetCountryName(string ipAddress)
|
||||||
/// makes a webrequest to determine IP origin
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="ip">IP address to get location of</param>
|
|
||||||
/// <returns></returns>
|
|
||||||
private async Task<string> GetCountryName(string ip)
|
|
||||||
{
|
{
|
||||||
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