2020-04-09 00:28:56 +02:00
|
|
|
using System.Threading;
|
2021-11-22 21:31:50 +01:00
|
|
|
using Content.Server.Administration.Logs;
|
2022-06-26 15:20:45 +10:00
|
|
|
using Content.Server.AlertLevel;
|
2022-11-09 20:20:36 -08:00
|
|
|
using Content.Shared.CCVar;
|
2022-06-03 21:37:35 +10:00
|
|
|
using Content.Server.Chat;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Server.Chat.Managers;
|
2022-06-23 20:11:03 +10:00
|
|
|
using Content.Server.Chat.Systems;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Server.GameTicking;
|
2022-06-26 15:20:45 +10:00
|
|
|
using Content.Server.Shuttles.Systems;
|
|
|
|
|
using Content.Server.Station.Systems;
|
2021-11-28 14:56:53 +01:00
|
|
|
using Content.Shared.Database;
|
2020-11-26 17:07:46 +00:00
|
|
|
using Content.Shared.GameTicking;
|
2021-03-01 20:42:54 -08:00
|
|
|
using Robust.Shared.Audio;
|
2022-11-09 20:20:36 -08:00
|
|
|
using Robust.Shared.Configuration;
|
2021-03-01 20:42:54 -08:00
|
|
|
using Robust.Shared.Player;
|
2022-06-26 15:20:45 +10:00
|
|
|
using Robust.Shared.Prototypes;
|
2021-02-11 01:13:03 -08:00
|
|
|
using Robust.Shared.Timing;
|
2021-02-18 20:45:45 -08:00
|
|
|
using Timer = Robust.Shared.Timing.Timer;
|
2020-04-09 00:28:56 +02:00
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Server.RoundEnd
|
2020-04-09 00:28:56 +02:00
|
|
|
{
|
2022-06-26 15:20:45 +10:00
|
|
|
/// <summary>
|
|
|
|
|
/// Handles ending rounds normally and also via requesting it (e.g. via comms console)
|
|
|
|
|
/// If you request a round end then an escape shuttle will be used.
|
|
|
|
|
/// </summary>
|
2022-02-16 00:23:23 -07:00
|
|
|
public sealed class RoundEndSystem : EntitySystem
|
2020-04-09 00:28:56 +02:00
|
|
|
{
|
2022-06-26 15:20:45 +10:00
|
|
|
[Dependency] private readonly IAdminLogManager _adminLogger = default!;
|
2022-11-09 20:20:36 -08:00
|
|
|
[Dependency] private readonly IConfigurationManager _cfg = default!;
|
2020-09-23 11:53:31 +02:00
|
|
|
[Dependency] private readonly IChatManager _chatManager = default!;
|
2022-06-26 15:20:45 +10:00
|
|
|
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
|
|
|
|
[Dependency] private readonly IPrototypeManager _protoManager = default!;
|
2022-06-03 21:37:35 +10:00
|
|
|
[Dependency] private readonly ChatSystem _chatSystem = default!;
|
2022-01-10 11:24:41 -08:00
|
|
|
[Dependency] private readonly GameTicker _gameTicker = default!;
|
2023-03-24 12:54:41 +11:00
|
|
|
[Dependency] private readonly EmergencyShuttleSystem _shuttle = default!;
|
2022-06-26 15:20:45 +10:00
|
|
|
[Dependency] private readonly StationSystem _stationSystem = default!;
|
2020-04-09 00:28:56 +02:00
|
|
|
|
2022-01-10 11:24:41 -08:00
|
|
|
public TimeSpan DefaultCooldownDuration { get; set; } = TimeSpan.FromSeconds(30);
|
2022-06-26 15:20:45 +10:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Countdown to use where there is no station alert countdown to be found.
|
|
|
|
|
/// </summary>
|
2023-02-06 06:49:15 -06:00
|
|
|
public TimeSpan DefaultCountdownDuration { get; set; } = TimeSpan.FromMinutes(10);
|
2020-04-09 00:28:56 +02:00
|
|
|
|
2022-01-10 11:24:41 -08:00
|
|
|
private CancellationTokenSource? _countdownTokenSource = null;
|
|
|
|
|
private CancellationTokenSource? _cooldownTokenSource = null;
|
2022-07-01 13:40:36 -07:00
|
|
|
public TimeSpan? LastCountdownStart { get; set; } = null;
|
2022-01-10 11:24:41 -08:00
|
|
|
public TimeSpan? ExpectedCountdownEnd { get; set; } = null;
|
2022-07-01 13:40:36 -07:00
|
|
|
public TimeSpan? ExpectedShuttleLength => ExpectedCountdownEnd - LastCountdownStart;
|
|
|
|
|
public TimeSpan? ShuttleTimeLeft => ExpectedCountdownEnd - _gameTiming.CurTime;
|
2021-02-16 09:31:57 +01:00
|
|
|
|
2022-11-09 20:20:36 -08:00
|
|
|
public TimeSpan AutoCallStartTime;
|
|
|
|
|
private bool AutoCalledBefore = false;
|
|
|
|
|
|
2021-06-29 15:56:07 +02:00
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
|
|
|
|
base.Initialize();
|
2022-01-10 11:24:41 -08:00
|
|
|
SubscribeLocalEvent<RoundRestartCleanupEvent>(_ => Reset());
|
2022-11-09 20:20:36 -08:00
|
|
|
SetAutoCallTime();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetAutoCallTime()
|
|
|
|
|
{
|
|
|
|
|
AutoCallStartTime = _gameTiming.CurTime;
|
2021-06-29 15:56:07 +02:00
|
|
|
}
|
|
|
|
|
|
2022-01-10 11:24:41 -08:00
|
|
|
private void Reset()
|
2020-11-26 17:07:46 +00:00
|
|
|
{
|
2022-01-10 11:24:41 -08:00
|
|
|
if (_countdownTokenSource != null)
|
|
|
|
|
{
|
|
|
|
|
_countdownTokenSource.Cancel();
|
|
|
|
|
_countdownTokenSource = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_cooldownTokenSource != null)
|
|
|
|
|
{
|
|
|
|
|
_cooldownTokenSource.Cancel();
|
|
|
|
|
_cooldownTokenSource = null;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-01 13:40:36 -07:00
|
|
|
LastCountdownStart = null;
|
2020-11-26 17:07:46 +00:00
|
|
|
ExpectedCountdownEnd = null;
|
2022-11-09 20:20:36 -08:00
|
|
|
SetAutoCallTime();
|
|
|
|
|
AutoCalledBefore = false;
|
2022-01-10 11:24:41 -08:00
|
|
|
RaiseLocalEvent(RoundEndSystemChangedEvent.Default);
|
2021-02-16 09:31:57 +01:00
|
|
|
}
|
|
|
|
|
|
2022-07-01 13:40:36 -07:00
|
|
|
public bool CanCallOrRecall()
|
2021-02-16 09:31:57 +01:00
|
|
|
{
|
2022-01-10 11:24:41 -08:00
|
|
|
return _cooldownTokenSource == null;
|
2020-11-26 17:07:46 +00:00
|
|
|
}
|
|
|
|
|
|
2022-11-09 20:20:36 -08:00
|
|
|
public void RequestRoundEnd(EntityUid? requester = null, bool checkCooldown = true, bool autoCall = false)
|
2021-10-13 12:15:28 -05:00
|
|
|
{
|
2022-06-26 15:20:45 +10:00
|
|
|
var duration = DefaultCountdownDuration;
|
|
|
|
|
|
|
|
|
|
if (requester != null)
|
|
|
|
|
{
|
|
|
|
|
var stationUid = _stationSystem.GetOwningStation(requester.Value);
|
|
|
|
|
if (TryComp<AlertLevelComponent>(stationUid, out var alertLevel))
|
|
|
|
|
{
|
|
|
|
|
duration = _protoManager
|
|
|
|
|
.Index<AlertLevelPrototype>(AlertLevelSystem.DefaultAlertLevelSet)
|
|
|
|
|
.Levels[alertLevel.CurrentLevel].ShuttleTime;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-09 20:20:36 -08:00
|
|
|
RequestRoundEnd(duration, requester, checkCooldown, autoCall);
|
2021-10-13 12:15:28 -05:00
|
|
|
}
|
|
|
|
|
|
2022-11-09 20:20:36 -08:00
|
|
|
public void RequestRoundEnd(TimeSpan countdownTime, EntityUid? requester = null, bool checkCooldown = true, bool autoCall = false)
|
2020-04-09 00:28:56 +02:00
|
|
|
{
|
2022-01-10 11:24:41 -08:00
|
|
|
if (_gameTicker.RunLevel != GameRunLevel.InRound) return;
|
2020-04-09 00:28:56 +02:00
|
|
|
|
2022-01-10 11:24:41 -08:00
|
|
|
if (checkCooldown && _cooldownTokenSource != null) return;
|
|
|
|
|
|
|
|
|
|
if (_countdownTokenSource != null) return;
|
|
|
|
|
_countdownTokenSource = new();
|
2021-02-16 09:31:57 +01:00
|
|
|
|
2021-12-11 16:01:55 +01:00
|
|
|
if (requester != null)
|
2021-11-22 21:31:50 +01:00
|
|
|
{
|
2022-05-28 23:41:17 -07:00
|
|
|
_adminLogger.Add(LogType.ShuttleCalled, LogImpact.High, $"Shuttle called by {ToPrettyString(requester.Value):user}");
|
2021-11-22 21:31:50 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-05-28 23:41:17 -07:00
|
|
|
_adminLogger.Add(LogType.ShuttleCalled, LogImpact.High, $"Shuttle called");
|
2021-11-22 21:31:50 +01:00
|
|
|
}
|
|
|
|
|
|
2022-06-26 15:20:45 +10:00
|
|
|
// I originally had these set up here but somehow time gets passed as 0 to Loc so IDEK.
|
|
|
|
|
int time;
|
|
|
|
|
string units;
|
|
|
|
|
|
|
|
|
|
if (countdownTime.TotalSeconds < 60)
|
|
|
|
|
{
|
|
|
|
|
time = countdownTime.Seconds;
|
2022-06-29 06:13:36 +03:00
|
|
|
units = "eta-units-seconds";
|
2022-06-26 15:20:45 +10:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
time = countdownTime.Minutes;
|
2022-06-29 06:13:36 +03:00
|
|
|
units = "eta-units-minutes";
|
2022-06-26 15:20:45 +10:00
|
|
|
}
|
|
|
|
|
|
2022-11-09 20:20:36 -08:00
|
|
|
if (autoCall)
|
|
|
|
|
{
|
|
|
|
|
_chatSystem.DispatchGlobalAnnouncement(Loc.GetString("round-end-system-shuttle-auto-called-announcement",
|
|
|
|
|
("time", time),
|
|
|
|
|
("units", Loc.GetString(units))),
|
|
|
|
|
Loc.GetString("Station"),
|
|
|
|
|
false,
|
|
|
|
|
null,
|
|
|
|
|
Color.Gold);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_chatSystem.DispatchGlobalAnnouncement(Loc.GetString("round-end-system-shuttle-called-announcement",
|
|
|
|
|
("time", time),
|
|
|
|
|
("units", Loc.GetString(units))),
|
|
|
|
|
Loc.GetString("Station"),
|
|
|
|
|
false,
|
|
|
|
|
null,
|
|
|
|
|
Color.Gold);
|
|
|
|
|
}
|
2021-01-01 16:34:54 +01:00
|
|
|
|
2022-06-12 19:45:47 -04:00
|
|
|
SoundSystem.Play("/Audio/Announcements/shuttlecalled.ogg", Filter.Broadcast());
|
2021-01-01 16:34:54 +01:00
|
|
|
|
2022-07-01 13:40:36 -07:00
|
|
|
LastCountdownStart = _gameTiming.CurTime;
|
2021-10-13 12:15:28 -05:00
|
|
|
ExpectedCountdownEnd = _gameTiming.CurTime + countdownTime;
|
2022-06-26 15:20:45 +10:00
|
|
|
Timer.Spawn(countdownTime, _shuttle.CallEmergencyShuttle, _countdownTokenSource.Token);
|
2021-02-16 09:31:57 +01:00
|
|
|
|
|
|
|
|
ActivateCooldown();
|
2022-01-10 11:24:41 -08:00
|
|
|
RaiseLocalEvent(RoundEndSystemChangedEvent.Default);
|
2020-04-09 00:28:56 +02:00
|
|
|
}
|
|
|
|
|
|
2021-12-05 18:09:01 +01:00
|
|
|
public void CancelRoundEndCountdown(EntityUid? requester = null, bool checkCooldown = true)
|
2020-04-09 00:28:56 +02:00
|
|
|
{
|
2022-01-10 11:24:41 -08:00
|
|
|
if (_gameTicker.RunLevel != GameRunLevel.InRound) return;
|
|
|
|
|
if (checkCooldown && _cooldownTokenSource != null) return;
|
2020-04-09 01:43:28 +02:00
|
|
|
|
2022-01-10 11:24:41 -08:00
|
|
|
if (_countdownTokenSource == null) return;
|
|
|
|
|
_countdownTokenSource.Cancel();
|
|
|
|
|
_countdownTokenSource = null;
|
2021-02-16 09:31:57 +01:00
|
|
|
|
2021-12-11 16:01:55 +01:00
|
|
|
if (requester != null)
|
2021-11-22 21:31:50 +01:00
|
|
|
{
|
2022-05-28 23:41:17 -07:00
|
|
|
_adminLogger.Add(LogType.ShuttleRecalled, LogImpact.High, $"Shuttle recalled by {ToPrettyString(requester.Value):user}");
|
2021-11-22 21:31:50 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-05-28 23:41:17 -07:00
|
|
|
_adminLogger.Add(LogType.ShuttleRecalled, LogImpact.High, $"Shuttle recalled");
|
2021-11-22 21:31:50 +01:00
|
|
|
}
|
|
|
|
|
|
2022-07-04 16:00:51 +10:00
|
|
|
_chatSystem.DispatchGlobalAnnouncement(Loc.GetString("round-end-system-shuttle-recalled-announcement"),
|
2022-04-10 13:54:07 -07:00
|
|
|
Loc.GetString("Station"), false, colorOverride: Color.Gold);
|
2021-01-01 16:34:54 +01:00
|
|
|
|
2022-06-12 19:45:47 -04:00
|
|
|
SoundSystem.Play("/Audio/Announcements/shuttlerecalled.ogg", Filter.Broadcast());
|
2021-01-01 16:34:54 +01:00
|
|
|
|
2022-07-01 13:40:36 -07:00
|
|
|
LastCountdownStart = null;
|
2020-04-09 00:28:56 +02:00
|
|
|
ExpectedCountdownEnd = null;
|
2021-02-16 09:31:57 +01:00
|
|
|
ActivateCooldown();
|
2022-01-10 11:24:41 -08:00
|
|
|
RaiseLocalEvent(RoundEndSystemChangedEvent.Default);
|
2020-04-09 00:28:56 +02:00
|
|
|
}
|
|
|
|
|
|
2021-10-09 13:18:20 -05:00
|
|
|
public void EndRound()
|
2020-04-09 00:28:56 +02:00
|
|
|
{
|
2022-01-10 11:24:41 -08:00
|
|
|
if (_gameTicker.RunLevel != GameRunLevel.InRound) return;
|
2022-07-01 13:40:36 -07:00
|
|
|
LastCountdownStart = null;
|
2022-01-10 11:24:41 -08:00
|
|
|
ExpectedCountdownEnd = null;
|
|
|
|
|
RaiseLocalEvent(RoundEndSystemChangedEvent.Default);
|
|
|
|
|
_gameTicker.EndRound();
|
|
|
|
|
_countdownTokenSource?.Cancel();
|
|
|
|
|
_countdownTokenSource = new();
|
2023-08-16 03:36:50 +02:00
|
|
|
|
|
|
|
|
var countdownTime = TimeSpan.FromSeconds(_cfg.GetCVar(CCVars.RoundRestartTime));
|
|
|
|
|
int time;
|
|
|
|
|
string unitsLocString;
|
|
|
|
|
if (countdownTime.TotalSeconds < 60)
|
|
|
|
|
{
|
|
|
|
|
time = countdownTime.Seconds;
|
|
|
|
|
unitsLocString = "eta-units-seconds";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
time = countdownTime.Minutes;
|
|
|
|
|
unitsLocString = "eta-units-minutes";
|
|
|
|
|
}
|
|
|
|
|
_chatManager.DispatchServerAnnouncement(
|
|
|
|
|
Loc.GetString(
|
|
|
|
|
"round-end-system-round-restart-eta-announcement",
|
|
|
|
|
("time", time),
|
|
|
|
|
("units", Loc.GetString(unitsLocString))));
|
|
|
|
|
Timer.Spawn(countdownTime, AfterEndRoundRestart, _countdownTokenSource.Token);
|
2022-01-10 11:24:41 -08:00
|
|
|
}
|
2020-09-23 11:53:31 +02:00
|
|
|
|
2022-01-10 11:24:41 -08:00
|
|
|
private void AfterEndRoundRestart()
|
|
|
|
|
{
|
|
|
|
|
if (_gameTicker.RunLevel != GameRunLevel.PostRound) return;
|
|
|
|
|
Reset();
|
|
|
|
|
_gameTicker.RestartRound();
|
|
|
|
|
}
|
2020-09-23 11:53:31 +02:00
|
|
|
|
2022-01-10 11:24:41 -08:00
|
|
|
private void ActivateCooldown()
|
|
|
|
|
{
|
|
|
|
|
_cooldownTokenSource?.Cancel();
|
|
|
|
|
_cooldownTokenSource = new();
|
|
|
|
|
Timer.Spawn(DefaultCooldownDuration, () =>
|
|
|
|
|
{
|
|
|
|
|
_cooldownTokenSource.Cancel();
|
|
|
|
|
_cooldownTokenSource = null;
|
|
|
|
|
RaiseLocalEvent(RoundEndSystemChangedEvent.Default);
|
|
|
|
|
}, _cooldownTokenSource.Token);
|
2020-04-09 00:28:56 +02:00
|
|
|
}
|
2022-11-09 20:20:36 -08:00
|
|
|
|
|
|
|
|
public override void Update(float frameTime)
|
|
|
|
|
{
|
|
|
|
|
// Check if we should auto-call.
|
|
|
|
|
int mins = AutoCalledBefore ? _cfg.GetCVar(CCVars.EmergencyShuttleAutoCallExtensionTime)
|
|
|
|
|
: _cfg.GetCVar(CCVars.EmergencyShuttleAutoCallTime);
|
|
|
|
|
if (mins != 0 && _gameTiming.CurTime - AutoCallStartTime > TimeSpan.FromMinutes(mins))
|
|
|
|
|
{
|
|
|
|
|
if (!_shuttle.EmergencyShuttleArrived && ExpectedCountdownEnd is null)
|
|
|
|
|
{
|
|
|
|
|
RequestRoundEnd(null, false, true);
|
|
|
|
|
AutoCalledBefore = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Always reset auto-call in case of a recall.
|
|
|
|
|
SetAutoCallTime();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-09 00:28:56 +02:00
|
|
|
}
|
2022-01-10 11:24:41 -08:00
|
|
|
|
2022-02-16 00:23:23 -07:00
|
|
|
public sealed class RoundEndSystemChangedEvent : EntityEventArgs
|
2022-01-10 11:24:41 -08:00
|
|
|
{
|
|
|
|
|
public static RoundEndSystemChangedEvent Default { get; } = new();
|
|
|
|
|
}
|
2020-04-09 00:28:56 +02:00
|
|
|
}
|