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();
}
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
.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);
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