IdCardSystem.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System.Linq;
  2. using Content.Server.Administration.Logs;
  3. using Content.Server.Kitchen.Components;
  4. using Content.Server.Popups;
  5. using Content.Shared.Access;
  6. using Content.Shared.Access.Components;
  7. using Content.Shared.Access.Systems;
  8. using Content.Shared.Database;
  9. using Content.Shared.Popups;
  10. using Robust.Shared.Prototypes;
  11. using Robust.Shared.Random;
  12. using Content.Server.Kitchen.EntitySystems;
  13. namespace Content.Server.Access.Systems;
  14. public sealed class IdCardSystem : SharedIdCardSystem
  15. {
  16. [Dependency] private readonly PopupSystem _popupSystem = default!;
  17. [Dependency] private readonly IRobustRandom _random = default!;
  18. [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
  19. [Dependency] private readonly IAdminLogManager _adminLogger = default!;
  20. [Dependency] private readonly MicrowaveSystem _microwave = default!;
  21. public override void Initialize()
  22. {
  23. base.Initialize();
  24. SubscribeLocalEvent<IdCardComponent, BeingMicrowavedEvent>(OnMicrowaved);
  25. }
  26. private void OnMicrowaved(EntityUid uid, IdCardComponent component, BeingMicrowavedEvent args)
  27. {
  28. if (!component.CanMicrowave || !TryComp<MicrowaveComponent>(args.Microwave, out var micro) || micro.Broken)
  29. return;
  30. if (TryComp<AccessComponent>(uid, out var access))
  31. {
  32. float randomPick = _random.NextFloat();
  33. // if really unlucky, burn card
  34. if (randomPick <= 0.15f)
  35. {
  36. TryComp(uid, out TransformComponent? transformComponent);
  37. if (transformComponent != null)
  38. {
  39. _popupSystem.PopupCoordinates(Loc.GetString("id-card-component-microwave-burnt", ("id", uid)),
  40. transformComponent.Coordinates, PopupType.Medium);
  41. EntityManager.SpawnEntity("FoodBadRecipe",
  42. transformComponent.Coordinates);
  43. }
  44. _adminLogger.Add(LogType.Action, LogImpact.Medium,
  45. $"{ToPrettyString(args.Microwave)} burnt {ToPrettyString(uid):entity}");
  46. EntityManager.QueueDeleteEntity(uid);
  47. return;
  48. }
  49. //Explode if the microwave can't handle it
  50. if (!micro.CanMicrowaveIdsSafely)
  51. {
  52. _microwave.Explode((args.Microwave, micro));
  53. return;
  54. }
  55. // If they're unlucky, brick their ID
  56. if (randomPick <= 0.25f)
  57. {
  58. _popupSystem.PopupEntity(Loc.GetString("id-card-component-microwave-bricked", ("id", uid)), uid);
  59. access.Tags.Clear();
  60. Dirty(uid, access);
  61. _adminLogger.Add(LogType.Action, LogImpact.Medium,
  62. $"{ToPrettyString(args.Microwave)} cleared access on {ToPrettyString(uid):entity}");
  63. }
  64. else
  65. {
  66. _popupSystem.PopupEntity(Loc.GetString("id-card-component-microwave-safe", ("id", uid)), uid, PopupType.Medium);
  67. }
  68. // Give them a wonderful new access to compensate for everything
  69. var ids = _prototypeManager.EnumeratePrototypes<AccessLevelPrototype>().Where(x => x.CanAddToIdCard).ToArray();
  70. if (ids.Length == 0)
  71. return;
  72. var random = _random.Pick(ids);
  73. access.Tags.Add(random.ID);
  74. Dirty(uid, access);
  75. _adminLogger.Add(LogType.Action, LogImpact.Medium,
  76. $"{ToPrettyString(args.Microwave)} added {random.ID} access to {ToPrettyString(uid):entity}");
  77. }
  78. }
  79. }