2021-09-14 19:12:20 -04:00
|
|
|
|
using System;
|
|
|
|
|
using Microsoft.AspNetCore.Diagnostics;
|
2018-02-21 20:29:23 -05:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2019-12-02 16:52:36 -05:00
|
|
|
|
using SharedLibraryCore;
|
2019-07-24 11:36:37 -04:00
|
|
|
|
using SharedLibraryCore.Dtos;
|
2019-12-02 16:52:36 -05:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
2019-07-24 11:36:37 -04:00
|
|
|
|
using System.Linq;
|
2021-08-26 18:35:05 -04:00
|
|
|
|
using System.Threading;
|
2019-07-24 11:36:37 -04:00
|
|
|
|
using System.Threading.Tasks;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2020-04-13 21:26:13 -04:00
|
|
|
|
using static SharedLibraryCore.Server;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
2018-02-21 20:29:23 -05:00
|
|
|
|
|
|
|
|
|
namespace WebfrontCore.Controllers
|
|
|
|
|
{
|
2018-03-06 02:22:19 -05:00
|
|
|
|
public class HomeController : BaseController
|
2018-02-21 20:29:23 -05:00
|
|
|
|
{
|
2020-05-11 17:10:43 -04:00
|
|
|
|
private readonly ITranslationLookup _translationLookup;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
private readonly ILogger _logger;
|
2021-08-26 18:35:05 -04:00
|
|
|
|
private readonly IServerDataViewer _serverDataViewer;
|
2020-05-11 17:10:43 -04:00
|
|
|
|
|
2021-03-22 12:09:25 -04:00
|
|
|
|
public HomeController(ILogger<HomeController> logger, IManager manager, ITranslationLookup translationLookup,
|
2021-08-26 18:35:05 -04:00
|
|
|
|
IServerDataViewer serverDataViewer) : base(manager)
|
2020-01-31 21:15:07 -05:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
_logger = logger;
|
2020-05-11 17:10:43 -04:00
|
|
|
|
_translationLookup = translationLookup;
|
2021-08-26 18:35:05 -04:00
|
|
|
|
_serverDataViewer = serverDataViewer;
|
2019-12-02 16:52:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-26 18:35:05 -04:00
|
|
|
|
public async Task<IActionResult> Index(Game? game = null, CancellationToken cancellationToken = default)
|
2018-02-21 20:29:23 -05:00
|
|
|
|
{
|
2021-01-08 20:21:23 -05:00
|
|
|
|
ViewBag.Description = Localization["WEBFRONT_DESCRIPTION_HOME"];
|
2018-05-05 18:52:04 -04:00
|
|
|
|
ViewBag.Title = Localization["WEBFRONT_HOME_TITLE"];
|
2021-01-08 20:21:23 -05:00
|
|
|
|
ViewBag.Keywords = Localization["WEBFRONT_KEWORDS_HOME"];
|
2018-03-13 17:30:22 -04:00
|
|
|
|
|
2021-03-22 12:09:25 -04:00
|
|
|
|
var servers = Manager.GetServers().Where(_server => !game.HasValue || _server.GameName == game);
|
2021-09-14 19:12:20 -04:00
|
|
|
|
var (clientCount, time) = await _serverDataViewer.MaxConcurrentClientsAsync(token: cancellationToken);
|
2021-08-26 18:35:05 -04:00
|
|
|
|
var (count, recentCount) = await _serverDataViewer.ClientCountsAsync(token: cancellationToken);
|
2020-04-13 21:26:13 -04:00
|
|
|
|
|
2019-07-24 11:36:37 -04:00
|
|
|
|
var model = new IW4MAdminInfo()
|
|
|
|
|
{
|
2020-04-13 21:26:13 -04:00
|
|
|
|
TotalAvailableClientSlots = servers.Sum(_server => _server.MaxClients),
|
|
|
|
|
TotalOccupiedClientSlots = servers.SelectMany(_server => _server.GetClientsAsList()).Count(),
|
2021-03-22 12:09:25 -04:00
|
|
|
|
TotalClientCount = count,
|
|
|
|
|
RecentClientCount = recentCount,
|
2021-09-14 19:12:20 -04:00
|
|
|
|
MaxConcurrentClients = clientCount ?? 0,
|
|
|
|
|
MaxConcurrentClientsTime = time ?? DateTime.UtcNow,
|
2020-04-13 21:26:13 -04:00
|
|
|
|
Game = game,
|
|
|
|
|
ActiveServerGames = Manager.GetServers().Select(_server => _server.GameName).Distinct().ToArray()
|
2019-07-24 11:36:37 -04:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return View(model);
|
2018-02-21 20:29:23 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IActionResult Error()
|
|
|
|
|
{
|
2018-05-14 13:55:10 -04:00
|
|
|
|
var exceptionFeature = HttpContext.Features.Get<IExceptionHandlerPathFeature>();
|
2021-03-22 12:09:25 -04:00
|
|
|
|
_logger.LogError("[Webfront] {path} {message} {@exception}", exceptionFeature.Path,
|
|
|
|
|
exceptionFeature.Error.Message, exceptionFeature.Error);
|
2018-05-05 18:52:04 -04:00
|
|
|
|
ViewBag.Description = Localization["WEBFRONT_ERROR_DESC"];
|
|
|
|
|
ViewBag.Title = Localization["WEBFRONT_ERROR_TITLE"];
|
2019-10-23 14:35:20 -04:00
|
|
|
|
return View(exceptionFeature.Error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IActionResult ResponseStatusCode(int? statusCode = null)
|
|
|
|
|
{
|
|
|
|
|
return View(statusCode);
|
2018-02-21 20:29:23 -05:00
|
|
|
|
}
|
2019-12-28 21:44:39 -05:00
|
|
|
|
|
|
|
|
|
public IActionResult Help()
|
|
|
|
|
{
|
|
|
|
|
ViewBag.IsFluid = true;
|
2019-12-29 12:32:36 -05:00
|
|
|
|
ViewBag.Title = Localization["WEBFRONT_NAV_HELP"];
|
2020-07-31 21:40:03 -04:00
|
|
|
|
ViewBag.CommandPrefix = Manager.GetApplicationSettings().Configuration().CommandPrefix;
|
2019-12-29 12:32:36 -05:00
|
|
|
|
|
|
|
|
|
// we don't need to the name of the shared library assembly
|
2019-12-28 21:44:39 -05:00
|
|
|
|
var commands = Manager.GetCommands()
|
2022-07-05 16:57:39 -04:00
|
|
|
|
.Where(command => command.Permission <= Client.Level)
|
|
|
|
|
.OrderByDescending(command => command.Permission)
|
|
|
|
|
.GroupBy(command =>
|
2019-12-28 21:44:39 -05:00
|
|
|
|
{
|
2022-07-05 16:57:39 -04:00
|
|
|
|
if (command.GetType().Name == "ScriptCommand")
|
|
|
|
|
{
|
|
|
|
|
return _translationLookup["WEBFRONT_HELP_SCRIPT_PLUGIN"];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var assemblyName = command.GetType().Assembly.GetName().Name;
|
|
|
|
|
if (assemblyName is "IW4MAdmin" or "SharedLibraryCore")
|
|
|
|
|
{
|
|
|
|
|
return _translationLookup["WEBFRONT_HELP_COMMAND_NATIVE"];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var pluginType = command.GetType().Assembly.GetTypes()
|
|
|
|
|
.FirstOrDefault(type => typeof(IPlugin).IsAssignableFrom(type));
|
|
|
|
|
return Manager.Plugins.FirstOrDefault(plugin => plugin.GetType() == pluginType)?.Name ??
|
|
|
|
|
_translationLookup["WEBFRONT_HELP_COMMAND_NATIVE"];
|
2019-12-28 21:44:39 -05:00
|
|
|
|
})
|
2022-07-05 16:57:39 -04:00
|
|
|
|
.Select(group => (group.Key, group.AsEnumerable()));
|
2019-12-28 21:44:39 -05:00
|
|
|
|
|
|
|
|
|
return View(commands);
|
|
|
|
|
}
|
2018-02-21 20:29:23 -05:00
|
|
|
|
}
|
2022-02-07 19:43:36 -05:00
|
|
|
|
}
|