Fix all GameVersions showing in LBP3

Closes #196
This commit is contained in:
jvyden 2022-02-25 13:27:37 -05:00
parent 5d28256c3e
commit 02a1e89724
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
8 changed files with 73 additions and 29 deletions

View file

@ -2,6 +2,8 @@
<project version="4">
<component name="DiscordProjectSettings">
<option name="show" value="PROJECT_FILES" />
<option name="nameOverrideEnabled" value="true" />
<option name="nameOverrideText" value="Project Lighthouse" />
<option name="description" value="" />
</component>
</project>

View file

@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Helpers.Extensions;
using LBPUnion.ProjectLighthouse.Serialization;
using LBPUnion.ProjectLighthouse.Types;
using LBPUnion.ProjectLighthouse.Types.Levels;
@ -25,24 +26,6 @@ public class SlotsController : ControllerBase
this.database = database;
}
private IQueryable<Slot> getSlots(GameVersion gameVersion, bool includeSublevels = false)
{
IQueryable<Slot> query = this.database.Slots.Include(s => s.Creator).Include(s => s.Location);
if (gameVersion == GameVersion.LittleBigPlanetVita || gameVersion == GameVersion.LittleBigPlanetPSP || gameVersion == GameVersion.Unknown)
{
query = query.Where(s => s.GameVersion == gameVersion);
}
else
{
query = query.Where(s => s.GameVersion <= gameVersion);
}
if (!includeSublevels) query = query.Where(s => !s.SubLevel);
return query;
}
[HttpGet("slots/by")]
public async Task<IActionResult> SlotsBy([FromQuery] string u, [FromQuery] int pageStart, [FromQuery] int pageSize)
{
@ -56,7 +39,7 @@ public class SlotsController : ControllerBase
string response = Enumerable.Aggregate
(
this.getSlots(gameVersion, token.UserId == user.UserId)
this.database.Slots.ByGameVersion(gameVersion, token.UserId == user.UserId)
.Where(s => s.Creator!.Username == user.Username)
.Skip(pageStart - 1)
.Take(Math.Min(pageSize, ServerSettings.Instance.EntitledSlots)),
@ -94,7 +77,7 @@ public class SlotsController : ControllerBase
GameVersion gameVersion = token.GameVersion;
Slot? slot = await this.getSlots(gameVersion).FirstOrDefaultAsync(s => s.SlotId == id);
Slot? slot = await this.database.Slots.ByGameVersion(gameVersion).FirstOrDefaultAsync(s => s.SlotId == id);
if (slot == null) return this.NotFound();
@ -136,7 +119,11 @@ public class SlotsController : ControllerBase
GameVersion gameVersion = token.GameVersion;
IQueryable<Slot> slots = this.getSlots(gameVersion).OrderByDescending(s => s.FirstUploaded).Skip(pageStart - 1).Take(Math.Min(pageSize, 30));
IQueryable<Slot> slots = this.database.Slots.ByGameVersion
(gameVersion)
.OrderByDescending(s => s.FirstUploaded)
.Skip(pageStart - 1)
.Take(Math.Min(pageSize, 30));
string response = Enumerable.Aggregate(slots, string.Empty, (current, slot) => current + slot.Serialize(gameVersion));
@ -167,7 +154,7 @@ public class SlotsController : ControllerBase
GameVersion gameVersion = token.GameVersion;
IQueryable<Slot> slots = this.getSlots(gameVersion)
IQueryable<Slot> slots = this.database.Slots.ByGameVersion(gameVersion)
.Where(s => s.TeamPick)
.OrderByDescending(s => s.LastUpdated)
.Skip(pageStart - 1)
@ -201,7 +188,7 @@ public class SlotsController : ControllerBase
GameVersion gameVersion = token.GameVersion;
IEnumerable<Slot> slots = this.getSlots(gameVersion).OrderBy(_ => EF.Functions.Random()).Take(Math.Min(pageSize, 30));
IEnumerable<Slot> slots = this.database.Slots.ByGameVersion(gameVersion).OrderBy(_ => EF.Functions.Random()).Take(Math.Min(pageSize, 30));
string response = slots.Aggregate(string.Empty, (current, slot) => current + slot.Serialize(gameVersion));
@ -390,7 +377,7 @@ public class SlotsController : ControllerBase
{
if (version == GameVersion.LittleBigPlanetVita || version == GameVersion.LittleBigPlanetPSP || version == GameVersion.Unknown)
{
return this.getSlots(version);
return this.database.Slots.ByGameVersion(version);
}
string _dateFilterType = dateFilterType ?? "";

View file

@ -0,0 +1,31 @@
using System.Linq;
using LBPUnion.ProjectLighthouse.Types;
using LBPUnion.ProjectLighthouse.Types.Levels;
using Microsoft.EntityFrameworkCore;
namespace LBPUnion.ProjectLighthouse.Helpers.Extensions;
public static class SlotsExtensions
{
public static IQueryable<Slot> ByGameVersion
(this DbSet<Slot> set, GameVersion gameVersion, bool includeSublevels = false)
=> set.AsQueryable().ByGameVersion(gameVersion, includeSublevels);
public static IQueryable<Slot> ByGameVersion(this IQueryable<Slot> queryable, GameVersion gameVersion, bool includeSublevels = false)
{
IQueryable<Slot> query = queryable.Include(s => s.Creator).Include(s => s.Location);
if (gameVersion == GameVersion.LittleBigPlanetVita || gameVersion == GameVersion.LittleBigPlanetPSP || gameVersion == GameVersion.Unknown)
{
query = query.Where(s => s.GameVersion == gameVersion);
}
else
{
query = query.Where(s => s.GameVersion <= gameVersion);
}
if (!includeSublevels) query = query.Where(s => !s.SubLevel);
return query;
}
}

View file

@ -1,6 +1,7 @@
#nullable enable
using System.Collections.Generic;
using System.Linq;
using LBPUnion.ProjectLighthouse.Helpers.Extensions;
using LBPUnion.ProjectLighthouse.Types.Levels;
namespace LBPUnion.ProjectLighthouse.Types.Categories;
@ -34,6 +35,8 @@ public class CustomCategory : Category
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<Slot> GetSlots(Database database, int pageStart, int pageSize) => database.Slots.Where(s => this.SlotIds.Contains(s.SlotId));
public override IEnumerable<Slot> GetSlots
(Database database, int pageStart, int pageSize)
=> database.Slots.ByGameVersion(GameVersion.LittleBigPlanet3).Where(s => this.SlotIds.Contains(s.SlotId));
public override int GetTotalSlots(Database database) => this.SlotIds.Count;
}

View file

@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using LBPUnion.ProjectLighthouse.Helpers.Extensions;
using LBPUnion.ProjectLighthouse.Types.Levels;
using Microsoft.EntityFrameworkCore;
@ -16,5 +17,11 @@ public class HeartedCategory : CategoryWithUser
public override Slot? GetPreviewSlot(Database database, User user) => database.HeartedLevels.FirstOrDefault(h => h.UserId == user.UserId)?.Slot;
public override int GetTotalSlots(Database database, User user) => database.HeartedLevels.Count(h => h.UserId == user.UserId);
public override IEnumerable<Slot> GetSlots(Database database, User user, int pageStart, int pageSize)
=> database.HeartedLevels.Where(h => h.UserId == user.UserId).Include(h => h.Slot).Select(h => h.Slot).Skip(pageStart).Take(Math.Min(pageSize, 20));
=> database.HeartedLevels.Where
(h => h.UserId == user.UserId)
.Include(h => h.Slot)
.Select(h => h.Slot)
.ByGameVersion(GameVersion.LittleBigPlanet3)
.Skip(pageStart)
.Take(Math.Min(pageSize, 20));
}

View file

@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using LBPUnion.ProjectLighthouse.Helpers.Extensions;
using LBPUnion.ProjectLighthouse.Types.Levels;
namespace LBPUnion.ProjectLighthouse.Types.Categories;
@ -15,6 +16,6 @@ public class NewestLevelsCategory : Category
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 - 1).Take(Math.Min(pageSize, 20));
=> database.Slots.ByGameVersion(GameVersion.LittleBigPlanet3).OrderByDescending(s => s.FirstUploaded).Skip(pageStart - 1).Take(Math.Min(pageSize, 20));
public override int GetTotalSlots(Database database) => database.Slots.Count();
}

