clean up some logic related to tracking stats on player join

This commit is contained in:
RaidMax 2020-11-18 16:28:14 -06:00
parent 103d2726c2
commit 09f37d7941
2 changed files with 45 additions and 14 deletions

View File

@ -30,6 +30,7 @@ namespace IW4MAdmin.Plugins.Stats.Helpers
private static List<EFServer> serverModels;
public static string CLIENT_STATS_KEY = "ClientStats";
public static string CLIENT_DETECTIONS_KEY = "ClientDetections";
private readonly SemaphoreSlim _addPlayerWaiter = new SemaphoreSlim(1, 1);
public StatManager(ILogger<StatManager> logger, IManager mgr, IDatabaseContextFactory contextFactory, IConfigurationHandler<StatsConfiguration> configHandler)
{
@ -39,6 +40,11 @@ namespace IW4MAdmin.Plugins.Stats.Helpers
_configHandler = configHandler;
}
~StatManager()
{
_addPlayerWaiter.Dispose();
}
private void SetupServerIds()
{
using (var ctx = _contextFactory.CreateContext(enableTracking: false))
@ -283,9 +289,10 @@ namespace IW4MAdmin.Plugins.Stats.Helpers
{
return existingStats;
}
try
{
await _addPlayerWaiter.WaitAsync();
long serverId = GetIdForServer(pl.CurrentServer);
if (!_servers.ContainsKey(serverId))
@ -318,12 +325,13 @@ namespace IW4MAdmin.Plugins.Stats.Helpers
Skill = 0.0,
SPM = 0.0,
EloRating = 200.0,
HitLocations = Enum.GetValues(typeof(IW4Info.HitLocation)).OfType<IW4Info.HitLocation>().Select(hl => new EFHitLocationCount()
{
Active = true,
HitCount = 0,
Location = hl
}).ToList()
HitLocations = Enum.GetValues(typeof(IW4Info.HitLocation)).OfType<IW4Info.HitLocation>()
.Select(hl => new EFHitLocationCount()
{
Active = true,
HitCount = 0,
Location = hl
}).ToList()
};
// insert if they've not been added
@ -336,7 +344,8 @@ namespace IW4MAdmin.Plugins.Stats.Helpers
// migration for previous existing stats
if (clientStats.HitLocations.Count == 0)
{
clientStats.HitLocations = Enum.GetValues(typeof(IW4Info.HitLocation)).OfType<IW4Info.HitLocation>()
clientStats.HitLocations = Enum.GetValues(typeof(IW4Info.HitLocation))
.OfType<IW4Info.HitLocation>()
.Select(hl => new EFHitLocationCount()
{
Active = true,
@ -378,6 +387,14 @@ namespace IW4MAdmin.Plugins.Stats.Helpers
_log.LogError(ex, "Could not add client to stats {@client}", pl.ToString());
}
finally
{
if (_addPlayerWaiter.CurrentCount == 0)
{
_addPlayerWaiter.Release(1);
}
}
return null;
}

View File

@ -92,8 +92,8 @@ namespace IW4MAdmin.Plugins.Stats
{
E.Origin = E.Target;
}
await Manager.AddPlayer(E.Origin);
await Manager.AddPlayer(E.Target);
await EnsureClientsAdded(E.Origin, E.Target);
await Manager.AddScriptHit(false, E.Time, E.Origin, E.Target, StatManager.GetIdForServer(S), S.CurrentMap.Name, killInfo[7], killInfo[8],
killInfo[5], killInfo[6], killInfo[3], killInfo[4], killInfo[9], killInfo[10], killInfo[11], killInfo[12], killInfo[13], killInfo[14], killInfo[15], killInfo[16], killInfo[17]);
}
@ -112,8 +112,7 @@ namespace IW4MAdmin.Plugins.Stats
E.Origin = E.Target;
}
await Manager.AddPlayer(E.Origin);
await Manager.AddPlayer(E.Target);
await EnsureClientsAdded(E.Origin, E.Target);
await Manager.AddStandardKill(E.Origin, E.Target);
}
break;
@ -139,8 +138,7 @@ namespace IW4MAdmin.Plugins.Stats
E.Origin = E.Target;
}
await Manager.AddPlayer(E.Origin);
await Manager.AddPlayer(E.Target);
await EnsureClientsAdded(E.Origin, E.Target);
await Manager.AddScriptHit(true, E.Time, E.Origin, E.Target, StatManager.GetIdForServer(S), S.CurrentMap.Name, killInfo[7], killInfo[8],
killInfo[5], killInfo[6], killInfo[3], killInfo[4], killInfo[9], killInfo[10], killInfo[11], killInfo[12], killInfo[13], killInfo[14], killInfo[15], killInfo[16], killInfo[17]);
}
@ -475,5 +473,21 @@ namespace IW4MAdmin.Plugins.Stats
/// <param name="s"></param>
/// <returns></returns>
private bool ShouldOverrideAnticheatSetting(Server s) => Config.Configuration().AnticheatConfiguration.Enable && s.GameName == Server.Game.IW5;
/// <summary>
/// Makes sure both clients are added
/// </summary>
/// <param name="origin"></param>
/// <param name="target"></param>
/// <returns></returns>
private async Task EnsureClientsAdded(EFClient origin, EFClient target)
{
await Manager.AddPlayer(origin);
if (!origin.Equals(target))
{
await Manager.AddPlayer(target);
}
}
}
}