mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-07-11 15:51:27 +00:00
Refactor LBP3 text search to use the category system
This commit is contained in:
parent
2ec6bff8e2
commit
cd926a8415
11 changed files with 51 additions and 30 deletions
|
@ -43,7 +43,7 @@ public class CategoryController : ControllerBase
|
|||
|
||||
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;
|
||||
|
||||
|
@ -51,7 +51,8 @@ public class CategoryController : ControllerBase
|
|||
|
||||
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))
|
||||
.ToList())
|
||||
{
|
||||
|
@ -60,7 +61,10 @@ public class CategoryController : ControllerBase
|
|||
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}")]
|
||||
|
|
|
@ -26,10 +26,6 @@ public class SearchController : ControllerBase
|
|||
this.database = database;
|
||||
}
|
||||
|
||||
[HttpGet("searchLBP3")]
|
||||
public Task<IActionResult> SearchSlotsLBP3([FromQuery] string textFilter)
|
||||
=> this.SearchSlots(textFilter, "results");
|
||||
|
||||
[HttpGet("search")]
|
||||
public async Task<IActionResult> SearchSlots([FromQuery] string query, string? keyName = "slots")
|
||||
{
|
||||
|
|
|
@ -20,6 +20,7 @@ public static class CategoryHelper
|
|||
Categories.Add(new QueueCategory());
|
||||
Categories.Add(new HeartedCategory());
|
||||
Categories.Add(new LuckyDipCategory());
|
||||
Categories.Add(new TextSearchCategory());
|
||||
|
||||
using DatabaseContext database = DatabaseContext.CreateNewInstance();
|
||||
foreach (DatabaseCategoryEntity category in database.CustomCategories) Categories.Add(new CustomCategory(category));
|
||||
|
|
|
@ -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()));
|
||||
}
|
|
@ -91,8 +91,9 @@ public class CustomXmlSerializer : XmlSerializer
|
|||
}
|
||||
|
||||
}
|
||||
// Otherwise if the object isn't a ILbpSerializable, skip
|
||||
else if (!typeof(ILbpSerializable).IsAssignableFrom(propertyInfo.PropertyType))
|
||||
// Otherwise if the object isn't a ILbpSerializable or a nullable ILbpSerializable, skip
|
||||
else if (!typeof(ILbpSerializable).IsAssignableFrom(propertyInfo.PropertyType) &&
|
||||
!typeof(ILbpSerializable).IsAssignableFrom(Nullable.GetUnderlyingType(propertyInfo.PropertyType)))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
|
|
@ -8,9 +8,10 @@ public class CategoryListResponse : ILbpSerializable
|
|||
{
|
||||
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.Search = textSearch;
|
||||
this.Total = total;
|
||||
this.Hint = hint;
|
||||
this.HintStart = hintStart;
|
||||
|
@ -26,7 +27,7 @@ public class CategoryListResponse : ILbpSerializable
|
|||
public int HintStart { get; set; }
|
||||
|
||||
[XmlElement("text_search")]
|
||||
public TextSearch Search { get; set; } = new();
|
||||
public GameCategory Search { get; set; }
|
||||
|
||||
[XmlElement("category")]
|
||||
public List<GameCategory> Categories { get; set; }
|
||||
|
|
|
@ -8,15 +8,18 @@ namespace LBPUnion.ProjectLighthouse.Types.Serialization;
|
|||
public class GameCategory : ILbpSerializable
|
||||
{
|
||||
[XmlElement("name")]
|
||||
[DefaultValue("")]
|
||||
public string Name { get; set; }
|
||||
|
||||
[XmlElement("description")]
|
||||
[DefaultValue("")]
|
||||
public string Description { get; set; }
|
||||
|
||||
[XmlElement("url")]
|
||||
public string Url { get; set; }
|
||||
|
||||
[XmlElement("icon")]
|
||||
[DefaultValue("")]
|
||||
public string Icon { get; set; }
|
||||
|
||||
[DefaultValue("")]
|
||||
|
@ -32,10 +35,11 @@ public class GameCategory : ILbpSerializable
|
|||
[XmlElement("tag")]
|
||||
public string Tag { get; set; }
|
||||
|
||||
[DefaultValue(null)]
|
||||
[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()
|
||||
{
|
||||
Name = category.Name,
|
||||
|
|
|
@ -165,7 +165,6 @@ public class GameUser : ILbpSerializable, INeedsPreparationForSerialization
|
|||
var stats = await database.Users.Where(u => u.UserId == this.UserId)
|
||||
.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(),
|
||||
PlaylistCount = database.Playlists.Count(p => p.CreatorId == this.UserId),
|
||||
ReviewCount = database.Reviews.Count(r => r.ReviewerId == this.UserId),
|
||||
|
@ -182,7 +181,6 @@ public class GameUser : ILbpSerializable, INeedsPreparationForSerialization
|
|||
.OrderBy(_ => 1)
|
||||
.FirstAsync();
|
||||
|
||||
this.UserHandle.Username = stats.Username;
|
||||
this.CommentsEnabled = this.CommentsEnabled && ServerConfiguration.Instance.UserGeneratedContentLimits.ProfileCommentsEnabled;
|
||||
|
||||
int entitledSlots = ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots + stats.BonusSlots;
|
||||
|
@ -236,7 +234,7 @@ public class GameUser : ILbpSerializable, INeedsPreparationForSerialization
|
|||
new()
|
||||
{
|
||||
UserId = entity.UserId,
|
||||
UserHandle = new NpHandle("", entity.IconHash),
|
||||
UserHandle = new NpHandle(entity.Username, entity.IconHash),
|
||||
Biography = entity.Biography,
|
||||
Location = entity.Location,
|
||||
ProfilePins = entity.Pins,
|
||||
|
|
|
@ -245,12 +245,16 @@ public class GameUserSlot : SlotBase, INeedsPreparationForSerialization
|
|||
PhotoCount = database.Photos.Count(p => p.SlotId == this.SlotId),
|
||||
AuthorPhotoCount = database.Photos.Count(p => p.SlotId == this.SlotId && p.CreatorId == this.CreatorId),
|
||||
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)
|
||||
.FirstAsync();
|
||||
ReflectionHelper.CopyAllFields(stats, this);
|
||||
this.AuthorHandle = new NpHandle(stats.Username, "");
|
||||
this.AuthorHandle = stats.AuthorHandle;
|
||||
|
||||
if (this.GameVersion == GameVersion.LittleBigPlanet1)
|
||||
{
|
||||
|
|
|
@ -28,7 +28,7 @@ public abstract class SlotBase : ILbpSerializable
|
|||
Description = slot.Description,
|
||||
Location = slot.Location,
|
||||
IconHash = slot.IconHash,
|
||||
BackgroundHash = slot.BackgroundHash ?? "",
|
||||
BackgroundHash = slot.BackgroundHash,
|
||||
AuthorLabels = slot.AuthorLabels,
|
||||
GameVersion = slot.GameVersion,
|
||||
Shareable = slot.IsShareable,
|
||||
|
@ -49,7 +49,7 @@ public abstract class SlotBase : ILbpSerializable
|
|||
public static SlotBase CreateFromEntity(SlotEntity slot, GameTokenEntity token)
|
||||
=> 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)
|
||||
{
|
||||
|
|
|
@ -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";
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue