Files
tbd-station-14/Content.Server/Afk/AfkManager.cs
Repo 0084121706 Fix aHelp relay to detect AFK Admins (#24482)
* Add AFK detection for aHelp relay and admin specific afk time.

* Correct query to new refactor

* Change AFK timeout to 10min or else Pancake closes my PR 😭

* It wasnt a bug it was a feature, way less aHelps that way.

* aHelp Colors arn't real!

* Update Content.Shared/CCVar/CCVars.cs

---------

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
2024-02-02 00:03:49 +11:00

96 lines
3.3 KiB
C#

using Content.Server.Administration.Managers;
using Content.Shared.CCVar;
using JetBrains.Annotations;
using Robust.Server.Player;
using Robust.Shared.Configuration;
using Robust.Shared.Console;
using Robust.Shared.Enums;
using Robust.Shared.Player;
using Robust.Shared.Timing;
namespace Content.Server.Afk
{
/// <summary>
/// Tracks AFK (away from keyboard) status for players.
/// </summary>
/// <seealso cref="CCVars.AfkTime"/>
public interface IAfkManager
{
/// <summary>
/// Check whether this player is currently AFK.
/// </summary>
/// <param name="player">The player to check.</param>
/// <returns>True if the player is AFK, false otherwise.</returns>
bool IsAfk(ICommonSession player);
/// <summary>
/// Resets AFK status for the player as if they just did an action and are definitely not AFK.
/// </summary>
/// <param name="player">The player to set AFK status for.</param>
void PlayerDidAction(ICommonSession player);
void Initialize();
}
[UsedImplicitly]
public sealed class AfkManager : IAfkManager
{
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly IConfigurationManager _cfg = default!;
[Dependency] private readonly IConsoleHost _consoleHost = default!;
[Dependency] private readonly IAdminManager _adminManager = default!;
private readonly Dictionary<ICommonSession, TimeSpan> _lastActionTimes = new();
public void Initialize()
{
// Connecting, console commands and input commands all reset AFK status.
_playerManager.PlayerStatusChanged += PlayerStatusChanged;
_consoleHost.AnyCommandExecuted += ConsoleHostOnAnyCommandExecuted;
}
public void PlayerDidAction(ICommonSession player)
{
if (player.Status == SessionStatus.Disconnected)
// Make sure we don't re-add to the dictionary if the player is disconnected now.
return;
_lastActionTimes[player] = _gameTiming.RealTime;
}
public bool IsAfk(ICommonSession player)
{
if (!_lastActionTimes.TryGetValue(player, out var time))
{
// Some weird edge case like disconnected clients. Just say true I guess.
return true;
}
var timeOut = _adminManager.IsAdmin(player)
? TimeSpan.FromSeconds(_cfg.GetCVar(CCVars.AdminAfkTime))
: TimeSpan.FromSeconds(_cfg.GetCVar(CCVars.AfkTime));
return _gameTiming.RealTime - time > timeOut;
}
private void PlayerStatusChanged(object? sender, SessionStatusEventArgs e)
{
if (e.NewStatus == SessionStatus.Disconnected)
{
_lastActionTimes.Remove(e.Session);
return;
}
PlayerDidAction(e.Session);
}
private void ConsoleHostOnAnyCommandExecuted(IConsoleShell shell, string commandname, string argstr, string[] args)
{
if (shell.Player is { } player)
PlayerDidAction(player);
}
}
}