implement pm admins command for issue #170

This commit is contained in:
RaidMax 2020-09-26 17:17:21 -05:00
parent 7e3c74e63c
commit 2ab0cfa9be
6 changed files with 116 additions and 4 deletions

View File

@ -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
});
}

View File

@ -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
/// </summary>
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;
/// <summary>
/// Argument list for the command

View File

@ -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;
}
}
}

View File

@ -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)
/// </summary>
public bool AllowImpersonation { get; set; }
/// <summary>
/// Specifies the games supporting the functionality of the command
/// </summary>
[JsonProperty(ItemConverterType = typeof(StringEnumConverter))]
public Game[] SupportedGames { get; set; } = new Game[0];
}
}

View File

@ -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
/// </summary>
Permission Permission { get; }
/// <summary>
/// Games the command is supported on
/// </summary>
Game[] SupportedGames { get; }
/// <summary>
/// Syntax for using the command
/// </summary>

View File

@ -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<IW4MServer>();
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<IW4MServer>();
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
}
}