fix refactor issue

This commit is contained in:
RaidMax 2022-01-28 17:28:49 -06:00
parent 8730a3fab8
commit 505a2c4c2d
10 changed files with 44 additions and 47 deletions

View File

@ -34,6 +34,7 @@ using IW4MAdmin.Plugins.Stats.Client.Abstractions;
using IW4MAdmin.Plugins.Stats.Client; using IW4MAdmin.Plugins.Stats.Client;
using Stats.Client.Abstractions; using Stats.Client.Abstractions;
using Stats.Client; using Stats.Client;
using Stats.Config;
using Stats.Helpers; using Stats.Helpers;
namespace IW4MAdmin.Application namespace IW4MAdmin.Application
@ -346,6 +347,8 @@ namespace IW4MAdmin.Application
await defaultConfigHandler.BuildAsync(); await defaultConfigHandler.BuildAsync();
var commandConfigHandler = new BaseConfigurationHandler<CommandConfiguration>("CommandConfiguration"); var commandConfigHandler = new BaseConfigurationHandler<CommandConfiguration>("CommandConfiguration");
await commandConfigHandler.BuildAsync(); await commandConfigHandler.BuildAsync();
var statsCommandHandler = new BaseConfigurationHandler<StatsConfiguration>();
await statsCommandHandler.BuildAsync();
var defaultConfig = defaultConfigHandler.Configuration(); var defaultConfig = defaultConfigHandler.Configuration();
var appConfig = appConfigHandler.Configuration(); var appConfig = appConfigHandler.Configuration();
var masterUri = Utilities.IsDevelopment var masterUri = Utilities.IsDevelopment
@ -386,6 +389,7 @@ namespace IW4MAdmin.Application
.AddSingleton((IConfigurationHandler<ApplicationConfiguration>) appConfigHandler) .AddSingleton((IConfigurationHandler<ApplicationConfiguration>) appConfigHandler)
.AddSingleton<IConfigurationHandler<CommandConfiguration>>(commandConfigHandler) .AddSingleton<IConfigurationHandler<CommandConfiguration>>(commandConfigHandler)
.AddSingleton(appConfig) .AddSingleton(appConfig)
.AddSingleton(statsCommandHandler.Configuration() ?? new StatsConfiguration())
.AddSingleton(serviceProvider => .AddSingleton(serviceProvider =>
serviceProvider.GetRequiredService<IConfigurationHandler<CommandConfiguration>>() serviceProvider.GetRequiredService<IConfigurationHandler<CommandConfiguration>>()
.Configuration() ?? new CommandConfiguration()) .Configuration() ?? new CommandConfiguration())

View File

