From 505a2c4c2d4af8b5e0c22279cc9b3ff8cf8c6607 Mon Sep 17 00:00:00 2001 From: RaidMax Date: Fri, 28 Jan 2022 17:28:49 -0600 Subject: [PATCH] fix refactor issue --- Application/Main.cs | 4 ++++ Application/Misc/ScriptPlugin.cs | 16 ++++++------- Plugins/Stats/Client/WeaponNameParser.cs | 8 +++---- Plugins/Stats/Helpers/StatManager.cs | 24 +++++++++---------- Plugins/Stats/Plugin.cs | 2 +- .../Controllers/Client/ClientController.cs | 9 ++++--- .../Client/ClientStatisticsController.cs | 6 ++--- .../Client/Legacy/StatsController.cs | 11 ++++----- WebfrontCore/Startup.cs | 2 +- .../ViewComponents/TopPlayersViewComponent.cs | 9 ++++--- 10 files changed, 44 insertions(+), 47 deletions(-) diff --git a/Application/Main.cs b/Application/Main.cs index de8c1601f..b778dd472 100644 --- a/Application/Main.cs +++ b/Application/Main.cs @@ -34,6 +34,7 @@ using IW4MAdmin.Plugins.Stats.Client.Abstractions; using IW4MAdmin.Plugins.Stats.Client; using Stats.Client.Abstractions; using Stats.Client; +using Stats.Config; using Stats.Helpers; namespace IW4MAdmin.Application @@ -346,6 +347,8 @@ namespace IW4MAdmin.Application await defaultConfigHandler.BuildAsync(); var commandConfigHandler = new BaseConfigurationHandler("CommandConfiguration"); await commandConfigHandler.BuildAsync(); + var statsCommandHandler = new BaseConfigurationHandler(); + await statsCommandHandler.BuildAsync(); var defaultConfig = defaultConfigHandler.Configuration(); var appConfig = appConfigHandler.Configuration(); var masterUri = Utilities.IsDevelopment @@ -386,6 +389,7 @@ namespace IW4MAdmin.Application .AddSingleton((IConfigurationHandler) appConfigHandler) .AddSingleton>(commandConfigHandler) .AddSingleton(appConfig) + .AddSingleton(statsCommandHandler.Configuration() ?? new StatsConfiguration()) .AddSingleton(serviceProvider => serviceProvider.GetRequiredService>() .Configuration() ?? new CommandConfiguration()) diff --git a/Application/Misc/ScriptPlugin.cs b/Application/Misc/ScriptPlugin.cs index fc6943749..f46bd2b0c 100644 --- a/Application/Misc/ScriptPlugin.cs +++ b/Application/Misc/ScriptPlugin.cs @@ -179,17 +179,15 @@ namespace IW4MAdmin.Application.Misc manager.AdditionalEventParsers.Add(eventParser); manager.AdditionalRConParsers.Add(rconParser); } - - else - { - var configWrapper = new ScriptPluginConfigurationWrapper(Name, _scriptEngine); - await configWrapper.InitializeAsync(); - _scriptEngine.SetValue("_configHandler", configWrapper); - await OnLoadAsync(manager); - } } - catch (RuntimeBinderException) { } + catch (RuntimeBinderException) + { + var configWrapper = new ScriptPluginConfigurationWrapper(Name, _scriptEngine); + await configWrapper.InitializeAsync(); + _scriptEngine.SetValue("_configHandler", configWrapper); + await OnLoadAsync(manager); + } if (!firstRun) { diff --git a/Plugins/Stats/Client/WeaponNameParser.cs b/Plugins/Stats/Client/WeaponNameParser.cs index a559660c8..88184c377 100644 --- a/Plugins/Stats/Client/WeaponNameParser.cs +++ b/Plugins/Stats/Client/WeaponNameParser.cs @@ -3,9 +3,7 @@ using Stats.Client.Abstractions; using Stats.Client.Game; using System.Collections.Generic; using System.Linq; -using IW4MAdmin.Plugins.Stats.Config; using SharedLibraryCore; -using SharedLibraryCore.Interfaces; using Stats.Config; using ILogger = Microsoft.Extensions.Logging.ILogger; @@ -16,10 +14,10 @@ namespace Stats.Client private readonly ILogger _logger; private readonly StatsConfiguration _config; - public WeaponNameParser(ILogger logger, IConfigurationHandler config) + public WeaponNameParser(ILogger logger, StatsConfiguration config) { _logger = logger; - _config = config.Configuration(); + _config = config; } public WeaponInfo Parse(string weaponName, Server.Game gameName) @@ -67,4 +65,4 @@ namespace Stats.Client return weaponInfo; } } -} \ No newline at end of file +} diff --git a/Plugins/Stats/Helpers/StatManager.cs b/Plugins/Stats/Helpers/StatManager.cs index 3ea142cbc..a197fb992 100644 --- a/Plugins/Stats/Helpers/StatManager.cs +++ b/Plugins/Stats/Helpers/StatManager.cs @@ -38,7 +38,7 @@ namespace IW4MAdmin.Plugins.Stats.Helpers private readonly ConcurrentDictionary _servers; private readonly ILogger _log; private readonly IDatabaseContextFactory _contextFactory; - private readonly IConfigurationHandler _configHandler; + private readonly StatsConfiguration _config; private static List serverModels; public static string CLIENT_STATS_KEY = "ClientStats"; public static string CLIENT_DETECTIONS_KEY = "ClientDetections"; @@ -47,13 +47,13 @@ namespace IW4MAdmin.Plugins.Stats.Helpers private readonly IServerDistributionCalculator _serverDistributionCalculator; public StatManager(ILogger logger, IManager mgr, IDatabaseContextFactory contextFactory, - IConfigurationHandler configHandler, + StatsConfiguration statsConfig, IServerDistributionCalculator serverDistributionCalculator) { _servers = new ConcurrentDictionary(); _log = logger; _contextFactory = contextFactory; - _configHandler = configHandler; + _config = statsConfig; _serverDistributionCalculator = serverDistributionCalculator; } @@ -75,7 +75,7 @@ namespace IW4MAdmin.Plugins.Stats.Helpers r.When > fifteenDaysAgo && r.RatingHistory.Client.Level != EFClient.Permission.Banned && r.Newest && - r.ActivityAmount >= _configHandler.Configuration().TopPlayersMinPlayTime; + r.ActivityAmount >= _config.TopPlayersMinPlayTime; } /// @@ -87,7 +87,7 @@ namespace IW4MAdmin.Plugins.Stats.Helpers { await using var context = _contextFactory.CreateContext(enableTracking: false); - if (_configHandler.Configuration().EnableAdvancedMetrics) + if (_config.EnableAdvancedMetrics) { var clientRanking = await context.Set() .Where(r => r.ClientId == clientId) @@ -126,7 +126,7 @@ namespace IW4MAdmin.Plugins.Stats.Helpers && ranking.PerformanceMetric != null && ranking.Newest && ranking.Client.TotalConnectionTime >= - _configHandler.Configuration().TopPlayersMinPlayTime; + _config.TopPlayersMinPlayTime; } public async Task GetTotalRankedPlayers(long serverId) @@ -217,7 +217,7 @@ namespace IW4MAdmin.Plugins.Stats.Helpers public async Task> GetTopStats(int start, int count, long? serverId = null) { - if (_configHandler.Configuration().EnableAdvancedMetrics) + if (_config.EnableAdvancedMetrics) { return await GetNewTopStats(start, count, serverId); } @@ -570,7 +570,7 @@ namespace IW4MAdmin.Plugins.Stats.Helpers { clientStats = UpdateStats(clientStats, pl); await SaveClientStats(clientStats); - if (_configHandler.Configuration().EnableAdvancedMetrics) + if (_config.EnableAdvancedMetrics) { await UpdateHistoricalRanking(pl.ClientId, clientStats, serverId); } @@ -973,7 +973,7 @@ namespace IW4MAdmin.Plugins.Stats.Helpers // update their performance if ((DateTime.UtcNow - attackerStats.LastStatHistoryUpdate).TotalMinutes >= - (Utilities.IsDevelopment ? 0.5 : _configHandler.Configuration().EnableAdvancedMetrics ? 5.0 : 2.5)) + (Utilities.IsDevelopment ? 0.5 : _config.EnableAdvancedMetrics ? 5.0 : 2.5)) { try { @@ -982,7 +982,7 @@ namespace IW4MAdmin.Plugins.Stats.Helpers // for stat history update, but one is already processing that invalidates the original await attackerStats.ProcessingHit.WaitAsync(Utilities.DefaultCommandTimeout, Plugin.ServerManager.CancellationToken); - if (_configHandler.Configuration().EnableAdvancedMetrics) + if (_config.EnableAdvancedMetrics) { await UpdateHistoricalRanking(attacker.ClientId, attackerStats, serverId); } @@ -1190,7 +1190,7 @@ namespace IW4MAdmin.Plugins.Stats.Helpers public async Task UpdateHistoricalRanking(int clientId, EFClientStatistics clientStats, long serverId) { await using var context = _contextFactory.CreateContext(); - var minPlayTime = _configHandler.Configuration().TopPlayersMinPlayTime; + var minPlayTime = _config.TopPlayersMinPlayTime; var performances = await context.Set() .AsNoTracking() @@ -1208,7 +1208,7 @@ namespace IW4MAdmin.Plugins.Stats.Helpers var serverRanking = await context.Set() .Where(stats => stats.ClientId != clientStats.ClientId) .Where(AdvancedClientStatsResourceQueryHelper.GetRankingFunc( - _configHandler.Configuration().TopPlayersMinPlayTime, clientStats.ZScore, serverId)) + _config.TopPlayersMinPlayTime, clientStats.ZScore, serverId)) .CountAsync(); var serverRankingSnapshot = new EFClientRankingHistory diff --git a/Plugins/Stats/Plugin.cs b/Plugins/Stats/Plugin.cs index 9e1f9992f..2c19ce3a7 100644 --- a/Plugins/Stats/Plugin.cs +++ b/Plugins/Stats/Plugin.cs @@ -442,7 +442,7 @@ namespace IW4MAdmin.Plugins.Stats } ServerManager = manager; - Manager = new StatManager(_managerLogger, manager, _databaseContextFactory, Config, _serverDistributionCalculator); + Manager = new StatManager(_managerLogger, manager, _databaseContextFactory, Config.Configuration(), _serverDistributionCalculator); await _serverDistributionCalculator.Initialize(); } diff --git a/WebfrontCore/Controllers/Client/ClientController.cs b/WebfrontCore/Controllers/Client/ClientController.cs index a78b29863..fce9ab5e3 100644 --- a/WebfrontCore/Controllers/Client/ClientController.cs +++ b/WebfrontCore/Controllers/Client/ClientController.cs @@ -19,13 +19,12 @@ namespace WebfrontCore.Controllers public class ClientController : BaseController { private readonly IMetaService _metaService; - private readonly IConfigurationHandler _configurationHandler; + private readonly StatsConfiguration _config; - public ClientController(IManager manager, IMetaService metaService, - IConfigurationHandler configurationHandler) : base(manager) + public ClientController(IManager manager, IMetaService metaService, StatsConfiguration config) : base(manager) { _metaService = metaService; - _configurationHandler = configurationHandler; + _config = config; } public async Task ProfileAsync(int id, MetaType? metaFilterType) @@ -114,7 +113,7 @@ namespace WebfrontCore.Controllers ViewBag.Title += " " + Localization["WEBFRONT_CLIENT_PROFILE_TITLE"]; ViewBag.Description = $"Client information for {strippedName}"; ViewBag.Keywords = $"IW4MAdmin, client, profile, {strippedName}"; - ViewBag.UseNewStats = _configurationHandler.Configuration()?.EnableAdvancedMetrics ?? true; + ViewBag.UseNewStats = _config?.EnableAdvancedMetrics ?? true; return View("Profile/Index", clientDto); } diff --git a/WebfrontCore/Controllers/Client/ClientStatisticsController.cs b/WebfrontCore/Controllers/Client/ClientStatisticsController.cs index 829c01f49..c96918b8c 100644 --- a/WebfrontCore/Controllers/Client/ClientStatisticsController.cs +++ b/WebfrontCore/Controllers/Client/ClientStatisticsController.cs @@ -16,10 +16,10 @@ namespace WebfrontCore.Controllers public ClientStatisticsController(IManager manager, IResourceQueryHelper queryHelper, - IConfigurationHandler configurationHandler) : base(manager) + DefaultSettings defaultConfig) : base(manager) { _queryHelper = queryHelper; - _defaultConfig = configurationHandler.Configuration(); + _defaultConfig = defaultConfig; } [HttpGet("{id:int}/advanced")] @@ -35,4 +35,4 @@ namespace WebfrontCore.Controllers return View("~/Views/Client/Statistics/Advanced.cshtml", hitInfo.Results.First()); } } -} \ No newline at end of file +} diff --git a/WebfrontCore/Controllers/Client/Legacy/StatsController.cs b/WebfrontCore/Controllers/Client/Legacy/StatsController.cs index 26a99486d..a84d25a38 100644 --- a/WebfrontCore/Controllers/Client/Legacy/StatsController.cs +++ b/WebfrontCore/Controllers/Client/Legacy/StatsController.cs @@ -27,19 +27,18 @@ namespace IW4MAdmin.Plugins.Web.StatsWeb.Controllers private readonly IResourceQueryHelper _chatResourceQueryHelper; private readonly ITranslationLookup _translationLookup; private readonly IDatabaseContextFactory _contextFactory; - private readonly IConfigurationHandler _configurationHandler; + private readonly StatsConfiguration _config; public StatsController(ILogger logger, IManager manager, IResourceQueryHelper resourceQueryHelper, ITranslationLookup translationLookup, - IDatabaseContextFactory contextFactory, - IConfigurationHandler configurationHandler) : base(manager) + IDatabaseContextFactory contextFactory, StatsConfiguration config) : base(manager) { _logger = logger; _manager = manager; _chatResourceQueryHelper = resourceQueryHelper; _translationLookup = translationLookup; _contextFactory = contextFactory; - _configurationHandler = configurationHandler; + _config = config; } [HttpGet] @@ -70,7 +69,7 @@ namespace IW4MAdmin.Plugins.Web.StatsWeb.Controllers serverId = StatManager.GetIdForServer(server); } - var results = _configurationHandler.Configuration().EnableAdvancedMetrics + var results = _config?.EnableAdvancedMetrics ?? true ? await Plugin.Manager.GetNewTopStats(offset, count, serverId) : await Plugin.Manager.GetTopStats(offset, count, serverId); @@ -80,7 +79,7 @@ namespace IW4MAdmin.Plugins.Web.StatsWeb.Controllers return Ok(); } - ViewBag.UseNewStats = _configurationHandler.Configuration().EnableAdvancedMetrics; + ViewBag.UseNewStats = _config?.EnableAdvancedMetrics; return View("~/Views/Client/Statistics/Components/TopPlayers/_List.cshtml", results); } diff --git a/WebfrontCore/Startup.cs b/WebfrontCore/Startup.cs index 185e36d3d..9f1e618b5 100644 --- a/WebfrontCore/Startup.cs +++ b/WebfrontCore/Startup.cs @@ -129,7 +129,7 @@ namespace WebfrontCore services.AddSingleton(Program.ApplicationServiceProvider .GetRequiredService>()); services.AddSingleton(Program.ApplicationServiceProvider - .GetRequiredService>()); + .GetRequiredService()); services.AddSingleton(Program.ApplicationServiceProvider.GetRequiredService()); } diff --git a/WebfrontCore/ViewComponents/TopPlayersViewComponent.cs b/WebfrontCore/ViewComponents/TopPlayersViewComponent.cs index 81591967f..3a1adc119 100644 --- a/WebfrontCore/ViewComponents/TopPlayersViewComponent.cs +++ b/WebfrontCore/ViewComponents/TopPlayersViewComponent.cs @@ -3,18 +3,17 @@ using System.Threading.Tasks; using IW4MAdmin.Plugins.Stats; using IW4MAdmin.Plugins.Stats.Helpers; using Microsoft.AspNetCore.Mvc; -using SharedLibraryCore.Interfaces; using Stats.Config; namespace WebfrontCore.ViewComponents { public class TopPlayersViewComponent : ViewComponent { - private readonly IConfigurationHandler _configurationHandler; + private readonly StatsConfiguration _config; - public TopPlayersViewComponent(IConfigurationHandler configurationHandler) + public TopPlayersViewComponent(StatsConfiguration config) { - _configurationHandler = configurationHandler; + _config = config; } public async Task InvokeAsync(int count, int offset, long? serverId = null) @@ -32,7 +31,7 @@ namespace WebfrontCore.ViewComponents } - ViewBag.UseNewStats = _configurationHandler.Configuration()?.EnableAdvancedMetrics ?? true; + ViewBag.UseNewStats = _config?.EnableAdvancedMetrics ?? true; return View("~/Views/Client/Statistics/Components/TopPlayers/_List.cshtml", ViewBag.UseNewStats ? await Plugin.Manager.GetNewTopStats(offset, count, serverId)