CrewManifestSystem.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using Content.Shared.CrewManifest;
  2. using Content.Shared.Roles;
  3. using Robust.Shared.Prototypes;
  4. namespace Content.Client.CrewManifest;
  5. public sealed class CrewManifestSystem : EntitySystem
  6. {
  7. [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
  8. private Dictionary<string, Dictionary<string, int>> _jobDepartmentLookup = new();
  9. private HashSet<string> _departments = new();
  10. public IReadOnlySet<string> Departments => _departments;
  11. public override void Initialize()
  12. {
  13. base.Initialize();
  14. BuildDepartmentLookup();
  15. SubscribeLocalEvent<PrototypesReloadedEventArgs>(OnPrototypesReload);
  16. }
  17. /// <summary>
  18. /// Requests a crew manifest from the server.
  19. /// </summary>
  20. /// <param name="netEntity">EntityUid of the entity we're requesting the crew manifest from.</param>
  21. public void RequestCrewManifest(NetEntity netEntity)
  22. {
  23. RaiseNetworkEvent(new RequestCrewManifestMessage(netEntity));
  24. }
  25. private void OnPrototypesReload(PrototypesReloadedEventArgs args)
  26. {
  27. if (args.WasModified<DepartmentPrototype>())
  28. BuildDepartmentLookup();
  29. }
  30. private void BuildDepartmentLookup()
  31. {
  32. _jobDepartmentLookup.Clear();
  33. _departments.Clear();
  34. foreach (var department in _prototypeManager.EnumeratePrototypes<DepartmentPrototype>())
  35. {
  36. _departments.Add(department.ID);
  37. for (var i = 1; i <= department.Roles.Count; i++)
  38. {
  39. if (!_jobDepartmentLookup.TryGetValue(department.Roles[i - 1], out var departments))
  40. {
  41. departments = new();
  42. _jobDepartmentLookup.Add(department.Roles[i - 1], departments);
  43. }
  44. departments.Add(department.ID, i);
  45. }
  46. }
  47. }
  48. public int GetDepartmentOrder(string department, string jobPrototype)
  49. {
  50. if (!Departments.Contains(department))
  51. {
  52. return -1;
  53. }
  54. if (!_jobDepartmentLookup.TryGetValue(jobPrototype, out var departments))
  55. {
  56. return -1;
  57. }
  58. return departments.TryGetValue(department, out var order)
  59. ? order
  60. : -1;
  61. }
  62. }