@ -179,17 +179,15 @@ namespace IW4MAdmin.Application.Misc
manager.AdditionalEventParsers.Add(eventParser); manager.AdditionalEventParsers.Add(eventParser);
manager.AdditionalRConParsers.Add(rconParser); 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) if (!firstRun)
{ {

View File

@ -3,9 +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 IW4MAdmin.Plugins.Stats.Config;
using SharedLibraryCore; using SharedLibraryCore;
using SharedLibraryCore.Interfaces;
using Stats.Config; using Stats.Config;
using ILogger = Microsoft.Extensions.Logging.ILogger; using ILogger = Microsoft.Extensions.Logging.ILogger;
@ -16,10 +14,10 @@ namespace Stats.Client
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly StatsConfiguration _config; private readonly StatsConfiguration _config;
public WeaponNameParser(ILogger<WeaponNameParser> logger, IConfigurationHandler<StatsConfiguration> config) public WeaponNameParser(ILogger<WeaponNameParser> logger, StatsConfiguration config)
{ {
_logger = logger; _logger = logger;
_config = config.Configuration(); _config = config;
} }
public WeaponInfo Parse(string weaponName, Server.Game gameName) public WeaponInfo Parse(string weaponName, Server.Game gameName)

View File

@ -38,7 +38,7 @@ namespace IW4MAdmin.Plugins.Stats.Helpers
private readonly ConcurrentDictionary<long, ServerStats> _servers; private readonly ConcurrentDictionary<long, ServerStats> _servers;
private readonly ILogger _log; private readonly ILogger _log;
private readonly IDatabaseContextFactory _contextFactory; private readonly IDatabaseContextFactory _contextFactory;
private readonly IConfigurationHandler<StatsConfiguration> _configHandler; private readonly StatsConfiguration _config;
private static List<EFServer> serverModels; private static List<EFServer> serverModels;
public static string CLIENT_STATS_KEY = "ClientStats"; public static string CLIENT_STATS_KEY = "ClientStats";
public static string CLIENT_DETECTIONS_KEY = "ClientDetections"; public static string CLIENT_DETECTIONS_KEY = "ClientDetections";
@ -47,13 +47,13 @@ namespace IW4MAdmin.Plugins.Stats.Helpers
private readonly IServerDistributionCalculator _serverDistributionCalculator; private readonly IServerDistributionCalculator _serverDistributionCalculator;
public StatManager(ILogger<StatManager> logger, IManager mgr, IDatabaseContextFactory contextFactory, public StatManager(ILogger<StatManager> logger, IManager mgr, IDatabaseContextFactory contextFactory,
IConfigurationHandler<StatsConfiguration> configHandler, StatsConfiguration statsConfig,
IServerDistributionCalculator serverDistributionCalculator) IServerDistributionCalculator serverDistributionCalculator)
{ {
_servers = new ConcurrentDictionary<long, ServerStats>(); _servers = new ConcurrentDictionary<long, ServerStats>();
_log = logger; _log = logger;
_contextFactory = contextFactory; _contextFactory = contextFactory;
_configHandler = configHandler; _config = statsConfig;
_serverDistributionCalculator = serverDistributionCalculator; _serverDistributionCalculator = serverDistributionCalculator;
} }
@ -75,7 +75,7 @@ namespace IW4MAdmin.Plugins.Stats.Helpers
r.When > fifteenDaysAgo && r.When > fifteenDaysAgo &&
r.RatingHistory.Client.Level != EFClient.Permission.Banned && r.RatingHistory.Client.Level != EFClient.Permission.Banned &&
r.Newest && r.Newest &&
r.ActivityAmount >= _configHandler.Configuration().TopPlayersMinPlayTime; r.ActivityAmount >= _config.TopPlayersMinPlayTime;
} }
/// <summary> /// <summary>
@ -87,7 +87,7 @@ namespace IW4MAdmin.Plugins.Stats.Helpers
{ {
await using var context = _contextFactory.CreateContext(enableTracking: false); await using var context = _contextFactory.CreateContext(enableTracking: false);
if (_configHandler.Configuration().EnableAdvancedMetrics) if (_config.EnableAdvancedMetrics)
{ {
var clientRanking = await context.Set<EFClientRankingHistory>() var clientRanking = await context.Set<EFClientRankingHistory>()
.Where(r => r.ClientId == clientId) .Where(r => r.ClientId == clientId)
@ -126,7 +126,7 @@ namespace IW4MAdmin.Plugins.Stats.Helpers
&& ranking.PerformanceMetric != null && ranking.PerformanceMetric != null
&& ranking.Newest && ranking.Newest
&& ranking.Client.TotalConnectionTime >= && ranking.Client.TotalConnectionTime >=
_configHandler.Configuration().TopPlayersMinPlayTime; _config.TopPlayersMinPlayTime;
} }
public async Task<int> GetTotalRankedPlayers(long serverId) public async Task<int> GetTotalRankedPlayers(long serverId)
@ -217,7 +217,7 @@ namespace IW4MAdmin.Plugins.Stats.Helpers
public async Task<List<TopStatsInfo>> GetTopStats(int start, int count, long? serverId = null) public async Task<List<TopStatsInfo>> GetTopStats(int start, int count, long? serverId = null)
{ {
if (_configHandler.Configuration().EnableAdvancedMetrics) if (_config.EnableAdvancedMetrics)
{ {
return await GetNewTopStats(start, count, serverId); return await GetNewTopStats(start, count, serverId);
} }
@ -570,7 +570,7 @@ namespace IW4MAdmin.Plugins.Stats.Helpers
{ {
clientStats = UpdateStats(clientStats, pl); clientStats = UpdateStats(clientStats, pl);
await SaveClientStats(clientStats); await SaveClientStats(clientStats);
if (_configHandler.Configuration().EnableAdvancedMetrics) if (_config.EnableAdvancedMetrics)
{ {
await UpdateHistoricalRanking(pl.ClientId, clientStats, serverId); await UpdateHistoricalRanking(pl.ClientId, clientStats, serverId);
} }
@ -973,7 +973,7 @@ namespace IW4MAdmin.Plugins.Stats.Helpers
// update their performance // update their performance
if ((DateTime.UtcNow - attackerStats.LastStatHistoryUpdate).TotalMinutes >= 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 try
{ {
@ -982,7 +982,7 @@ namespace IW4MAdmin.Plugins.Stats.Helpers
// for stat history update, but one is already processing that invalidates the original // for stat history update, but one is already processing that invalidates the original
await attackerStats.ProcessingHit.WaitAsync(Utilities.DefaultCommandTimeout, await attackerStats.ProcessingHit.WaitAsync(Utilities.DefaultCommandTimeout,
Plugin.ServerManager.CancellationToken); Plugin.ServerManager.CancellationToken);
if (_configHandler.Configuration().EnableAdvancedMetrics) if (_config.EnableAdvancedMetrics)
{ {
await UpdateHistoricalRanking(attacker.ClientId, attackerStats, serverId); 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) public async Task UpdateHistoricalRanking(int clientId, EFClientStatistics clientStats, long serverId)
{ {
await using var context = _contextFactory.CreateContext(); await using var context = _contextFactory.CreateContext();
var minPlayTime = _configHandler.Configuration().TopPlayersMinPlayTime; var minPlayTime = _config.TopPlayersMinPlayTime;
var performances = await context.Set<EFClientStatistics>() var performances = await context.Set<EFClientStatistics>()
.AsNoTracking() .AsNoTracking()
@ -1208,7 +1208,7 @@ namespace IW4MAdmin.Plugins.Stats.Helpers
var serverRanking = await context.Set<EFClientStatistics>() var serverRanking = await context.Set<EFClientStatistics>()
.Where(stats => stats.ClientId != clientStats.ClientId) .Where(stats => stats.ClientId != clientStats.ClientId)
.Where(AdvancedClientStatsResourceQueryHelper.GetRankingFunc( .Where(AdvancedClientStatsResourceQueryHelper.GetRankingFunc(
_configHandler.Configuration().TopPlayersMinPlayTime, clientStats.ZScore, serverId)) _config.TopPlayersMinPlayTime, clientStats.ZScore, serverId))
.CountAsync(); .CountAsync();
var serverRankingSnapshot = new EFClientRankingHistory var serverRankingSnapshot = new EFClientRankingHistory

View File

@ -442,7 +442,7 @@ namespace IW4MAdmin.Plugins.Stats
} }
ServerManager = manager; ServerManager = manager;
Manager = new StatManager(_managerLogger, manager, _databaseContextFactory, Config, _serverDistributionCalculator); Manager = new StatManager(_managerLogger, manager, _databaseContextFactory, Config.Configuration(), _serverDistributionCalculator);
await _serverDistributionCalculator.Initialize(); await _serverDistributionCalculator.Initialize();
} }

View File

@ -19,13 +19,12 @@ namespace WebfrontCore.Controllers
public class ClientController : BaseController public class ClientController : BaseController
{ {
private readonly IMetaService _metaService; private readonly IMetaService _metaService;
private readonly IConfigurationHandler<StatsConfiguration> _configurationHandler; private readonly StatsConfiguration _config;
public ClientController(IManager manager, IMetaService metaService, public ClientController(IManager manager, IMetaService metaService, StatsConfiguration config) : base(manager)
IConfigurationHandler<StatsConfiguration> configurationHandler) : base(manager)
{ {
_metaService = metaService; _metaService = metaService;
_configurationHandler = configurationHandler; _config = config;
} }
public async Task<IActionResult> ProfileAsync(int id, MetaType? metaFilterType) public async Task<IActionResult> ProfileAsync(int id, MetaType? metaFilterType)
@ -114,7 +113,7 @@ namespace WebfrontCore.Controllers
ViewBag.Title += " " + Localization["WEBFRONT_CLIENT_PROFILE_TITLE"]; ViewBag.Title += " " + Localization["WEBFRONT_CLIENT_PROFILE_TITLE"];
ViewBag.Description = $"Client information for {strippedName}"; ViewBag.Description = $"Client information for {strippedName}";
ViewBag.Keywords = $"IW4MAdmin, client, profile, {strippedName}"; ViewBag.Keywords = $"IW4MAdmin, client, profile, {strippedName}";
ViewBag.UseNewStats = _configurationHandler.Configuration()?.EnableAdvancedMetrics ?? true; ViewBag.UseNewStats = _config?.EnableAdvancedMetrics ?? true;
return View("Profile/Index", clientDto); return View("Profile/Index", clientDto);
} }

View File

@ -16,10 +16,10 @@ namespace WebfrontCore.Controllers
public ClientStatisticsController(IManager manager, public ClientStatisticsController(IManager manager,
IResourceQueryHelper<StatsInfoRequest, AdvancedStatsInfo> queryHelper, IResourceQueryHelper<StatsInfoRequest, AdvancedStatsInfo> queryHelper,
IConfigurationHandler<DefaultSettings> configurationHandler) : base(manager) DefaultSettings defaultConfig) : base(manager)
{ {
_queryHelper = queryHelper; _queryHelper = queryHelper;
_defaultConfig = configurationHandler.Configuration(); _defaultConfig = defaultConfig;
} }
[HttpGet("{id:int}/advanced")] [HttpGet("{id:int}/advanced")]

View File

@ -27,19 +27,18 @@ namespace IW4MAdmin.Plugins.Web.StatsWeb.Controllers
private readonly IResourceQueryHelper<ChatSearchQuery, MessageResponse> _chatResourceQueryHelper; private readonly IResourceQueryHelper<ChatSearchQuery, MessageResponse> _chatResourceQueryHelper;
private readonly ITranslationLookup _translationLookup; private readonly ITranslationLookup _translationLookup;
private readonly IDatabaseContextFactory _contextFactory; private readonly IDatabaseContextFactory _contextFactory;
private readonly IConfigurationHandler<StatsConfiguration> _configurationHandler; private readonly StatsConfiguration _config;
public StatsController(ILogger<StatsController> logger, IManager manager, IResourceQueryHelper<ChatSearchQuery, public StatsController(ILogger<StatsController> logger, IManager manager, IResourceQueryHelper<ChatSearchQuery,
MessageResponse> resourceQueryHelper, ITranslationLookup translationLookup, MessageResponse> resourceQueryHelper, ITranslationLookup translationLookup,
IDatabaseContextFactory contextFactory, IDatabaseContextFactory contextFactory, StatsConfiguration config) : base(manager)
IConfigurationHandler<StatsConfiguration> configurationHandler) : base(manager)
{ {
_logger = logger; _logger = logger;
_manager = manager; _manager = manager;
_chatResourceQueryHelper = resourceQueryHelper; _chatResourceQueryHelper = resourceQueryHelper;
_translationLookup = translationLookup; _translationLookup = translationLookup;
_contextFactory = contextFactory; _contextFactory = contextFactory;
_configurationHandler = configurationHandler; _config = config;
} }
[HttpGet] [HttpGet]
@ -70,7 +69,7 @@ namespace IW4MAdmin.Plugins.Web.StatsWeb.Controllers
serverId = StatManager.GetIdForServer(server); serverId = StatManager.GetIdForServer(server);
} }
var results = _configurationHandler.Configuration().EnableAdvancedMetrics var results = _config?.EnableAdvancedMetrics ?? true
? await Plugin.Manager.GetNewTopStats(offset, count, serverId) ? await Plugin.Manager.GetNewTopStats(offset, count, serverId)
: await Plugin.Manager.GetTopStats(offset, count, serverId); : await Plugin.Manager.GetTopStats(offset, count, serverId);
@ -80,7 +79,7 @@ namespace IW4MAdmin.Plugins.Web.StatsWeb.Controllers
return Ok(); return Ok();
} }
ViewBag.UseNewStats = _configurationHandler.Configuration().EnableAdvancedMetrics; ViewBag.UseNewStats = _config?.EnableAdvancedMetrics;
return View("~/Views/Client/Statistics/Components/TopPlayers/_List.cshtml", results); return View("~/Views/Client/Statistics/Components/TopPlayers/_List.cshtml", results);
} }

View File

@ -129,7 +129,7 @@ namespace WebfrontCore
services.AddSingleton(Program.ApplicationServiceProvider services.AddSingleton(Program.ApplicationServiceProvider
.GetRequiredService<IConfigurationHandler<DefaultSettings>>()); .GetRequiredService<IConfigurationHandler<DefaultSettings>>());
services.AddSingleton(Program.ApplicationServiceProvider services.AddSingleton(Program.ApplicationServiceProvider
.GetRequiredService<IConfigurationHandler<StatsConfiguration>>()); .GetRequiredService<StatsConfiguration>());
services.AddSingleton(Program.ApplicationServiceProvider.GetRequiredService<IServerDataViewer>()); services.AddSingleton(Program.ApplicationServiceProvider.GetRequiredService<IServerDataViewer>());
} }

