From fc2d53eb4f0f5e321997f725908a2b793c42f20d Mon Sep 17 00:00:00 2001 From: Pieter-Jan Briers Date: Sun, 19 Jan 2020 18:33:22 +0100 Subject: [PATCH] Adds preference unavailable setting to profiles. --- .../UserInterface/HumanoidProfileEditor.cs | 44 +++++++++++++++--- .../PreferencesDbContextModelSnapshot.cs | 3 ++ Content.Server.Database/Model.cs | 8 ++++ .../Preferences/PreferencesDatabase.cs | 6 ++- .../Preferences/HumanoidCharacterProfile.cs | 46 ++++++++++++++----- .../Preferences/PreferenceUnavailableMode.cs | 20 ++++++++ .../Preferences/PreferencesDatabaseTests.cs | 12 +++-- 7 files changed, 113 insertions(+), 26 deletions(-) create mode 100644 Content.Shared/Preferences/PreferenceUnavailableMode.cs diff --git a/Content.Client/UserInterface/HumanoidProfileEditor.cs b/Content.Client/UserInterface/HumanoidProfileEditor.cs index 2101a0f157f..42445237160 100644 --- a/Content.Client/UserInterface/HumanoidProfileEditor.cs +++ b/Content.Client/UserInterface/HumanoidProfileEditor.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using Content.Client.GameObjects.Components; using Content.Client.Interfaces; +using Content.Shared; using Content.Shared.Jobs; using Content.Shared.Preferences; using Content.Shared.Text; @@ -39,6 +40,7 @@ namespace Content.Client.UserInterface private readonly HairStylePicker _hairPicker; private readonly FacialHairStylePicker _facialHairPicker; private readonly List _jobPriorities; + private readonly OptionButton _preferenceUnavailableButton; private bool _isDirty; public int CharacterSlot; @@ -109,10 +111,7 @@ namespace Content.Client.UserInterface CustomMinimumSize = (270, 0), SizeFlagsVertical = SizeFlags.ShrinkCenter }; - _nameEdit.OnTextChanged += args => - { - NameChanged(args.Text); - }; + _nameEdit.OnTextChanged += args => { NameChanged(args.Text); }; var nameRandomButton = new Button { Text = localization.GetString("Randomize"), @@ -280,13 +279,42 @@ namespace Content.Client.UserInterface { var jobList = new VBoxContainer(); - tabContainer.AddChild(new ScrollContainer + var jobVBox = new VBoxContainer { - Children = {jobList} - }); + Children = + { + (_preferenceUnavailableButton = new OptionButton()), + new ScrollContainer + { + SizeFlagsVertical = SizeFlags.FillExpand, + Children = + { + jobList + } + } + } + }; + + tabContainer.AddChild(jobVBox); tabContainer.SetTabTitle(1, Loc.GetString("Jobs")); + _preferenceUnavailableButton.AddItem( + Loc.GetString("Stay in lobby if preference unavailable."), + (int) PreferenceUnavailableMode.StayInLobby); + _preferenceUnavailableButton.AddItem( + Loc.GetString("Be an {0} if preference unavailable.", + Loc.GetString(SharedGameTicker.OverflowJobName)), + (int) PreferenceUnavailableMode.SpawnAsOverflow); + + _preferenceUnavailableButton.OnItemSelected += args => + { + _preferenceUnavailableButton.SelectId(args.Id); + + Profile = Profile.WithPreferenceUnavailable((PreferenceUnavailableMode) args.Id); + IsDirty = true; + }; + _jobPriorities = new List(); foreach (var job in prototypeManager.EnumeratePrototypes().OrderBy(j => j.Name)) @@ -423,6 +451,8 @@ namespace Content.Client.UserInterface UpdateHairPickers(); UpdateSaveButton(); UpdateJobPriorities(); + + _preferenceUnavailableButton.SelectId((int) Profile.PreferenceUnavailable); } private void UpdateJobPriorities() diff --git a/Content.Server.Database/Migrations/PreferencesDbContextModelSnapshot.cs b/Content.Server.Database/Migrations/PreferencesDbContextModelSnapshot.cs index 9803ee3c2bd..271ec78c0b4 100644 --- a/Content.Server.Database/Migrations/PreferencesDbContextModelSnapshot.cs +++ b/Content.Server.Database/Migrations/PreferencesDbContextModelSnapshot.cs @@ -48,6 +48,9 @@ namespace Content.Server.Database.Migrations .IsRequired() .HasColumnType("TEXT"); + b.Property("PreferenceUnavailable") + .HasColumnType("INTEGER"); + b.Property("PrefsId") .HasColumnType("INTEGER"); diff --git a/Content.Server.Database/Model.cs b/Content.Server.Database/Model.cs index f35f0081786..2ea94a49b9a 100644 --- a/Content.Server.Database/Model.cs +++ b/Content.Server.Database/Model.cs @@ -48,6 +48,7 @@ namespace Content.Server.Database public string EyeColor { get; set; } = null!; public string SkinColor { get; set; } = null!; public List Jobs { get; } = new List(); + public DbPreferenceUnavailableMode PreferenceUnavailable { get; set; } public int PrefsId { get; set; } public Prefs Prefs { get; set; } = null!; @@ -70,4 +71,11 @@ namespace Content.Server.Database Medium = 2, High = 3 } + + public enum DbPreferenceUnavailableMode + { + // These enum values HAVE to match the ones in PreferenceUnavailableMode in Shared. + StayInLobby = 0, + SpawnAsOverflow, + } } diff --git a/Content.Server/Preferences/PreferencesDatabase.cs b/Content.Server/Preferences/PreferencesDatabase.cs index efbff33c539..ebf1baf54e8 100644 --- a/Content.Server/Preferences/PreferencesDatabase.cs +++ b/Content.Server/Preferences/PreferencesDatabase.cs @@ -45,7 +45,8 @@ namespace Content.Server.Preferences Color.FromHex(profile.EyeColor), Color.FromHex(profile.SkinColor) ), - jobs + jobs, + (PreferenceUnavailableMode) profile.PreferenceUnavailable ); } @@ -88,7 +89,8 @@ namespace Content.Server.Preferences FacialHairColor = appearance.FacialHairColor.ToHex(), EyeColor = appearance.EyeColor.ToHex(), SkinColor = appearance.SkinColor.ToHex(), - Slot = slot + Slot = slot, + PreferenceUnavailable = (DbPreferenceUnavailableMode) humanoid.PreferenceUnavailable }; entity.Jobs.AddRange( humanoid.JobPriorities diff --git a/Content.Shared/Preferences/HumanoidCharacterProfile.cs b/Content.Shared/Preferences/HumanoidCharacterProfile.cs index b8327f1742a..0c11579ffbd 100644 --- a/Content.Shared/Preferences/HumanoidCharacterProfile.cs +++ b/Content.Shared/Preferences/HumanoidCharacterProfile.cs @@ -10,32 +10,41 @@ namespace Content.Shared.Preferences { private readonly Dictionary _jobPriorities; - private HumanoidCharacterProfile(string name, + private HumanoidCharacterProfile( + string name, int age, Sex sex, HumanoidCharacterAppearance appearance, - Dictionary jobPriorities) + Dictionary jobPriorities, + PreferenceUnavailableMode preferenceUnavailable) { Name = name; Age = age; Sex = sex; Appearance = appearance; _jobPriorities = jobPriorities; + PreferenceUnavailable = preferenceUnavailable; } - public HumanoidCharacterProfile(string name, + public HumanoidCharacterProfile( + string name, int age, Sex sex, HumanoidCharacterAppearance appearance, - IReadOnlyDictionary jobPriorities) - : this(name, age, sex, appearance, new Dictionary(jobPriorities)) + IReadOnlyDictionary jobPriorities, + PreferenceUnavailableMode preferenceUnavailable) + : this(name, age, sex, appearance, new Dictionary(jobPriorities), + preferenceUnavailable) { } public static HumanoidCharacterProfile Default() { return new HumanoidCharacterProfile("John Doe", 18, Sex.Male, HumanoidCharacterAppearance.Default(), - new Dictionary()); + new Dictionary + { + {SharedGameTicker.OverflowJob, JobPriority.High} + }, PreferenceUnavailableMode.StayInLobby); } public string Name { get; } @@ -44,30 +53,37 @@ namespace Content.Shared.Preferences public ICharacterAppearance CharacterAppearance => Appearance; public HumanoidCharacterAppearance Appearance { get; } public IReadOnlyDictionary JobPriorities => _jobPriorities; + public PreferenceUnavailableMode PreferenceUnavailable { get; } public HumanoidCharacterProfile WithName(string name) { - return new HumanoidCharacterProfile(name, Age, Sex, Appearance, _jobPriorities); + return new HumanoidCharacterProfile(name, Age, Sex, Appearance, _jobPriorities, PreferenceUnavailable); } public HumanoidCharacterProfile WithAge(int age) { - return new HumanoidCharacterProfile(Name, age, Sex, Appearance, _jobPriorities); + return new HumanoidCharacterProfile(Name, age, Sex, Appearance, _jobPriorities, PreferenceUnavailable); } public HumanoidCharacterProfile WithSex(Sex sex) { - return new HumanoidCharacterProfile(Name, Age, sex, Appearance, _jobPriorities); + return new HumanoidCharacterProfile(Name, Age, sex, Appearance, _jobPriorities, PreferenceUnavailable); } public HumanoidCharacterProfile WithCharacterAppearance(HumanoidCharacterAppearance appearance) { - return new HumanoidCharacterProfile(Name, Age, Sex, appearance, _jobPriorities); + return new HumanoidCharacterProfile(Name, Age, Sex, appearance, _jobPriorities, PreferenceUnavailable); } public HumanoidCharacterProfile WithJobPriorities(IReadOnlyDictionary jobPriorities) { - return new HumanoidCharacterProfile(Name, Age, Sex, Appearance, new Dictionary(jobPriorities)); + return new HumanoidCharacterProfile( + Name, + Age, + Sex, + Appearance, + new Dictionary(jobPriorities), + PreferenceUnavailable); } public HumanoidCharacterProfile WithJobPriority(string jobId, JobPriority priority) @@ -82,7 +98,12 @@ namespace Content.Shared.Preferences dictionary[jobId] = priority; } - return new HumanoidCharacterProfile(Name, Age, Sex, Appearance, dictionary); + return new HumanoidCharacterProfile(Name, Age, Sex, Appearance, dictionary, PreferenceUnavailable); + } + + public HumanoidCharacterProfile WithPreferenceUnavailable(PreferenceUnavailableMode mode) + { + return new HumanoidCharacterProfile(Name, Age, Sex, Appearance, _jobPriorities, mode); } public string Summary => @@ -94,6 +115,7 @@ namespace Content.Shared.Preferences if (Name != other.Name) return false; if (Age != other.Age) return false; if (Sex != other.Sex) return false; + if (PreferenceUnavailable != other.PreferenceUnavailable) return false; if (!_jobPriorities.SequenceEqual(other._jobPriorities)) return false; return Appearance.MemberwiseEquals(other.Appearance); } diff --git a/Content.Shared/Preferences/PreferenceUnavailableMode.cs b/Content.Shared/Preferences/PreferenceUnavailableMode.cs new file mode 100644 index 00000000000..4c00ec41482 --- /dev/null +++ b/Content.Shared/Preferences/PreferenceUnavailableMode.cs @@ -0,0 +1,20 @@ +namespace Content.Shared.Preferences +{ + /// + /// Specifies behavior when none of the jobs you want are available at round start. + /// + public enum PreferenceUnavailableMode + { + // These enum values HAVE to match the ones in DbPreferenceUnavailableMode in Server.Database. + + /// + /// Stay in the lobby (if the lobby is enabled). + /// + StayInLobby = 0, + + /// + /// Spawn as overflow role if preference unavailable. + /// + SpawnAsOverflow, + } +} diff --git a/Content.Tests/Server/Preferences/PreferencesDatabaseTests.cs b/Content.Tests/Server/Preferences/PreferencesDatabaseTests.cs index ad43543200a..031654b3d82 100644 --- a/Content.Tests/Server/Preferences/PreferencesDatabaseTests.cs +++ b/Content.Tests/Server/Preferences/PreferencesDatabaseTests.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; using Content.Server.Preferences; +using Content.Shared; using Content.Shared.Preferences; using NUnit.Framework; using Robust.Shared.Maths; @@ -27,12 +28,13 @@ namespace Content.Tests.Server.Preferences Color.Aquamarine, Color.Azure, Color.Beige - ), + ), new Dictionary { - {"Assistant", JobPriority.High} - } - ); + {SharedGameTicker.OverflowJob, JobPriority.High} + }, + PreferenceUnavailableMode.StayInLobby + ); } private static PreferencesDatabase GetDb() @@ -99,7 +101,7 @@ namespace Content.Tests.Server.Preferences db.SaveSelectedCharacterIndex(username, MaxCharacterSlots); prefs = db.GetPlayerPreferences(username); - Assert.AreEqual(prefs.SelectedCharacterIndex, MaxCharacterSlots-1); + Assert.AreEqual(prefs.SelectedCharacterIndex, MaxCharacterSlots - 1); } } }