mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-05-14 13:52:28 +00:00
Initial support for lbp3 playlists
Will add migration when user settings is merged
This commit is contained in:
parent
9073a8266f
commit
78faff36b5
4 changed files with 236 additions and 6 deletions
|
@ -1,4 +1,7 @@
|
|||
#nullable enable
|
||||
using System.Xml.Schema;
|
||||
using System.Xml.Serialization;
|
||||
using LBPUnion.ProjectLighthouse.Configuration;
|
||||
using LBPUnion.ProjectLighthouse.Helpers;
|
||||
using LBPUnion.ProjectLighthouse.Levels;
|
||||
using LBPUnion.ProjectLighthouse.Levels.Categories;
|
||||
|
@ -6,8 +9,8 @@ using LBPUnion.ProjectLighthouse.Logging;
|
|||
using LBPUnion.ProjectLighthouse.PlayerData;
|
||||
using LBPUnion.ProjectLighthouse.PlayerData.Profiles;
|
||||
using LBPUnion.ProjectLighthouse.Serialization;
|
||||
using LBPUnion.ProjectLighthouse.Types;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Servers.GameServer.Controllers.Slots;
|
||||
|
||||
|
@ -23,8 +26,143 @@ public class CollectionController : ControllerBase
|
|||
this.database = database;
|
||||
}
|
||||
|
||||
|
||||
[HttpGet("playlists/{playlistId:int}/slots")]
|
||||
public async Task<IActionResult> GetPlaylistSlots(int playlistId)
|
||||
{
|
||||
GameToken? token = await this.database.GameTokenFromRequest(this.Request);
|
||||
if (token == null) return this.StatusCode(403, "");
|
||||
|
||||
Playlist? targetPlaylist = await this.database.Playlists.FirstOrDefaultAsync(p => p.PlaylistId == playlistId);
|
||||
if (targetPlaylist == null) return this.BadRequest();
|
||||
|
||||
IQueryable<Slot> slots = this.database.Slots.Include(s => s.Creator)
|
||||
.Include(s => s.Location)
|
||||
.Where(s => targetPlaylist.SlotIds.Contains(s.SlotId));
|
||||
|
||||
string response = Enumerable.Aggregate(slots, string.Empty, (current, slot) => current + slot.Serialize());
|
||||
int total = targetPlaylist.SlotIds.Length;
|
||||
|
||||
return this.Ok(LbpSerializer.TaggedStringElement("slots", response, "total", total));
|
||||
}
|
||||
|
||||
[HttpPost("playlists/{playlistId:int}")]
|
||||
[HttpPost("playlists/{playlistId:int}/slots")]
|
||||
[HttpPost("playlists/{playlistId:int}/slots/{slotId:int}/delete")]
|
||||
[HttpPost("playlists/{playlistId:int}/order_slots")]
|
||||
public async Task<IActionResult> UpdatePlaylist(int playlistId, int slotId)
|
||||
{
|
||||
GameToken? token = await this.database.GameTokenFromRequest(this.Request);
|
||||
if (token == null) return this.StatusCode(403, "");
|
||||
|
||||
Playlist? targetPlaylist = await this.database.Playlists.FirstOrDefaultAsync(p => p.PlaylistId == playlistId);
|
||||
if (targetPlaylist == null) return this.BadRequest();
|
||||
|
||||
if (token.UserId != targetPlaylist.CreatorId) return this.BadRequest();
|
||||
|
||||
// Delete a slot from playlist
|
||||
if (slotId != 0)
|
||||
{
|
||||
targetPlaylist.SlotIds = targetPlaylist.SlotIds.Where(s => s != slotId).ToArray();
|
||||
await this.database.SaveChangesAsync();
|
||||
return this.Ok(this.GetUserPlaylists(token.UserId));
|
||||
}
|
||||
|
||||
this.Request.Body.Position = 0;
|
||||
string bodyString = await new StreamReader(this.Request.Body).ReadToEndAsync();
|
||||
|
||||
|
||||
string rootElement = bodyString.Contains("levels") ? "levels" : "playlist"; // I hate lbp3
|
||||
XmlSerializer serializer = new(typeof(Playlist), new XmlRootAttribute(rootElement));
|
||||
Playlist? newPlaylist = (Playlist?)serializer.Deserialize(new StringReader(bodyString));
|
||||
|
||||
if (newPlaylist == null) return this.BadRequest();
|
||||
|
||||
if (newPlaylist.LevelIds != null)
|
||||
{
|
||||
HashSet<int> slotIds = new(targetPlaylist.SlotIds);
|
||||
|
||||
// Reorder
|
||||
if (slotIds.SetEquals(newPlaylist.LevelIds))
|
||||
{
|
||||
targetPlaylist.SlotIds = newPlaylist.LevelIds;
|
||||
}
|
||||
// Add a level
|
||||
else
|
||||
{
|
||||
foreach (int id in newPlaylist.LevelIds)
|
||||
{
|
||||
targetPlaylist.SlotIds = targetPlaylist.SlotIds.Append(id).ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(newPlaylist.Name))
|
||||
{
|
||||
targetPlaylist.Name = newPlaylist.Name;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(newPlaylist.Description))
|
||||
{
|
||||
targetPlaylist.Description = newPlaylist.Description;
|
||||
}
|
||||
|
||||
await this.database.SaveChangesAsync();
|
||||
|
||||
return this.Ok(this.GetUserPlaylists(token.UserId));
|
||||
}
|
||||
|
||||
private string GetUserPlaylists(int userId)
|
||||
{
|
||||
string response = Enumerable.Aggregate(
|
||||
this.database.Playlists.Include(p => p.Creator).Where(p => p.CreatorId == userId),
|
||||
string.Empty,
|
||||
(current, slot) => current + slot.Serialize());
|
||||
int total = this.database.Playlists.Count(p => p.CreatorId == userId);
|
||||
|
||||
return LbpSerializer.TaggedStringElement("playlists", response, "total", total);
|
||||
}
|
||||
|
||||
[HttpPost("playlists")]
|
||||
public async Task<IActionResult> CreatePlaylist()
|
||||
{
|
||||
GameToken? token = await this.database.GameTokenFromRequest(this.Request);
|
||||
if (token == null) return this.StatusCode(403, "");
|
||||
|
||||
int playlistCount = await this.database.Playlists.CountAsync(p => p.CreatorId == token.UserId);
|
||||
|
||||
if (playlistCount > ServerConfiguration.Instance.UserGeneratedContentLimits.ListsQuota) return this.BadRequest();
|
||||
|
||||
this.Request.Body.Position = 0;
|
||||
string bodyString = await new StreamReader(this.Request.Body).ReadToEndAsync();
|
||||
|
||||
XmlSerializer serializer = new(typeof(Playlist), new XmlRootAttribute("playlist"));
|
||||
Playlist? playlist = (Playlist?)serializer.Deserialize(new StringReader(bodyString));
|
||||
|
||||
if (playlist == null) return this.BadRequest();
|
||||
|
||||
playlist.CreatorId = token.UserId;
|
||||
|
||||
SanitizationHelper.SanitizeStringsInClass(playlist);
|
||||
|
||||
this.database.Playlists.Add(playlist);
|
||||
|
||||
await this.database.SaveChangesAsync();
|
||||
|
||||
return this.Ok(this.GetUserPlaylists(token.UserId));
|
||||
}
|
||||
|
||||
[HttpGet("user/{username}/playlists")]
|
||||
public IActionResult GetUserPlaylists(string username) => this.Ok("<playlists></playlists>");
|
||||
public async Task<IActionResult> GetUserPlaylists(string username)
|
||||
{
|
||||
GameToken? token = await this.database.GameTokenFromRequest(this.Request);
|
||||
if (token == null) return this.StatusCode(403, "");
|
||||
|
||||
int targetUserId = await this.database.Users.Where(u => u.Username == username).Select(u => u.UserId).FirstOrDefaultAsync();
|
||||
if (targetUserId == 0) return this.BadRequest();
|
||||
|
||||
return this.Ok(this.GetUserPlaylists(targetUserId));
|
||||
}
|
||||
|
||||
[HttpGet("searches")]
|
||||
[HttpGet("genres")]
|
||||
|
|
21
ProjectLighthouse/Levels/HeartedPlaylist.cs
Normal file
21
ProjectLighthouse/Levels/HeartedPlaylist.cs
Normal file
|
@ -0,0 +1,21 @@
|
|||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using LBPUnion.ProjectLighthouse.PlayerData.Profiles;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Levels;
|
||||
|
||||
public class HeartedPlaylist
|
||||
{
|
||||
[Key]
|
||||
public int HeartedPlaylistId { get; set; }
|
||||
|
||||
public int UserId { get; set; }
|
||||
|
||||
[ForeignKey(nameof(UserId))]
|
||||
public User User { get; set; }
|
||||
|
||||
public int PlaylistId { get; set; }
|
||||
|
||||
[ForeignKey(nameof(PlaylistId))]
|
||||
public Playlist Playlist { get; set; }
|
||||
}
|
70
ProjectLighthouse/Levels/Playlist.cs
Normal file
70
ProjectLighthouse/Levels/Playlist.cs
Normal file
|
@ -0,0 +1,70 @@
|
|||
#nullable enable
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Xml.Serialization;
|
||||
using LBPUnion.ProjectLighthouse.Configuration;
|
||||
using LBPUnion.ProjectLighthouse.PlayerData.Profiles;
|
||||
using LBPUnion.ProjectLighthouse.Serialization;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Levels;
|
||||
|
||||
public class Playlist
|
||||
{
|
||||
[Key]
|
||||
public int PlaylistId { get; set; }
|
||||
|
||||
[XmlElement("name")]
|
||||
public string Name { get; set; } = "";
|
||||
|
||||
[XmlElement("description")]
|
||||
public string Description { get; set; } = "";
|
||||
|
||||
public int CreatorId { get; set; }
|
||||
|
||||
[ForeignKey(nameof(CreatorId))]
|
||||
[JsonIgnore]
|
||||
public User? Creator { get; set; }
|
||||
|
||||
public string SlotCollection { get; set; } = "";
|
||||
|
||||
[JsonIgnore]
|
||||
[NotMapped]
|
||||
[XmlElement("level_id")]
|
||||
public int[]? LevelIds { get; set; }
|
||||
|
||||
[NotMapped]
|
||||
public int[] SlotIds
|
||||
{
|
||||
get => this.SlotCollection.Split(",").Where(x => int.TryParse(x, out _)).Select(int.Parse).ToArray();
|
||||
set => this.SlotCollection = string.Join(",", value);
|
||||
}
|
||||
|
||||
public string Serialize()
|
||||
{
|
||||
using Database database = new();
|
||||
string playlist = LbpSerializer.StringElement("id", this.PlaylistId) +
|
||||
LbpSerializer.StringElement("author",
|
||||
LbpSerializer.StringElement("npHandle", this.Creator?.Username)) +
|
||||
LbpSerializer.StringElement("name", this.Name) +
|
||||
LbpSerializer.StringElement("description", this.Description) +
|
||||
LbpSerializer.StringElement("levels", this.SlotIds.Length) +
|
||||
LbpSerializer.StringElement("thumbs", 0) +
|
||||
LbpSerializer.StringElement("plays", 0) +
|
||||
LbpSerializer.StringElement("unique_plays", 0) +
|
||||
LbpSerializer.StringElement("levels_quota", ServerConfiguration.Instance.UserGeneratedContentLimits.ListsQuota) +
|
||||
this.SerializeIcons(database);
|
||||
|
||||
return LbpSerializer.StringElement("playlist", playlist);
|
||||
}
|
||||
|
||||
private string SerializeIcons(Database database)
|
||||
{
|
||||
string iconList = this.SlotIds.Select(id => database.Slots.FirstOrDefault(s => s.SlotId == id))
|
||||
.Where(slot => slot != null)
|
||||
.Aggregate(string.Empty, (current, slot) => current + LbpSerializer.StringElement("icon", slot?.IconHash));
|
||||
return LbpSerializer.StringElement("icons", iconList);
|
||||
}
|
||||
|
||||
}
|
|
@ -49,7 +49,7 @@ public class User
|
|||
|
||||
[NotMapped]
|
||||
[JsonIgnore]
|
||||
public int Lists => 0;
|
||||
public int Lists => this.database.Playlists.Count(p => p.CreatorId == this.UserId);
|
||||
|
||||
/// <summary>
|
||||
/// A user-customizable biography shown on the profile card
|
||||
|
@ -183,6 +183,7 @@ public class User
|
|||
/// <summary>
|
||||
/// ARRR! Forces the user to see Pirate English translations on the website.
|
||||
/// </summary>
|
||||
[NotMapped]
|
||||
public bool IsAPirate { get; set; }
|
||||
|
||||
public PrivacyType LevelVisibility { get; set; } = PrivacyType.All;
|
||||
|
@ -197,14 +198,14 @@ public class User
|
|||
string user = LbpSerializer.TaggedStringElement("npHandle", this.Username, "icon", this.IconHash) +
|
||||
LbpSerializer.StringElement("game", (int)gameVersion) +
|
||||
this.serializeSlots(gameVersion) +
|
||||
LbpSerializer.StringElement<string>("lists", this.Lists, true) +
|
||||
LbpSerializer.StringElement<string>
|
||||
LbpSerializer.StringElement<int>("lists", this.Lists, true) +
|
||||
LbpSerializer.StringElement<int>
|
||||
(
|
||||
"lists_quota",
|
||||
ServerConfiguration.Instance.UserGeneratedContentLimits.ListsQuota,
|
||||
true
|
||||
) + // technically not a part of the user but LBP expects it
|
||||
LbpSerializer.StringElement<string>("heartCount", this.Hearts, true) +
|
||||
LbpSerializer.StringElement<int>("heartCount", this.Hearts, true) +
|
||||
this.serializeEarth(gameVersion) +
|
||||
LbpSerializer.StringElement<string>("yay2", this.YayHash, true) +
|
||||
LbpSerializer.StringElement<string>("boo2", this.BooHash, true) +
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue