update stats plugin to IPluginV2

This commit is contained in:
RaidMax 2023-02-11 21:01:28 -06:00
parent 7b8f6421aa
commit 66c0561e7f
19 changed files with 1388 additions and 1326 deletions

View File

@ -1,6 +1,5 @@
using SharedLibraryCore.Database.Models; using SharedLibraryCore.Database.Models;
using SharedLibraryCore.Helpers; using SharedLibraryCore.Helpers;
using SharedLibraryCore.Interfaces;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -10,6 +9,7 @@ using Data.Models.Client;
using Data.Models.Client.Stats; using Data.Models.Client.Stats;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using SharedLibraryCore; using SharedLibraryCore;
using Stats.Config;
using ILogger = Microsoft.Extensions.Logging.ILogger; using ILogger = Microsoft.Extensions.Logging.ILogger;
namespace IW4MAdmin.Plugins.Stats.Cheat namespace IW4MAdmin.Plugins.Stats.Cheat
@ -37,6 +37,7 @@ namespace IW4MAdmin.Plugins.Stats.Cheat
Dictionary<IW4Info.HitLocation, HitInfo> HitLocationCount; Dictionary<IW4Info.HitLocation, HitInfo> HitLocationCount;
double AngleDifferenceAverage; double AngleDifferenceAverage;
EFClientStatistics ClientStats; EFClientStatistics ClientStats;
private readonly StatsConfiguration _statsConfiguration;
long LastOffset; long LastOffset;
string LastWeapon; string LastWeapon;
ILogger Log; ILogger Log;
@ -55,7 +56,7 @@ namespace IW4MAdmin.Plugins.Stats.Cheat
public double Offset { get; set; } public double Offset { get; set; }
}; };
public Detection(ILogger log, EFClientStatistics clientStats) public Detection(ILogger log, EFClientStatistics clientStats, StatsConfiguration statsConfiguration)
{ {
Log = log; Log = log;
HitLocationCount = new Dictionary<IW4Info.HitLocation, HitInfo>(); HitLocationCount = new Dictionary<IW4Info.HitLocation, HitInfo>();
@ -65,6 +66,7 @@ namespace IW4MAdmin.Plugins.Stats.Cheat
} }
ClientStats = clientStats; ClientStats = clientStats;
_statsConfiguration = statsConfiguration;
Strain = new Strain(); Strain = new Strain();
Tracker = new ChangeTracking<EFACSnapshot>(); Tracker = new ChangeTracking<EFACSnapshot>();
TrackedHits = new List<EFClientKill>(); TrackedHits = new List<EFClientKill>();
@ -308,7 +310,7 @@ namespace IW4MAdmin.Plugins.Stats.Cheat
bool shouldIgnoreDetection = false; bool shouldIgnoreDetection = false;
try try
{ {
shouldIgnoreDetection = Plugin.Config.Configuration().AnticheatConfiguration.IgnoredDetectionSpecification[(Server.Game)hit.GameName][DetectionType.Recoil] shouldIgnoreDetection = _statsConfiguration.AnticheatConfiguration.IgnoredDetectionSpecification[(Server.Game)hit.GameName][DetectionType.Recoil]
.Any(_weaponRegex => Regex.IsMatch(hit.WeaponReference, _weaponRegex)); .Any(_weaponRegex => Regex.IsMatch(hit.WeaponReference, _weaponRegex));
} }
@ -340,7 +342,7 @@ namespace IW4MAdmin.Plugins.Stats.Cheat
try try
{ {
shouldIgnoreDetection = false; shouldIgnoreDetection = false;
shouldIgnoreDetection = Plugin.Config.Configuration().AnticheatConfiguration.IgnoredDetectionSpecification[(Server.Game)hit.GameName][DetectionType.Button] shouldIgnoreDetection = _statsConfiguration.AnticheatConfiguration.IgnoredDetectionSpecification[(Server.Game)hit.GameName][DetectionType.Button]
.Any(_weaponRegex => Regex.IsMatch(hit.WeaponReference, _weaponRegex)); .Any(_weaponRegex => Regex.IsMatch(hit.WeaponReference, _weaponRegex));
} }
@ -453,7 +455,7 @@ namespace IW4MAdmin.Plugins.Stats.Cheat
try try
{ {
shouldIgnoreDetection = false; // reset previous value shouldIgnoreDetection = false; // reset previous value
shouldIgnoreDetection = Plugin.Config.Configuration().AnticheatConfiguration.IgnoredDetectionSpecification[(Server.Game)hit.GameName][DetectionType.Chest] shouldIgnoreDetection = _statsConfiguration.AnticheatConfiguration.IgnoredDetectionSpecification[(Server.Game)hit.GameName][DetectionType.Chest]
.Any(_weaponRegex => Regex.IsMatch(hit.WeaponReference, _weaponRegex)); .Any(_weaponRegex => Regex.IsMatch(hit.WeaponReference, _weaponRegex));
} }

View File

@ -1,11 +1,11 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using SharedLibraryCore; using SharedLibraryCore.Events;
namespace IW4MAdmin.Plugins.Stats.Client.Abstractions namespace IW4MAdmin.Plugins.Stats.Client.Abstractions
{ {
public interface IClientStatisticCalculator public interface IClientStatisticCalculator
{ {
Task GatherDependencies(); Task GatherDependencies();
Task CalculateForEvent(GameEvent gameEvent); Task CalculateForEvent(CoreEvent coreEvent);
} }
} }

View File

@ -1,11 +1,11 @@
using IW4MAdmin.Plugins.Stats.Client.Game; using Data.Models;
using SharedLibraryCore; using IW4MAdmin.Plugins.Stats.Client.Game;
using SharedLibraryCore.Interfaces; using SharedLibraryCore.Interfaces;
namespace Stats.Client.Abstractions namespace Stats.Client.Abstractions
{ {
public interface IHitInfoBuilder public interface IHitInfoBuilder
{ {
HitInfo Build(string[] log, ParserRegex parserRegex, int entityId, bool isSelf, bool isVictim, Server.Game gameName); HitInfo Build(string[] log, ParserRegex parserRegex, int entityId, bool isSelf, bool isVictim, Reference.Game gameName);
} }
} }

View File

@ -1,10 +1,9 @@
using SharedLibraryCore; using Data.Models;
using Stats.Client.Game; using Stats.Client.Game;
namespace Stats.Client.Abstractions namespace Stats.Client.Abstractions;
public interface IWeaponNameParser
{ {
public interface IWeaponNameParser WeaponInfo Parse(string weaponName, Reference.Game gameName);
{
WeaponInfo Parse(string weaponName, Server.Game gameName);
}
} }

File diff suppressed because it is too large Load Diff

View File

