2018-02-07 00:19:06 -05:00
|
|
|
|
using System;
|
2018-02-15 23:01:28 -05:00
|
|
|
|
using System.Collections.Concurrent;
|
2018-02-07 00:19:06 -05:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using SharedLibrary;
|
|
|
|
|
using SharedLibrary.Helpers;
|
|
|
|
|
using SharedLibrary.Interfaces;
|
|
|
|
|
using SharedLibrary.Objects;
|
|
|
|
|
using SharedLibrary.Services;
|
|
|
|
|
using StatsPlugin.Models;
|
2018-03-06 02:22:19 -05:00
|
|
|
|
using SharedLibrary.Commands;
|
2018-03-18 22:25:11 -04:00
|
|
|
|
using SharedLibrary.Configuration;
|
|
|
|
|
using StatsPlugin.Config;
|
2018-02-07 00:19:06 -05:00
|
|
|
|
|
|
|
|
|
namespace StatsPlugin.Helpers
|
|
|
|
|
{
|
|
|
|
|
public class StatManager
|
|
|
|
|
{
|
2018-02-15 23:01:28 -05:00
|
|
|
|
private ConcurrentDictionary<int, ServerStats> Servers;
|
|
|
|
|
private ConcurrentDictionary<int, ThreadSafeStatsService> ContextThreads;
|
2018-02-07 00:19:06 -05:00
|
|
|
|
private ILogger Log;
|
|
|
|
|
private IManager Manager;
|
|
|
|
|
|
|
|
|
|
public StatManager(IManager mgr)
|
|
|
|
|
{
|
2018-02-15 23:01:28 -05:00
|
|
|
|
Servers = new ConcurrentDictionary<int, ServerStats>();
|
|
|
|
|
ContextThreads = new ConcurrentDictionary<int, ThreadSafeStatsService>();
|
2018-02-07 00:19:06 -05:00
|
|
|
|
Log = mgr.GetLogger();
|
|
|
|
|
Manager = mgr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~StatManager()
|
|
|
|
|
{
|
|
|
|
|
Servers.Clear();
|
|
|
|
|
Log = null;
|
|
|
|
|
Servers = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Add a server to the StatManager server pool
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sv"></param>
|
|
|
|
|
public void AddServer(Server sv)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
int serverId = sv.GetHashCode();
|
2018-02-10 01:26:38 -05:00
|
|
|
|
var statsSvc = new ThreadSafeStatsService();
|
2018-02-15 23:01:28 -05:00
|
|
|
|
ContextThreads.TryAdd(serverId, statsSvc);
|
2018-02-09 02:21:25 -05:00
|
|
|
|
|
2018-02-07 00:19:06 -05:00
|
|
|
|
// get the server from the database if it exists, otherwise create and insert a new one
|
2018-02-10 01:26:38 -05:00
|
|
|
|
var server = statsSvc.ServerSvc.Find(c => c.ServerId == serverId).FirstOrDefault();
|
2018-02-07 00:19:06 -05:00
|
|
|
|
if (server == null)
|
|
|
|
|
{
|
|
|
|
|
server = new EFServer()
|
|
|
|
|
{
|
|
|
|
|
Port = sv.GetPort(),
|
|
|
|
|
Active = true,
|
|
|
|
|
ServerId = serverId
|
|
|
|
|
};
|
|
|
|
|
|
2018-02-10 01:26:38 -05:00
|
|
|
|
statsSvc.ServerSvc.Insert(server);
|
2018-02-07 00:19:06 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// this doesn't need to be async as it's during initialization
|
2018-02-10 01:26:38 -05:00
|
|
|
|
statsSvc.ServerSvc.SaveChanges();
|
2018-02-09 02:21:25 -05:00
|
|
|
|
// check to see if the stats have ever been initialized
|
2018-02-10 01:26:38 -05:00
|
|
|
|
InitializeServerStats(sv);
|
|
|
|
|
statsSvc.ServerStatsSvc.SaveChanges();
|
2018-02-09 02:21:25 -05:00
|
|
|
|
|
2018-02-10 01:26:38 -05:00
|
|
|
|
var serverStats = statsSvc.ServerStatsSvc.Find(c => c.ServerId == serverId).FirstOrDefault();
|
2018-02-15 23:01:28 -05:00
|
|
|
|
Servers.TryAdd(serverId, new ServerStats(server, serverStats));
|
2018-02-07 00:19:06 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2018-03-06 02:22:19 -05:00
|
|
|
|
Log.WriteError($"Could not add server to ServerStats - {e.Message}");
|
2018-02-07 00:19:06 -05: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-03-06 02:22:19 -05:00
|
|
|
|
public async Task<EFClientStatistics> AddPlayer(Player pl)
|
2018-02-07 00:19:06 -05:00
|
|
|
|
{
|
2018-03-06 02:22:19 -05:00
|
|
|
|
Log.WriteInfo($"Adding {pl} to stats");
|
2018-02-07 00:19:06 -05:00
|
|
|
|
int serverId = pl.CurrentServer.GetHashCode();
|
2018-03-06 02:22:19 -05:00
|
|
|
|
|
|
|
|
|
if (!Servers.ContainsKey(serverId))
|
|
|
|
|
{
|
|
|
|
|
Log.WriteError($"[Stats::AddPlayer] Server with id {serverId} could not be found");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-07 00:19:06 -05:00
|
|
|
|
var playerStats = Servers[serverId].PlayerStats;
|
2018-02-10 01:26:38 -05:00
|
|
|
|
var statsSvc = ContextThreads[serverId];
|
2018-02-07 00:19:06 -05: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-02-10 01:26:38 -05:00
|
|
|
|
var clientStats = statsSvc.ClientStatSvc.Find(c => c.ClientId == pl.ClientId && c.ServerId == serverId).FirstOrDefault();
|
2018-02-15 23:01:28 -05:00
|
|
|
|
|
2018-02-07 00:19:06 -05:00
|
|
|
|
if (clientStats == null)
|
|
|
|
|
{
|
|
|
|
|
clientStats = new EFClientStatistics()
|
|
|
|
|
{
|
|
|
|
|
Active = true,
|
|
|
|
|
ClientId = pl.ClientId,
|
|
|
|
|
Deaths = 0,
|
|
|
|
|
Kills = 0,
|
|
|
|
|
ServerId = serverId,
|
|
|
|
|
Skill = 0.0,
|
|
|
|
|
SPM = 0.0,
|
2018-03-06 02:22:19 -05:00
|
|
|
|
HitLocations = Enum.GetValues(typeof(IW4Info.HitLocation)).OfType<IW4Info.HitLocation>().Select(hl => new EFHitLocationCount()
|
|
|
|
|
{
|
|
|
|
|
Active = true,
|
|
|
|
|
HitCount = 0,
|
|
|
|
|
Location = hl
|
|
|
|
|
})
|
|
|
|
|
.ToList()
|
2018-02-07 00:19:06 -05:00
|
|
|
|
};
|
|
|
|
|
|
2018-02-10 01:26:38 -05:00
|
|
|
|
clientStats = statsSvc.ClientStatSvc.Insert(clientStats);
|
2018-03-06 02:22:19 -05:00
|
|
|
|
await statsSvc.ClientStatSvc.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// migration for previous existing stats
|
|
|
|
|
else 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();
|
|
|
|
|
await statsSvc.ClientStatSvc.SaveChangesAsync();
|
2018-02-07 00:19:06 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-09 02:21:25 -05:00
|
|
|
|
// set these on connecting
|
|
|
|
|
clientStats.LastActive = DateTime.UtcNow;
|
|
|
|
|
clientStats.LastStatCalculation = DateTime.UtcNow;
|
2018-03-06 02:22:19 -05:00
|
|
|
|
clientStats.SessionScore = pl.Score;
|
2018-02-09 02:21:25 -05:00
|
|
|
|
|
2018-03-06 02:22:19 -05:00
|
|
|
|
if (playerStats.ContainsKey(pl.ClientId))
|
2018-02-08 02:23:45 -05:00
|
|
|
|
{
|
2018-03-06 02:22:19 -05:00
|
|
|
|
Log.WriteWarning($"Duplicate ClientId in stats {pl.ClientId} vs {playerStats[pl.ClientId].ClientId}");
|
|
|
|
|
playerStats.TryRemove(pl.ClientId, out EFClientStatistics removedValue);
|
2018-02-08 02:23:45 -05:00
|
|
|
|
}
|
2018-03-06 02:22:19 -05:00
|
|
|
|
playerStats.TryAdd(pl.ClientId, clientStats);
|
2018-02-26 23:24:19 -05:00
|
|
|
|
|
|
|
|
|
var detectionStats = Servers[serverId].PlayerDetections;
|
|
|
|
|
|
2018-03-06 02:22:19 -05:00
|
|
|
|
if (detectionStats.ContainsKey(pl.ClientId))
|
|
|
|
|
detectionStats.TryRemove(pl.ClientId, out Cheat.Detection removedValue);
|
|
|
|
|
|
|
|
|
|
detectionStats.TryAdd(pl.ClientId, new Cheat.Detection(Log));
|
2018-02-26 23:24:19 -05:00
|
|
|
|
|
2018-02-07 00:19:06 -05:00
|
|
|
|
return clientStats;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-10 01:26:38 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Perform stat updates for disconnecting client
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="pl">Disconnecting client</param>
|
|
|
|
|
/// <returns></returns>
|
2018-02-07 00:19:06 -05:00
|
|
|
|
public async Task RemovePlayer(Player pl)
|
|
|
|
|
{
|
2018-03-06 02:22:19 -05:00
|
|
|
|
Log.WriteInfo($"Removing {pl} from stats");
|
|
|
|
|
|
2018-02-07 00:19:06 -05:00
|
|
|
|
int serverId = pl.CurrentServer.GetHashCode();
|
|
|
|
|
var playerStats = Servers[serverId].PlayerStats;
|
2018-02-26 23:24:19 -05:00
|
|
|
|
var detectionStats = Servers[serverId].PlayerDetections;
|
2018-02-10 01:26:38 -05:00
|
|
|
|
var serverStats = Servers[serverId].ServerStatistics;
|
|
|
|
|
var statsSvc = ContextThreads[serverId];
|
|
|
|
|
|
2018-03-06 02:22:19 -05:00
|
|
|
|
if (!playerStats.ContainsKey(pl.ClientId))
|
|
|
|
|
{
|
|
|
|
|
Log.WriteWarning($"Client disconnecting not in stats {pl}");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-07 00:19:06 -05:00
|
|
|
|
// get individual client's stats
|
2018-03-06 02:22:19 -05:00
|
|
|
|
var clientStats = playerStats[pl.ClientId];
|
|
|
|
|
// sync their score
|
|
|
|
|
clientStats.SessionScore = pl.Score;
|
2018-02-07 00:19:06 -05:00
|
|
|
|
// remove the client from the stats dictionary as they're leaving
|
2018-03-06 02:22:19 -05:00
|
|
|
|
playerStats.TryRemove(pl.ClientId, out EFClientStatistics removedValue);
|
|
|
|
|
detectionStats.TryRemove(pl.ClientId, out Cheat.Detection removedValue2);
|
2018-02-09 02:21:25 -05:00
|
|
|
|
|
2018-02-10 01:26:38 -05:00
|
|
|
|
// sync their stats before they leave
|
|
|
|
|
UpdateStats(clientStats);
|
|
|
|
|
|
|
|
|
|
// todo: should this be saved every disconnect?
|
|
|
|
|
await statsSvc.ClientStatSvc.SaveChangesAsync();
|
|
|
|
|
// increment the total play time
|
2018-02-09 02:21:25 -05:00
|
|
|
|
serverStats.TotalPlayTime += (int)(DateTime.UtcNow - pl.LastConnection).TotalSeconds;
|
2018-02-15 23:01:28 -05:00
|
|
|
|
await statsSvc.ServerStatsSvc.SaveChangesAsync();
|
2018-02-07 00:19:06 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Process stats for kill event
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2018-02-08 02:23:45 -05:00
|
|
|
|
public async Task AddScriptKill(Player attacker, Player victim, int serverId, string map, string hitLoc, string type,
|
2018-03-22 14:50:09 -04:00
|
|
|
|
string damage, string weapon, string killOrigin, string deathOrigin, string viewAngles, string offset)
|
2018-02-07 00:19:06 -05:00
|
|
|
|
{
|
2018-02-10 01:26:38 -05:00
|
|
|
|
var statsSvc = ContextThreads[serverId];
|
2018-02-26 23:24:19 -05:00
|
|
|
|
|
2018-02-07 00:19:06 -05:00
|
|
|
|
var kill = new EFClientKill()
|
|
|
|
|
{
|
|
|
|
|
Active = true,
|
|
|
|
|
AttackerId = attacker.ClientId,
|
|
|
|
|
VictimId = victim.ClientId,
|
|
|
|
|
ServerId = serverId,
|
|
|
|
|
Map = ParseEnum<IW4Info.MapName>.Get(map, typeof(IW4Info.MapName)),
|
|
|
|
|
DeathOrigin = Vector3.Parse(deathOrigin),
|
|
|
|
|
KillOrigin = Vector3.Parse(killOrigin),
|
|
|
|
|
DeathType = ParseEnum<IW4Info.MeansOfDeath>.Get(type, typeof(IW4Info.MeansOfDeath)),
|
|
|
|
|
Damage = Int32.Parse(damage),
|
|
|
|
|
HitLoc = ParseEnum<IW4Info.HitLocation>.Get(hitLoc, typeof(IW4Info.HitLocation)),
|
2018-03-22 14:50:09 -04:00
|
|
|
|
Weapon = ParseEnum<IW4Info.WeaponName>.Get(weapon, typeof(IW4Info.WeaponName)),
|
|
|
|
|
ViewAngles = Vector3.Parse(viewAngles).FixIW4Angles(),
|
|
|
|
|
TimeOffset = Int64.Parse(offset),
|
|
|
|
|
When = DateTime.UtcNow
|
2018-02-07 00:19:06 -05:00
|
|
|
|
};
|
|
|
|
|
|
2018-03-06 02:22:19 -05:00
|
|
|
|
if (kill.DeathType == IW4Info.MeansOfDeath.MOD_SUICIDE &&
|
2018-03-13 17:30:22 -04:00
|
|
|
|
kill.Damage == 100000)
|
2018-03-06 02:22:19 -05:00
|
|
|
|
{
|
|
|
|
|
// suicide by switching teams so let's not count it against them
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await AddStandardKill(attacker, victim);
|
|
|
|
|
|
|
|
|
|
var playerDetection = Servers[serverId].PlayerDetections[attacker.ClientId];
|
|
|
|
|
var playerStats = Servers[serverId].PlayerStats[attacker.ClientId];
|
|
|
|
|
|
|
|
|
|
// increment their hit count
|
|
|
|
|
if (kill.DeathType == IW4Info.MeansOfDeath.MOD_PISTOL_BULLET ||
|
2018-03-22 14:50:09 -04:00
|
|
|
|
kill.DeathType == IW4Info.MeansOfDeath.MOD_RIFLE_BULLET ||
|
|
|
|
|
kill.DeathType == IW4Info.MeansOfDeath.MOD_HEAD_SHOT)
|
2018-03-06 02:22:19 -05:00
|
|
|
|
{
|
|
|
|
|
playerStats.HitLocations.Single(hl => hl.Location == kill.HitLoc).HitCount += 1;
|
|
|
|
|
await statsSvc.ClientStatSvc.SaveChangesAsync();
|
|
|
|
|
}
|
2018-02-26 23:24:19 -05:00
|
|
|
|
|
2018-03-22 14:50:09 -04:00
|
|
|
|
statsSvc.KillStatsSvc.Insert(kill);
|
|
|
|
|
await statsSvc.KillStatsSvc.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
if (Plugin.Config.Configuration().EnableAntiCheat)
|
2018-03-06 02:22:19 -05:00
|
|
|
|
{
|
2018-03-13 17:30:22 -04:00
|
|
|
|
async Task executePenalty(Cheat.DetectionPenaltyResult penalty)
|
2018-03-06 02:22:19 -05:00
|
|
|
|
{
|
2018-03-13 17:30:22 -04:00
|
|
|
|
switch (penalty.ClientPenalty)
|
|
|
|
|
{
|
|
|
|
|
case Penalty.PenaltyType.Ban:
|
|
|
|
|
await attacker.Ban("You appear to be cheating", new Player() { ClientId = 1 });
|
|
|
|
|
break;
|
|
|
|
|
case Penalty.PenaltyType.Flag:
|
|
|
|
|
if (attacker.Level != Player.Permission.User)
|
|
|
|
|
break;
|
|
|
|
|
var flagCmd = new CFlag();
|
|
|
|
|
await flagCmd.ExecuteAsync(new Event(Event.GType.Flag, $"{(int)penalty.Bone}-{Math.Round(penalty.RatioAmount, 2).ToString()}@{penalty.KillCount}", new Player()
|
|
|
|
|
{
|
|
|
|
|
ClientId = 1,
|
|
|
|
|
Level = Player.Permission.Console,
|
|
|
|
|
ClientNumber = -1,
|
|
|
|
|
CurrentServer = attacker.CurrentServer
|
|
|
|
|
}, attacker, attacker.CurrentServer));
|
2018-03-06 02:22:19 -05:00
|
|
|
|
break;
|
2018-03-13 17:30:22 -04:00
|
|
|
|
}
|
2018-03-06 02:22:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-13 17:30:22 -04:00
|
|
|
|
await executePenalty(playerDetection.ProcessKill(kill));
|
|
|
|
|
await executePenalty(playerDetection.ProcessTotalRatio(playerStats));
|
|
|
|
|
}
|
2018-02-07 00:19:06 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-10 23:33:42 -05:00
|
|
|
|
public async Task AddStandardKill(Player attacker, Player victim)
|
2018-02-07 00:19:06 -05:00
|
|
|
|
{
|
2018-02-09 02:21:25 -05:00
|
|
|
|
int serverId = attacker.CurrentServer.GetHashCode();
|
2018-03-06 02:22:19 -05:00
|
|
|
|
EFClientStatistics attackerStats = null;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
attackerStats = Servers[serverId].PlayerStats[attacker.ClientId];
|
|
|
|
|
}
|
2018-02-10 23:33:42 -05:00
|
|
|
|
|
2018-03-06 02:22:19 -05:00
|
|
|
|
catch (KeyNotFoundException)
|
2018-02-10 23:33:42 -05:00
|
|
|
|
{
|
2018-03-06 02:22:19 -05:00
|
|
|
|
Log.WriteError($"[Stats::AddStandardKill] kill attacker ClientId is invalid {attacker.ClientId}-{attacker}");
|
2018-02-10 23:33:42 -05:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-06 02:22:19 -05:00
|
|
|
|
EFClientStatistics victimStats = null;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
victimStats = Servers[serverId].PlayerStats[victim.ClientId];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (KeyNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
Log.WriteError($"[Stats::AddStandardKill] kill victim ClientId is invalid {victim.ClientId}-{victim}");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-02-08 02:23:45 -05:00
|
|
|
|
|
2018-02-09 02:21:25 -05:00
|
|
|
|
// update the total stats
|
|
|
|
|
Servers[serverId].ServerStatistics.TotalKills += 1;
|
|
|
|
|
|
2018-03-06 02:22:19 -05:00
|
|
|
|
attackerStats.SessionScore = attacker.Score;
|
|
|
|
|
victimStats.SessionScore = victim.Score;
|
|
|
|
|
|
2018-02-09 02:21:25 -05:00
|
|
|
|
// calculate for the clients
|
2018-02-08 02:23:45 -05:00
|
|
|
|
CalculateKill(attackerStats, victimStats);
|
2018-02-09 02:21:25 -05:00
|
|
|
|
|
2018-02-10 23:33:42 -05:00
|
|
|
|
// show encouragement/discouragement
|
2018-02-15 23:01:28 -05:00
|
|
|
|
string streakMessage = (attackerStats.ClientId != victimStats.ClientId) ?
|
2018-03-18 22:25:11 -04:00
|
|
|
|
StreakMessage.MessageOnStreak(attackerStats.KillStreak, attackerStats.DeathStreak) :
|
|
|
|
|
StreakMessage.MessageOnStreak(-1, -1);
|
2018-02-10 23:33:42 -05:00
|
|
|
|
|
|
|
|
|
if (streakMessage != string.Empty)
|
|
|
|
|
await attacker.Tell(streakMessage);
|
|
|
|
|
|
2018-02-09 02:21:25 -05:00
|
|
|
|
// immediately write changes in debug
|
2018-02-15 23:01:28 -05:00
|
|
|
|
//#if DEBUG
|
2018-02-10 01:26:38 -05:00
|
|
|
|
var statsSvc = ContextThreads[serverId];
|
|
|
|
|
statsSvc.ClientStatSvc.SaveChanges();
|
2018-02-15 23:01:28 -05:00
|
|
|
|
//statsSvc.ServerStatsSvc.SaveChanges();
|
|
|
|
|
//#endif
|
2018-02-08 02:23:45 -05: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)
|
|
|
|
|
{
|
2018-02-10 23:33:42 -05:00
|
|
|
|
bool suicide = attackerStats.ClientId == victimStats.ClientId;
|
|
|
|
|
|
|
|
|
|
// only update their kills if they didn't kill themselves
|
|
|
|
|
if (!suicide)
|
|
|
|
|
{
|
|
|
|
|
attackerStats.Kills += 1;
|
|
|
|
|
attackerStats.SessionKills += 1;
|
|
|
|
|
attackerStats.KillStreak += 1;
|
|
|
|
|
attackerStats.DeathStreak = 0;
|
|
|
|
|
}
|
2018-02-08 02:23:45 -05:00
|
|
|
|
|
|
|
|
|
victimStats.Deaths += 1;
|
|
|
|
|
victimStats.SessionDeaths += 1;
|
|
|
|
|
victimStats.DeathStreak += 1;
|
|
|
|
|
victimStats.KillStreak = 0;
|
|
|
|
|
|
|
|
|
|
// process the attacker's stats after the kills
|
|
|
|
|
UpdateStats(attackerStats);
|
|
|
|
|
|
2018-02-09 02:21:25 -05:00
|
|
|
|
// update after calculation
|
2018-02-10 23:33:42 -05:00
|
|
|
|
attackerStats.TimePlayed += (int)(DateTime.UtcNow - attackerStats.LastActive).TotalSeconds;
|
|
|
|
|
victimStats.TimePlayed += (int)(DateTime.UtcNow - victimStats.LastActive).TotalSeconds;
|
2018-02-09 02:21:25 -05:00
|
|
|
|
attackerStats.LastActive = DateTime.UtcNow;
|
|
|
|
|
victimStats.LastActive = DateTime.UtcNow;
|
2018-02-08 02:23:45 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Update the client stats (skill etc)
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="clientStats">Client statistics</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private EFClientStatistics UpdateStats(EFClientStatistics clientStats)
|
|
|
|
|
{
|
|
|
|
|
double timeSinceLastCalc = (DateTime.UtcNow - clientStats.LastStatCalculation).TotalSeconds / 60.0;
|
2018-02-09 02:21:25 -05:00
|
|
|
|
double timeSinceLastActive = (DateTime.UtcNow - clientStats.LastActive).TotalSeconds / 60.0;
|
2018-02-08 02:23:45 -05:00
|
|
|
|
|
2018-02-09 02:21:25 -05:00
|
|
|
|
// prevent NaN or inactive time lowering SPM
|
|
|
|
|
if (timeSinceLastCalc == 0 || timeSinceLastActive > 3)
|
|
|
|
|
return clientStats;
|
2018-02-08 02:23:45 -05:00
|
|
|
|
|
2018-02-09 02:21:25 -05:00
|
|
|
|
// calculate the players Score Per Minute for the current session
|
2018-03-06 02:22:19 -05:00
|
|
|
|
int currentScore = clientStats.SessionScore;
|
2018-02-09 02:21:25 -05:00
|
|
|
|
double killSPM = currentScore / (timeSinceLastCalc * 60.0);
|
2018-02-08 02:23:45 -05:00
|
|
|
|
|
2018-02-09 02:21:25 -05:00
|
|
|
|
// calculate how much the KDR should weigh
|
|
|
|
|
// 1.637 is a Eddie-Generated number that weights the KDR nicely
|
|
|
|
|
double KDRWeight = Math.Round(Math.Pow(clientStats.KDR, 1.637 / Math.E), 3);
|
2018-02-08 02:23:45 -05:00
|
|
|
|
|
2018-02-09 02:21:25 -05:00
|
|
|
|
// if no SPM, weight is 1 else the weight ishe current session's spm / lifetime average score per minute
|
|
|
|
|
double SPMWeightAgainstAverage = (clientStats.SPM < 1) ? 1 : killSPM / clientStats.SPM;
|
2018-02-08 02:23:45 -05:00
|
|
|
|
|
2018-02-09 02:21:25 -05:00
|
|
|
|
// calculate the weight of the new play time against last 10 hours of gameplay
|
2018-02-15 23:01:28 -05:00
|
|
|
|
int totalPlayTime = (clientStats.TimePlayed == 0) ?
|
|
|
|
|
(int)(DateTime.UtcNow - clientStats.LastActive).TotalSeconds :
|
|
|
|
|
clientStats.TimePlayed + (int)(DateTime.UtcNow - clientStats.LastActive).TotalSeconds;
|
2018-02-08 02:23:45 -05:00
|
|
|
|
|
2018-02-15 23:01:28 -05:00
|
|
|
|
double SPMAgainstPlayWeight = timeSinceLastCalc / Math.Min(600, (totalPlayTime / 60.0));
|
2018-02-08 02:23:45 -05:00
|
|
|
|
|
2018-02-09 02:21:25 -05:00
|
|
|
|
// calculate the new weight against average times the weight against play time
|
|
|
|
|
clientStats.SPM = (killSPM * SPMAgainstPlayWeight) + (clientStats.SPM * (1 - SPMAgainstPlayWeight));
|
|
|
|
|
clientStats.SPM = Math.Round(clientStats.SPM, 3);
|
|
|
|
|
clientStats.Skill = Math.Round((clientStats.SPM * KDRWeight), 3);
|
2018-02-08 02:23:45 -05:00
|
|
|
|
|
2018-02-09 02:21:25 -05:00
|
|
|
|
clientStats.LastStatCalculation = DateTime.UtcNow;
|
|
|
|
|
clientStats.LastScore = currentScore;
|
2018-02-08 02:23:45 -05:00
|
|
|
|
|
|
|
|
|
return clientStats;
|
2018-02-07 00:19:06 -05:00
|
|
|
|
}
|
2018-02-09 02:21:25 -05:00
|
|
|
|
|
|
|
|
|
public void InitializeServerStats(Server sv)
|
|
|
|
|
{
|
|
|
|
|
int serverId = sv.GetHashCode();
|
2018-02-10 01:26:38 -05:00
|
|
|
|
var statsSvc = ContextThreads[serverId];
|
|
|
|
|
|
|
|
|
|
var serverStats = statsSvc.ServerStatsSvc.Find(s => s.ServerId == serverId).FirstOrDefault();
|
2018-02-09 02:21:25 -05:00
|
|
|
|
if (serverStats == null)
|
|
|
|
|
{
|
|
|
|
|
Log.WriteDebug($"Initializing server stats for {sv}");
|
|
|
|
|
// server stats have never been generated before
|
|
|
|
|
serverStats = new EFServerStatistics()
|
|
|
|
|
{
|
|
|
|
|
Active = true,
|
|
|
|
|
ServerId = serverId,
|
|
|
|
|
TotalKills = 0,
|
|
|
|
|
TotalPlayTime = 0,
|
|
|
|
|
};
|
|
|
|
|
|
2018-02-10 01:26:38 -05:00
|
|
|
|
var ieClientStats = statsSvc.ClientStatSvc.Find(cs => cs.ServerId == serverId);
|
2018-02-09 02:21:25 -05:00
|
|
|
|
|
|
|
|
|
// set these incase they've we've imported settings
|
|
|
|
|
serverStats.TotalKills = ieClientStats.Sum(cs => cs.Kills);
|
|
|
|
|
serverStats.TotalPlayTime = Manager.GetClientService().GetTotalPlayTime().Result;
|
|
|
|
|
|
2018-02-10 01:26:38 -05:00
|
|
|
|
statsSvc.ServerStatsSvc.Insert(serverStats);
|
2018-02-09 02:21:25 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-06 02:22:19 -05:00
|
|
|
|
public void ResetKillstreaks(int serverId)
|
|
|
|
|
{
|
|
|
|
|
var serverStats = Servers[serverId];
|
|
|
|
|
foreach (var stat in serverStats.PlayerStats.Values)
|
|
|
|
|
{
|
|
|
|
|
stat.KillStreak = 0;
|
|
|
|
|
stat.DeathStreak = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ResetStats(int clientId, int serverId)
|
|
|
|
|
{
|
|
|
|
|
var stats = Servers[serverId].PlayerStats[clientId];
|
|
|
|
|
stats.Kills = 0;
|
|
|
|
|
stats.Deaths = 0;
|
|
|
|
|
stats.SPM = 0;
|
|
|
|
|
stats.Skill = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-10 01:26:38 -05:00
|
|
|
|
public async Task AddMessageAsync(int clientId, int serverId, string message)
|
2018-02-09 02:21:25 -05:00
|
|
|
|
{
|
2018-02-11 20:17:20 -05:00
|
|
|
|
// the web users can have no account
|
|
|
|
|
if (clientId < 1)
|
|
|
|
|
return;
|
|
|
|
|
|
2018-02-10 01:26:38 -05:00
|
|
|
|
var messageSvc = ContextThreads[serverId].MessageSvc;
|
|
|
|
|
messageSvc.Insert(new EFClientMessage()
|
|
|
|
|
{
|
|
|
|
|
Active = true,
|
|
|
|
|
ClientId = clientId,
|
|
|
|
|
Message = message,
|
|
|
|
|
ServerId = serverId,
|
|
|
|
|
TimeSent = DateTime.UtcNow
|
|
|
|
|
});
|
|
|
|
|
await messageSvc.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task Sync(Server sv)
|
|
|
|
|
{
|
|
|
|
|
int serverId = sv.GetHashCode();
|
|
|
|
|
var statsSvc = ContextThreads[serverId];
|
|
|
|
|
|
2018-02-09 02:21:25 -05:00
|
|
|
|
Log.WriteDebug("Syncing server stats");
|
2018-02-10 01:26:38 -05:00
|
|
|
|
await statsSvc.ServerStatsSvc.SaveChangesAsync();
|
2018-02-09 02:21:25 -05:00
|
|
|
|
|
|
|
|
|
Log.WriteDebug("Syncing client stats");
|
2018-02-10 01:26:38 -05:00
|
|
|
|
await statsSvc.ClientStatSvc.SaveChangesAsync();
|
2018-02-09 02:21:25 -05:00
|
|
|
|
|
|
|
|
|
Log.WriteDebug("Syncing kill stats");
|
2018-02-10 01:26:38 -05:00
|
|
|
|
await statsSvc.KillStatsSvc.SaveChangesAsync();
|
2018-02-09 02:21:25 -05:00
|
|
|
|
|
|
|
|
|
Log.WriteDebug("Syncing servers");
|
2018-02-10 01:26:38 -05:00
|
|
|
|
await statsSvc.ServerSvc.SaveChangesAsync();
|
2018-02-09 02:21:25 -05:00
|
|
|
|
}
|
2018-02-07 00:19:06 -05:00
|
|
|
|
}
|
|
|
|
|
}
|