View file

@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using LBPUnion.ProjectLighthouse.Helpers.Extensions;
using LBPUnion.ProjectLighthouse.Types.Levels;
using Microsoft.EntityFrameworkCore;
@ -18,7 +19,13 @@ public class QueueCategory : CategoryWithUser
public override Slot? GetPreviewSlot(Database database, User user)
=> database.QueuedLevels.Include(q => q.Slot).FirstOrDefault(q => q.UserId == user.UserId)?.Slot;
public override IEnumerable<Slot> 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));
=> database.QueuedLevels.Include
(q => q.Slot)
.Include(q => q.Slot.Location)
.Select(q => q.Slot)
.ByGameVersion(GameVersion.LittleBigPlanet3)
.Skip(pageStart - 1)
.Take(Math.Min(pageSize, 20));
public override int GetTotalSlots(Database database, User user) => database.QueuedLevels.Count(q => q.UserId == user.UserId);
}

View file

@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using LBPUnion.ProjectLighthouse.Helpers.Extensions;
using LBPUnion.ProjectLighthouse.Types.Levels;
namespace LBPUnion.ProjectLighthouse.Types.Categories;
@ -15,6 +16,11 @@ public class TeamPicksCategory : Category
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 - 1).Take(Math.Min(pageSize, 20));
=> database.Slots.ByGameVersion
(GameVersion.LittleBigPlanet3)
.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);
}