From 636ee85e187475201c9eed7b1ceb0e5dcbe4b72d Mon Sep 17 00:00:00 2001 From: DrSmugleaf Date: Sat, 13 May 2023 05:35:55 -0700 Subject: [PATCH] Modify existing profile in SaveCharacterSlotAsync if one exists (#16381) --- Content.Server/Database/ServerDbBase.cs | 83 ++++++++++++++----------- 1 file changed, 45 insertions(+), 38 deletions(-) diff --git a/Content.Server/Database/ServerDbBase.cs b/Content.Server/Database/ServerDbBase.cs index 0f7973aa97..96fddf9a40 100644 --- a/Content.Server/Database/ServerDbBase.cs +++ b/Content.Server/Database/ServerDbBase.cs @@ -68,23 +68,26 @@ namespace Content.Server.Database throw new NotImplementedException(); } - var entity = ConvertProfiles(humanoid, slot); + var oldProfile = db.DbContext.Profile + .Include(p => p.Preference) + .Where(p => p.Preference.UserId == userId.UserId) + .Include(p => p.Jobs) + .Include(p => p.Antags) + .Include(p => p.Traits) + .AsSplitQuery() + .SingleOrDefault(h => h.Slot == slot); - var prefs = await db.DbContext - .Preference - .Include(p => p.Profiles) - .SingleAsync(p => p.UserId == userId.UserId); - - var oldProfile = prefs - .Profiles - .SingleOrDefault(h => h.Slot == entity.Slot); - - if (oldProfile is not null) + var newProfile = ConvertProfiles(humanoid, slot, oldProfile); + if (oldProfile == null) { - prefs.Profiles.Remove(oldProfile); + var prefs = await db.DbContext + .Preference + .Include(p => p.Profiles) + .SingleAsync(p => p.UserId == userId.UserId); + + prefs.Profiles.Add(newProfile); } - prefs.Profiles.Add(entity); await db.DbContext.SaveChangesAsync(); } @@ -216,8 +219,9 @@ namespace Content.Server.Database ); } - private static Profile ConvertProfiles(HumanoidCharacterProfile humanoid, int slot) + private static Profile ConvertProfiles(HumanoidCharacterProfile humanoid, int slot, Profile? profile = null) { + profile ??= new Profile(); var appearance = (HumanoidCharacterAppearance) humanoid.CharacterAppearance; List markingStrings = new(); foreach (var marking in appearance.Markings) @@ -226,41 +230,44 @@ namespace Content.Server.Database } var markings = JsonSerializer.SerializeToDocument(markingStrings); - var entity = new Profile - { - CharacterName = humanoid.Name, - FlavorText = humanoid.FlavorText, - Species = humanoid.Species, - Age = humanoid.Age, - Sex = humanoid.Sex.ToString(), - Gender = humanoid.Gender.ToString(), - HairName = appearance.HairStyleId, - HairColor = appearance.HairColor.ToHex(), - FacialHairName = appearance.FacialHairStyleId, - FacialHairColor = appearance.FacialHairColor.ToHex(), - EyeColor = appearance.EyeColor.ToHex(), - SkinColor = appearance.SkinColor.ToHex(), - Clothing = humanoid.Clothing.ToString(), - Backpack = humanoid.Backpack.ToString(), - Markings = markings, - Slot = slot, - PreferenceUnavailable = (DbPreferenceUnavailableMode) humanoid.PreferenceUnavailable - }; - entity.Jobs.AddRange( + profile.CharacterName = humanoid.Name; + profile.FlavorText = humanoid.FlavorText; + profile.Species = humanoid.Species; + profile.Age = humanoid.Age; + profile.Sex = humanoid.Sex.ToString(); + profile.Gender = humanoid.Gender.ToString(); + profile.HairName = appearance.HairStyleId; + profile.HairColor = appearance.HairColor.ToHex(); + profile.FacialHairName = appearance.FacialHairStyleId; + profile.FacialHairColor = appearance.FacialHairColor.ToHex(); + profile.EyeColor = appearance.EyeColor.ToHex(); + profile.SkinColor = appearance.SkinColor.ToHex(); + profile.Clothing = humanoid.Clothing.ToString(); + profile.Backpack = humanoid.Backpack.ToString(); + profile.Markings = markings; + profile.Slot = slot; + profile.PreferenceUnavailable = (DbPreferenceUnavailableMode) humanoid.PreferenceUnavailable; + + profile.Jobs.Clear(); + profile.Jobs.AddRange( humanoid.JobPriorities .Where(j => j.Value != JobPriority.Never) .Select(j => new Job {JobName = j.Key, Priority = (DbJobPriority) j.Value}) ); - entity.Antags.AddRange( + + profile.Antags.Clear(); + profile.Antags.AddRange( humanoid.AntagPreferences .Select(a => new Antag {AntagName = a}) ); - entity.Traits.AddRange( + + profile.Traits.Clear(); + profile.Traits.AddRange( humanoid.TraitPreferences .Select(t => new Trait {TraitName = t}) ); - return entity; + return profile; } #endregion