From 740a7f7803cc14e7066344554fd752242030925c Mon Sep 17 00:00:00 2001 From: jvyden Date: Mon, 15 Nov 2021 01:55:33 -0500 Subject: [PATCH] Fix vita slot counts --- .../Controllers/FriendsController.cs | 11 ++++++-- .../Controllers/ListController.cs | 7 +++-- .../Controllers/UserController.cs | 19 +++++++++---- ProjectLighthouse/Types/User.cs | 28 ++++++++++++++----- 4 files changed, 48 insertions(+), 17 deletions(-) diff --git a/ProjectLighthouse/Controllers/FriendsController.cs b/ProjectLighthouse/Controllers/FriendsController.cs index aea7a0d2..e4b2ef6a 100644 --- a/ProjectLighthouse/Controllers/FriendsController.cs +++ b/ProjectLighthouse/Controllers/FriendsController.cs @@ -69,8 +69,13 @@ namespace LBPUnion.ProjectLighthouse.Controllers [HttpGet("myFriends")] public async Task MyFriends() { - User? user = await this.database.UserFromRequest(this.Request); - if (user == null) return this.StatusCode(403, ""); + (User, Token)? userAndToken = await this.database.UserAndTokenFromRequest(this.Request); + + if (userAndToken == null) return this.StatusCode(403, ""); + + // ReSharper disable once PossibleInvalidOperationException + User user = userAndToken.Value.Item1; + Token token = userAndToken.Value.Item2; if (!FriendHelper.FriendIdsByUserId.TryGetValue(user.UserId, out int[]? friendIds) || friendIds == null) { @@ -83,7 +88,7 @@ namespace LBPUnion.ProjectLighthouse.Controllers User? friend = await this.database.Users.Include(u => u.Location).FirstOrDefaultAsync(u => u.UserId == friendId); if (friend == null) continue; - friends += friend.Serialize(); + friends += friend.Serialize(token.GameVersion); } return this.Ok(LbpSerializer.StringElement("myFriends", friends)); diff --git a/ProjectLighthouse/Controllers/ListController.cs b/ProjectLighthouse/Controllers/ListController.cs index 720c90b3..765582a5 100644 --- a/ProjectLighthouse/Controllers/ListController.cs +++ b/ProjectLighthouse/Controllers/ListController.cs @@ -152,8 +152,11 @@ namespace LBPUnion.ProjectLighthouse.Controllers #region Users [HttpGet("favouriteUsers/{username}")] - public IActionResult GetFavouriteUsers(string username) + public async Task GetFavouriteUsers(string username) { + Token? token = await this.database.TokenFromRequest(this.Request); + if (token == null) return this.StatusCode(403, ""); + IEnumerable heartedProfiles = this.database.HeartedProfiles.Include (q => q.User) .Include(q => q.HeartedUser) @@ -161,7 +164,7 @@ namespace LBPUnion.ProjectLighthouse.Controllers .Where(q => q.User.Username == username) .AsEnumerable(); - string response = heartedProfiles.Aggregate(string.Empty, (current, q) => current + q.HeartedUser.Serialize()); + string response = heartedProfiles.Aggregate(string.Empty, (current, q) => current + q.HeartedUser.Serialize(token.GameVersion)); return this.Ok(LbpSerializer.TaggedStringElement("favouriteUsers", response, "total", 1)); } diff --git a/ProjectLighthouse/Controllers/UserController.cs b/ProjectLighthouse/Controllers/UserController.cs index 32db5f5e..d3f07198 100644 --- a/ProjectLighthouse/Controllers/UserController.cs +++ b/ProjectLighthouse/Controllers/UserController.cs @@ -26,26 +26,35 @@ namespace LBPUnion.ProjectLighthouse.Controllers this.database = database; } - public async Task GetSerializedUser(string username) + public async Task GetSerializedUser(string username, GameVersion gameVersion = GameVersion.LittleBigPlanet1) { + User? user = await this.database.Users.Include(u => u.Location).FirstOrDefaultAsync(u => u.Username == username); - return user?.Serialize(); + return user?.Serialize(gameVersion); } [HttpGet("user/{username}")] - public async Task GetUser(string username) { - string? user = await this.GetSerializedUser(username); + public async Task GetUser(string username) + { + Token? token = await this.database.TokenFromRequest(this.Request); + if (token == null) return this.StatusCode(403, ""); + + string? user = await this.GetSerializedUser(username, token.GameVersion); if (user == null) return this.NotFound(); + return this.Ok(user); } [HttpGet("users")] public async Task GetUserAlt([FromQuery] string[] u) { + Token? token = await this.database.TokenFromRequest(this.Request); + if (token == null) return this.StatusCode(403, ""); + List serializedUsers = new(); foreach (string userId in u) { - serializedUsers.Add(await this.GetSerializedUser(userId)); + serializedUsers.Add(await this.GetSerializedUser(userId, token.GameVersion)); } string serialized = serializedUsers.Aggregate(string.Empty, (current, u) => u == null ? current : current + u); diff --git a/ProjectLighthouse/Types/User.cs b/ProjectLighthouse/Types/User.cs index 8cdfef8d..b4c51d47 100644 --- a/ProjectLighthouse/Types/User.cs +++ b/ProjectLighthouse/Types/User.cs @@ -93,11 +93,11 @@ namespace LBPUnion.ProjectLighthouse.Types } } - public string Serialize() + public string Serialize(GameVersion gameVersion = GameVersion.LittleBigPlanet1) { string user = LbpSerializer.TaggedStringElement("npHandle", this.Username, "icon", this.IconHash) + LbpSerializer.StringElement("game", this.Game) + - this.SerializeSlots() + + this.SerializeSlots(gameVersion == GameVersion.LittleBigPlanetVita) + LbpSerializer.StringElement("lists", this.Lists) + LbpSerializer.StringElement("lists_quota", ServerSettings.ListsQuota) + // technically not a part of the user but LBP expects it LbpSerializer.StringElement("biography", this.Biography) + @@ -149,18 +149,32 @@ namespace LBPUnion.ProjectLighthouse.Types "lbp2", "lbp3", "crossControl", }; - private string SerializeSlots() + private string SerializeSlots(bool isVita = false) { string slots = string.Empty; - slots += LbpSerializer.StringElement("lbp1UsedSlots", this.GetUsedSlotsForGame(GameVersion.LittleBigPlanet1)); - slots += LbpSerializer.StringElement("lbp2UsedSlots", this.GetUsedSlotsForGame(GameVersion.LittleBigPlanet2)); - slots += LbpSerializer.StringElement("lbp3UsedSlots", this.GetUsedSlotsForGame(GameVersion.LittleBigPlanet3)); + string[] slotTypesLocal; + + if (isVita) + { + slots += LbpSerializer.StringElement("lbp2UsedSlots", this.GetUsedSlotsForGame(GameVersion.LittleBigPlanetVita)); + slotTypesLocal = new[] + { + "lbp2", + }; + } + else + { + slots += LbpSerializer.StringElement("lbp1UsedSlots", this.GetUsedSlotsForGame(GameVersion.LittleBigPlanet1)); + slots += LbpSerializer.StringElement("lbp2UsedSlots", this.GetUsedSlotsForGame(GameVersion.LittleBigPlanet2)); + slots += LbpSerializer.StringElement("lbp3UsedSlots", this.GetUsedSlotsForGame(GameVersion.LittleBigPlanet3)); + slotTypesLocal = slotTypes; + } slots += LbpSerializer.StringElement("entitledSlots", ServerSettings.EntitledSlots); slots += LbpSerializer.StringElement("freeSlots", this.FreeSlots); - foreach (string slotType in slotTypes) + foreach (string slotType in slotTypesLocal) { slots += LbpSerializer.StringElement(slotType + "EntitledSlots", ServerSettings.EntitledSlots); // ReSharper disable once StringLiteralTypo