Initial support for lbp3 playlists

Will add migration when user settings is merged
This commit is contained in:
Slendy 2022-09-15 22:53:00 -05:00
parent 9073a8266f
commit 78faff36b5
No known key found for this signature in database
GPG key ID: 7288D68361B91428
4 changed files with 236 additions and 6 deletions

View file

@ -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")]