@ -3,82 +3,80 @@ using System.Linq;
using Data.Models; using Data.Models;
using IW4MAdmin.Plugins.Stats.Client.Game; using IW4MAdmin.Plugins.Stats.Client.Game;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using SharedLibraryCore;
using SharedLibraryCore.Interfaces; using SharedLibraryCore.Interfaces;
using Stats.Client.Abstractions; using Stats.Client.Abstractions;
using Stats.Client.Game; using Stats.Client.Game;
using ILogger = Microsoft.Extensions.Logging.ILogger; using ILogger = Microsoft.Extensions.Logging.ILogger;
namespace Stats.Client namespace Stats.Client;
public class HitInfoBuilder : IHitInfoBuilder
{ {
public class HitInfoBuilder : IHitInfoBuilder private readonly IWeaponNameParser _weaponNameParser;
private readonly ILogger _logger;
private const int MaximumDamage = 1000;
public HitInfoBuilder(ILogger<HitInfoBuilder> logger, IWeaponNameParser weaponNameParser)
{ {
private readonly IWeaponNameParser _weaponNameParser; _weaponNameParser = weaponNameParser;
private readonly ILogger _logger; _logger = logger;
private const int MaximumDamage = 1000; }
public HitInfoBuilder(ILogger<HitInfoBuilder> logger, IWeaponNameParser weaponNameParser) public HitInfo Build(string[] log, ParserRegex parserRegex, int entityId, bool isSelf, bool isVictim,
Reference.Game gameName)
{
var eventType = log[(uint)ParserRegex.GroupType.EventType].First();
HitType hitType;
if (isVictim)
{ {
_weaponNameParser = weaponNameParser; if (isSelf)
_logger = logger;
}
public HitInfo Build(string[] log, ParserRegex parserRegex, int entityId, bool isSelf, bool isVictim,
Server.Game gameName)
{
var eventType = log[(uint) ParserRegex.GroupType.EventType].First();
HitType hitType;
if (isVictim)
{ {
if (isSelf) hitType = HitType.Suicide;
{
hitType = HitType.Suicide;
}
else
{
hitType = eventType == 'D' ? HitType.WasDamaged : HitType.WasKilled;
}
} }
else else
{ {
hitType = eventType == 'D' ? HitType.Damage : HitType.Kill; hitType = eventType == 'D' ? HitType.WasDamaged : HitType.WasKilled;
} }
var damage = 0;
try
{
damage = Math.Min(MaximumDamage,
log.Length > parserRegex.GroupMapping[ParserRegex.GroupType.Damage]
? int.Parse(log[parserRegex.GroupMapping[ParserRegex.GroupType.Damage]])
: 0);
}
catch
{
// ignored
}
var hitInfo = new HitInfo()
{
EntityId = entityId,
IsVictim = isVictim,
HitType = hitType,
Damage = damage,
Location = log.Length > parserRegex.GroupMapping[ParserRegex.GroupType.HitLocation]
? log[parserRegex.GroupMapping[ParserRegex.GroupType.HitLocation]]
: "Unknown",
Weapon = log.Length > parserRegex.GroupMapping[ParserRegex.GroupType.Weapon]
? _weaponNameParser.Parse(log[parserRegex.GroupMapping[ParserRegex.GroupType.Weapon]], gameName)
: new WeaponInfo {Name = "Unknown"},
MeansOfDeath = log.Length > parserRegex.GroupMapping[ParserRegex.GroupType.MeansOfDeath]
? log[parserRegex.GroupMapping[ParserRegex.GroupType.MeansOfDeath]]
: "Unknown",
Game = (Reference.Game) gameName
};
return hitInfo;
} }
else
{
hitType = eventType == 'D' ? HitType.Damage : HitType.Kill;
}
var damage = 0;
try
{
damage = Math.Min(MaximumDamage,
log.Length > parserRegex.GroupMapping[ParserRegex.GroupType.Damage]
? int.Parse(log[parserRegex.GroupMapping[ParserRegex.GroupType.Damage]])
: 0);
}
catch
{
// ignored
}
var hitInfo = new HitInfo()
{
EntityId = entityId,
IsVictim = isVictim,
HitType = hitType,
Damage = damage,
Location = log.Length > parserRegex.GroupMapping[ParserRegex.GroupType.HitLocation]
? log[parserRegex.GroupMapping[ParserRegex.GroupType.HitLocation]]
: "Unknown",
Weapon = log.Length > parserRegex.GroupMapping[ParserRegex.GroupType.Weapon]
? _weaponNameParser.Parse(log[parserRegex.GroupMapping[ParserRegex.GroupType.Weapon]], gameName)
: new WeaponInfo { Name = "Unknown" },
MeansOfDeath = log.Length > parserRegex.GroupMapping[ParserRegex.GroupType.MeansOfDeath]
? log[parserRegex.GroupMapping[ParserRegex.GroupType.MeansOfDeath]]
: "Unknown",
Game = gameName
};
return hitInfo;
} }
} }

View File

