2021-06-21 02:13:54 +02:00
|
|
|
using System;
|
2019-05-08 09:55:36 +02:00
|
|
|
using System.Threading;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Server.Chat.Managers;
|
2020-11-07 01:15:56 +01:00
|
|
|
using Content.Shared;
|
2021-06-13 14:52:40 +02:00
|
|
|
using Content.Shared.CCVar;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Shared.Damage;
|
|
|
|
|
using Content.Shared.MobState;
|
2019-05-08 09:55:36 +02:00
|
|
|
using Robust.Server.Player;
|
2021-02-11 01:13:03 -08:00
|
|
|
using Robust.Shared.Configuration;
|
2019-05-08 09:55:36 +02:00
|
|
|
using Robust.Shared.Enums;
|
|
|
|
|
using Robust.Shared.GameObjects;
|
|
|
|
|
using Robust.Shared.IoC;
|
2020-09-13 14:23:52 +02:00
|
|
|
using Robust.Shared.Localization;
|
2021-02-18 20:45:45 -08:00
|
|
|
using Timer = Robust.Shared.Timing.Timer;
|
2019-05-08 09:55:36 +02:00
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Server.GameTicking.Rules
|
2019-05-08 09:55:00 +02:00
|
|
|
{
|
2019-05-08 09:55:36 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Simple GameRule that will do a free-for-all death match.
|
|
|
|
|
/// Kill everybody else to win.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public sealed class RuleDeathMatch : GameRule, IEntityEventSubscriber
|
2019-05-08 09:55:00 +02:00
|
|
|
{
|
2019-05-08 09:55:36 +02:00
|
|
|
private static readonly TimeSpan DeadCheckDelay = TimeSpan.FromSeconds(5);
|
|
|
|
|
|
2020-08-24 14:10:28 +02:00
|
|
|
[Dependency] private readonly IPlayerManager _playerManager = default!;
|
|
|
|
|
[Dependency] private readonly IEntityManager _entityManager = default!;
|
|
|
|
|
[Dependency] private readonly IChatManager _chatManager = default!;
|
2020-08-25 16:10:54 +02:00
|
|
|
[Dependency] private readonly IConfigurationManager _cfg = default!;
|
2019-05-08 09:55:36 +02:00
|
|
|
|
2021-03-16 15:50:20 +01:00
|
|
|
private CancellationTokenSource? _checkTimerCancel;
|
2019-05-08 09:55:36 +02:00
|
|
|
|
|
|
|
|
public override void Added()
|
|
|
|
|
{
|
2021-06-21 02:13:54 +02:00
|
|
|
_chatManager.DispatchServerAnnouncement(Loc.GetString("rule-death-match-added-announcement"));
|
2019-05-08 09:55:36 +02:00
|
|
|
|
2021-09-15 03:07:37 +10:00
|
|
|
_entityManager.EventBus.SubscribeEvent<DamageChangedEvent>(EventSource.Local, this, OnHealthChanged);
|
2019-05-08 09:55:36 +02:00
|
|
|
_playerManager.PlayerStatusChanged += PlayerManagerOnPlayerStatusChanged;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Removed()
|
|
|
|
|
{
|
|
|
|
|
base.Removed();
|
|
|
|
|
|
2021-09-15 03:07:37 +10:00
|
|
|
_entityManager.EventBus.UnsubscribeEvent<DamageChangedEvent>(EventSource.Local, this);
|
2019-05-08 09:55:36 +02:00
|
|
|
_playerManager.PlayerStatusChanged -= PlayerManagerOnPlayerStatusChanged;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-15 03:07:37 +10:00
|
|
|
private void OnHealthChanged(DamageChangedEvent _)
|
2019-05-08 09:55:36 +02:00
|
|
|
{
|
|
|
|
|
_runDelayedCheck();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void _checkForWinner()
|
|
|
|
|
{
|
|
|
|
|
_checkTimerCancel = null;
|
|
|
|
|
|
2020-11-07 01:15:56 +01:00
|
|
|
if (!_cfg.GetCVar(CCVars.GameLobbyEnableWin))
|
2020-08-25 16:10:54 +02:00
|
|
|
return;
|
|
|
|
|
|
2021-03-16 15:50:20 +01:00
|
|
|
IPlayerSession? winner = null;
|
2019-05-08 09:55:36 +02:00
|
|
|
foreach (var playerSession in _playerManager.GetAllPlayers())
|
|
|
|
|
{
|
2020-12-07 14:52:55 +01:00
|
|
|
var playerEntity = playerSession.AttachedEntity;
|
|
|
|
|
if (playerEntity == null
|
2021-03-16 15:50:20 +01:00
|
|
|
|| !playerEntity.TryGetComponent(out IMobStateComponent? state))
|
2019-05-08 09:55:36 +02:00
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-07 14:52:55 +01:00
|
|
|
if (!state.IsAlive())
|
2019-05-08 09:55:36 +02:00
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (winner != null)
|
|
|
|
|
{
|
|
|
|
|
// Found a second person alive, nothing decided yet!
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
winner = playerSession;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-13 14:23:52 +02:00
|
|
|
_chatManager.DispatchServerAnnouncement(winner == null
|
2021-06-21 02:13:54 +02:00
|
|
|
? Loc.GetString("rule-death-match-check-winner-stalemate")
|
|
|
|
|
: Loc.GetString("rule-death-match-check-winner",("winner", winner)));
|
2020-09-13 14:23:52 +02:00
|
|
|
|
|
|
|
|
var restartDelay = 10;
|
2019-05-08 09:55:36 +02:00
|
|
|
|
2021-06-21 02:13:54 +02:00
|
|
|
_chatManager.DispatchServerAnnouncement(Loc.GetString("rule-restarting-in-seconds", ("seconds", restartDelay)));
|
2019-05-08 09:55:36 +02:00
|
|
|
|
2021-06-20 10:09:24 +02:00
|
|
|
Timer.Spawn(TimeSpan.FromSeconds(restartDelay), () => EntitySystem.Get<GameTicker>().RestartRound());
|
2019-05-08 09:55:36 +02:00
|
|
|
}
|
|
|
|
|
|
2021-03-16 15:50:20 +01:00
|
|
|
private void PlayerManagerOnPlayerStatusChanged(object? sender, SessionStatusEventArgs e)
|
2019-05-08 09:55:36 +02:00
|
|
|
{
|
|
|
|
|
if (e.NewStatus == SessionStatus.Disconnected)
|
|
|
|
|
{
|
|
|
|
|
_runDelayedCheck();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void _runDelayedCheck()
|
|
|
|
|
{
|
|
|
|
|
_checkTimerCancel?.Cancel();
|
|
|
|
|
_checkTimerCancel = new CancellationTokenSource();
|
|
|
|
|
|
|
|
|
|
Timer.Spawn(DeadCheckDelay, _checkForWinner, _checkTimerCancel.Token);
|
|
|
|
|
}
|
2019-05-08 09:55:00 +02:00
|
|
|
}
|
|
|
|
|
}
|