From 66b18bb4f19dc9db58f866acd842e494e39eeb33 Mon Sep 17 00:00:00 2001 From: Beyley Thomas Date: Mon, 2 Oct 2023 18:39:26 -0700 Subject: [PATCH] Send favourite users and levels to PSP clients --- .../DataTypes/Response/GameUserResponse.cs | 32 +++++++++++++++---- .../Endpoints/Game/RelationEndpoints.cs | 2 +- .../Endpoints/Game/UserEndpoints.cs | 6 ++-- .../SerializedMinimalFavouriteUserList.cs | 26 +++++++++++++++ 4 files changed, 56 insertions(+), 10 deletions(-) create mode 100644 Refresh.GameServer/Types/Lists/SerializedMinimalFavouriteUserList.cs diff --git a/Refresh.GameServer/Endpoints/Game/DataTypes/Response/GameUserResponse.cs b/Refresh.GameServer/Endpoints/Game/DataTypes/Response/GameUserResponse.cs index 05d1b78b..e0e8c27e 100644 --- a/Refresh.GameServer/Endpoints/Game/DataTypes/Response/GameUserResponse.cs +++ b/Refresh.GameServer/Endpoints/Game/DataTypes/Response/GameUserResponse.cs @@ -1,7 +1,10 @@ using System.Xml.Serialization; using Refresh.GameServer.Authentication; +using Refresh.GameServer.Database; using Refresh.GameServer.Endpoints.ApiV3.DataTypes; using Refresh.GameServer.Types; +using Refresh.GameServer.Types.Levels; +using Refresh.GameServer.Types.Lists; using Refresh.GameServer.Types.UserData; namespace Refresh.GameServer.Endpoints.Game.DataTypes.Response; @@ -39,12 +42,21 @@ public class GameUserResponse : IDataConvertableFrom [XmlElement("lbp2PurchasedSlots")] public int PurchasedSlotsLBP2 { get; set; } [XmlElement("lbp3PurchasedSlots")] public int PurchasedSlotsLBP3 { get; set; } - public static GameUserResponse? FromOldWithExtraData(GameUser? old, TokenGame gameVersion) + /// + /// The levels the user has favourited, only used by LBP PSP + /// + [XmlElement("favouriteSlots")] public SerializedMinimalFavouriteLevelList? FavouriteLevels { get; set; } + /// + /// The users the user has favourited, only used by LBP PSP + /// + [XmlElement("favouriteUsers")] public SerializedMinimalFavouriteUserList? FavouriteUsers { get; set; } + + public static GameUserResponse? FromOldWithExtraData(GameUser? old, TokenGame gameVersion, GameDatabaseContext database) { if (old == null) return null; GameUserResponse response = FromOld(old)!; - response.FillInExtraData(old, gameVersion); + response.FillInExtraData(old, gameVersion, database); return response; } @@ -85,10 +97,10 @@ public class GameUserResponse : IDataConvertableFrom public static IEnumerable FromOldList(IEnumerable oldList) => oldList.Select(FromOld)!; - public static IEnumerable FromOldListWithExtraData(IEnumerable oldList, TokenGame gameVersion) - => oldList.Select(old => FromOldWithExtraData(old, gameVersion))!; + public static IEnumerable FromOldListWithExtraData(IEnumerable oldList, TokenGame gameVersion, GameDatabaseContext database) + => oldList.Select(old => FromOldWithExtraData(old, gameVersion, database))!; - private void FillInExtraData(GameUser old, TokenGame gameVersion) + private void FillInExtraData(GameUser old, TokenGame gameVersion, GameDatabaseContext database) { this.PlanetsHash = gameVersion switch { @@ -141,10 +153,18 @@ private void FillInExtraData(GameUser old, TokenGame gameVersion) throw new ArgumentOutOfRangeException(nameof(gameVersion), gameVersion, null); } - // Apply PSP-specific icon hashes if (gameVersion == TokenGame.LittleBigPlanetPSP) { + // Apply PSP-specific icon hashes this.IconHash = old.PspIconHash; + + //Fill out PSP favourite users + List users = database.GetUsersFavouritedByUser(old, 20, 0).ToList(); + this.FavouriteUsers = new SerializedMinimalFavouriteUserList(users.Select(SerializedUserHandle.FromUser).ToList(), users.Count); + + //Fill out PSP favourite levels + List favouriteLevels = old.FavouriteLevelRelations.Select(f => GameMinimalLevelResponse.FromOld(f.Level)).ToList()!; + this.FavouriteLevels = new SerializedMinimalFavouriteLevelList(new SerializedMinimalLevelList(favouriteLevels, favouriteLevels.Count)); } } } \ No newline at end of file diff --git a/Refresh.GameServer/Endpoints/Game/RelationEndpoints.cs b/Refresh.GameServer/Endpoints/Game/RelationEndpoints.cs index c2b2ca3d..ba6837d0 100644 --- a/Refresh.GameServer/Endpoints/Game/RelationEndpoints.cs +++ b/Refresh.GameServer/Endpoints/Game/RelationEndpoints.cs @@ -88,7 +88,7 @@ public Response UnfavouriteUser(RequestContext context, GameDatabaseContext data List users = database.GetUsersFavouritedByUser(user, count, skip) .ToList(); - return new SerializedFavouriteUserList(GameUserResponse.FromOldListWithExtraData(users, token.TokenGame).ToList(), users.Count); + return new SerializedFavouriteUserList(GameUserResponse.FromOldListWithExtraData(users, token.TokenGame, database).ToList(), users.Count); } [GameEndpoint("lolcatftw/add/user/{id}", HttpMethods.Post)] diff --git a/Refresh.GameServer/Endpoints/Game/UserEndpoints.cs b/Refresh.GameServer/Endpoints/Game/UserEndpoints.cs index a9c78829..0d30536c 100644 --- a/Refresh.GameServer/Endpoints/Game/UserEndpoints.cs +++ b/Refresh.GameServer/Endpoints/Game/UserEndpoints.cs @@ -19,7 +19,7 @@ public class UserEndpoints : EndpointGroup [GameEndpoint("user/{name}", HttpMethods.Get, ContentType.Xml)] [MinimumRole(GameUserRole.Restricted)] public GameUserResponse? GetUser(RequestContext context, GameDatabaseContext database, string name, Token token) - => GameUserResponse.FromOldWithExtraData(database.GetUserByUsername(name), token.TokenGame); + => GameUserResponse.FromOldWithExtraData(database.GetUserByUsername(name), token.TokenGame, database); [GameEndpoint("users", HttpMethods.Get, ContentType.Xml)] [MinimumRole(GameUserRole.Restricted)] @@ -35,7 +35,7 @@ public SerializedUserList GetMultipleUsers(RequestContext context, GameDatabaseC GameUser? user = database.GetUserByUsername(username); if (user == null) continue; - users.Add(GameUserResponse.FromOldWithExtraData(user, token.TokenGame)!); + users.Add(GameUserResponse.FromOldWithExtraData(user, token.TokenGame, database)!); } return new SerializedUserList @@ -53,7 +53,7 @@ public SerializedUserList GetMultipleUsers(RequestContext context, GameDatabaseC List? friends = friendService.GetUsersFriends(user, database)?.ToList(); if (friends == null) return null; - return new SerializedFriendsList(GameUserResponse.FromOldListWithExtraData(friends, token.TokenGame).ToList()); + return new SerializedFriendsList(GameUserResponse.FromOldListWithExtraData(friends, token.TokenGame, database).ToList()); } [GameEndpoint("updateUser", HttpMethods.Post, ContentType.Xml)] diff --git a/Refresh.GameServer/Types/Lists/SerializedMinimalFavouriteUserList.cs b/Refresh.GameServer/Types/Lists/SerializedMinimalFavouriteUserList.cs new file mode 100644 index 00000000..6486cbf6 --- /dev/null +++ b/Refresh.GameServer/Types/Lists/SerializedMinimalFavouriteUserList.cs @@ -0,0 +1,26 @@ +using System.Xml.Serialization; +using Refresh.GameServer.Endpoints.Game.DataTypes.Response; +using Refresh.GameServer.Types.UserData; + +namespace Refresh.GameServer.Types.Lists; + +#nullable disable + +/// +/// Minimal favourite user list, used only by LBP PSP, since it structures things a bit differently +/// +[XmlRoot("favouriteUsers")] +[XmlType("favouriteUsers")] +public class SerializedMinimalFavouriteUserList : SerializedList +{ + public SerializedMinimalFavouriteUserList() {} + + public SerializedMinimalFavouriteUserList(List list, int count) + { + this.Total = count; + this.Items = list; + } + + [XmlElement("user")] + public override List Items { get; set; } +} \ No newline at end of file