Files
OldThink/Content.Server/GameTicking/Rules/DeathMatchRuleSystem.cs

131 lines
4.3 KiB
C#
Raw Normal View History

2021-12-21 21:23:29 +01:00
using Content.Server.Chat.Managers;
2023-04-25 20:23:14 -04:00
using Content.Server.GameTicking.Rules.Components;
2021-12-21 21:23:29 +01:00
using Content.Shared.CCVar;
using Content.Shared.Damage;
using Content.Shared.Mobs.Components;
using Content.Shared.Mobs.Systems;
2021-12-21 21:23:29 +01:00
using Robust.Server.Player;
using Robust.Shared.Configuration;
using Robust.Shared.Enums;
namespace Content.Server.GameTicking.Rules;
/// <summary>
2023-04-25 20:23:14 -04:00
/// Manages <see cref="DeathMatchRuleComponent"/>
2021-12-21 21:23:29 +01:00
/// </summary>
2023-04-25 20:23:14 -04:00
public sealed class DeathMatchRuleSystem : GameRuleSystem<DeathMatchRuleComponent>
2021-12-21 21:23:29 +01:00
{
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly IChatManager _chatManager = default!;
[Dependency] private readonly MobStateSystem _mobStateSystem = default!;
2021-12-21 21:23:29 +01:00
[Dependency] private readonly IConfigurationManager _cfg = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<DamageChangedEvent>(OnHealthChanged);
2023-04-25 20:23:14 -04:00
_playerManager.PlayerStatusChanged += OnPlayerStatusChanged;
}
public override void Shutdown()
{
base.Shutdown();
_playerManager.PlayerStatusChanged -= OnPlayerStatusChanged;
2021-12-21 21:23:29 +01:00
}
2023-04-25 20:23:14 -04:00
protected override void Started(EntityUid uid, DeathMatchRuleComponent component, GameRuleComponent gameRule, GameRuleStartedEvent args)
2021-12-21 21:23:29 +01:00
{
_chatManager.DispatchServerAnnouncement(Loc.GetString("rule-death-match-added-announcement"));
}
2023-04-25 20:23:14 -04:00
protected override void Ended(EntityUid uid, DeathMatchRuleComponent component, GameRuleComponent gameRule, GameRuleEndedEvent args)
2021-12-21 21:23:29 +01:00
{
2023-04-25 20:23:14 -04:00
base.Ended(uid, component, gameRule, args);
component.DeadCheckTimer = null;
component.RestartTimer = null;
2021-12-21 21:23:29 +01:00
}
private void OnHealthChanged(DamageChangedEvent _)
{
RunDelayedCheck();
}
2023-04-25 20:23:14 -04:00
private void OnPlayerStatusChanged(object? ojb, SessionStatusEventArgs e)
2021-12-21 21:23:29 +01:00
{
if (e.NewStatus == SessionStatus.Disconnected)
{
RunDelayedCheck();
}
}
private void RunDelayedCheck()
{
2023-04-25 20:23:14 -04:00
var query = EntityQueryEnumerator<DeathMatchRuleComponent, GameRuleComponent>();
while (query.MoveNext(out var uid, out var deathMatch, out var gameRule))
{
if (!GameTicker.IsGameRuleActive(uid, gameRule) || deathMatch.DeadCheckTimer != null)
continue;
2021-12-21 21:23:29 +01:00
2023-04-25 20:23:14 -04:00
deathMatch.DeadCheckTimer = deathMatch.DeadCheckDelay;
}
2021-12-21 21:23:29 +01:00
}
2023-04-25 20:23:14 -04:00
protected override void ActiveTick(EntityUid uid, DeathMatchRuleComponent component, GameRuleComponent gameRule, float frameTime)
2021-12-21 21:23:29 +01:00
{
2023-04-25 20:23:14 -04:00
base.ActiveTick(uid, component, gameRule, frameTime);
2021-12-21 21:23:29 +01:00
// 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...
2023-04-25 20:23:14 -04:00
if (component.RestartTimer != null)
2021-12-21 21:23:29 +01:00
{
2023-04-25 20:23:14 -04:00
component.RestartTimer -= frameTime;
2021-12-21 21:23:29 +01:00
2023-04-25 20:23:14 -04:00
if (component.RestartTimer > 0f)
2021-12-21 21:23:29 +01:00
return;
GameTicker.EndRound();
GameTicker.RestartRound();
return;
}
2023-04-25 20:23:14 -04:00
if (!_cfg.GetCVar(CCVars.GameLobbyEnableWin) || component.DeadCheckTimer == null)
2021-12-21 21:23:29 +01:00
return;
2023-04-25 20:23:14 -04:00
component.DeadCheckTimer -= frameTime;
2021-12-21 21:23:29 +01:00
2023-04-25 20:23:14 -04:00
if (component.DeadCheckTimer > 0)
2021-12-21 21:23:29 +01:00
return;
2023-04-25 20:23:14 -04:00
component.DeadCheckTimer = null;
2021-12-21 21:23:29 +01:00
IPlayerSession? winner = null;
foreach (var playerSession in _playerManager.ServerSessions)
{
2023-04-25 20:23:14 -04:00
if (playerSession.AttachedEntity is not { Valid: true } playerEntity
2021-12-21 21:23:29 +01:00
|| !TryComp(playerEntity, out MobStateComponent? state))
continue;
if (!_mobStateSystem.IsAlive(playerEntity, state))
2021-12-21 21:23:29 +01:00
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")
2023-04-25 20:23:14 -04:00
: Loc.GetString("rule-death-match-check-winner", ("winner", winner)));
2021-12-21 21:23:29 +01:00
2023-04-25 20:23:14 -04:00
_chatManager.DispatchServerAnnouncement(Loc.GetString("rule-restarting-in-seconds",
("seconds", component.RestartDelay)));
component.RestartTimer = component.RestartDelay;
2021-12-21 21:23:29 +01:00
}
}