GameTicker.StatusShell.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System.Linq;
  2. using System.Text.Json.Nodes;
  3. using Content.Shared.CCVar;
  4. using Content.Shared.GameTicking;
  5. using Robust.Server.ServerStatus;
  6. using Robust.Shared.Configuration;
  7. namespace Content.Server.GameTicking
  8. {
  9. public sealed partial class GameTicker
  10. {
  11. /// <summary>
  12. /// Used for thread safety, given <see cref="IStatusHost.OnStatusRequest"/> is called from another thread.
  13. /// </summary>
  14. private readonly object _statusShellLock = new();
  15. /// <summary>
  16. /// Round start time in UTC, for status shell purposes.
  17. /// </summary>
  18. [ViewVariables]
  19. private DateTime _roundStartDateTime;
  20. /// <summary>
  21. /// For access to CVars in status responses.
  22. /// </summary>
  23. [Dependency] private readonly IConfigurationManager _cfg = default!;
  24. /// <summary>
  25. /// For access to the round ID in status responses.
  26. /// </summary>
  27. [Dependency] private readonly SharedGameTicker _gameTicker = default!;
  28. private void InitializeStatusShell()
  29. {
  30. IoCManager.Resolve<IStatusHost>().OnStatusRequest += GetStatusResponse;
  31. }
  32. private void GetStatusResponse(JsonNode jObject)
  33. {
  34. var preset = CurrentPreset ?? Preset;
  35. // This method is raised from another thread, so this better be thread safe!
  36. lock (_statusShellLock)
  37. {
  38. jObject["name"] = _baseServer.ServerName;
  39. jObject["map"] = _gameMapManager.GetSelectedMap()?.MapName;
  40. jObject["round_id"] = _gameTicker.RoundId;
  41. jObject["players"] = _cfg.GetCVar(CCVars.AdminsCountInReportedPlayerCount)
  42. ? _playerManager.PlayerCount
  43. : _playerManager.PlayerCount - _adminManager.ActiveAdmins.Count();
  44. jObject["soft_max_players"] = _cfg.GetCVar(CCVars.SoftMaxPlayers);
  45. jObject["panic_bunker"] = _cfg.GetCVar(CCVars.PanicBunkerEnabled);
  46. jObject["run_level"] = (int) _runLevel;
  47. if (preset != null)
  48. jObject["preset"] = Loc.GetString(preset.ModeTitle);
  49. if (_runLevel >= GameRunLevel.InRound)
  50. {
  51. jObject["round_start_time"] = _roundStartDateTime.ToString("o");
  52. }
  53. }
  54. }
  55. }
  56. }