Refactor LBP3 text search to use the category system

This commit is contained in:
Slendy 2023-05-31 20:35:39 -05:00
parent 2ec6bff8e2
commit cd926a8415
No known key found for this signature in database
GPG key ID: 7288D68361B91428
11 changed files with 51 additions and 30 deletions

View file

@ -43,7 +43,7 @@ public class CategoryController : ControllerBase
PaginationData pageData = this.Request.GetPaginationData(); PaginationData pageData = this.Request.GetPaginationData();
pageData.TotalElements = CategoryHelper.Categories.Count; pageData.TotalElements = CategoryHelper.Categories.Count(c => !string.IsNullOrWhiteSpace(c.Name));
if (!int.TryParse(this.Request.Query["num_categories_with_results"], out int results)) results = 5; if (!int.TryParse(this.Request.Query["num_categories_with_results"], out int results)) results = 5;
@ -51,7 +51,8 @@ public class CategoryController : ControllerBase
SlotQueryBuilder queryBuilder = this.FilterFromRequest(token); SlotQueryBuilder queryBuilder = this.FilterFromRequest(token);
foreach (Category category in CategoryHelper.Categories.Skip(Math.Max(0, pageData.PageStart - 1)) foreach (Category category in CategoryHelper.Categories.Where(c => !string.IsNullOrWhiteSpace(c.Name))
.Skip(Math.Max(0, pageData.PageStart - 1))
.Take(Math.Min(pageData.PageSize, pageData.MaxElements)) .Take(Math.Min(pageData.PageSize, pageData.MaxElements))
.ToList()) .ToList())
{ {
@ -60,7 +61,10 @@ public class CategoryController : ControllerBase
results--; results--;
} }
return this.Ok(new CategoryListResponse(categories, pageData.TotalElements, "", pageData.HintStart)); Category searchCategory = CategoryHelper.Categories.First(c => c.Tag == "text");
GameCategory gameSearchCategory = GameCategory.CreateFromEntity(searchCategory, null);
return this.Ok(new CategoryListResponse(categories, gameSearchCategory, pageData.TotalElements, "", pageData.HintStart));
} }
[HttpGet("searches/{endpointName}")] [HttpGet("searches/{endpointName}")]

View file

@ -26,10 +26,6 @@ public class SearchController : ControllerBase
this.database = database; this.database = database;
} }
[HttpGet("searchLBP3")]
public Task<IActionResult> SearchSlotsLBP3([FromQuery] string textFilter)
=> this.SearchSlots(textFilter, "results");
[HttpGet("search")] [HttpGet("search")]
public async Task<IActionResult> SearchSlots([FromQuery] string query, string? keyName = "slots") public async Task<IActionResult> SearchSlots([FromQuery] string query, string? keyName = "slots")
{ {

View file

@ -20,6 +20,7 @@ public static class CategoryHelper
Categories.Add(new QueueCategory()); Categories.Add(new QueueCategory());
Categories.Add(new HeartedCategory()); Categories.Add(new HeartedCategory());
Categories.Add(new LuckyDipCategory()); Categories.Add(new LuckyDipCategory());
Categories.Add(new TextSearchCategory());
using DatabaseContext database = DatabaseContext.CreateNewInstance(); using DatabaseContext database = DatabaseContext.CreateNewInstance();
foreach (DatabaseCategoryEntity category in database.CustomCategories) Categories.Add(new CustomCategory(category)); foreach (DatabaseCategoryEntity category in database.CustomCategories) Categories.Add(new CustomCategory(category));

View file

@ -0,0 +1,22 @@
using LBPUnion.ProjectLighthouse.Database;
using LBPUnion.ProjectLighthouse.Extensions;
using LBPUnion.ProjectLighthouse.Filter;
using LBPUnion.ProjectLighthouse.Filter.Sorts;
using LBPUnion.ProjectLighthouse.Types.Entities.Level;
using LBPUnion.ProjectLighthouse.Types.Entities.Token;
namespace LBPUnion.ProjectLighthouse.Servers.GameServer.Types.Categories;
public class TextSearchCategory : SlotCategory
{
public override string Name { get; set; } = "";
public override string Description { get; set; } = "";
public override string IconHash { get; set; } = "";
public override string Endpoint { get; set; } = "text";
public override string Tag => "text";
public override IQueryable<SlotEntity> GetItems
(DatabaseContext database, GameTokenEntity token, SlotQueryBuilder queryBuilder) =>
database.Slots.Where(queryBuilder.Build())
.ApplyOrdering(new SlotSortBuilder<SlotEntity>().AddSort(new TotalPlaysSort()));
}

View file

@ -91,8 +91,9 @@ public class CustomXmlSerializer : XmlSerializer
} }
} }
// Otherwise if the object isn't a ILbpSerializable, skip // Otherwise if the object isn't a ILbpSerializable or a nullable ILbpSerializable, skip
else if (!typeof(ILbpSerializable).IsAssignableFrom(propertyInfo.PropertyType)) else if (!typeof(ILbpSerializable).IsAssignableFrom(propertyInfo.PropertyType) &&
!typeof(ILbpSerializable).IsAssignableFrom(Nullable.GetUnderlyingType(propertyInfo.PropertyType)))
{ {
continue; continue;
} }

