diff --git a/ProjectLighthouse/Controllers/GameApi/UserController.cs b/ProjectLighthouse/Controllers/GameApi/UserController.cs index 90c8f227..62c99565 100644 --- a/ProjectLighthouse/Controllers/GameApi/UserController.cs +++ b/ProjectLighthouse/Controllers/GameApi/UserController.cs @@ -61,8 +61,13 @@ public class UserController : ControllerBase [HttpPost("updateUser")] public async Task UpdateUser() { - User? user = await this.database.UserFromGameRequest(this.Request); - if (user == null) return this.StatusCode(403, ""); + (User, GameToken)? userAndToken = await this.database.UserAndGameTokenFromRequest(this.Request); + + if (userAndToken == null) return this.StatusCode(403, ""); + + // ReSharper disable once PossibleInvalidOperationException + User user = userAndToken.Value.Item1; + GameToken gameToken = userAndToken.Value.Item2; XmlReaderSettings settings = new() { @@ -121,7 +126,32 @@ public class UserController : ControllerBase } case "planets": { - user.PlanetHash = await reader.GetValueAsync(); + switch (gameToken.GameVersion) + { + case GameVersion.LittleBigPlanet2: // LBP2 planets will apply to LBP3 + { + user.PlanetHashLBP2 = await reader.GetValueAsync(); + user.PlanetHashLBP3 = await reader.GetValueAsync(); + break; + } + case GameVersion.LittleBigPlanet3: // LBP3 and vita can only apply to their own games, only set 1 here + { + user.PlanetHashLBP3 = await reader.GetValueAsync(); + break; + } + case GameVersion.LittleBigPlanetVita: + { + user.PlanetHashLBPVita = await reader.GetValueAsync(); + break; + } + case GameVersion.LittleBigPlanet1: + case GameVersion.LittleBigPlanetPSP: + case GameVersion.Unknown: + default: // The rest do not support custom earths. + { + throw new ArgumentException($"invalid gameVersion {gameToken.GameVersion} for setting earth"); + } + } break; } case "yay2": @@ -129,16 +159,16 @@ public class UserController : ControllerBase user.YayHash = await reader.GetValueAsync(); break; } - case "boo2": - { - user.BooHash = await reader.GetValueAsync(); - break; - } case "meh2": { user.MehHash = await reader.GetValueAsync(); break; } + case "boo2": + { + user.BooHash = await reader.GetValueAsync(); + break; + } } break; diff --git a/ProjectLighthouse/Migrations/20220216230824_AddEarthHashesForAllGames.cs b/ProjectLighthouse/Migrations/20220216230824_AddEarthHashesForAllGames.cs new file mode 100644 index 00000000..d44d08fa --- /dev/null +++ b/ProjectLighthouse/Migrations/20220216230824_AddEarthHashesForAllGames.cs @@ -0,0 +1,51 @@ +using LBPUnion.ProjectLighthouse; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace ProjectLighthouse.Migrations +{ + [DbContext(typeof(Database))] + [Migration("20220216230824_AddEarthHashesForAllGames")] + public partial class AddEarthHashesForAllGames : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.RenameColumn( + name: "PlanetHash", + table: "Users", + newName: "PlanetHashLBP2"); + + migrationBuilder.AddColumn( + name: "PlanetHashLBP3", + table: "Users", + type: "longtext", + nullable: true) + .Annotation("MySql:CharSet", "utf8mb4"); + + migrationBuilder.AddColumn( + name: "PlanetHashLBPVita", + table: "Users", + type: "longtext", + nullable: true) + .Annotation("MySql:CharSet", "utf8mb4"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "PlanetHashLBP2", + table: "Users"); + + migrationBuilder.DropColumn( + name: "PlanetHashLBP3", + table: "Users"); + + migrationBuilder.RenameColumn( + name: "PlanetHashLBPVita", + table: "Users", + newName: "PlanetHash"); + } + } +} diff --git a/ProjectLighthouse/Migrations/DatabaseModelSnapshot.cs b/ProjectLighthouse/Migrations/DatabaseModelSnapshot.cs index 9f747f1c..21676d01 100644 --- a/ProjectLighthouse/Migrations/DatabaseModelSnapshot.cs +++ b/ProjectLighthouse/Migrations/DatabaseModelSnapshot.cs @@ -687,7 +687,13 @@ namespace ProjectLighthouse.Migrations b.Property("Pins") .HasColumnType("longtext"); - b.Property("PlanetHash") + b.Property("PlanetHashLBP2") + .HasColumnType("longtext"); + + b.Property("PlanetHashLBP3") + .HasColumnType("longtext"); + + b.Property("PlanetHashLBPVita") .HasColumnType("longtext"); b.Property("Username") diff --git a/ProjectLighthouse/Types/User.cs b/ProjectLighthouse/Types/User.cs index be237d02..7b730d49 100644 --- a/ProjectLighthouse/Types/User.cs +++ b/ProjectLighthouse/Types/User.cs @@ -123,7 +123,13 @@ public class User public string Pins { get; set; } = ""; [JsonIgnore] - public string PlanetHash { get; set; } = ""; + public string PlanetHashLBP2 { get; set; } = ""; + + [JsonIgnore] + public string PlanetHashLBP3 { get; set; } = ""; + + [JsonIgnore] + public string PlanetHashLBPVita { get; set; } = ""; [JsonIgnore] public int Hearts { @@ -170,8 +176,8 @@ public class User public string Serialize(GameVersion gameVersion = GameVersion.LittleBigPlanet1) { string user = LbpSerializer.TaggedStringElement("npHandle", this.Username, "icon", this.IconHash) + - LbpSerializer.StringElement("game", this.Game) + - this.SerializeSlots(gameVersion) + + LbpSerializer.StringElement("game", (int)gameVersion) + + this.serializeSlots(gameVersion) + LbpSerializer.StringElement("lists", this.Lists) + LbpSerializer.StringElement("lists_quota", ServerSettings.Instance.ListsQuota) + // technically not a part of the user but LBP expects it LbpSerializer.StringElement("biography", this.Biography) + @@ -185,7 +191,7 @@ public class User LbpSerializer.StringElement("favouriteUserCount", this.HeartedUsers) + LbpSerializer.StringElement("lolcatftwCount", this.QueuedLevels) + LbpSerializer.StringElement("pins", this.Pins) + - LbpSerializer.StringElement("planets", this.PlanetHash) + + serializeEarth(gameVersion) + LbpSerializer.BlankElement("photos") + LbpSerializer.StringElement("heartCount", this.Hearts) + LbpSerializer.StringElement("yay2", this.YayHash) + @@ -195,6 +201,21 @@ public class User return LbpSerializer.TaggedStringElement("user", user, "type", "user"); } + private string serializeEarth(GameVersion gameVersion) + { + return LbpSerializer.StringElement + ( + "planets", + gameVersion switch + { + GameVersion.LittleBigPlanet2 => PlanetHashLBP2, + GameVersion.LittleBigPlanet3 => PlanetHashLBP3, + GameVersion.LittleBigPlanetVita => PlanetHashLBPVita, + _ => "", // other versions do not have custom planets + } + ); + } + #region Slots /// @@ -226,7 +247,7 @@ public class User "lbp2", "lbp3", "crossControl", }; - private string SerializeSlots(GameVersion gameVersion) + private string serializeSlots(GameVersion gameVersion) { string slots = string.Empty;