2019-12-02 16:52:36 -05:00
|
|
|
|
using IW4MAdmin.Plugins.Stats;
|
|
|
|
|
using IW4MAdmin.Plugins.Stats.Helpers;
|
2019-02-26 22:25:27 -05:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2018-06-05 17:31:36 -04:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2018-05-28 21:30:31 -04:00
|
|
|
|
using SharedLibraryCore;
|
2019-02-25 20:36:10 -05:00
|
|
|
|
using SharedLibraryCore.Dtos;
|
2020-08-20 11:38:11 -04:00
|
|
|
|
using SharedLibraryCore.Dtos.Meta.Responses;
|
2019-12-02 16:52:36 -05:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
2020-08-20 11:38:11 -04:00
|
|
|
|
using Stats.Dtos;
|
2018-05-28 21:30:31 -04:00
|
|
|
|
using System;
|
2020-11-12 21:39:56 -05:00
|
|
|
|
using System.Collections.Generic;
|
2018-06-05 17:31:36 -04:00
|
|
|
|
using System.Linq;
|
2022-06-09 10:56:41 -04:00
|
|
|
|
using System.Threading;
|
2018-05-28 21:30:31 -04:00
|
|
|
|
using System.Threading.Tasks;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
using Data.Abstractions;
|
|
|
|
|
using IW4MAdmin.Plugins.Stats.Config;
|
2021-11-23 18:26:33 -05:00
|
|
|
|
using Stats.Config;
|
2018-05-28 21:30:31 -04:00
|
|
|
|
|
2019-12-02 16:52:36 -05:00
|
|
|
|
namespace IW4MAdmin.Plugins.Web.StatsWeb.Controllers
|
2018-05-28 21:30:31 -04:00
|
|
|
|
{
|
|
|
|
|
public class StatsController : BaseController
|
|
|
|
|
{
|
2020-05-22 21:29:41 -04:00
|
|
|
|
private readonly ILogger _logger;
|
2019-12-02 16:52:36 -05:00
|
|
|
|
private readonly IManager _manager;
|
2020-08-20 11:38:11 -04:00
|
|
|
|
private readonly IResourceQueryHelper<ChatSearchQuery, MessageResponse> _chatResourceQueryHelper;
|
2020-05-22 21:29:41 -04:00
|
|
|
|
private readonly ITranslationLookup _translationLookup;
|
2020-11-27 22:52:52 -05:00
|
|
|
|
private readonly IDatabaseContextFactory _contextFactory;
|
2022-01-28 18:28:49 -05:00
|
|
|
|
private readonly StatsConfiguration _config;
|
2022-06-09 10:56:41 -04:00
|
|
|
|
private readonly IServerDataViewer _serverDataViewer;
|
2023-04-04 23:10:37 -04:00
|
|
|
|
private readonly StatManager _statManager;
|
2019-12-02 16:52:36 -05:00
|
|
|
|
|
2021-03-22 12:09:25 -04:00
|
|
|
|
public StatsController(ILogger<StatsController> logger, IManager manager, IResourceQueryHelper<ChatSearchQuery,
|
|
|
|
|
MessageResponse> resourceQueryHelper, ITranslationLookup translationLookup,
|
2023-04-04 23:10:37 -04:00
|
|
|
|
IDatabaseContextFactory contextFactory, StatsConfiguration config, IServerDataViewer serverDataViewer, StatManager statManager) : base(manager)
|
2019-12-02 16:52:36 -05:00
|
|
|
|
{
|
2020-05-22 21:29:41 -04:00
|
|
|
|
_logger = logger;
|
2019-12-02 16:52:36 -05:00
|
|
|
|
_manager = manager;
|
2020-05-22 21:29:41 -04:00
|
|
|
|
_chatResourceQueryHelper = resourceQueryHelper;
|
|
|
|
|
_translationLookup = translationLookup;
|
2020-11-27 22:52:52 -05:00
|
|
|
|
_contextFactory = contextFactory;
|
2022-01-28 18:28:49 -05:00
|
|
|
|
_config = config;
|
2022-06-09 10:56:41 -04:00
|
|
|
|
_serverDataViewer = serverDataViewer;
|
2023-04-04 23:10:37 -04:00
|
|
|
|
_statManager = statManager;
|
2019-12-02 16:52:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-28 21:30:31 -04:00
|
|
|
|
[HttpGet]
|
2022-06-09 10:56:41 -04:00
|
|
|
|
public async Task<IActionResult> TopPlayers(string serverId = null, CancellationToken token = default)
|
2018-05-28 21:30:31 -04:00
|
|
|
|
{
|
2019-03-24 22:34:20 -04:00
|
|
|
|
ViewBag.Title = Utilities.CurrentLocalization.LocalizationIndex["WEBFRONT_STATS_INDEX_TITLE"];
|
|
|
|
|
ViewBag.Description = Utilities.CurrentLocalization.LocalizationIndex["WEBFRONT_STATS_INDEX_DESC"];
|
2020-08-17 22:21:11 -04:00
|
|
|
|
ViewBag.Localization = _translationLookup;
|
2022-04-19 19:43:58 -04:00
|
|
|
|
ViewBag.SelectedServerId = serverId;
|
2022-06-09 10:56:41 -04:00
|
|
|
|
|
2023-04-04 23:10:37 -04:00
|
|
|
|
var server = _manager.GetServers().FirstOrDefault(server => server.Id == serverId) as IGameServer;
|
2022-06-09 10:56:41 -04:00
|
|
|
|
long? matchedServerId = null;
|
|
|
|
|
|
|
|
|
|
if (server != null)
|
|
|
|
|
{
|
2023-04-04 23:10:37 -04:00
|
|
|
|
matchedServerId = server.LegacyDatabaseId;
|
2022-06-09 10:56:41 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ViewBag.TotalRankedClients = await _serverDataViewer.RankedClientsCountAsync(matchedServerId, token);
|
2022-06-09 14:50:58 -04:00
|
|
|
|
ViewBag.ServerId = matchedServerId;
|
2022-04-19 19:43:58 -04:00
|
|
|
|
|
|
|
|
|
return View("~/Views/Client/Statistics/Index.cshtml", _manager.GetServers()
|
2023-04-04 23:10:37 -04:00
|
|
|
|
.Select(selectedServer => new ServerInfo
|
2022-04-19 19:43:58 -04:00
|
|
|
|
{
|
2023-04-04 23:10:37 -04:00
|
|
|
|
Name = selectedServer.Hostname,
|
|
|
|
|
IPAddress = selectedServer.ListenAddress,
|
|
|
|
|
Port = selectedServer.ListenPort
|
2022-04-19 19:43:58 -04:00
|
|
|
|
}));
|
2018-05-28 21:30:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
2019-02-26 22:25:27 -05:00
|
|
|
|
public async Task<IActionResult> GetTopPlayersAsync(int count, int offset, long? serverId = null)
|
2018-05-28 21:30:31 -04:00
|
|
|
|
{
|
2019-02-26 22:25:27 -05:00
|
|
|
|
// this prevents empty results when we really want aggregate
|
|
|
|
|
if (serverId == 0)
|
|
|
|
|
{
|
|
|
|
|
serverId = null;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-04 23:10:37 -04:00
|
|
|
|
if (_manager.GetServers().FirstOrDefault(activeServer => activeServer.EndPoint == serverId) is IGameServer server)
|
2019-02-26 22:25:27 -05:00
|
|
|
|
{
|
2023-04-04 23:10:37 -04:00
|
|
|
|
serverId = server.LegacyDatabaseId;
|
2019-02-26 22:25:27 -05:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-28 18:28:49 -05:00
|
|
|
|
var results = _config?.EnableAdvancedMetrics ?? true
|
2023-04-04 23:10:37 -04:00
|
|
|
|
? await _statManager.GetNewTopStats(offset, count, serverId)
|
|
|
|
|
: await _statManager.GetTopStats(offset, count, serverId);
|
2019-02-26 22:25:27 -05:00
|
|
|
|
|
|
|
|
|
// this returns an empty result so we know to stale the loader
|
|
|
|
|
if (results.Count == 0 && offset > 0)
|
|
|
|
|
{
|
|
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-28 18:28:49 -05:00
|
|
|
|
ViewBag.UseNewStats = _config?.EnableAdvancedMetrics;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
return View("~/Views/Client/Statistics/Components/TopPlayers/_List.cshtml", results);
|
2018-05-28 21:30:31 -04:00
|
|
|
|
}
|
2018-06-05 17:31:36 -04:00
|
|
|
|
|
|
|
|
|
[HttpGet]
|
2020-08-20 11:38:11 -04:00
|
|
|
|
public async Task<IActionResult> GetMessageAsync(string serverId, long when)
|
2018-06-05 17:31:36 -04:00
|
|
|
|
{
|
2019-04-08 21:31:32 -04:00
|
|
|
|
var whenTime = DateTime.FromFileTimeUtc(when);
|
|
|
|
|
var whenUpper = whenTime.AddMinutes(5);
|
|
|
|
|
var whenLower = whenTime.AddMinutes(-5);
|
2018-06-05 17:31:36 -04:00
|
|
|
|
|
2022-01-25 15:24:27 -05:00
|
|
|
|
var messages = await _chatResourceQueryHelper.QueryResource(new ChatSearchQuery
|
2018-06-05 17:31:36 -04:00
|
|
|
|
{
|
2020-08-20 11:38:11 -04:00
|
|
|
|
ServerId = serverId,
|
|
|
|
|
SentBefore = whenUpper,
|
|
|
|
|
SentAfter = whenLower
|
|
|
|
|
});
|
|
|
|
|
|
2022-01-25 15:24:27 -05:00
|
|
|
|
return View("~/Views/Client/_MessageContext.cshtml",
|
|
|
|
|
messages.Results.OrderBy(message => message.When).ToList());
|
2018-06-05 17:31:36 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-22 21:29:41 -04:00
|
|
|
|
[HttpGet("Message/Find")]
|
2020-08-20 11:38:11 -04:00
|
|
|
|
public async Task<IActionResult> FindMessage([FromQuery] string query)
|
2020-05-22 21:29:41 -04:00
|
|
|
|
{
|
|
|
|
|
ViewBag.Localization = _translationLookup;
|
|
|
|
|
ViewBag.EnableColorCodes = _manager.GetApplicationSettings().Configuration().EnableColorCodes;
|
|
|
|
|
ViewBag.Query = query;
|
|
|
|
|
ViewBag.QueryLimit = 100;
|
|
|
|
|
ViewBag.Title = _translationLookup["WEBFRONT_STATS_MESSAGES_TITLE"];
|
|
|
|
|
ViewBag.Error = null;
|
|
|
|
|
ViewBag.IsFluid = true;
|
|
|
|
|
ChatSearchQuery searchRequest = null;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
searchRequest = query.ParseSearchInfo(int.MaxValue, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (ArgumentException e)
|
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
_logger.LogWarning(e, "Could not parse chat message search query {query}", query);
|
2020-05-22 21:29:41 -04:00
|
|
|
|
ViewBag.Error = e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (FormatException e)
|
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
_logger.LogWarning(e, "Could not parse chat message search query filter format {query}", query);
|
2020-05-22 21:29:41 -04:00
|
|
|
|
ViewBag.Error = e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result = searchRequest != null ? await _chatResourceQueryHelper.QueryResource(searchRequest) : null;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
return View("~/Views/Client/Message/Find.cshtml", result);
|
2020-05-22 21:29:41 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet("Message/FindNext")]
|
2021-03-22 12:09:25 -04:00
|
|
|
|
public async Task<IActionResult> FindNextMessages([FromQuery] string query, [FromQuery] int count,
|
|
|
|
|
[FromQuery] int offset)
|
2020-05-22 21:29:41 -04:00
|
|
|
|
{
|
|
|
|
|
ChatSearchQuery searchRequest;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
searchRequest = query.ParseSearchInfo(count, offset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (ArgumentException e)
|
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
_logger.LogWarning(e, "Could not parse chat message search query {query}", query);
|
2020-05-22 21:29:41 -04:00
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (FormatException e)
|
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
_logger.LogWarning(e, "Could not parse chat message search query filter format {query}", query);
|
2020-05-22 21:29:41 -04:00
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result = await _chatResourceQueryHelper.QueryResource(searchRequest);
|
2021-03-22 12:09:25 -04:00
|
|
|
|
return PartialView("~/Views/Client/Message/_Item.cshtml", result.Results);
|
2020-05-22 21:29:41 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-05 17:31:36 -04:00
|
|
|
|
[HttpGet]
|
2018-09-04 22:07:34 -04:00
|
|
|
|
[Authorize]
|
2019-08-06 14:36:37 -04:00
|
|
|
|
public async Task<IActionResult> GetAutomatedPenaltyInfoAsync(int penaltyId)
|
2018-06-05 17:31:36 -04:00
|
|
|
|
{
|
2020-11-27 22:52:52 -05:00
|
|
|
|
await using var context = _contextFactory.CreateContext(false);
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2020-11-27 22:52:52 -05:00
|
|
|
|
var penalty = await context.Penalties
|
2021-03-22 12:09:25 -04:00
|
|
|
|
.Select(_penalty => new
|
2022-04-19 19:43:58 -04:00
|
|
|
|
{ _penalty.OffenderId, _penalty.PenaltyId, _penalty.When, _penalty.AutomatedOffense })
|
2020-11-27 22:52:52 -05:00
|
|
|
|
.FirstOrDefaultAsync(_penalty => _penalty.PenaltyId == penaltyId);
|
|
|
|
|
|
|
|
|
|
if (penalty == null)
|
2018-06-05 17:31:36 -04:00
|
|
|
|
{
|
2020-11-27 22:52:52 -05:00
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
2019-03-31 20:56:31 -04:00
|
|
|
|
|
2020-11-27 22:52:52 -05:00
|
|
|
|
// todo: this can be optimized
|
2021-03-22 12:09:25 -04:00
|
|
|
|
var iqSnapshotInfo = context.ACSnapshots
|
2020-11-27 22:52:52 -05:00
|
|
|
|
.Where(s => s.ClientId == penalty.OffenderId)
|
|
|
|
|
.Include(s => s.LastStrainAngle)
|
|
|
|
|
.Include(s => s.HitOrigin)
|
|
|
|
|
.Include(s => s.HitDestination)
|
|
|
|
|
.Include(s => s.CurrentViewAngle)
|
2021-06-29 16:02:01 -04:00
|
|
|
|
.Include(s => s.Server)
|
2020-11-27 22:52:52 -05:00
|
|
|
|
.Include(s => s.PredictedViewAngles)
|
2023-04-04 23:10:37 -04:00
|
|
|
|
.ThenInclude(angles => angles.Vector)
|
2020-11-27 22:52:52 -05:00
|
|
|
|
.OrderBy(s => s.When)
|
|
|
|
|
.ThenBy(s => s.Hits);
|
|
|
|
|
|
|
|
|
|
var penaltyInfo = await iqSnapshotInfo.ToListAsync();
|
|
|
|
|
|
|
|
|
|
if (penaltyInfo.Count > 0)
|
|
|
|
|
{
|
2021-03-22 12:09:25 -04:00
|
|
|
|
return View("~/Views/Client/_PenaltyInfo.cshtml", penaltyInfo);
|
2020-11-27 22:52:52 -05:00
|
|
|
|
}
|
2018-06-05 17:31:36 -04:00
|
|
|
|
|
2020-11-27 22:52:52 -05:00
|
|
|
|
// we want to show anything related to the automated offense
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-03-22 12:09:25 -04:00
|
|
|
|
return View("~/Views/Client/_MessageContext.cshtml", new List<MessageResponse>
|
2019-08-06 14:36:37 -04:00
|
|
|
|
{
|
2022-07-05 13:42:17 -04:00
|
|
|
|
new()
|
2019-08-06 14:36:37 -04:00
|
|
|
|
{
|
2020-11-27 22:52:52 -05:00
|
|
|
|
ClientId = penalty.OffenderId,
|
|
|
|
|
Message = penalty.AutomatedOffense,
|
|
|
|
|
When = penalty.When
|
|
|
|
|
}
|
|
|
|
|
});
|
2018-06-05 17:31:36 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-28 21:30:31 -04:00
|
|
|
|
}
|
2022-01-25 15:24:27 -05:00
|
|
|
|
}
|