From c21bf2ebf1e8a9a828e6a41724316e7a0dd21e03 Mon Sep 17 00:00:00 2001 From: RaidMax Date: Tue, 26 Feb 2019 21:25:27 -0600 Subject: [PATCH] continue working on per servver topstats --- Plugins/Stats/Controllers/StatsController.cs | 37 ++++++++++++++--- Plugins/Stats/Dtos/TopStatsInfo.cs | 1 - Plugins/Stats/Helpers/StatManager.cs | 6 +-- Plugins/Stats/Plugin.cs | 2 +- .../ViewComponents/TopPlayersViewComponent.cs | 28 +++++++++++++ Plugins/Web/StatsWeb/StatsWeb.csproj | 6 +-- .../{ => Components/TopPlayers}/_List.cshtml | 4 ++ Plugins/Web/StatsWeb/Views/Stats/Index.cshtml | 41 +++++-------------- WebfrontCore/Controllers/ActionController.cs | 2 +- WebfrontCore/Views/Server/_Server.cshtml | 2 +- .../Components/PenaltyList/_List.cshtml | 2 +- .../Shared/Components/ServerList/_List.cshtml | 2 +- WebfrontCore/WebfrontCore.csproj | 10 ++++- WebfrontCore/wwwroot/js/loader.js | 4 -- WebfrontCore/wwwroot/js/stats.js | 4 ++ 15 files changed, 96 insertions(+), 55 deletions(-) create mode 100644 Plugins/Stats/ViewComponents/TopPlayersViewComponent.cs rename Plugins/Web/StatsWeb/Views/Stats/{ => Components/TopPlayers}/_List.cshtml (95%) diff --git a/Plugins/Stats/Controllers/StatsController.cs b/Plugins/Stats/Controllers/StatsController.cs index eb37c8bcb..bbc2f77dd 100644 --- a/Plugins/Stats/Controllers/StatsController.cs +++ b/Plugins/Stats/Controllers/StatsController.cs @@ -1,4 +1,5 @@ -using Microsoft.AspNetCore.Authorization; +using IW4MAdmin.Plugins.Stats.Helpers; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using SharedLibraryCore; @@ -13,19 +14,43 @@ namespace IW4MAdmin.Plugins.Stats.Web.Controllers public class StatsController : BaseController { [HttpGet] - public async Task TopPlayersAsync() + public IActionResult TopPlayersAsync() { ViewBag.Title = Utilities.CurrentLocalization.LocalizationIndex.Set["WEBFRONT_STATS_INDEX_TITLE"]; ViewBag.Description = Utilities.CurrentLocalization.LocalizationIndex.Set["WEBFRONT_STATS_INDEX_DESC"]; ViewBag.Servers = Manager.GetServers().Select(_server => new ServerInfo() { Name = _server.Hostname, ID = _server.GetHashCode() }); - return View("Index", await Plugin.Manager.GetTopStats(0, 50)); + return View("Index"); } [HttpGet] - public async Task GetTopPlayersAsync(int count, int offset) + public async Task GetTopPlayersAsync(int count, int offset, long? serverId = null) { - return View("_List", await Plugin.Manager.GetTopStats(offset, count)); + // this prevents empty results when we really want aggregate + if (serverId == 0) + { + serverId = null; + } + + var server = Manager.GetServers().FirstOrDefault(_server => _server.EndPoint == serverId); + + if (server != null) + { + serverId = await StatManager.GetIdForServer(server); + } + + var results = await Plugin.Manager.GetTopStats(offset, count, serverId); + + // this returns an empty result so we know to stale the loader + if (results.Count == 0 && offset > 0) + { + return Ok(); + } + + else + { + return View("Components/TopPlayers/_List", results); + } } [HttpGet] @@ -40,7 +65,7 @@ namespace IW4MAdmin.Plugins.Stats.Web.Controllers where message.ServerId == serverId where message.TimeSent >= whenLower where message.TimeSent <= whenUpper - select new SharedLibraryCore.Dtos.ChatInfo() + select new ChatInfo() { ClientId = message.ClientId, Message = message.Message, diff --git a/Plugins/Stats/Dtos/TopStatsInfo.cs b/Plugins/Stats/Dtos/TopStatsInfo.cs index f49e03f32..22fa8a8f0 100644 --- a/Plugins/Stats/Dtos/TopStatsInfo.cs +++ b/Plugins/Stats/Dtos/TopStatsInfo.cs @@ -18,6 +18,5 @@ namespace IW4MAdmin.Plugins.Stats.Web.Dtos public int Deaths { get; set; } public int RatingChange { get; set; } public List PerformanceHistory { get; set; } - public List Servers { get; set; } } } diff --git a/Plugins/Stats/Helpers/StatManager.cs b/Plugins/Stats/Helpers/StatManager.cs index 66e9a7757..871b99d74 100644 --- a/Plugins/Stats/Helpers/StatManager.cs +++ b/Plugins/Stats/Helpers/StatManager.cs @@ -82,13 +82,13 @@ namespace IW4MAdmin.Plugins.Stats.Helpers } } - public async Task> GetTopStats(int start, int count) + public async Task> GetTopStats(int start, int count, long? serverId = null) { using (var context = new DatabaseContext(true)) { // setup the query for the clients within the given rating range var iqClientRatings = (from rating in context.Set() - .Where(GetRankingFunc()) + .Where(GetRankingFunc(serverId)) select new { rating.RatingHistory.ClientId, @@ -113,7 +113,7 @@ namespace IW4MAdmin.Plugins.Stats.Helpers var iqRatingInfo = from rating in context.Set() where clientIds.Contains(rating.RatingHistory.ClientId) - where rating.ServerId == null + where rating.ServerId == serverId select new { rating.Ranking, diff --git a/Plugins/Stats/Plugin.cs b/Plugins/Stats/Plugin.cs index c93ecc53d..3012f5b38 100644 --- a/Plugins/Stats/Plugin.cs +++ b/Plugins/Stats/Plugin.cs @@ -26,7 +26,7 @@ namespace IW4MAdmin.Plugins.Stats public string Author => "RaidMax"; public static StatManager Manager { get; private set; } - private IManager ServerManager; + public static IManager ServerManager; public static BaseConfigurationHandler Config { get; private set; } public async Task OnEventAsync(GameEvent E, Server S) diff --git a/Plugins/Stats/ViewComponents/TopPlayersViewComponent.cs b/Plugins/Stats/ViewComponents/TopPlayersViewComponent.cs new file mode 100644 index 000000000..88e43bccb --- /dev/null +++ b/Plugins/Stats/ViewComponents/TopPlayersViewComponent.cs @@ -0,0 +1,28 @@ +using IW4MAdmin.Plugins.Stats; +using IW4MAdmin.Plugins.Stats.Helpers; +using Microsoft.AspNetCore.Mvc; +using System.Linq; +using System.Threading.Tasks; + +namespace Stats.ViewComponents +{ + public class TopPlayersViewComponent : ViewComponent + { + public async Task InvokeAsync(int count, int offset, long? serverId = null) + { + if (serverId == 0) + { + serverId = null; + } + + var server = Plugin.ServerManager.GetServers().FirstOrDefault(_server => _server.EndPoint == serverId); + + if (server != null) + { + serverId = await StatManager.GetIdForServer(server); + } + + return View("_List", await Plugin.Manager.GetTopStats(offset, count, serverId)); + } + } +} diff --git a/Plugins/Web/StatsWeb/StatsWeb.csproj b/Plugins/Web/StatsWeb/StatsWeb.csproj index 51f178402..c0e420009 100644 --- a/Plugins/Web/StatsWeb/StatsWeb.csproj +++ b/Plugins/Web/StatsWeb/StatsWeb.csproj @@ -1,7 +1,7 @@  netcoreapp2.2 - false + true true RazorSdk Debug;Release;Prerelease @@ -19,10 +19,6 @@ - - - - diff --git a/Plugins/Web/StatsWeb/Views/Stats/_List.cshtml b/Plugins/Web/StatsWeb/Views/Stats/Components/TopPlayers/_List.cshtml similarity index 95% rename from Plugins/Web/StatsWeb/Views/Stats/_List.cshtml rename to Plugins/Web/StatsWeb/Views/Stats/Components/TopPlayers/_List.cshtml index 66588a0f3..c67bee115 100644 --- a/Plugins/Web/StatsWeb/Views/Stats/_List.cshtml +++ b/Plugins/Web/StatsWeb/Views/Stats/Components/TopPlayers/_List.cshtml @@ -25,6 +25,10 @@ return "0_no-place/menu_div_no_place.png"; } } +@if (Model.Count == 0) +{ +
@SharedLibraryCore.Utilities.CurrentLocalization.LocalizationIndex.Set["PLUGINS_STATS_TEXT_NOQUALIFY"]
+} @foreach (var stat in Model) {
diff --git a/Plugins/Web/StatsWeb/Views/Stats/Index.cshtml b/Plugins/Web/StatsWeb/Views/Stats/Index.cshtml index 2d82a5a92..e9e361f46 100644 --- a/Plugins/Web/StatsWeb/Views/Stats/Index.cshtml +++ b/Plugins/Web/StatsWeb/Views/Stats/Index.cshtml @@ -1,53 +1,34 @@ -@model List - -
- @Html.Partial("../Server/_ClientActivity", Model) + @await Html.PartialAsync("../Server/_ClientActivity", Model)
diff --git a/WebfrontCore/Views/Shared/Components/PenaltyList/_List.cshtml b/WebfrontCore/Views/Shared/Components/PenaltyList/_List.cshtml index 70b50c4d3..63f6c2ac6 100644 --- a/WebfrontCore/Views/Shared/Components/PenaltyList/_List.cshtml +++ b/WebfrontCore/Views/Shared/Components/PenaltyList/_List.cshtml @@ -6,6 +6,6 @@ @{ foreach (var penalty in Model) { - Html.RenderPartial("_Penalty", penalty); + await Html.RenderPartialAsync("_Penalty", penalty); } } \ No newline at end of file diff --git a/WebfrontCore/Views/Shared/Components/ServerList/_List.cshtml b/WebfrontCore/Views/Shared/Components/ServerList/_List.cshtml index e31f16f35..6e0b273d4 100644 --- a/WebfrontCore/Views/Shared/Components/ServerList/_List.cshtml +++ b/WebfrontCore/Views/Shared/Components/ServerList/_List.cshtml @@ -6,6 +6,6 @@ @{ foreach (var s in Model) { - Html.RenderPartial("../Server/_Server", s); + await Html.RenderPartialAsync("../Server/_Server", s); } } \ No newline at end of file diff --git a/WebfrontCore/WebfrontCore.csproj b/WebfrontCore/WebfrontCore.csproj index 94693f988..d30b9ee91 100644 --- a/WebfrontCore/WebfrontCore.csproj +++ b/WebfrontCore/WebfrontCore.csproj @@ -3,7 +3,7 @@ netcoreapp2.2 2.2.2 - false + true true true 2.6 @@ -33,12 +33,20 @@ + + + + + + + + diff --git a/WebfrontCore/wwwroot/js/loader.js b/WebfrontCore/wwwroot/js/loader.js index 15682520e..b6513b7d8 100644 --- a/WebfrontCore/wwwroot/js/loader.js +++ b/WebfrontCore/wwwroot/js/loader.js @@ -64,10 +64,6 @@ if ($(loaderResponseId).length === 1) { $window .off('scroll', ScrollHandler) .on('scroll', ScrollHandler); - - /*$('#load_penalties_button').click(function () { - loadMorePenalties(); - });*/ }); function ScrollHandler(e) { diff --git a/WebfrontCore/wwwroot/js/stats.js b/WebfrontCore/wwwroot/js/stats.js index 3d31e2147..bb80a74db 100644 --- a/WebfrontCore/wwwroot/js/stats.js +++ b/WebfrontCore/wwwroot/js/stats.js @@ -72,6 +72,10 @@ $(document).ready(function () { getStatsChart($(element).attr('id'), $(element).width(), $(element).height()).render(); }); }); + + $('.top-players-link').click(function (event) { + initLoader('/Stats/GetTopPlayersAsync?serverId=' + $(this).data('serverid'), $(this).attr('href'), 50); + }); }); $(document).on("loaderFinished", function (event, response) {