SharedGameTicker.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. using Content.Shared.Roles;
  2. using Robust.Shared.Network;
  3. using Robust.Shared.Prototypes;
  4. using Robust.Shared.Replays;
  5. using Robust.Shared.Serialization;
  6. using Robust.Shared.Serialization.Markdown.Mapping;
  7. using Robust.Shared.Serialization.Markdown.Value;
  8. using Robust.Shared.Timing;
  9. using Robust.Shared.Audio;
  10. using Robust.Shared.Player;
  11. using Content.Shared.NPC.Prototypes;
  12. namespace Content.Shared.GameTicking
  13. {
  14. public abstract class SharedGameTicker : EntitySystem
  15. {
  16. [Dependency] private readonly IReplayRecordingManager _replay = default!;
  17. [Dependency] private readonly IGameTiming _gameTiming = default!;
  18. [Dependency] private readonly ISharedPlayerManager _playerManager = default!;
  19. // See ideally these would be pulled from the job definition or something.
  20. // But this is easier, and at least it isn't hardcoded.
  21. //TODO: Move these, they really belong in StationJobsSystem or a cvar.
  22. [ValidatePrototypeId<JobPrototype>]
  23. public const string FallbackOverflowJob = "Nomad";
  24. public const string FallbackOverflowJobName = "job-name-passenger";
  25. // TODO network.
  26. // Probably most useful for replays, round end info, and probably things like lobby menus.
  27. [ViewVariables]
  28. public int RoundId { get; protected set; }
  29. [ViewVariables] public TimeSpan RoundStartTimeSpan { get; protected set; }
  30. public override void Initialize()
  31. {
  32. base.Initialize();
  33. _replay.RecordingStarted += OnRecordingStart;
  34. }
  35. public override void Shutdown()
  36. {
  37. _replay.RecordingStarted -= OnRecordingStart;
  38. }
  39. private void OnRecordingStart(MappingDataNode metadata, List<object> events)
  40. {
  41. if (RoundId != 0)
  42. {
  43. metadata["roundId"] = new ValueDataNode(RoundId.ToString());
  44. }
  45. }
  46. public TimeSpan RoundDuration()
  47. {
  48. return _gameTiming.CurTime.Subtract(RoundStartTimeSpan);
  49. }
  50. }
  51. [Serializable, NetSerializable]
  52. public sealed class TickerJoinLobbyEvent : EntityEventArgs
  53. {
  54. }
  55. [Serializable, NetSerializable]
  56. public sealed class TickerJoinGameEvent : EntityEventArgs
  57. {
  58. }
  59. [Serializable, NetSerializable]
  60. public sealed class TickerLateJoinStatusEvent : EntityEventArgs
  61. {
  62. // TODO: Make this a replicated CVar, honestly.
  63. public bool Disallowed { get; }
  64. public TickerLateJoinStatusEvent(bool disallowed)
  65. {
  66. Disallowed = disallowed;
  67. }
  68. }
  69. [Serializable, NetSerializable]
  70. public sealed class TickerConnectionStatusEvent : EntityEventArgs
  71. {
  72. public TimeSpan RoundStartTimeSpan { get; }
  73. public TickerConnectionStatusEvent(TimeSpan roundStartTimeSpan)
  74. {
  75. RoundStartTimeSpan = roundStartTimeSpan;
  76. }
  77. }
  78. [Serializable, NetSerializable]
  79. public sealed class TickerLobbyStatusEvent : EntityEventArgs
  80. {
  81. public bool IsRoundStarted { get; }
  82. public string? LobbyBackground { get; }
  83. public bool YouAreReady { get; }
  84. // UTC.
  85. public TimeSpan StartTime { get; }
  86. public TimeSpan RoundStartTimeSpan { get; }
  87. public bool Paused { get; }
  88. public TickerLobbyStatusEvent(bool isRoundStarted, string? lobbyBackground, bool youAreReady, TimeSpan startTime, TimeSpan preloadTime, TimeSpan roundStartTimeSpan, bool paused)
  89. {
  90. IsRoundStarted = isRoundStarted;
  91. LobbyBackground = lobbyBackground;
  92. YouAreReady = youAreReady;
  93. StartTime = startTime;
  94. RoundStartTimeSpan = roundStartTimeSpan;
  95. Paused = paused;
  96. }
  97. }
  98. [Serializable, NetSerializable]
  99. public sealed class TickerLobbyInfoEvent : EntityEventArgs
  100. {
  101. public string TextBlob { get; }
  102. public TickerLobbyInfoEvent(string textBlob)
  103. {
  104. TextBlob = textBlob;
  105. }
  106. }
  107. [Serializable, NetSerializable]
  108. public sealed class GetPlayerFactionCounts : EntityEventArgs
  109. {
  110. public Dictionary<ProtoId<NpcFactionPrototype>, int> FactionCounts { get; } = new();
  111. public GetPlayerFactionCounts(Dictionary<ProtoId<NpcFactionPrototype>, int> factionCounts)
  112. {
  113. FactionCounts = factionCounts;
  114. }
  115. }
  116. [Serializable, NetSerializable]
  117. public sealed class TickerLobbyCountdownEvent : EntityEventArgs
  118. {
  119. /// <summary>
  120. /// The game time that the game will start at.
  121. /// </summary>
  122. public TimeSpan StartTime { get; }
  123. /// <summary>
  124. /// Whether or not the countdown is paused
  125. /// </summary>
  126. public bool Paused { get; }
  127. public TickerLobbyCountdownEvent(TimeSpan startTime, bool paused)
  128. {
  129. StartTime = startTime;
  130. Paused = paused;
  131. }
  132. }
  133. [Serializable, NetSerializable]
  134. public sealed class TickerJobsAvailableEvent(
  135. Dictionary<NetEntity, string> stationNames,
  136. Dictionary<NetEntity, Dictionary<ProtoId<JobPrototype>, int?>> jobsAvailableByStation)
  137. : EntityEventArgs
  138. {
  139. /// <summary>
  140. /// The Status of the Player in the lobby (ready, observer, ...)
  141. /// </summary>
  142. public Dictionary<NetEntity, Dictionary<ProtoId<JobPrototype>, int?>> JobsAvailableByStation { get; } = jobsAvailableByStation;
  143. public Dictionary<NetEntity, string> StationNames { get; } = stationNames;
  144. }
  145. [Serializable, NetSerializable, DataDefinition]
  146. public sealed partial class RoundEndMessageEvent : EntityEventArgs
  147. {
  148. [Serializable, NetSerializable, DataDefinition]
  149. public partial struct RoundEndPlayerInfo
  150. {
  151. [DataField]
  152. public string PlayerOOCName;
  153. [DataField]
  154. public string? PlayerICName;
  155. [DataField, NonSerialized]
  156. public NetUserId? PlayerGuid;
  157. public string Role;
  158. [DataField, NonSerialized]
  159. public string[] JobPrototypes;
  160. [DataField, NonSerialized]
  161. public string[] AntagPrototypes;
  162. public NetEntity? PlayerNetEntity;
  163. [DataField]
  164. public bool Antag;
  165. [DataField]
  166. public bool Observer;
  167. public bool Connected;
  168. }
  169. public string GamemodeTitle { get; }
  170. public string RoundEndText { get; }
  171. public TimeSpan RoundDuration { get; }
  172. public int RoundId { get; }
  173. public int PlayerCount { get; }
  174. public RoundEndPlayerInfo[] AllPlayersEndInfo { get; }
  175. /// <summary>
  176. /// Sound gets networked due to how entity lifecycle works between client / server and to avoid clipping.
  177. /// </summary>
  178. public ResolvedSoundSpecifier? RestartSound;
  179. public RoundEndMessageEvent(
  180. string gamemodeTitle,
  181. string roundEndText,
  182. TimeSpan roundDuration,
  183. int roundId,
  184. int playerCount,
  185. RoundEndPlayerInfo[] allPlayersEndInfo,
  186. ResolvedSoundSpecifier? restartSound)
  187. {
  188. GamemodeTitle = gamemodeTitle;
  189. RoundEndText = roundEndText;
  190. RoundDuration = roundDuration;
  191. RoundId = roundId;
  192. PlayerCount = playerCount;
  193. AllPlayersEndInfo = allPlayersEndInfo;
  194. RestartSound = restartSound;
  195. }
  196. }
  197. [Serializable, NetSerializable]
  198. public enum PlayerGameStatus : sbyte
  199. {
  200. NotReadyToPlay = 0,
  201. ReadyToPlay,
  202. JoinedGame,
  203. }
  204. }