using System.Linq; using System.Text.Json.Nodes; using Content.Shared.CCVar; using Content.Shared.GameTicking; using Robust.Server.ServerStatus; using Robust.Shared.Collections; using Robust.Shared.Configuration; namespace Content.Server.GameTicking { public sealed partial class GameTicker { /// /// Used for thread safety, given is called from another thread. /// private readonly object _statusShellLock = new(); /// /// Round start time in UTC, for status shell purposes. /// [ViewVariables] private DateTime _roundStartDateTime; /// /// For access to CVars in status responses. /// [Dependency] private readonly IConfigurationManager _cfg = default!; /// /// For access to the round ID in status responses. /// [Dependency] private readonly SharedGameTicker _gameTicker = default!; private void InitializeStatusShell() { IoCManager.Resolve().OnStatusRequest += GetStatusResponse; } private void GetStatusResponse(JsonNode jObject) { var preset = CurrentPreset ?? Preset; var _parsedPlayerList = new ValueList(); foreach (var player in _playerManager.Sessions) { _parsedPlayerList.Add(JsonValue.Create(player.Name.ToString())!); } // This method is raised from another thread, so this better be thread safe! lock (_statusShellLock) { jObject["name"] = _baseServer.ServerName; jObject["map"] = _gameMapManager.GetSelectedMap()?.MapName; jObject["round_id"] = _gameTicker.RoundId; jObject["players"] = _cfg.GetCVar(CCVars.AdminsCountInReportedPlayerCount) ? _playerManager.PlayerCount : _playerManager.PlayerCount - _adminManager.ActiveAdmins.Count(); jObject["soft_max_players"] = _cfg.GetCVar(CCVars.SoftMaxPlayers); jObject["panic_bunker"] = _cfg.GetCVar(CCVars.PanicBunkerEnabled); jObject["run_level"] = (int)_runLevel; if (preset != null) jObject["preset"] = Loc.GetString(preset.ModeTitle); if (_runLevel >= GameRunLevel.InRound) { jObject["round_start_time"] = _roundStartDateTime.ToString("o"); } jObject["playerlist"] = new JsonArray(_parsedPlayerList.ToArray()); } } } }