2018-11-05 22:01:29 -05:00
|
|
|
|
using IW4MAdmin.Plugins.Stats.Cheat;
|
2018-04-09 23:33:42 -04:00
|
|
|
|
using IW4MAdmin.Plugins.Stats.Models;
|
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.Database;
|
2018-05-30 21:50:20 -04:00
|
|
|
|
using SharedLibraryCore.Database.Models;
|
2018-11-05 22:01:29 -05:00
|
|
|
|
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;
|
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-05-08 21:34:17 -04:00
|
|
|
|
private readonly ConcurrentDictionary<long, ServerStats> _servers;
|
|
|
|
|
private readonly ILogger _log;
|
2019-08-23 19:34:31 -04:00
|
|
|
|
private static List<EFServer> serverModels;
|
2018-09-11 15:28:37 -04:00
|
|
|
|
private readonly SemaphoreSlim OnProcessingPenalty;
|
2018-10-07 22:34:30 -04:00
|
|
|
|
private readonly SemaphoreSlim OnProcessingSensitive;
|
2019-08-23 19:34:31 -04:00
|
|
|
|
private readonly List<EFClientKill> _hitCache;
|
2018-09-08 18:29:30 -04:00
|
|
|
|
|
2018-02-07 00:19:06 -05:00
|
|
|
|
public StatManager(IManager mgr)
|
|
|
|
|
{
|
2019-05-08 21:34:17 -04:00
|
|
|
|
_servers = new ConcurrentDictionary<long, ServerStats>();
|
2019-08-23 19:34:31 -04:00
|
|
|
|
_hitCache = new List<EFClientKill>();
|
2019-05-08 21:34:17 -04:00
|
|
|
|
_log = mgr.GetLogger(0);
|
2018-09-11 15:28:37 -04:00
|
|
|
|
OnProcessingPenalty = new SemaphoreSlim(1, 1);
|
2018-10-07 22:34:30 -04:00
|
|
|
|
OnProcessingSensitive = new SemaphoreSlim(1, 1);
|
2018-02-07 00:19:06 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-23 19:34:31 -04:00
|
|
|
|
private void SetupServerIds()
|
|
|
|
|
{
|
|
|
|
|
using (var ctx = new DatabaseContext(disableTracking: true))
|
|
|
|
|
{
|
|
|
|
|
serverModels = ctx.Set<EFServer>().ToList();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-27 19:31:48 -05:00
|
|
|
|
public EFClientStatistics GetClientStats(int clientId, long serverId)
|
2018-11-05 22:01:29 -05:00
|
|
|
|
{
|
2019-05-08 21:34:17 -04:00
|
|
|
|
return _servers[serverId].PlayerStats[clientId];
|
2018-11-05 22:01:29 -05:00
|
|
|
|
}
|
2018-05-28 21:30:31 -04:00
|
|
|
|
|
2018-11-27 19:31:48 -05:00
|
|
|
|
public static 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 &&
|
2018-09-11 15:28:37 -04:00
|
|
|
|
r.When > fifteenDaysAgo &&
|
2018-11-05 22:01:29 -05:00
|
|
|
|
r.RatingHistory.Client.Level != EFClient.Permission.Banned &&
|
2018-09-07 23:29:42 -04:00
|
|
|
|
r.Newest &&
|
|
|
|
|
r.ActivityAmount >= Plugin.Config.Configuration().TopPlayersMinPlayTime;
|
|
|
|
|
}
|
|
|
|
|
|
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>
|
2018-10-15 20:51:04 -04:00
|
|
|
|
public static async Task<int> GetClientOverallRanking(int clientId)
|
2018-05-28 21:30:31 -04:00
|
|
|
|
{
|
2018-09-04 22:07:34 -04:00
|
|
|
|
using (var context = new DatabaseContext(true))
|
2018-05-28 21:30:31 -04:00
|
|
|
|
{
|
2018-09-04 22:07:34 -04:00
|
|
|
|
var clientPerformance = await context.Set<EFRating>()
|
|
|
|
|
.Where(r => r.RatingHistory.ClientId == clientId)
|
|
|
|
|
.Where(r => r.ServerId == null)
|
|
|
|
|
.Where(r => r.Newest)
|
|
|
|
|
.Select(r => r.Performance)
|
|
|
|
|
.FirstOrDefaultAsync();
|
|
|
|
|
|
2018-09-08 18:29:30 -04:00
|
|
|
|
if (clientPerformance != 0)
|
|
|
|
|
{
|
|
|
|
|
var iqClientRanking = context.Set<EFRating>()
|
|
|
|
|
.Where(r => r.RatingHistory.ClientId != clientId)
|
|
|
|
|
.Where(r => r.Performance > clientPerformance)
|
|
|
|
|
.Where(GetRankingFunc());
|
|
|
|
|
|
|
|
|
|
return await iqClientRanking.CountAsync() + 1;
|
|
|
|
|
}
|
2018-09-07 23:29:42 -04:00
|
|
|
|
|
2018-09-08 18:29:30 -04:00
|
|
|
|
return 0;
|
2018-09-04 22:07:34 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-28 21:30:31 -04:00
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
|
using (var context = new DatabaseContext(true))
|
|
|
|
|
{
|
2018-09-07 23:29:42 -04:00
|
|
|
|
// setup the query for the clients within the given rating range
|
2018-05-31 20:17:52 -04:00
|
|
|
|
var iqClientRatings = (from rating in context.Set<EFRating>()
|
2019-02-26 22:25:27 -05:00
|
|
|
|
.Where(GetRankingFunc(serverId))
|
2018-05-31 20:17:52 -04:00
|
|
|
|
select new
|
|
|
|
|
{
|
|
|
|
|
rating.RatingHistory.ClientId,
|
|
|
|
|
rating.RatingHistory.Client.CurrentAlias.Name,
|
|
|
|
|
rating.RatingHistory.Client.LastConnection,
|
|
|
|
|
rating.Performance,
|
2018-09-11 15:28:37 -04:00
|
|
|
|
})
|
|
|
|
|
.OrderByDescending(c => c.Performance)
|
2018-09-07 23:29:42 -04:00
|
|
|
|
.Skip(start)
|
|
|
|
|
.Take(count);
|
|
|
|
|
#if DEBUG == true
|
2018-09-11 15:28:37 -04:00
|
|
|
|
var clientRatingsSql = iqClientRatings.ToSql();
|
2018-09-07 23:29:42 -04:00
|
|
|
|
#endif
|
|
|
|
|
// materialized list
|
2018-05-31 20:17:52 -04:00
|
|
|
|
var clientRatings = await iqClientRatings.ToListAsync();
|
|
|
|
|
|
2018-09-11 15:28:37 -04:00
|
|
|
|
// get all the unique client ids that are in the top stats
|
2018-09-04 13:40:29 -04:00
|
|
|
|
var clientIds = clientRatings
|
|
|
|
|
.GroupBy(r => r.ClientId)
|
|
|
|
|
.Select(r => r.First().ClientId)
|
|
|
|
|
.ToList();
|
2018-05-28 21:30:31 -04:00
|
|
|
|
|
2018-09-11 15:28:37 -04:00
|
|
|
|
var iqRatingInfo = from rating in context.Set<EFRating>()
|
|
|
|
|
where clientIds.Contains(rating.RatingHistory.ClientId)
|
2019-02-26 22:25:27 -05:00
|
|
|
|
where rating.ServerId == serverId
|
2018-09-11 15:28:37 -04:00
|
|
|
|
select new
|
|
|
|
|
{
|
|
|
|
|
rating.Ranking,
|
|
|
|
|
rating.Performance,
|
|
|
|
|
rating.RatingHistory.ClientId,
|
|
|
|
|
rating.When
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#if DEBUG == true
|
|
|
|
|
var ratingQuery = iqRatingInfo.ToSql();
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
var ratingInfo = (await iqRatingInfo.ToListAsync())
|
|
|
|
|
.GroupBy(r => r.ClientId)
|
|
|
|
|
.Select(grp => new
|
|
|
|
|
{
|
|
|
|
|
grp.Key,
|
|
|
|
|
Ratings = grp.Select(r => new { r.Performance, r.Ranking, r.When })
|
|
|
|
|
});
|
|
|
|
|
|
2018-05-31 20:17:52 -04:00
|
|
|
|
var iqStatsInfo = (from stat in context.Set<EFClientStatistics>()
|
|
|
|
|
where clientIds.Contains(stat.ClientId)
|
2019-05-08 21:34:17 -04:00
|
|
|
|
where stat.Kills > 0 || stat.Deaths > 0
|
2018-09-07 23:29:42 -04:00
|
|
|
|
group stat by stat.ClientId into s
|
2018-05-31 20:17:52 -04:00
|
|
|
|
select new
|
|
|
|
|
{
|
2018-09-07 23:29:42 -04:00
|
|
|
|
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)
|
2018-05-31 20:17:52 -04:00
|
|
|
|
});
|
2018-09-11 15:28:37 -04:00
|
|
|
|
|
2018-09-07 23:29:42 -04:00
|
|
|
|
#if DEBUG == true
|
2018-10-06 16:31:05 -04:00
|
|
|
|
var statsInfoSql = iqStatsInfo.ToSql();
|
2018-09-07 23:29:42 -04:00
|
|
|
|
#endif
|
2018-09-16 16:34:16 -04:00
|
|
|
|
var topPlayers = await iqStatsInfo.ToListAsync();
|
2018-05-28 21:30:31 -04:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
var clientRatingsDict = clientRatings.ToDictionary(r => r.ClientId);
|
|
|
|
|
var finished = topPlayers.Select(s => new TopStatsInfo()
|
|
|
|
|
{
|
|
|
|
|
ClientId = s.ClientId,
|
2019-02-27 21:13:15 -05:00
|
|
|
|
Id = (int?)serverId ?? 0,
|
2018-09-16 16:34:16 -04:00
|
|
|
|
Deaths = s.Deaths,
|
|
|
|
|
Kills = s.Kills,
|
|
|
|
|
KDR = Math.Round(s.KDR, 2),
|
|
|
|
|
LastSeen = Utilities.GetTimePassed(clientRatingsDict[s.ClientId].LastConnection, false),
|
|
|
|
|
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"),
|
|
|
|
|
})
|
|
|
|
|
.OrderByDescending(r => r.Performance)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
// set the ranking numerically
|
|
|
|
|
int i = start + 1;
|
|
|
|
|
foreach (var stat in finished)
|
|
|
|
|
{
|
|
|
|
|
stat.Ranking = i;
|
|
|
|
|
i++;
|
|
|
|
|
}
|
2018-09-11 15:28:37 -04:00
|
|
|
|
|
2018-09-16 16:34:16 -04: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
|
|
|
|
|
2018-10-07 22:34:30 -04:00
|
|
|
|
using (var ctx = new DatabaseContext(disableTracking: true))
|
2018-02-07 00:19:06 -05:00
|
|
|
|
{
|
2018-10-07 22:34:30 -04:00
|
|
|
|
var serverSet = ctx.Set<EFServer>();
|
|
|
|
|
// get the server from the database if it exists, otherwise create and insert a new one
|
2018-11-27 19:31:48 -05:00
|
|
|
|
server = serverSet.FirstOrDefault(s => s.ServerId == serverId);
|
2018-02-09 02:21:25 -05:00
|
|
|
|
|
2018-11-27 19:31:48 -05:00
|
|
|
|
// the server might be using legacy server id
|
|
|
|
|
if (server == null)
|
|
|
|
|
{
|
|
|
|
|
server = serverSet.FirstOrDefault(s => s.EndPoint == sv.ToString());
|
|
|
|
|
|
|
|
|
|
if (server != null)
|
|
|
|
|
{
|
|
|
|
|
// this provides a way to identify legacy server entries
|
|
|
|
|
server.EndPoint = sv.ToString();
|
|
|
|
|
ctx.Update(server);
|
|
|
|
|
ctx.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// server has never been added before
|
2018-10-07 22:34:30 -04:00
|
|
|
|
if (server == null)
|
|
|
|
|
{
|
|
|
|
|
server = new EFServer()
|
|
|
|
|
{
|
2019-05-31 11:17:01 -04:00
|
|
|
|
Port = sv.Port,
|
2018-11-27 19:31:48 -05:00
|
|
|
|
EndPoint = sv.ToString(),
|
2019-04-23 18:27:20 -04:00
|
|
|
|
ServerId = serverId,
|
|
|
|
|
GameName = sv.GameName
|
2018-10-07 22:34:30 -04:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
server = serverSet.Add(server).Entity;
|
|
|
|
|
// this doesn't need to be async as it's during initialization
|
|
|
|
|
ctx.SaveChanges();
|
|
|
|
|
}
|
2019-04-23 18:27:20 -04:00
|
|
|
|
|
|
|
|
|
// we want to set the gamename up if it's never been set, or it changed
|
|
|
|
|
else if (!server.GameName.HasValue || server.GameName.HasValue && server.GameName.Value != sv.GameName)
|
|
|
|
|
{
|
|
|
|
|
server.GameName = sv.GameName;
|
|
|
|
|
ctx.Entry(server).Property(_prop => _prop.GameName).IsModified = true;
|
|
|
|
|
ctx.SaveChanges();
|
|
|
|
|
}
|
2018-09-16 16:34:16 -04:00
|
|
|
|
}
|
2018-02-07 00:19:06 -05:00
|
|
|
|
|
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-05-08 21:34:17 -04:00
|
|
|
|
_servers.TryAdd(serverId, new ServerStats(server, serverStats)
|
2018-09-16 16:34:16 -04:00
|
|
|
|
{
|
|
|
|
|
IsTeamBased = sv.Gametype != "dm"
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (Exception e)
|
2018-02-07 00:19:06 -05:00
|
|
|
|
{
|
2019-05-08 21:34:17 -04:00
|
|
|
|
_log.WriteError($"{Utilities.CurrentLocalization.LocalizationIndex["PLUGIN_STATS_ERROR_ADD"]} - {e.Message}");
|
|
|
|
|
_log.WriteDebug(e.GetExceptionInfo());
|
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
|
|
|
|
{
|
2018-10-07 22:34:30 -04:00
|
|
|
|
await OnProcessingSensitive.WaitAsync();
|
2018-03-06 02:22:19 -05:00
|
|
|
|
|
2018-10-07 22:34:30 -04:00
|
|
|
|
try
|
2018-09-16 16:34:16 -04:00
|
|
|
|
{
|
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
|
|
|
|
{
|
2019-05-08 21:34:17 -04:00
|
|
|
|
_log.WriteError($"[Stats::AddPlayer] Server with id {serverId} could not be found");
|
2018-10-07 22:34:30 -04:00
|
|
|
|
return null;
|
|
|
|
|
}
|
2018-05-03 01:25:49 -04:00
|
|
|
|
|
2019-05-08 21:34:17 -04:00
|
|
|
|
var playerStats = _servers[serverId].PlayerStats;
|
|
|
|
|
var detectionStats = _servers[serverId].PlayerDetections;
|
2018-10-05 23:10:39 -04:00
|
|
|
|
|
2018-10-07 22:34:30 -04:00
|
|
|
|
if (playerStats.ContainsKey(pl.ClientId))
|
2018-10-05 23:10:39 -04:00
|
|
|
|
{
|
2019-05-08 21:34:17 -04:00
|
|
|
|
_log.WriteWarning($"Duplicate ClientId in stats {pl.ClientId}");
|
2018-10-07 22:34:30 -04:00
|
|
|
|
return playerStats[pl.ClientId];
|
2018-10-05 23:10:39 -04:00
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
using (var ctx = new DatabaseContext(disableTracking: true))
|
2018-10-05 23:10:39 -04:00
|
|
|
|
{
|
2018-10-07 22:34:30 -04:00
|
|
|
|
var clientStatsSet = ctx.Set<EFClientStatistics>();
|
|
|
|
|
clientStats = clientStatsSet
|
|
|
|
|
.Include(cl => cl.HitLocations)
|
|
|
|
|
.FirstOrDefault(c => c.ClientId == pl.ClientId && c.ServerId == serverId);
|
2018-10-05 23:10:39 -04:00
|
|
|
|
|
2018-10-07 22:34:30 -04:00
|
|
|
|
if (clientStats == null)
|
2018-10-05 23:10:39 -04:00
|
|
|
|
{
|
2018-10-07 22:34:30 -04:00
|
|
|
|
clientStats = new EFClientStatistics()
|
|
|
|
|
{
|
|
|
|
|
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>().Select(hl => new EFHitLocationCount()
|
|
|
|
|
{
|
|
|
|
|
Active = true,
|
|
|
|
|
HitCount = 0,
|
|
|
|
|
Location = hl
|
|
|
|
|
}).ToList()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// insert if they've not been added
|
|
|
|
|
clientStats = clientStatsSet.Add(clientStats).Entity;
|
|
|
|
|
await ctx.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
if (!playerStats.TryAdd(clientStats.ClientId, clientStats))
|
|
|
|
|
{
|
2019-05-08 21:34:17 -04:00
|
|
|
|
_log.WriteWarning("Adding new client to stats failed");
|
2018-10-07 22:34:30 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (!playerStats.TryAdd(clientStats.ClientId, clientStats))
|
|
|
|
|
{
|
2019-05-08 21:34:17 -04:00
|
|
|
|
_log.WriteWarning("Adding pre-existing client to stats failed");
|
2018-10-07 22:34:30 -04: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,
|
|
|
|
|
Location = hl
|
|
|
|
|
})
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
ctx.Update(clientStats);
|
|
|
|
|
await ctx.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// for stats before rating
|
|
|
|
|
if (clientStats.EloRating == 0.0)
|
|
|
|
|
{
|
|
|
|
|
clientStats.EloRating = clientStats.Skill;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (clientStats.RollingWeightedKDR == 0)
|
|
|
|
|
{
|
|
|
|
|
clientStats.RollingWeightedKDR = clientStats.KDR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// set these on connecting
|
|
|
|
|
clientStats.LastActive = DateTime.UtcNow;
|
|
|
|
|
clientStats.LastStatCalculation = DateTime.UtcNow;
|
|
|
|
|
clientStats.SessionScore = pl.Score;
|
|
|
|
|
clientStats.LastScore = pl.Score;
|
|
|
|
|
|
2019-08-23 19:34:31 -04:00
|
|
|
|
if (!detectionStats.TryAdd(pl.ClientId, new Detection(_log, clientStats)))
|
2018-10-07 22:34:30 -04:00
|
|
|
|
{
|
2019-05-08 21:34:17 -04:00
|
|
|
|
_log.WriteWarning("Could not add client to detection");
|
2018-10-07 22:34:30 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-25 21:00:36 -05:00
|
|
|
|
pl.CurrentServer.Logger.WriteInfo($"Adding {pl} to stats");
|
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
|
|
|
|
|
2018-10-07 22:34:30 -04:00
|
|
|
|
catch (Exception ex)
|
2018-09-16 16:34:16 -04:00
|
|
|
|
{
|
2019-05-08 21:34:17 -04:00
|
|
|
|
_log.WriteWarning("Could not add client to stats");
|
|
|
|
|
_log.WriteDebug(ex.GetExceptionInfo());
|
2018-10-07 22:34:30 -04:00
|
|
|
|
}
|
2018-02-26 23:24:19 -05:00
|
|
|
|
|
2018-10-07 22:34:30 -04:00
|
|
|
|
finally
|
2018-10-05 23:10:39 -04:00
|
|
|
|
{
|
2018-10-07 22:34:30 -04:00
|
|
|
|
OnProcessingSensitive.Release(1);
|
2018-10-05 23:10:39 -04:00
|
|
|
|
}
|
2018-09-11 15:28:37 -04:00
|
|
|
|
|
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
|
|
|
|
{
|
2018-11-27 19:31:48 -05:00
|
|
|
|
pl.CurrentServer.Logger.WriteInfo($"Removing {pl} from stats");
|
2018-02-10 01:26:38 -05:00
|
|
|
|
|
2019-08-23 19:34:31 -04:00
|
|
|
|
long serverId = GetIdForServer(pl.CurrentServer);
|
2019-05-08 21:34:17 -04:00
|
|
|
|
var playerStats = _servers[serverId].PlayerStats;
|
|
|
|
|
var detectionStats = _servers[serverId].PlayerDetections;
|
|
|
|
|
var serverStats = _servers[serverId].ServerStatistics;
|
2018-03-06 02:22:19 -05:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
if (!playerStats.ContainsKey(pl.ClientId))
|
|
|
|
|
{
|
2018-11-27 19:31:48 -05:00
|
|
|
|
pl.CurrentServer.Logger.WriteWarning($"Client disconnecting not in stats {pl}");
|
2018-09-16 16:34:16 -04:00
|
|
|
|
// remove the client from the stats dictionary as they're leaving
|
2019-08-23 19:34:31 -04:00
|
|
|
|
playerStats.TryRemove(pl.ClientId, out _);
|
|
|
|
|
detectionStats.TryRemove(pl.ClientId, out _);
|
2018-09-16 16:34:16 -04:00
|
|
|
|
return;
|
|
|
|
|
}
|
2018-09-11 15:28:37 -04:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
// get individual client's stats
|
|
|
|
|
var clientStats = playerStats[pl.ClientId];
|
2018-04-26 20:19:42 -04:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
// remove the client from the stats dictionary as they're leaving
|
2019-08-23 19:34:31 -04:00
|
|
|
|
playerStats.TryRemove(pl.ClientId, out _);
|
|
|
|
|
detectionStats.TryRemove(pl.ClientId, out _);
|
2018-05-24 15:48:57 -04:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
// sync their stats before they leave
|
|
|
|
|
clientStats = UpdateStats(clientStats);
|
2018-10-07 22:34:30 -04:00
|
|
|
|
|
|
|
|
|
using (var ctx = new DatabaseContext(disableTracking: true))
|
|
|
|
|
{
|
|
|
|
|
ctx.Update(clientStats);
|
|
|
|
|
await ctx.SaveChangesAsync();
|
|
|
|
|
}
|
2018-02-07 00:19:06 -05:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
// increment the total play time
|
2018-11-27 19:31:48 -05:00
|
|
|
|
serverStats.TotalPlayTime += pl.ConnectionLength;
|
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>
|
2018-11-27 19:31:48 -05:00
|
|
|
|
public async Task AddScriptHit(bool isDamage, DateTime time, EFClient attacker, EFClient victim, long serverId, string map, string hitLoc, string type,
|
2018-09-16 16:34:16 -04:00
|
|
|
|
string damage, string weapon, string killOrigin, string deathOrigin, string viewAngles, string offset, string isKillstreakKill, string Ads,
|
|
|
|
|
string fraction, string visibilityPercentage, string snapAngles)
|
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;
|
2018-05-03 01:25:49 -04:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
vDeathOrigin = Vector3.Parse(deathOrigin);
|
|
|
|
|
vKillOrigin = Vector3.Parse(killOrigin);
|
|
|
|
|
vViewAngles = Vector3.Parse(viewAngles).FixIW4Angles();
|
|
|
|
|
}
|
2018-05-08 00:58:46 -04:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
catch (FormatException)
|
2018-05-03 01:25:49 -04:00
|
|
|
|
{
|
2019-05-08 21:34:17 -04:00
|
|
|
|
_log.WriteWarning("Could not parse kill or death origin or viewangle vectors");
|
|
|
|
|
_log.WriteDebug($"Kill - {killOrigin} Death - {deathOrigin} ViewAngle - {viewAngles}");
|
2018-09-16 16:34:16 -04:00
|
|
|
|
await AddStandardKill(attacker, victim);
|
|
|
|
|
return;
|
2018-05-03 01:25:49 -04:00
|
|
|
|
}
|
2018-05-08 00:58:46 -04:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
var snapshotAngles = new List<Vector3>();
|
2018-02-07 00:19:06 -05:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
foreach (string angle in snapAngles.Split(':', StringSplitOptions.RemoveEmptyEntries))
|
|
|
|
|
{
|
|
|
|
|
snapshotAngles.Add(Vector3.Parse(angle).FixIW4Angles());
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-03-06 02:22:19 -05:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
catch (FormatException)
|
|
|
|
|
{
|
2019-05-08 21:34:17 -04:00
|
|
|
|
_log.WriteWarning("Could not parse snapshot angles");
|
2018-09-16 16:34:16 -04:00
|
|
|
|
return;
|
|
|
|
|
}
|
2018-03-06 02:22:19 -05:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
var hit = new EFClientKill()
|
|
|
|
|
{
|
|
|
|
|
Active = true,
|
|
|
|
|
AttackerId = attacker.ClientId,
|
|
|
|
|
VictimId = victim.ClientId,
|
|
|
|
|
ServerId = serverId,
|
2019-05-17 10:02:09 -04:00
|
|
|
|
//Map = ParseEnum<IW4Info.MapName>.Get(map, typeof(IW4Info.MapName)),
|
2018-09-16 16:34:16 -04:00
|
|
|
|
DeathOrigin = vDeathOrigin,
|
|
|
|
|
KillOrigin = vKillOrigin,
|
|
|
|
|
DeathType = ParseEnum<IW4Info.MeansOfDeath>.Get(type, typeof(IW4Info.MeansOfDeath)),
|
2019-06-24 17:56:47 -04:00
|
|
|
|
Damage = int.Parse(damage),
|
2018-09-16 16:34:16 -04:00
|
|
|
|
HitLoc = ParseEnum<IW4Info.HitLocation>.Get(hitLoc, typeof(IW4Info.HitLocation)),
|
|
|
|
|
Weapon = ParseEnum<IW4Info.WeaponName>.Get(weapon, typeof(IW4Info.WeaponName)),
|
|
|
|
|
ViewAngles = vViewAngles,
|
2019-06-24 17:56:47 -04:00
|
|
|
|
TimeOffset = long.Parse(offset),
|
2018-09-16 16:34:16 -04:00
|
|
|
|
When = time,
|
|
|
|
|
IsKillstreakKill = isKillstreakKill[0] != '0',
|
2019-06-09 10:50:58 -04:00
|
|
|
|
AdsPercent = float.Parse(Ads, System.Globalization.CultureInfo.InvariantCulture),
|
|
|
|
|
Fraction = double.Parse(fraction, System.Globalization.CultureInfo.InvariantCulture),
|
|
|
|
|
VisibilityPercentage = double.Parse(visibilityPercentage, System.Globalization.CultureInfo.InvariantCulture),
|
2018-09-16 16:34:16 -04:00
|
|
|
|
IsKill = !isDamage,
|
|
|
|
|
AnglesList = snapshotAngles
|
|
|
|
|
};
|
2018-03-26 00:51:25 -04:00
|
|
|
|
|
2019-08-23 19:34:31 -04:00
|
|
|
|
if (hit.HitLoc == IW4Info.HitLocation.shield)
|
2018-09-16 16:34:16 -04:00
|
|
|
|
{
|
2019-08-23 19:34:31 -04:00
|
|
|
|
// we don't care about shield hits
|
2018-09-16 16:34:16 -04:00
|
|
|
|
return;
|
|
|
|
|
}
|
2018-03-06 02:22:19 -05:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
if (!isDamage)
|
|
|
|
|
{
|
|
|
|
|
await AddStandardKill(attacker, victim);
|
|
|
|
|
}
|
2018-02-26 23:24:19 -05:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
if (hit.IsKillstreakKill)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-25 14:00:54 -04:00
|
|
|
|
// incase the add player event get delayed
|
2019-05-08 21:34:17 -04:00
|
|
|
|
if (!_servers[serverId].PlayerStats.ContainsKey(attacker.ClientId))
|
2018-10-07 22:34:30 -04:00
|
|
|
|
{
|
|
|
|
|
await AddPlayer(attacker);
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-08 21:34:17 -04:00
|
|
|
|
var clientDetection = _servers[serverId].PlayerDetections[attacker.ClientId];
|
|
|
|
|
var clientStats = _servers[serverId].PlayerStats[attacker.ClientId];
|
2018-10-07 22:34:30 -04:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
// increment their hit count
|
|
|
|
|
if (hit.DeathType == IW4Info.MeansOfDeath.MOD_PISTOL_BULLET ||
|
|
|
|
|
hit.DeathType == IW4Info.MeansOfDeath.MOD_RIFLE_BULLET ||
|
|
|
|
|
hit.DeathType == IW4Info.MeansOfDeath.MOD_HEAD_SHOT)
|
|
|
|
|
{
|
|
|
|
|
clientStats.HitLocations.Single(hl => hl.Location == hit.HitLoc).HitCount += 1;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-23 19:34:31 -04:00
|
|
|
|
try
|
2019-06-24 17:56:47 -04:00
|
|
|
|
{
|
2019-08-23 19:34:31 -04:00
|
|
|
|
if (Plugin.Config.Configuration().StoreClientKills)
|
2018-03-06 02:22:19 -05:00
|
|
|
|
{
|
2019-08-23 22:27:36 -04:00
|
|
|
|
_hitCache.Add(hit);
|
2019-08-23 19:34:31 -04:00
|
|
|
|
|
2019-08-23 22:27:36 -04:00
|
|
|
|
if (_hitCache.Count > Detection.MAX_TRACKED_HIT_COUNT)
|
|
|
|
|
{
|
|
|
|
|
OnProcessingPenalty.Wait();
|
|
|
|
|
using (var ctx = new DatabaseContext())
|
2019-08-23 19:34:31 -04:00
|
|
|
|
{
|
2019-08-23 22:27:36 -04:00
|
|
|
|
ctx.AddRange(_hitCache);
|
|
|
|
|
await ctx.SaveChangesAsync();
|
2019-08-23 20:11:36 -04:00
|
|
|
|
}
|
2019-08-23 22:27:36 -04:00
|
|
|
|
|
|
|
|
|
_hitCache.Clear();
|
|
|
|
|
OnProcessingPenalty.Release();
|
2018-09-23 20:45:54 -04:00
|
|
|
|
}
|
2019-08-23 19:34:31 -04:00
|
|
|
|
}
|
2018-09-23 20:45:54 -04:00
|
|
|
|
|
2019-08-23 19:34:31 -04:00
|
|
|
|
|
|
|
|
|
if (Plugin.Config.Configuration().EnableAntiCheat && !attacker.IsBot && attacker.ClientId != victim.ClientId)
|
|
|
|
|
{
|
|
|
|
|
DetectionPenaltyResult result = new DetectionPenaltyResult() { ClientPenalty = EFPenalty.PenaltyType.Any };
|
2019-06-15 18:37:43 -04:00
|
|
|
|
#if DEBUG
|
2019-08-23 19:34:31 -04:00
|
|
|
|
if (clientDetection.TrackedHits.Count > 0)
|
2019-06-15 18:37:43 -04:00
|
|
|
|
#else
|
2019-08-23 20:11:36 -04:00
|
|
|
|
if (clientDetection.TrackedHits.Count > Detection.MAX_TRACKED_HIT_COUNT)
|
2019-06-15 18:37:43 -04:00
|
|
|
|
#endif
|
2019-08-23 19:34:31 -04:00
|
|
|
|
{
|
|
|
|
|
while (clientDetection.TrackedHits.Count > 0)
|
2018-11-05 22:01:29 -05:00
|
|
|
|
{
|
2019-08-23 19:34:31 -04:00
|
|
|
|
await OnProcessingPenalty.WaitAsync();
|
2019-06-24 17:56:47 -04:00
|
|
|
|
|
2019-08-23 19:34:31 -04:00
|
|
|
|
var oldestHit = clientDetection.TrackedHits.OrderBy(_hits => _hits.TimeOffset).First();
|
|
|
|
|
clientDetection.TrackedHits.Remove(oldestHit);
|
2019-06-24 17:56:47 -04:00
|
|
|
|
|
2019-08-23 19:34:31 -04:00
|
|
|
|
result = clientDetection.ProcessHit(oldestHit, isDamage);
|
|
|
|
|
await ApplyPenalty(result, attacker);
|
2019-06-16 15:49:04 -04:00
|
|
|
|
|
2019-08-23 19:34:31 -04:00
|
|
|
|
if (clientDetection.Tracker.HasChanges && result.ClientPenalty != EFPenalty.PenaltyType.Any)
|
|
|
|
|
{
|
|
|
|
|
using (var ctx = new DatabaseContext())
|
2019-06-16 15:49:04 -04:00
|
|
|
|
{
|
|
|
|
|
SaveTrackedSnapshots(clientDetection, ctx);
|
2019-08-23 19:34:31 -04:00
|
|
|
|
await ctx.SaveChangesAsync();
|
2019-06-24 17:56:47 -04:00
|
|
|
|
}
|
2019-06-16 15:49:04 -04:00
|
|
|
|
|
2019-08-23 19:34:31 -04:00
|
|
|
|
if (result.ClientPenalty == EFPenalty.PenaltyType.Ban)
|
|
|
|
|
{
|
|
|
|
|
OnProcessingPenalty.Release(1);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-06-16 15:49:04 -04:00
|
|
|
|
}
|
2018-11-05 22:01:29 -05:00
|
|
|
|
|
2019-08-23 19:34:31 -04:00
|
|
|
|
OnProcessingPenalty.Release(1);
|
2018-11-05 22:01:29 -05:00
|
|
|
|
}
|
2018-09-23 20:45:54 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-23 19:34:31 -04:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
clientDetection.TrackedHits.Add(hit);
|
|
|
|
|
}
|
2018-09-11 15:28:37 -04:00
|
|
|
|
}
|
2019-08-23 19:34:31 -04:00
|
|
|
|
}
|
2018-09-23 20:45:54 -04:00
|
|
|
|
|
2019-08-23 19:34:31 -04:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_log.WriteError("Could not save hit or AC info");
|
|
|
|
|
_log.WriteDebug(ex.GetExceptionInfo());
|
2018-09-16 16:34:16 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-11 15:28:37 -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()
|
|
|
|
|
{
|
|
|
|
|
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-05-08 21:34:17 -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
|
|
|
|
|
|
|
|
|
string flagReason = penalty.Type == Cheat.Detection.DetectionType.Bone ?
|
2018-09-23 20:45:54 -04:00
|
|
|
|
$"{penalty.Type}-{(int)penalty.Location}-{Math.Round(penalty.Value, 2)}@{penalty.HitCount}" :
|
2018-10-05 23:10:39 -04:00
|
|
|
|
$"{penalty.Type}-{Math.Round(penalty.Value, 2)}@{penalty.HitCount}";
|
|
|
|
|
|
2019-06-25 19:01:47 -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-04-25 14:00:54 -04:00
|
|
|
|
void SaveTrackedSnapshots(Detection clientDetection, DatabaseContext ctx)
|
2018-02-07 00:19:06 -05:00
|
|
|
|
{
|
2018-09-23 20:45:54 -04:00
|
|
|
|
// todo: why does this cause duplicate primary key
|
2019-05-13 11:36:11 -04:00
|
|
|
|
_ = clientDetection.Tracker.GetNextChange();
|
|
|
|
|
EFACSnapshot change;
|
2018-09-23 20:45:54 -04:00
|
|
|
|
while ((change = clientDetection.Tracker.GetNextChange()) != default(EFACSnapshot))
|
2018-09-16 16:34:16 -04:00
|
|
|
|
{
|
2018-09-23 20:45:54 -04:00
|
|
|
|
|
|
|
|
|
if (change.HitOrigin.Vector3Id > 0)
|
2018-09-16 16:34:16 -04:00
|
|
|
|
{
|
2018-09-29 15:52:22 -04:00
|
|
|
|
change.HitOriginId = change.HitOrigin.Vector3Id;
|
2018-09-23 20:45:54 -04:00
|
|
|
|
ctx.Attach(change.HitOrigin);
|
2018-09-16 16:34:16 -04:00
|
|
|
|
}
|
2018-09-29 15:52:22 -04:00
|
|
|
|
|
|
|
|
|
else if (change.HitOrigin.Vector3Id == 0)
|
|
|
|
|
{
|
|
|
|
|
ctx.Add(change.HitOrigin);
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-23 20:45:54 -04:00
|
|
|
|
if (change.HitDestination.Vector3Id > 0)
|
2018-09-16 16:34:16 -04:00
|
|
|
|
{
|
2018-09-29 15:52:22 -04:00
|
|
|
|
change.HitDestinationId = change.HitDestination.Vector3Id;
|
2018-09-23 20:45:54 -04:00
|
|
|
|
ctx.Attach(change.HitDestination);
|
2018-09-16 16:34:16 -04:00
|
|
|
|
}
|
2018-09-29 15:52:22 -04:00
|
|
|
|
|
|
|
|
|
else if (change.HitDestination.Vector3Id == 0)
|
|
|
|
|
{
|
|
|
|
|
ctx.Add(change.HitOrigin);
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-23 20:45:54 -04:00
|
|
|
|
if (change.CurrentViewAngle.Vector3Id > 0)
|
2018-09-16 16:34:16 -04:00
|
|
|
|
{
|
2018-09-29 15:52:22 -04:00
|
|
|
|
change.CurrentViewAngleId = change.CurrentViewAngle.Vector3Id;
|
2018-09-23 20:45:54 -04:00
|
|
|
|
ctx.Attach(change.CurrentViewAngle);
|
2018-09-16 16:34:16 -04:00
|
|
|
|
}
|
2018-09-29 15:52:22 -04:00
|
|
|
|
|
|
|
|
|
else if (change.CurrentViewAngle.Vector3Id == 0)
|
|
|
|
|
{
|
|
|
|
|
ctx.Add(change.HitOrigin);
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-23 20:45:54 -04:00
|
|
|
|
if (change.LastStrainAngle.Vector3Id > 0)
|
|
|
|
|
{
|
2018-09-29 15:52:22 -04:00
|
|
|
|
change.LastStrainAngleId = change.LastStrainAngle.Vector3Id;
|
2018-09-23 20:45:54 -04:00
|
|
|
|
ctx.Attach(change.LastStrainAngle);
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-29 15:52:22 -04:00
|
|
|
|
else if (change.LastStrainAngle.Vector3Id == 0)
|
|
|
|
|
{
|
|
|
|
|
ctx.Add(change.HitOrigin);
|
|
|
|
|
}
|
2018-09-16 18:51:11 -04:00
|
|
|
|
|
2018-09-29 15:52:22 -04:00
|
|
|
|
ctx.Add(change);
|
2018-09-23 20:45:54 -04:00
|
|
|
|
}
|
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
|
|
|
|
{
|
2019-08-23 19:34:31 -04:00
|
|
|
|
long serverId = GetIdForServer(attacker.CurrentServer);
|
2018-09-29 15:52:22 -04:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
EFClientStatistics attackerStats = null;
|
2019-05-08 21:34:17 -04:00
|
|
|
|
if (!_servers[serverId].PlayerStats.ContainsKey(attacker.ClientId))
|
2018-09-16 16:34:16 -04:00
|
|
|
|
{
|
2018-09-29 15:52:22 -04:00
|
|
|
|
attackerStats = await AddPlayer(attacker);
|
2018-09-16 16:34:16 -04:00
|
|
|
|
}
|
2018-09-11 15:28:37 -04:00
|
|
|
|
|
2018-09-29 15:52:22 -04:00
|
|
|
|
else
|
2018-09-16 16:34:16 -04:00
|
|
|
|
{
|
2019-05-08 21:34:17 -04:00
|
|
|
|
attackerStats = _servers[serverId].PlayerStats[attacker.ClientId];
|
2018-09-16 16:34:16 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EFClientStatistics victimStats = null;
|
2019-05-08 21:34:17 -04:00
|
|
|
|
if (!_servers[serverId].PlayerStats.ContainsKey(victim.ClientId))
|
2018-09-16 16:34:16 -04:00
|
|
|
|
{
|
2018-09-29 15:52:22 -04:00
|
|
|
|
victimStats = await AddPlayer(victim);
|
2018-09-16 16:34:16 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-29 15:52:22 -04:00
|
|
|
|
else
|
2018-09-16 16:34:16 -04:00
|
|
|
|
{
|
2019-05-08 21:34:17 -04:00
|
|
|
|
victimStats = _servers[serverId].PlayerStats[victim.ClientId];
|
2018-09-16 16:34:16 -04:00
|
|
|
|
}
|
2018-02-08 02:23:45 -05:00
|
|
|
|
|
2018-04-28 01:22:18 -04:00
|
|
|
|
#if DEBUG
|
2019-05-08 21:34:17 -04:00
|
|
|
|
_log.WriteDebug("Calculating standard kill");
|
2018-04-28 01:22:18 -04:00
|
|
|
|
#endif
|
|
|
|
|
|
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;
|
2018-02-09 02:21:25 -05:00
|
|
|
|
|
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
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
attackerStats.SessionScore = attacker.Score;
|
|
|
|
|
victimStats.SessionScore = victim.Score;
|
2018-03-06 02:22:19 -05:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
// calculate for the clients
|
|
|
|
|
CalculateKill(attackerStats, victimStats);
|
|
|
|
|
// this should fix the negative SPM
|
|
|
|
|
// updates their last score after being calculated
|
|
|
|
|
attackerStats.LastScore = attacker.Score;
|
|
|
|
|
victimStats.LastScore = victim.Score;
|
2018-02-09 02:21:25 -05:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
// show encouragement/discouragement
|
|
|
|
|
string streakMessage = (attackerStats.ClientId != victimStats.ClientId) ?
|
|
|
|
|
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))
|
|
|
|
|
{
|
2019-05-08 21:34:17 -04:00
|
|
|
|
_log.WriteDebug($"[StatManager::AddStandardKill] victim SPM/SKILL {victimStats.SPM} {victimStats.Skill}");
|
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))
|
|
|
|
|
{
|
2019-05-08 21:34:17 -04:00
|
|
|
|
_log.WriteDebug($"[StatManager::AddStandardKill] attacker SPM/SKILL {victimStats.SPM} {victimStats.Skill}");
|
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
|
2018-09-04 13:40:29 -04:00
|
|
|
|
#if !DEBUG
|
2018-06-01 20:55:26 -04:00
|
|
|
|
if ((DateTime.UtcNow - attackerStats.LastStatHistoryUpdate).TotalMinutes >= 2.5)
|
2018-09-29 15:52:22 -04:00
|
|
|
|
#else
|
2018-10-06 16:31:05 -04:00
|
|
|
|
if ((DateTime.UtcNow - attackerStats.LastStatHistoryUpdate).TotalMinutes >= 0.1)
|
2018-09-04 13:40:29 -04:00
|
|
|
|
#endif
|
2018-09-16 16:34:16 -04:00
|
|
|
|
{
|
|
|
|
|
attackerStats.LastStatHistoryUpdate = DateTime.UtcNow;
|
|
|
|
|
await UpdateStatHistory(attacker, attackerStats);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-07 23:29:42 -04:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Update the invidual and average stat history for a client
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="client">client to update</param>
|
|
|
|
|
/// <param name="clientStats">stats of client that is being updated</param>
|
|
|
|
|
/// <returns></returns>
|
2018-11-05 22:01:29 -05:00
|
|
|
|
private async Task UpdateStatHistory(EFClient client, EFClientStatistics clientStats)
|
2018-09-16 16:34:16 -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
|
2019-08-23 19:34:31 -04:00
|
|
|
|
//#if DEBUG == false
|
2018-09-07 23:29:42 -04:00
|
|
|
|
if (currentSessionTime < 60)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-08-23 19:34:31 -04:00
|
|
|
|
//#endif
|
2018-09-07 23:29:42 -04:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
int currentServerTotalPlaytime = clientStats.TimePlayed + currentSessionTime;
|
2018-05-30 21:50:20 -04:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
using (var ctx = new DatabaseContext())
|
|
|
|
|
{
|
|
|
|
|
// select the rating history for client
|
|
|
|
|
var iqHistoryLink = from history in ctx.Set<EFClientRatingHistory>()
|
|
|
|
|
.Include(h => h.Ratings)
|
|
|
|
|
where history.ClientId == client.ClientId
|
|
|
|
|
select history;
|
|
|
|
|
|
|
|
|
|
// get the client ratings
|
|
|
|
|
var clientHistory = await iqHistoryLink
|
|
|
|
|
.FirstOrDefaultAsync() ?? new EFClientRatingHistory()
|
|
|
|
|
{
|
|
|
|
|
Active = true,
|
|
|
|
|
ClientId = client.ClientId,
|
|
|
|
|
Ratings = new List<EFRating>()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// it's the first time they've played
|
|
|
|
|
if (clientHistory.RatingHistoryId == 0)
|
|
|
|
|
{
|
|
|
|
|
ctx.Add(clientHistory);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-24 17:56:47 -04:00
|
|
|
|
#region INDIVIDUAL_SERVER_PERFORMANCE
|
2018-09-16 16:34:16 -04:00
|
|
|
|
// get the client ranking for the current server
|
2019-08-23 19:34:31 -04:00
|
|
|
|
int individualClientRanking = await ctx.Set<EFRating>()
|
|
|
|
|
.Where(GetRankingFunc(clientStats.ServerId))
|
|
|
|
|
// ignore themselves in the query
|
|
|
|
|
.Where(c => c.RatingHistory.ClientId != client.ClientId)
|
|
|
|
|
.Where(c => c.Performance > clientStats.Performance)
|
|
|
|
|
.CountAsync() + 1;
|
2018-09-16 16:34:16 -04:00
|
|
|
|
|
|
|
|
|
// limit max history per server to 40
|
|
|
|
|
if (clientHistory.Ratings.Count(r => r.ServerId == clientStats.ServerId) >= 40)
|
2018-05-30 21:50:20 -04:00
|
|
|
|
{
|
2018-09-16 16:34:16 -04:00
|
|
|
|
// select the oldest one
|
|
|
|
|
var ratingToRemove = clientHistory.Ratings
|
|
|
|
|
.Where(r => r.ServerId == clientStats.ServerId)
|
|
|
|
|
.OrderBy(r => r.When)
|
|
|
|
|
.First();
|
|
|
|
|
|
|
|
|
|
ctx.Remove(ratingToRemove);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// set the previous newest to false
|
|
|
|
|
var ratingToUnsetNewest = clientHistory.Ratings
|
|
|
|
|
.Where(r => r.ServerId == clientStats.ServerId)
|
|
|
|
|
.OrderByDescending(r => r.When)
|
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
if (ratingToUnsetNewest != null)
|
|
|
|
|
{
|
|
|
|
|
if (ratingToUnsetNewest.Newest)
|
|
|
|
|
{
|
|
|
|
|
ctx.Update(ratingToUnsetNewest);
|
|
|
|
|
ctx.Entry(ratingToUnsetNewest).Property(r => r.Newest).IsModified = true;
|
|
|
|
|
ratingToUnsetNewest.Newest = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var newServerRating = new EFRating()
|
|
|
|
|
{
|
|
|
|
|
Performance = clientStats.Performance,
|
|
|
|
|
Ranking = individualClientRanking,
|
2018-05-30 21:50:20 -04:00
|
|
|
|
Active = true,
|
2018-09-16 16:34:16 -04:00
|
|
|
|
Newest = true,
|
|
|
|
|
ServerId = clientStats.ServerId,
|
|
|
|
|
RatingHistoryId = clientHistory.RatingHistoryId,
|
|
|
|
|
ActivityAmount = currentServerTotalPlaytime,
|
2018-09-08 18:29:30 -04:00
|
|
|
|
};
|
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
// add new rating for current server
|
|
|
|
|
ctx.Add(newServerRating);
|
|
|
|
|
|
2019-06-24 17:56:47 -04:00
|
|
|
|
#endregion
|
|
|
|
|
#region OVERALL_RATING
|
2018-09-16 16:34:16 -04:00
|
|
|
|
// select all performance & time played for current client
|
|
|
|
|
var iqClientStats = from stats in ctx.Set<EFClientStatistics>()
|
|
|
|
|
where stats.ClientId == client.ClientId
|
|
|
|
|
where stats.ServerId != clientStats.ServerId
|
|
|
|
|
select new
|
|
|
|
|
{
|
|
|
|
|
stats.Performance,
|
|
|
|
|
stats.TimePlayed
|
|
|
|
|
};
|
2018-05-30 21:50:20 -04:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
var clientStatsList = await iqClientStats.ToListAsync();
|
2018-08-28 17:32:59 -04:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
// add the current server's so we don't have to pull it frmo the database
|
|
|
|
|
clientStatsList.Add(new
|
|
|
|
|
{
|
|
|
|
|
clientStats.Performance,
|
|
|
|
|
TimePlayed = currentServerTotalPlaytime
|
|
|
|
|
});
|
2018-05-30 21:50:20 -04:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
// weight the overall performance based on play time
|
|
|
|
|
double performanceAverage = clientStatsList.Sum(p => (p.Performance * p.TimePlayed)) / clientStatsList.Sum(p => p.TimePlayed);
|
2018-05-30 21:50:20 -04:00
|
|
|
|
|
2018-09-16 16:34:16 -04: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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int overallClientRanking = await ctx.Set<EFRating>()
|
|
|
|
|
.Where(GetRankingFunc())
|
|
|
|
|
.Where(r => r.RatingHistory.ClientId != client.ClientId)
|
|
|
|
|
.Where(r => r.Performance > performanceAverage)
|
|
|
|
|
.CountAsync() + 1;
|
|
|
|
|
|
|
|
|
|
// limit max average history to 40
|
|
|
|
|
if (clientHistory.Ratings.Count(r => r.ServerId == null) >= 40)
|
2018-05-31 20:17:52 -04:00
|
|
|
|
{
|
2018-09-16 16:34:16 -04:00
|
|
|
|
var ratingToRemove = clientHistory.Ratings
|
|
|
|
|
.Where(r => r.ServerId == null)
|
|
|
|
|
.OrderBy(r => r.When)
|
|
|
|
|
.First();
|
|
|
|
|
|
|
|
|
|
ctx.Remove(ratingToRemove);
|
2018-05-31 20:17:52 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-16 16:34:16 -04: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
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
if (ratingToUnsetNewest != null)
|
|
|
|
|
{
|
|
|
|
|
if (ratingToUnsetNewest.Newest)
|
|
|
|
|
{
|
|
|
|
|
ctx.Update(ratingToUnsetNewest);
|
|
|
|
|
ctx.Entry(ratingToUnsetNewest).Property(r => r.Newest).IsModified = true;
|
|
|
|
|
ratingToUnsetNewest.Newest = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-30 21:50:20 -04:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
// add new average rating
|
|
|
|
|
var averageRating = new EFRating()
|
|
|
|
|
{
|
|
|
|
|
Active = true,
|
|
|
|
|
Newest = true,
|
|
|
|
|
Performance = performanceAverage,
|
|
|
|
|
Ranking = overallClientRanking,
|
|
|
|
|
ServerId = null,
|
|
|
|
|
RatingHistoryId = clientHistory.RatingHistoryId,
|
|
|
|
|
ActivityAmount = clientStatsList.Sum(s => s.TimePlayed)
|
|
|
|
|
};
|
2018-09-07 23:29:42 -04:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
ctx.Add(averageRating);
|
2019-06-24 17:56:47 -04:00
|
|
|
|
#endregion
|
2018-09-29 15:52:22 -04:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
await ctx.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-30 21:50:20 -04:00
|
|
|
|
|
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>
|
|
|
|
|
public void CalculateKill(EFClientStatistics attackerStats, EFClientStatistics victimStats)
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
attackerStats.SessionKills += 1;
|
|
|
|
|
attackerStats.KillStreak += 1;
|
|
|
|
|
attackerStats.DeathStreak = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
victimStats.Deaths += 1;
|
|
|
|
|
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
|
|
|
|
|
attackerStats = UpdateStats(attackerStats);
|
2018-02-08 02:23:45 -05:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
// calulate elo
|
2019-05-08 21:34:17 -04:00
|
|
|
|
if (_servers[attackerStats.ServerId].PlayerStats.Count > 1)
|
2018-05-16 00:57:37 -04:00
|
|
|
|
{
|
2019-06-24 17:56:47 -04:00
|
|
|
|
#region DEPRECATED
|
2018-09-16 16:34:16 -04:00
|
|
|
|
/* var validAttackerLobbyRatings = Servers[attackerStats.ServerId].PlayerStats
|
|
|
|
|
.Where(cs => cs.Value.ClientId != attackerStats.ClientId)
|
|
|
|
|
.Where(cs =>
|
|
|
|
|
Servers[attackerStats.ServerId].IsTeamBased ?
|
|
|
|
|
cs.Value.Team != attackerStats.Team :
|
|
|
|
|
cs.Value.Team != IW4Info.Team.Spectator)
|
|
|
|
|
.Where(cs => cs.Value.Team != IW4Info.Team.Spectator);
|
|
|
|
|
|
|
|
|
|
double attackerLobbyRating = validAttackerLobbyRatings.Count() > 0 ?
|
|
|
|
|
validAttackerLobbyRatings.Average(cs => cs.Value.EloRating) :
|
|
|
|
|
attackerStats.EloRating;
|
|
|
|
|
|
|
|
|
|
var validVictimLobbyRatings = Servers[victimStats.ServerId].PlayerStats
|
|
|
|
|
.Where(cs => cs.Value.ClientId != victimStats.ClientId)
|
|
|
|
|
.Where(cs =>
|
|
|
|
|
Servers[attackerStats.ServerId].IsTeamBased ?
|
|
|
|
|
cs.Value.Team != victimStats.Team :
|
|
|
|
|
cs.Value.Team != IW4Info.Team.Spectator)
|
|
|
|
|
.Where(cs => cs.Value.Team != IW4Info.Team.Spectator);
|
|
|
|
|
|
|
|
|
|
double victimLobbyRating = validVictimLobbyRatings.Count() > 0 ?
|
|
|
|
|
validVictimLobbyRatings.Average(cs => cs.Value.EloRating) :
|
|
|
|
|
victimStats.EloRating;*/
|
2019-06-24 17:56:47 -04:00
|
|
|
|
#endregion
|
2018-09-16 16:34:16 -04:00
|
|
|
|
|
|
|
|
|
double attackerEloDifference = Math.Log(Math.Max(1, victimStats.EloRating)) - Math.Log(Math.Max(1, attackerStats.EloRating));
|
|
|
|
|
double winPercentage = 1.0 / (1 + Math.Pow(10, attackerEloDifference / Math.E));
|
|
|
|
|
|
|
|
|
|
// double victimEloDifference = Math.Log(Math.Max(1, attackerStats.EloRating)) - Math.Log(Math.Max(1, victimStats.EloRating));
|
|
|
|
|
// double lossPercentage = 1.0 / (1 + Math.Pow(10, victimEloDifference/ Math.E));
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
attackerStats.TimePlayed += (int)(DateTime.UtcNow - attackerStats.LastActive).TotalSeconds;
|
|
|
|
|
victimStats.TimePlayed += (int)(DateTime.UtcNow - victimStats.LastActive).TotalSeconds;
|
|
|
|
|
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>
|
|
|
|
|
private EFClientStatistics UpdateStats(EFClientStatistics clientStats)
|
|
|
|
|
{
|
|
|
|
|
// 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
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
double timeSinceLastCalc = (DateTime.UtcNow - clientStats.LastStatCalculation).TotalSeconds / 60.0;
|
|
|
|
|
double timeSinceLastActive = (DateTime.UtcNow - clientStats.LastActive).TotalSeconds / 60.0;
|
2018-04-10 20:25:44 -04:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
int scoreDifference = 0;
|
|
|
|
|
// 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
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
double killSPM = scoreDifference / timeSinceLastCalc;
|
2019-05-08 21:34:17 -04:00
|
|
|
|
double spmMultiplier = 2.934 * Math.Pow(_servers[clientStats.ServerId].TeamCount(clientStats.Team == IW4Info.Team.Allies ? IW4Info.Team.Axis : IW4Info.Team.Allies), -0.454);
|
2018-09-16 16:34:16 -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
|
|
|
|
|
clientStats.SessionSPM = killSPM;
|
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
|
|
|
|
|
double currentKDR = clientStats.SessionDeaths == 0 ? clientStats.SessionKills : clientStats.SessionKills / clientStats.SessionDeaths;
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
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)
|
|
|
|
|
{
|
2019-05-08 21:34:17 -04:00
|
|
|
|
_log.WriteWarning("[StatManager:UpdateStats] clientStats SPM < 0");
|
|
|
|
|
_log.WriteDebug($"{scoreDifference}-{clientStats.RoundScore} - {clientStats.LastScore} - {clientStats.SessionScore}");
|
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))
|
|
|
|
|
{
|
2019-05-08 21:34:17 -04:00
|
|
|
|
_log.WriteWarning("[StatManager::UpdateStats] clientStats SPM/Skill NaN");
|
|
|
|
|
_log.WriteDebug($"{killSPM}-{KDRWeight}-{totalPlayTime}-{SPMAgainstPlayWeight}-{clientStats.SPM}-{clientStats.Skill}-{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;
|
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
|
|
|
|
|
2018-10-07 22:34:30 -04:00
|
|
|
|
using (var ctx = new DatabaseContext(disableTracking: true))
|
2018-09-16 16:34:16 -04:00
|
|
|
|
{
|
2018-10-07 22:34:30 -04:00
|
|
|
|
var serverStatsSet = ctx.Set<EFServerStatistics>();
|
|
|
|
|
serverStats = serverStatsSet.FirstOrDefault(s => s.ServerId == serverId);
|
2018-09-11 15:28:37 -04:00
|
|
|
|
|
2018-10-07 22:34:30 -04:00
|
|
|
|
if (serverStats == null)
|
|
|
|
|
{
|
2019-05-08 21:34:17 -04:00
|
|
|
|
_log.WriteDebug($"Initializing server stats for {serverId}");
|
2018-10-07 22:34:30 -04:00
|
|
|
|
// server stats have never been generated before
|
|
|
|
|
serverStats = new EFServerStatistics()
|
|
|
|
|
{
|
|
|
|
|
ServerId = serverId,
|
|
|
|
|
TotalKills = 0,
|
|
|
|
|
TotalPlayTime = 0,
|
|
|
|
|
};
|
2018-09-11 15:28:37 -04:00
|
|
|
|
|
2018-10-07 22:34:30 -04: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
|
|
|
|
|
2018-11-27 19:31:48 -05:00
|
|
|
|
public void ResetKillstreaks(long serverId)
|
2018-03-06 02:22:19 -05:00
|
|
|
|
{
|
2019-05-08 21:34:17 -04:00
|
|
|
|
var serverStats = _servers[serverId];
|
2018-10-07 22:34:30 -04:00
|
|
|
|
|
2018-09-16 16:34:16 -04:00
|
|
|
|
foreach (var stat in serverStats.PlayerStats.Values)
|
2018-10-07 22:34:30 -04:00
|
|
|
|
{
|
2018-09-16 16:34:16 -04:00
|
|
|
|
stat.StartNewSession();
|
2018-10-07 22:34:30 -04:00
|
|
|
|
}
|
2018-03-06 02:22:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-27 19:31:48 -05:00
|
|
|
|
public void ResetStats(int clientId, long serverId)
|
2018-03-06 02:22:19 -05:00
|
|
|
|
{
|
2019-05-08 21:34:17 -04:00
|
|
|
|
var stats = _servers[serverId].PlayerStats[clientId];
|
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
|
|
|
|
}
|
|
|
|
|
|
2018-11-27 19:31:48 -05:00
|
|
|
|
public async Task AddMessageAsync(int clientId, long serverId, 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
|
|
|
|
|
2018-10-07 22:34:30 -04:00
|
|
|
|
using (var ctx = new DatabaseContext(disableTracking: true))
|
2018-02-10 01:26:38 -05:00
|
|
|
|
{
|
2018-10-07 22:34:30 -04:00
|
|
|
|
ctx.Set<EFClientMessage>().Add(new EFClientMessage()
|
|
|
|
|
{
|
|
|
|
|
ClientId = clientId,
|
|
|
|
|
Message = message,
|
|
|
|
|
ServerId = serverId,
|
|
|
|
|
TimeSent = DateTime.UtcNow
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2018-10-07 22:34:30 -04:00
|
|
|
|
using (var ctx = new DatabaseContext(disableTracking: true))
|
|
|
|
|
{
|
|
|
|
|
var serverSet = ctx.Set<EFServer>();
|
2019-05-08 21:34:17 -04:00
|
|
|
|
serverSet.Update(_servers[serverId].Server);
|
2018-11-27 19:31:48 -05:00
|
|
|
|
|
|
|
|
|
var serverStatsSet = ctx.Set<EFServerStatistics>();
|
2019-05-08 21:34:17 -04:00
|
|
|
|
serverStatsSet.Update(_servers[serverId].ServerStatistics);
|
2018-11-27 19:31:48 -05:00
|
|
|
|
|
2018-10-07 22:34:30 -04:00
|
|
|
|
await ctx.SaveChangesAsync();
|
|
|
|
|
}
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
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 ||
|
|
|
|
|
_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
|
|
|
|
}
|
|
|
|
|
}
|