2018-11-05 22:01:29 -05:00
|
|
|
|
using IW4MAdmin.Plugins.Stats.Cheat;
|
2020-04-25 20:01:26 -04:00
|
|
|
|
using IW4MAdmin.Plugins.Stats.Config;
|
2018-05-28 21:30:31 -04:00
|
|
|
|
using IW4MAdmin.Plugins.Stats.Web.Dtos;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2018-11-05 22:01:29 -05:00
|
|
|
|
using SharedLibraryCore;
|
|
|
|
|
using SharedLibraryCore.Helpers;
|
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2018-09-07 23:29:42 -04:00
|
|
|
|
using System.Linq.Expressions;
|
2018-09-08 18:29:30 -04:00
|
|
|
|
using System.Threading;
|
2018-11-05 22:01:29 -05:00
|
|
|
|
using System.Threading.Tasks;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
using Data.Abstractions;
|
|
|
|
|
using Data.Context;
|
|
|
|
|
using Data.Models;
|
|
|
|
|
using Data.Models.Client;
|
|
|
|
|
using Data.Models.Client.Stats;
|
|
|
|
|
using Data.Models.Server;
|
|
|
|
|
using Humanizer.Localisation;
|
2021-12-26 18:14:06 -05:00
|
|
|
|
using Microsoft.Data.Sqlite;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2022-01-26 11:32:16 -05:00
|
|
|
|
using MySqlConnector;
|
2021-12-26 18:14:06 -05:00
|
|
|
|
using Npgsql;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
using Stats.Client.Abstractions;
|
2021-11-23 18:26:33 -05:00
|
|
|
|
using Stats.Config;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
using Stats.Helpers;
|
2019-10-23 11:40:24 -04:00
|
|
|
|
using static IW4MAdmin.Plugins.Stats.Cheat.Detection;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
using EFClient = SharedLibraryCore.Database.Models.EFClient;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
2018-02-07 00:19:06 -05:00
|
|
|
|
|
2018-04-08 17:50:58 -04:00
|
|
|
|
namespace IW4MAdmin.Plugins.Stats.Helpers
|
2018-02-07 00:19:06 -05:00
|
|
|
|
{
|
|
|
|
|
public class StatManager
|
|
|
|
|
{
|
2019-09-09 18:40:04 -04:00
|
|
|
|
private const int MAX_CACHED_HITS = 100;
|
2019-05-08 21:34:17 -04:00
|
|
|
|
private readonly ConcurrentDictionary<long, ServerStats> _servers;
|
|
|
|
|
private readonly ILogger _log;
|
2020-04-25 20:01:26 -04:00
|
|
|
|
private readonly IDatabaseContextFactory _contextFactory;
|
|
|
|
|
private readonly IConfigurationHandler<StatsConfiguration> _configHandler;
|
2019-08-23 19:34:31 -04:00
|
|
|
|
private static List<EFServer> serverModels;
|
2019-10-18 14:39:21 -04:00
|
|
|
|
public static string CLIENT_STATS_KEY = "ClientStats";
|
|
|
|
|
public static string CLIENT_DETECTIONS_KEY = "ClientDetections";
|
2021-06-16 09:53:50 -04:00
|
|
|
|
public static string ESTIMATED_SCORE = "EstimatedScore";
|
2021-03-22 12:09:25 -04:00
|
|
|
|
private readonly SemaphoreSlim _addPlayerWaiter = new SemaphoreSlim(1, 1);
|
|
|
|
|
private readonly IServerDistributionCalculator _serverDistributionCalculator;
|
2018-09-08 18:29:30 -04:00
|
|
|
|
|
2021-03-22 12:09:25 -04:00
|
|
|
|
public StatManager(ILogger<StatManager> logger, IManager mgr, IDatabaseContextFactory contextFactory,
|
|
|
|
|
IConfigurationHandler<StatsConfiguration> configHandler,
|
|
|
|
|
IServerDistributionCalculator serverDistributionCalculator)
|
2018-02-07 00:19:06 -05:00
|
|
|
|
{
|
2019-05-08 21:34:17 -04:00
|
|
|
|
_servers = new ConcurrentDictionary<long, ServerStats>();
|
2020-11-11 18:31:26 -05:00
|
|
|
|
_log = logger;
|
2020-04-25 20:01:26 -04:00
|
|
|
|
_contextFactory = contextFactory;
|
|
|
|
|
_configHandler = configHandler;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
_serverDistributionCalculator = serverDistributionCalculator;
|
2018-02-07 00:19:06 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-18 17:28:14 -05:00
|
|
|
|
~StatManager()
|
|
|
|
|
{
|
|
|
|
|
_addPlayerWaiter.Dispose();
|
|
|
|
|
}
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2019-08-23 19:34:31 -04:00
|
|
|
|
private void SetupServerIds()
|
|
|
|
|
{
|
2020-11-27 22:52:52 -05:00
|
|
|
|
using var ctx = _contextFactory.CreateContext(enableTracking: false);
|
|
|
|
|
serverModels = ctx.Set<EFServer>().ToList();
|
2019-08-23 19:34:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-25 20:01:26 -04:00
|
|
|
|
public Expression<Func<EFRating, bool>> GetRankingFunc(long? serverId = null)
|
2018-09-07 23:29:42 -04:00
|
|
|
|
{
|
|
|
|
|
var fifteenDaysAgo = DateTime.UtcNow.AddDays(-15);
|
|
|
|
|
return (r) => r.ServerId == serverId &&
|
2021-03-22 12:09:25 -04:00
|
|
|
|
r.When > fifteenDaysAgo &&
|
|
|
|
|
r.RatingHistory.Client.Level != EFClient.Permission.Banned &&
|
|
|
|
|
r.Newest &&
|
|
|
|
|
r.ActivityAmount >= _configHandler.Configuration().TopPlayersMinPlayTime;
|
2018-09-07 23:29:42 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-04 22:07:34 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// gets a ranking across all servers for given client id
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="clientId">client id of the player</param>
|
|
|
|
|
/// <returns></returns>
|
2021-03-22 12:09:25 -04:00
|
|
|
|
public async Task<int> GetClientOverallRanking(int clientId, long? serverId = null)
|
2018-05-28 21:30:31 -04:00
|
|
|
|
{
|
2020-11-29 17:01:52 -05:00
|
|
|
|
await using var context = _contextFactory.CreateContext(enableTracking: false);
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
|
|
|
|
if (_configHandler.Configuration().EnableAdvancedMetrics)
|
|
|
|
|
{
|
|
|
|
|
var clientRanking = await context.Set<EFClientRankingHistory>()
|
|
|
|
|
.Where(r => r.ClientId == clientId)
|
|
|
|
|
.Where(r => r.ServerId == serverId)
|
|
|
|
|
.Where(r => r.Newest)
|
|
|
|
|
.FirstOrDefaultAsync();
|
|
|
|
|
return clientRanking?.Ranking + 1 ?? 0;
|
|
|
|
|
}
|
2018-09-04 22:07:34 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
var clientPerformance = await context.Set<EFRating>()
|
|
|
|
|
.Where(r => r.RatingHistory.ClientId == clientId)
|
2021-03-22 12:09:25 -04:00
|
|
|
|
.Where(r => r.ServerId == serverId)
|
2020-11-29 17:01:52 -05:00
|
|
|
|
.Where(r => r.Newest)
|
|
|
|
|
.Select(r => r.Performance)
|
|
|
|
|
.FirstOrDefaultAsync();
|
2018-09-08 18:29:30 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
if (clientPerformance != 0)
|
|
|
|
|
{
|
|
|
|
|
var iqClientRanking = context.Set<EFRating>()
|
|
|
|
|
.Where(r => r.RatingHistory.ClientId != clientId)
|
|
|
|
|
.Where(r => r.Performance > clientPerformance)
|
|
|
|
|
.Where(GetRankingFunc());
|
2018-09-07 23:29:42 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
return await iqClientRanking.CountAsync() + 1;
|
2018-09-04 22:07:34 -04:00
|
|
|
|
}
|
2020-11-29 17:01:52 -05:00
|
|
|
|
|
|
|
|
|
return 0;
|
2018-09-04 22:07:34 -04:00
|
|
|
|
}
|
2018-05-28 21:30:31 -04:00
|
|
|
|
|
2021-06-29 18:14:25 -04:00
|
|
|
|
public Expression<Func<EFClientRankingHistory, bool>> GetNewRankingFunc(int? clientId = null, long? serverId = null)
|
|
|
|
|
{
|
|
|
|
|
return (ranking) => ranking.ServerId == serverId
|
|
|
|
|
&& ranking.Client.Level != Data.Models.Client.EFClient.Permission.Banned
|
2022-01-24 16:08:31 -05:00
|
|
|
|
&& ranking.CreatedDateTime >= Extensions.FifteenDaysAgo()
|
2021-06-29 18:14:25 -04:00
|
|
|
|
&& ranking.ZScore != null
|
|
|
|
|
&& ranking.PerformanceMetric != null
|
|
|
|
|
&& ranking.Newest
|
|
|
|
|
&& ranking.Client.TotalConnectionTime >=
|
|
|
|
|
_configHandler.Configuration().TopPlayersMinPlayTime;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<int> GetTotalRankedPlayers(long serverId)
|
|
|
|
|
{
|
|
|
|
|
await using var context = _contextFactory.CreateContext(enableTracking: false);
|
|
|
|
|
|
|
|
|
|
return await context.Set<EFClientRankingHistory>()
|
|
|
|
|
.Where(GetNewRankingFunc(serverId: serverId))
|
|
|
|
|
.CountAsync();
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-22 12:09:25 -04:00
|
|
|
|
public async Task<List<TopStatsInfo>> GetNewTopStats(int start, int count, long? serverId = null)
|
|
|
|
|
{
|
|
|
|
|
await using var context = _contextFactory.CreateContext(false);
|
|
|
|
|
|
|
|
|
|
var clientIdsList = await context.Set<EFClientRankingHistory>()
|
2021-06-29 18:14:25 -04:00
|
|
|
|
.Where(GetNewRankingFunc(serverId: serverId))
|
2021-03-22 12:09:25 -04:00
|
|
|
|
.OrderByDescending(ranking => ranking.PerformanceMetric)
|
|
|
|
|
.Select(ranking => ranking.ClientId)
|
|
|
|
|
.Skip(start)
|
|
|
|
|
.Take(count)
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
|
|
|
|
var rankings = await context.Set<EFClientRankingHistory>()
|
|
|
|
|
.Where(ranking => clientIdsList.Contains(ranking.ClientId))
|
|
|
|
|
.Where(ranking => ranking.ServerId == serverId)
|
|
|
|
|
.Select(ranking => new
|
|
|
|
|
{
|
|
|
|
|
ranking.ClientId,
|
|
|
|
|
ranking.Client.CurrentAlias.Name,
|
|
|
|
|
ranking.Client.LastConnection,
|
|
|
|
|
ranking.PerformanceMetric,
|
|
|
|
|
ranking.ZScore,
|
|
|
|
|
ranking.Ranking,
|
|
|
|
|
ranking.CreatedDateTime
|
|
|
|
|
})
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
|
|
|
|
var rankingsDict = rankings.GroupBy(rank => rank.ClientId)
|
|
|
|
|
.ToDictionary(rank => rank.Key, rank => rank.OrderBy(r => r.CreatedDateTime).ToList());
|
|
|
|
|
|
|
|
|
|
var statsInfo = await context.Set<EFClientStatistics>()
|
|
|
|
|
.Where(stat => clientIdsList.Contains(stat.ClientId))
|
|
|
|
|
.Where(stat => stat.TimePlayed > 0)
|
|
|
|
|
.Where(stat => stat.Kills > 0 || stat.Deaths > 0)
|
|
|
|
|
.Where(stat => serverId == null || stat.ServerId == serverId)
|
|
|
|
|
.GroupBy(stat => stat.ClientId)
|
|
|
|
|
.Select(s => new
|
|
|
|
|
{
|
|
|
|
|
ClientId = s.Key,
|
|
|
|
|
Kills = s.Sum(c => c.Kills),
|
|
|
|
|
Deaths = s.Sum(c => c.Deaths),
|
|
|
|
|
KDR = s.Sum(c => (c.Kills / (double) (c.Deaths == 0 ? 1 : c.Deaths)) * c.TimePlayed) /
|
|
|
|
|
s.Sum(c => c.TimePlayed),
|
|
|
|
|
TotalTimePlayed = s.Sum(c => c.TimePlayed),
|
2021-08-25 18:47:57 -04:00
|
|
|
|
UpdatedAt = s.Max(c => c.UpdatedAt)
|
2021-03-22 12:09:25 -04:00
|
|
|
|
})
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
|
|
|
|
var finished = statsInfo
|
|
|
|
|
.OrderByDescending(stat => rankingsDict[stat.ClientId].Last().PerformanceMetric)
|
|
|
|
|
.Select((s, index) => new TopStatsInfo()
|
|
|
|
|
{
|
|
|
|
|
ClientId = s.ClientId,
|
|
|
|
|
Id = (int?) serverId ?? 0,
|
|
|
|
|
Deaths = s.Deaths,
|
|
|
|
|
Kills = s.Kills,
|
|
|
|
|
KDR = Math.Round(s.KDR, 2),
|
2021-08-25 18:47:57 -04:00
|
|
|
|
LastSeen = (DateTime.UtcNow - (s.UpdatedAt ?? rankingsDict[s.ClientId].Last().LastConnection))
|
2021-03-22 12:09:25 -04:00
|
|
|
|
.HumanizeForCurrentCulture(1, TimeUnit.Week, TimeUnit.Second, ",", false),
|
2021-08-25 18:47:57 -04:00
|
|
|
|
LastSeenValue = DateTime.UtcNow - (s.UpdatedAt ?? rankingsDict[s.ClientId].Last().LastConnection),
|
2021-03-22 12:09:25 -04:00
|
|
|
|
Name = rankingsDict[s.ClientId].First().Name,
|
|
|
|
|
Performance = Math.Round(rankingsDict[s.ClientId].Last().PerformanceMetric ?? 0, 2),
|
|
|
|
|
RatingChange = (rankingsDict[s.ClientId].First().Ranking -
|
|
|
|
|
rankingsDict[s.ClientId].Last().Ranking) ?? 0,
|
|
|
|
|
PerformanceHistory = rankingsDict[s.ClientId].Select(ranking => ranking.PerformanceMetric ?? 0).ToList(),
|
|
|
|
|
TimePlayed = Math.Round(s.TotalTimePlayed / 3600.0, 1).ToString("#,##0"),
|
|
|
|
|
TimePlayedValue = TimeSpan.FromSeconds(s.TotalTimePlayed),
|
|
|
|
|
Ranking = index + start + 1,
|
|
|
|
|
ZScore = rankingsDict[s.ClientId].Last().ZScore,
|
|
|
|
|
ServerId = serverId
|
|
|
|
|
})
|
|
|
|
|
.OrderBy(r => r.Ranking)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
return finished;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-26 22:25:27 -05:00
|
|
|
|
public async Task<List<TopStatsInfo>> GetTopStats(int start, int count, long? serverId = null)
|
2018-09-04 22:07:34 -04:00
|
|
|
|
{
|
2021-03-22 12:09:25 -04:00
|
|
|
|
if (_configHandler.Configuration().EnableAdvancedMetrics)
|
|
|
|
|
{
|
|
|
|
|
return await GetNewTopStats(start, count, serverId);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
await using var context = _contextFactory.CreateContext(enableTracking: false);
|
|
|
|
|
// setup the query for the clients within the given rating range
|
|
|
|
|
var iqClientRatings = (from rating in context.Set<EFRating>()
|
2021-03-22 12:09:25 -04:00
|
|
|
|
.Where(GetRankingFunc(serverId))
|
|
|
|
|
select new
|
|
|
|
|
{
|
|
|
|
|
rating.RatingHistory.ClientId,
|
|
|
|
|
rating.RatingHistory.Client.CurrentAlias.Name,
|
|
|
|
|
rating.RatingHistory.Client.LastConnection,
|
|
|
|
|
rating.Performance,
|
|
|
|
|
})
|
2020-11-29 17:01:52 -05:00
|
|
|
|
.OrderByDescending(c => c.Performance)
|
|
|
|
|
.Skip(start)
|
|
|
|
|
.Take(count);
|
|
|
|
|
|
|
|
|
|
// materialized list
|
2021-03-22 12:09:25 -04:00
|
|
|
|
var clientRatings = (await iqClientRatings.ToListAsync())
|
|
|
|
|
.GroupBy(rating => rating.ClientId) // prevent duplicate keys
|
|
|
|
|
.Select(group => group.FirstOrDefault());
|
2020-11-29 17:01:52 -05:00
|
|
|
|
|
|
|
|
|
// get all the unique client ids that are in the top stats
|
|
|
|
|
var clientIds = clientRatings
|
|
|
|
|
.GroupBy(r => r.ClientId)
|
|
|
|
|
.Select(r => r.First().ClientId)
|
2018-09-16 16:34:16 -04:00
|
|
|
|
.ToList();
|
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
var iqRatingInfo = from rating in context.Set<EFRating>()
|
2021-03-22 12:09:25 -04:00
|
|
|
|
where clientIds.Contains(rating.RatingHistory.ClientId)
|
|
|
|
|
where rating.ServerId == serverId
|
|
|
|
|
select new
|
|
|
|
|
{
|
|
|
|
|
rating.Ranking,
|
|
|
|
|
rating.Performance,
|
|
|
|
|
rating.RatingHistory.ClientId,
|
|
|
|
|
rating.When
|
|
|
|
|
};
|
2020-11-29 17:01:52 -05:00
|
|
|
|
|
|
|
|
|
var ratingInfo = (await iqRatingInfo.ToListAsync())
|
|
|
|
|
.GroupBy(r => r.ClientId)
|
|
|
|
|
.Select(grp => new
|
2018-09-16 16:34:16 -04:00
|
|
|
|
{
|
2020-11-29 17:01:52 -05:00
|
|
|
|
grp.Key,
|
2021-03-22 12:09:25 -04:00
|
|
|
|
Ratings = grp.Select(r => new {r.Performance, r.Ranking, r.When})
|
2020-11-29 17:01:52 -05:00
|
|
|
|
});
|
2018-09-11 15:28:37 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
var iqStatsInfo = (from stat in context.Set<EFClientStatistics>()
|
2021-03-22 12:09:25 -04:00
|
|
|
|
where clientIds.Contains(stat.ClientId)
|
|
|
|
|
where stat.Kills > 0 || stat.Deaths > 0
|
|
|
|
|
where serverId == null || stat.ServerId == serverId
|
|
|
|
|
group stat by stat.ClientId
|
|
|
|
|
into s
|
|
|
|
|
select new
|
|
|
|
|
{
|
|
|
|
|
ClientId = s.Key,
|
|
|
|
|
Kills = s.Sum(c => c.Kills),
|
|
|
|
|
Deaths = s.Sum(c => c.Deaths),
|
|
|
|
|
KDR = s.Sum(c => (c.Kills / (double) (c.Deaths == 0 ? 1 : c.Deaths)) * c.TimePlayed) /
|
|
|
|
|
s.Sum(c => c.TimePlayed),
|
|
|
|
|
TotalTimePlayed = s.Sum(c => c.TimePlayed),
|
|
|
|
|
});
|
2020-11-29 17:01:52 -05:00
|
|
|
|
|
|
|
|
|
var topPlayers = await iqStatsInfo.ToListAsync();
|
|
|
|
|
|
|
|
|
|
var clientRatingsDict = clientRatings.ToDictionary(r => r.ClientId);
|
|
|
|
|
var finished = topPlayers.Select(s => new TopStatsInfo()
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
|
|
|
|
ClientId = s.ClientId,
|
|
|
|
|
Id = (int?) serverId ?? 0,
|
|
|
|
|
Deaths = s.Deaths,
|
|
|
|
|
Kills = s.Kills,
|
|
|
|
|
KDR = Math.Round(s.KDR, 2),
|
|
|
|
|
LastSeen = (DateTime.UtcNow - clientRatingsDict[s.ClientId].LastConnection)
|
|
|
|
|
.HumanizeForCurrentCulture(),
|
|
|
|
|
LastSeenValue = DateTime.UtcNow - clientRatingsDict[s.ClientId].LastConnection,
|
|
|
|
|
Name = clientRatingsDict[s.ClientId].Name,
|
|
|
|
|
Performance = Math.Round(clientRatingsDict[s.ClientId].Performance, 2),
|
|
|
|
|
RatingChange = ratingInfo.First(r => r.Key == s.ClientId).Ratings.First().Ranking -
|
|
|
|
|
ratingInfo.First(r => r.Key == s.ClientId).Ratings.Last().Ranking,
|
|
|
|
|
PerformanceHistory = ratingInfo.First(r => r.Key == s.ClientId).Ratings.Count() > 1
|
|
|
|
|
? ratingInfo.First(r => r.Key == s.ClientId).Ratings.OrderBy(r => r.When)
|
|
|
|
|
.Select(r => r.Performance).ToList()
|
|
|
|
|
: new List<double>()
|
|
|
|
|
{clientRatingsDict[s.ClientId].Performance, clientRatingsDict[s.ClientId].Performance},
|
|
|
|
|
TimePlayed = Math.Round(s.TotalTimePlayed / 3600.0, 1).ToString("#,##0"),
|
|
|
|
|
TimePlayedValue = TimeSpan.FromSeconds(s.TotalTimePlayed)
|
|
|
|
|
})
|
|
|
|
|
.OrderByDescending(r => r.Performance)
|
|
|
|
|
.ToList();
|
2020-11-29 17:01:52 -05:00
|
|
|
|
|
|
|
|
|
// set the ranking numerically
|
|
|
|
|
int i = start + 1;
|
|
|
|
|
foreach (var stat in finished)
|
|
|
|
|
{
|
|
|
|
|
stat.Ranking = i;
|
|
|
|
|
i++;
|
2018-09-16 16:34:16 -04:00
|
|
|
|
}
|
2020-11-29 17:01:52 -05:00
|
|
|
|
|
|
|
|
|
return finished;
|
2018-05-28 21:30:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Add a server to the StatManager server pool
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sv"></param>
|
|
|
|
|
public void AddServer(Server sv)
|
2018-02-07 00:19:06 -05:00
|
|
|
|
{
|
2018-10-07 22:34:30 -04:00
|
|
|
|
// insert the server if it does not exist
|
2018-09-16 16:34:16 -04:00
|
|
|
|
try
|
2018-09-11 15:28:37 -04:00
|
|
|
|
{
|
2019-08-23 19:34:31 -04:00
|
|
|
|
if (serverModels == null)
|
|
|
|
|
{
|
|
|
|
|
SetupServerIds();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
long serverId = GetIdForServer(sv);
|
2018-10-07 22:34:30 -04:00
|
|
|
|
EFServer server;
|
2018-09-16 16:34:16 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
using var ctx = _contextFactory.CreateContext(enableTracking: false);
|
|
|
|
|
var serverSet = ctx.Set<EFServer>();
|
|
|
|
|
// get the server from the database if it exists, otherwise create and insert a new one
|
|
|
|
|
server = serverSet.FirstOrDefault(s => s.ServerId == serverId);
|
2018-11-27 19:31:48 -05:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
// the server might be using legacy server id
|
|
|
|
|
if (server == null)
|
|
|
|
|
{
|
|
|
|
|
server = serverSet.FirstOrDefault(s => s.EndPoint == sv.ToString());
|
2018-11-27 19:31:48 -05:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
if (server != null)
|
2018-10-07 22:34:30 -04:00
|
|
|
|
{
|
2020-11-29 17:01:52 -05:00
|
|
|
|
// this provides a way to identify legacy server entries
|
|
|
|
|
server.EndPoint = sv.ToString();
|
|
|
|
|
ctx.Update(server);
|
2018-10-07 22:34:30 -04:00
|
|
|
|
ctx.SaveChanges();
|
|
|
|
|
}
|
2020-11-29 17:01:52 -05:00
|
|
|
|
}
|
2019-04-23 18:27:20 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
// server has never been added before
|
|
|
|
|
if (server == null)
|
|
|
|
|
{
|
|
|
|
|
server = new EFServer()
|
2019-04-23 18:27:20 -04:00
|
|
|
|
{
|
2020-11-29 17:01:52 -05:00
|
|
|
|
Port = sv.Port,
|
|
|
|
|
EndPoint = sv.ToString(),
|
|
|
|
|
ServerId = serverId,
|
2021-03-22 12:09:25 -04:00
|
|
|
|
GameName = (Reference.Game?) sv.GameName,
|
2020-11-29 17:01:52 -05:00
|
|
|
|
HostName = sv.Hostname
|
|
|
|
|
};
|
2020-05-22 21:29:41 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
server = serverSet.Add(server).Entity;
|
|
|
|
|
// this doesn't need to be async as it's during initialization
|
|
|
|
|
ctx.SaveChanges();
|
|
|
|
|
}
|
2020-08-20 11:38:11 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
// we want to set the gamename up if it's never been set, or it changed
|
2021-03-22 12:09:25 -04:00
|
|
|
|
else if (!server.GameName.HasValue || server.GameName.Value != (Reference.Game) sv.GameName)
|
2020-11-29 17:01:52 -05:00
|
|
|
|
{
|
2021-03-22 12:09:25 -04:00
|
|
|
|
server.GameName = (Reference.Game) sv.GameName;
|
2020-11-29 17:01:52 -05:00
|
|
|
|
ctx.Entry(server).Property(_prop => _prop.GameName).IsModified = true;
|
2020-08-20 11:38:11 -04:00
|
|
|
|
ctx.SaveChanges();
|
2018-09-16 16:34:16 -04:00
|
|
|
|
}
|
2018-02-07 00:19:06 -05:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
if (server.HostName == null || server.HostName != sv.Hostname)
|
|
|
|
|
{
|
|
|
|
|
server.HostName = sv.Hostname;
|
|
|
|
|
ctx.Entry(server).Property(_prop => _prop.HostName).IsModified = true;
|
|
|
|
|
ctx.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx.Entry(server).Property(_prop => _prop.IsPasswordProtected).IsModified = true;
|
|
|
|
|
server.IsPasswordProtected = !string.IsNullOrEmpty(sv.GamePassword);
|
|
|
|
|
ctx.SaveChanges();
|
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
// check to see if the stats have ever been initialized
|
2018-11-27 19:31:48 -05:00
|
|
|
|
var serverStats = InitializeServerStats(server.ServerId);
|
2018-09-11 15:28:37 -04:00
|
|
|
|
|
2019-10-18 14:39:21 -04:00
|
|
|
|
_servers.TryAdd(serverId, new ServerStats(server, serverStats, sv)
|
2018-09-16 16:34:16 -04:00
|
|
|
|
{
|
|
|
|
|
IsTeamBased = sv.Gametype != "dm"
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (Exception e)
|
2018-02-07 00:19:06 -05:00
|
|
|
|
{
|
2021-03-22 12:09:25 -04:00
|
|
|
|
_log.LogError(e, "{message}",
|
|
|
|
|
Utilities.CurrentLocalization.LocalizationIndex["PLUGIN_STATS_ERROR_ADD"]);
|
2018-09-16 16:34:16 -04:00
|
|
|
|
}
|
2018-02-07 00:19:06 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Add Player to the player stats
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="pl">Player to add/retrieve stats for</param>
|
|
|
|
|
/// <returns>EFClientStatistic of specified player</returns>
|
2018-11-05 22:01:29 -05:00
|
|
|
|
public async Task<EFClientStatistics> AddPlayer(EFClient pl)
|
2018-02-07 00:19:06 -05:00
|
|
|
|
{
|
2020-11-14 19:24:51 -05:00
|
|
|
|
var existingStats = pl.GetAdditionalProperty<EFClientStatistics>(CLIENT_STATS_KEY);
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2020-11-14 19:24:51 -05:00
|
|
|
|
if (existingStats != null)
|
|
|
|
|
{
|
|
|
|
|
return existingStats;
|
|
|
|
|
}
|
2020-11-18 17:28:14 -05:00
|
|
|
|
|
2018-10-07 22:34:30 -04:00
|
|
|
|
try
|
2018-09-16 16:34:16 -04:00
|
|
|
|
{
|
2020-11-18 17:28:14 -05:00
|
|
|
|
await _addPlayerWaiter.WaitAsync();
|
2019-08-23 19:34:31 -04:00
|
|
|
|
long serverId = GetIdForServer(pl.CurrentServer);
|
2018-02-15 23:01:28 -05:00
|
|
|
|
|
2019-05-08 21:34:17 -04:00
|
|
|
|
if (!_servers.ContainsKey(serverId))
|
2018-09-16 16:34:16 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
_log.LogError("[Stats::AddPlayer] Server with id {serverId} could not be found", serverId);
|
2018-10-07 22:34:30 -04:00
|
|
|
|
return null;
|
|
|
|
|
}
|
2018-05-03 01:25:49 -04:00
|
|
|
|
|
2021-12-26 18:14:06 -05:00
|
|
|
|
if (pl.ClientId <= 0)
|
|
|
|
|
{
|
|
|
|
|
_log.LogWarning("Stats for {Client} are not yet initialized", pl.ToString());
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-07 22:34:30 -04:00
|
|
|
|
// get the client's stats from the database if it exists, otherwise create and attach a new one
|
|
|
|
|
// if this fails we want to throw an exception
|
2018-09-16 16:34:16 -04:00
|
|
|
|
|
2018-10-07 22:34:30 -04:00
|
|
|
|
EFClientStatistics clientStats;
|
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
await using var ctx = _contextFactory.CreateContext(enableTracking: false);
|
|
|
|
|
var clientStatsSet = ctx.Set<EFClientStatistics>();
|
|
|
|
|
clientStats = clientStatsSet
|
|
|
|
|
.Include(cl => cl.HitLocations)
|
|
|
|
|
.FirstOrDefault(c => c.ClientId == pl.ClientId && c.ServerId == serverId);
|
2018-10-07 22:34:30 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
if (clientStats == null)
|
|
|
|
|
{
|
|
|
|
|
clientStats = new EFClientStatistics()
|
2018-10-07 22:34:30 -04:00
|
|
|
|
{
|
2020-11-29 17:01:52 -05:00
|
|
|
|
Active = true,
|
|
|
|
|
ClientId = pl.ClientId,
|
|
|
|
|
Deaths = 0,
|
|
|
|
|
Kills = 0,
|
|
|
|
|
ServerId = serverId,
|
|
|
|
|
Skill = 0.0,
|
|
|
|
|
SPM = 0.0,
|
|
|
|
|
EloRating = 200.0,
|
|
|
|
|
HitLocations = Enum.GetValues(typeof(IW4Info.HitLocation)).OfType<IW4Info.HitLocation>()
|
2018-10-07 22:34:30 -04:00
|
|
|
|
.Select(hl => new EFHitLocationCount()
|
|
|
|
|
{
|
|
|
|
|
Active = true,
|
|
|
|
|
HitCount = 0,
|
2021-03-22 12:09:25 -04:00
|
|
|
|
Location = (int) hl
|
2020-11-29 17:01:52 -05:00
|
|
|
|
}).ToList()
|
|
|
|
|
};
|
2018-10-07 22:34:30 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
// insert if they've not been added
|
|
|
|
|
clientStats = clientStatsSet.Add(clientStats).Entity;
|
|
|
|
|
await ctx.SaveChangesAsync();
|
|
|
|
|
}
|
2018-10-07 22:34:30 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
pl.SetAdditionalProperty(CLIENT_STATS_KEY, clientStats);
|
2018-10-07 22:34:30 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
// migration for previous existing stats
|
|
|
|
|
if (clientStats.HitLocations.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
clientStats.HitLocations = Enum.GetValues(typeof(IW4Info.HitLocation))
|
|
|
|
|
.OfType<IW4Info.HitLocation>()
|
|
|
|
|
.Select(hl => new EFHitLocationCount()
|
|
|
|
|
{
|
|
|
|
|
Active = true,
|
|
|
|
|
HitCount = 0,
|
2021-03-22 12:09:25 -04:00
|
|
|
|
Location = (int) hl
|
2020-11-29 17:01:52 -05:00
|
|
|
|
})
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
ctx.Update(clientStats);
|
|
|
|
|
await ctx.SaveChangesAsync();
|
|
|
|
|
}
|
2018-10-07 22:34:30 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
// for stats before rating
|
|
|
|
|
if (clientStats.EloRating == 0.0)
|
|
|
|
|
{
|
|
|
|
|
clientStats.EloRating = clientStats.Skill;
|
|
|
|
|
}
|
2018-10-07 22:34:30 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
if (clientStats.RollingWeightedKDR == 0)
|
|
|
|
|
{
|
|
|
|
|
clientStats.RollingWeightedKDR = clientStats.KDR;
|
2018-10-07 22:34:30 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
// set these on connecting
|
|
|
|
|
clientStats.LastActive = DateTime.UtcNow;
|
|
|
|
|
clientStats.LastStatCalculation = DateTime.UtcNow;
|
|
|
|
|
clientStats.SessionScore = pl.Score;
|
|
|
|
|
clientStats.LastScore = pl.Score;
|
|
|
|
|
|
|
|
|
|
pl.SetAdditionalProperty(CLIENT_DETECTIONS_KEY, new Detection(_log, clientStats));
|
|
|
|
|
_log.LogDebug("Added {client} to stats", pl.ToString());
|
|
|
|
|
|
2018-10-07 22:34:30 -04:00
|
|
|
|
return clientStats;
|
2018-09-16 16:34:16 -04:00
|
|
|
|
}
|
2018-02-09 02:21:25 -05:00
|
|
|
|
|
2021-12-26 18:14:06 -05:00
|
|
|
|
catch (DbUpdateException updateException) when (
|
|
|
|
|
updateException.InnerException is PostgresException {SqlState: "23503"}
|
|
|
|
|
|| updateException.InnerException is SqliteException {SqliteErrorCode: 787}
|
|
|
|
|
|| updateException.InnerException is MySqlException {SqlState: "23503"})
|
|
|
|
|
{
|
|
|
|
|
_log.LogWarning("Trying to add {Client} to stats before they have been added to the database",
|
|
|
|
|
pl.ToString());
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-07 22:34:30 -04:00
|
|
|
|
catch (Exception ex)
|
2018-09-16 16:34:16 -04:00
|
|
|
|
{
|
2020-11-14 19:24:51 -05:00
|
|
|
|
_log.LogError(ex, "Could not add client to stats {@client}", pl.ToString());
|
2018-10-07 22:34:30 -04:00
|
|
|
|
}
|
2018-02-26 23:24:19 -05:00
|
|
|
|
|
2020-11-18 17:28:14 -05:00
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
if (_addPlayerWaiter.CurrentCount == 0)
|
|
|
|
|
{
|
|
|
|
|
_addPlayerWaiter.Release(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-07 22:34:30 -04:00
|
|
|
|
return null;
|
2018-09-16 16:34:16 -04:00
|
|
|
|
}
|
2018-03-06 02:22:19 -05:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Perform stat updates for disconnecting client
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="pl">Disconnecting client</param>
|
|
|
|
|
/// <returns></returns>
|
2018-11-05 22:01:29 -05:00
|
|
|
|
public async Task RemovePlayer(EFClient pl)
|
2018-09-16 16:34:16 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
_log.LogDebug("Removing {client} from stats", pl.ToString());
|
2018-02-10 01:26:38 -05:00
|
|
|
|
|
2020-02-07 12:15:21 -05:00
|
|
|
|
if (pl.CurrentServer == null)
|
|
|
|
|
{
|
2020-11-14 11:53:01 -05:00
|
|
|
|
_log.LogWarning("Disconnecting client {client} is not on a server", pl.ToString());
|
2020-02-07 12:15:21 -05:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-22 12:09:25 -04:00
|
|
|
|
var serverId = GetIdForServer(pl.CurrentServer);
|
2019-05-08 21:34:17 -04:00
|
|
|
|
var serverStats = _servers[serverId].ServerStatistics;
|
2018-03-06 02:22:19 -05:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
// get individual client's stats
|
2019-10-18 14:39:21 -04:00
|
|
|
|
var clientStats = pl.GetAdditionalProperty<EFClientStatistics>(CLIENT_STATS_KEY);
|
2018-09-16 16:34:16 -04:00
|
|
|
|
// sync their stats before they leave
|
2020-02-07 12:15:21 -05:00
|
|
|
|
if (clientStats != null)
|
|
|
|
|
{
|
2021-06-30 10:57:07 -04:00
|
|
|
|
clientStats = UpdateStats(clientStats, pl);
|
2020-02-07 12:15:21 -05:00
|
|
|
|
await SaveClientStats(clientStats);
|
2021-03-22 12:09:25 -04:00
|
|
|
|
if (_configHandler.Configuration().EnableAdvancedMetrics)
|
|
|
|
|
{
|
|
|
|
|
await UpdateHistoricalRanking(pl.ClientId, clientStats, serverId);
|
|
|
|
|
}
|
2018-10-07 22:34:30 -04:00
|
|
|
|
|
2020-02-07 12:15:21 -05:00
|
|
|
|
// increment the total play time
|
|
|
|
|
serverStats.TotalPlayTime += pl.ConnectionLength;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
pl.SetAdditionalProperty(CLIENT_STATS_KEY, null);
|
2020-02-07 12:15:21 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-11-14 11:53:01 -05:00
|
|
|
|
_log.LogWarning("Disconnecting client {client} has not been added to stats", pl.ToString());
|
2020-02-07 12:15:21 -05:00
|
|
|
|
}
|
2019-08-24 11:02:53 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-25 20:01:26 -04:00
|
|
|
|
private async Task SaveClientStats(EFClientStatistics clientStats)
|
2019-08-24 11:02:53 -04:00
|
|
|
|
{
|
2020-11-29 17:01:52 -05:00
|
|
|
|
await using var ctx = _contextFactory.CreateContext();
|
|
|
|
|
ctx.Update(clientStats);
|
|
|
|
|
await ctx.SaveChangesAsync();
|
2018-09-11 15:28:37 -04:00
|
|
|
|
}
|
2018-04-09 23:33:42 -04:00
|
|
|
|
|
2018-11-27 19:31:48 -05:00
|
|
|
|
public void AddDamageEvent(string eventLine, int attackerClientId, int victimClientId, long serverId)
|
2018-09-11 15:28:37 -04:00
|
|
|
|
{
|
|
|
|
|
}
|
2018-04-09 23:33:42 -04:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Process stats for kill event
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2021-03-22 12:09:25 -04:00
|
|
|
|
public async Task AddScriptHit(bool isDamage, DateTime time, EFClient attacker, EFClient victim, long serverId,
|
|
|
|
|
string map, string hitLoc, string type,
|
|
|
|
|
string damage, string weapon, string killOrigin, string deathOrigin, string viewAngles, string offset,
|
|
|
|
|
string isKillstreakKill, string Ads,
|
2020-01-06 19:43:00 -05:00
|
|
|
|
string fraction, string visibilityPercentage, string snapAngles, string isAlive, string lastAttackTime)
|
2018-09-11 15:28:37 -04:00
|
|
|
|
{
|
2018-09-16 16:34:16 -04:00
|
|
|
|
Vector3 vDeathOrigin = null;
|
|
|
|
|
Vector3 vKillOrigin = null;
|
|
|
|
|
Vector3 vViewAngles = null;
|
2019-11-15 15:50:20 -05:00
|
|
|
|
var snapshotAngles = new List<Vector3>();
|
2019-09-09 18:40:04 -04:00
|
|
|
|
SemaphoreSlim waiter = null;
|
2018-05-03 01:25:49 -04:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
try
|
|
|
|
|
{
|
2019-09-09 18:40:04 -04:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
vDeathOrigin = Vector3.Parse(deathOrigin);
|
|
|
|
|
vKillOrigin = Vector3.Parse(killOrigin);
|
|
|
|
|
vViewAngles = Vector3.Parse(viewAngles).FixIW4Angles();
|
2018-05-08 00:58:46 -04:00
|
|
|
|
|
2019-09-09 18:40:04 -04:00
|
|
|
|
foreach (string angle in snapAngles.Split(':', StringSplitOptions.RemoveEmptyEntries))
|
|
|
|
|
{
|
|
|
|
|
snapshotAngles.Add(Vector3.Parse(angle).FixIW4Angles());
|
|
|
|
|
}
|
2018-09-16 16:34:16 -04:00
|
|
|
|
}
|
2018-03-06 02:22:19 -05:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
catch (FormatException ex)
|
2019-09-09 18:40:04 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
_log.LogWarning(ex, "Could not parse vector data from hit");
|
2019-09-09 18:40:04 -04:00
|
|
|
|
return;
|
|
|
|
|
}
|
2018-03-06 02:22:19 -05:00
|
|
|
|
|
2019-09-09 18:40:04 -04:00
|
|
|
|
var hit = new EFClientKill()
|
|
|
|
|
{
|
|
|
|
|
Active = true,
|
|
|
|
|
AttackerId = attacker.ClientId,
|
|
|
|
|
VictimId = victim.ClientId,
|
|
|
|
|
ServerId = serverId,
|
|
|
|
|
DeathOrigin = vDeathOrigin,
|
|
|
|
|
KillOrigin = vKillOrigin,
|
2021-03-22 12:09:25 -04:00
|
|
|
|
DeathType = (int) ParseEnum<IW4Info.MeansOfDeath>.Get(type, typeof(IW4Info.MeansOfDeath)),
|
2019-09-09 18:40:04 -04:00
|
|
|
|
Damage = int.Parse(damage),
|
2021-03-22 12:09:25 -04:00
|
|
|
|
HitLoc = (int) ParseEnum<IW4Info.HitLocation>.Get(hitLoc, typeof(IW4Info.HitLocation)),
|
2021-06-29 16:02:01 -04:00
|
|
|
|
WeaponReference = weapon,
|
2019-09-09 18:40:04 -04:00
|
|
|
|
ViewAngles = vViewAngles,
|
|
|
|
|
TimeOffset = long.Parse(offset),
|
|
|
|
|
When = time,
|
|
|
|
|
IsKillstreakKill = isKillstreakKill[0] != '0',
|
|
|
|
|
AdsPercent = float.Parse(Ads, System.Globalization.CultureInfo.InvariantCulture),
|
|
|
|
|
Fraction = double.Parse(fraction, System.Globalization.CultureInfo.InvariantCulture),
|
2021-03-22 12:09:25 -04:00
|
|
|
|
VisibilityPercentage = double.Parse(visibilityPercentage,
|
|
|
|
|
System.Globalization.CultureInfo.InvariantCulture),
|
2019-09-09 18:40:04 -04:00
|
|
|
|
IsKill = !isDamage,
|
2020-01-06 19:43:00 -05:00
|
|
|
|
AnglesList = snapshotAngles,
|
|
|
|
|
IsAlive = isAlive == "1",
|
2020-09-30 18:15:47 -04:00
|
|
|
|
TimeSinceLastAttack = long.Parse(lastAttackTime),
|
2021-03-22 12:09:25 -04:00
|
|
|
|
GameName = (int) attacker.CurrentServer.GameName
|
2019-09-09 18:40:04 -04:00
|
|
|
|
};
|
2021-07-08 22:12:09 -04:00
|
|
|
|
|
|
|
|
|
hit.SetAdditionalProperty("HitLocationReference", hitLoc);
|
2018-03-06 02:22:19 -05:00
|
|
|
|
|
2021-03-22 12:09:25 -04:00
|
|
|
|
if (hit.HitLoc == (int) IW4Info.HitLocation.shield)
|
2019-09-09 18:40:04 -04:00
|
|
|
|
{
|
|
|
|
|
// we don't care about shield hits
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-02-26 23:24:19 -05:00
|
|
|
|
|
2019-10-18 14:39:21 -04:00
|
|
|
|
var clientDetection = attacker.GetAdditionalProperty<Detection>(CLIENT_DETECTIONS_KEY);
|
|
|
|
|
var clientStats = attacker.GetAdditionalProperty<EFClientStatistics>(CLIENT_STATS_KEY);
|
2018-09-16 16:34:16 -04:00
|
|
|
|
|
2021-12-26 18:14:06 -05:00
|
|
|
|
if (clientDetection == null || clientStats?.ClientId == null)
|
|
|
|
|
{
|
|
|
|
|
_log.LogWarning("Client stats state for {Client} is not yet initialized", attacker.ToString());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-09 18:40:04 -04:00
|
|
|
|
waiter = clientStats.ProcessingHit;
|
2019-11-15 15:50:20 -05:00
|
|
|
|
await waiter.WaitAsync(Utilities.DefaultCommandTimeout, Plugin.ServerManager.CancellationToken);
|
2019-08-24 11:02:53 -04:00
|
|
|
|
|
2019-09-09 18:40:04 -04:00
|
|
|
|
// increment their hit count
|
2021-03-22 12:09:25 -04:00
|
|
|
|
if (hit.DeathType == (int) IW4Info.MeansOfDeath.MOD_PISTOL_BULLET ||
|
|
|
|
|
hit.DeathType == (int) IW4Info.MeansOfDeath.MOD_RIFLE_BULLET ||
|
|
|
|
|
hit.DeathType == (int) IW4Info.MeansOfDeath.MOD_HEAD_SHOT)
|
2019-09-09 18:40:04 -04:00
|
|
|
|
{
|
|
|
|
|
clientStats.HitLocations.First(hl => hl.Location == hit.HitLoc).HitCount += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (hit.IsKillstreakKill)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-08-24 21:15:50 -04:00
|
|
|
|
|
2019-08-23 19:34:31 -04:00
|
|
|
|
if (Plugin.Config.Configuration().StoreClientKills)
|
2018-03-06 02:22:19 -05:00
|
|
|
|
{
|
2019-11-15 15:50:20 -05:00
|
|
|
|
var serverWaiter = _servers[serverId].OnSaving;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await serverWaiter.WaitAsync();
|
|
|
|
|
var cache = _servers[serverId].HitCache;
|
|
|
|
|
cache.Add(hit);
|
2019-08-23 19:34:31 -04:00
|
|
|
|
|
2019-11-15 15:50:20 -05:00
|
|
|
|
if (cache.Count > MAX_CACHED_HITS)
|
|
|
|
|
{
|
|
|
|
|
await SaveHitCache(serverId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (Exception e)
|
2019-08-23 22:27:36 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
_log.LogError(e, "Could not store client kills");
|
2019-11-15 15:50:20 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
if (serverWaiter.CurrentCount == 0)
|
|
|
|
|
{
|
|
|
|
|
serverWaiter.Release(1);
|
|
|
|
|
}
|
2018-09-23 20:45:54 -04:00
|
|
|
|
}
|
2019-08-23 19:34:31 -04:00
|
|
|
|
}
|
2018-09-23 20:45:54 -04:00
|
|
|
|
|
2021-03-22 12:09:25 -04:00
|
|
|
|
if (Plugin.Config.Configuration().AnticheatConfiguration.Enable && !attacker.IsBot &&
|
|
|
|
|
attacker.ClientId != victim.ClientId)
|
2019-08-23 19:34:31 -04:00
|
|
|
|
{
|
2019-09-09 18:40:04 -04:00
|
|
|
|
clientDetection.TrackedHits.Add(hit);
|
2019-08-28 14:45:53 -04:00
|
|
|
|
|
2020-01-06 19:43:00 -05:00
|
|
|
|
if (clientDetection.TrackedHits.Count >= MIN_HITS_TO_RUN_DETECTION)
|
2019-08-23 19:34:31 -04:00
|
|
|
|
{
|
|
|
|
|
while (clientDetection.TrackedHits.Count > 0)
|
2018-11-05 22:01:29 -05:00
|
|
|
|
{
|
2019-09-09 18:40:04 -04:00
|
|
|
|
var oldestHit = clientDetection.TrackedHits
|
|
|
|
|
.OrderBy(_hits => _hits.TimeOffset)
|
|
|
|
|
.First();
|
2019-06-24 17:56:47 -04:00
|
|
|
|
|
2019-08-23 19:34:31 -04:00
|
|
|
|
clientDetection.TrackedHits.Remove(oldestHit);
|
2019-06-24 17:56:47 -04:00
|
|
|
|
|
2020-01-06 19:43:00 -05:00
|
|
|
|
if (oldestHit.IsAlive)
|
|
|
|
|
{
|
2020-09-30 18:15:47 -04:00
|
|
|
|
var result = DeterminePenaltyResult(clientDetection.ProcessHit(oldestHit), attacker);
|
2020-09-30 22:00:40 -04:00
|
|
|
|
|
|
|
|
|
if (!Utilities.IsDevelopment)
|
2020-09-30 18:15:47 -04:00
|
|
|
|
{
|
|
|
|
|
await ApplyPenalty(result, attacker);
|
|
|
|
|
}
|
2019-06-16 15:49:04 -04:00
|
|
|
|
|
2021-03-22 12:09:25 -04:00
|
|
|
|
if (clientDetection.Tracker.HasChanges &&
|
|
|
|
|
result.ClientPenalty != EFPenalty.PenaltyType.Any)
|
2019-08-23 19:34:31 -04:00
|
|
|
|
{
|
2020-01-06 19:43:00 -05:00
|
|
|
|
await SaveTrackedSnapshots(clientDetection);
|
|
|
|
|
|
|
|
|
|
if (result.ClientPenalty == EFPenalty.PenaltyType.Ban)
|
|
|
|
|
{
|
|
|
|
|
// we don't care about any additional hits now that they're banned
|
|
|
|
|
clientDetection.TrackedHits.Clear();
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-08-23 19:34:31 -04:00
|
|
|
|
}
|
2019-06-16 15:49:04 -04:00
|
|
|
|
}
|
2018-11-05 22:01:29 -05:00
|
|
|
|
}
|
2018-09-23 20:45:54 -04:00
|
|
|
|
}
|
2018-09-11 15:28:37 -04:00
|
|
|
|
}
|
2019-08-23 19:34:31 -04:00
|
|
|
|
}
|
2018-09-23 20:45:54 -04:00
|
|
|
|
|
2021-03-22 12:09:25 -04:00
|
|
|
|
catch (TaskCanceledException)
|
|
|
|
|
{
|
|
|
|
|
}
|
2019-08-23 19:34:31 -04:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2021-03-22 12:09:25 -04:00
|
|
|
|
_log.LogError(ex, "Could not save hit or anti-cheat info {@attacker} {@victim} {server}", attacker,
|
|
|
|
|
victim, serverId);
|
2019-09-09 18:40:04 -04:00
|
|
|
|
}
|
2019-08-24 15:06:23 -04:00
|
|
|
|
|
2019-09-09 18:40:04 -04:00
|
|
|
|
finally
|
|
|
|
|
{
|
2019-11-15 15:50:20 -05:00
|
|
|
|
if (waiter?.CurrentCount == 0)
|
|
|
|
|
{
|
|
|
|
|
waiter.Release();
|
|
|
|
|
}
|
2019-09-09 18:40:04 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-22 12:09:25 -04:00
|
|
|
|
private DetectionPenaltyResult DeterminePenaltyResult(IEnumerable<DetectionPenaltyResult> results,
|
|
|
|
|
EFClient client)
|
2020-02-06 19:35:30 -05:00
|
|
|
|
{
|
|
|
|
|
// allow disabling of certain detection types
|
2020-09-30 18:15:47 -04:00
|
|
|
|
results = results.Where(_result => ShouldUseDetection(client.CurrentServer, _result.Type, client.ClientId));
|
2020-02-06 19:35:30 -05:00
|
|
|
|
return results.FirstOrDefault(_result => _result.ClientPenalty == EFPenalty.PenaltyType.Ban) ??
|
2021-03-22 12:09:25 -04:00
|
|
|
|
results.FirstOrDefault(_result => _result.ClientPenalty == EFPenalty.PenaltyType.Flag) ??
|
|
|
|
|
new DetectionPenaltyResult()
|
|
|
|
|
{
|
|
|
|
|
ClientPenalty = EFPenalty.PenaltyType.Any,
|
|
|
|
|
};
|
2020-02-06 19:35:30 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-09 18:40:04 -04:00
|
|
|
|
public async Task SaveHitCache(long serverId)
|
|
|
|
|
{
|
2020-11-29 17:01:52 -05:00
|
|
|
|
await using var ctx = _contextFactory.CreateContext(enableTracking: false);
|
|
|
|
|
var server = _servers[serverId];
|
|
|
|
|
ctx.AddRange(server.HitCache.ToList());
|
|
|
|
|
await ctx.SaveChangesAsync();
|
|
|
|
|
server.HitCache.Clear();
|
2018-09-16 16:34:16 -04:00
|
|
|
|
}
|
2018-09-11 15:28:37 -04:00
|
|
|
|
|
2020-09-30 18:15:47 -04:00
|
|
|
|
private bool ShouldUseDetection(Server server, DetectionType detectionType, long clientId)
|
2019-10-23 11:40:24 -04:00
|
|
|
|
{
|
2020-09-30 18:15:47 -04:00
|
|
|
|
var detectionTypes = Plugin.Config.Configuration().AnticheatConfiguration.ServerDetectionTypes;
|
|
|
|
|
var ignoredClients = Plugin.Config.Configuration().AnticheatConfiguration.IgnoredClientIds;
|
2019-10-23 11:40:24 -04:00
|
|
|
|
|
2020-09-30 22:00:40 -04:00
|
|
|
|
if (ignoredClients.Contains(clientId))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-09-30 18:15:47 -04:00
|
|
|
|
try
|
2019-10-23 11:40:24 -04:00
|
|
|
|
{
|
2020-10-02 09:09:38 -04:00
|
|
|
|
if (!detectionTypes[server.EndPoint].Contains(detectionType))
|
2020-09-30 22:00:40 -04:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-10-23 11:40:24 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-30 18:15:47 -04:00
|
|
|
|
catch (KeyNotFoundException)
|
2019-10-23 11:40:24 -04:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-30 22:00:40 -04:00
|
|
|
|
return true;
|
2019-10-23 11:40:24 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-23 19:34:31 -04:00
|
|
|
|
async Task ApplyPenalty(DetectionPenaltyResult penalty, EFClient attacker)
|
2018-09-16 16:34:16 -04:00
|
|
|
|
{
|
2019-04-25 14:00:54 -04:00
|
|
|
|
var penaltyClient = Utilities.IW4MAdminClient(attacker.CurrentServer);
|
2018-09-23 20:45:54 -04:00
|
|
|
|
switch (penalty.ClientPenalty)
|
2018-09-16 16:34:16 -04:00
|
|
|
|
{
|
2019-05-29 17:55:35 -04:00
|
|
|
|
case EFPenalty.PenaltyType.Ban:
|
2018-11-05 22:01:29 -05:00
|
|
|
|
if (attacker.Level == EFClient.Permission.Banned)
|
2018-09-23 20:45:54 -04:00
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-04-25 14:00:54 -04:00
|
|
|
|
|
|
|
|
|
penaltyClient.AdministeredPenalties = new List<EFPenalty>()
|
2018-09-23 20:45:54 -04:00
|
|
|
|
{
|
2019-04-25 14:00:54 -04:00
|
|
|
|
new EFPenalty()
|
|
|
|
|
{
|
2021-03-22 12:09:25 -04:00
|
|
|
|
AutomatedOffense = penalty.Type == Detection.DetectionType.Bone
|
|
|
|
|
? $"{penalty.Type}-{(int) penalty.Location}-{Math.Round(penalty.Value, 2)}@{penalty.HitCount}"
|
|
|
|
|
: $"{penalty.Type}-{Math.Round(penalty.Value, 2)}@{penalty.HitCount}",
|
2019-04-25 14:00:54 -04:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-03-22 12:09:25 -04:00
|
|
|
|
await attacker
|
|
|
|
|
.Ban(Utilities.CurrentLocalization.LocalizationIndex["PLUGIN_STATS_CHEAT_DETECTED"],
|
|
|
|
|
penaltyClient, false).WaitAsync(Utilities.DefaultCommandTimeout,
|
|
|
|
|
attacker.CurrentServer.Manager.CancellationToken);
|
2018-09-23 20:45:54 -04:00
|
|
|
|
break;
|
2019-05-29 17:55:35 -04:00
|
|
|
|
case EFPenalty.PenaltyType.Flag:
|
2018-11-05 22:01:29 -05:00
|
|
|
|
if (attacker.Level != EFClient.Permission.User)
|
2018-09-23 20:45:54 -04:00
|
|
|
|
{
|
2018-09-16 16:34:16 -04:00
|
|
|
|
break;
|
2018-09-23 20:45:54 -04:00
|
|
|
|
}
|
2018-10-05 23:10:39 -04:00
|
|
|
|
|
2021-03-22 12:09:25 -04:00
|
|
|
|
string flagReason = penalty.Type == Cheat.Detection.DetectionType.Bone
|
|
|
|
|
? $"{penalty.Type}-{(int) penalty.Location}-{Math.Round(penalty.Value, 2)}@{penalty.HitCount}"
|
|
|
|
|
: $"{penalty.Type}-{Math.Round(penalty.Value, 2)}@{penalty.HitCount}";
|
2018-10-05 23:10:39 -04:00
|
|
|
|
|
2020-08-17 22:21:11 -04:00
|
|
|
|
penaltyClient.AdministeredPenalties = new List<EFPenalty>()
|
|
|
|
|
{
|
|
|
|
|
new EFPenalty()
|
|
|
|
|
{
|
|
|
|
|
AutomatedOffense = flagReason
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-03-22 12:09:25 -04:00
|
|
|
|
await attacker.Flag(flagReason, penaltyClient, new TimeSpan(168, 0, 0))
|
|
|
|
|
.WaitAsync(Utilities.DefaultCommandTimeout, attacker.CurrentServer.Manager.CancellationToken);
|
2018-09-23 20:45:54 -04:00
|
|
|
|
break;
|
2018-03-13 17:30:22 -04:00
|
|
|
|
}
|
2018-02-07 00:19:06 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-09 18:40:04 -04:00
|
|
|
|
async Task SaveTrackedSnapshots(Detection clientDetection)
|
2018-02-07 00:19:06 -05:00
|
|
|
|
{
|
2019-05-13 11:36:11 -04:00
|
|
|
|
EFACSnapshot change;
|
2018-09-23 20:45:54 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
await using var ctx = _contextFactory.CreateContext();
|
|
|
|
|
while ((change = clientDetection.Tracker.GetNextChange()) != default(EFACSnapshot))
|
2019-09-09 18:40:04 -04:00
|
|
|
|
{
|
2020-11-29 17:01:52 -05:00
|
|
|
|
ctx.Add(change);
|
2018-09-23 20:45:54 -04:00
|
|
|
|
}
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
await ctx.SaveChangesAsync();
|
2018-09-11 15:28:37 -04:00
|
|
|
|
}
|
2018-03-06 02:22:19 -05:00
|
|
|
|
|
2018-11-05 22:01:29 -05:00
|
|
|
|
public async Task AddStandardKill(EFClient attacker, EFClient victim)
|
2018-09-11 15:28:37 -04:00
|
|
|
|
{
|
2022-01-24 16:08:31 -05:00
|
|
|
|
var serverId = GetIdForServer(attacker.CurrentServer);
|
2018-09-29 15:52:22 -04:00
|
|
|
|
|
2019-10-18 14:39:21 -04:00
|
|
|
|
var attackerStats = attacker.GetAdditionalProperty<EFClientStatistics>(CLIENT_STATS_KEY);
|
|
|
|
|
var victimStats = victim.GetAdditionalProperty<EFClientStatistics>(CLIENT_STATS_KEY);
|
2018-02-08 02:23:45 -05:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
// update the total stats
|
2019-05-08 21:34:17 -04:00
|
|
|
|
_servers[serverId].ServerStatistics.TotalKills += 1;
|
2021-06-16 09:53:50 -04:00
|
|
|
|
|
2022-01-24 16:08:31 -05:00
|
|
|
|
if (attackerStats == null)
|
|
|
|
|
{
|
|
|
|
|
_log.LogWarning("Stats for {Client} are not yet initialized", attacker.ToString());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (victimStats == null)
|
|
|
|
|
{
|
|
|
|
|
_log.LogWarning("Stats for {Client} are not yet initialized", victim.ToString());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
// this happens when the round has changed
|
|
|
|
|
if (attackerStats.SessionScore == 0)
|
2018-11-05 22:01:29 -05:00
|
|
|
|
{
|
2018-09-16 16:34:16 -04:00
|
|
|
|
attackerStats.LastScore = 0;
|
2018-11-05 22:01:29 -05:00
|
|
|
|
}
|
2018-04-28 01:22:18 -04:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
if (victimStats.SessionScore == 0)
|
2018-11-05 22:01:29 -05:00
|
|
|
|
{
|
2018-09-16 16:34:16 -04:00
|
|
|
|
victimStats.LastScore = 0;
|
2018-11-05 22:01:29 -05:00
|
|
|
|
}
|
2018-04-28 01:22:18 -04:00
|
|
|
|
|
2021-07-01 14:06:31 -04:00
|
|
|
|
var estimatedAttackerScore = attacker.CurrentServer.GameName != Server.Game.CSGO
|
2021-06-27 21:31:39 -04:00
|
|
|
|
? attacker.Score
|
2021-06-29 16:02:01 -04:00
|
|
|
|
: attackerStats.SessionKills * 50;
|
2021-07-01 14:06:31 -04:00
|
|
|
|
var estimatedVictimScore = attacker.CurrentServer.GameName != Server.Game.CSGO
|
2021-06-27 21:31:39 -04:00
|
|
|
|
? victim.Score
|
2021-06-29 16:02:01 -04:00
|
|
|
|
: victimStats.SessionKills * 50;
|
2021-06-16 09:53:50 -04:00
|
|
|
|
|
|
|
|
|
attackerStats.SessionScore = estimatedAttackerScore;
|
|
|
|
|
victimStats.SessionScore = estimatedVictimScore;
|
|
|
|
|
|
|
|
|
|
attacker.SetAdditionalProperty(ESTIMATED_SCORE, estimatedAttackerScore);
|
|
|
|
|
victim.SetAdditionalProperty(ESTIMATED_SCORE, estimatedVictimScore);
|
2018-03-06 02:22:19 -05:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
// calculate for the clients
|
2021-06-30 10:57:07 -04:00
|
|
|
|
CalculateKill(attackerStats, victimStats, attacker, victim);
|
2018-09-16 16:34:16 -04:00
|
|
|
|
// this should fix the negative SPM
|
|
|
|
|
// updates their last score after being calculated
|
2021-06-16 09:53:50 -04:00
|
|
|
|
attackerStats.LastScore = estimatedAttackerScore;
|
|
|
|
|
victimStats.LastScore = estimatedVictimScore;
|
2018-02-09 02:21:25 -05:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
// show encouragement/discouragement
|
2021-06-16 09:53:50 -04:00
|
|
|
|
var streakMessage = (attackerStats.ClientId != victimStats.ClientId)
|
2021-03-22 12:09:25 -04:00
|
|
|
|
? StreakMessage.MessageOnStreak(attackerStats.KillStreak, attackerStats.DeathStreak)
|
|
|
|
|
: StreakMessage.MessageOnStreak(-1, -1);
|
2018-02-10 23:33:42 -05:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
if (streakMessage != string.Empty)
|
2018-09-29 15:52:22 -04:00
|
|
|
|
{
|
|
|
|
|
attacker.Tell(streakMessage);
|
|
|
|
|
}
|
2018-04-10 20:25:44 -04:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
// fixme: why?
|
|
|
|
|
if (double.IsNaN(victimStats.SPM) || double.IsNaN(victimStats.Skill))
|
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
_log.LogWarning("victim SPM/SKILL {@victimStats}", victimStats);
|
2018-09-16 16:34:16 -04:00
|
|
|
|
victimStats.SPM = 0.0;
|
|
|
|
|
victimStats.Skill = 0.0;
|
|
|
|
|
}
|
2018-04-11 18:24:21 -04:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
if (double.IsNaN(attackerStats.SPM) || double.IsNaN(attackerStats.Skill))
|
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
_log.LogWarning("attacker SPM/SKILL {@attackerStats}", attackerStats);
|
2018-09-16 16:34:16 -04:00
|
|
|
|
attackerStats.SPM = 0.0;
|
|
|
|
|
attackerStats.Skill = 0.0;
|
|
|
|
|
}
|
2018-04-13 02:32:30 -04:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
// update their performance
|
2021-03-22 12:09:25 -04:00
|
|
|
|
if ((DateTime.UtcNow - attackerStats.LastStatHistoryUpdate).TotalMinutes >=
|
2022-01-24 16:08:31 -05:00
|
|
|
|
(Utilities.IsDevelopment ? 0.5 : _configHandler.Configuration().EnableAdvancedMetrics ? 5.0 : 2.5))
|
2018-09-16 16:34:16 -04:00
|
|
|
|
{
|
2020-04-25 20:01:26 -04:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// kill event is not designated as blocking, so we should be able to enter and exit
|
|
|
|
|
// we need to make this thread safe because we can potentially have kills qualify
|
|
|
|
|
// for stat history update, but one is already processing that invalidates the original
|
2021-03-22 12:09:25 -04:00
|
|
|
|
await attackerStats.ProcessingHit.WaitAsync(Utilities.DefaultCommandTimeout,
|
|
|
|
|
Plugin.ServerManager.CancellationToken);
|
|
|
|
|
if (_configHandler.Configuration().EnableAdvancedMetrics)
|
|
|
|
|
{
|
|
|
|
|
await UpdateHistoricalRanking(attacker.ClientId, attackerStats, serverId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
await UpdateStatHistory(attacker, attackerStats);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-25 20:01:26 -04:00
|
|
|
|
attackerStats.LastStatHistoryUpdate = DateTime.UtcNow;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
_log.LogWarning(e, "Could not update stat history for {attacker}", attacker.ToString());
|
2020-04-25 20:01:26 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
finally
|
|
|
|
|
{
|
2020-04-26 13:32:41 -04:00
|
|
|
|
if (attackerStats.ProcessingHit.CurrentCount == 0)
|
|
|
|
|
{
|
|
|
|
|
attackerStats.ProcessingHit.Release(1);
|
|
|
|
|
}
|
2020-04-25 20:01:26 -04:00
|
|
|
|
}
|
2018-09-16 16:34:16 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-07 23:29:42 -04:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
/// <summary>
|
2021-03-22 12:09:25 -04:00
|
|
|
|
/// Update the individual and average stat history for a client
|
2018-09-16 16:34:16 -04:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="client">client to update</param>
|
|
|
|
|
/// <param name="clientStats">stats of client that is being updated</param>
|
|
|
|
|
/// <returns></returns>
|
2020-04-25 20:01:26 -04:00
|
|
|
|
public async Task UpdateStatHistory(EFClient client, EFClientStatistics clientStats)
|
2018-09-16 16:34:16 -04:00
|
|
|
|
{
|
2021-03-22 12:09:25 -04:00
|
|
|
|
int currentSessionTime = (int) (DateTime.UtcNow - client.LastConnection).TotalSeconds;
|
2018-09-11 15:28:37 -04:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
// don't update their stat history if they haven't played long
|
2018-09-07 23:29:42 -04:00
|
|
|
|
if (currentSessionTime < 60)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
int currentServerTotalPlaytime = clientStats.TimePlayed + currentSessionTime;
|
2018-05-30 21:50:20 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
await using var ctx = _contextFactory.CreateContext(enableTracking: true);
|
|
|
|
|
// select the rating history for client
|
|
|
|
|
var iqHistoryLink = from history in ctx.Set<EFClientRatingHistory>()
|
2021-03-22 12:09:25 -04:00
|
|
|
|
.Include(h => h.Ratings)
|
|
|
|
|
where history.ClientId == client.ClientId
|
|
|
|
|
select history;
|
2018-09-16 16:34:16 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
// get the client ratings
|
|
|
|
|
var clientHistory = await iqHistoryLink
|
|
|
|
|
.FirstOrDefaultAsync() ?? new EFClientRatingHistory()
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
|
|
|
|
Active = true,
|
|
|
|
|
ClientId = client.ClientId,
|
|
|
|
|
Ratings = new List<EFRating>()
|
|
|
|
|
};
|
2018-09-16 16:34:16 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
// it's the first time they've played
|
|
|
|
|
if (clientHistory.RatingHistoryId == 0)
|
|
|
|
|
{
|
|
|
|
|
ctx.Add(clientHistory);
|
|
|
|
|
}
|
2018-09-16 16:34:16 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
#region INDIVIDUAL_SERVER_PERFORMANCE
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
// get the client ranking for the current server
|
|
|
|
|
int individualClientRanking = await ctx.Set<EFRating>()
|
2021-03-22 12:09:25 -04:00
|
|
|
|
.Where(GetRankingFunc(clientStats.ServerId))
|
|
|
|
|
// ignore themselves in the query
|
|
|
|
|
.Where(c => c.RatingHistory.ClientId != client.ClientId)
|
|
|
|
|
.Where(c => c.Performance > clientStats.Performance)
|
|
|
|
|
.CountAsync() + 1;
|
2020-11-29 17:01:52 -05:00
|
|
|
|
|
|
|
|
|
// limit max history per server to 40
|
|
|
|
|
if (clientHistory.Ratings.Count(r => r.ServerId == clientStats.ServerId) >= 40)
|
|
|
|
|
{
|
|
|
|
|
// select the oldest one
|
|
|
|
|
var ratingToRemove = clientHistory.Ratings
|
2018-09-16 16:34:16 -04:00
|
|
|
|
.Where(r => r.ServerId == clientStats.ServerId)
|
2020-11-29 17:01:52 -05:00
|
|
|
|
.OrderBy(r => r.When)
|
|
|
|
|
.First();
|
2018-09-16 16:34:16 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
ctx.Remove(ratingToRemove);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// set the previous newest to false
|
|
|
|
|
var ratingToUnsetNewest = clientHistory.Ratings
|
|
|
|
|
.Where(r => r.ServerId == clientStats.ServerId)
|
|
|
|
|
.OrderByDescending(r => r.When)
|
|
|
|
|
.FirstOrDefault();
|
2018-09-16 16:34:16 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
if (ratingToUnsetNewest != null)
|
|
|
|
|
{
|
|
|
|
|
if (ratingToUnsetNewest.Newest)
|
2018-09-16 16:34:16 -04:00
|
|
|
|
{
|
2020-11-29 17:01:52 -05:00
|
|
|
|
ctx.Update(ratingToUnsetNewest);
|
|
|
|
|
ctx.Entry(ratingToUnsetNewest).Property(r => r.Newest).IsModified = true;
|
|
|
|
|
ratingToUnsetNewest.Newest = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-08 18:29:30 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
var newServerRating = new EFRating()
|
|
|
|
|
{
|
|
|
|
|
Performance = clientStats.Performance,
|
|
|
|
|
Ranking = individualClientRanking,
|
|
|
|
|
Active = true,
|
|
|
|
|
Newest = true,
|
|
|
|
|
ServerId = clientStats.ServerId,
|
|
|
|
|
RatingHistory = clientHistory,
|
|
|
|
|
ActivityAmount = currentServerTotalPlaytime,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// add new rating for current server
|
|
|
|
|
ctx.Add(newServerRating);
|
2018-09-16 16:34:16 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
#endregion
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
#region OVERALL_RATING
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
// select all performance & time played for current client
|
|
|
|
|
var iqClientStats = from stats in ctx.Set<EFClientStatistics>()
|
2021-03-22 12:09:25 -04:00
|
|
|
|
where stats.ClientId == client.ClientId
|
|
|
|
|
where stats.ServerId != clientStats.ServerId
|
|
|
|
|
select new
|
|
|
|
|
{
|
|
|
|
|
stats.Performance,
|
|
|
|
|
stats.TimePlayed
|
|
|
|
|
};
|
2018-05-30 21:50:20 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
var clientStatsList = await iqClientStats.ToListAsync();
|
2018-08-28 17:32:59 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
// add the current server's so we don't have to pull it from the database
|
|
|
|
|
clientStatsList.Add(new
|
|
|
|
|
{
|
|
|
|
|
clientStats.Performance,
|
|
|
|
|
TimePlayed = currentServerTotalPlaytime
|
|
|
|
|
});
|
2018-05-30 21:50:20 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
// weight the overall performance based on play time
|
2021-03-22 12:09:25 -04:00
|
|
|
|
double performanceAverage = clientStatsList.Sum(p => (p.Performance * p.TimePlayed)) /
|
|
|
|
|
clientStatsList.Sum(p => p.TimePlayed);
|
2018-05-30 21:50:20 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
// shouldn't happen but just in case the sum of time played is 0
|
|
|
|
|
if (double.IsNaN(performanceAverage))
|
|
|
|
|
{
|
|
|
|
|
performanceAverage = clientStatsList.Average(p => p.Performance);
|
|
|
|
|
}
|
2018-09-16 16:34:16 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
int overallClientRanking = await ctx.Set<EFRating>()
|
2021-03-22 12:09:25 -04:00
|
|
|
|
.Where(GetRankingFunc())
|
|
|
|
|
.Where(r => r.RatingHistory.ClientId != client.ClientId)
|
|
|
|
|
.Where(r => r.Performance > performanceAverage)
|
|
|
|
|
.CountAsync() + 1;
|
2018-09-16 16:34:16 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
// limit max average history to 40
|
|
|
|
|
if (clientHistory.Ratings.Count(r => r.ServerId == null) >= 40)
|
|
|
|
|
{
|
|
|
|
|
var ratingToRemove = clientHistory.Ratings
|
|
|
|
|
.Where(r => r.ServerId == null)
|
|
|
|
|
.OrderBy(r => r.When)
|
|
|
|
|
.First();
|
2018-09-16 16:34:16 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
ctx.Remove(ratingToRemove);
|
|
|
|
|
}
|
2018-05-31 20:17:52 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
// set the previous average newest to false
|
|
|
|
|
ratingToUnsetNewest = clientHistory.Ratings
|
|
|
|
|
.Where(r => r.ServerId == null)
|
|
|
|
|
.OrderByDescending(r => r.When)
|
|
|
|
|
.FirstOrDefault();
|
2018-09-08 18:29:30 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
if (ratingToUnsetNewest != null)
|
|
|
|
|
{
|
|
|
|
|
if (ratingToUnsetNewest.Newest)
|
2018-09-16 16:34:16 -04:00
|
|
|
|
{
|
2020-11-29 17:01:52 -05:00
|
|
|
|
ctx.Update(ratingToUnsetNewest);
|
|
|
|
|
ctx.Entry(ratingToUnsetNewest).Property(r => r.Newest).IsModified = true;
|
|
|
|
|
ratingToUnsetNewest.Newest = false;
|
2018-09-16 16:34:16 -04:00
|
|
|
|
}
|
2020-11-29 17:01:52 -05:00
|
|
|
|
}
|
2018-05-30 21:50:20 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
// add new average rating
|
|
|
|
|
var averageRating = new EFRating()
|
|
|
|
|
{
|
|
|
|
|
Active = true,
|
|
|
|
|
Newest = true,
|
|
|
|
|
Performance = performanceAverage,
|
|
|
|
|
Ranking = overallClientRanking,
|
|
|
|
|
ServerId = null,
|
|
|
|
|
RatingHistory = clientHistory,
|
|
|
|
|
ActivityAmount = clientStatsList.Sum(s => s.TimePlayed)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ctx.Add(averageRating);
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
#endregion
|
2018-09-29 15:52:22 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
await ctx.SaveChangesAsync();
|
2018-09-16 16:34:16 -04:00
|
|
|
|
}
|
2018-05-30 21:50:20 -04:00
|
|
|
|
|
2021-03-22 12:09:25 -04:00
|
|
|
|
public async Task UpdateHistoricalRanking(int clientId, EFClientStatistics clientStats, long serverId)
|
|
|
|
|
{
|
|
|
|
|
await using var context = _contextFactory.CreateContext();
|
2022-01-24 16:08:31 -05:00
|
|
|
|
var minPlayTime = _configHandler.Configuration().TopPlayersMinPlayTime;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
|
|
|
|
var performances = await context.Set<EFClientStatistics>()
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.Where(stat => stat.ClientId == clientId)
|
|
|
|
|
.Where(stat => stat.ServerId != serverId) // ignore the one we're currently tracking
|
|
|
|
|
.Where(stats => stats.UpdatedAt >= Extensions.FifteenDaysAgo())
|
2022-01-24 16:08:31 -05:00
|
|
|
|
.Where(stats => stats.TimePlayed >= minPlayTime)
|
2021-03-22 12:09:25 -04:00
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
2022-01-24 16:08:31 -05:00
|
|
|
|
if (clientStats.TimePlayed >= minPlayTime)
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
|
|
|
|
clientStats.ZScore = await _serverDistributionCalculator.GetZScoreForServer(serverId,
|
|
|
|
|
clientStats.Performance);
|
|
|
|
|
|
|
|
|
|
var serverRanking = await context.Set<EFClientStatistics>()
|
|
|
|
|
.Where(stats => stats.ClientId != clientStats.ClientId)
|
|
|
|
|
.Where(AdvancedClientStatsResourceQueryHelper.GetRankingFunc(
|
|
|
|
|
_configHandler.Configuration().TopPlayersMinPlayTime, clientStats.ZScore, serverId))
|
|
|
|
|
.CountAsync();
|
|
|
|
|
|
2022-01-24 16:08:31 -05:00
|
|
|
|
var serverRankingSnapshot = new EFClientRankingHistory
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
|
|
|
|
ClientId = clientId,
|
|
|
|
|
ServerId = serverId,
|
|
|
|
|
ZScore = clientStats.ZScore,
|
|
|
|
|
Ranking = serverRanking,
|
|
|
|
|
PerformanceMetric = clientStats.Performance,
|
|
|
|
|
Newest = true
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
context.Add(serverRankingSnapshot);
|
|
|
|
|
await PruneOldRankings(context, clientId, serverId);
|
|
|
|
|
await context.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
performances.Add(clientStats);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-24 16:08:31 -05:00
|
|
|
|
if (performances.Any(performance => performance.TimePlayed >= minPlayTime))
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
2022-01-24 16:08:31 -05:00
|
|
|
|
var aggregateZScore = performances.WeightValueByPlaytime(nameof(EFClientStatistics.ZScore), minPlayTime);
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
|
|
|
|
int? aggregateRanking = await context.Set<EFClientStatistics>()
|
|
|
|
|
.Where(stat => stat.ClientId != clientId)
|
2022-01-24 16:08:31 -05:00
|
|
|
|
.Where(AdvancedClientStatsResourceQueryHelper.GetRankingFunc(minPlayTime))
|
2021-03-22 12:09:25 -04:00
|
|
|
|
.GroupBy(stat => stat.ClientId)
|
|
|
|
|
.Where(group =>
|
|
|
|
|
group.Sum(stat => stat.ZScore * stat.TimePlayed) / group.Sum(stat => stat.TimePlayed) >
|
|
|
|
|
aggregateZScore)
|
|
|
|
|
.Select(c => c.Key)
|
|
|
|
|
.CountAsync();
|
2022-01-24 16:08:31 -05:00
|
|
|
|
|
|
|
|
|
var newPerformanceMetric = await _serverDistributionCalculator.GetRatingForZScore(aggregateZScore);
|
|
|
|
|
|
|
|
|
|
if (newPerformanceMetric == null)
|
|
|
|
|
{
|
|
|
|
|
_log.LogWarning("Could not determine performance metric for {Client} {AggregateZScore}",
|
|
|
|
|
clientStats.Client?.ToString(), aggregateZScore);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2022-01-24 16:08:31 -05:00
|
|
|
|
var aggregateRankingSnapshot = new EFClientRankingHistory
|
2021-03-22 12:09:25 -04:00
|
|
|
|
{
|
|
|
|
|
ClientId = clientId,
|
|
|
|
|
ZScore = aggregateZScore,
|
|
|
|
|
Ranking = aggregateRanking,
|
2022-01-24 16:08:31 -05:00
|
|
|
|
PerformanceMetric = newPerformanceMetric,
|
2021-03-22 12:09:25 -04:00
|
|
|
|
Newest = true,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
context.Add(aggregateRankingSnapshot);
|
|
|
|
|
|
|
|
|
|
await PruneOldRankings(context, clientId);
|
|
|
|
|
await context.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task PruneOldRankings(DatabaseContext context, int clientId, long? serverId = null)
|
|
|
|
|
{
|
|
|
|
|
var totalRankingEntries = await context.Set<EFClientRankingHistory>()
|
|
|
|
|
.Where(r => r.ClientId == clientId)
|
|
|
|
|
.Where(r => r.ServerId == serverId)
|
|
|
|
|
.CountAsync();
|
|
|
|
|
|
|
|
|
|
var mostRecent = await context.Set<EFClientRankingHistory>()
|
|
|
|
|
.Where(r => r.ClientId == clientId)
|
|
|
|
|
.Where(r => r.ServerId == serverId)
|
|
|
|
|
.FirstOrDefaultAsync(r => r.Newest);
|
|
|
|
|
|
|
|
|
|
if (mostRecent != null)
|
|
|
|
|
{
|
|
|
|
|
mostRecent.Newest = false;
|
|
|
|
|
context.Update(mostRecent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (totalRankingEntries > EFClientRankingHistory.MaxRankingCount)
|
|
|
|
|
{
|
|
|
|
|
var lastRating = await context.Set<EFClientRankingHistory>()
|
|
|
|
|
.Where(r => r.ClientId == clientId)
|
|
|
|
|
.Where(r => r.ServerId == serverId)
|
|
|
|
|
.OrderBy(r => r.CreatedDateTime)
|
|
|
|
|
.FirstOrDefaultAsync();
|
|
|
|
|
context.Remove(lastRating);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Performs the incrementation of kills and deaths for client statistics
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="attackerStats">Stats of the attacker</param>
|
|
|
|
|
/// <param name="victimStats">Stats of the victim</param>
|
2021-06-30 10:57:07 -04:00
|
|
|
|
public void CalculateKill(EFClientStatistics attackerStats, EFClientStatistics victimStats,
|
|
|
|
|
EFClient attacker, EFClient victim)
|
2018-09-16 16:34:16 -04:00
|
|
|
|
{
|
|
|
|
|
bool suicide = attackerStats.ClientId == victimStats.ClientId;
|
2018-02-10 23:33:42 -05:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
// only update their kills if they didn't kill themselves
|
|
|
|
|
if (!suicide)
|
2018-02-10 23:33:42 -05:00
|
|
|
|
{
|
2018-09-16 16:34:16 -04:00
|
|
|
|
attackerStats.Kills += 1;
|
2022-01-22 13:49:12 -05:00
|
|
|
|
attackerStats.MatchData.Kills += 1;
|
2018-09-16 16:34:16 -04:00
|
|
|
|
attackerStats.SessionKills += 1;
|
|
|
|
|
attackerStats.KillStreak += 1;
|
|
|
|
|
attackerStats.DeathStreak = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
victimStats.Deaths += 1;
|
2022-01-22 13:49:12 -05:00
|
|
|
|
victimStats.MatchData.Deaths += 1;
|
2018-09-16 16:34:16 -04:00
|
|
|
|
victimStats.SessionDeaths += 1;
|
|
|
|
|
victimStats.DeathStreak += 1;
|
|
|
|
|
victimStats.KillStreak = 0;
|
2018-02-08 02:23:45 -05:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
// process the attacker's stats after the kills
|
2021-06-30 10:57:07 -04:00
|
|
|
|
attackerStats = UpdateStats(attackerStats, attacker);
|
2018-02-08 02:23:45 -05:00
|
|
|
|
|
2019-10-18 14:39:21 -04:00
|
|
|
|
// calculate elo
|
2021-06-16 09:53:50 -04:00
|
|
|
|
var attackerEloDifference = Math.Log(Math.Max(1, victimStats.EloRating)) -
|
2021-03-22 12:09:25 -04:00
|
|
|
|
Math.Log(Math.Max(1, attackerStats.EloRating));
|
2021-06-16 09:53:50 -04:00
|
|
|
|
var winPercentage = 1.0 / (1 + Math.Pow(10, attackerEloDifference / Math.E));
|
2019-10-18 14:39:21 -04:00
|
|
|
|
|
|
|
|
|
attackerStats.EloRating += 6.0 * (1 - winPercentage);
|
|
|
|
|
victimStats.EloRating -= 6.0 * (1 - winPercentage);
|
|
|
|
|
|
|
|
|
|
attackerStats.EloRating = Math.Max(0, Math.Round(attackerStats.EloRating, 2));
|
|
|
|
|
victimStats.EloRating = Math.Max(0, Math.Round(victimStats.EloRating, 2));
|
2018-05-16 00:57:37 -04:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
// update after calculation
|
2021-03-22 12:09:25 -04:00
|
|
|
|
attackerStats.TimePlayed += (int) (DateTime.UtcNow - attackerStats.LastActive).TotalSeconds;
|
|
|
|
|
victimStats.TimePlayed += (int) (DateTime.UtcNow - victimStats.LastActive).TotalSeconds;
|
2018-09-16 16:34:16 -04:00
|
|
|
|
attackerStats.LastActive = DateTime.UtcNow;
|
|
|
|
|
victimStats.LastActive = DateTime.UtcNow;
|
|
|
|
|
}
|
2018-02-08 02:23:45 -05:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Update the client stats (skill etc)
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="clientStats">Client statistics</param>
|
|
|
|
|
/// <returns></returns>
|
2021-06-30 10:57:07 -04:00
|
|
|
|
private EFClientStatistics UpdateStats(EFClientStatistics clientStats, EFClient client)
|
2018-09-16 16:34:16 -04:00
|
|
|
|
{
|
|
|
|
|
// prevent NaN or inactive time lowering SPM
|
|
|
|
|
if ((DateTime.UtcNow - clientStats.LastStatCalculation).TotalSeconds / 60.0 < 0.01 ||
|
|
|
|
|
(DateTime.UtcNow - clientStats.LastActive).TotalSeconds / 60.0 > 3 ||
|
|
|
|
|
clientStats.SessionScore == 0)
|
2018-04-28 01:22:18 -04:00
|
|
|
|
{
|
2018-09-16 16:34:16 -04:00
|
|
|
|
// prevents idle time counting
|
|
|
|
|
clientStats.LastStatCalculation = DateTime.UtcNow;
|
|
|
|
|
return clientStats;
|
2018-04-28 01:22:18 -04:00
|
|
|
|
}
|
2018-02-08 02:23:45 -05:00
|
|
|
|
|
2021-06-16 09:53:50 -04:00
|
|
|
|
var timeSinceLastCalc = (DateTime.UtcNow - clientStats.LastStatCalculation).TotalSeconds / 60.0;
|
2018-04-10 20:25:44 -04:00
|
|
|
|
|
2021-06-16 09:53:50 -04:00
|
|
|
|
var scoreDifference = 0;
|
2018-09-16 16:34:16 -04:00
|
|
|
|
// this means they've been tking or suicide and is the only time they can have a negative SPM
|
|
|
|
|
if (clientStats.RoundScore < 0)
|
2018-05-08 00:58:46 -04:00
|
|
|
|
{
|
2018-09-16 16:34:16 -04:00
|
|
|
|
scoreDifference = clientStats.RoundScore + clientStats.LastScore;
|
2018-05-08 00:58:46 -04:00
|
|
|
|
}
|
2018-05-24 15:48:57 -04:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
else if (clientStats.RoundScore > 0 && clientStats.LastScore < clientStats.RoundScore)
|
2018-05-08 00:58:46 -04:00
|
|
|
|
{
|
2018-09-16 16:34:16 -04:00
|
|
|
|
scoreDifference = clientStats.RoundScore - clientStats.LastScore;
|
|
|
|
|
}
|
2018-06-05 17:31:36 -04:00
|
|
|
|
|
2021-06-16 09:53:50 -04:00
|
|
|
|
var killSpm = scoreDifference / timeSinceLastCalc;
|
|
|
|
|
var spmMultiplier = 2.934 *
|
2021-03-22 12:09:25 -04:00
|
|
|
|
Math.Pow(
|
|
|
|
|
_servers[clientStats.ServerId]
|
|
|
|
|
.TeamCount((IW4Info.Team) clientStats.Team == IW4Info.Team.Allies
|
|
|
|
|
? IW4Info.Team.Axis
|
|
|
|
|
: IW4Info.Team.Allies), -0.454);
|
2021-06-16 09:53:50 -04:00
|
|
|
|
killSpm *= Math.Max(1, spmMultiplier);
|
2018-02-08 02:23:45 -05:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
// update this for ac tracking
|
2021-06-30 10:57:07 -04:00
|
|
|
|
clientStats.SessionSPM = clientStats.SessionScore / Math.Max(1, client.ConnectionLength / 60.0);
|
2018-02-08 02:23:45 -05:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
// calculate how much the KDR should weigh
|
|
|
|
|
// 1.637 is a Eddie-Generated number that weights the KDR nicely
|
2021-03-22 12:09:25 -04:00
|
|
|
|
double currentKDR = clientStats.SessionDeaths == 0
|
|
|
|
|
? clientStats.SessionKills
|
|
|
|
|
: clientStats.SessionKills / clientStats.SessionDeaths;
|
2018-09-16 16:34:16 -04:00
|
|
|
|
double alpha = Math.Sqrt(2) / Math.Min(600, Math.Max(clientStats.Kills + clientStats.Deaths, 1));
|
|
|
|
|
clientStats.RollingWeightedKDR = (alpha * currentKDR) + (1.0 - alpha) * clientStats.KDR;
|
|
|
|
|
double KDRWeight = Math.Round(Math.Pow(clientStats.RollingWeightedKDR, 1.637 / Math.E), 3);
|
2018-02-08 02:23:45 -05:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
// calculate the weight of the new play time against last 10 hours of gameplay
|
2021-03-22 12:09:25 -04:00
|
|
|
|
int totalPlayTime = (clientStats.TimePlayed == 0)
|
|
|
|
|
? (int) (DateTime.UtcNow - clientStats.LastActive).TotalSeconds
|
|
|
|
|
: clientStats.TimePlayed + (int) (DateTime.UtcNow - clientStats.LastActive).TotalSeconds;
|
2018-04-29 16:44:04 -04:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
double SPMAgainstPlayWeight = timeSinceLastCalc / Math.Min(600, (totalPlayTime / 60.0));
|
2018-04-29 16:44:04 -04:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
// calculate the new weight against average times the weight against play time
|
2021-06-16 09:53:50 -04:00
|
|
|
|
clientStats.SPM = (killSpm * SPMAgainstPlayWeight) + (clientStats.SPM * (1 - SPMAgainstPlayWeight));
|
2018-02-08 02:23:45 -05:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
if (clientStats.SPM < 0)
|
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
_log.LogWarning("clientStats SPM < 0 {scoreDifference} {@clientStats}", scoreDifference, clientStats);
|
2018-09-16 16:34:16 -04:00
|
|
|
|
clientStats.SPM = 0;
|
|
|
|
|
}
|
2018-04-11 18:24:21 -04:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
clientStats.SPM = Math.Round(clientStats.SPM, 3);
|
|
|
|
|
clientStats.Skill = Math.Round((clientStats.SPM * KDRWeight), 3);
|
|
|
|
|
|
|
|
|
|
// fixme: how does this happen?
|
|
|
|
|
if (double.IsNaN(clientStats.SPM) || double.IsNaN(clientStats.Skill))
|
|
|
|
|
{
|
2021-03-22 12:09:25 -04:00
|
|
|
|
_log.LogWarning("clientStats SPM/Skill NaN {@killInfo}",
|
2021-06-16 09:53:50 -04:00
|
|
|
|
new {killSPM = killSpm, KDRWeight, totalPlayTime, SPMAgainstPlayWeight, clientStats, scoreDifference});
|
2018-09-16 16:34:16 -04:00
|
|
|
|
clientStats.SPM = 0;
|
|
|
|
|
clientStats.Skill = 0;
|
|
|
|
|
}
|
2018-02-09 02:21:25 -05:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
clientStats.LastStatCalculation = DateTime.UtcNow;
|
|
|
|
|
//clientStats.LastScore = clientStats.SessionScore;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
clientStats.UpdatedAt = DateTime.UtcNow;
|
2018-09-11 15:28:37 -04:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
return clientStats;
|
2018-09-11 15:28:37 -04:00
|
|
|
|
}
|
2018-02-10 01:26:38 -05:00
|
|
|
|
|
2018-11-27 19:31:48 -05:00
|
|
|
|
public EFServerStatistics InitializeServerStats(long serverId)
|
2018-09-11 15:28:37 -04:00
|
|
|
|
{
|
2018-10-07 22:34:30 -04:00
|
|
|
|
EFServerStatistics serverStats;
|
2018-02-09 02:21:25 -05:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
using var ctx = _contextFactory.CreateContext(enableTracking: false);
|
|
|
|
|
var serverStatsSet = ctx.Set<EFServerStatistics>();
|
|
|
|
|
serverStats = serverStatsSet.FirstOrDefault(s => s.ServerId == serverId);
|
2018-09-11 15:28:37 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
if (serverStats == null)
|
|
|
|
|
{
|
|
|
|
|
_log.LogDebug("Initializing server stats for {serverId}", serverId);
|
|
|
|
|
// server stats have never been generated before
|
|
|
|
|
serverStats = new EFServerStatistics()
|
2018-10-07 22:34:30 -04:00
|
|
|
|
{
|
2020-11-29 17:01:52 -05:00
|
|
|
|
ServerId = serverId,
|
|
|
|
|
TotalKills = 0,
|
|
|
|
|
TotalPlayTime = 0,
|
|
|
|
|
};
|
2018-09-11 15:28:37 -04:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
serverStats = serverStatsSet.Add(serverStats).Entity;
|
|
|
|
|
ctx.SaveChanges();
|
2018-09-16 16:34:16 -04:00
|
|
|
|
}
|
2018-10-07 22:34:30 -04:00
|
|
|
|
|
|
|
|
|
return serverStats;
|
2018-09-16 16:34:16 -04:00
|
|
|
|
}
|
2018-02-09 02:21:25 -05:00
|
|
|
|
|
2019-10-18 14:39:21 -04:00
|
|
|
|
public void ResetKillstreaks(Server sv)
|
2018-03-06 02:22:19 -05:00
|
|
|
|
{
|
2020-09-30 18:15:47 -04:00
|
|
|
|
foreach (var session in sv.GetClientsAsList()
|
|
|
|
|
.Select(_client => new
|
|
|
|
|
{
|
|
|
|
|
stat = _client.GetAdditionalProperty<EFClientStatistics>(CLIENT_STATS_KEY),
|
|
|
|
|
detection = _client.GetAdditionalProperty<Detection>(CLIENT_DETECTIONS_KEY)
|
|
|
|
|
}))
|
2018-10-07 22:34:30 -04:00
|
|
|
|
{
|
2020-09-30 18:15:47 -04:00
|
|
|
|
session.stat?.StartNewSession();
|
|
|
|
|
session.detection?.OnMapChange();
|
2022-01-22 13:49:12 -05:00
|
|
|
|
session.stat?.MatchData?.StartNewMatch();
|
2018-10-07 22:34:30 -04:00
|
|
|
|
}
|
2018-03-06 02:22:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-18 14:39:21 -04:00
|
|
|
|
public void ResetStats(EFClient client)
|
2018-03-06 02:22:19 -05:00
|
|
|
|
{
|
2019-10-18 14:39:21 -04:00
|
|
|
|
var stats = client.GetAdditionalProperty<EFClientStatistics>(CLIENT_STATS_KEY);
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2020-12-16 14:11:30 -05:00
|
|
|
|
// the cached stats have not been loaded yet
|
|
|
|
|
if (stats == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-03-22 12:09:25 -04:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
stats.Kills = 0;
|
|
|
|
|
stats.Deaths = 0;
|
|
|
|
|
stats.SPM = 0;
|
|
|
|
|
stats.Skill = 0;
|
|
|
|
|
stats.TimePlayed = 0;
|
|
|
|
|
stats.EloRating = 200;
|
2018-03-06 02:22:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-18 10:08:24 -05:00
|
|
|
|
public async Task AddMessageAsync(int clientId, long serverId, bool sentIngame, string message)
|
2018-09-11 15:28:37 -04:00
|
|
|
|
{
|
2018-09-16 16:34:16 -04:00
|
|
|
|
// the web users can have no account
|
|
|
|
|
if (clientId < 1)
|
2018-11-05 22:01:29 -05:00
|
|
|
|
{
|
2018-09-16 16:34:16 -04:00
|
|
|
|
return;
|
2018-11-05 22:01:29 -05:00
|
|
|
|
}
|
2018-09-16 16:34:16 -04:00
|
|
|
|
|
2020-11-18 10:08:24 -05:00
|
|
|
|
await using var ctx = _contextFactory.CreateContext(enableTracking: false);
|
|
|
|
|
ctx.Set<EFClientMessage>().Add(new EFClientMessage()
|
2018-02-10 01:26:38 -05:00
|
|
|
|
{
|
2020-11-18 10:08:24 -05:00
|
|
|
|
ClientId = clientId,
|
|
|
|
|
Message = message,
|
|
|
|
|
ServerId = serverId,
|
|
|
|
|
TimeSent = DateTime.UtcNow,
|
|
|
|
|
SentIngame = sentIngame
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await ctx.SaveChangesAsync();
|
2018-02-09 02:21:25 -05:00
|
|
|
|
}
|
2018-09-11 15:28:37 -04:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
public async Task Sync(Server sv)
|
|
|
|
|
{
|
2019-08-23 19:34:31 -04:00
|
|
|
|
long serverId = GetIdForServer(sv);
|
2018-05-24 15:48:57 -04:00
|
|
|
|
|
2019-11-15 15:50:20 -05:00
|
|
|
|
var waiter = _servers[serverId].OnSaving;
|
|
|
|
|
try
|
2018-10-07 22:34:30 -04:00
|
|
|
|
{
|
2019-11-15 15:50:20 -05:00
|
|
|
|
await waiter.WaitAsync();
|
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
await using var ctx = _contextFactory.CreateContext();
|
|
|
|
|
var serverStatsSet = ctx.Set<EFServerStatistics>();
|
|
|
|
|
serverStatsSet.Update(_servers[serverId].ServerStatistics);
|
|
|
|
|
await ctx.SaveChangesAsync();
|
2019-11-15 15:50:20 -05:00
|
|
|
|
|
|
|
|
|
foreach (var stats in sv.GetClientsAsList()
|
|
|
|
|
.Select(_client => _client.GetAdditionalProperty<EFClientStatistics>(CLIENT_STATS_KEY))
|
|
|
|
|
.Where(_stats => _stats != null))
|
|
|
|
|
{
|
|
|
|
|
await SaveClientStats(stats);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await SaveHitCache(serverId);
|
2019-09-09 18:40:04 -04:00
|
|
|
|
}
|
2018-11-27 19:31:48 -05:00
|
|
|
|
|
2019-11-15 15:50:20 -05:00
|
|
|
|
catch (Exception e)
|
2019-09-09 18:40:04 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
_log.LogError(e, "There was a problem syncing server stats");
|
2018-10-07 22:34:30 -04:00
|
|
|
|
}
|
2019-09-09 18:40:04 -04:00
|
|
|
|
|
2019-11-15 15:50:20 -05:00
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
if (waiter.CurrentCount == 0)
|
|
|
|
|
{
|
|
|
|
|
waiter.Release(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-16 16:34:16 -04:00
|
|
|
|
}
|
2018-09-11 15:28:37 -04:00
|
|
|
|
|
2018-11-27 19:31:48 -05:00
|
|
|
|
public void SetTeamBased(long serverId, bool isTeamBased)
|
2018-05-24 15:48:57 -04:00
|
|
|
|
{
|
2019-05-08 21:34:17 -04:00
|
|
|
|
_servers[serverId].IsTeamBased = isTeamBased;
|
2018-09-16 16:34:16 -04:00
|
|
|
|
}
|
2018-11-27 19:31:48 -05:00
|
|
|
|
|
2019-08-23 19:34:31 -04:00
|
|
|
|
public static long GetIdForServer(Server server)
|
2018-11-27 19:31:48 -05:00
|
|
|
|
{
|
2019-05-31 11:17:01 -04:00
|
|
|
|
if ($"{server.IP}:{server.Port.ToString()}" == "66.150.121.184:28965")
|
2019-05-29 17:55:35 -04:00
|
|
|
|
{
|
|
|
|
|
return 886229536;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-21 16:30:42 -04:00
|
|
|
|
// todo: this is not stable and will need to be migrated again...
|
2019-05-31 11:17:01 -04:00
|
|
|
|
long id = HashCode.Combine(server.IP, server.Port);
|
2019-05-17 10:02:09 -04:00
|
|
|
|
id = id < 0 ? Math.Abs(id) : id;
|
|
|
|
|
long? serverId;
|
2018-11-27 19:31:48 -05:00
|
|
|
|
|
2019-08-23 19:34:31 -04:00
|
|
|
|
serverId = serverModels.FirstOrDefault(_server => _server.ServerId == server.EndPoint ||
|
2021-03-22 12:09:25 -04:00
|
|
|
|
_server.EndPoint == server.ToString() ||
|
|
|
|
|
_server.ServerId == id)?.ServerId;
|
2018-11-27 19:31:48 -05:00
|
|
|
|
|
2019-05-17 10:02:09 -04:00
|
|
|
|
if (!serverId.HasValue)
|
2018-11-27 19:31:48 -05:00
|
|
|
|
{
|
2019-05-17 10:02:09 -04:00
|
|
|
|
return id;
|
2018-11-27 19:31:48 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-17 10:02:09 -04:00
|
|
|
|
return serverId.Value;
|
2018-11-27 19:31:48 -05:00
|
|
|
|
}
|
2018-09-11 15:28:37 -04:00
|
|
|
|
}
|
2021-12-26 18:14:06 -05:00
|
|
|
|
}
|