From c8dc84e02975b3a75c2407ee977cecaeceac0bd5 Mon Sep 17 00:00:00 2001 From: jvyden Date: Tue, 11 Jan 2022 02:23:45 -0500 Subject: [PATCH] Fix CategoryWithUser not pulling slots & total slots properly --- .../Controllers/CollectionController.cs | 16 ++++++++-- ProjectLighthouse/Helpers/CollectionHelper.cs | 14 +++++++++ .../Types/Categories/CategoryWithUser.cs | 10 ++++++ .../Types/Categories/CustomCategory.cs | 31 +++++++++++++++++++ .../Types/Categories/NewestLevelsCategory.cs | 2 +- .../Types/Categories/QueueCategory.cs | 4 ++- .../Types/Categories/TeamPicksCategory.cs | 2 +- 7 files changed, 74 insertions(+), 5 deletions(-) create mode 100644 ProjectLighthouse/Types/Categories/CustomCategory.cs diff --git a/ProjectLighthouse/Controllers/CollectionController.cs b/ProjectLighthouse/Controllers/CollectionController.cs index 6be7c155..d6c182a0 100644 --- a/ProjectLighthouse/Controllers/CollectionController.cs +++ b/ProjectLighthouse/Controllers/CollectionController.cs @@ -82,7 +82,19 @@ namespace LBPUnion.ProjectLighthouse.Controllers Logger.Log("Found category " + category, LoggerLevelCategory.Instance); - List slots = category.GetSlots(this.database, pageStart, pageSize).ToList(); + List slots; + int totalSlots; + + if (category is CategoryWithUser categoryWithUser) + { + slots = categoryWithUser.GetSlots(this.database, user, pageStart, pageSize).ToList(); + totalSlots = categoryWithUser.GetTotalSlots(this.database, user); + } + else + { + slots = category.GetSlots(this.database, pageStart, pageSize).ToList(); + totalSlots = category.GetTotalSlots(this.database); + } string slotsSerialized = slots.Aggregate(string.Empty, (current, slot) => current + slot.Serialize()); @@ -95,7 +107,7 @@ namespace LBPUnion.ProjectLighthouse.Controllers new Dictionary { { - "total", category.GetTotalSlots(this.database) + "total", totalSlots }, { "hint_start", pageStart + pageSize diff --git a/ProjectLighthouse/Helpers/CollectionHelper.cs b/ProjectLighthouse/Helpers/CollectionHelper.cs index 4e1c7985..bf8a7ff0 100644 --- a/ProjectLighthouse/Helpers/CollectionHelper.cs +++ b/ProjectLighthouse/Helpers/CollectionHelper.cs @@ -12,6 +12,20 @@ namespace LBPUnion.ProjectLighthouse.Helpers Categories.Add(new TeamPicksCategory()); Categories.Add(new NewestLevelsCategory()); Categories.Add(new QueueCategory()); + Categories.Add + ( + new CustomCategory + ( + "Custom Category", + "This is a custom category!", + "custom1", + "g820614", + new[] + { + 35, 37, 2979, 3042, + } + ) + ); } } } \ No newline at end of file diff --git a/ProjectLighthouse/Types/Categories/CategoryWithUser.cs b/ProjectLighthouse/Types/Categories/CategoryWithUser.cs index 76fea879..fee2f4d0 100644 --- a/ProjectLighthouse/Types/Categories/CategoryWithUser.cs +++ b/ProjectLighthouse/Types/Categories/CategoryWithUser.cs @@ -30,6 +30,16 @@ public abstract class CategoryWithUser : Category return -1; } + public abstract IEnumerable GetSlots(Database database, User user, int pageStart, int pageSize); + public override IEnumerable GetSlots(Database database, int pageStart, int pageSize) + { + #if DEBUG + Logger.Log("tried to get slots without user on CategoryWithUser", LoggerLevelCategory.Instance); + if (Debugger.IsAttached) Debugger.Break(); + #endif + return new List(); + } + public new string Serialize(Database database) { Logger.Log("tried to serialize without user on CategoryWithUser", LoggerLevelCategory.Instance); diff --git a/ProjectLighthouse/Types/Categories/CustomCategory.cs b/ProjectLighthouse/Types/Categories/CustomCategory.cs new file mode 100644 index 00000000..1a2feeae --- /dev/null +++ b/ProjectLighthouse/Types/Categories/CustomCategory.cs @@ -0,0 +1,31 @@ +#nullable enable +using System.Collections.Generic; +using System.Linq; +using LBPUnion.ProjectLighthouse.Types.Levels; + +namespace LBPUnion.ProjectLighthouse.Types.Categories +{ + public class CustomCategory : Category + { + public CustomCategory(string name, string description, string endpoint, string icon, IEnumerable slotIds) + { + this.Name = name; + this.Description = description; + this.IconHash = icon; + this.Endpoint = endpoint; + + this.SlotIds = slotIds.ToList(); + } + + public List SlotIds; + + public sealed override string Name { get; set; } + public sealed override string Description { get; set; } + public sealed override string IconHash { get; set; } + public sealed override string Endpoint { get; set; } + public override Slot? GetPreviewSlot(Database database) => database.Slots.FirstOrDefault(s => s.SlotId == this.SlotIds[0]); + public override IEnumerable GetSlots(Database database, int pageStart, int pageSize) + => database.Slots.Where(s => this.SlotIds.Contains(s.SlotId)); + public override int GetTotalSlots(Database database) => this.SlotIds.Count; + } +} \ No newline at end of file diff --git a/ProjectLighthouse/Types/Categories/NewestLevelsCategory.cs b/ProjectLighthouse/Types/Categories/NewestLevelsCategory.cs index fffd85a1..e010b2d1 100644 --- a/ProjectLighthouse/Types/Categories/NewestLevelsCategory.cs +++ b/ProjectLighthouse/Types/Categories/NewestLevelsCategory.cs @@ -15,7 +15,7 @@ namespace LBPUnion.ProjectLighthouse.Types.Categories public override Slot? GetPreviewSlot(Database database) => database.Slots.OrderByDescending(s => s.FirstUploaded).FirstOrDefault(); public override IEnumerable GetSlots (Database database, int pageStart, int pageSize) - => database.Slots.OrderByDescending(s => s.FirstUploaded).Skip(pageStart).Take(Math.Min(pageSize, 20)); + => database.Slots.OrderByDescending(s => s.FirstUploaded).Skip(pageStart - 1).Take(Math.Min(pageSize, 20)); public override int GetTotalSlots(Database database) => database.Slots.Count(); } } \ No newline at end of file diff --git a/ProjectLighthouse/Types/Categories/QueueCategory.cs b/ProjectLighthouse/Types/Categories/QueueCategory.cs index c8c24c30..274f8023 100644 --- a/ProjectLighthouse/Types/Categories/QueueCategory.cs +++ b/ProjectLighthouse/Types/Categories/QueueCategory.cs @@ -1,4 +1,5 @@ #nullable enable +using System; using System.Collections.Generic; using System.Linq; using LBPUnion.ProjectLighthouse.Types.Levels; @@ -13,10 +14,11 @@ namespace LBPUnion.ProjectLighthouse.Types.Categories public override string IconHash { get; set; } = "g820614"; public override string Endpoint { get; set; } = "queue"; - public override IEnumerable GetSlots(Database database, int pageStart, int pageSize) => new List(); public override Slot? GetPreviewSlot(Database database, User user) => database.QueuedLevels.Include(q => q.Slot).FirstOrDefault(q => q.UserId == user.UserId)?.Slot; + public override IEnumerable GetSlots(Database database, User user, int pageStart, int pageSize) + => database.QueuedLevels.Include(q => q.Slot).Include(q => q.Slot.Location).Select(q => q.Slot).Skip(pageStart - 1).Take(Math.Min(pageSize, 20)); public override int GetTotalSlots(Database database, User user) => database.QueuedLevels.Count(q => q.UserId == user.UserId); } diff --git a/ProjectLighthouse/Types/Categories/TeamPicksCategory.cs b/ProjectLighthouse/Types/Categories/TeamPicksCategory.cs index 5bb6f4af..883d20da 100644 --- a/ProjectLighthouse/Types/Categories/TeamPicksCategory.cs +++ b/ProjectLighthouse/Types/Categories/TeamPicksCategory.cs @@ -15,7 +15,7 @@ namespace LBPUnion.ProjectLighthouse.Types.Categories public override Slot? GetPreviewSlot(Database database) => database.Slots.OrderByDescending(s => s.FirstUploaded).FirstOrDefault(s => s.TeamPick); public override IEnumerable GetSlots (Database database, int pageStart, int pageSize) - => database.Slots.OrderByDescending(s => s.FirstUploaded).Where(s => s.TeamPick).Skip(pageStart).Take(Math.Min(pageSize, 20)); + => database.Slots.OrderByDescending(s => s.FirstUploaded).Where(s => s.TeamPick).Skip(pageStart - 1).Take(Math.Min(pageSize, 20)); public override int GetTotalSlots(Database database) => database.Slots.Count(s => s.TeamPick); } } \ No newline at end of file