1
0
mirror of https://github.com/RaidMax/IW4M-Admin.git synced 2025-06-25 14:40:31 -05:00

[issue #139] client lookup and stats api

This commit is contained in:
RaidMax
2020-05-24 21:22:26 -05:00
parent 8bb8a6832e
commit 39e0874bec
32 changed files with 907 additions and 36 deletions

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">