1
0

ClientGameTicker.cs 6.5 KB

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