2020-05-11 17:10:43 -04:00
|
|
|
|
using SharedLibraryCore;
|
|
|
|
|
using SharedLibraryCore.Commands;
|
|
|
|
|
using SharedLibraryCore.Configuration;
|
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
using Data.Models.Client;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
2020-05-11 17:10:43 -04:00
|
|
|
|
|
|
|
|
|
namespace IW4MAdmin.Application.Misc
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// generic script command implementation
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class ScriptCommand : Command
|
|
|
|
|
{
|
2022-02-07 19:43:36 -05:00
|
|
|
|
private readonly Func<GameEvent, Task> _executeAction;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
private readonly ILogger _logger;
|
2020-05-11 17:10:43 -04:00
|
|
|
|
|
2022-02-07 19:43:36 -05:00
|
|
|
|
public ScriptCommand(string name, string alias, string description, bool isTargetRequired,
|
|
|
|
|
EFClient.Permission permission,
|
|
|
|
|
CommandArgument[] args, Func<GameEvent, Task> executeAction, CommandConfiguration config,
|
|
|
|
|
ITranslationLookup layout, ILogger<ScriptCommand> logger, Server.Game[] supportedGames)
|
2020-05-11 17:10:43 -04:00
|
|
|
|
: base(config, layout)
|
|
|
|
|
{
|
|
|
|
|
_executeAction = executeAction;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
_logger = logger;
|
2020-05-11 17:10:43 -04:00
|
|
|
|
Name = name;
|
|
|
|
|
Alias = alias;
|
|
|
|
|
Description = description;
|
2020-09-28 21:32:53 -04:00
|
|
|
|
RequiresTarget = isTargetRequired;
|
2020-05-11 17:10:43 -04:00
|
|
|
|
Permission = permission;
|
|
|
|
|
Arguments = args;
|
2022-02-07 19:43:36 -05:00
|
|
|
|
SupportedGames = supportedGames;
|
2020-05-11 17:10:43 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
public override async Task ExecuteAsync(GameEvent e)
|
2020-05-11 17:10:43 -04:00
|
|
|
|
{
|
|
|
|
|
if (_executeAction == null)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException($"No execute action defined for command \"{Name}\"");
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
try
|
|
|
|
|
{
|
2022-02-07 19:43:36 -05:00
|
|
|
|
await _executeAction(e);
|
2020-11-11 18:31:26 -05:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Failed to execute ScriptCommand action for command {command} {@event}", Name, e);
|
|
|
|
|
}
|
2020-05-11 17:10:43 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|