IW4M-Admin/WebfrontCore/Controllers/ConsoleController.cs

82 lines
2.5 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,
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,
Remote = true
};
2018-02-23 02:06:13 -05:00
Manager.GetEventHandler().AddEvent(remoteEvent);
2018-09-06 14:25:58 -04:00
List<CommandResponseInfo> response;
// wait for the event to process
if (!(await remoteEvent.WaitAsync(60 * 1000)).Failed)
2018-09-06 14:25:58 -04:00
{
response = server.CommandResult.Where(c => c.ClientId == client.ClientId).ToList();
// remove the added command response
for (int i = 0; i < response.Count; i++)
{
2018-09-06 14:25:58 -04:00
server.CommandResult.Remove(response[i]);
}
2018-09-06 14:25:58 -04:00
}
2018-02-23 02:06:13 -05:00
2018-09-06 14:25:58 -04:00
else
{
response = new List<CommandResponseInfo>()
{
new CommandResponseInfo()
{
ClientId = client.ClientId,
Response = Utilities.CurrentLocalization.LocalizationIndex["SERVER_ERROR_COMMAND_TIMEOUT"]
}
};
}
2018-02-23 02:06:13 -05:00
return View("_Response", response);
}
}
}