IW4M-Admin/WebfrontCore/Controllers/ConsoleController.cs

100 lines
3.3 KiB
C#
Raw Normal View History

2018-02-23 02:06:13 -05:00
using Microsoft.AspNetCore.Mvc;
2018-04-08 02:44:42 -04:00
using SharedLibraryCore;
using SharedLibraryCore.Database.Models;
2018-04-08 02:44:42 -04:00
using SharedLibraryCore.Dtos;
2018-02-23 02:06:13 -05:00
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace WebfrontCore.Controllers
{
2018-03-06 02:22:19 -05:00
public class ConsoleController : BaseController
2018-02-23 02:06:13 -05:00
{
public IActionResult Index()
{
var activeServers = Manager.GetServers().Select(s => new ServerInfo()
2018-02-23 02:06:13 -05:00
{
Name = s.Hostname,
ID = s.EndPoint,
2018-02-23 02:06:13 -05:00
});
ViewBag.Description = "Use the IW4MAdmin web console to execute commands";
ViewBag.Title = Localization["WEBFRONT_CONSOLE_TITLE"];
ViewBag.Keywords = "IW4MAdmin, console, execute, commands";
2018-02-23 02:06:13 -05:00
return View(activeServers);
}
public async Task<IActionResult> ExecuteAsync(long serverId, string command)
2018-02-23 02:06:13 -05:00
{
var server = Manager.GetServers().First(s => s.EndPoint == serverId);
var client = new EFClient()
{
ClientId = Client.ClientId,
Level = Client.Level,
NetworkId = Client.NetworkId,
CurrentServer = server,
CurrentAlias = new EFAlias()
{
Name = Client.Name
}
};
var remoteEvent = new GameEvent()
{
2018-05-10 01:34:29 -04:00
Type = GameEvent.EventType.Command,
Data = command,
Origin = client,
2018-04-14 00:51:38 -04:00
Owner = server,
IsRemote = true
};
2018-02-23 02:06:13 -05:00
Manager.GetEventHandler().AddEvent(remoteEvent);
List<CommandResponseInfo> response = null;
try
2018-09-06 14:25:58 -04:00
{
var completedEvent = await remoteEvent.WaitAsync(Utilities.DefaultCommandTimeout, server.Manager.CancellationToken);
// wait for the event to process
if (!completedEvent.Failed)
{
response = server.CommandResult.Where(c => c.ClientId == client.ClientId).ToList();
2018-09-06 14:25:58 -04:00
// remove the added command response
for (int i = 0; i < response.Count; i++)
{
server.CommandResult.Remove(response[i]);
}
}
else if (completedEvent.FailReason == GameEvent.EventFailReason.Timeout)
{
response = new List<CommandResponseInfo>()
{
new CommandResponseInfo()
{
ClientId = client.ClientId,
Response = Utilities.CurrentLocalization.LocalizationIndex["SERVER_ERROR_COMMAND_TIMEOUT"]
}
};
}
2018-09-06 14:25:58 -04:00
}
2018-02-23 02:06:13 -05:00
catch (System.OperationCanceledException)
2018-09-06 14:25:58 -04:00
{
response = new List<CommandResponseInfo>()
{
new CommandResponseInfo()
{
ClientId = client.ClientId,
Response = Utilities.CurrentLocalization.LocalizationIndex["COMMADS_RESTART_SUCCESS"]
2018-09-06 14:25:58 -04:00
}
};
}
2018-02-23 02:06:13 -05:00
return View("_Response", response);
}
}
}