Files
tbd-station-14/Content.Server/GameTicking/Rules/DeathMatchRuleSystem.cs
Kara b9a0894d7c Event refactor (#9589)
* Station event refactor

* Remove clientside `IStationEventManager`

we can just use prototypes

* Basic API idea

* Cruft

* first attempt at epicness

* okay yeah this shit is really clean

* sort out minor stuff

* Convert `BreakerFlip`

* `BureaucraticError` + general cleanup

* `DiseaseOutbreak`

* `FalseAlarm`

* `GasLeak`

* `KudzuGrowth`

* `MeteorSwarm`

* `MouseMigration`

* misc errors

* `PowerGridCheck`

* `RandomSentience`

* `VentClog`

* `VentCritters`

* `ZombieOutbreak`

* Rewrite basic event scheduler

* Minor fixes and logging

* ooooops

* errors + fix

* linter

* completions, `RuleStarted` property, update loop fixes

* Tweaks

* Fix #9462

* Basic scheduler update fix, and fixes #8174

* Add test

* UI cleanup

* really this was just for testing
2022-07-10 20:48:41 -05:00

127 lines
3.6 KiB
C#

using Content.Server.Chat.Managers;
using Content.Server.GameTicking.Rules.Configurations;
using Content.Shared.CCVar;
using Content.Shared.Damage;
using Content.Shared.MobState.Components;
using Robust.Server.Player;
using Robust.Shared.Configuration;
using Robust.Shared.Enums;
namespace Content.Server.GameTicking.Rules;
/// <summary>
/// Simple GameRule that will do a free-for-all death match.
/// Kill everybody else to win.
/// </summary>
public sealed class DeathMatchRuleSystem : GameRuleSystem
{
public override string Prototype => "DeathMatch";
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly IChatManager _chatManager = default!;
[Dependency] private readonly IConfigurationManager _cfg = default!;
private const float RestartDelay = 10f;
private const float DeadCheckDelay = 5f;
private float? _deadCheckTimer = null;
private float? _restartTimer = null;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<DamageChangedEvent>(OnHealthChanged);
}
public override void Started()
{
_chatManager.DispatchServerAnnouncement(Loc.GetString("rule-death-match-added-announcement"));
_playerManager.PlayerStatusChanged += OnPlayerStatusChanged;
}
public override void Ended()
{
_deadCheckTimer = null;
_restartTimer = null;
_playerManager.PlayerStatusChanged -= OnPlayerStatusChanged;
}
private void OnHealthChanged(DamageChangedEvent _)
{
RunDelayedCheck();
}
private void OnPlayerStatusChanged(object? _, SessionStatusEventArgs e)
{
if (e.NewStatus == SessionStatus.Disconnected)
{
RunDelayedCheck();
}
}
private void RunDelayedCheck()
{
if (!RuleAdded || _deadCheckTimer != null)
return;
_deadCheckTimer = DeadCheckDelay;
}
public override void Update(float frameTime)
{
if (!RuleAdded)
return;
// If the restart timer is active, that means the round is ending soon, no need to check for winners.
// TODO: We probably want a sane, centralized round end thingie in GameTicker, RoundEndSystem is no good...
if (_restartTimer != null)
{
_restartTimer -= frameTime;
if (_restartTimer > 0f)
return;
GameTicker.EndRound();
GameTicker.RestartRound();
return;
}
if (!_cfg.GetCVar(CCVars.GameLobbyEnableWin) || _deadCheckTimer == null)
return;
_deadCheckTimer -= frameTime;
if (_deadCheckTimer > 0)
return;
_deadCheckTimer = null;
IPlayerSession? winner = null;
foreach (var playerSession in _playerManager.ServerSessions)
{
if (playerSession.AttachedEntity is not {Valid: true} playerEntity
|| !TryComp(playerEntity, out MobStateComponent? state))
continue;
if (!state.IsAlive())
continue;
// Found a second person alive, nothing decided yet!
if (winner != null)
return;
winner = playerSession;
}
_chatManager.DispatchServerAnnouncement(winner == null
? Loc.GetString("rule-death-match-check-winner-stalemate")
: Loc.GetString("rule-death-match-check-winner",("winner", winner)));
_chatManager.DispatchServerAnnouncement(Loc.GetString("rule-restarting-in-seconds", ("seconds", RestartDelay)));
_restartTimer = RestartDelay;
}
}