DeliverySystem.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using Content.Server.Cargo.Components;
  2. using Content.Server.Cargo.Systems;
  3. using Content.Server.Station.Systems;
  4. using Content.Server.StationRecords.Systems;
  5. using Content.Shared.Delivery;
  6. using Content.Shared.FingerprintReader;
  7. using Content.Shared.Labels.EntitySystems;
  8. using Content.Shared.StationRecords;
  9. using Robust.Shared.Audio.Systems;
  10. using Robust.Shared.Containers;
  11. namespace Content.Server.Delivery;
  12. /// <summary>
  13. /// System for managing deliveries spawned by the mail teleporter.
  14. /// This covers for mail spawning, as well as granting cargo money.
  15. /// </summary>
  16. public sealed partial class DeliverySystem : SharedDeliverySystem
  17. {
  18. [Dependency] private readonly CargoSystem _cargo = default!;
  19. [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
  20. [Dependency] private readonly SharedAudioSystem _audio = default!;
  21. [Dependency] private readonly StationRecordsSystem _records = default!;
  22. [Dependency] private readonly StationSystem _station = default!;
  23. [Dependency] private readonly FingerprintReaderSystem _fingerprintReader = default!;
  24. [Dependency] private readonly SharedLabelSystem _label = default!;
  25. [Dependency] private readonly SharedContainerSystem _container = default!;
  26. public override void Initialize()
  27. {
  28. base.Initialize();
  29. SubscribeLocalEvent<DeliveryComponent, MapInitEvent>(OnMapInit);
  30. InitializeSpawning();
  31. }
  32. private void OnMapInit(Entity<DeliveryComponent> ent, ref MapInitEvent args)
  33. {
  34. _container.EnsureContainer<Container>(ent, ent.Comp.Container);
  35. var stationId = _station.GetStationInMap(Transform(ent).MapID);
  36. if (stationId == null)
  37. return;
  38. _records.TryGetRandomRecord<GeneralStationRecord>(stationId.Value, out var entry);
  39. if (entry == null)
  40. return;
  41. ent.Comp.RecipientName = entry.Name;
  42. ent.Comp.RecipientJobTitle = entry.JobTitle;
  43. ent.Comp.RecipientStation = stationId.Value;
  44. _appearance.SetData(ent, DeliveryVisuals.JobIcon, entry.JobIcon);
  45. _label.Label(ent, ent.Comp.RecipientName);
  46. if (TryComp<FingerprintReaderComponent>(ent, out var reader) && entry.Fingerprint != null)
  47. {
  48. _fingerprintReader.AddAllowedFingerprint((ent.Owner, reader), entry.Fingerprint);
  49. }
  50. Dirty(ent);
  51. }
  52. protected override void GrantSpesoReward(Entity<DeliveryComponent?> ent)
  53. {
  54. if (!Resolve(ent, ref ent.Comp))
  55. return;
  56. if (!TryComp<StationBankAccountComponent>(ent.Comp.RecipientStation, out var account))
  57. return;
  58. _cargo.UpdateBankAccount((ent.Comp.RecipientStation.Value, account), ent.Comp.SpesoReward);
  59. }
  60. public override void Update(float frameTime)
  61. {
  62. base.Update(frameTime);
  63. UpdateSpawner(frameTime);
  64. }
  65. }