View file

@ -8,9 +8,10 @@ public class CategoryListResponse : ILbpSerializable
{ {
public CategoryListResponse() { } public CategoryListResponse() { }
public CategoryListResponse(List<GameCategory> categories, int total, string hint, int hintStart) public CategoryListResponse(List<GameCategory> categories, GameCategory textSearch, int total, string hint, int hintStart)
{ {
this.Categories = categories; this.Categories = categories;
this.Search = textSearch;
this.Total = total; this.Total = total;
this.Hint = hint; this.Hint = hint;
this.HintStart = hintStart; this.HintStart = hintStart;
@ -26,7 +27,7 @@ public class CategoryListResponse : ILbpSerializable
public int HintStart { get; set; } public int HintStart { get; set; }
[XmlElement("text_search")] [XmlElement("text_search")]
public TextSearch Search { get; set; } = new(); public GameCategory Search { get; set; }
[XmlElement("category")] [XmlElement("category")]
public List<GameCategory> Categories { get; set; } public List<GameCategory> Categories { get; set; }

View file

@ -8,15 +8,18 @@ namespace LBPUnion.ProjectLighthouse.Types.Serialization;
public class GameCategory : ILbpSerializable public class GameCategory : ILbpSerializable
{ {
[XmlElement("name")] [XmlElement("name")]
[DefaultValue("")]
public string Name { get; set; } public string Name { get; set; }
[XmlElement("description")] [XmlElement("description")]
[DefaultValue("")]
public string Description { get; set; } public string Description { get; set; }
[XmlElement("url")] [XmlElement("url")]
public string Url { get; set; } public string Url { get; set; }
[XmlElement("icon")] [XmlElement("icon")]
[DefaultValue("")]
public string Icon { get; set; } public string Icon { get; set; }
[DefaultValue("")] [DefaultValue("")]
@ -32,10 +35,11 @@ public class GameCategory : ILbpSerializable
[XmlElement("tag")] [XmlElement("tag")]
public string Tag { get; set; } public string Tag { get; set; }
[DefaultValue(null)]
[XmlElement("results")] [XmlElement("results")]
public GenericSerializableList Results { get; set; } public GenericSerializableList? Results { get; set; }
public static GameCategory CreateFromEntity(Category category, GenericSerializableList results) => public static GameCategory CreateFromEntity(Category category, GenericSerializableList? results) =>
new() new()
{ {
Name = category.Name, Name = category.Name,

View file

@ -165,7 +165,6 @@ public class GameUser : ILbpSerializable, INeedsPreparationForSerialization
var stats = await database.Users.Where(u => u.UserId == this.UserId) var stats = await database.Users.Where(u => u.UserId == this.UserId)
.Select(_ => new .Select(_ => new
{ {
Username = database.Users.Where(u => u.UserId == this.UserId).Select(u => u.Username).First(),
BonusSlots = database.Users.Where(u => u.UserId == this.UserId).Select(u => u.AdminGrantedSlots).First(), BonusSlots = database.Users.Where(u => u.UserId == this.UserId).Select(u => u.AdminGrantedSlots).First(),
PlaylistCount = database.Playlists.Count(p => p.CreatorId == this.UserId), PlaylistCount = database.Playlists.Count(p => p.CreatorId == this.UserId),
ReviewCount = database.Reviews.Count(r => r.ReviewerId == this.UserId), ReviewCount = database.Reviews.Count(r => r.ReviewerId == this.UserId),
@ -182,7 +181,6 @@ public class GameUser : ILbpSerializable, INeedsPreparationForSerialization
.OrderBy(_ => 1) .OrderBy(_ => 1)
.FirstAsync(); .FirstAsync();
this.UserHandle.Username = stats.Username;
this.CommentsEnabled = this.CommentsEnabled && ServerConfiguration.Instance.UserGeneratedContentLimits.ProfileCommentsEnabled; this.CommentsEnabled = this.CommentsEnabled && ServerConfiguration.Instance.UserGeneratedContentLimits.ProfileCommentsEnabled;
int entitledSlots = ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots + stats.BonusSlots; int entitledSlots = ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots + stats.BonusSlots;
@ -236,7 +234,7 @@ public class GameUser : ILbpSerializable, INeedsPreparationForSerialization
new() new()
{ {
UserId = entity.UserId, UserId = entity.UserId,
UserHandle = new NpHandle("", entity.IconHash), UserHandle = new NpHandle(entity.Username, entity.IconHash),
Biography = entity.Biography, Biography = entity.Biography,
Location = entity.Location, Location = entity.Location,
ProfilePins = entity.Pins, ProfilePins = entity.Pins,

View file

@ -245,12 +245,16 @@ public class GameUserSlot : SlotBase, INeedsPreparationForSerialization
PhotoCount = database.Photos.Count(p => p.SlotId == this.SlotId), PhotoCount = database.Photos.Count(p => p.SlotId == this.SlotId),
AuthorPhotoCount = database.Photos.Count(p => p.SlotId == this.SlotId && p.CreatorId == this.CreatorId), AuthorPhotoCount = database.Photos.Count(p => p.SlotId == this.SlotId && p.CreatorId == this.CreatorId),
HeartCount = database.HeartedLevels.Count(h => h.SlotId == this.SlotId), HeartCount = database.HeartedLevels.Count(h => h.SlotId == this.SlotId),
Username = database.Users.Where(u => u.UserId == this.CreatorId).Select(u => u.Username).First(), AuthorHandle = database.Users.Where(u => u.UserId == this.CreatorId).Select(u => new NpHandle
{
Username = u.Username,
IconHash = u.IconHash,
}).First(),
}) })
.OrderBy(_ => 1) .OrderBy(_ => 1)
.FirstAsync(); .FirstAsync();
ReflectionHelper.CopyAllFields(stats, this); ReflectionHelper.CopyAllFields(stats, this);
this.AuthorHandle = new NpHandle(stats.Username, ""); this.AuthorHandle = stats.AuthorHandle;
if (this.GameVersion == GameVersion.LittleBigPlanet1) if (this.GameVersion == GameVersion.LittleBigPlanet1)
{ {

View file

@ -28,7 +28,7 @@ public abstract class SlotBase : ILbpSerializable
Description = slot.Description, Description = slot.Description,
Location = slot.Location, Location = slot.Location,
IconHash = slot.IconHash, IconHash = slot.IconHash,
BackgroundHash = slot.BackgroundHash ?? "", BackgroundHash = slot.BackgroundHash,
AuthorLabels = slot.AuthorLabels, AuthorLabels = slot.AuthorLabels,
GameVersion = slot.GameVersion, GameVersion = slot.GameVersion,
Shareable = slot.IsShareable, Shareable = slot.IsShareable,
@ -49,7 +49,7 @@ public abstract class SlotBase : ILbpSerializable
public static SlotBase CreateFromEntity(SlotEntity slot, GameTokenEntity token) public static SlotBase CreateFromEntity(SlotEntity slot, GameTokenEntity token)
=> CreateFromEntity(slot, token.GameVersion, token.UserId); => CreateFromEntity(slot, token.GameVersion, token.UserId);
public static SlotBase CreateFromEntity(SlotEntity slot, GameVersion targetGame, int targetUserId) private static SlotBase CreateFromEntity(SlotEntity slot, GameVersion targetGame, int targetUserId)
{ {
if (slot == null) if (slot == null)
{ {

View file

@ -1,10 +0,0 @@
using System.Xml.Serialization;
namespace LBPUnion.ProjectLighthouse.Types.Serialization;
[XmlRoot("text_search")]
public class TextSearch
{
[XmlElement("url")]
public string Url { get; set; } = "/slots/searchLBP3";
}