ClericalErrorRule.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using Content.Server.GameTicking.Rules.Components;
  2. using Content.Server.StationEvents.Components;
  3. using Content.Server.StationRecords;
  4. using Content.Server.StationRecords.Systems;
  5. using Content.Shared.StationRecords;
  6. using Content.Shared.GameTicking.Components;
  7. using Robust.Shared.Random;
  8. namespace Content.Server.StationEvents.Events;
  9. public sealed class ClericalErrorRule : StationEventSystem<ClericalErrorRuleComponent>
  10. {
  11. [Dependency] private readonly StationRecordsSystem _stationRecords = default!;
  12. protected override void Started(EntityUid uid, ClericalErrorRuleComponent component, GameRuleComponent gameRule, GameRuleStartedEvent args)
  13. {
  14. base.Started(uid, component, gameRule, args);
  15. if (!TryGetRandomStation(out var chosenStation))
  16. return;
  17. if (!TryComp<StationRecordsComponent>(chosenStation, out var stationRecords))
  18. return;
  19. var recordCount = stationRecords.Records.Keys.Count;
  20. if (recordCount == 0)
  21. return;
  22. var min = (int) Math.Max(1, Math.Round(component.MinToRemove * recordCount));
  23. var max = (int) Math.Max(min, Math.Round(component.MaxToRemove * recordCount));
  24. var toRemove = RobustRandom.Next(min, max);
  25. var keys = new List<uint>();
  26. for (var i = 0; i < toRemove; i++)
  27. {
  28. keys.Add(RobustRandom.Pick(stationRecords.Records.Keys));
  29. }
  30. foreach (var id in keys)
  31. {
  32. var key = new StationRecordKey(id, chosenStation.Value);
  33. _stationRecords.RemoveRecord(key, stationRecords);
  34. }
  35. }
  36. }