From f8c02cb3aa90cc332e048fda3f464968bd5c39f3 Mon Sep 17 00:00:00 2001 From: jvyden Date: Tue, 28 Dec 2021 16:55:00 -0500 Subject: [PATCH] Add QueueCategory --- .../Controllers/CollectionController.cs | 8 +++-- .../Types/Categories/Category.cs | 15 ++++++--- .../Types/Categories/NewestLevelsCategory.cs | 4 +-- .../Types/Categories/QueueCategory.cs | 32 +++++++++++++++++++ .../Types/Categories/TeamPicksCategory.cs | 4 +-- 5 files changed, 52 insertions(+), 11 deletions(-) create mode 100644 ProjectLighthouse/Types/Categories/QueueCategory.cs diff --git a/ProjectLighthouse/Controllers/CollectionController.cs b/ProjectLighthouse/Controllers/CollectionController.cs index 590ebd8a..1578e4a1 100644 --- a/ProjectLighthouse/Controllers/CollectionController.cs +++ b/ProjectLighthouse/Controllers/CollectionController.cs @@ -1,7 +1,9 @@ +#nullable enable using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using LBPUnion.ProjectLighthouse.Serialization; +using LBPUnion.ProjectLighthouse.Types; using LBPUnion.ProjectLighthouse.Types.Categories; using Microsoft.AspNetCore.Mvc; @@ -26,12 +28,14 @@ namespace LBPUnion.ProjectLighthouse.Controllers [HttpGet("genres")] public async Task GenresAndSearches() { + User? user = await this.database.UserFromGameRequest(this.Request); + if (user == null) return this.StatusCode(403, ""); + List categories = new() { - new TeamPicksCategory(), new TeamPicksCategory(), new NewestLevelsCategory(), - new NewestLevelsCategory(), + new QueueCategory(user), }; string categoriesSerialized = categories.Aggregate(string.Empty, (current, category) => current + category.Serialize(this.database)); diff --git a/ProjectLighthouse/Types/Categories/Category.cs b/ProjectLighthouse/Types/Categories/Category.cs index 945aeffe..8d9de280 100644 --- a/ProjectLighthouse/Types/Categories/Category.cs +++ b/ProjectLighthouse/Types/Categories/Category.cs @@ -1,5 +1,4 @@ -using System.Collections.Generic; -using System.Linq; +#nullable enable using System.Xml.Serialization; using LBPUnion.ProjectLighthouse.Serialization; using LBPUnion.ProjectLighthouse.Types.Levels; @@ -28,11 +27,17 @@ namespace LBPUnion.ProjectLighthouse.Types.Categories set => this.Endpoint = value.Replace("/searches/", ""); } - public abstract IEnumerable Slots(Database database); + public abstract Slot? GetPreviewSlot(Database database); public string Serialize(Database database) { - string slots = this.Slots(database).Aggregate(string.Empty, (current, slot) => current + slot.Serialize()); + Slot? previewSlot = this.GetPreviewSlot(database); + + string previewResults = ""; + if (previewSlot != null) + { + previewResults = LbpSerializer.StringElement("results", LbpSerializer.StringElement("slots", previewSlot.Serialize())); + } return LbpSerializer.StringElement ( @@ -40,7 +45,7 @@ namespace LBPUnion.ProjectLighthouse.Types.Categories LbpSerializer.StringElement("name", this.Name) + LbpSerializer.StringElement("description", this.Description) + LbpSerializer.StringElement("url", this.IngameEndpoint) + - LbpSerializer.StringElement("results", slots) + + (previewSlot == null ? "" : previewResults) + LbpSerializer.StringElement("icon", IconHash) ); } diff --git a/ProjectLighthouse/Types/Categories/NewestLevelsCategory.cs b/ProjectLighthouse/Types/Categories/NewestLevelsCategory.cs index fa01f922..a46ddb31 100644 --- a/ProjectLighthouse/Types/Categories/NewestLevelsCategory.cs +++ b/ProjectLighthouse/Types/Categories/NewestLevelsCategory.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +#nullable enable using System.Linq; using LBPUnion.ProjectLighthouse.Types.Levels; @@ -10,6 +10,6 @@ namespace LBPUnion.ProjectLighthouse.Types.Categories public override string Description { get; set; } = "Levels recently published"; public override string IconHash { get; set; } = "g820623"; public override string Endpoint { get; set; } = "newest"; - public override IEnumerable Slots(Database database) => database.Slots.OrderByDescending(s => s.FirstUploaded).Take(1); + public override Slot? GetPreviewSlot(Database database) => database.Slots.OrderByDescending(s => s.FirstUploaded).FirstOrDefault(); } } \ No newline at end of file diff --git a/ProjectLighthouse/Types/Categories/QueueCategory.cs b/ProjectLighthouse/Types/Categories/QueueCategory.cs new file mode 100644 index 00000000..5629eb80 --- /dev/null +++ b/ProjectLighthouse/Types/Categories/QueueCategory.cs @@ -0,0 +1,32 @@ +#nullable enable +using System.Linq; +using LBPUnion.ProjectLighthouse.Types.Levels; +using Microsoft.EntityFrameworkCore; + +namespace LBPUnion.ProjectLighthouse.Types.Categories +{ + public class QueueCategory : Category + { + private User user; + + public QueueCategory(User user) + { + this.user = user; + } + + public override string Name { get; set; } = "My Queue"; + public override string Description { get; set; } = "Your queued levels"; + public override string IconHash { get; set; } = "g820614"; + + public override string Endpoint { + get => $"queue/{this.user.UserId}"; + set { + // cry about it, i don't care + } + } + + public override Slot? GetPreviewSlot + (Database database) + => database.QueuedLevels.Include(q => q.Slot).FirstOrDefault(q => q.UserId == this.user.UserId)?.Slot; + } +} \ No newline at end of file diff --git a/ProjectLighthouse/Types/Categories/TeamPicksCategory.cs b/ProjectLighthouse/Types/Categories/TeamPicksCategory.cs index c33b5afd..58a3441b 100644 --- a/ProjectLighthouse/Types/Categories/TeamPicksCategory.cs +++ b/ProjectLighthouse/Types/Categories/TeamPicksCategory.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +#nullable enable using System.Linq; using LBPUnion.ProjectLighthouse.Types.Levels; @@ -10,6 +10,6 @@ namespace LBPUnion.ProjectLighthouse.Types.Categories public override string Description { get; set; } = "Levels given awards by your instance admin"; public override string IconHash { get; set; } = "g820626"; public override string Endpoint { get; set; } = "team_picks"; - public override IEnumerable Slots(Database database) => database.Slots.OrderByDescending(s => s.FirstUploaded).Where(s => s.TeamPick).Take(1); + public override Slot? GetPreviewSlot(Database database) => database.Slots.OrderByDescending(s => s.FirstUploaded).FirstOrDefault(s => s.TeamPick); } } \ No newline at end of file