1
0

NukeCodePaperSystem.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System.Diagnostics.CodeAnalysis;
  2. using Content.Server.Chat.Systems;
  3. using Content.Server.Fax;
  4. using Content.Shared.Fax.Components;
  5. using Content.Server.Station.Components;
  6. using Content.Server.Station.Systems;
  7. using Content.Shared.Paper;
  8. using Robust.Shared.Random;
  9. using Robust.Shared.Utility;
  10. namespace Content.Server.Nuke
  11. {
  12. public sealed class NukeCodePaperSystem : EntitySystem
  13. {
  14. [Dependency] private readonly IRobustRandom _random = default!;
  15. [Dependency] private readonly ChatSystem _chatSystem = default!;
  16. [Dependency] private readonly StationSystem _station = default!;
  17. [Dependency] private readonly PaperSystem _paper = default!;
  18. [Dependency] private readonly FaxSystem _faxSystem = default!;
  19. public override void Initialize()
  20. {
  21. base.Initialize();
  22. SubscribeLocalEvent<NukeCodePaperComponent, MapInitEvent>(OnMapInit,
  23. after: new []{ typeof(NukeLabelSystem) });
  24. }
  25. private void OnMapInit(EntityUid uid, NukeCodePaperComponent component, MapInitEvent args)
  26. {
  27. SetupPaper(uid, component);
  28. }
  29. private void SetupPaper(EntityUid uid, NukeCodePaperComponent? component = null, EntityUid? station = null)
  30. {
  31. if (!Resolve(uid, ref component))
  32. return;
  33. if (TryGetRelativeNukeCode(uid, out var paperContent, station, onlyCurrentStation: component.AllNukesAvailable))
  34. {
  35. if (TryComp<PaperComponent>(uid, out var paperComp))
  36. _paper.SetContent((uid, paperComp), paperContent);
  37. }
  38. }
  39. /// <summary>
  40. /// Send a nuclear code to all faxes on that station which are authorized to receive nuke codes.
  41. /// </summary>
  42. /// <returns>True if at least one fax received codes</returns>
  43. public bool SendNukeCodes(EntityUid station)
  44. {
  45. if (!HasComp<StationDataComponent>(station))
  46. {
  47. return false;
  48. }
  49. var faxes = EntityQueryEnumerator<FaxMachineComponent>();
  50. var wasSent = false;
  51. while (faxes.MoveNext(out var faxEnt, out var fax))
  52. {
  53. if (!fax.ReceiveNukeCodes || !TryGetRelativeNukeCode(faxEnt, out var paperContent, station))
  54. {
  55. continue;
  56. }
  57. var printout = new FaxPrintout(
  58. paperContent,
  59. Loc.GetString("nuke-codes-fax-paper-name"),
  60. null,
  61. null,
  62. "paper_stamp-centcom",
  63. new List<StampDisplayInfo>
  64. {
  65. new StampDisplayInfo { StampedName = Loc.GetString("stamp-component-stamped-name-centcom"), StampedColor = Color.FromHex("#BB3232") },
  66. }
  67. );
  68. _faxSystem.Receive(faxEnt, printout, null, fax);
  69. wasSent = true;
  70. }
  71. if (wasSent)
  72. {
  73. var msg = Loc.GetString("nuke-component-announcement-send-codes");
  74. _chatSystem.DispatchStationAnnouncement(station, msg, colorOverride: Color.Red);
  75. }
  76. return wasSent;
  77. }
  78. private bool TryGetRelativeNukeCode(
  79. EntityUid uid,
  80. [NotNullWhen(true)] out string? nukeCode,
  81. EntityUid? station = null,
  82. TransformComponent? transform = null,
  83. bool onlyCurrentStation = false)
  84. {
  85. nukeCode = null;
  86. if (!Resolve(uid, ref transform))
  87. {
  88. return false;
  89. }
  90. var owningStation = station ?? _station.GetOwningStation(uid);
  91. var codesMessage = new FormattedMessage();
  92. // Find the first nuke that matches the passed location.
  93. var nukes = new List<Entity<NukeComponent>>();
  94. var query = EntityQueryEnumerator<NukeComponent>();
  95. while (query.MoveNext(out var nukeUid, out var nuke))
  96. {
  97. nukes.Add((nukeUid, nuke));
  98. }
  99. _random.Shuffle(nukes);
  100. foreach (var (nukeUid, nuke) in nukes)
  101. {
  102. if (!onlyCurrentStation &&
  103. (owningStation == null &&
  104. nuke.OriginMapGrid != (transform.MapID, transform.GridUid) ||
  105. nuke.OriginStation != owningStation))
  106. {
  107. continue;
  108. }
  109. codesMessage.PushNewline();
  110. codesMessage.AddMarkupOrThrow(Loc.GetString("nuke-codes-list", ("name", MetaData(nukeUid).EntityName), ("code", nuke.Code)));
  111. break;
  112. }
  113. if (!codesMessage.IsEmpty)
  114. nukeCode = Loc.GetString("nuke-codes-message")+codesMessage;
  115. return !codesMessage.IsEmpty;
  116. }
  117. }
  118. }