ClientGameTicker.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using Content.Client.Administration.Managers;
  2. using Content.Client.Gameplay;
  3. using Content.Client.Lobby;
  4. using Content.Client.RoundEnd;
  5. using Content.Shared.GameTicking;
  6. using Content.Shared.GameWindow;
  7. using Content.Shared.Roles;
  8. using JetBrains.Annotations;
  9. using Robust.Client.Graphics;
  10. using Robust.Client.State;
  11. using Robust.Client.UserInterface;
  12. using Robust.Shared.Prototypes;
  13. using Robust.Shared.Audio;
  14. namespace Content.Client.GameTicking.Managers
  15. {
  16. [UsedImplicitly]
  17. public sealed class ClientGameTicker : SharedGameTicker
  18. {
  19. [Dependency] private readonly IStateManager _stateManager = default!;
  20. [Dependency] private readonly IClientAdminManager _admin = default!;
  21. [Dependency] private readonly IClyde _clyde = default!;
  22. [Dependency] private readonly IUserInterfaceManager _userInterfaceManager = default!;
  23. private Dictionary<NetEntity, Dictionary<ProtoId<JobPrototype>, int?>> _jobsAvailable = new();
  24. private Dictionary<NetEntity, string> _stationNames = new();
  25. [ViewVariables] public bool AreWeReady { get; private set; }
  26. [ViewVariables] public bool IsGameStarted { get; private set; }
  27. [ViewVariables] public ResolvedSoundSpecifier? RestartSound { get; private set; }
  28. [ViewVariables] public string? LobbyBackground { get; private set; }
  29. [ViewVariables] public bool DisallowedLateJoin { get; private set; }
  30. [ViewVariables] public string? ServerInfoBlob { get; private set; }
  31. [ViewVariables] public TimeSpan StartTime { get; private set; }
  32. [ViewVariables] public new bool Paused { get; private set; }
  33. [ViewVariables] public IReadOnlyDictionary<NetEntity, Dictionary<ProtoId<JobPrototype>, int?>> JobsAvailable => _jobsAvailable;
  34. [ViewVariables] public IReadOnlyDictionary<NetEntity, string> StationNames => _stationNames;
  35. public event Action? InfoBlobUpdated;
  36. public event Action? LobbyStatusUpdated;
  37. public event Action? LobbyLateJoinStatusUpdated;
  38. public event Action<IReadOnlyDictionary<NetEntity, Dictionary<ProtoId<JobPrototype>, int?>>>? LobbyJobsAvailableUpdated;
  39. public override void Initialize()
  40. {
  41. base.Initialize();
  42. SubscribeNetworkEvent<TickerJoinLobbyEvent>(JoinLobby);
  43. SubscribeNetworkEvent<TickerJoinGameEvent>(JoinGame);
  44. SubscribeNetworkEvent<TickerConnectionStatusEvent>(ConnectionStatus);
  45. SubscribeNetworkEvent<TickerLobbyStatusEvent>(LobbyStatus);
  46. SubscribeNetworkEvent<TickerLobbyInfoEvent>(LobbyInfo);
  47. SubscribeNetworkEvent<TickerLobbyCountdownEvent>(LobbyCountdown);
  48. SubscribeNetworkEvent<RoundEndMessageEvent>(RoundEnd);
  49. SubscribeNetworkEvent<RequestWindowAttentionEvent>(OnAttentionRequest);
  50. SubscribeNetworkEvent<TickerLateJoinStatusEvent>(LateJoinStatus);
  51. SubscribeNetworkEvent<TickerJobsAvailableEvent>(UpdateJobsAvailable);
  52. _admin.AdminStatusUpdated += OnAdminUpdated;
  53. OnAdminUpdated();
  54. }
  55. public override void Shutdown()
  56. {
  57. _admin.AdminStatusUpdated -= OnAdminUpdated;
  58. base.Shutdown();
  59. }
  60. private void OnAdminUpdated()
  61. {
  62. // Hide some map/grid related logs from clients. This is to try prevent some easy metagaming by just
  63. // reading the console. E.g., logs like this one could leak the nuke station/grid:
  64. // > Grid NT-Arrivals 1101 (122/n25896) changed parent. Old parent: map 10 (121/n25895). New parent: FTL (123/n26470)
  65. #if !DEBUG
  66. EntityManager.System<SharedMapSystem>().Log.Level = _admin.IsAdmin() ? LogLevel.Info : LogLevel.Warning;
  67. #endif
  68. }
  69. private void OnAttentionRequest(RequestWindowAttentionEvent ev)
  70. {
  71. _clyde.RequestWindowAttention();
  72. }
  73. private void LateJoinStatus(TickerLateJoinStatusEvent message)
  74. {
  75. DisallowedLateJoin = message.Disallowed;
  76. LobbyLateJoinStatusUpdated?.Invoke();
  77. }
  78. private void UpdateJobsAvailable(TickerJobsAvailableEvent message)
  79. {
  80. _jobsAvailable.Clear();
  81. foreach (var (job, data) in message.JobsAvailableByStation)
  82. {
  83. _jobsAvailable[job] = data;
  84. }
  85. _stationNames.Clear();
  86. foreach (var weh in message.StationNames)
  87. {
  88. _stationNames[weh.Key] = weh.Value;
  89. }
  90. LobbyJobsAvailableUpdated?.Invoke(JobsAvailable);
  91. }
  92. private void JoinLobby(TickerJoinLobbyEvent message)
  93. {
  94. _stateManager.RequestStateChange<LobbyState>();
  95. }
  96. private void ConnectionStatus(TickerConnectionStatusEvent message)
  97. {
  98. RoundStartTimeSpan = message.RoundStartTimeSpan;
  99. }
  100. private void LobbyStatus(TickerLobbyStatusEvent message)
  101. {
  102. StartTime = message.StartTime;
  103. RoundStartTimeSpan = message.RoundStartTimeSpan;
  104. IsGameStarted = message.IsRoundStarted;
  105. AreWeReady = message.YouAreReady;
  106. LobbyBackground = message.LobbyBackground;
  107. Paused = message.Paused;
  108. LobbyStatusUpdated?.Invoke();
  109. }
  110. private void LobbyInfo(TickerLobbyInfoEvent message)
  111. {
  112. ServerInfoBlob = message.TextBlob;
  113. InfoBlobUpdated?.Invoke();
  114. }
  115. private void JoinGame(TickerJoinGameEvent message)
  116. {
  117. _stateManager.RequestStateChange<GameplayState>();
  118. }
  119. private void LobbyCountdown(TickerLobbyCountdownEvent message)
  120. {
  121. StartTime = message.StartTime;
  122. Paused = message.Paused;
  123. }
  124. private void RoundEnd(RoundEndMessageEvent message)
  125. {
  126. // Force an update in the event of this song being the same as the last.
  127. RestartSound = message.RestartSound;
  128. _userInterfaceManager.GetUIController<RoundEndSummaryUIController>().OpenRoundEndSummaryWindow(message);
  129. }
  130. }
  131. }