IW4M-Admin/WebfrontCore/Controllers/ConsoleController.cs
RaidMax 46bdc2ac33 moved event API stuff around
finally fixed threading issue (which actually had to do with IW4x log outputs being out of sync (not an issue with my code). What a lot of headache over something that wasn't my fault.
2018-08-30 20:53:00 -05:00

62 lines
1.9 KiB
C#

using Microsoft.AspNetCore.Mvc;
using SharedLibraryCore;
using SharedLibraryCore.Dtos;
using SharedLibraryCore.Objects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace WebfrontCore.Controllers
{
public class ConsoleController : BaseController
{
public IActionResult Index()
{
var activeServers = Manager.GetServers().Select(s => new ServerInfo()
{
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";
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()
{
Type = GameEvent.EventType.Command,
Data = command,
Origin = client,
Owner = server,
Remote = true
};
Manager.GetEventHandler().AddEvent(remoteEvent);
// wait for the event to process
await remoteEvent.OnProcessed.WaitAsync(60*1000);
var response = server.CommandResult.Where(c => c.ClientId == client.ClientId).ToList();
// remove the added command response
for (int i = 0; i < response.Count; i++)
server.CommandResult.Remove(response[i]);
return View("_Response", response);
}
}
}