From 00841217062ba60d38320dfd2b93288548bb4201 Mon Sep 17 00:00:00 2001 From: Repo <47093363+Titian3@users.noreply.github.com> Date: Fri, 2 Feb 2024 02:03:49 +1300 Subject: [PATCH] Fix aHelp relay to detect AFK Admins (#24482) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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> --- .../Administration/Systems/BwoinkSystem.cs | 24 +++++++++++++------ Content.Server/Afk/AfkManager.cs | 11 +++++++-- Content.Shared/CCVar/CCVars.cs | 6 +++++ 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/Content.Server/Administration/Systems/BwoinkSystem.cs b/Content.Server/Administration/Systems/BwoinkSystem.cs index 87c200e08b..5439b27f42 100644 --- a/Content.Server/Administration/Systems/BwoinkSystem.cs +++ b/Content.Server/Administration/Systems/BwoinkSystem.cs @@ -6,6 +6,7 @@ using System.Text.Json.Nodes; using System.Text.RegularExpressions; using System.Threading.Tasks; using Content.Server.Administration.Managers; +using Content.Server.Afk; using Content.Server.Discord; using Content.Server.GameTicking; using Content.Shared.Administration; @@ -33,6 +34,7 @@ namespace Content.Server.Administration.Systems [Dependency] private readonly IPlayerLocator _playerLocator = default!; [Dependency] private readonly GameTicker _gameTicker = default!; [Dependency] private readonly SharedMindSystem _minds = default!; + [Dependency] private readonly IAfkManager _afkManager = default!; private ISawmill _sawmill = default!; private readonly HttpClient _httpClient = new(); @@ -327,7 +329,7 @@ namespace Content.Server.Administration.Systems username += $" ({characterName})"; // If no admins are online, set embed color to red. Otherwise green - var color = GetTargetAdmins().Count > 0 ? 0x41F097 : 0xFF0000; + var color = GetNonAfkAdmins().Count > 0 ? 0x41F097 : 0xFF0000; // Limit server name to 1500 characters, in case someone tries to be a little funny var serverName = _serverName[..Math.Min(_serverName.Length, 1500)]; @@ -471,7 +473,8 @@ namespace Content.Server.Administration.Systems { str = str[..(DescriptionMax - _maxAdditionalChars - unameLength)]; } - _messageQueues[msg.UserId].Enqueue(GenerateAHelpMessage(senderSession.Name, str, !personalChannel, _gameTicker.RoundDuration().ToString("hh\\:mm\\:ss"), _gameTicker.RunLevel, admins.Count == 0)); + var nonAfkAdmins = GetNonAfkAdmins(); + _messageQueues[msg.UserId].Enqueue(GenerateAHelpMessage(senderSession.Name, str, !personalChannel, _gameTicker.RoundDuration().ToString("hh\\:mm\\:ss"), _gameTicker.RunLevel, nonAfkAdmins.Count == 0)); } if (admins.Count != 0 || sendsWebhook) @@ -483,19 +486,26 @@ namespace Content.Server.Administration.Systems RaiseNetworkEvent(starMuteMsg, senderSession.Channel); } - // Returns all online admins with AHelp access + private IList GetNonAfkAdmins() + { + return _adminManager.ActiveAdmins + .Where(p => (_adminManager.GetAdminData(p)?.HasFlag(AdminFlags.Adminhelp) ?? false) && !_afkManager.IsAfk(p)) + .Select(p => p.Channel) + .ToList(); + } + private IList GetTargetAdmins() { return _adminManager.ActiveAdmins - .Where(p => _adminManager.GetAdminData(p)?.HasFlag(AdminFlags.Adminhelp) ?? false) - .Select(p => p.Channel) - .ToList(); + .Where(p => _adminManager.GetAdminData(p)?.HasFlag(AdminFlags.Adminhelp) ?? false) + .Select(p => p.Channel) + .ToList(); } private static string GenerateAHelpMessage(string username, string message, bool admin, string roundTime, GameRunLevel roundState, bool noReceivers = false) { var stringbuilder = new StringBuilder(); - + if (admin) stringbuilder.Append(":outbox_tray:"); else if (noReceivers) diff --git a/Content.Server/Afk/AfkManager.cs b/Content.Server/Afk/AfkManager.cs index 8f70904317..96dcb475cd 100644 --- a/Content.Server/Afk/AfkManager.cs +++ b/Content.Server/Afk/AfkManager.cs @@ -1,4 +1,5 @@ -using Content.Shared.CCVar; +using Content.Server.Administration.Managers; +using Content.Shared.CCVar; using JetBrains.Annotations; using Robust.Server.Player; using Robust.Shared.Configuration; @@ -38,6 +39,7 @@ namespace Content.Server.Afk [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 _lastActionTimes = new(); @@ -61,10 +63,15 @@ namespace Content.Server.Afk 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)); - var timeOut = TimeSpan.FromSeconds(_cfg.GetCVar(CCVars.AfkTime)); return _gameTiming.RealTime - time > timeOut; } diff --git a/Content.Shared/CCVar/CCVars.cs b/Content.Shared/CCVar/CCVars.cs index f0b6c2f923..08c80abdc8 100644 --- a/Content.Shared/CCVar/CCVars.cs +++ b/Content.Shared/CCVar/CCVars.cs @@ -833,6 +833,12 @@ namespace Content.Shared.CCVar public static readonly CVarDef NewPlayerThreshold = CVarDef.Create("admin.new_player_threshold", 0, CVar.ARCHIVE | CVar.REPLICATED | CVar.SERVER); + /// + /// How long an admin client can go without any input before being considered AFK. + /// + public static readonly CVarDef AdminAfkTime = + CVarDef.Create("admin.afk_time", 600f, CVar.SERVERONLY); + /* * Explosions */