From 9b014fa01c45b56bf28efbb8371980594499a14d Mon Sep 17 00:00:00 2001 From: W0lf4llo <96880844+W0lf4llo@users.noreply.github.com> Date: Thu, 16 Mar 2023 23:51:55 -0400 Subject: [PATCH] StasticsHelper + Community Tab Performance Fix (#713) * Player count shows per game * Fixed LBP3 categories lagigng & StatisticsHelper Co-authored-by: koko * Fixed "planetStats" mmPicksCount * Hopefully the final update to this PR! (Bug fixes) * Update ProjectLighthouse/Types/Entities/Level/Slot.cs Co-authored-by: koko <68549366+sudokoko@users.noreply.github.com> * Update ProjectLighthouse.Servers.GameServer/Controllers/Slots/SlotsController.cs I'm trusting you that nothing breaks here lol Co-authored-by: Josh * Update ProjectLighthouse.Servers.GameServer/Controllers/Slots/SlotsController.cs bad slendy >:( Co-authored-by: koko <68549366+sudokoko@users.noreply.github.com> --------- Co-authored-by: koko Co-authored-by: koko <68549366+sudokoko@users.noreply.github.com> Co-authored-by: Josh --- .../Controllers/Slots/SlotsController.cs | 2 +- .../Controllers/StatisticsController.cs | 6 ++++-- .../Types/Categories/CategoryWithUser.cs | 4 ++-- .../Types/Categories/CustomCategory.cs | 2 +- .../Types/Categories/HeartedCategory.cs | 2 +- .../Types/Categories/LuckyDipCategory.cs | 2 +- .../Types/Categories/MostPlayedCategory.cs | 2 +- .../Types/Categories/NewestLevelsCategory.cs | 2 +- .../Types/Categories/QueueCategory.cs | 2 +- .../Types/Categories/TeamPicksCategory.cs | 2 +- ProjectLighthouse/Helpers/StatisticsHelper.cs | 5 +++++ ProjectLighthouse/Types/Entities/Level/Slot.cs | 1 - 12 files changed, 19 insertions(+), 13 deletions(-) diff --git a/ProjectLighthouse.Servers.GameServer/Controllers/Slots/SlotsController.cs b/ProjectLighthouse.Servers.GameServer/Controllers/Slots/SlotsController.cs index f4b67c83..3a8f9047 100644 --- a/ProjectLighthouse.Servers.GameServer/Controllers/Slots/SlotsController.cs +++ b/ProjectLighthouse.Servers.GameServer/Controllers/Slots/SlotsController.cs @@ -280,7 +280,7 @@ public class SlotsController : ControllerBase .Take(Math.Min(pageSize, 30)); string response = Enumerable.Aggregate(slots, string.Empty, (current, slot) => current + slot.Serialize(gameVersion)); int start = pageStart + Math.Min(pageSize, ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots); - int total = await StatisticsHelper.TeamPickCount(this.database); + int total = await StatisticsHelper.TeamPickCountForGame(this.database, token.GameVersion); return this.Ok(generateSlotsResponse(response, start, total)); } diff --git a/ProjectLighthouse.Servers.GameServer/Controllers/StatisticsController.cs b/ProjectLighthouse.Servers.GameServer/Controllers/StatisticsController.cs index 399b36f8..b8af87ff 100644 --- a/ProjectLighthouse.Servers.GameServer/Controllers/StatisticsController.cs +++ b/ProjectLighthouse.Servers.GameServer/Controllers/StatisticsController.cs @@ -22,14 +22,16 @@ public class StatisticsController : ControllerBase } [HttpGet("playersInPodCount")] + public IActionResult PlayersInPodCount() => this.Ok((StatisticsHelper.UserCountInPod(this.database)).ToString()); + [HttpGet("totalPlayerCount")] - public async Task TotalPlayerCount() => this.Ok((await StatisticsHelper.RecentMatches(this.database)).ToString()); + public async Task TotalPlayerCount() => this.Ok((await StatisticsHelper.RecentMatchesForGame(this.database, this.GetToken().GameVersion)).ToString()); [HttpGet("planetStats")] public async Task PlanetStats() { int totalSlotCount = await StatisticsHelper.SlotCountForGame(this.database, this.GetToken().GameVersion); - int mmPicksCount = await StatisticsHelper.TeamPickCount(this.database); + int mmPicksCount = await StatisticsHelper.TeamPickCountForGame(this.database, this.GetToken().GameVersion); return this.Ok ( diff --git a/ProjectLighthouse.Servers.GameServer/Types/Categories/CategoryWithUser.cs b/ProjectLighthouse.Servers.GameServer/Types/Categories/CategoryWithUser.cs index 42f8b6f7..1e3f1b86 100644 --- a/ProjectLighthouse.Servers.GameServer/Types/Categories/CategoryWithUser.cs +++ b/ProjectLighthouse.Servers.GameServer/Types/Categories/CategoryWithUser.cs @@ -32,8 +32,8 @@ public abstract class CategoryWithUser : Category return -1; } - public abstract IEnumerable GetSlots(DatabaseContext database, User user, int pageStart, int pageSize); - public override IEnumerable GetSlots(DatabaseContext database, int pageStart, int pageSize) + public abstract IQueryable GetSlots(DatabaseContext database, User user, int pageStart, int pageSize); + public override IList GetSlots(DatabaseContext database, int pageStart, int pageSize) { #if DEBUG Logger.Error("tried to get slots without user on CategoryWithUser", LogArea.Category); diff --git a/ProjectLighthouse.Servers.GameServer/Types/Categories/CustomCategory.cs b/ProjectLighthouse.Servers.GameServer/Types/Categories/CustomCategory.cs index 792d36b5..8fb2033a 100644 --- a/ProjectLighthouse.Servers.GameServer/Types/Categories/CustomCategory.cs +++ b/ProjectLighthouse.Servers.GameServer/Types/Categories/CustomCategory.cs @@ -36,7 +36,7 @@ public class CustomCategory : Category public sealed override string IconHash { get; set; } public sealed override string Endpoint { get; set; } public override Slot? GetPreviewSlot(DatabaseContext database) => database.Slots.FirstOrDefault(s => s.SlotId == this.SlotIds[0]); - public override IEnumerable GetSlots + public override IQueryable GetSlots (DatabaseContext database, int pageStart, int pageSize) => database.Slots.ByGameVersion(GameVersion.LittleBigPlanet3).Where(s => this.SlotIds.Contains(s.SlotId)); public override int GetTotalSlots(DatabaseContext database) => this.SlotIds.Count; diff --git a/ProjectLighthouse.Servers.GameServer/Types/Categories/HeartedCategory.cs b/ProjectLighthouse.Servers.GameServer/Types/Categories/HeartedCategory.cs index 47936078..80aaccba 100644 --- a/ProjectLighthouse.Servers.GameServer/Types/Categories/HeartedCategory.cs +++ b/ProjectLighthouse.Servers.GameServer/Types/Categories/HeartedCategory.cs @@ -24,7 +24,7 @@ public class HeartedCategory : CategoryWithUser .ByGameVersion(GameVersion.LittleBigPlanet3, false, false, true) .FirstOrDefault(); - public override IEnumerable GetSlots(DatabaseContext database, User user, int pageStart, int pageSize) + public override IQueryable GetSlots(DatabaseContext database, User user, int pageStart, int pageSize) => database.HeartedLevels.Where(h => h.UserId == user.UserId) .Where(h => h.Slot.Type == SlotType.User && !h.Slot.Hidden && h.Slot.GameVersion <= GameVersion.LittleBigPlanet3) .OrderByDescending(h => h.HeartedLevelId) diff --git a/ProjectLighthouse.Servers.GameServer/Types/Categories/LuckyDipCategory.cs b/ProjectLighthouse.Servers.GameServer/Types/Categories/LuckyDipCategory.cs index 760c1570..7d078a42 100644 --- a/ProjectLighthouse.Servers.GameServer/Types/Categories/LuckyDipCategory.cs +++ b/ProjectLighthouse.Servers.GameServer/Types/Categories/LuckyDipCategory.cs @@ -15,7 +15,7 @@ public class LuckyDipCategory : Category public override string IconHash { get; set; } = "g820605"; public override string Endpoint { get; set; } = "lbp2luckydip"; public override Slot? GetPreviewSlot(DatabaseContext database) => database.Slots.Where(s => s.Type == SlotType.User).OrderByDescending(_ => EF.Functions.Random()).FirstOrDefault(); - public override IEnumerable GetSlots + public override IQueryable GetSlots (DatabaseContext database, int pageStart, int pageSize) => database.Slots.ByGameVersion(GameVersion.LittleBigPlanet3, false, true) .OrderByDescending(_ => EF.Functions.Random()) diff --git a/ProjectLighthouse.Servers.GameServer/Types/Categories/MostPlayedCategory.cs b/ProjectLighthouse.Servers.GameServer/Types/Categories/MostPlayedCategory.cs index 74fa4e9e..eb9772b4 100644 --- a/ProjectLighthouse.Servers.GameServer/Types/Categories/MostPlayedCategory.cs +++ b/ProjectLighthouse.Servers.GameServer/Types/Categories/MostPlayedCategory.cs @@ -18,7 +18,7 @@ public class MostPlayedCategory : Category .OrderByDescending(s => s.PlaysLBP1Unique + s.PlaysLBP2Unique + s.PlaysLBP3Unique) .ThenByDescending(s => s.PlaysLBP1 + s.PlaysLBP2 + s.PlaysLBP3) .FirstOrDefault(); - public override IEnumerable GetSlots + public override IQueryable GetSlots (DatabaseContext database, int pageStart, int pageSize) => database.Slots.ByGameVersion(GameVersion.LittleBigPlanet3, false, true) .OrderByDescending(s => s.PlaysLBP1Unique + s.PlaysLBP2Unique + s.PlaysLBP3Unique) diff --git a/ProjectLighthouse.Servers.GameServer/Types/Categories/NewestLevelsCategory.cs b/ProjectLighthouse.Servers.GameServer/Types/Categories/NewestLevelsCategory.cs index b432db26..8331076a 100644 --- a/ProjectLighthouse.Servers.GameServer/Types/Categories/NewestLevelsCategory.cs +++ b/ProjectLighthouse.Servers.GameServer/Types/Categories/NewestLevelsCategory.cs @@ -14,7 +14,7 @@ public class NewestLevelsCategory : Category public override string IconHash { get; set; } = "g820623"; public override string Endpoint { get; set; } = "newest"; public override Slot? GetPreviewSlot(DatabaseContext database) => database.Slots.Where(s => s.Type == SlotType.User).OrderByDescending(s => s.FirstUploaded).FirstOrDefault(); - public override IEnumerable GetSlots + public override IQueryable GetSlots (DatabaseContext database, int pageStart, int pageSize) => database.Slots.ByGameVersion(GameVersion.LittleBigPlanet3, false, true) .OrderByDescending(s => s.FirstUploaded) diff --git a/ProjectLighthouse.Servers.GameServer/Types/Categories/QueueCategory.cs b/ProjectLighthouse.Servers.GameServer/Types/Categories/QueueCategory.cs index 29ef6b26..ffbc4bf8 100644 --- a/ProjectLighthouse.Servers.GameServer/Types/Categories/QueueCategory.cs +++ b/ProjectLighthouse.Servers.GameServer/Types/Categories/QueueCategory.cs @@ -24,7 +24,7 @@ public class QueueCategory : CategoryWithUser .ByGameVersion(GameVersion.LittleBigPlanet3, false, false, true) .FirstOrDefault(); - public override IEnumerable GetSlots(DatabaseContext database, User user, int pageStart, int pageSize) + public override IQueryable GetSlots(DatabaseContext database, User user, int pageStart, int pageSize) => database.QueuedLevels.Where(q => q.UserId == user.UserId) .Where(q => q.Slot.Type == SlotType.User && !q.Slot.Hidden && q.Slot.GameVersion <= GameVersion.LittleBigPlanet3) .OrderByDescending(q => q.QueuedLevelId) diff --git a/ProjectLighthouse.Servers.GameServer/Types/Categories/TeamPicksCategory.cs b/ProjectLighthouse.Servers.GameServer/Types/Categories/TeamPicksCategory.cs index bb8cfb29..62a18419 100644 --- a/ProjectLighthouse.Servers.GameServer/Types/Categories/TeamPicksCategory.cs +++ b/ProjectLighthouse.Servers.GameServer/Types/Categories/TeamPicksCategory.cs @@ -14,7 +14,7 @@ public class TeamPicksCategory : Category public override string IconHash { get; set; } = "g820626"; public override string Endpoint { get; set; } = "team_picks"; public override Slot? GetPreviewSlot(DatabaseContext database) => database.Slots.OrderByDescending(s => s.FirstUploaded).FirstOrDefault(s => s.TeamPick); - public override IEnumerable GetSlots + public override IQueryable GetSlots (DatabaseContext database, int pageStart, int pageSize) => database.Slots.ByGameVersion(GameVersion.LittleBigPlanet3, false, true) .OrderByDescending(s => s.FirstUploaded) diff --git a/ProjectLighthouse/Helpers/StatisticsHelper.cs b/ProjectLighthouse/Helpers/StatisticsHelper.cs index 4de4aecb..8daf2cd3 100644 --- a/ProjectLighthouse/Helpers/StatisticsHelper.cs +++ b/ProjectLighthouse/Helpers/StatisticsHelper.cs @@ -3,6 +3,7 @@ using System.Threading.Tasks; using LBPUnion.ProjectLighthouse.Database; using LBPUnion.ProjectLighthouse.Extensions; using LBPUnion.ProjectLighthouse.Types.Levels; +using LBPUnion.ProjectLighthouse.Types.Matchmaking.Rooms; using LBPUnion.ProjectLighthouse.Types.Users; using Microsoft.EntityFrameworkCore; @@ -22,8 +23,12 @@ public static class StatisticsHelper public static async Task UserCount(DatabaseContext database) => await database.Users.CountAsync(u => u.PermissionLevel != PermissionLevel.Banned); + public static int UserCountInPod(DatabaseContext database) => RoomHelper.Rooms.Count(r => r.State == RoomState.Idle); + public static async Task TeamPickCount(DatabaseContext database) => await database.Slots.CountAsync(s => s.TeamPick); + public static async Task TeamPickCountForGame(DatabaseContext database, GameVersion gameVersion) => await database.Slots.ByGameVersion(gameVersion).CountAsync(s => s.TeamPick); + public static async Task PhotoCount(DatabaseContext database) => await database.Photos.CountAsync(); #region Moderator/Admin specific diff --git a/ProjectLighthouse/Types/Entities/Level/Slot.cs b/ProjectLighthouse/Types/Entities/Level/Slot.cs index 6644b743..98897563 100644 --- a/ProjectLighthouse/Types/Entities/Level/Slot.cs +++ b/ProjectLighthouse/Types/Entities/Level/Slot.cs @@ -163,7 +163,6 @@ public class Slot [XmlIgnore] public bool TeamPick { get; set; } - [XmlIgnore] public GameVersion GameVersion { get; set; }