mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-07-29 16:38:37 +00:00
Add ability to fetch levels from a category
This commit is contained in:
parent
c3d4a97565
commit
2a9537e769
8 changed files with 165 additions and 25 deletions
|
@ -2,9 +2,13 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Kettu;
|
||||
using LBPUnion.ProjectLighthouse.Helpers;
|
||||
using LBPUnion.ProjectLighthouse.Logging;
|
||||
using LBPUnion.ProjectLighthouse.Serialization;
|
||||
using LBPUnion.ProjectLighthouse.Types;
|
||||
using LBPUnion.ProjectLighthouse.Types.Categories;
|
||||
using LBPUnion.ProjectLighthouse.Types.Levels;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Controllers
|
||||
|
@ -31,14 +35,19 @@ namespace LBPUnion.ProjectLighthouse.Controllers
|
|||
User? user = await this.database.UserFromGameRequest(this.Request);
|
||||
if (user == null) return this.StatusCode(403, "");
|
||||
|
||||
List<Category> categories = new()
|
||||
{
|
||||
new TeamPicksCategory(),
|
||||
new NewestLevelsCategory(),
|
||||
new QueueCategory(user),
|
||||
};
|
||||
string categoriesSerialized = CollectionHelper.Categories.Aggregate
|
||||
(
|
||||
string.Empty,
|
||||
(current, category) =>
|
||||
{
|
||||
string serialized;
|
||||
|
||||
string categoriesSerialized = categories.Aggregate(string.Empty, (current, category) => current + category.Serialize(this.database));
|
||||
if (category is CategoryWithUser categoryWithUser) serialized = categoryWithUser.Serialize(this.database, user);
|
||||
else serialized = category.Serialize(this.database);
|
||||
|
||||
return current + serialized;
|
||||
}
|
||||
);
|
||||
|
||||
return this.Ok
|
||||
(
|
||||
|
@ -55,7 +64,41 @@ namespace LBPUnion.ProjectLighthouse.Controllers
|
|||
"hint_start", 1
|
||||
},
|
||||
{
|
||||
"total", categories.Count
|
||||
"total", CollectionHelper.Categories.Count
|
||||
},
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
[HttpGet("searches/{endpointName}")]
|
||||
public async Task<IActionResult> GetCategorySlots(string endpointName, [FromQuery] int pageStart, [FromQuery] int pageSize)
|
||||
{
|
||||
User? user = await this.database.UserFromGameRequest(this.Request);
|
||||
if (user == null) return this.StatusCode(403, "");
|
||||
|
||||
Category? category = CollectionHelper.Categories.FirstOrDefault(c => c.Endpoint == endpointName);
|
||||
if (category == null) return this.NotFound();
|
||||
|
||||
Logger.Log("Found category " + category, LoggerLevelCategory.Instance);
|
||||
|
||||
List<Slot> slots = category.GetSlots(this.database, pageStart, pageSize).ToList();
|
||||
|
||||
string slotsSerialized = slots.Aggregate(string.Empty, (current, slot) => current + slot.Serialize());
|
||||
|
||||
return this.Ok
|
||||
(
|
||||
LbpSerializer.TaggedStringElement
|
||||
(
|
||||
"results",
|
||||
slotsSerialized,
|
||||
new Dictionary<string, object>
|
||||
{
|
||||
{
|
||||
"total", category.GetTotalSlots(this.database)
|
||||
},
|
||||
{
|
||||
"hint_start", pageStart + pageSize
|
||||
},
|
||||
}
|
||||
)
|
||||
|
|
17
ProjectLighthouse/Helpers/CollectionHelper.cs
Normal file
17
ProjectLighthouse/Helpers/CollectionHelper.cs
Normal file
|
@ -0,0 +1,17 @@
|
|||
using System.Collections.Generic;
|
||||
using LBPUnion.ProjectLighthouse.Types.Categories;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Helpers
|
||||
{
|
||||
public static class CollectionHelper
|
||||
{
|
||||
public static readonly List<Category> Categories = new();
|
||||
|
||||
static CollectionHelper()
|
||||
{
|
||||
Categories.Add(new TeamPicksCategory());
|
||||
Categories.Add(new NewestLevelsCategory());
|
||||
Categories.Add(new QueueCategory());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -72,4 +72,10 @@ namespace LBPUnion.ProjectLighthouse.Logging
|
|||
}
|
||||
public override string Name => "AspNet";
|
||||
}
|
||||
|
||||
public class LoggerLevelCategory : LoggerLevel
|
||||
{
|
||||
public static readonly LoggerLevelCategory Instance = new();
|
||||
public override string Name => "Category";
|
||||
}
|
||||
}
|
|
@ -30,6 +30,8 @@ namespace LBPUnion.ProjectLighthouse.Types.Categories
|
|||
|
||||
public abstract Slot? GetPreviewSlot(Database database);
|
||||
|
||||
public abstract IEnumerable<Slot> GetSlots(Database database, int pageStart, int pageSize);
|
||||
|
||||
public abstract int GetTotalSlots(Database database);
|
||||
|
||||
public string Serialize(Database database)
|
||||
|
|
72
ProjectLighthouse/Types/Categories/CategoryWithUser.cs
Normal file
72
ProjectLighthouse/Types/Categories/CategoryWithUser.cs
Normal file
|
@ -0,0 +1,72 @@
|
|||
#nullable enable
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using Kettu;
|
||||
using LBPUnion.ProjectLighthouse.Logging;
|
||||
using LBPUnion.ProjectLighthouse.Serialization;
|
||||
using LBPUnion.ProjectLighthouse.Types.Levels;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Types.Categories;
|
||||
|
||||
public abstract class CategoryWithUser : Category
|
||||
{
|
||||
public abstract Slot? GetPreviewSlot(Database database, User user);
|
||||
public override Slot? GetPreviewSlot(Database database)
|
||||
{
|
||||
#if DEBUG
|
||||
Logger.Log("tried to get preview slot without user on CategoryWithUser", LoggerLevelCategory.Instance);
|
||||
if (Debugger.IsAttached) Debugger.Break();
|
||||
#endif
|
||||
return null;
|
||||
}
|
||||
|
||||
public abstract int GetTotalSlots(Database database, User user);
|
||||
public override int GetTotalSlots(Database database)
|
||||
{
|
||||
#if DEBUG
|
||||
Logger.Log("tried to get total slots without user on CategoryWithUser", LoggerLevelCategory.Instance);
|
||||
if (Debugger.IsAttached) Debugger.Break();
|
||||
#endif
|
||||
return -1;
|
||||
}
|
||||
|
||||
public new string Serialize(Database database)
|
||||
{
|
||||
Logger.Log("tried to serialize without user on CategoryWithUser", LoggerLevelCategory.Instance);
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
public string Serialize(Database database, User user)
|
||||
{
|
||||
Slot? previewSlot = this.GetPreviewSlot(database, user);
|
||||
|
||||
string previewResults = "";
|
||||
if (previewSlot != null)
|
||||
{
|
||||
previewResults = LbpSerializer.TaggedStringElement
|
||||
(
|
||||
"results",
|
||||
previewSlot.Serialize(),
|
||||
new Dictionary<string, object>
|
||||
{
|
||||
{
|
||||
"total", this.GetTotalSlots(database, user)
|
||||
},
|
||||
{
|
||||
"hint_start", "2"
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
return LbpSerializer.StringElement
|
||||
(
|
||||
"category",
|
||||
LbpSerializer.StringElement("name", this.Name) +
|
||||
LbpSerializer.StringElement("description", this.Description) +
|
||||
LbpSerializer.StringElement("url", this.IngameEndpoint) +
|
||||
(previewSlot == null ? "" : previewResults) +
|
||||
LbpSerializer.StringElement("icon", IconHash)
|
||||
);
|
||||
}
|
||||
}
|
|
@ -1,4 +1,6 @@
|
|||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using LBPUnion.ProjectLighthouse.Types.Levels;
|
||||
|
||||
|
@ -11,6 +13,9 @@ namespace LBPUnion.ProjectLighthouse.Types.Categories
|
|||
public override string IconHash { get; set; } = "g820623";
|
||||
public override string Endpoint { get; set; } = "newest";
|
||||
public override Slot? GetPreviewSlot(Database database) => database.Slots.OrderByDescending(s => s.FirstUploaded).FirstOrDefault();
|
||||
public override IEnumerable<Slot> GetSlots
|
||||
(Database database, int pageStart, int pageSize)
|
||||
=> database.Slots.OrderByDescending(s => s.FirstUploaded).Skip(pageStart).Take(Math.Min(pageSize, 20));
|
||||
public override int GetTotalSlots(Database database) => database.Slots.Count();
|
||||
}
|
||||
}
|
|
@ -1,33 +1,23 @@
|
|||
#nullable enable
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using LBPUnion.ProjectLighthouse.Types.Levels;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Types.Categories
|
||||
{
|
||||
public class QueueCategory : Category
|
||||
public class QueueCategory : CategoryWithUser
|
||||
{
|
||||
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 string Endpoint { get; set; } = "queue";
|
||||
public override IEnumerable<Slot> GetSlots(Database database, int pageStart, int pageSize) => new List<Slot>();
|
||||
|
||||
public override Slot? GetPreviewSlot(Database database)
|
||||
=> database.QueuedLevels.Include(q => q.Slot).FirstOrDefault(q => q.UserId == this.user.UserId)?.Slot;
|
||||
public override Slot? GetPreviewSlot(Database database, User user)
|
||||
=> database.QueuedLevels.Include(q => q.Slot).FirstOrDefault(q => q.UserId == user.UserId)?.Slot;
|
||||
|
||||
public override int GetTotalSlots(Database database) => database.QueuedLevels.Count(q => q.UserId == this.user.UserId);
|
||||
public override int GetTotalSlots(Database database, User user) => database.QueuedLevels.Count(q => q.UserId == user.UserId);
|
||||
}
|
||||
}
|
|
@ -1,4 +1,6 @@
|
|||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using LBPUnion.ProjectLighthouse.Types.Levels;
|
||||
|
||||
|
@ -11,6 +13,9 @@ namespace LBPUnion.ProjectLighthouse.Types.Categories
|
|||
public override string IconHash { get; set; } = "g820626";
|
||||
public override string Endpoint { get; set; } = "team_picks";
|
||||
public override Slot? GetPreviewSlot(Database database) => database.Slots.OrderByDescending(s => s.FirstUploaded).FirstOrDefault(s => s.TeamPick);
|
||||
public override IEnumerable<Slot> GetSlots
|
||||
(Database database, int pageStart, int pageSize)
|
||||
=> database.Slots.OrderByDescending(s => s.FirstUploaded).Where(s => s.TeamPick).Skip(pageStart).Take(Math.Min(pageSize, 20));
|
||||
public override int GetTotalSlots(Database database) => database.Slots.Count(s => s.TeamPick);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue