2021-01-18 18:14:53 +08:00
#nullable enable
using System.Collections.Generic ;
2020-09-21 09:14:40 +10:00
using System.Threading ;
2020-08-14 06:52:17 +10:00
using Content.Server.GameObjects.Components.Power.ApcNetComponents ;
using JetBrains.Annotations ;
2021-02-11 01:13:03 -08:00
using Robust.Server.GameObjects ;
using Robust.Shared.GameObjects ;
2020-08-14 06:52:17 +10:00
using Robust.Shared.IoC ;
using Robust.Shared.Localization ;
2021-02-11 01:13:03 -08:00
using Robust.Shared.Random ;
2021-02-18 20:45:45 -08:00
using Timer = Robust . Shared . Timing . Timer ;
2020-08-14 06:52:17 +10:00
namespace Content.Server.StationEvents
{
[UsedImplicitly]
public sealed class PowerGridCheck : StationEvent
{
public override string Name = > "PowerGridCheck" ;
2021-01-18 18:14:53 +08:00
public override float Weight = > WeightNormal ;
public override int? MaxOccurrences = > 3 ;
public override string StartAnnouncement = > Loc . GetString (
2020-08-14 06:52:17 +10:00
"Abnormal activity detected in the station's powernet. As a precautionary measure, the station's power will be shut off for an indeterminate duration." ) ;
protected override string EndAnnouncement = > Loc . GetString (
"Power has been restored to the station. We apologize for the inconvenience." ) ;
2021-01-18 18:14:53 +08:00
public override string? StartAudio = > "/Audio/Announcements/power_off.ogg" ;
2020-08-14 06:52:17 +10:00
2021-01-18 18:14:53 +08:00
// If you need EndAudio it's down below. Not set here because we can't play it at the normal time without spamming sounds.
2020-11-21 14:02:00 +01:00
2021-01-18 18:14:53 +08:00
protected override float StartAfter = > 12.0f ;
2020-09-21 09:14:40 +10:00
2021-01-18 18:14:53 +08:00
private CancellationTokenSource ? _announceCancelToken ;
2020-09-03 21:21:16 -05:00
2020-11-27 11:00:49 +01:00
private readonly List < IEntity > _powered = new ( ) ;
2020-11-21 14:02:00 +01:00
2021-01-18 18:14:53 +08:00
public override void Announce ( )
2020-08-14 06:52:17 +10:00
{
2021-01-18 18:14:53 +08:00
base . Announce ( ) ;
EndAfter = IoCManager . Resolve < IRobustRandom > ( ) . Next ( 60 , 120 ) ;
}
2020-08-14 06:52:17 +10:00
2021-01-18 18:14:53 +08:00
public override void Startup ( )
{
2020-08-14 06:52:17 +10:00
var componentManager = IoCManager . Resolve < IComponentManager > ( ) ;
2020-11-21 14:02:00 +01:00
2021-02-04 00:20:48 +11:00
foreach ( var component in componentManager . EntityQuery < PowerReceiverComponent > ( true ) )
2020-08-14 06:52:17 +10:00
{
component . PowerDisabled = true ;
2020-09-03 21:21:16 -05:00
_powered . Add ( component . Owner ) ;
2020-08-14 06:52:17 +10:00
}
2021-01-18 18:14:53 +08:00
base . Startup ( ) ;
2020-08-14 06:52:17 +10:00
}
public override void Shutdown ( )
{
2020-09-03 21:21:16 -05:00
foreach ( var entity in _powered )
2020-08-14 06:52:17 +10:00
{
if ( entity . Deleted ) continue ;
2020-11-21 14:02:00 +01:00
2021-01-18 18:14:53 +08:00
if ( entity . TryGetComponent ( out PowerReceiverComponent ? powerReceiverComponent ) )
2020-08-14 06:52:17 +10:00
{
2020-09-03 21:21:16 -05:00
powerReceiverComponent . PowerDisabled = false ;
2020-08-14 06:52:17 +10:00
}
}
2020-11-21 14:02:00 +01:00
2021-01-18 18:14:53 +08:00
// Can't use the default EndAudio
2020-09-21 09:14:40 +10:00
_announceCancelToken ? . Cancel ( ) ;
_announceCancelToken = new CancellationTokenSource ( ) ;
Timer . Spawn ( 3000 , ( ) = >
{
EntitySystem . Get < AudioSystem > ( ) . PlayGlobal ( "/Audio/Announcements/power_on.ogg" ) ;
} , _announceCancelToken . Token ) ;
2020-08-14 06:52:17 +10:00
_powered . Clear ( ) ;
2021-01-18 18:14:53 +08:00
base . Shutdown ( ) ;
2020-08-14 06:52:17 +10:00
}
}
2020-09-03 21:21:16 -05:00
}