View File

@ -3,18 +3,17 @@ using System.Threading.Tasks;
using IW4MAdmin.Plugins.Stats; using IW4MAdmin.Plugins.Stats;
using IW4MAdmin.Plugins.Stats.Helpers; using IW4MAdmin.Plugins.Stats.Helpers;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using SharedLibraryCore.Interfaces;
using Stats.Config; using Stats.Config;
namespace WebfrontCore.ViewComponents namespace WebfrontCore.ViewComponents
{ {
public class TopPlayersViewComponent : ViewComponent public class TopPlayersViewComponent : ViewComponent
{ {
private readonly IConfigurationHandler<StatsConfiguration> _configurationHandler; private readonly StatsConfiguration _config;
public TopPlayersViewComponent(IConfigurationHandler<StatsConfiguration> configurationHandler) public TopPlayersViewComponent(StatsConfiguration config)
{ {
_configurationHandler = configurationHandler; _config = config;
} }
public async Task<IViewComponentResult> InvokeAsync(int count, int offset, long? serverId = null) public async Task<IViewComponentResult> 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", return View("~/Views/Client/Statistics/Components/TopPlayers/_List.cshtml",
ViewBag.UseNewStats ViewBag.UseNewStats
? await Plugin.Manager.GetNewTopStats(offset, count, serverId) ? await Plugin.Manager.GetNewTopStats(offset, count, serverId)