2021-03-22 12:09:25 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Data.Abstractions;
|
|
|
|
|
using Data.Models;
|
|
|
|
|
using Data.Models.Client.Stats;
|
|
|
|
|
using Data.Models.Client.Stats.Reference;
|
|
|
|
|
using Data.Models.Server;
|
|
|
|
|
using IW4MAdmin.Plugins.Stats.Client.Abstractions;
|
|
|
|
|
using IW4MAdmin.Plugins.Stats.Client.Game;
|
2021-06-16 09:53:50 -04:00
|
|
|
|
using IW4MAdmin.Plugins.Stats.Helpers;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using SharedLibraryCore.Database.Models;
|
2023-02-11 22:01:28 -05:00
|
|
|
|
using SharedLibraryCore.Events;
|
|
|
|
|
using SharedLibraryCore.Events.Game;
|
|
|
|
|
using SharedLibraryCore.Events.Management;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
using Stats.Client.Abstractions;
|
|
|
|
|
using Stats.Client.Game;
|
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
namespace IW4MAdmin.Plugins.Stats.Client;
|
|
|
|
|
|
|
|
|
|
public class HitState
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-02-11 22:01:28 -05:00
|
|
|
|
public HitState()
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-02-11 22:01:28 -05:00
|
|
|
|
OnTransaction = new SemaphoreSlim(1, 1);
|
|
|
|
|
}
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
~HitState()
|
|
|
|
|
{
|
|
|
|
|
OnTransaction.Dispose();
|
|
|
|
|
}
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
public List<EFClientHitStatistic> Hits { get; set; }
|
|
|
|
|
public DateTime? LastUsage { get; set; }
|
|
|
|
|
public int? LastWeaponId { get; set; }
|
|
|
|
|
public EFServer Server { get; set; }
|
|
|
|
|
public SemaphoreSlim OnTransaction { get; }
|
|
|
|
|
public int UpdateCount { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class HitCalculator : IClientStatisticCalculator
|
|
|
|
|
{
|
|
|
|
|
private readonly IDatabaseContextFactory _contextFactory;
|
|
|
|
|
private readonly ILogger<HitCalculator> _logger;
|
|
|
|
|
|
|
|
|
|
private readonly ConcurrentDictionary<int, HitState> _clientHitStatistics = new();
|
|
|
|
|
|
|
|
|
|
private readonly SemaphoreSlim _onTransaction = new SemaphoreSlim(1, 1);
|
|
|
|
|
|
|
|
|
|
private readonly ILookupCache<EFServer> _serverCache;
|
|
|
|
|
private readonly ILookupCache<EFHitLocation> _hitLocationCache;
|
|
|
|
|
private readonly ILookupCache<EFWeapon> _weaponCache;
|
|
|
|
|
private readonly ILookupCache<EFWeaponAttachment> _attachmentCache;
|
|
|
|
|
private readonly ILookupCache<EFWeaponAttachmentCombo> _attachmentComboCache;
|
|
|
|
|
private readonly ILookupCache<EFMeansOfDeath> _modCache;
|
|
|
|
|
private readonly IHitInfoBuilder _hitInfoBuilder;
|
|
|
|
|
private readonly IServerDistributionCalculator _serverDistributionCalculator;
|
|
|
|
|
|
|
|
|
|
private readonly TimeSpan _maxActiveTime = TimeSpan.FromMinutes(2);
|
|
|
|
|
private const int MaxUpdatesBeforePersist = 20;
|
|
|
|
|
private const string SessionScores = nameof(SessionScores);
|
|
|
|
|
|
|
|
|
|
public HitCalculator(ILogger<HitCalculator> logger, IDatabaseContextFactory contextFactory,
|
|
|
|
|
ILookupCache<EFHitLocation> hitLocationCache, ILookupCache<EFWeapon> weaponCache,
|
|
|
|
|
ILookupCache<EFWeaponAttachment> attachmentCache,
|
|
|
|
|
ILookupCache<EFWeaponAttachmentCombo> attachmentComboCache,
|
|
|
|
|
ILookupCache<EFServer> serverCache, ILookupCache<EFMeansOfDeath> modCache, IHitInfoBuilder hitInfoBuilder,
|
|
|
|
|
IServerDistributionCalculator serverDistributionCalculator)
|
|
|
|
|
{
|
|
|
|
|
_contextFactory = contextFactory;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_hitLocationCache = hitLocationCache;
|
|
|
|
|
_weaponCache = weaponCache;
|
|
|
|
|
_attachmentCache = attachmentCache;
|
|
|
|
|
_attachmentComboCache = attachmentComboCache;
|
|
|
|
|
_serverCache = serverCache;
|
|
|
|
|
_hitInfoBuilder = hitInfoBuilder;
|
|
|
|
|
_modCache = modCache;
|
|
|
|
|
_serverDistributionCalculator = serverDistributionCalculator;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
public async Task GatherDependencies()
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-02-11 22:01:28 -05:00
|
|
|
|
await _hitLocationCache.InitializeAsync();
|
|
|
|
|
await _weaponCache.InitializeAsync();
|
|
|
|
|
await _attachmentCache.InitializeAsync();
|
|
|
|
|
await _attachmentComboCache.InitializeAsync();
|
|
|
|
|
await _serverCache.InitializeAsync();
|
|
|
|
|
await _modCache.InitializeAsync();
|
|
|
|
|
}
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
public async Task CalculateForEvent(CoreEvent coreEvent)
|
|
|
|
|
{
|
|
|
|
|
if (coreEvent is ClientStateInitializeEvent clientStateInitializeEvent)
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-02-11 22:01:28 -05:00
|
|
|
|
// if no servers have been cached yet we need to pull them here
|
|
|
|
|
// as they could have gotten added after we've initialized
|
|
|
|
|
if (!_serverCache.GetAll().Any())
|
|
|
|
|
{
|
|
|
|
|
await _serverCache.InitializeAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clientStateInitializeEvent.Client.SetAdditionalProperty(SessionScores, new List<(int, DateTime)>());
|
|
|
|
|
return;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
if (coreEvent is ClientStateDisposeEvent clientStateDisposeEvent)
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-02-11 22:01:28 -05:00
|
|
|
|
_clientHitStatistics.Remove(clientStateDisposeEvent.Client.ClientId, out var state);
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
if (state == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("No client hit state available for disconnecting client {Client}",
|
|
|
|
|
clientStateDisposeEvent.Client.ToString());
|
2021-03-22 12:09:25 -04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
try
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-02-11 22:01:28 -05:00
|
|
|
|
await state.OnTransaction.WaitAsync();
|
|
|
|
|
HandleDisconnectCalculations(clientStateDisposeEvent.Client, state);
|
|
|
|
|
await UpdateClientStatistics(clientStateDisposeEvent.Client.ClientId, state);
|
|
|
|
|
}
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Could not handle disconnect calculations for client {Client}",
|
|
|
|
|
clientStateDisposeEvent.Client.ToString());
|
2021-03-22 12:09:25 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
finally
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-02-11 22:01:28 -05:00
|
|
|
|
if (state.OnTransaction.CurrentCount == 0)
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-02-11 22:01:28 -05:00
|
|
|
|
state.OnTransaction.Release();
|
2021-03-22 12:09:25 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (coreEvent is MatchEndEvent matchEndEvent)
|
|
|
|
|
{
|
|
|
|
|
foreach (var client in matchEndEvent.Server.ConnectedClients)
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-02-11 22:01:28 -05:00
|
|
|
|
var scores = client.GetAdditionalProperty<List<(int, DateTime)>>(SessionScores);
|
|
|
|
|
scores?.Add((client.GetAdditionalProperty<int?>(StatManager.ESTIMATED_SCORE) ?? client.Score,
|
|
|
|
|
DateTime.Now));
|
2021-03-22 12:09:25 -04:00
|
|
|
|
}
|
2023-02-11 22:01:28 -05:00
|
|
|
|
}
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
var damageEvent = coreEvent as ClientKillEvent ?? coreEvent as ClientDamageEvent;
|
|
|
|
|
|
|
|
|
|
if (damageEvent is null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
var eventRegex = damageEvent is ClientKillEvent
|
|
|
|
|
? damageEvent.Owner.EventParser.Configuration.Kill
|
|
|
|
|
: damageEvent.Owner.EventParser.Configuration.Damage;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
var match = eventRegex.PatternMatcher.Match(damageEvent.Data);
|
|
|
|
|
|
|
|
|
|
if (!match.Success)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Log for event type {Type} does not match pattern {LogLine}", damageEvent.Type,
|
|
|
|
|
damageEvent.Data);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
var attackerHitInfo = _hitInfoBuilder.Build(match.Values.ToArray(), eventRegex, damageEvent.Attacker.ClientId,
|
|
|
|
|
damageEvent.Attacker.ClientId == damageEvent.Victim.ClientId, false, damageEvent.Server.GameCode);
|
|
|
|
|
var victimHitInfo = _hitInfoBuilder.Build(match.Values.ToArray(), eventRegex, damageEvent.Victim.ClientId,
|
|
|
|
|
damageEvent.Attacker.ClientId == damageEvent.Victim.ClientId, true, damageEvent.Server.GameCode);
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
foreach (var hitInfo in new[] {attackerHitInfo, victimHitInfo})
|
|
|
|
|
{
|
2023-05-09 22:14:13 -04:00
|
|
|
|
if (hitInfo.MeansOfDeath == null || hitInfo.Location == null || hitInfo.Weapon == null || hitInfo.EntityId == 0)
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-02-11 22:01:28 -05:00
|
|
|
|
_logger.LogDebug("Skipping hit because it does not contain the required data");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2021-06-03 11:51:03 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await _onTransaction.WaitAsync();
|
|
|
|
|
if (!_clientHitStatistics.ContainsKey(hitInfo.EntityId))
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-02-11 22:01:28 -05:00
|
|
|
|
_logger.LogDebug("Starting to track hits for {Client}", hitInfo.EntityId);
|
|
|
|
|
var clientHits = await GetHitsForClient(hitInfo.EntityId);
|
|
|
|
|
_clientHitStatistics.TryAdd(hitInfo.EntityId, new HitState
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-02-11 22:01:28 -05:00
|
|
|
|
Hits = clientHits,
|
|
|
|
|
Server = await _serverCache
|
|
|
|
|
.FirstAsync(server =>
|
|
|
|
|
server.EndPoint == damageEvent.Server.Id && server.HostName != null)
|
|
|
|
|
});
|
2021-03-22 12:09:25 -04:00
|
|
|
|
}
|
2023-02-11 22:01:28 -05:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Could not retrieve previous hit data for client {Client}", hitInfo.EntityId);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
if (_onTransaction.CurrentCount == 0)
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-02-11 22:01:28 -05:00
|
|
|
|
_onTransaction.Release();
|
2021-03-22 12:09:25 -04:00
|
|
|
|
}
|
2023-02-11 22:01:28 -05:00
|
|
|
|
}
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
var state = _clientHitStatistics[hitInfo.EntityId];
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await _onTransaction.WaitAsync();
|
|
|
|
|
var calculatedHits = await RunTasksForHitInfo(hitInfo, state.Server.ServerId);
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
foreach (var clientHit in calculatedHits)
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-02-11 22:01:28 -05:00
|
|
|
|
RunCalculation(clientHit, hitInfo, state);
|
2021-03-22 12:09:25 -04:00
|
|
|
|
}
|
2023-02-11 22:01:28 -05:00
|
|
|
|
}
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Could not update hit calculations for {Client}", hitInfo.EntityId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
if (_onTransaction.CurrentCount == 0)
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-02-11 22:01:28 -05:00
|
|
|
|
_onTransaction.Release();
|
2021-03-22 12:09:25 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-11 22:01:28 -05:00
|
|
|
|
}
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
private async Task<IEnumerable<EFClientHitStatistic>> RunTasksForHitInfo(HitInfo hitInfo, long? serverId)
|
|
|
|
|
{
|
|
|
|
|
var weapon = await GetOrAddWeapon(hitInfo.Weapon, hitInfo.Game);
|
|
|
|
|
var attachments =
|
|
|
|
|
await Task.WhenAll(hitInfo.Weapon.Attachments.Select(attachment =>
|
|
|
|
|
GetOrAddAttachment(attachment, hitInfo.Game)));
|
|
|
|
|
var attachmentCombo = await GetOrAddAttachmentCombo(attachments, hitInfo.Game);
|
|
|
|
|
var matchingLocation = await GetOrAddHitLocation(hitInfo.Location, hitInfo.Game);
|
|
|
|
|
var meansOfDeath = await GetOrAddMeansOfDeath(hitInfo.MeansOfDeath, hitInfo.Game);
|
|
|
|
|
|
|
|
|
|
var baseTasks = new[]
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-02-11 22:01:28 -05:00
|
|
|
|
// just the client
|
|
|
|
|
GetOrAddClientHit(hitInfo.EntityId, null),
|
|
|
|
|
// client and server
|
|
|
|
|
GetOrAddClientHit(hitInfo.EntityId, serverId),
|
|
|
|
|
// just the location
|
|
|
|
|
GetOrAddClientHit(hitInfo.EntityId, null, matchingLocation.HitLocationId),
|
|
|
|
|
// location and server
|
|
|
|
|
GetOrAddClientHit(hitInfo.EntityId, serverId, matchingLocation.HitLocationId),
|
|
|
|
|
// per weapon
|
|
|
|
|
GetOrAddClientHit(hitInfo.EntityId, null, null, weapon.WeaponId),
|
|
|
|
|
// per weapon and server
|
|
|
|
|
GetOrAddClientHit(hitInfo.EntityId, serverId, null, weapon.WeaponId),
|
|
|
|
|
// means of death aggregate
|
|
|
|
|
GetOrAddClientHit(hitInfo.EntityId, meansOfDeathId: meansOfDeath.MeansOfDeathId),
|
|
|
|
|
// means of death per server aggregate
|
|
|
|
|
GetOrAddClientHit(hitInfo.EntityId, serverId,
|
|
|
|
|
meansOfDeathId: meansOfDeath.MeansOfDeathId)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var allTasks = baseTasks.AsEnumerable();
|
|
|
|
|
|
|
|
|
|
if (attachmentCombo != null)
|
|
|
|
|
{
|
|
|
|
|
allTasks = allTasks
|
|
|
|
|
// per weapon per attachment combo
|
|
|
|
|
.Append(GetOrAddClientHit(hitInfo.EntityId, null, null,
|
|
|
|
|
weapon.WeaponId, attachmentCombo.WeaponAttachmentComboId))
|
|
|
|
|
.Append(GetOrAddClientHit(hitInfo.EntityId, serverId, null,
|
|
|
|
|
weapon.WeaponId, attachmentCombo.WeaponAttachmentComboId));
|
2021-03-22 12:09:25 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
return await Task.WhenAll(allTasks);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void RunCalculation(EFClientHitStatistic clientHit, HitInfo hitInfo, HitState hitState)
|
|
|
|
|
{
|
|
|
|
|
if (hitInfo.HitType == HitType.Kill || hitInfo.HitType == HitType.Damage)
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-02-11 22:01:28 -05:00
|
|
|
|
if (clientHit.WeaponId != null) // we only want to calculate usage time for weapons
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-02-11 22:01:28 -05:00
|
|
|
|
var timeElapsed = DateTime.Now - hitState.LastUsage;
|
|
|
|
|
var isSameWeapon = clientHit.WeaponId == hitState.LastWeaponId;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
clientHit.UsageSeconds ??= 60;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
if (timeElapsed.HasValue && timeElapsed <= _maxActiveTime)
|
|
|
|
|
{
|
|
|
|
|
clientHit.UsageSeconds
|
|
|
|
|
+= // if it's the same weapon we can count the entire elapsed time
|
|
|
|
|
// otherwise we split it to make a best guess
|
|
|
|
|
(int) Math.Round(timeElapsed.Value.TotalSeconds / (isSameWeapon ? 1.0 : 2.0));
|
2021-03-22 12:09:25 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
hitState.LastUsage = DateTime.Now;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
clientHit.DamageInflicted += hitInfo.Damage;
|
|
|
|
|
clientHit.HitCount++;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
if (hitInfo.HitType == HitType.Kill)
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-02-11 22:01:28 -05:00
|
|
|
|
clientHit.KillCount++;
|
|
|
|
|
}
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
if (hitInfo.HitType == HitType.WasKilled || hitInfo.HitType == HitType.WasDamaged ||
|
|
|
|
|
hitInfo.HitType == HitType.Suicide)
|
|
|
|
|
{
|
|
|
|
|
clientHit.ReceivedHitCount++;
|
|
|
|
|
clientHit.DamageReceived += hitInfo.Damage;
|
|
|
|
|
}
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
if (hitInfo.HitType == HitType.WasKilled)
|
|
|
|
|
{
|
|
|
|
|
clientHit.DeathCount++;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
}
|
2023-02-11 22:01:28 -05:00
|
|
|
|
}
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
private async Task<List<EFClientHitStatistic>> GetHitsForClient(int clientId)
|
|
|
|
|
{
|
|
|
|
|
try
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-02-11 22:01:28 -05:00
|
|
|
|
await using var context = _contextFactory.CreateContext();
|
|
|
|
|
var hitLocations = await context.Set<EFClientHitStatistic>()
|
|
|
|
|
.Where(stat => stat.ClientId == clientId)
|
|
|
|
|
.ToListAsync();
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
return !hitLocations.Any() ? new List<EFClientHitStatistic>() : hitLocations;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Could not retrieve {hitName} for client with id {id}",
|
|
|
|
|
nameof(EFClientHitStatistic), clientId);
|
|
|
|
|
}
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
return new List<EFClientHitStatistic>();
|
|
|
|
|
}
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
private async Task UpdateClientStatistics(int clientId, HitState locState = null)
|
|
|
|
|
{
|
|
|
|
|
if (!_clientHitStatistics.ContainsKey(clientId) && locState == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError("No {statsName} found for id {id}", nameof(EFClientHitStatistic), clientId);
|
|
|
|
|
return;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
var state = locState ?? _clientHitStatistics[clientId];
|
|
|
|
|
|
|
|
|
|
try
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-02-11 22:01:28 -05:00
|
|
|
|
await using var context = _contextFactory.CreateContext();
|
|
|
|
|
context.Set<EFClientHitStatistic>().UpdateRange(state.Hits);
|
|
|
|
|
await context.SaveChangesAsync();
|
|
|
|
|
}
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Could not update hit location stats for id {id}", clientId);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
private async Task<EFClientHitStatistic> GetOrAddClientHit(int clientId, long? serverId = null,
|
|
|
|
|
int? hitLocationId = null, int? weaponId = null, int? attachmentComboId = null,
|
|
|
|
|
int? meansOfDeathId = null)
|
|
|
|
|
{
|
|
|
|
|
var state = _clientHitStatistics[clientId];
|
|
|
|
|
await state.OnTransaction.WaitAsync();
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
var hitStat = state.Hits
|
|
|
|
|
.FirstOrDefault(hit => hit.HitLocationId == hitLocationId
|
|
|
|
|
&& hit.WeaponId == weaponId
|
|
|
|
|
&& hit.WeaponAttachmentComboId == attachmentComboId
|
|
|
|
|
&& hit.MeansOfDeathId == meansOfDeathId
|
|
|
|
|
&& hit.ServerId == serverId);
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
if (hitStat != null)
|
|
|
|
|
{
|
|
|
|
|
state.OnTransaction.Release();
|
|
|
|
|
return hitStat;
|
|
|
|
|
}
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
hitStat = new EFClientHitStatistic()
|
|
|
|
|
{
|
|
|
|
|
ClientId = clientId,
|
|
|
|
|
ServerId = serverId,
|
|
|
|
|
WeaponId = weaponId,
|
|
|
|
|
WeaponAttachmentComboId = attachmentComboId,
|
|
|
|
|
HitLocationId = hitLocationId,
|
|
|
|
|
MeansOfDeathId = meansOfDeathId
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
/*if (state.UpdateCount > MaxUpdatesBeforePersist)
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-02-11 22:01:28 -05:00
|
|
|
|
await UpdateClientStatistics(clientId);
|
|
|
|
|
state.UpdateCount = 0;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
state.UpdateCount++;*/
|
|
|
|
|
state.Hits.Add(hitStat);
|
2021-03-22 12:09:25 -04:00
|
|
|
|
}
|
2023-02-11 22:01:28 -05:00
|
|
|
|
catch (Exception ex)
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-02-11 22:01:28 -05:00
|
|
|
|
_logger.LogError(ex, "Could not add {statsName} for {id}", nameof(EFClientHitStatistic),
|
|
|
|
|
clientId);
|
|
|
|
|
state.Hits.Remove(hitStat);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
if (state.OnTransaction.CurrentCount == 0)
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-02-11 22:01:28 -05:00
|
|
|
|
state.OnTransaction.Release();
|
2021-03-22 12:09:25 -04:00
|
|
|
|
}
|
2023-02-11 22:01:28 -05:00
|
|
|
|
}
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
return hitStat;
|
|
|
|
|
}
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
private async Task<EFHitLocation> GetOrAddHitLocation(string location, Reference.Game game)
|
|
|
|
|
{
|
|
|
|
|
var matchingLocation = (await _hitLocationCache
|
|
|
|
|
.FirstAsync(loc => loc.Name == location && loc.Game == game));
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
if (matchingLocation != null)
|
|
|
|
|
{
|
|
|
|
|
return matchingLocation;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
var hitLocation = new EFHitLocation()
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-02-11 22:01:28 -05:00
|
|
|
|
Name = location,
|
|
|
|
|
Game = game
|
|
|
|
|
};
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
hitLocation = await _hitLocationCache.AddAsync(hitLocation);
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
return hitLocation;
|
|
|
|
|
}
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
private async Task<EFWeapon> GetOrAddWeapon(WeaponInfo weapon, Reference.Game game)
|
|
|
|
|
{
|
|
|
|
|
var matchingWeapon = (await _weaponCache
|
|
|
|
|
.FirstAsync(wep => wep.Name == weapon.Name && wep.Game == game));
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
if (matchingWeapon != null)
|
|
|
|
|
{
|
2021-03-22 12:09:25 -04:00
|
|
|
|
return matchingWeapon;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
matchingWeapon = new EFWeapon()
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-02-11 22:01:28 -05:00
|
|
|
|
Name = weapon.Name,
|
|
|
|
|
Game = game
|
|
|
|
|
};
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
matchingWeapon = await _weaponCache.AddAsync(matchingWeapon);
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
return matchingWeapon;
|
|
|
|
|
}
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
private async Task<EFWeaponAttachment> GetOrAddAttachment(AttachmentInfo attachment, Reference.Game game)
|
|
|
|
|
{
|
|
|
|
|
var matchingAttachment = (await _attachmentCache
|
|
|
|
|
.FirstAsync(attach => attach.Name == attachment.Name && attach.Game == game));
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
if (matchingAttachment != null)
|
|
|
|
|
{
|
2021-03-22 12:09:25 -04:00
|
|
|
|
return matchingAttachment;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
matchingAttachment = new EFWeaponAttachment()
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-02-11 22:01:28 -05:00
|
|
|
|
Name = attachment.Name,
|
|
|
|
|
Game = game
|
|
|
|
|
};
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
matchingAttachment = await _attachmentCache.AddAsync(matchingAttachment);
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
return matchingAttachment;
|
|
|
|
|
}
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
private async Task<EFWeaponAttachmentCombo> GetOrAddAttachmentCombo(EFWeaponAttachment[] attachments,
|
|
|
|
|
Reference.Game game)
|
|
|
|
|
{
|
|
|
|
|
if (!attachments.Any())
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
var allAttachments = attachments.ToList();
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
if (allAttachments.Count() < 3)
|
|
|
|
|
{
|
|
|
|
|
for (var i = allAttachments.Count(); i <= 3; i++)
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-02-11 22:01:28 -05:00
|
|
|
|
allAttachments.Add(null);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
var matchingAttachmentCombo = (await _attachmentComboCache.FirstAsync(combo =>
|
|
|
|
|
combo.Game == game
|
|
|
|
|
&& combo.Attachment1Id == allAttachments[0].Id
|
|
|
|
|
&& combo.Attachment2Id == allAttachments[1]?.Id
|
|
|
|
|
&& combo.Attachment3Id == allAttachments[2]?.Id));
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
if (matchingAttachmentCombo != null)
|
|
|
|
|
{
|
2021-03-22 12:09:25 -04:00
|
|
|
|
return matchingAttachmentCombo;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
matchingAttachmentCombo = new EFWeaponAttachmentCombo()
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-02-11 22:01:28 -05:00
|
|
|
|
Game = game,
|
|
|
|
|
Attachment1Id = (int) allAttachments[0].Id,
|
|
|
|
|
Attachment2Id = (int?) allAttachments[1]?.Id,
|
|
|
|
|
Attachment3Id = (int?) allAttachments[2]?.Id,
|
|
|
|
|
};
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
matchingAttachmentCombo = await _attachmentComboCache.AddAsync(matchingAttachmentCombo);
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
return matchingAttachmentCombo;
|
|
|
|
|
}
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
private async Task<EFMeansOfDeath> GetOrAddMeansOfDeath(string meansOfDeath, Reference.Game game)
|
|
|
|
|
{
|
|
|
|
|
var matchingMod = (await _modCache
|
|
|
|
|
.FirstAsync(mod => mod.Name == meansOfDeath && mod.Game == game));
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
if (matchingMod != null)
|
|
|
|
|
{
|
|
|
|
|
return matchingMod;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
var mod = new EFMeansOfDeath()
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-02-11 22:01:28 -05:00
|
|
|
|
Name = meansOfDeath,
|
|
|
|
|
Game = game
|
|
|
|
|
};
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
mod = await _modCache.AddAsync(mod);
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
return mod;
|
|
|
|
|
}
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
private void HandleDisconnectCalculations(EFClient client, HitState state)
|
|
|
|
|
{
|
|
|
|
|
// todo: this not added to states fast connect/disconnect
|
|
|
|
|
var serverStats = state.Hits.FirstOrDefault(stat =>
|
|
|
|
|
stat.ServerId == state.Server.ServerId && stat.WeaponId == null &&
|
|
|
|
|
stat.WeaponAttachmentComboId == null && stat.HitLocationId == null && stat.MeansOfDeathId == null);
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
if (serverStats == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("No server hits were found for {serverId} on disconnect for {client}",
|
|
|
|
|
state.Server.ServerId, client.ToString());
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
var aggregate = state.Hits.FirstOrDefault(stat => stat.WeaponId == null &&
|
|
|
|
|
stat.WeaponAttachmentComboId == null &&
|
|
|
|
|
stat.HitLocationId == null &&
|
|
|
|
|
stat.MeansOfDeathId == null &&
|
|
|
|
|
stat.ServerId == null);
|
|
|
|
|
|
|
|
|
|
if (aggregate == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("No aggregate found for {serverId} on disconnect for {client}",
|
|
|
|
|
state.Server.ServerId, client.ToString());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var sessionScores = client.GetAdditionalProperty<List<(int, DateTime)>>(SessionScores);
|
|
|
|
|
|
|
|
|
|
if (sessionScores == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("No session scores available for {Client}", client.ToString());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var stat in new[] {serverStats, aggregate})
|
|
|
|
|
{
|
|
|
|
|
stat.Score ??= 0;
|
|
|
|
|
|
|
|
|
|
if (sessionScores.Count == 0)
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-02-11 22:01:28 -05:00
|
|
|
|
stat.Score += client.Score > 0 ? client.Score : client.GetAdditionalProperty<int?>(Helpers.StatManager.ESTIMATED_SCORE) ?? 0 * 50;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-11 22:01:28 -05:00
|
|
|
|
else
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2023-02-11 22:01:28 -05:00
|
|
|
|
stat.Score += sessionScores.Sum(item => item.Item1) +
|
|
|
|
|
(sessionScores.Last().Item1 == client.Score &&
|
|
|
|
|
(DateTime.Now - sessionScores.Last().Item2).TotalMinutes < 1
|
|
|
|
|
? 0
|
|
|
|
|
: client.Score);
|
2021-03-22 12:09:25 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-26 11:32:16 -05:00
|
|
|
|
}
|