1
0

StationEventSystem.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using Content.Server.Administration.Logs;
  2. using Content.Server.Chat.Systems;
  3. using Content.Server.GameTicking;
  4. using Content.Server.GameTicking.Rules;
  5. using Content.Server.Station.Systems;
  6. using Content.Server.StationEvents.Components;
  7. using Content.Shared.Database;
  8. using Content.Shared.GameTicking.Components;
  9. using Robust.Shared.Audio.Systems;
  10. using Robust.Shared.Player;
  11. using Robust.Shared.Prototypes;
  12. namespace Content.Server.StationEvents.Events;
  13. /// <summary>
  14. /// An abstract entity system inherited by all station events for their behavior.
  15. /// </summary>
  16. public abstract class StationEventSystem<T> : GameRuleSystem<T> where T : IComponent
  17. {
  18. [Dependency] protected readonly IAdminLogManager AdminLogManager = default!;
  19. [Dependency] protected readonly IPrototypeManager PrototypeManager = default!;
  20. [Dependency] protected readonly ChatSystem ChatSystem = default!;
  21. [Dependency] protected readonly SharedAudioSystem Audio = default!;
  22. [Dependency] protected readonly StationSystem StationSystem = default!;
  23. protected ISawmill Sawmill = default!;
  24. public override void Initialize()
  25. {
  26. base.Initialize();
  27. Sawmill = Logger.GetSawmill("stationevents");
  28. }
  29. /// <inheritdoc/>
  30. protected override void Added(EntityUid uid, T component, GameRuleComponent gameRule, GameRuleAddedEvent args)
  31. {
  32. base.Added(uid, component, gameRule, args);
  33. if (!TryComp<StationEventComponent>(uid, out var stationEvent))
  34. return;
  35. AdminLogManager.Add(LogType.EventAnnounced, $"Event added / announced: {ToPrettyString(uid)}");
  36. // we don't want to send to players who aren't in game (i.e. in the lobby)
  37. Filter allPlayersInGame = Filter.Empty().AddWhere(GameTicker.UserHasJoinedGame);
  38. if (stationEvent.StartAnnouncement != null)
  39. ChatSystem.DispatchFilteredAnnouncement(allPlayersInGame, Loc.GetString(stationEvent.StartAnnouncement), playSound: false, colorOverride: stationEvent.StartAnnouncementColor);
  40. Audio.PlayGlobal(stationEvent.StartAudio, allPlayersInGame, true);
  41. }
  42. /// <inheritdoc/>
  43. protected override void Started(EntityUid uid, T component, GameRuleComponent gameRule, GameRuleStartedEvent args)
  44. {
  45. base.Started(uid, component, gameRule, args);
  46. if (!TryComp<StationEventComponent>(uid, out var stationEvent))
  47. return;
  48. AdminLogManager.Add(LogType.EventStarted, LogImpact.High, $"Event started: {ToPrettyString(uid)}");
  49. if (stationEvent.Duration != null)
  50. {
  51. var duration = stationEvent.MaxDuration == null
  52. ? stationEvent.Duration
  53. : TimeSpan.FromSeconds(RobustRandom.NextDouble(stationEvent.Duration.Value.TotalSeconds,
  54. stationEvent.MaxDuration.Value.TotalSeconds));
  55. stationEvent.EndTime = Timing.CurTime + duration;
  56. }
  57. }
  58. /// <inheritdoc/>
  59. protected override void Ended(EntityUid uid, T component, GameRuleComponent gameRule, GameRuleEndedEvent args)
  60. {
  61. base.Ended(uid, component, gameRule, args);
  62. if (!TryComp<StationEventComponent>(uid, out var stationEvent))
  63. return;
  64. AdminLogManager.Add(LogType.EventStopped, $"Event ended: {ToPrettyString(uid)}");
  65. // we don't want to send to players who aren't in game (i.e. in the lobby)
  66. Filter allPlayersInGame = Filter.Empty().AddWhere(GameTicker.UserHasJoinedGame);
  67. if (stationEvent.EndAnnouncement != null)
  68. ChatSystem.DispatchFilteredAnnouncement(allPlayersInGame, Loc.GetString(stationEvent.EndAnnouncement), playSound: false, colorOverride: stationEvent.EndAnnouncementColor);
  69. Audio.PlayGlobal(stationEvent.EndAudio, allPlayersInGame, true);
  70. }
  71. /// <summary>
  72. /// Called every tick when this event is running.
  73. /// Events are responsible for their own lifetime, so this handles starting and ending after time.
  74. /// </summary>
  75. /// <inheritdoc/>
  76. public override void Update(float frameTime)
  77. {
  78. base.Update(frameTime);
  79. var query = EntityQueryEnumerator<StationEventComponent, GameRuleComponent>();
  80. while (query.MoveNext(out var uid, out var stationEvent, out var ruleData))
  81. {
  82. if (!GameTicker.IsGameRuleAdded(uid, ruleData))
  83. continue;
  84. if (!GameTicker.IsGameRuleActive(uid, ruleData) && !HasComp<DelayedStartRuleComponent>(uid))
  85. {
  86. GameTicker.StartGameRule(uid, ruleData);
  87. }
  88. else if (stationEvent.EndTime != null && Timing.CurTime >= stationEvent.EndTime && GameTicker.IsGameRuleActive(uid, ruleData))
  89. {
  90. GameTicker.EndGameRule(uid, ruleData);
  91. }
  92. }
  93. }
  94. }