1
0

CrewManifestSystem.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. using System.Linq;
  2. using Content.Server.Administration;
  3. using Content.Server.EUI;
  4. using Content.Server.Station.Components;
  5. using Content.Server.Station.Systems;
  6. using Content.Server.StationRecords;
  7. using Content.Server.StationRecords.Systems;
  8. using Content.Shared.Administration;
  9. using Content.Shared.CCVar;
  10. using Content.Shared.CrewManifest;
  11. using Content.Shared.GameTicking;
  12. using Content.Shared.Roles;
  13. using Content.Shared.StationRecords;
  14. using Robust.Shared.Configuration;
  15. using Robust.Shared.Console;
  16. using Robust.Shared.Player;
  17. using Robust.Shared.Prototypes;
  18. using Robust.Shared.Utility;
  19. namespace Content.Server.CrewManifest;
  20. public sealed class CrewManifestSystem : EntitySystem
  21. {
  22. [Dependency] private readonly StationSystem _stationSystem = default!;
  23. [Dependency] private readonly StationRecordsSystem _recordsSystem = default!;
  24. [Dependency] private readonly EuiManager _euiManager = default!;
  25. [Dependency] private readonly IConfigurationManager _configManager = default!;
  26. [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
  27. /// <summary>
  28. /// Cached crew manifest entries. The alternative is to outright
  29. /// rebuild the crew manifest every time the state is requested:
  30. /// this is inefficient.
  31. /// </summary>
  32. private readonly Dictionary<EntityUid, CrewManifestEntries> _cachedEntries = new();
  33. private readonly Dictionary<EntityUid, Dictionary<ICommonSession, CrewManifestEui>> _openEuis = new();
  34. public override void Initialize()
  35. {
  36. SubscribeLocalEvent<AfterGeneralRecordCreatedEvent>(AfterGeneralRecordCreated);
  37. SubscribeLocalEvent<RecordModifiedEvent>(OnRecordModified);
  38. SubscribeLocalEvent<RecordRemovedEvent>(OnRecordRemoved);
  39. SubscribeLocalEvent<RoundRestartCleanupEvent>(OnRoundRestart);
  40. SubscribeNetworkEvent<RequestCrewManifestMessage>(OnRequestCrewManifest);
  41. SubscribeLocalEvent<CrewManifestViewerComponent, BoundUIClosedEvent>(OnBoundUiClose);
  42. SubscribeLocalEvent<CrewManifestViewerComponent, CrewManifestOpenUiMessage>(OpenEuiFromBui);
  43. }
  44. private void OnRoundRestart(RoundRestartCleanupEvent ev)
  45. {
  46. foreach (var (_, euis) in _openEuis)
  47. {
  48. foreach (var (_, eui) in euis)
  49. {
  50. eui.Close();
  51. }
  52. }
  53. _openEuis.Clear();
  54. _cachedEntries.Clear();
  55. }
  56. private void OnRequestCrewManifest(RequestCrewManifestMessage message, EntitySessionEventArgs args)
  57. {
  58. if (args.SenderSession is not { } sessionCast
  59. || !_configManager.GetCVar(CCVars.CrewManifestWithoutEntity))
  60. {
  61. return;
  62. }
  63. OpenEui(GetEntity(message.Id), sessionCast);
  64. }
  65. // Not a big fan of this one. Rebuilds the crew manifest every time
  66. // somebody spawns in, meaning that at round start, it rebuilds the crew manifest
  67. // wrt the amount of players readied up.
  68. private void AfterGeneralRecordCreated(AfterGeneralRecordCreatedEvent ev)
  69. {
  70. BuildCrewManifest(ev.Key.OriginStation);
  71. UpdateEuis(ev.Key.OriginStation);
  72. }
  73. private void OnRecordModified(RecordModifiedEvent ev)
  74. {
  75. BuildCrewManifest(ev.Key.OriginStation);
  76. UpdateEuis(ev.Key.OriginStation);
  77. }
  78. private void OnRecordRemoved(RecordRemovedEvent ev)
  79. {
  80. BuildCrewManifest(ev.Key.OriginStation);
  81. UpdateEuis(ev.Key.OriginStation);
  82. }
  83. private void OnBoundUiClose(EntityUid uid, CrewManifestViewerComponent component, BoundUIClosedEvent ev)
  84. {
  85. if (!Equals(ev.UiKey, component.OwnerKey))
  86. return;
  87. var owningStation = _stationSystem.GetOwningStation(uid);
  88. if (owningStation == null || !TryComp(ev.Actor, out ActorComponent? actorComp))
  89. {
  90. return;
  91. }
  92. CloseEui(owningStation.Value, actorComp.PlayerSession, uid);
  93. }
  94. /// <summary>
  95. /// Gets the crew manifest for a given station, along with the name of the station.
  96. /// </summary>
  97. /// <param name="station">Entity uid of the station.</param>
  98. /// <returns>The name and crew manifest entries (unordered) of the station.</returns>
  99. public (string name, CrewManifestEntries? entries) GetCrewManifest(EntityUid station)
  100. {
  101. var valid = _cachedEntries.TryGetValue(station, out var manifest);
  102. return (valid ? MetaData(station).EntityName : string.Empty, valid ? manifest : null);
  103. }
  104. private void UpdateEuis(EntityUid station)
  105. {
  106. if (_openEuis.TryGetValue(station, out var euis))
  107. {
  108. foreach (var eui in euis.Values)
  109. {
  110. eui.StateDirty();
  111. }
  112. }
  113. }
  114. private void OpenEuiFromBui(EntityUid uid, CrewManifestViewerComponent component, CrewManifestOpenUiMessage msg)
  115. {
  116. if (!msg.UiKey.Equals(component.OwnerKey))
  117. {
  118. Log.Error(
  119. "{User} tried to open crew manifest from wrong UI: {Key}. Correct owned is {ExpectedKey}",
  120. msg.Actor, msg.UiKey, component.OwnerKey);
  121. return;
  122. }
  123. var owningStation = _stationSystem.GetOwningStation(uid);
  124. if (owningStation == null || !TryComp(msg.Actor, out ActorComponent? actorComp))
  125. {
  126. return;
  127. }
  128. if (!_configManager.GetCVar(CCVars.CrewManifestUnsecure) && component.Unsecure)
  129. {
  130. return;
  131. }
  132. OpenEui(owningStation.Value, actorComp.PlayerSession, uid);
  133. }
  134. /// <summary>
  135. /// Opens a crew manifest EUI for a given player.
  136. /// </summary>
  137. /// <param name="station">Station that we're displaying the crew manifest for.</param>
  138. /// <param name="session">The player's session.</param>
  139. /// <param name="owner">If this EUI should be 'owned' by an entity.</param>
  140. public void OpenEui(EntityUid station, ICommonSession session, EntityUid? owner = null)
  141. {
  142. if (!HasComp<StationRecordsComponent>(station))
  143. {
  144. return;
  145. }
  146. if (!_openEuis.TryGetValue(station, out var euis))
  147. {
  148. euis = new();
  149. _openEuis.Add(station, euis);
  150. }
  151. if (euis.ContainsKey(session))
  152. {
  153. return;
  154. }
  155. var eui = new CrewManifestEui(station, owner, this);
  156. euis.Add(session, eui);
  157. _euiManager.OpenEui(eui, session);
  158. eui.StateDirty();
  159. }
  160. /// <summary>
  161. /// Closes an EUI for a given player.
  162. /// </summary>
  163. /// <param name="station">Station that we're displaying the crew manifest for.</param>
  164. /// <param name="session">The player's session.</param>
  165. /// <param name="owner">The owner of this EUI, if there was one.</param>
  166. public void CloseEui(EntityUid station, ICommonSession session, EntityUid? owner = null)
  167. {
  168. if (!HasComp<StationRecordsComponent>(station))
  169. {
  170. return;
  171. }
  172. if (!_openEuis.TryGetValue(station, out var euis)
  173. || !euis.TryGetValue(session, out var eui))
  174. {
  175. return;
  176. }
  177. if (eui.Owner == owner)
  178. {
  179. euis.Remove(session);
  180. eui.Close();
  181. }
  182. if (euis.Count == 0)
  183. {
  184. _openEuis.Remove(station);
  185. }
  186. }
  187. /// <summary>
  188. /// Builds the crew manifest for a station. Stores it in the cache afterwards.
  189. /// </summary>
  190. /// <param name="station"></param>
  191. private void BuildCrewManifest(EntityUid station)
  192. {
  193. var iter = _recordsSystem.GetRecordsOfType<GeneralStationRecord>(station);
  194. var entries = new CrewManifestEntries();
  195. var entriesSort = new List<(JobPrototype? job, CrewManifestEntry entry)>();
  196. foreach (var recordObject in iter)
  197. {
  198. var record = recordObject.Item2;
  199. var entry = new CrewManifestEntry(record.Name, record.JobTitle, record.JobIcon, record.JobPrototype);
  200. _prototypeManager.TryIndex(record.JobPrototype, out JobPrototype? job);
  201. entriesSort.Add((job, entry));
  202. }
  203. entriesSort.Sort((a, b) =>
  204. {
  205. var cmp = JobUIComparer.Instance.Compare(a.job, b.job);
  206. if (cmp != 0)
  207. return cmp;
  208. return string.Compare(a.entry.Name, b.entry.Name, StringComparison.CurrentCultureIgnoreCase);
  209. });
  210. entries.Entries = entriesSort.Select(x => x.entry).ToArray();
  211. _cachedEntries[station] = entries;
  212. }
  213. }
  214. [AdminCommand(AdminFlags.Admin)]
  215. public sealed class CrewManifestCommand : IConsoleCommand
  216. {
  217. public string Command => "crewmanifest";
  218. public string Description => "Opens the crew manifest for the given station.";
  219. public string Help => $"Usage: {Command} <entity uid>";
  220. [Dependency] private readonly IEntityManager _entityManager = default!;
  221. public CrewManifestCommand()
  222. {
  223. IoCManager.InjectDependencies(this);
  224. }
  225. public void Execute(IConsoleShell shell, string argStr, string[] args)
  226. {
  227. if (args.Length != 1)
  228. {
  229. shell.WriteLine($"Invalid argument count.\n{Help}");
  230. return;
  231. }
  232. if (!NetEntity.TryParse(args[0], out var uidNet) || !_entityManager.TryGetEntity(uidNet, out var uid))
  233. {
  234. shell.WriteLine($"{args[0]} is not a valid entity UID.");
  235. return;
  236. }
  237. if (shell.Player == null || shell.Player is not { } session)
  238. {
  239. shell.WriteLine("You must run this from a client.");
  240. return;
  241. }
  242. var crewManifestSystem = _entityManager.System<CrewManifestSystem>();
  243. crewManifestSystem.OpenEui(uid.Value, session);
  244. }
  245. public CompletionResult GetCompletion(IConsoleShell shell, string[] args)
  246. {
  247. if (args.Length != 1)
  248. {
  249. return CompletionResult.Empty;
  250. }
  251. var stations = new List<CompletionOption>();
  252. var query = _entityManager.EntityQueryEnumerator<StationDataComponent>();
  253. while (query.MoveNext(out var uid, out _))
  254. {
  255. var meta = _entityManager.GetComponent<MetaDataComponent>(uid);
  256. stations.Add(new CompletionOption(uid.ToString(), meta.EntityName));
  257. }
  258. return CompletionResult.FromHintOptions(stations, null);
  259. }
  260. }