Modify existing profile in SaveCharacterSlotAsync if one exists (#16381)

This commit is contained in:
DrSmugleaf
2023-05-13 05:35:55 -07:00
committed by GitHub
parent ca21b13b76
commit 636ee85e18

View File

@@ -68,23 +68,26 @@ namespace Content.Server.Database
throw new NotImplementedException(); 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 newProfile = ConvertProfiles(humanoid, slot, oldProfile);
if (oldProfile == null)
{
var prefs = await db.DbContext var prefs = await db.DbContext
.Preference .Preference
.Include(p => p.Profiles) .Include(p => p.Profiles)
.SingleAsync(p => p.UserId == userId.UserId); .SingleAsync(p => p.UserId == userId.UserId);
var oldProfile = prefs prefs.Profiles.Add(newProfile);
.Profiles
.SingleOrDefault(h => h.Slot == entity.Slot);
if (oldProfile is not null)
{
prefs.Profiles.Remove(oldProfile);
} }
prefs.Profiles.Add(entity);
await db.DbContext.SaveChangesAsync(); 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; var appearance = (HumanoidCharacterAppearance) humanoid.CharacterAppearance;
List<string> markingStrings = new(); List<string> markingStrings = new();
foreach (var marking in appearance.Markings) foreach (var marking in appearance.Markings)
@@ -226,41 +230,44 @@ namespace Content.Server.Database
} }
var markings = JsonSerializer.SerializeToDocument(markingStrings); var markings = JsonSerializer.SerializeToDocument(markingStrings);
var entity = new Profile profile.CharacterName = humanoid.Name;
{ profile.FlavorText = humanoid.FlavorText;
CharacterName = humanoid.Name, profile.Species = humanoid.Species;
FlavorText = humanoid.FlavorText, profile.Age = humanoid.Age;
Species = humanoid.Species, profile.Sex = humanoid.Sex.ToString();
Age = humanoid.Age, profile.Gender = humanoid.Gender.ToString();
Sex = humanoid.Sex.ToString(), profile.HairName = appearance.HairStyleId;
Gender = humanoid.Gender.ToString(), profile.HairColor = appearance.HairColor.ToHex();
HairName = appearance.HairStyleId, profile.FacialHairName = appearance.FacialHairStyleId;
HairColor = appearance.HairColor.ToHex(), profile.FacialHairColor = appearance.FacialHairColor.ToHex();
FacialHairName = appearance.FacialHairStyleId, profile.EyeColor = appearance.EyeColor.ToHex();
FacialHairColor = appearance.FacialHairColor.ToHex(), profile.SkinColor = appearance.SkinColor.ToHex();
EyeColor = appearance.EyeColor.ToHex(), profile.Clothing = humanoid.Clothing.ToString();
SkinColor = appearance.SkinColor.ToHex(), profile.Backpack = humanoid.Backpack.ToString();
Clothing = humanoid.Clothing.ToString(), profile.Markings = markings;
Backpack = humanoid.Backpack.ToString(), profile.Slot = slot;
Markings = markings, profile.PreferenceUnavailable = (DbPreferenceUnavailableMode) humanoid.PreferenceUnavailable;
Slot = slot,
PreferenceUnavailable = (DbPreferenceUnavailableMode) humanoid.PreferenceUnavailable profile.Jobs.Clear();
}; profile.Jobs.AddRange(
entity.Jobs.AddRange(
humanoid.JobPriorities humanoid.JobPriorities
.Where(j => j.Value != JobPriority.Never) .Where(j => j.Value != JobPriority.Never)
.Select(j => new Job {JobName = j.Key, Priority = (DbJobPriority) j.Value}) .Select(j => new Job {JobName = j.Key, Priority = (DbJobPriority) j.Value})
); );
entity.Antags.AddRange(
profile.Antags.Clear();
profile.Antags.AddRange(
humanoid.AntagPreferences humanoid.AntagPreferences
.Select(a => new Antag {AntagName = a}) .Select(a => new Antag {AntagName = a})
); );
entity.Traits.AddRange(
profile.Traits.Clear();
profile.Traits.AddRange(
humanoid.TraitPreferences humanoid.TraitPreferences
.Select(t => new Trait {TraitName = t}) .Select(t => new Trait {TraitName = t})
); );
return entity; return profile;
} }
#endregion #endregion