]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Modify existing profile in SaveCharacterSlotAsync if one exists (#16381)
authorDrSmugleaf <DrSmugleaf@users.noreply.github.com>
Sat, 13 May 2023 12:35:55 +0000 (05:35 -0700)
committerGitHub <noreply@github.com>
Sat, 13 May 2023 12:35:55 +0000 (22:35 +1000)
Content.Server/Database/ServerDbBase.cs

index 0f7973aa9709753064f34ec3c11962d5cfa64b44..96fddf9a40cf9d5d1cbfeaf5161db48836a90f78 100644 (file)
@@ -68,23 +68,26 @@ namespace Content.Server.Database
                 throw new NotImplementedException();
             }
 
-            var entity = ConvertProfiles(humanoid, 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)
-            {
-                prefs.Profiles.Remove(oldProfile);
+            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 newProfile = ConvertProfiles(humanoid, slot, oldProfile);
+            if (oldProfile == null)
+            {
+                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<string> 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