Adds preference unavailable setting to profiles.

This commit is contained in:
Pieter-Jan Briers
2020-01-19 18:33:22 +01:00
parent 77367345b6
commit fc2d53eb4f
7 changed files with 113 additions and 26 deletions

View File

@@ -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<JobPrioritySelector> _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<JobPrioritySelector>();
foreach (var job in prototypeManager.EnumeratePrototypes<JobPrototype>().OrderBy(j => j.Name))
@@ -423,6 +451,8 @@ namespace Content.Client.UserInterface
UpdateHairPickers();
UpdateSaveButton();
UpdateJobPriorities();
_preferenceUnavailableButton.SelectId((int) Profile.PreferenceUnavailable);
}
private void UpdateJobPriorities()

View File

@@ -48,6 +48,9 @@ namespace Content.Server.Database.Migrations
.IsRequired()
.HasColumnType("TEXT");
b.Property<int>("PreferenceUnavailable")
.HasColumnType("INTEGER");
b.Property<int>("PrefsId")
.HasColumnType("INTEGER");

View File

@@ -48,6 +48,7 @@ namespace Content.Server.Database
public string EyeColor { get; set; } = null!;
public string SkinColor { get; set; } = null!;
public List<Job> Jobs { get; } = new List<Job>();
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,
}
}

View File

@@ -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

View File

@@ -10,32 +10,41 @@ namespace Content.Shared.Preferences
{
private readonly Dictionary<string, JobPriority> _jobPriorities;
private HumanoidCharacterProfile(string name,
private HumanoidCharacterProfile(
string name,
int age,
Sex sex,
HumanoidCharacterAppearance appearance,
Dictionary<string, JobPriority> jobPriorities)
Dictionary<string, JobPriority> 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<string, JobPriority> jobPriorities)
: this(name, age, sex, appearance, new Dictionary<string, JobPriority>(jobPriorities))
IReadOnlyDictionary<string, JobPriority> jobPriorities,
PreferenceUnavailableMode preferenceUnavailable)
: this(name, age, sex, appearance, new Dictionary<string, JobPriority>(jobPriorities),
preferenceUnavailable)
{
}
public static HumanoidCharacterProfile Default()
{
return new HumanoidCharacterProfile("John Doe", 18, Sex.Male, HumanoidCharacterAppearance.Default(),
new Dictionary<string, JobPriority>());
new Dictionary<string, JobPriority>
{
{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<string, JobPriority> 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<string, JobPriority> jobPriorities)
{
return new HumanoidCharacterProfile(Name, Age, Sex, Appearance, new Dictionary<string, JobPriority>(jobPriorities));
return new HumanoidCharacterProfile(
Name,
Age,
Sex,
Appearance,
new Dictionary<string, JobPriority>(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);
}

View File

@@ -0,0 +1,20 @@
namespace Content.Shared.Preferences
{
/// <summary>
/// Specifies behavior when none of the jobs you want are available at round start.
/// </summary>
public enum PreferenceUnavailableMode
{
// These enum values HAVE to match the ones in DbPreferenceUnavailableMode in Server.Database.
/// <summary>
/// Stay in the lobby (if the lobby is enabled).
/// </summary>
StayInLobby = 0,
/// <summary>
/// Spawn as overflow role if preference unavailable.
/// </summary>
SpawnAsOverflow,
}
}

View File

@@ -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<string, JobPriority>
{
{"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);
}
}
}