1
0
mirror of https://github.com/RaidMax/IW4M-Admin.git synced 2025-06-10 15:20:48 -05:00

start work to allow customizing command properties via configuration

This commit is contained in:
RaidMax
2020-01-26 18:06:50 -06:00
parent 8c3ea779f2
commit 475ef92917
16 changed files with 1061 additions and 587 deletions

View File

@ -1,38 +1,121 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SharedLibraryCore.Commands;
using SharedLibraryCore.Configuration;
using SharedLibraryCore.Database.Models;
using SharedLibraryCore.Interfaces;
namespace SharedLibraryCore
{
public class CommandArgument
/// <summary>
/// Abstract class for command
/// </summary>
public abstract class Command : IManagerCommand
{
public string Name { get; set; }
public bool Required { get; set; }
}
private readonly CommandConfiguration _config;
protected readonly ITranslationLookup _translationLookup;
public abstract class Command
{
public Command(String commandName, String commandDescription, String commandAlias, EFClient.Permission requiredPermission, bool requiresTarget, CommandArgument[] param = null)
{
Name = commandName;
Description = commandDescription;
Alias = commandAlias;
Permission = requiredPermission;
RequiresTarget = requiresTarget;
Arguments = param ?? new CommandArgument[0];
public Command(CommandConfiguration config, ITranslationLookup layout)
{
_config = config;
_translationLookup = layout;
}
//Execute the command
/// <summary>
/// Executes the command
/// </summary>
/// <param name="E"></param>
/// <returns></returns>
abstract public Task ExecuteAsync(GameEvent E);
public String Name { get; private set; }
public String Description { get; private set; }
public String Syntax => $"{Utilities.CurrentLocalization.LocalizationIndex["COMMAND_HELP_SYNTAX"]} !{Alias} {String.Join(" ", Arguments.Select(a => $"<{(a.Required ? "" : Utilities.CurrentLocalization.LocalizationIndex["COMMAND_HELP_OPTIONAL"] + " ")}{a.Name}>"))}";
public String Alias { get; private set; }
/// <summary>
/// Specifies the name and string that triggers the command
/// </summary>
public string Name
{
get => name;
protected set
{
try
{
name = _config?.Commands[GetType().Name].Name ?? value;
}
catch (KeyNotFoundException)
{
name = value;
}
}
}
private string name;
/// <summary>
/// Specifies the command description
/// </summary>
public string Description { get; protected set; }
/// <summary>
/// Helper property to provide the syntax of the command
/// </summary>
public string Syntax => $"{_translationLookup["COMMAND_HELP_SYNTAX"]} !{Alias} {string.Join(" ", Arguments.Select(a => $"<{(a.Required ? "" : _translationLookup["COMMAND_HELP_OPTIONAL"] + " ")}{a.Name}>"))}";
/// <summary>
/// Alternate name for this command to be executed by
/// </summary>
public string Alias
{
get => alias;
protected set
{
try
{
alias = _config?.Commands[GetType().Name].Alias ?? value;
}
catch (KeyNotFoundException)
{
alias = value;
}
}
}
private string alias;
/// <summary>
/// Helper property to determine the number of required args
/// </summary>
public int RequiredArgumentCount => Arguments.Count(c => c.Required);
public bool RequiresTarget { get; private set; }
public EFClient.Permission Permission { get; private set; }
public CommandArgument[] Arguments { get; private set; }
/// <summary>
/// Indicates if the command requires a target to execute on
/// </summary>
public bool RequiresTarget { get; protected set; }
/// <summary>
/// Minimum permission level to execute command
/// </summary>
public EFClient.Permission Permission
{
get => permission;
protected set
{
try
{
permission = _config?.Commands[GetType().Name].MinimumPermission ?? value;
}
catch (KeyNotFoundException)
{
permission = value;
}
}
}
private EFClient.Permission permission;
/// <summary>
/// Argument list for the command
/// </summary>
public CommandArgument[] Arguments { get; protected set; } = new CommandArgument[0];
}
}