IW4M-Admin/Application/Plugin/Script/ScriptCommand.cs

59 lines
1.9 KiB
C#
Raw Normal View History

2023-04-04 19:24:13 -04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
2023-04-04 19:24:13 -04:00
using Data.Models;
using Data.Models.Client;
using Microsoft.Extensions.Logging;
2023-04-04 19:24:13 -04:00
using SharedLibraryCore;
using SharedLibraryCore.Commands;
using SharedLibraryCore.Configuration;
using SharedLibraryCore.Interfaces;
using ILogger = Microsoft.Extensions.Logging.ILogger;
2023-04-04 19:24:13 -04:00
namespace IW4MAdmin.Application.Plugin.Script
{
/// <summary>
/// generic script command implementation
/// </summary>
public class ScriptCommand : Command
{
private readonly Func<GameEvent, Task> _executeAction;
private readonly ILogger _logger;
public ScriptCommand(string name, string alias, string description, bool isTargetRequired,
EFClient.Permission permission,
2023-04-04 19:24:13 -04:00
IEnumerable<CommandArgument> args, Func<GameEvent, Task> executeAction, CommandConfiguration config,
ITranslationLookup layout, ILogger<ScriptCommand> logger, IEnumerable<Reference.Game> supportedGames)
: base(config, layout)
{
_executeAction = executeAction;
_logger = logger;
Name = name;
Alias = alias;
Description = description;
RequiresTarget = isTargetRequired;
Permission = permission;
2023-04-04 19:24:13 -04:00
Arguments = args.ToArray();
SupportedGames = supportedGames?.Select(game => (Server.Game)game).ToArray();
}
public override async Task ExecuteAsync(GameEvent e)
{
if (_executeAction == null)
{
throw new InvalidOperationException($"No execute action defined for command \"{Name}\"");
}
try
{
await _executeAction(e);
}
catch (Exception ex)
{
2023-04-04 19:24:13 -04:00
_logger.LogError(ex, "Failed to execute ScriptCommand action for command {Command} {@Event}", Name, e);
}
}
}
}