LobbyState.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. using Content.Client.Audio;
  2. using Content.Client.GameTicking.Managers;
  3. using Content.Client.LateJoin;
  4. using Content.Client.Lobby.UI;
  5. using Content.Client.Message;
  6. using Content.Client.UserInterface.Systems.Chat;
  7. using Content.Client.Voting;
  8. using Content.Shared.CCVar;
  9. using Robust.Client;
  10. using Robust.Client.Console;
  11. using Robust.Client.ResourceManagement;
  12. using Robust.Client.UserInterface;
  13. using Robust.Client.UserInterface.Controls;
  14. using Robust.Shared.Configuration;
  15. using Robust.Shared.Timing;
  16. namespace Content.Client.Lobby
  17. {
  18. public sealed class LobbyState : Robust.Client.State.State
  19. {
  20. [Dependency] private readonly IBaseClient _baseClient = default!;
  21. [Dependency] private readonly IConfigurationManager _cfg = default!;
  22. [Dependency] private readonly IClientConsoleHost _consoleHost = default!;
  23. [Dependency] private readonly IEntityManager _entityManager = default!;
  24. [Dependency] private readonly IResourceCache _resourceCache = default!;
  25. [Dependency] private readonly IUserInterfaceManager _userInterfaceManager = default!;
  26. [Dependency] private readonly IGameTiming _gameTiming = default!;
  27. [Dependency] private readonly IVoteManager _voteManager = default!;
  28. private ClientGameTicker _gameTicker = default!;
  29. private ContentAudioSystem _contentAudioSystem = default!;
  30. protected override Type? LinkedScreenType { get; } = typeof(LobbyGui);
  31. public LobbyGui? Lobby;
  32. protected override void Startup()
  33. {
  34. if (_userInterfaceManager.ActiveScreen == null)
  35. {
  36. return;
  37. }
  38. Lobby = (LobbyGui) _userInterfaceManager.ActiveScreen;
  39. var chatController = _userInterfaceManager.GetUIController<ChatUIController>();
  40. _gameTicker = _entityManager.System<ClientGameTicker>();
  41. _contentAudioSystem = _entityManager.System<ContentAudioSystem>();
  42. _contentAudioSystem.LobbySoundtrackChanged += UpdateLobbySoundtrackInfo;
  43. chatController.SetMainChat(true);
  44. _voteManager.SetPopupContainer(Lobby.VoteContainer);
  45. LayoutContainer.SetAnchorPreset(Lobby, LayoutContainer.LayoutPreset.Wide);
  46. var lobbyNameCvar = _cfg.GetCVar(CCVars.ServerLobbyName);
  47. var serverName = _baseClient.GameInfo?.ServerName ?? string.Empty;
  48. Lobby.ServerName.Text = string.IsNullOrEmpty(lobbyNameCvar)
  49. ? Loc.GetString("ui-lobby-title", ("serverName", serverName))
  50. : lobbyNameCvar;
  51. var width = _cfg.GetCVar(CCVars.ServerLobbyRightPanelWidth);
  52. Lobby.RightSide.SetWidth = width;
  53. UpdateLobbyUi();
  54. Lobby.CharacterPreview.CharacterSetupButton.OnPressed += OnSetupPressed;
  55. Lobby.ReadyButton.OnPressed += OnReadyPressed;
  56. Lobby.ReadyButton.OnToggled += OnReadyToggled;
  57. _gameTicker.InfoBlobUpdated += UpdateLobbyUi;
  58. _gameTicker.LobbyStatusUpdated += LobbyStatusUpdated;
  59. _gameTicker.LobbyLateJoinStatusUpdated += LobbyLateJoinStatusUpdated;
  60. }
  61. protected override void Shutdown()
  62. {
  63. var chatController = _userInterfaceManager.GetUIController<ChatUIController>();
  64. chatController.SetMainChat(false);
  65. _gameTicker.InfoBlobUpdated -= UpdateLobbyUi;
  66. _gameTicker.LobbyStatusUpdated -= LobbyStatusUpdated;
  67. _gameTicker.LobbyLateJoinStatusUpdated -= LobbyLateJoinStatusUpdated;
  68. _contentAudioSystem.LobbySoundtrackChanged -= UpdateLobbySoundtrackInfo;
  69. _voteManager.ClearPopupContainer();
  70. Lobby!.CharacterPreview.CharacterSetupButton.OnPressed -= OnSetupPressed;
  71. Lobby!.ReadyButton.OnPressed -= OnReadyPressed;
  72. Lobby!.ReadyButton.OnToggled -= OnReadyToggled;
  73. Lobby = null;
  74. }
  75. public void SwitchState(LobbyGui.LobbyGuiState state)
  76. {
  77. // Yeah I hate this but LobbyState contains all the badness for now.
  78. Lobby?.SwitchState(state);
  79. }
  80. private void OnSetupPressed(BaseButton.ButtonEventArgs args)
  81. {
  82. SetReady(false);
  83. Lobby?.SwitchState(LobbyGui.LobbyGuiState.CharacterSetup);
  84. }
  85. private void OnReadyPressed(BaseButton.ButtonEventArgs args)
  86. {
  87. if (!_gameTicker.IsGameStarted)
  88. {
  89. return;
  90. }
  91. new LateJoinGui().OpenCentered();
  92. }
  93. private void OnReadyToggled(BaseButton.ButtonToggledEventArgs args)
  94. {
  95. SetReady(args.Pressed);
  96. }
  97. public override void FrameUpdate(FrameEventArgs e)
  98. {
  99. if (_gameTicker.IsGameStarted)
  100. {
  101. Lobby!.StartTime.Text = string.Empty;
  102. var roundTime = _gameTiming.CurTime.Subtract(_gameTicker.RoundStartTimeSpan);
  103. Lobby!.StationTime.Text = Loc.GetString("lobby-state-player-status-round-time", ("hours", roundTime.Hours), ("minutes", roundTime.Minutes));
  104. return;
  105. }
  106. Lobby!.StationTime.Text = Loc.GetString("lobby-state-player-status-round-not-started");
  107. string text;
  108. if (_gameTicker.Paused)
  109. {
  110. text = Loc.GetString("lobby-state-paused");
  111. }
  112. else if (_gameTicker.StartTime < _gameTiming.CurTime)
  113. {
  114. Lobby!.StartTime.Text = Loc.GetString("lobby-state-soon");
  115. return;
  116. }
  117. else
  118. {
  119. var difference = _gameTicker.StartTime - _gameTiming.CurTime;
  120. var seconds = difference.TotalSeconds;
  121. if (seconds < 0)
  122. {
  123. text = Loc.GetString(seconds < -5 ? "lobby-state-right-now-question" : "lobby-state-right-now-confirmation");
  124. }
  125. else if (difference.TotalHours >= 1)
  126. {
  127. text = $"{Math.Floor(difference.TotalHours)}:{difference.Minutes:D2}:{difference.Seconds:D2}";
  128. }
  129. else
  130. {
  131. text = $"{difference.Minutes}:{difference.Seconds:D2}";
  132. }
  133. }
  134. Lobby!.StartTime.Text = Loc.GetString("lobby-state-round-start-countdown-text", ("timeLeft", text));
  135. }
  136. private void LobbyStatusUpdated()
  137. {
  138. UpdateLobbyBackground();
  139. UpdateLobbyUi();
  140. }
  141. private void LobbyLateJoinStatusUpdated()
  142. {
  143. Lobby!.ReadyButton.Disabled = _gameTicker.DisallowedLateJoin;
  144. }
  145. private void UpdateLobbyUi()
  146. {
  147. if (_gameTicker.IsGameStarted)
  148. {
  149. Lobby!.ReadyButton.Text = Loc.GetString("lobby-state-ready-button-join-state");
  150. Lobby!.ReadyButton.ToggleMode = false;
  151. Lobby!.ReadyButton.Pressed = false;
  152. Lobby!.ObserveButton.Disabled = false;
  153. }
  154. else
  155. {
  156. Lobby!.StartTime.Text = string.Empty;
  157. Lobby!.ReadyButton.Text = Loc.GetString(Lobby!.ReadyButton.Pressed ? "lobby-state-player-status-ready": "lobby-state-player-status-not-ready");
  158. Lobby!.ReadyButton.ToggleMode = true;
  159. Lobby!.ReadyButton.Disabled = false;
  160. Lobby!.ReadyButton.Pressed = _gameTicker.AreWeReady;
  161. Lobby!.ObserveButton.Disabled = true;
  162. }
  163. if (_gameTicker.ServerInfoBlob != null)
  164. {
  165. Lobby!.ServerInfo.SetInfoBlob(_gameTicker.ServerInfoBlob);
  166. }
  167. }
  168. private void UpdateLobbySoundtrackInfo(LobbySoundtrackChangedEvent ev)
  169. {
  170. if (ev.SoundtrackFilename == null)
  171. {
  172. Lobby!.LobbySong.SetMarkup(Loc.GetString("lobby-state-song-no-song-text"));
  173. }
  174. else if (
  175. ev.SoundtrackFilename != null
  176. && _resourceCache.TryGetResource<AudioResource>(ev.SoundtrackFilename, out var lobbySongResource)
  177. )
  178. {
  179. var lobbyStream = lobbySongResource.AudioStream;
  180. var title = string.IsNullOrEmpty(lobbyStream.Title)
  181. ? Loc.GetString("lobby-state-song-unknown-title")
  182. : lobbyStream.Title;
  183. var artist = string.IsNullOrEmpty(lobbyStream.Artist)
  184. ? Loc.GetString("lobby-state-song-unknown-artist")
  185. : lobbyStream.Artist;
  186. var markup = Loc.GetString("lobby-state-song-text",
  187. ("songTitle", title),
  188. ("songArtist", artist));
  189. Lobby!.LobbySong.SetMarkup(markup);
  190. }
  191. }
  192. private void UpdateLobbyBackground()
  193. {
  194. if (_gameTicker.LobbyBackground != null)
  195. {
  196. Lobby!.Background.Texture = _resourceCache.GetResource<TextureResource>(_gameTicker.LobbyBackground );
  197. }
  198. else
  199. {
  200. Lobby!.Background.Texture = null;
  201. }
  202. }
  203. private void SetReady(bool newReady)
  204. {
  205. if (_gameTicker.IsGameStarted)
  206. {
  207. return;
  208. }
  209. _consoleHost.ExecuteCommand($"toggleready {newReady}");
  210. }
  211. }
  212. }