Files
tbd-station-14/Content.Server/GameTicking/GameTicker.StatusShell.cs
Simon 14b8762bf3 Add round ID to status HTTP API (#21904)
* added round_id

* Update Content.Server/GameTicking/GameTicker.StatusShell.cs

Co-authored-by: 0x6273 <0x40@keemail.me>

---------

Co-authored-by: Moony <moony@hellomouse.net>
Co-authored-by: 0x6273 <0x40@keemail.me>
2023-11-26 13:54:43 -06:00

55 lines
1.9 KiB
C#

using System.Text.Json.Nodes;
using Content.Shared.CCVar;
using Content.Shared.GameTicking;
using Robust.Server.ServerStatus;
using Robust.Shared.Configuration;
namespace Content.Server.GameTicking
{
public sealed partial class GameTicker
{
/// <summary>
/// Used for thread safety, given <see cref="IStatusHost.OnStatusRequest"/> is called from another thread.
/// </summary>
private readonly object _statusShellLock = new();
/// <summary>
/// Round start time in UTC, for status shell purposes.
/// </summary>
[ViewVariables]
private DateTime _roundStartDateTime;
/// <summary>
/// For access to CVars in status responses.
/// </summary>
[Dependency] private readonly IConfigurationManager _cfg = default!;
/// <summary>
/// For access to the round ID in status responses.
/// </summary>
[Dependency] private readonly SharedGameTicker _gameTicker = default!;
private void InitializeStatusShell()
{
IoCManager.Resolve<IStatusHost>().OnStatusRequest += GetStatusResponse;
}
private void GetStatusResponse(JsonNode jObject)
{
// 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"] = _playerManager.PlayerCount;
jObject["soft_max_players"] = _cfg.GetCVar(CCVars.SoftMaxPlayers);
jObject["run_level"] = (int) _runLevel;
if (_runLevel >= GameRunLevel.InRound)
{
jObject["round_start_time"] = _roundStartDateTime.ToString("o");
}
}
}
}
}