JobsCommand.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System.Linq;
  2. using Content.Server.Administration;
  3. using Content.Server.Station.Systems;
  4. using Content.Shared.Administration;
  5. using Content.Shared.Roles;
  6. using Robust.Shared.Toolshed;
  7. using Robust.Shared.Toolshed.Syntax;
  8. using Robust.Shared.Toolshed.TypeParsers;
  9. namespace Content.Server.Station.Commands;
  10. [ToolshedCommand, AdminCommand(AdminFlags.VarEdit)]
  11. public sealed class JobsCommand : ToolshedCommand
  12. {
  13. private StationJobsSystem? _jobs;
  14. [CommandImplementation("jobs")]
  15. public IEnumerable<JobSlotRef> Jobs([PipedArgument] EntityUid station)
  16. {
  17. _jobs ??= GetSys<StationJobsSystem>();
  18. foreach (var (job, _) in _jobs.GetJobs(station))
  19. {
  20. yield return new JobSlotRef(job, station, _jobs, EntityManager);
  21. }
  22. }
  23. [CommandImplementation("jobs")]
  24. public IEnumerable<JobSlotRef> Jobs([PipedArgument] IEnumerable<EntityUid> stations)
  25. => stations.SelectMany(Jobs);
  26. [CommandImplementation("job")]
  27. public JobSlotRef Job([PipedArgument] EntityUid station, Prototype<JobPrototype> job)
  28. {
  29. _jobs ??= GetSys<StationJobsSystem>();
  30. return new JobSlotRef(job.Value.ID, station, _jobs, EntityManager);
  31. }
  32. [CommandImplementation("job")]
  33. public IEnumerable<JobSlotRef> Job([PipedArgument] IEnumerable<EntityUid> stations, Prototype<JobPrototype> job)
  34. => stations.Select(x => Job(x, job));
  35. [CommandImplementation("isinfinite")]
  36. public bool IsInfinite([PipedArgument] JobSlotRef job, [CommandInverted] bool inverted)
  37. => job.Infinite() ^ inverted;
  38. [CommandImplementation("isinfinite")]
  39. public IEnumerable<bool> IsInfinite([PipedArgument] IEnumerable<JobSlotRef> jobs, [CommandInverted] bool inverted)
  40. => jobs.Select(x => IsInfinite(x, inverted));
  41. [CommandImplementation("adjust")]
  42. public JobSlotRef Adjust([PipedArgument] JobSlotRef @ref, int by)
  43. {
  44. _jobs ??= GetSys<StationJobsSystem>();
  45. _jobs.TryAdjustJobSlot(@ref.Station, @ref.Job, by, true, true);
  46. return @ref;
  47. }
  48. [CommandImplementation("adjust")]
  49. public IEnumerable<JobSlotRef> Adjust([PipedArgument] IEnumerable<JobSlotRef> @ref, int by)
  50. => @ref.Select(x => Adjust(x, by));
  51. [CommandImplementation("set")]
  52. public JobSlotRef Set([PipedArgument] JobSlotRef @ref, int by)
  53. {
  54. _jobs ??= GetSys<StationJobsSystem>();
  55. _jobs.TrySetJobSlot(@ref.Station, @ref.Job, by, true);
  56. return @ref;
  57. }
  58. [CommandImplementation("set")]
  59. public IEnumerable<JobSlotRef> Set([PipedArgument] IEnumerable<JobSlotRef> @ref, int by)
  60. => @ref.Select(x => Set(x, by));
  61. [CommandImplementation("amount")]
  62. public int Amount([PipedArgument] JobSlotRef @ref)
  63. {
  64. _jobs ??= GetSys<StationJobsSystem>();
  65. _jobs.TryGetJobSlot(@ref.Station, @ref.Job, out var slots);
  66. return slots ?? 0;
  67. }
  68. [CommandImplementation("amount")]
  69. public IEnumerable<int> Amount([PipedArgument] IEnumerable<JobSlotRef> @ref)
  70. => @ref.Select(Amount);
  71. }
  72. // Used for Toolshed queries.
  73. public readonly record struct JobSlotRef(string Job, EntityUid Station, StationJobsSystem Jobs, IEntityManager EntityManager)
  74. {
  75. public override string ToString()
  76. {
  77. if (!Jobs.TryGetJobSlot(Station, Job, out var slot))
  78. {
  79. return $"{EntityManager.ToPrettyString(Station)} job {Job} : (not a slot)";
  80. }
  81. return $"{EntityManager.ToPrettyString(Station)} job {Job} : {slot?.ToString() ?? "infinite"}";
  82. }
  83. public bool Infinite()
  84. {
  85. return Jobs.TryGetJobSlot(Station, Job, out var slot) && slot is null;
  86. }
  87. }