[issue #139] client lookup and stats api

This commit is contained in:
RaidMax
2020-05-24 21:22:26 -05:00
parent 4457ee5461
commit 30f2f7bf09
32 changed files with 907 additions and 36 deletions

View File

@ -10,7 +10,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.SyndicationFeed.ReaderWriter" Version="1.0.2" />
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2.2.11" PrivateAssets="All" />
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2.4.2" PrivateAssets="All" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">

View File

@ -10,7 +10,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2.2.11" PrivateAssets="All" />
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2.4.2" PrivateAssets="All" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">

View File

@ -16,7 +16,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2.2.11" PrivateAssets="All" />
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2.4.2" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>

View File

@ -23,7 +23,7 @@
</Target>
<ItemGroup>
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2.2.11" PrivateAssets="All" />
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2.4.2" PrivateAssets="All" />
</ItemGroup>
</Project>

View File

@ -16,7 +16,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2.2.11" PrivateAssets="All" />
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2.4.2" PrivateAssets="All" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">

View File

@ -0,0 +1,10 @@
namespace Stats.Dtos
{
public class StatsInfoRequest
{
/// <summary>
/// client identifier
/// </summary>
public int? ClientId { get; set; }
}
}

View File

@ -0,0 +1,56 @@
using System;
using System.Text.Json.Serialization;
namespace Stats.Dtos
{
public class StatsInfoResult
{
/// <summary>
/// ranking on the server
/// </summary>
public int Ranking { get; set; }
/// <summary>
/// number of kills
/// </summary>
public int Kills { get; set; }
/// <summary>
/// number of deaths
/// </summary>
public int Deaths { get; set; }
/// <summary>
/// performance level (elo rating + skill) / 2
/// </summary>
public double Performance { get; set; }
/// <summary>
/// SPM
/// </summary>
public double ScorePerMinute { get; set; }
/// <summary>
/// last connection
/// </summary>
public DateTime LastPlayed { get; set; }
/// <summary>
/// how many seconds played on the server
/// </summary>
public double TotalSecondsPlayed { get; set; }
/// <summary>
/// name of the server
/// </summary>
public string ServerName { get; set; }
/// <summary>
/// server game
/// </summary>
public string ServerGame { get; set; }
[JsonIgnore]
public long ServerId { get; set; }
}
}

View File

@ -0,0 +1,75 @@
using IW4MAdmin.Plugins.Stats.Models;
using Microsoft.EntityFrameworkCore;
using SharedLibraryCore.Helpers;
using SharedLibraryCore.Interfaces;
using Stats.Dtos;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace Stats.Helpers
{
/// <summary>
/// implementation for IResourceQueryHelper
/// used to obtain client statistics information
/// </summary>
public class StatsResourceQueryHelper : IResourceQueryHelper<StatsInfoRequest, StatsInfoResult>
{
private readonly IDatabaseContextFactory _contextFactory;
public StatsResourceQueryHelper(IDatabaseContextFactory databaseContextFactory)
{
_contextFactory = databaseContextFactory;
}
/// <inheritdoc/>
public async Task<ResourceQueryHelperResult<StatsInfoResult>> QueryResource(StatsInfoRequest query)
{
var result = new ResourceQueryHelperResult<StatsInfoResult>();
using var context = _contextFactory.CreateContext(enableTracking: false);
// we need to get the ratings separately because there's not explicit FK
var ratings = await context.Set<EFClientRatingHistory>()
.Where(_ratingHistory => _ratingHistory.ClientId == query.ClientId)
.SelectMany(_ratingHistory => _ratingHistory.Ratings.Where(_rating => _rating.ServerId != null && _rating.Newest)
.Select(_rating => new
{
_rating.ServerId,
_rating.Ranking,
_rating.When
}))
.ToListAsync();
var iqStats = context.Set<EFClientStatistics>()
.Where(_stats => _stats.ClientId == query.ClientId)
.Select(_stats => new StatsInfoResult
{
ServerId = _stats.ServerId,
Kills = _stats.Kills,
Deaths = _stats.Deaths,
Performance = Math.Round((_stats.EloRating + _stats.Skill) / 2.0, 2),
ScorePerMinute = _stats.SPM,
LastPlayed = _stats.Client.LastConnection,
TotalSecondsPlayed = _stats.TimePlayed,
ServerGame = _stats.Server.GameName.ToString(),
ServerName = _stats.Server.HostName,
});
var queryResults = await iqStats.ToListAsync();
// add the rating query's results to the full query
foreach(var eachResult in queryResults)
{
var rating = ratings.FirstOrDefault(_rating => _rating.ServerId == eachResult.ServerId);
eachResult.Ranking = rating?.Ranking ?? 0;
eachResult.LastPlayed = rating?.When ?? eachResult.LastPlayed;
}
result.Results = queryResults;
result.RetrievedResultCount = queryResults.Count;
result.TotalResultCount = result.RetrievedResultCount;
return result;
}
}
}

View File

@ -12,11 +12,11 @@
<Description>Client Statistics Plugin for IW4MAdmin</Description>
<Copyright>2018</Copyright>
<Configurations>Debug;Release;Prerelease</Configurations>
<LangVersion>7.1</LangVersion>
<LangVersion>8.0</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2.2.12" PrivateAssets="All" />
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2.4.2" PrivateAssets="All" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">

View File

@ -0,0 +1,69 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SharedLibraryCore;
using SharedLibraryCore.Dtos;
using SharedLibraryCore.Interfaces;
using Stats.Dtos;
using System;
using System.Threading.Tasks;
namespace StatsWeb.API
{
[ApiController]
[Route("api/stats")]
public class StatsController : ControllerBase
{
private readonly ILogger _logger;
private readonly IResourceQueryHelper<StatsInfoRequest, StatsInfoResult> _statsQueryHelper;
public StatsController(ILogger logger, IResourceQueryHelper<StatsInfoRequest, StatsInfoResult> statsQueryHelper)
{
_statsQueryHelper = statsQueryHelper;
_logger = logger;
}
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[HttpGet("{clientId}")]
public async Task<IActionResult> ClientStats(int clientId)
{
if (clientId < 1 || !ModelState.IsValid)
{
return BadRequest(new ErrorResponse
{
Messages = new[] { $"Client Id must be between 1 and {int.MaxValue}" }
});
}
var request = new StatsInfoRequest()
{
ClientId = clientId
};
try
{
var result = await _statsQueryHelper.QueryResource(request);
if (result.RetrievedResultCount == 0)
{
return NotFound();
}
return Ok(result.Results);
}
catch (Exception e)
{
_logger.WriteWarning($"Could not get client stats for client id {clientId}");
_logger.WriteDebug(e.GetExceptionInfo());
return StatusCode(StatusCodes.Status500InternalServerError, new ErrorResponse
{
Messages = new[] { e.Message }
});
}
}
}
}

View File

@ -14,7 +14,7 @@
<RunPostBuildEvent>Always</RunPostBuildEvent>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2.4.0" PrivateAssets="All" />
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2.4.2" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>

View File

@ -16,7 +16,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2.2.11" PrivateAssets="All" />
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2.4.2" PrivateAssets="All" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">