]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Add TryGetPrimaryDepartment to jobs system (#23317)
authordeltanedas <39013340+deltanedas@users.noreply.github.com>
Sun, 14 Jan 2024 08:18:34 +0000 (08:18 +0000)
committerGitHub <noreply@github.com>
Sun, 14 Jan 2024 08:18:34 +0000 (19:18 +1100)
* add primary departments

* make command and station specific secondary

* add a unit test

* fixy

* compile

* webedit ops

---------

Co-authored-by: deltanedas <@deltanedas:kde.org>
Content.IntegrationTests/Tests/Minds/JobTests.cs [new file with mode: 0644]
Content.Shared/Roles/DepartmentPrototype.cs
Content.Shared/Roles/Jobs/SharedJobSystem.cs
Resources/Prototypes/Roles/Jobs/departments.yml

diff --git a/Content.IntegrationTests/Tests/Minds/JobTests.cs b/Content.IntegrationTests/Tests/Minds/JobTests.cs
new file mode 100644 (file)
index 0000000..5172049
--- /dev/null
@@ -0,0 +1,48 @@
+using Content.Shared.Roles;
+using Content.Shared.Roles.Jobs;
+using Robust.Shared.Prototypes;
+using System.Linq;
+
+namespace Content.IntegrationTests.Tests.Station;
+
+[TestFixture]
+[TestOf(typeof(SharedJobSystem))]
+public sealed class JobTest
+{
+    /// <summary>
+    /// Ensures that every job belongs to at most 1 primary department.
+    /// Having no primary department is ok.
+    /// </summary>
+    [Test]
+    public async Task PrimaryDepartmentsTest()
+    {
+        await using var pair = await PoolManager.GetServerClient();
+        var server = pair.Server;
+
+        var prototypeManager = server.ResolveDependency<IPrototypeManager>();
+
+        await server.WaitAssertion(() =>
+        {
+            // only checking primary departments so don't bother with others
+            var departments = prototypeManager.EnumeratePrototypes<DepartmentPrototype>()
+                .Where(department => department.Primary)
+                .ToList();
+            var jobs = prototypeManager.EnumeratePrototypes<JobPrototype>();
+            foreach (var job in jobs)
+            {
+                // not actually using the jobs system since that will return the first department
+                // and we need to test that there is never more than 1, so it not sorting them is correct
+                var primaries = 0;
+                foreach (var department in departments)
+                {
+                    if (!department.Roles.Contains(job.ID))
+                        continue;
+
+                    primaries++;
+                    Assert.That(primaries, Is.EqualTo(1), $"The job {job.ID} has more than 1 primary department!");
+                }
+            }
+        });
+        await pair.CleanReturnAsync();
+    }
+}
index f91d1a43fd00febc880e4e5b1670bdc3ac988fed..b3549d9584dad1957c65b39870e42b2109b0153a 100644 (file)
@@ -23,4 +23,11 @@ public sealed partial class DepartmentPrototype : IPrototype
     [ViewVariables(VVAccess.ReadWrite),
      DataField("roles", customTypeSerializer: typeof(PrototypeIdListSerializer<JobPrototype>))]
     public List<string> Roles = new();
+
+    /// <summary>
+    /// Whether this is a primary department or not.
+    /// For example, CE's primary department is engineering since Command has primary: false.
+    /// </summary>
+    [DataField, ViewVariables(VVAccess.ReadWrite)]
+    public bool Primary = true;
 }
index fe0f9d115bf9e2406595e64ca2d40be25d75f216..04ac45c4c5f4d939c82992b441e101fdb4bc9e8d 100644 (file)
@@ -76,6 +76,30 @@ public abstract class SharedJobSystem : EntitySystem
         return false;
     }
 
+    /// <summary>
+    /// Like <see cref="TryGetDepartment"/> but ignores any non-primary departments.
+    /// For example, with CE it will return Engineering but with captain it will
+    /// not return anything, since Command is not a primary department.
+    /// </summary>
+    public bool TryGetPrimaryDepartment(string jobProto, [NotNullWhen(true)] out DepartmentPrototype? departmentPrototype)
+    {
+        // not sorting it since there should only be 1 primary department for a job.
+        // this is enforced by the job tests.
+        var departmentProtos = _protoManager.EnumeratePrototypes<DepartmentPrototype>();
+
+        foreach (var department in departmentProtos)
+        {
+            if (department.Primary && department.Roles.Contains(jobProto))
+            {
+                departmentPrototype = department;
+                return true;
+            }
+        }
+
+        departmentPrototype = null;
+        return false;
+    }
+
     public bool MindHasJobWithId(EntityUid? mindId, string prototypeId)
     {
         return CompOrNull<JobComponent>(mindId)?.Prototype == prototypeId;
index 5b5ee2252de00be2aade2b2c37514b33044549ce..e1df3977219bf28a71f48d266520b7c1f3fbd623 100644 (file)
@@ -43,6 +43,7 @@
   - HeadOfSecurity
   - ResearchDirector
   - Quartermaster
+  primary: false
 
 - type: department
   id: Engineering
@@ -95,3 +96,4 @@
   - Reporter
   - Zookeeper
   - Psychologist
+  primary: false