Files
tbd-station-14/Content.Client/CrewManifest/CrewManifestSystem.cs
Flipp Syder 3d36a6e1f6 Station records (#8720)
Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
2022-08-09 15:10:01 +10:00

83 lines
2.3 KiB
C#

using Content.Client.GameTicking.Managers;
using Content.Shared.CrewManifest;
using Content.Shared.Roles;
using Robust.Shared.Prototypes;
namespace Content.Client.CrewManifest;
public sealed class CrewManifestSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
private Dictionary<string, Dictionary<string, int>> _jobDepartmentLookup = new();
private HashSet<string> _departments = new();
public IReadOnlySet<string> Departments => _departments;
public override void Initialize()
{
base.Initialize();
BuildDepartmentLookup();
_prototypeManager.PrototypesReloaded += OnPrototypesReload;
}
public override void Shutdown()
{
_prototypeManager.PrototypesReloaded -= OnPrototypesReload;
}
/// <summary>
/// Requests a crew manifest from the server.
/// </summary>
/// <param name="uid">EntityUid of the entity we're requesting the crew manifest from.</param>
public void RequestCrewManifest(EntityUid uid)
{
RaiseNetworkEvent(new RequestCrewManifestMessage(uid));
}
private void OnPrototypesReload(PrototypesReloadedEventArgs _)
{
_jobDepartmentLookup.Clear();
_departments.Clear();
BuildDepartmentLookup();
}
private void BuildDepartmentLookup()
{
foreach (var department in _prototypeManager.EnumeratePrototypes<DepartmentPrototype>())
{
_departments.Add(department.ID);
for (var i = 1; i <= department.Roles.Count; i++)
{
if (!_jobDepartmentLookup.TryGetValue(department.Roles[i - 1], out var departments))
{
departments = new();
_jobDepartmentLookup.Add(department.Roles[i - 1], departments);
}
departments.Add(department.ID, i);
}
}
}
public int GetDepartmentOrder(string department, string jobPrototype)
{
if (!Departments.Contains(department))
{
return -1;
}
if (!_jobDepartmentLookup.TryGetValue(jobPrototype, out var departments))
{
return -1;
}
return departments.TryGetValue(department, out var order)
? order
: -1;
}
}