@ -3,7 +3,7 @@ using Stats.Client.Abstractions;
using Stats.Client.Game; using Stats.Client.Game;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using SharedLibraryCore; using Data.Models;
using Stats.Config; using Stats.Config;
using ILogger = Microsoft.Extensions.Logging.ILogger; using ILogger = Microsoft.Extensions.Logging.ILogger;
@ -20,7 +20,7 @@ namespace Stats.Client
_config = config; _config = config;
} }
public WeaponInfo Parse(string weaponName, Server.Game gameName) public WeaponInfo Parse(string weaponName, Reference.Game gameName)
{ {
var configForGame = _config.WeaponNameParserConfigurations var configForGame = _config.WeaponNameParserConfigurations
?.FirstOrDefault(config => config.Game == gameName) ?? new WeaponNameParserConfiguration() ?.FirstOrDefault(config => config.Game == gameName) ?? new WeaponNameParserConfiguration()

View File

@ -12,69 +12,70 @@ using SharedLibraryCore.Interfaces;
using IW4MAdmin.Plugins.Stats.Helpers; using IW4MAdmin.Plugins.Stats.Helpers;
using Stats.Config; using Stats.Config;
namespace IW4MAdmin.Plugins.Stats.Commands namespace IW4MAdmin.Plugins.Stats.Commands;
class MostKillsCommand : Command
{ {
class MostKillsCommand : Command private readonly IDatabaseContextFactory _contextFactory;
private readonly StatsConfiguration _statsConfig;
public MostKillsCommand(CommandConfiguration config, ITranslationLookup translationLookup,
IDatabaseContextFactory contextFactory, StatsConfiguration statsConfig) : base(config, translationLookup)
{ {
private readonly IDatabaseContextFactory _contextFactory; Name = "mostkills";
Description = translationLookup["PLUGINS_STATS_COMMANDS_MOSTKILLS_DESC"];
Alias = "mk";
Permission = EFClient.Permission.User;
public MostKillsCommand(CommandConfiguration config, ITranslationLookup translationLookup, _contextFactory = contextFactory;
IDatabaseContextFactory contextFactory) : base(config, translationLookup) _statsConfig = statsConfig;
}
public override async Task ExecuteAsync(GameEvent gameEvent)
{
var mostKills = await GetMostKills(StatManager.GetIdForServer(gameEvent.Owner), _statsConfig,
_contextFactory, _translationLookup);
if (!gameEvent.Message.IsBroadcastCommand(_config.BroadcastCommandPrefix))
{ {
Name = "mostkills"; await gameEvent.Origin.TellAsync(mostKills, gameEvent.Owner.Manager.CancellationToken);
Description = translationLookup["PLUGINS_STATS_COMMANDS_MOSTKILLS_DESC"];
Alias = "mk";
Permission = EFClient.Permission.User;
_contextFactory = contextFactory;
} }
public override async Task ExecuteAsync(GameEvent gameEvent) else
{ {
var mostKills = await GetMostKills(StatManager.GetIdForServer(gameEvent.Owner), Plugin.Config.Configuration(), foreach (var stat in mostKills)
_contextFactory, _translationLookup);
if (!gameEvent.Message.IsBroadcastCommand(_config.BroadcastCommandPrefix))
{ {
await gameEvent.Origin.TellAsync(mostKills, gameEvent.Owner.Manager.CancellationToken); await gameEvent.Owner.Broadcast(stat).WaitAsync(Utilities.DefaultCommandTimeout,
gameEvent.Owner.Manager.CancellationToken);
} }
else
{
foreach (var stat in mostKills)
{
await gameEvent.Owner.Broadcast(stat).WaitAsync(Utilities.DefaultCommandTimeout,
gameEvent.Owner.Manager.CancellationToken);
}
}
}
public static async Task<IEnumerable<string>> GetMostKills(long? serverId, StatsConfiguration config,
IDatabaseContextFactory contextFactory, ITranslationLookup translationLookup)
{
await using var ctx = contextFactory.CreateContext(enableTracking: false);
var dayInPast = DateTime.UtcNow.AddDays(-config.MostKillsMaxInactivityDays);
var iqStats = (from stats in ctx.Set<EFClientStatistics>()
join client in ctx.Clients
on stats.ClientId equals client.ClientId
join alias in ctx.Aliases
on client.CurrentAliasId equals alias.AliasId
where stats.ServerId == serverId
where client.Level != EFClient.Permission.Banned
where client.LastConnection >= dayInPast
orderby stats.Kills descending
select new
{
alias.Name,
stats.Kills
})
.Take(config.MostKillsClientLimit);
var iqList = await iqStats.ToListAsync();
return iqList.Select((stats, index) => translationLookup["PLUGINS_STATS_COMMANDS_MOSTKILLS_FORMAT_V2"]
.FormatExt(index + 1, stats.Name, stats.Kills))
.Prepend(Utilities.CurrentLocalization.LocalizationIndex["PLUGINS_STATS_COMMANDS_MOSTKILLS_HEADER"]);
} }
} }
public static async Task<IEnumerable<string>> GetMostKills(long? serverId, StatsConfiguration config,
IDatabaseContextFactory contextFactory, ITranslationLookup translationLookup)
{
await using var ctx = contextFactory.CreateContext(enableTracking: false);
var dayInPast = DateTime.UtcNow.AddDays(-config.MostKillsMaxInactivityDays);
var iqStats = (from stats in ctx.Set<EFClientStatistics>()
join client in ctx.Clients
on stats.ClientId equals client.ClientId
join alias in ctx.Aliases
on client.CurrentAliasId equals alias.AliasId
where stats.ServerId == serverId
where client.Level != EFClient.Permission.Banned
where client.LastConnection >= dayInPast
orderby stats.Kills descending
select new
{
alias.Name,
stats.Kills
})
.Take(config.MostKillsClientLimit);
var iqList = await iqStats.ToListAsync();
return iqList.Select((stats, index) => translationLookup["PLUGINS_STATS_COMMANDS_MOSTKILLS_FORMAT_V2"]
.FormatExt(index + 1, stats.Name, stats.Kills))
.Prepend(Utilities.CurrentLocalization.LocalizationIndex["PLUGINS_STATS_COMMANDS_MOSTKILLS_HEADER"]);
}
} }

View File

@ -7,15 +7,18 @@ using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Data.Abstractions; using Data.Abstractions;
using Data.Models.Client.Stats; using Data.Models.Client.Stats;
using IW4MAdmin.Plugins.Stats.Helpers;
using Stats.Config;
namespace IW4MAdmin.Plugins.Stats.Commands namespace IW4MAdmin.Plugins.Stats.Commands
{ {
public class ResetStats : Command public class ResetStats : Command
{ {
private readonly IDatabaseContextFactory _contextFactory; private readonly IDatabaseContextFactory _contextFactory;
private readonly StatManager _statManager;
public ResetStats(CommandConfiguration config, ITranslationLookup translationLookup, public ResetStats(CommandConfiguration config, ITranslationLookup translationLookup,
IDatabaseContextFactory contextFactory) : base(config, translationLookup) IDatabaseContextFactory contextFactory, StatManager statManager) : base(config, translationLookup)
{ {
Name = "resetstats"; Name = "resetstats";
Description = translationLookup["PLUGINS_STATS_COMMANDS_RESET_DESC"]; Description = translationLookup["PLUGINS_STATS_COMMANDS_RESET_DESC"];
@ -25,6 +28,7 @@ namespace IW4MAdmin.Plugins.Stats.Commands
AllowImpersonation = true; AllowImpersonation = true;
_contextFactory = contextFactory; _contextFactory = contextFactory;
_statManager = statManager;
} }
public override async Task ExecuteAsync(GameEvent gameEvent) public override async Task ExecuteAsync(GameEvent gameEvent)
@ -53,7 +57,7 @@ namespace IW4MAdmin.Plugins.Stats.Commands
} }
// reset the cached version // reset the cached version
Plugin.Manager.ResetStats(gameEvent.Origin); _statManager.ResetStats(gameEvent.Origin);
gameEvent.Origin.Tell(_translationLookup["PLUGINS_STATS_COMMANDS_RESET_SUCCESS"]); gameEvent.Origin.Tell(_translationLookup["PLUGINS_STATS_COMMANDS_RESET_SUCCESS"]);
} }

View File

@ -11,15 +11,15 @@ namespace IW4MAdmin.Plugins.Stats.Commands
{ {
public class TopStats : Command public class TopStats : Command
{ {
public static async Task<List<string>> GetTopStats(Server s, ITranslationLookup translationLookup) public static async Task<List<string>> GetTopStats(IGameServer server, ITranslationLookup translationLookup, StatManager statManager)
{ {
var serverId = StatManager.GetIdForServer(s); var serverId = StatManager.GetIdForServer(server);
var topStatsText = new List<string>() var topStatsText = new List<string>()
{ {
$"(Color::Accent)--{translationLookup["PLUGINS_STATS_COMMANDS_TOP_TEXT"]}--" $"(Color::Accent)--{translationLookup["PLUGINS_STATS_COMMANDS_TOP_TEXT"]}--"
}; };
var stats = await Plugin.Manager.GetTopStats(0, 5, serverId); var stats = await statManager.GetTopStats(0, 5, serverId);
var statsList = stats.Select((stats, index) => var statsList = stats.Select((stats, index) =>
translationLookup["COMMANDS_TOPSTATS_RESULT"] translationLookup["COMMANDS_TOPSTATS_RESULT"]
.FormatExt(index + 1, stats.Name, stats.KDR, stats.Performance)); .FormatExt(index + 1, stats.Name, stats.KDR, stats.Performance));
@ -39,8 +39,9 @@ namespace IW4MAdmin.Plugins.Stats.Commands
} }
private new readonly CommandConfiguration _config; private new readonly CommandConfiguration _config;
private readonly StatManager _statManager;
public TopStats(CommandConfiguration config, ITranslationLookup translationLookup) : base(config, public TopStats(CommandConfiguration config, ITranslationLookup translationLookup, StatManager statManager) : base(config,
translationLookup) translationLookup)
{ {
Name = "topstats"; Name = "topstats";
@ -50,11 +51,12 @@ namespace IW4MAdmin.Plugins.Stats.Commands
RequiresTarget = false; RequiresTarget = false;
_config = config; _config = config;
_statManager = statManager;
} }
public override async Task ExecuteAsync(GameEvent gameEvent) public override async Task ExecuteAsync(GameEvent gameEvent)
{ {
var topStats = await GetTopStats(gameEvent.Owner, _translationLookup); var topStats = await GetTopStats(gameEvent.Owner, _translationLookup, _statManager);
if (!gameEvent.Message.IsBroadcastCommand(_config.BroadcastCommandPrefix)) if (!gameEvent.Message.IsBroadcastCommand(_config.BroadcastCommandPrefix))
{ {
await gameEvent.Origin.TellAsync(topStats, gameEvent.Owner.Manager.CancellationToken); await gameEvent.Origin.TellAsync(topStats, gameEvent.Owner.Manager.CancellationToken);

View File

@ -15,9 +15,10 @@ namespace IW4MAdmin.Plugins.Stats.Commands
public class ViewStatsCommand : Command public class ViewStatsCommand : Command
{ {
private readonly IDatabaseContextFactory _contextFactory; private readonly IDatabaseContextFactory _contextFactory;
private readonly StatManager _statManager;
public ViewStatsCommand(CommandConfiguration config, ITranslationLookup translationLookup, public ViewStatsCommand(CommandConfiguration config, ITranslationLookup translationLookup,
IDatabaseContextFactory contextFactory) : base(config, translationLookup) IDatabaseContextFactory contextFactory, StatManager statManager) : base(config, translationLookup)
{ {
Name = "stats"; Name = "stats";
Description = translationLookup["PLUGINS_STATS_COMMANDS_VIEW_DESC"]; Description = translationLookup["PLUGINS_STATS_COMMANDS_VIEW_DESC"];
@ -34,6 +35,7 @@ namespace IW4MAdmin.Plugins.Stats.Commands
}; };
_contextFactory = contextFactory; _contextFactory = contextFactory;
_statManager = statManager;
} }
public override async Task ExecuteAsync(GameEvent E) public override async Task ExecuteAsync(GameEvent E)
@ -53,12 +55,12 @@ namespace IW4MAdmin.Plugins.Stats.Commands
var serverId = StatManager.GetIdForServer(E.Owner); var serverId = StatManager.GetIdForServer(E.Owner);
var totalRankedPlayers = await Plugin.Manager.GetTotalRankedPlayers(serverId); var totalRankedPlayers = await _statManager.GetTotalRankedPlayers(serverId);
// getting stats for a particular client // getting stats for a particular client
if (E.Target != null) if (E.Target != null)
{ {
var performanceRanking = await Plugin.Manager.GetClientOverallRanking(E.Target.ClientId, serverId); var performanceRanking = await _statManager.GetClientOverallRanking(E.Target.ClientId, serverId);
var performanceRankingString = performanceRanking == 0 var performanceRankingString = performanceRanking == 0
? _translationLookup["WEBFRONT_STATS_INDEX_UNRANKED"] ? _translationLookup["WEBFRONT_STATS_INDEX_UNRANKED"]
: $"{_translationLookup["WEBFRONT_STATS_INDEX_RANKED"]} (Color::Accent)#{performanceRanking}/{totalRankedPlayers}"; : $"{_translationLookup["WEBFRONT_STATS_INDEX_RANKED"]} (Color::Accent)#{performanceRanking}/{totalRankedPlayers}";
@ -87,7 +89,7 @@ namespace IW4MAdmin.Plugins.Stats.Commands
// getting self stats // getting self stats
else else
{ {
var performanceRanking = await Plugin.Manager.GetClientOverallRanking(E.Origin.ClientId, serverId); var performanceRanking = await _statManager.GetClientOverallRanking(E.Origin.ClientId, serverId);
var performanceRankingString = performanceRanking == 0 var performanceRankingString = performanceRanking == 0
? _translationLookup["WEBFRONT_STATS_INDEX_UNRANKED"] ? _translationLookup["WEBFRONT_STATS_INDEX_UNRANKED"]
: $"{_translationLookup["WEBFRONT_STATS_INDEX_RANKED"]} (Color::Accent)#{performanceRanking}/{totalRankedPlayers}"; : $"{_translationLookup["WEBFRONT_STATS_INDEX_RANKED"]} (Color::Accent)#{performanceRanking}/{totalRankedPlayers}";
@ -131,4 +133,4 @@ namespace IW4MAdmin.Plugins.Stats.Commands
} }
} }
} }
} }

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Data.Models;
using IW4MAdmin.Plugins.Stats.Config; using IW4MAdmin.Plugins.Stats.Config;
using SharedLibraryCore; using SharedLibraryCore;
using SharedLibraryCore.Interfaces; using SharedLibraryCore.Interfaces;
@ -21,26 +22,26 @@ namespace Stats.Config
public WeaponNameParserConfiguration[] WeaponNameParserConfigurations { get; set; } = { public WeaponNameParserConfiguration[] WeaponNameParserConfigurations { get; set; } = {
new() new()
{ {
Game = Server.Game.IW3, Game = Reference.Game.IW3,
WeaponSuffix = "mp", WeaponSuffix = "mp",
Delimiters = new[] {'_'} Delimiters = new[] {'_'}
}, },
new() new()
{ {
Game = Server.Game.IW4, Game = Reference.Game.IW4,
WeaponSuffix = "mp", WeaponSuffix = "mp",
Delimiters = new[] {'_'} Delimiters = new[] {'_'}
}, },
new() new()
{ {
Game = Server.Game.IW5, Game = Reference.Game.IW5,
WeaponSuffix = "mp", WeaponSuffix = "mp",
WeaponPrefix = "iw5", WeaponPrefix = "iw5",
Delimiters = new[] {'_'} Delimiters = new[] {'_'}
}, },
new() new()
{ {
Game = Server.Game.T6, Game = Reference.Game.T6,
WeaponSuffix = "mp", WeaponSuffix = "mp",
Delimiters = new[] {'_', '+'} Delimiters = new[] {'_', '+'}
} }

View File

@ -1,12 +1,12 @@
using SharedLibraryCore; using Data.Models;
namespace Stats.Config namespace Stats.Config
{ {
public class WeaponNameParserConfiguration public class WeaponNameParserConfiguration
{ {
public Server.Game Game { get; set; } public Reference.Game Game { get; set; }
public char[] Delimiters { get; set; } public char[] Delimiters { get; set; }
public string WeaponSuffix { get; set; } public string WeaponSuffix { get; set; }
public string WeaponPrefix { get; set; } public string WeaponPrefix { get; set; }
} }
} }

View File

@ -2,111 +2,101 @@
using SharedLibraryCore.Database.Models; using SharedLibraryCore.Database.Models;
using SharedLibraryCore.Interfaces; using SharedLibraryCore.Interfaces;
using System.Collections.Generic; using System.Collections.Generic;
using EventGeneratorCallback = System.ValueTuple<string, string, using SharedLibraryCore.Events.Game;
using EventGeneratorCallback = System.ValueTuple<string, string,
System.Func<string, SharedLibraryCore.Interfaces.IEventParserConfiguration, System.Func<string, SharedLibraryCore.Interfaces.IEventParserConfiguration,
SharedLibraryCore.GameEvent, SharedLibraryCore.GameEvent,
SharedLibraryCore.GameEvent>>; SharedLibraryCore.GameEvent>>;
namespace IW4MAdmin.Plugins.Stats.Events namespace IW4MAdmin.Plugins.Stats.Events
{ {
public class Script : IRegisterEvent public class Script : IRegisterEvent
{ {
private const string EVENT_SCRIPTKILL = "ScriptKill"; private const string EventScriptKill = "ScriptKill";
private const string EVENT_SCRIPTDAMAGE = "ScriptDamage"; private const string EventScriptDamage = "ScriptDamage";
private const string EVENT_JOINTEAM = "JoinTeam";
/// <summary> /// <summary>
/// this is a custom event printed out by _customcallbacks.gsc (used for anticheat) /// this is a custom event printed out by _customcallbacks.gsc (used for anticheat)
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
private EventGeneratorCallback ScriptKill() private static EventGeneratorCallback ScriptKill()
{ {
return (EVENT_SCRIPTKILL, EVENT_SCRIPTKILL, (string eventLine, IEventParserConfiguration config, GameEvent autoEvent) => return (EventScriptKill, EventScriptKill,
{ (eventLine, config, autoEvent) =>
string[] lineSplit = eventLine.Split(";"); {
var lineSplit = eventLine.Split(";");
if (lineSplit[1].IsBotGuid() || lineSplit[2].IsBotGuid()) if (lineSplit[1].IsBotGuid() || lineSplit[2].IsBotGuid())
{ {
return autoEvent; return autoEvent;
} }
long originId = lineSplit[1].ConvertGuidToLong(config.GuidNumberStyle, 1); var originId = lineSplit[1].ConvertGuidToLong(config.GuidNumberStyle, 1);
long targetId = lineSplit[2].ConvertGuidToLong(config.GuidNumberStyle, 1); var targetId = lineSplit[2].ConvertGuidToLong(config.GuidNumberStyle, 1);
autoEvent.Type = GameEvent.EventType.ScriptKill; var anticheatEvent = new AntiCheatDamageEvent
autoEvent.Origin = new EFClient() { NetworkId = originId }; {
autoEvent.Target = new EFClient() { NetworkId = targetId }; ScriptData = eventLine,
autoEvent.RequiredEntity = GameEvent.EventRequiredEntity.Origin | GameEvent.EventRequiredEntity.Target; Type = GameEvent.EventType.ScriptKill,
autoEvent.GameTime = autoEvent.GameTime; Origin = new EFClient { NetworkId = originId },
Target = new EFClient { NetworkId = targetId },
RequiredEntity =
GameEvent.EventRequiredEntity.Origin | GameEvent.EventRequiredEntity.Target,
GameTime = autoEvent.GameTime,
IsKill = true
};
return autoEvent; return anticheatEvent;
} }
); );
} }
/// <summary> /// <summary>
/// this is a custom event printed out by _customcallbacks.gsc (used for anticheat) /// this is a custom event printed out by _customcallbacks.gsc (used for anticheat)
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
private EventGeneratorCallback ScriptDamage() public EventGeneratorCallback ScriptDamage()
{ {
// this is a custom event printed out by _customcallbacks.gsc (used for anticheat) // this is a custom event printed out by _customcallbacks.gsc (used for anticheat)
return (EVENT_SCRIPTDAMAGE, EVENT_SCRIPTDAMAGE, (string eventLine, IEventParserConfiguration config, GameEvent autoEvent) => return (EventScriptDamage, EventScriptDamage,
{ (eventLine, config, autoEvent) =>
string[] lineSplit = eventLine.Split(";"); {
var lineSplit = eventLine.Split(";");
if (lineSplit[1].IsBotGuid() || lineSplit[2].IsBotGuid()) if (lineSplit[1].IsBotGuid() || lineSplit[2].IsBotGuid())
{ {
return autoEvent; return autoEvent;
} }
long originId = lineSplit[1].ConvertGuidToLong(config.GuidNumberStyle, 1); var originId = lineSplit[1].ConvertGuidToLong(config.GuidNumberStyle, 1);
long targetId = lineSplit[2].ConvertGuidToLong(config.GuidNumberStyle, 1); var targetId = lineSplit[2].ConvertGuidToLong(config.GuidNumberStyle, 1);
autoEvent.Type = GameEvent.EventType.ScriptDamage; var anticheatEvent = new AntiCheatDamageEvent
autoEvent.Origin = new EFClient() { NetworkId = originId }; {
autoEvent.Target = new EFClient() { NetworkId = targetId }; ScriptData = eventLine,
autoEvent.RequiredEntity = GameEvent.EventRequiredEntity.Origin | GameEvent.EventRequiredEntity.Target; Type = GameEvent.EventType.ScriptDamage,
Origin = new EFClient { NetworkId = originId },
Target = new EFClient { NetworkId = targetId },
RequiredEntity =
GameEvent.EventRequiredEntity.Origin | GameEvent.EventRequiredEntity.Target,
GameTime = autoEvent.GameTime
};
return autoEvent; return anticheatEvent;
} }
); );
}
/// <summary>
/// this is a custom event printed out by _customcallbacks.gsc (used for anticheat)
/// </summary>
/// <returns></returns>
private EventGeneratorCallback JoinTeam()
{
// this is a custom event printed out by _customcallbacks.gsc (used for anticheat)
return (EVENT_JOINTEAM, EVENT_JOINTEAM, (string eventLine, IEventParserConfiguration config, GameEvent autoEvent) =>
{
string[] lineSplit = eventLine.Split(";");
if (lineSplit[1].IsBotGuid() || lineSplit[2].IsBotGuid())
{
return autoEvent;
}
long originId = lineSplit[1].ConvertGuidToLong(config.GuidNumberStyle, 1);
long targetId = lineSplit[2].ConvertGuidToLong(config.GuidNumberStyle, 1);
autoEvent.Type = GameEvent.EventType.JoinTeam;
autoEvent.Origin = new EFClient() { NetworkId = lineSplit[1].ConvertGuidToLong(config.GuidNumberStyle) };
autoEvent.RequiredEntity = GameEvent.EventRequiredEntity.Target;
return autoEvent;
}
);
} }
public IEnumerable<EventGeneratorCallback> Events => public IEnumerable<EventGeneratorCallback> Events =>
new[] new[]
{ {
ScriptKill(), ScriptKill(),
ScriptDamage(), ScriptDamage()
JoinTeam()
}; };
} }
public class AntiCheatDamageEvent : GameScriptEvent
{
public bool IsKill { get; init; }
}
} }

View File

@ -7,7 +7,6 @@ using Data.Models;
using Data.Models.Client; using Data.Models.Client;
using Data.Models.Client.Stats; using Data.Models.Client.Stats;
using IW4MAdmin.Plugins.Stats; using IW4MAdmin.Plugins.Stats;
using IW4MAdmin.Plugins.Stats.Config;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using SharedLibraryCore.Dtos; using SharedLibraryCore.Dtos;
@ -114,7 +113,7 @@ namespace Stats.Helpers
All = hitStats, All = hitStats,
Servers = _manager.GetServers() Servers = _manager.GetServers()
.Select(server => new ServerInfo .Select(server => new ServerInfo
{Name = server.Hostname, IPAddress = server.IP, Port = server.Port, Game = (Reference.Game)server.GameName}) {Name = server.Hostname, IPAddress = server.ListenAddress, Port = server.ListenPort, Game = (Reference.Game)server.GameName})
.Where(server => server.Game == clientInfo.GameName) .Where(server => server.Game == clientInfo.GameName)
.ToList(), .ToList(),
Aggregate = hitStats.FirstOrDefault(hit => Aggregate = hitStats.FirstOrDefault(hit =>

View File

@ -46,7 +46,7 @@ namespace IW4MAdmin.Plugins.Stats.Helpers
private readonly SemaphoreSlim _addPlayerWaiter = new SemaphoreSlim(1, 1); private readonly SemaphoreSlim _addPlayerWaiter = new SemaphoreSlim(1, 1);
private readonly IServerDistributionCalculator _serverDistributionCalculator; private readonly IServerDistributionCalculator _serverDistributionCalculator;
public StatManager(ILogger<StatManager> logger, IManager mgr, IDatabaseContextFactory contextFactory, public StatManager(ILogger<StatManager> logger, IDatabaseContextFactory contextFactory,
StatsConfiguration statsConfig, StatsConfiguration statsConfig,
IServerDistributionCalculator serverDistributionCalculator) IServerDistributionCalculator serverDistributionCalculator)
{ {
@ -360,13 +360,8 @@ namespace IW4MAdmin.Plugins.Stats.Helpers
return finished; return finished;
} }
/// <summary> public async Task EnsureServerAdded(IGameServer gameServer, CancellationToken token)
/// Add a server to the StatManager server pool
/// </summary>
/// <param name="sv"></param>
public void AddServer(Server sv)
{ {
// insert the server if it does not exist
try try
{ {
if (serverModels == null) if (serverModels == null)
@ -374,76 +369,75 @@ namespace IW4MAdmin.Plugins.Stats.Helpers
SetupServerIds(); SetupServerIds();
} }
long serverId = GetIdForServer(sv); var serverId = GetIdForServer(gameServer as Server);
EFServer server;
using var ctx = _contextFactory.CreateContext(enableTracking: false); await using var ctx = _contextFactory.CreateContext(enableTracking: false);
var serverSet = ctx.Set<EFServer>(); var serverSet = ctx.Set<EFServer>();
// get the server from the database if it exists, otherwise create and insert a new one // get the server from the database if it exists, otherwise create and insert a new one
server = serverSet.FirstOrDefault(s => s.ServerId == serverId); var cachedServerModel = await serverSet.FirstOrDefaultAsync(s => s.ServerId == serverId, token);
// the server might be using legacy server id // the server might be using legacy server id
if (server == null) if (cachedServerModel == null)
{ {
server = serverSet.FirstOrDefault(s => s.EndPoint == sv.ToString()); cachedServerModel = await serverSet.FirstOrDefaultAsync(s => s.EndPoint == gameServer.Id, token);
if (server != null) if (cachedServerModel != null)
{ {
// this provides a way to identify legacy server entries // this provides a way to identify legacy server entries
server.EndPoint = sv.ToString(); cachedServerModel.EndPoint = gameServer.Id;
ctx.Update(server); ctx.Update(cachedServerModel);
ctx.SaveChanges(); ctx.SaveChanges();
} }
} }
// server has never been added before // server has never been added before
if (server == null) if (cachedServerModel == null)
{ {
server = new EFServer() cachedServerModel = new EFServer
{ {
Port = sv.Port, Port = gameServer.ListenPort,
EndPoint = sv.ToString(), EndPoint = gameServer.Id,
ServerId = serverId, ServerId = serverId,
GameName = (Reference.Game?)sv.GameName, GameName = gameServer.GameCode,
HostName = sv.Hostname HostName = gameServer.ListenAddress
}; };
server = serverSet.Add(server).Entity; cachedServerModel = serverSet.Add(cachedServerModel).Entity;
// this doesn't need to be async as it's during initialization // this doesn't need to be async as it's during initialization
ctx.SaveChanges(); await ctx.SaveChangesAsync(token);
} }
// we want to set the gamename up if it's never been set, or it changed // we want to set the gamename up if it's never been set, or it changed
else if (!server.GameName.HasValue || server.GameName.Value != (Reference.Game)sv.GameName) else if (!cachedServerModel.GameName.HasValue || cachedServerModel.GameName.Value != gameServer.GameCode)
{ {
server.GameName = (Reference.Game)sv.GameName; cachedServerModel.GameName = gameServer.GameCode;
ctx.Entry(server).Property(_prop => _prop.GameName).IsModified = true; ctx.Entry(cachedServerModel).Property(property => property.GameName).IsModified = true;
ctx.SaveChanges(); await ctx.SaveChangesAsync(token);
} }
if (server.HostName == null || server.HostName != sv.Hostname) if (cachedServerModel.HostName == null || cachedServerModel.HostName != gameServer.ServerName)
{ {
server.HostName = sv.Hostname; cachedServerModel.HostName = gameServer.ServerName;
ctx.Entry(server).Property(_prop => _prop.HostName).IsModified = true; ctx.Entry(cachedServerModel).Property(property => property.HostName).IsModified = true;
ctx.SaveChanges(); await ctx.SaveChangesAsync(token);
} }
ctx.Entry(server).Property(_prop => _prop.IsPasswordProtected).IsModified = true; ctx.Entry(cachedServerModel).Property(property => property.IsPasswordProtected).IsModified = true;
server.IsPasswordProtected = !string.IsNullOrEmpty(sv.GamePassword); cachedServerModel.IsPasswordProtected = !string.IsNullOrEmpty(gameServer.GamePassword);
ctx.SaveChanges(); await ctx.SaveChangesAsync(token);
// check to see if the stats have ever been initialized // check to see if the stats have ever been initialized
var serverStats = InitializeServerStats(server.ServerId); var serverStats = InitializeServerStats(cachedServerModel.ServerId);
_servers.TryAdd(serverId, new ServerStats(server, serverStats, sv) _servers.TryAdd(serverId, new ServerStats(cachedServerModel, serverStats, gameServer as Server)
{ {
IsTeamBased = sv.Gametype != "dm" IsTeamBased = gameServer.Gametype != "dm"
}); });
} }
catch (Exception e) catch (Exception ex)
{ {
_log.LogError(e, "{message}", _log.LogError(ex, "{Message}",
Utilities.CurrentLocalization.LocalizationIndex["PLUGIN_STATS_ERROR_ADD"]); Utilities.CurrentLocalization.LocalizationIndex["PLUGIN_STATS_ERROR_ADD"]);
} }
} }
@ -552,7 +546,7 @@ namespace IW4MAdmin.Plugins.Stats.Helpers
clientStats.SessionScore = pl.Score; clientStats.SessionScore = pl.Score;
clientStats.LastScore = pl.Score; clientStats.LastScore = pl.Score;
pl.SetAdditionalProperty(CLIENT_DETECTIONS_KEY, new Detection(_log, clientStats)); pl.SetAdditionalProperty(CLIENT_DETECTIONS_KEY, new Detection(_log, clientStats, _config));
_log.LogDebug("Added {client} to stats", pl.ToString()); _log.LogDebug("Added {client} to stats", pl.ToString());
return clientStats; return clientStats;
@ -586,41 +580,42 @@ namespace IW4MAdmin.Plugins.Stats.Helpers
/// <summary> /// <summary>
/// Perform stat updates for disconnecting client /// Perform stat updates for disconnecting client
/// </summary> /// </summary>
/// <param name="pl">Disconnecting client</param> /// <param name="client">Disconnecting client</param>
/// <param name="cancellationToken"></param>
/// <returns></returns> /// <returns></returns>
public async Task RemovePlayer(EFClient pl) public async Task RemovePlayer(EFClient client, CancellationToken cancellationToken)
{ {
_log.LogDebug("Removing {client} from stats", pl.ToString()); _log.LogDebug("Removing {Client} from stats", client.ToString());
if (pl.CurrentServer == null) if (client.CurrentServer == null)
{ {
_log.LogWarning("Disconnecting client {client} is not on a server", pl.ToString()); _log.LogWarning("Disconnecting client {Client} is not on a server", client.ToString());
return; return;
} }
var serverId = GetIdForServer(pl.CurrentServer); var serverId = GetIdForServer(client.CurrentServer);
var serverStats = _servers[serverId].ServerStatistics; var serverStats = _servers[serverId].ServerStatistics;
// get individual client's stats // get individual client's stats
var clientStats = pl.GetAdditionalProperty<EFClientStatistics>(CLIENT_STATS_KEY); var clientStats = client.GetAdditionalProperty<EFClientStatistics>(CLIENT_STATS_KEY);
// sync their stats before they leave // sync their stats before they leave
if (clientStats != null) if (clientStats != null)
{ {
clientStats = UpdateStats(clientStats, pl); clientStats = UpdateStats(clientStats, client);
await SaveClientStats(clientStats); await SaveClientStats(clientStats);
if (_config.EnableAdvancedMetrics) if (_config.EnableAdvancedMetrics)
{ {
await UpdateHistoricalRanking(pl.ClientId, clientStats, serverId); await UpdateHistoricalRanking(client.ClientId, clientStats, serverId);
} }
// increment the total play time // increment the total play time
serverStats.TotalPlayTime += pl.ConnectionLength; serverStats.TotalPlayTime += client.ConnectionLength;
pl.SetAdditionalProperty(CLIENT_STATS_KEY, null); client.SetAdditionalProperty(CLIENT_STATS_KEY, null);
} }
else else
{ {
_log.LogWarning("Disconnecting client {client} has not been added to stats", pl.ToString()); _log.LogWarning("Disconnecting client {Client} has not been added to stats", client.ToString());
} }
} }
@ -743,7 +738,7 @@ namespace IW4MAdmin.Plugins.Stats.Helpers
return; return;
} }
if (Plugin.Config.Configuration().StoreClientKills) if (_config.StoreClientKills)
{ {
var serverWaiter = _servers[serverId].OnSaving; var serverWaiter = _servers[serverId].OnSaving;
try try
@ -772,7 +767,7 @@ namespace IW4MAdmin.Plugins.Stats.Helpers
} }
} }
if (Plugin.Config.Configuration().AnticheatConfiguration.Enable && !attacker.IsBot && if (_config.AnticheatConfiguration.Enable && !attacker.IsBot &&
attacker.ClientId != victim.ClientId) attacker.ClientId != victim.ClientId)
{ {
clientDetection.TrackedHits.Add(hit); clientDetection.TrackedHits.Add(hit);
@ -857,10 +852,10 @@ namespace IW4MAdmin.Plugins.Stats.Helpers
private bool ShouldUseDetection(Server server, DetectionType detectionType, long clientId) private bool ShouldUseDetection(Server server, DetectionType detectionType, long clientId)
{ {
#pragma warning disable CS0612 #pragma warning disable CS0612
var serverDetectionTypes = Plugin.Config.Configuration().AnticheatConfiguration.ServerDetectionTypes; var serverDetectionTypes = _config.AnticheatConfiguration.ServerDetectionTypes;
#pragma warning restore CS0612 #pragma warning restore CS0612
var gameDetectionTypes = Plugin.Config.Configuration().AnticheatConfiguration.GameDetectionTypes; var gameDetectionTypes = _config.AnticheatConfiguration.GameDetectionTypes;
var ignoredClients = Plugin.Config.Configuration().AnticheatConfiguration.IgnoredClientIds; var ignoredClients = _config.AnticheatConfiguration.IgnoredClientIds;
if (ignoredClients.Contains(clientId)) if (ignoredClients.Contains(clientId))
{ {
@ -1011,9 +1006,9 @@ namespace IW4MAdmin.Plugins.Stats.Helpers
victimStats.LastScore = estimatedVictimScore; victimStats.LastScore = estimatedVictimScore;
// show encouragement/discouragement // show encouragement/discouragement
var streakMessage = (attackerStats.ClientId != victimStats.ClientId) var streakMessage = attackerStats.ClientId != victimStats.ClientId
? StreakMessage.MessageOnStreak(attackerStats.KillStreak, attackerStats.DeathStreak) ? StreakMessage.MessageOnStreak(attackerStats.KillStreak, attackerStats.DeathStreak, _config)
: StreakMessage.MessageOnStreak(-1, -1); : StreakMessage.MessageOnStreak(-1, -1, _config);
if (streakMessage != string.Empty) if (streakMessage != string.Empty)
{ {
@ -1530,13 +1525,13 @@ namespace IW4MAdmin.Plugins.Stats.Helpers
return serverStats; return serverStats;
} }
public void ResetKillstreaks(Server sv) public void ResetKillstreaks(IGameServer gameServer)
{ {
foreach (var session in sv.GetClientsAsList() foreach (var session in gameServer.ConnectedClients
.Select(_client => new .Select(client => new
{ {
stat = _client.GetAdditionalProperty<EFClientStatistics>(CLIENT_STATS_KEY), stat = client.GetAdditionalProperty<EFClientStatistics>(CLIENT_STATS_KEY),
detection = _client.GetAdditionalProperty<Detection>(CLIENT_DETECTIONS_KEY) detection = client.GetAdditionalProperty<Detection>(CLIENT_DETECTIONS_KEY)
})) }))
{ {
session.stat?.StartNewSession(); session.stat?.StartNewSession();
@ -1563,7 +1558,8 @@ namespace IW4MAdmin.Plugins.Stats.Helpers
stats.EloRating = 200; stats.EloRating = 200;
} }
public async Task AddMessageAsync(int clientId, long serverId, bool sentIngame, string message) public async Task AddMessageAsync(int clientId, long serverId, bool sentIngame, string message,
CancellationToken cancellationToken)
{ {
// the web users can have no account // the web users can have no account
if (clientId < 1) if (clientId < 1)
@ -1571,8 +1567,8 @@ namespace IW4MAdmin.Plugins.Stats.Helpers
return; return;
} }
await using var ctx = _contextFactory.CreateContext(enableTracking: false); await using var context = _contextFactory.CreateContext(enableTracking: false);
ctx.Set<EFClientMessage>().Add(new EFClientMessage() context.Set<EFClientMessage>().Add(new EFClientMessage()
{ {
ClientId = clientId, ClientId = clientId,
Message = message, Message = message,
@ -1581,26 +1577,26 @@ namespace IW4MAdmin.Plugins.Stats.Helpers
SentIngame = sentIngame SentIngame = sentIngame
}); });
await ctx.SaveChangesAsync(); await context.SaveChangesAsync(cancellationToken);
} }
public async Task Sync(Server sv) public async Task Sync(IGameServer gameServer, CancellationToken token)
{ {
long serverId = GetIdForServer(sv); var serverId = GetIdForServer(gameServer);
var waiter = _servers[serverId].OnSaving; var waiter = _servers[serverId].OnSaving;
try try
{ {
await waiter.WaitAsync(); await waiter.WaitAsync(token);
await using var ctx = _contextFactory.CreateContext(); await using var context = _contextFactory.CreateContext();
var serverStatsSet = ctx.Set<EFServerStatistics>(); var serverStatsSet = context.Set<EFServerStatistics>();
serverStatsSet.Update(_servers[serverId].ServerStatistics); serverStatsSet.Update(_servers[serverId].ServerStatistics);
await ctx.SaveChangesAsync(); await context.SaveChangesAsync(token);
foreach (var stats in sv.GetClientsAsList() foreach (var stats in gameServer.ConnectedClients
.Select(_client => _client.GetAdditionalProperty<EFClientStatistics>(CLIENT_STATS_KEY)) .Select(client => client.GetAdditionalProperty<EFClientStatistics>(CLIENT_STATS_KEY))
.Where(_stats => _stats != null)) .Where(stats => stats != null))
{ {
await SaveClientStats(stats); await SaveClientStats(stats);
} }
@ -1608,9 +1604,9 @@ namespace IW4MAdmin.Plugins.Stats.Helpers
await SaveHitCache(serverId); await SaveHitCache(serverId);
} }
catch (Exception e) catch (Exception ex)
{ {
_log.LogError(e, "There was a problem syncing server stats"); _log.LogError(ex, "There was a problem syncing server stats");
} }
finally finally
@ -1627,28 +1623,24 @@ namespace IW4MAdmin.Plugins.Stats.Helpers
_servers[serverId].IsTeamBased = isTeamBased; _servers[serverId].IsTeamBased = isTeamBased;
} }
public static long GetIdForServer(Server server) public static long GetIdForServer(IGameServer gameServer)
{ {
if ($"{server.IP}:{server.Port.ToString()}" == "66.150.121.184:28965") if (gameServer.Id == "66.150.121.184:28965")
{ {
return 886229536; return 886229536;
} }
// todo: this is not stable and will need to be migrated again... // todo: this is not stable and will need to be migrated again...
long id = HashCode.Combine(server.IP, server.Port); long id = HashCode.Combine(gameServer.ListenAddress, gameServer.ListenPort);
id = id < 0 ? Math.Abs(id) : id; id = id < 0 ? Math.Abs(id) : id;
long? serverId;
serverId = serverModels.FirstOrDefault(_server => _server.ServerId == server.EndPoint || #pragma warning disable CS0618
_server.EndPoint == server.ToString() || var serverId = serverModels.FirstOrDefault(cachedServer => cachedServer.ServerId == gameServer.LegacyEndpoint ||
_server.ServerId == id)?.ServerId; #pragma warning restore CS0618
cachedServer.EndPoint == gameServer.ToString() ||
cachedServer.ServerId == id)?.ServerId;
if (!serverId.HasValue) return serverId ?? id;
{
return id;
}
return serverId.Value;
} }
} }
} }

View File

@ -1,29 +1,17 @@
using SharedLibraryCore; using System.Linq;
using SharedLibraryCore.Helpers; using Stats.Config;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IW4MAdmin.Plugins.Stats.Helpers namespace IW4MAdmin.Plugins.Stats.Helpers;
public static class StreakMessage
{ {
public class StreakMessage public static string MessageOnStreak(int killStreak, int deathStreak, StatsConfiguration config)
{ {
/// <summary> var killstreakMessage = config.KillstreakMessages;
/// Get a message from the configuration encouraging or discouraging clients var deathstreakMessage = config.DeathstreakMessages;
/// </summary>
/// <param name="killStreak">how many kills the client has without dying</param>
/// <param name="deathStreak">how many deaths the client has without getting a kill</param>
/// <returns>message to send to the client</returns>
public static string MessageOnStreak(int killStreak, int deathStreak)
{
var killstreakMessage = Plugin.Config.Configuration().KillstreakMessages;
var deathstreakMessage = Plugin.Config.Configuration().DeathstreakMessages;
string message = killstreakMessage?.FirstOrDefault(m => m.Count == killStreak)?.Message; var message = killstreakMessage?.FirstOrDefault(m => m.Count == killStreak)?.Message;
message = message ?? deathstreakMessage?.FirstOrDefault(m => m.Count == deathStreak)?.Message; message ??= deathstreakMessage?.FirstOrDefault(m => m.Count == deathStreak)?.Message;
return message ?? ""; return message ?? "";
}
} }
} }

File diff suppressed because it is too large Load Diff

View File

@ -17,7 +17,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2022.10.13.1" PrivateAssets="All" /> <PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2023.2.11.1" PrivateAssets="All" />
</ItemGroup> </ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent"> <Target Name="PostBuild" AfterTargets="PostBuildEvent">