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;
|
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;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
private readonly IConfigurationHandler<StatsConfiguration> _configurationHandler;
|
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,
|
|
|
|
|
IDatabaseContextFactory contextFactory,
|
|
|
|
|
IConfigurationHandler<StatsConfiguration> configurationHandler) : 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;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
_configurationHandler = configurationHandler;
|
2019-12-02 16:52:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-28 21:30:31 -04:00
|
|
|
|
[HttpGet]
|
2019-02-26 22:25:27 -05:00
|
|
|
|
public IActionResult TopPlayersAsync()
|
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"];
|
2021-03-22 12:09:25 -04:00
|
|
|
|
ViewBag.Servers = _manager.GetServers()
|
|
|
|
|
.Select(_server => new ServerInfo() {Name = _server.Hostname, ID = _server.EndPoint});
|
2020-08-17 22:21:11 -04:00
|
|
|
|
ViewBag.Localization = _translationLookup;
|
2018-05-28 21:30:31 -04:00
|
|
|
|
|
2021-03-22 12:09:25 -04:00
|
|
|
|
return View("~/Views/Client/Statistics/Index.cshtml");
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-02 16:52:36 -05:00
|
|
|
|
var server = _manager.GetServers().FirstOrDefault(_server => _server.EndPoint == serverId);
|
2019-02-26 22:25:27 -05:00
|
|
|
|
|
|
|
|
|
if (server != null)
|
|
|
|
|
{
|
2019-08-23 19:34:31 -04:00
|
|
|
|
serverId = StatManager.GetIdForServer(server);
|
2019-02-26 22:25:27 -05:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-22 12:09:25 -04:00
|
|
|
|
var results = _configurationHandler.Configuration().EnableAdvancedMetrics
|
|
|
|
|
? await Plugin.Manager.GetNewTopStats(offset, count, serverId)
|
|
|
|
|
: await Plugin.Manager.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();
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-22 12:09:25 -04:00
|
|
|
|
ViewBag.UseNewStats = _configurationHandler.Configuration().EnableAdvancedMetrics;
|
|
|
|
|
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
|
|
|
|
|
{_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)
|
|
|
|
|
.ThenInclude(_angles => _angles.Vector)
|
|
|
|
|
.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
|
|
|
|
{
|
2020-11-27 22:52:52 -05:00
|
|
|
|
new MessageResponse()
|
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
|
|
|
|
}
|