using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Data.Models;
using Data.Models.Client;
using Microsoft.Extensions.Logging;
using SharedLibraryCore;
using SharedLibraryCore.Commands;
using SharedLibraryCore.Configuration;
using SharedLibraryCore.Interfaces;
using ILogger = Microsoft.Extensions.Logging.ILogger;
namespace IW4MAdmin.Application.Plugin.Script
{
///
/// generic script command implementation
///
public class ScriptCommand : Command
{
private readonly Func _executeAction;
private readonly ILogger _logger;
public ScriptCommand(string name, string alias, string description, bool isTargetRequired,
EFClient.Permission permission,
IEnumerable args, Func executeAction, CommandConfiguration config,
ITranslationLookup layout, ILogger logger, IEnumerable supportedGames)
: base(config, layout)
{
_executeAction = executeAction;
_logger = logger;
Name = name;
Alias = alias;
Description = description;
RequiresTarget = isTargetRequired;
Permission = permission;
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)
{
_logger.LogError(ex, "Failed to execute ScriptCommand action for command {Command} {@Event}", Name, e);
}
}
}
}