2020-09-13 14:23:52 +02:00
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 ;
using Robust.Server.GameObjects.EntitySystems ;
using Robust.Shared.GameObjects.Systems ;
using Robust.Shared.Interfaces.GameObjects ;
using Robust.Shared.Interfaces.Random ;
using Robust.Shared.IoC ;
using Robust.Shared.Localization ;
2020-09-21 09:14:40 +10:00
using Timer = Robust . Shared . Timers . Timer ;
2020-08-14 06:52:17 +10:00
namespace Content.Server.StationEvents
{
[UsedImplicitly]
public sealed class PowerGridCheck : StationEvent
{
public override string Name = > "PowerGridCheck" ;
public override StationEventWeight Weight = > StationEventWeight . Normal ;
2020-10-08 11:59:20 -04:00
public override int? MaxOccurrences = > 2 ;
2020-08-14 06:52:17 +10:00
protected override string StartAnnouncement = > Loc . GetString (
"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." ) ;
private float _elapsedTime ;
private int _failDuration ;
2020-11-21 14:02:00 +01:00
2020-09-21 09:14:40 +10:00
/// <summary>
/// So we don't overlap the announcement with power-down sounds we'll delay it a few seconds.
/// </summary>
private bool _announced ;
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
2020-08-14 06:52:17 +10:00
public override void Startup ( )
{
base . Startup ( ) ;
2020-09-21 09:14:40 +10:00
_announced = false ;
2020-08-14 06:52:17 +10:00
_elapsedTime = 0.0f ;
2020-10-08 11:59:20 -04:00
_failDuration = IoCManager . Resolve < IRobustRandom > ( ) . Next ( 60 , 120 ) ;
2020-08-14 06:52:17 +10:00
var componentManager = IoCManager . Resolve < IComponentManager > ( ) ;
2020-11-21 14:02:00 +01:00
2020-09-03 21:21:16 -05:00
foreach ( PowerReceiverComponent component in componentManager . EntityQuery < PowerReceiverComponent > ( ) )
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
}
}
public override void Shutdown ( )
{
base . 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
2020-08-14 06:52:17 +10:00
if ( entity . TryGetComponent ( out PowerReceiverComponent powerReceiverComponent ) )
{
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
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 ( ) ;
}
public override void Update ( float frameTime )
{
if ( ! Running )
{
return ;
}
2020-09-21 09:14:40 +10:00
if ( ! _announced & & _elapsedTime > 3.0f )
{
EntitySystem . Get < AudioSystem > ( ) . PlayGlobal ( "/Audio/Announcements/power_off.ogg" ) ;
_announced = true ;
}
2020-11-21 14:02:00 +01:00
2020-08-14 06:52:17 +10:00
_elapsedTime + = frameTime ;
if ( _elapsedTime < _failDuration )
{
return ;
}
Running = false ;
}
}
2020-09-03 21:21:16 -05:00
}