Add Job preference tests (#28625)

* Misc Job related changes

* Add JobTest

* A

* Aa

* Lets not confuse the yaml linter

* fixes

* a
This commit is contained in:
Leon Friedrich
2024-06-06 02:19:24 +12:00
committed by GitHub
parent e1541351a5
commit adeed705e6
44 changed files with 538 additions and 262 deletions

View File

@@ -35,7 +35,7 @@ namespace Content.Shared.Preferences
/// Job preferences for initial spawn.
/// </summary>
[DataField]
private Dictionary<string, JobPriority> _jobPriorities = new()
private Dictionary<ProtoId<JobPrototype>, JobPriority> _jobPriorities = new()
{
{
SharedGameTicker.FallbackOverflowJob, JobPriority.High
@@ -46,13 +46,13 @@ namespace Content.Shared.Preferences
/// Antags we have opted in to.
/// </summary>
[DataField]
private HashSet<string> _antagPreferences = new();
private HashSet<ProtoId<AntagPrototype>> _antagPreferences = new();
/// <summary>
/// Enabled traits.
/// </summary>
[DataField]
private HashSet<string> _traitPreferences = new();
private HashSet<ProtoId<TraitPrototype>> _traitPreferences = new();
/// <summary>
/// <see cref="_loadouts"/>
@@ -75,7 +75,7 @@ namespace Content.Shared.Preferences
/// Associated <see cref="SpeciesPrototype"/> for this profile.
/// </summary>
[DataField]
public string Species { get; set; } = SharedHumanoidAppearanceSystem.DefaultSpecies;
public ProtoId<SpeciesPrototype> Species { get; set; } = SharedHumanoidAppearanceSystem.DefaultSpecies;
[DataField]
public int Age { get; set; } = 18;
@@ -106,17 +106,17 @@ namespace Content.Shared.Preferences
/// <summary>
/// <see cref="_jobPriorities"/>
/// </summary>
public IReadOnlyDictionary<string, JobPriority> JobPriorities => _jobPriorities;
public IReadOnlyDictionary<ProtoId<JobPrototype>, JobPriority> JobPriorities => _jobPriorities;
/// <summary>
/// <see cref="_antagPreferences"/>
/// </summary>
public IReadOnlySet<string> AntagPreferences => _antagPreferences;
public IReadOnlySet<ProtoId<AntagPrototype>> AntagPreferences => _antagPreferences;
/// <summary>
/// <see cref="_traitPreferences"/>
/// </summary>
public IReadOnlySet<string> TraitPreferences => _traitPreferences;
public IReadOnlySet<ProtoId<TraitPrototype>> TraitPreferences => _traitPreferences;
/// <summary>
/// If we're unable to get one of our preferred jobs do we spawn as a fallback job or do we stay in lobby.
@@ -134,10 +134,10 @@ namespace Content.Shared.Preferences
Gender gender,
HumanoidCharacterAppearance appearance,
SpawnPriorityPreference spawnPriority,
Dictionary<string, JobPriority> jobPriorities,
Dictionary<ProtoId<JobPrototype>, JobPriority> jobPriorities,
PreferenceUnavailableMode preferenceUnavailable,
HashSet<string> antagPreferences,
HashSet<string> traitPreferences,
HashSet<ProtoId<AntagPrototype>> antagPreferences,
HashSet<ProtoId<TraitPrototype>> traitPreferences,
Dictionary<string, RoleLoadout> loadouts)
{
Name = name;
@@ -153,6 +153,20 @@ namespace Content.Shared.Preferences
_antagPreferences = antagPreferences;
_traitPreferences = traitPreferences;
_loadouts = loadouts;
var hasHighPrority = false;
foreach (var (key, value) in _jobPriorities)
{
if (value == JobPriority.Never)
_jobPriorities.Remove(key);
else if (value != JobPriority.High)
continue;
if (hasHighPrority)
_jobPriorities[key] = JobPriority.Medium;
hasHighPrority = true;
}
}
/// <summary>Copy constructor</summary>
@@ -165,10 +179,10 @@ namespace Content.Shared.Preferences
other.Gender,
other.Appearance.Clone(),
other.SpawnPriority,
new Dictionary<string, JobPriority>(other.JobPriorities),
new Dictionary<ProtoId<JobPrototype>, JobPriority>(other.JobPriorities),
other.PreferenceUnavailable,
new HashSet<string>(other.AntagPreferences),
new HashSet<string>(other.TraitPreferences),
new HashSet<ProtoId<AntagPrototype>>(other.AntagPreferences),
new HashSet<ProtoId<TraitPrototype>>(other.TraitPreferences),
new Dictionary<string, RoleLoadout>(other.Loadouts))
{
}
@@ -289,21 +303,48 @@ namespace Content.Shared.Preferences
return new(this) { SpawnPriority = spawnPriority };
}
public HumanoidCharacterProfile WithJobPriorities(IEnumerable<KeyValuePair<string, JobPriority>> jobPriorities)
public HumanoidCharacterProfile WithJobPriorities(IEnumerable<KeyValuePair<ProtoId<JobPrototype>, JobPriority>> jobPriorities)
{
var dictionary = new Dictionary<ProtoId<JobPrototype>, JobPriority>(jobPriorities);
var hasHighPrority = false;
foreach (var (key, value) in dictionary)
{
if (value == JobPriority.Never)
dictionary.Remove(key);
else if (value != JobPriority.High)
continue;
if (hasHighPrority)
dictionary[key] = JobPriority.Medium;
hasHighPrority = true;
}
return new(this)
{
_jobPriorities = new Dictionary<string, JobPriority>(jobPriorities),
_jobPriorities = dictionary
};
}
public HumanoidCharacterProfile WithJobPriority(string jobId, JobPriority priority)
public HumanoidCharacterProfile WithJobPriority(ProtoId<JobPrototype> jobId, JobPriority priority)
{
var dictionary = new Dictionary<string, JobPriority>(_jobPriorities);
var dictionary = new Dictionary<ProtoId<JobPrototype>, JobPriority>(_jobPriorities);
if (priority == JobPriority.Never)
{
dictionary.Remove(jobId);
}
else if (priority == JobPriority.High)
{
// There can only ever be one high priority job.
foreach (var (job, value) in dictionary)
{
if (value == JobPriority.High)
dictionary[job] = JobPriority.Medium;
}
dictionary[jobId] = priority;
}
else
{
dictionary[jobId] = priority;
@@ -320,17 +361,17 @@ namespace Content.Shared.Preferences
return new(this) { PreferenceUnavailable = mode };
}
public HumanoidCharacterProfile WithAntagPreferences(IEnumerable<string> antagPreferences)
public HumanoidCharacterProfile WithAntagPreferences(IEnumerable<ProtoId<AntagPrototype>> antagPreferences)
{
return new(this)
{
_antagPreferences = new HashSet<string>(antagPreferences),
_antagPreferences = new (antagPreferences),
};
}
public HumanoidCharacterProfile WithAntagPreference(string antagId, bool pref)
public HumanoidCharacterProfile WithAntagPreference(ProtoId<AntagPrototype> antagId, bool pref)
{
var list = new HashSet<string>(_antagPreferences);
var list = new HashSet<ProtoId<AntagPrototype>>(_antagPreferences);
if (pref)
{
list.Add(antagId);
@@ -346,16 +387,16 @@ namespace Content.Shared.Preferences
};
}
public HumanoidCharacterProfile WithTraitPreference(string traitId, string? categoryId, bool pref)
public HumanoidCharacterProfile WithTraitPreference(ProtoId<TraitPrototype> traitId, string? categoryId, bool pref)
{
var prototypeManager = IoCManager.Resolve<IPrototypeManager>();
var traitProto = prototypeManager.Index<TraitPrototype>(traitId);
var traitProto = prototypeManager.Index(traitId);
TraitCategoryPrototype? categoryProto = null;
if (categoryId != null && categoryId != "default")
categoryProto = prototypeManager.Index<TraitCategoryPrototype>(categoryId);
var list = new HashSet<string>(_traitPreferences);
var list = new HashSet<ProtoId<TraitPrototype>>(_traitPreferences);
if (pref)
{
@@ -372,7 +413,7 @@ namespace Content.Shared.Preferences
var count = 0;
foreach (var trait in list)
{
var traitProtoTemp = prototypeManager.Index<TraitPrototype>(trait);
var traitProtoTemp = prototypeManager.Index(trait);
count += traitProtoTemp.Cost;
}
@@ -514,7 +555,7 @@ namespace Content.Shared.Preferences
_ => SpawnPriorityPreference.None // Invalid enum values.
};
var priorities = new Dictionary<string, JobPriority>(JobPriorities
var priorities = new Dictionary<ProtoId<JobPrototype>, JobPriority>(JobPriorities
.Where(p => prototypeManager.TryIndex<JobPrototype>(p.Key, out var job) && job.SetPreference && p.Value switch
{
JobPriority.Never => false, // Drop never since that's assumed default.
@@ -524,6 +565,17 @@ namespace Content.Shared.Preferences
_ => false
}));
var hasHighPrio = false;
foreach (var (key, value) in priorities)
{
if (value != JobPriority.High)
continue;
if (hasHighPrio)
priorities[key] = JobPriority.Medium;
hasHighPrio = true;
}
var antags = AntagPreferences
.Where(id => prototypeManager.TryIndex<AntagPrototype>(id, out var antag) && antag.SetPreference)
.ToList();