IW4M-Admin/SharedLibraryCore/Commands/RunAsCommand.cs
RaidMax 23a33ba489 implement more robust command api and login
improve web console command response reliability and consistency
2021-01-17 21:58:18 -06:00

65 lines
2.1 KiB
C#

using SharedLibraryCore.Configuration;
using SharedLibraryCore.Database.Models;
using SharedLibraryCore.Interfaces;
using System.Linq;
using System.Threading.Tasks;
namespace SharedLibraryCore.Commands
{
public class RunAsCommand : Command
{
public RunAsCommand(CommandConfiguration config, ITranslationLookup lookup) : base(config, lookup)
{
Name = "runas";
Description = lookup["COMMANDS_RUN_AS_DESC"];
Alias = "ra";
Permission = EFClient.Permission.Moderator;
RequiresTarget = true;
Arguments = new[]
{
new CommandArgument()
{
Name = lookup["COMMANDS_ARGS_COMMANDS"],
Required = true
}
};
}
public override async Task ExecuteAsync(GameEvent gameEvent)
{
if (gameEvent.IsTargetingSelf())
{
gameEvent.Origin.Tell(_translationLookup["COMMANDS_RUN_AS_SELF"]);
return;
}
if (!gameEvent.CanPerformActionOnTarget())
{
gameEvent.Origin.Tell(_translationLookup["COMMANDS_RUN_AS_FAIL_PERM"]);
return;
}
var cmd = $"{Utilities.CommandPrefix}{gameEvent.Data}";
var impersonatedCommandEvent = new GameEvent()
{
Type = GameEvent.EventType.Command,
Origin = gameEvent.Target,
ImpersonationOrigin = gameEvent.Origin,
Message = cmd,
Data = cmd,
Owner = gameEvent.Owner,
CorrelationId = gameEvent.CorrelationId
};
gameEvent.Owner.Manager.AddEvent(impersonatedCommandEvent);
var result = await impersonatedCommandEvent.WaitAsync(Utilities.DefaultCommandTimeout, gameEvent.Owner.Manager.CancellationToken);
// remove the added command response
foreach (var output in result.Output)
{
gameEvent.Origin.Tell(_translationLookup["COMMANDS_RUN_AS_SUCCESS"].FormatExt(output));
}
}
}
}