2022-01-26 11:32:16 -05:00
|
|
|
|
using System.Linq;
|
2020-04-26 22:12:49 -04:00
|
|
|
|
using System.Threading.Tasks;
|
2022-01-26 11:32:16 -05:00
|
|
|
|
using Data.Models.Client;
|
|
|
|
|
using SharedLibraryCore.Configuration;
|
|
|
|
|
using SharedLibraryCore.Interfaces;
|
2020-04-26 22:12:49 -04:00
|
|
|
|
|
|
|
|
|
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[]
|
|
|
|
|
{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
new CommandArgument
|
2020-04-26 22:12:49 -04:00
|
|
|
|
{
|
|
|
|
|
Name = lookup["COMMANDS_ARGS_COMMANDS"],
|
|
|
|
|
Required = true
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-17 22:58:18 -05:00
|
|
|
|
public override async Task ExecuteAsync(GameEvent gameEvent)
|
2020-04-26 22:12:49 -04:00
|
|
|
|
{
|
2021-01-17 22:58:18 -05:00
|
|
|
|
if (gameEvent.IsTargetingSelf())
|
2020-04-26 22:12:49 -04:00
|
|
|
|
{
|
2021-01-17 22:58:18 -05:00
|
|
|
|
gameEvent.Origin.Tell(_translationLookup["COMMANDS_RUN_AS_SELF"]);
|
2020-04-26 22:12:49 -04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-17 22:58:18 -05:00
|
|
|
|
if (!gameEvent.CanPerformActionOnTarget())
|
2020-04-26 22:12:49 -04:00
|
|
|
|
{
|
2021-07-08 22:12:09 -04:00
|
|
|
|
gameEvent.Origin.Tell(_translationLookup["COMMANDS_RUN_AS_FAIL_PERM"].FormatExt(gameEvent.Target.Name));
|
2020-04-26 22:12:49 -04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-17 22:58:18 -05:00
|
|
|
|
var cmd = $"{Utilities.CommandPrefix}{gameEvent.Data}";
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var impersonatedCommandEvent = new GameEvent
|
2020-04-26 22:12:49 -04:00
|
|
|
|
{
|
|
|
|
|
Type = GameEvent.EventType.Command,
|
2021-01-17 22:58:18 -05:00
|
|
|
|
Origin = gameEvent.Target,
|
|
|
|
|
ImpersonationOrigin = gameEvent.Origin,
|
2020-04-26 22:12:49 -04:00
|
|
|
|
Message = cmd,
|
|
|
|
|
Data = cmd,
|
2021-01-17 22:58:18 -05:00
|
|
|
|
Owner = gameEvent.Owner,
|
|
|
|
|
CorrelationId = gameEvent.CorrelationId
|
2020-04-26 22:12:49 -04:00
|
|
|
|
};
|
2021-01-17 22:58:18 -05:00
|
|
|
|
gameEvent.Owner.Manager.AddEvent(impersonatedCommandEvent);
|
2020-04-26 22:12:49 -04:00
|
|
|
|
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var result = await impersonatedCommandEvent.WaitAsync(Utilities.DefaultCommandTimeout,
|
|
|
|
|
gameEvent.Owner.Manager.CancellationToken);
|
2021-07-08 22:12:09 -04:00
|
|
|
|
await result.WaitAsync(Utilities.DefaultCommandTimeout, gameEvent.Owner.Manager.CancellationToken);
|
2020-04-26 22:12:49 -04:00
|
|
|
|
|
|
|
|
|
// remove the added command response
|
2021-07-08 22:12:09 -04:00
|
|
|
|
// todo: something weird happening making this change required
|
|
|
|
|
var responses = gameEvent.Owner.Manager.ProcessingEvents
|
|
|
|
|
.Where(ev => ev.Value.CorrelationId == impersonatedCommandEvent.CorrelationId)
|
|
|
|
|
.SelectMany(ev => ev.Value.Output)
|
|
|
|
|
.ToList();
|
2022-01-26 11:32:16 -05:00
|
|
|
|
|
2023-04-04 22:53:51 -04:00
|
|
|
|
await gameEvent.Origin.TellAsync(responses.Select(output =>
|
|
|
|
|
_translationLookup["COMMANDS_RUN_AS_SUCCESS"].FormatExt(output)));
|
2020-04-26 22:12:49 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-04 22:53:51 -04:00
|
|
|
|
}
|