IW4M-Admin/WebfrontCore/Controllers/ConsoleController.cs

62 lines
1.9 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.Dtos;
using SharedLibraryCore.Objects;
2018-02-23 02:06:13 -05:00
using System;
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.GetHashCode(),
});
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(int serverId, string command)
{
var server = Manager.GetServers().First(s => s.GetHashCode() == serverId);
var client = new Player()
{
ClientId = Client.ClientId,
Level = Client.Level,
CurrentServer = server,
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);
// wait for the event to process
2018-05-10 01:34:29 -04:00
await Task.Run(() => remoteEvent.OnProcessed.Wait(5000));
var response = server.CommandResult.Where(c => c.ClientId == client.ClientId).ToList();
2018-02-23 02:06:13 -05:00
// remove the added command response
for (int i = 0; i < response.Count; i++)
server.CommandResult.Remove(response[i]);
return View("_Response", response);
}
}
}