From 00114655063a95a1714011915287da642ee0650a Mon Sep 17 00:00:00 2001 From: fox Date: Mon, 16 Dec 2024 23:33:15 +0300 Subject: [PATCH] Fixes --- .../GameTicking/StationEventCondition.cs | 34 ++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/Content.Server/FloofStation/GameTicking/StationEventCondition.cs b/Content.Server/FloofStation/GameTicking/StationEventCondition.cs index 549ada404b9..42418c42cc2 100644 --- a/Content.Server/FloofStation/GameTicking/StationEventCondition.cs +++ b/Content.Server/FloofStation/GameTicking/StationEventCondition.cs @@ -1,3 +1,4 @@ +using System.Linq; using Content.Server.GameTicking; using Content.Server.Mind; using Content.Server.StationEvents; @@ -10,7 +11,6 @@ using Robust.Shared.Player; using Robust.Shared.Prototypes; using Robust.Shared.Random; -using Serilog; namespace Content.Server.FloofStation.GameTicking; @@ -53,15 +53,15 @@ public sealed class Dependencies(IEntityManager entMan, GameTicker ticker, Event [Dependency] public IPlayerManager PlayerManager = default!; /// - /// List of all players along with their jobs. The dept field may have the default value if it could not be determined. + /// The list of all players along with their jobs. /// - public List<(ICommonSession session, EntityUid uid, ProtoId job, ProtoId dept)> Players = new(); + public List<(ICommonSession session, EntityUid uid, ProtoId job)> Players = new(); public Dictionary, int> JobCounts = new(); public Dictionary, int> DeptCounts = new(); // Lookups private readonly Dictionary> _jobTitleToPrototype = new(); - private readonly Dictionary, ProtoId> _jobToDept = new(); + private readonly Dictionary, List>> _jobToDepts = new(); /// /// Called once after the instantiation of the class. @@ -74,20 +74,18 @@ public void Initialize() IdCard = EntMan.System(); Minds = EntMan.System(); - // Build the lookups - SharedJobSystem contains methods that iterate over all of those lists each time, + // Build the inverse lookups - SharedJobSystem contains methods that iterate over all of those lists each time, // Resulting in an O(n^2 * m) performance cost for each update() call. foreach (var job in ProtoMan.EnumeratePrototypes()) { _jobTitleToPrototype[job.LocalizedName] = job.ID; - foreach (var dept in ProtoMan.EnumeratePrototypes()) - { - if (!dept.Primary || !dept.Roles.Contains(job.ID)) - continue; + var depts = ProtoMan.EnumeratePrototypes() + .Where(it => it.Roles.Contains(job.ID)) + .Select(it => new ProtoId(it.ID)) + .ToList(); - _jobToDept[job.ID] = dept.ID; - break; - } + _jobToDepts[job.ID] = depts; } } @@ -124,11 +122,15 @@ public void Update() if (job == default) continue; - var dept = _jobToDept.GetValueOrDefault(job); - - Players.Add((session, player, job, dept)); + // Update the info + Players.Add((session, player, job)); JobCounts[job] = JobCounts.GetValueOrDefault(job, 0) + 1; - DeptCounts[dept] = DeptCounts.GetValueOrDefault(dept, 0) + 1; + // Increment the number of players in each dept this job belongs to + if (_jobToDepts.TryGetValue(job, out var depts)) + { + foreach (var dept in depts) + DeptCounts[dept] = DeptCounts.GetValueOrDefault(dept, 0) + 1; + } } #if DEBUG