From bacafebb99dea168a1bc36e0db9cc231073cf4d8 Mon Sep 17 00:00:00 2001 From: RaidMax Date: Sat, 26 Sep 2020 17:17:21 -0500 Subject: [PATCH] implement pm admins command for issue #170 --- Application/ApplicationManager.cs | 3 +- SharedLibraryCore/Command.cs | 22 +++++++++- .../Commands/PrivateMessageAdminsCommand.cs | 37 +++++++++++++++++ .../Configuration/CommandProperties.cs | 11 ++++- .../Interfaces/IManagerCommand.cs | 6 +++ Tests/ApplicationTests/CommandTests.cs | 41 +++++++++++++++++++ 6 files changed, 116 insertions(+), 4 deletions(-) create mode 100644 SharedLibraryCore/Commands/PrivateMessageAdminsCommand.cs diff --git a/Application/ApplicationManager.cs b/Application/ApplicationManager.cs index bad4832a..5a2d17f3 100644 --- a/Application/ApplicationManager.cs +++ b/Application/ApplicationManager.cs @@ -449,7 +449,8 @@ namespace IW4MAdmin.Application Name = cmd.Name, Alias = cmd.Alias, MinimumPermission = cmd.Permission, - AllowImpersonation = cmd.AllowImpersonation + AllowImpersonation = cmd.AllowImpersonation, + SupportedGames = cmd.SupportedGames }); } diff --git a/SharedLibraryCore/Command.cs b/SharedLibraryCore/Command.cs index fbe2cf30..2f0c5e5f 100644 --- a/SharedLibraryCore/Command.cs +++ b/SharedLibraryCore/Command.cs @@ -5,6 +5,7 @@ using SharedLibraryCore.Commands; using SharedLibraryCore.Configuration; using SharedLibraryCore.Database.Models; using SharedLibraryCore.Interfaces; +using static SharedLibraryCore.Server; namespace SharedLibraryCore { @@ -13,7 +14,7 @@ namespace SharedLibraryCore /// public abstract class Command : IManagerCommand { - private readonly CommandConfiguration _config; + protected readonly CommandConfiguration _config; protected readonly ITranslationLookup _translationLookup; protected ILogger logger; @@ -113,6 +114,25 @@ namespace SharedLibraryCore } private EFClient.Permission permission; + public Game[] SupportedGames + { + get => supportedGames; + protected set + { + try + { + var savedGames = _config?.Commands[GetType().Name].SupportedGames; + supportedGames = savedGames?.Length != 0 ? savedGames : value; + } + + catch (KeyNotFoundException) + { + supportedGames = value; + } + } + } + private Game[] supportedGames; + /// /// Argument list for the command diff --git a/SharedLibraryCore/Commands/PrivateMessageAdminsCommand.cs b/SharedLibraryCore/Commands/PrivateMessageAdminsCommand.cs new file mode 100644 index 00000000..95db8b21 --- /dev/null +++ b/SharedLibraryCore/Commands/PrivateMessageAdminsCommand.cs @@ -0,0 +1,37 @@ +using SharedLibraryCore.Configuration; +using SharedLibraryCore.Database.Models; +using SharedLibraryCore.Interfaces; +using System; +using System.Linq; +using System.Threading.Tasks; +using static SharedLibraryCore.Server; + +namespace SharedLibraryCore.Commands +{ + public class PrivateMessageAdminsCommand : Command + { + public PrivateMessageAdminsCommand(CommandConfiguration config, ITranslationLookup lookup) : base(config, lookup) + { + Name = "privatemessageadmin"; + Description = lookup["COMMANDS_PMADMINS_DESC"]; + Alias = "pma"; + Permission = EFClient.Permission.Moderator; + SupportedGames = new[] { Game.IW4, Game.IW5 }; + } + + public override Task ExecuteAsync(GameEvent E) + { + bool isGameSupported = _config.Commands[nameof(PrivateMessageAdminsCommand)].SupportedGames.Length > 0 && + _config.Commands[nameof(PrivateMessageAdminsCommand)].SupportedGames.Contains(E.Owner.GameName); + + if (!isGameSupported) + { + E.Origin.Tell(_translationLookup["COMMANDS_GAME_NOT_SUPPORTED"].FormatExt(nameof(PrivateMessageAdminsCommand))); + return Task.CompletedTask; + } + + E.Owner.ToAdmins(E.Data); + return Task.CompletedTask; + } + } +} diff --git a/SharedLibraryCore/Configuration/CommandProperties.cs b/SharedLibraryCore/Configuration/CommandProperties.cs index 9eab8a9b..88e52914 100644 --- a/SharedLibraryCore/Configuration/CommandProperties.cs +++ b/SharedLibraryCore/Configuration/CommandProperties.cs @@ -1,6 +1,7 @@ -using Newtonsoft.Json.Converters; -using System.Text.Json.Serialization; +using Newtonsoft.Json; +using Newtonsoft.Json.Converters; using static SharedLibraryCore.Database.Models.EFClient; +using static SharedLibraryCore.Server; namespace SharedLibraryCore.Configuration { @@ -29,5 +30,11 @@ namespace SharedLibraryCore.Configuration /// Indicates if the command can be run by another user (impersonation) /// public bool AllowImpersonation { get; set; } + + /// + /// Specifies the games supporting the functionality of the command + /// + [JsonProperty(ItemConverterType = typeof(StringEnumConverter))] + public Game[] SupportedGames { get; set; } = new Game[0]; } } diff --git a/SharedLibraryCore/Interfaces/IManagerCommand.cs b/SharedLibraryCore/Interfaces/IManagerCommand.cs index 88743e36..d9222bda 100644 --- a/SharedLibraryCore/Interfaces/IManagerCommand.cs +++ b/SharedLibraryCore/Interfaces/IManagerCommand.cs @@ -1,5 +1,6 @@ using System.Threading.Tasks; using static SharedLibraryCore.Database.Models.EFClient; +using static SharedLibraryCore.Server; namespace SharedLibraryCore.Interfaces { @@ -35,6 +36,11 @@ namespace SharedLibraryCore.Interfaces /// Permission Permission { get; } + /// + /// Games the command is supported on + /// + Game[] SupportedGames { get; } + /// /// Syntax for using the command /// diff --git a/Tests/ApplicationTests/CommandTests.cs b/Tests/ApplicationTests/CommandTests.cs index d9c9fccb..0576f924 100644 --- a/Tests/ApplicationTests/CommandTests.cs +++ b/Tests/ApplicationTests/CommandTests.cs @@ -537,5 +537,46 @@ namespace ApplicationTests Assert.IsTrue(result.IsBroadcast); } #endregion + + #region PMADMINS + [Test] + public async Task Test_PrivateMessageAdmins_HappyPath() + { + var cmd = new PrivateMessageAdminsCommand(cmdConfig, transLookup); + var server = serviceProvider.GetRequiredService(); + var origin = ClientGenerators.CreateDatabaseClient(); + origin.Level = Permission.Administrator; + origin.CurrentServer = server; + var gameEvent = EventGenerators.GenerateEvent(GameEvent.EventType.Command, "", server); + cmdConfig.Commands.Add(nameof(PrivateMessageAdminsCommand), new CommandProperties { SupportedGames = new[] { server.GameName } }); + + server.Clients[0] = origin; + server.Clients[1] = origin; + await cmd.ExecuteAsync(gameEvent); + int expectedEvents = 2; + + Assert.AreEqual(expectedEvents, mockEventHandler.Events.Count(_event => _event.Type == GameEvent.EventType.Tell)); + } + + [Test] + public async Task Test_PrivateMessageAdmins_GameNotSupported() + { + var cmd = new PrivateMessageAdminsCommand(cmdConfig, transLookup); + var server = serviceProvider.GetRequiredService(); + var origin = ClientGenerators.CreateDatabaseClient(); + origin.Level = Permission.Administrator; + origin.CurrentServer = server; + var gameEvent = EventGenerators.GenerateEvent(GameEvent.EventType.Command, "", server); + gameEvent.Origin = origin; + cmdConfig.Commands.Add(nameof(PrivateMessageAdminsCommand), new CommandProperties()); + + server.Clients[0] = origin; + server.Clients[1] = origin; + await cmd.ExecuteAsync(gameEvent); + int expectedEvents = 1; + + Assert.AreEqual(expectedEvents, mockEventHandler.Events.Count(_event => _event.Type == GameEvent.EventType.Tell)); + } + #endregion } }