2021-03-22 12:09:25 -04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2020-05-24 22:22:26 -04:00
|
|
|
|
using SharedLibraryCore.Helpers;
|
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using Stats.Dtos;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
using Data.Abstractions;
|
|
|
|
|
using Data.Models.Client.Stats;
|
2020-05-24 22:22:26 -04:00
|
|
|
|
|
|
|
|
|
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>();
|
2020-11-29 17:01:52 -05:00
|
|
|
|
await using var context = _contextFactory.CreateContext(enableTracking: false);
|
2020-05-24 22:22:26 -04:00
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
{
|
2020-05-30 15:14:42 -04:00
|
|
|
|
Name = _stats.Client.CurrentAlias.Name,
|
2020-05-24 22:22:26 -04:00
|
|
|
|
ServerId = _stats.ServerId,
|
|
|
|
|
Kills = _stats.Kills,
|
|
|
|
|
Deaths = _stats.Deaths,
|
2021-03-22 12:09:25 -04:00
|
|
|
|
Performance = Math.Round(_stats.EloRating * 1/3.0 + _stats.Skill * 2/3.0, 2),
|
2020-05-24 22:22:26 -04:00
|
|
|
|
ScorePerMinute = _stats.SPM,
|
2021-08-25 18:47:57 -04:00
|
|
|
|
LastPlayed = _stats.UpdatedAt ?? _stats.Client.LastConnection,
|
2020-05-24 22:22:26 -04:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|