JobTest.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #nullable enable
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Content.IntegrationTests.Pair;
  5. using Content.Server.GameTicking;
  6. using Content.Server.Mind;
  7. using Content.Server.Roles;
  8. using Content.Shared.CCVar;
  9. using Content.Shared.GameTicking;
  10. using Content.Shared.Preferences;
  11. using Content.Shared.Roles;
  12. using Content.Shared.Roles.Jobs;
  13. using Robust.Shared.Network;
  14. using Robust.Shared.Prototypes;
  15. namespace Content.IntegrationTests.Tests.Round;
  16. [TestFixture]
  17. public sealed class JobTest
  18. {
  19. private static readonly ProtoId<JobPrototype> Passenger = "Passenger";
  20. private static readonly ProtoId<JobPrototype> Engineer = "StationEngineer";
  21. private static readonly ProtoId<JobPrototype> Captain = "Captain";
  22. private static string _map = "JobTestMap";
  23. [TestPrototypes]
  24. private static readonly string JobTestMap = @$"
  25. - type: gameMap
  26. id: {_map}
  27. mapName: {_map}
  28. mapPath: /Maps/civ/nomads.yml
  29. minPlayers: 0
  30. stations:
  31. Empty:
  32. stationProto: StandardStationArena
  33. components:
  34. - type: StationNameSetup
  35. mapNameTemplate: ""Nomads""
  36. - type: StationJobs
  37. availableJobs:
  38. {Passenger}: [ -1, -1 ]
  39. {Engineer}: [ -1, -1 ]
  40. {Captain}: [ 1, 1 ]
  41. ";
  42. private void AssertJob(TestPair pair, ProtoId<JobPrototype> job, NetUserId? user = null, bool isAntag = false)
  43. {
  44. var jobSys = pair.Server.System<SharedJobSystem>();
  45. var mindSys = pair.Server.System<MindSystem>();
  46. var roleSys = pair.Server.System<RoleSystem>();
  47. var ticker = pair.Server.System<GameTicker>();
  48. user ??= pair.Client.User!.Value;
  49. Assert.That(ticker.RunLevel, Is.EqualTo(GameRunLevel.InRound));
  50. Assert.That(ticker.PlayerGameStatuses[user.Value], Is.EqualTo(PlayerGameStatus.JoinedGame));
  51. var uid = pair.Server.PlayerMan.SessionsDict.GetValueOrDefault(user.Value)?.AttachedEntity;
  52. Assert.That(pair.Server.EntMan.EntityExists(uid));
  53. var mind = mindSys.GetMind(uid!.Value);
  54. Assert.That(pair.Server.EntMan.EntityExists(mind));
  55. Assert.That(jobSys.MindTryGetJobId(mind, out var actualJob));
  56. Assert.That(actualJob, Is.EqualTo(job));
  57. Assert.That(roleSys.MindIsAntagonist(mind), Is.EqualTo(isAntag));
  58. }
  59. /// <summary>
  60. /// Simple test that checks that starting the round spawns the player into the test map as a passenger.
  61. /// </summary>
  62. [Test]
  63. public async Task StartRoundTest()
  64. {
  65. await using var pair = await PoolManager.GetServerClient(new PoolSettings
  66. {
  67. DummyTicker = false,
  68. Connected = true,
  69. InLobby = true
  70. });
  71. pair.Server.CfgMan.SetCVar(CCVars.GameMap, _map);
  72. var ticker = pair.Server.System<GameTicker>();
  73. // Initially in the lobby
  74. Assert.That(ticker.RunLevel, Is.EqualTo(GameRunLevel.PreRoundLobby));
  75. Assert.That(pair.Client.AttachedEntity, Is.Null);
  76. Assert.That(ticker.PlayerGameStatuses[pair.Client.User!.Value], Is.EqualTo(PlayerGameStatus.NotReadyToPlay));
  77. // Ready up and start the round
  78. ticker.ToggleReadyAll(true);
  79. Assert.That(ticker.PlayerGameStatuses[pair.Client.User!.Value], Is.EqualTo(PlayerGameStatus.ReadyToPlay));
  80. await pair.Server.WaitPost(() => ticker.StartRound());
  81. await pair.RunTicksSync(10);
  82. AssertJob(pair, Passenger);
  83. await pair.Server.WaitPost(() => ticker.RestartRound());
  84. await pair.CleanReturnAsync();
  85. }
  86. /// <summary>
  87. /// Check that job preferences are respected.
  88. /// </summary>
  89. [Test]
  90. public async Task JobPreferenceTest()
  91. {
  92. await using var pair = await PoolManager.GetServerClient(new PoolSettings
  93. {
  94. DummyTicker = false,
  95. Connected = true,
  96. InLobby = true
  97. });
  98. pair.Server.CfgMan.SetCVar(CCVars.GameMap, _map);
  99. var ticker = pair.Server.System<GameTicker>();
  100. Assert.That(ticker.RunLevel, Is.EqualTo(GameRunLevel.PreRoundLobby));
  101. Assert.That(pair.Client.AttachedEntity, Is.Null);
  102. await pair.SetJobPriorities((Passenger, JobPriority.Medium), (Engineer, JobPriority.High));
  103. ticker.ToggleReadyAll(true);
  104. await pair.Server.WaitPost(() => ticker.StartRound());
  105. await pair.RunTicksSync(10);
  106. AssertJob(pair, Engineer);
  107. await pair.Server.WaitPost(() => ticker.RestartRound());
  108. Assert.That(ticker.RunLevel, Is.EqualTo(GameRunLevel.PreRoundLobby));
  109. await pair.SetJobPriorities((Passenger, JobPriority.High), (Engineer, JobPriority.Medium));
  110. ticker.ToggleReadyAll(true);
  111. await pair.Server.WaitPost(() => ticker.StartRound());
  112. await pair.RunTicksSync(10);
  113. AssertJob(pair, Passenger);
  114. await pair.Server.WaitPost(() => ticker.RestartRound());
  115. await pair.CleanReturnAsync();
  116. }
  117. /// <summary>
  118. /// Check high priority jobs (e.g., captain) are selected before other roles, even if it means a player does not
  119. /// get their preferred job.
  120. /// </summary>
  121. [Test]
  122. public async Task JobWeightTest()
  123. {
  124. await using var pair = await PoolManager.GetServerClient(new PoolSettings
  125. {
  126. DummyTicker = false,
  127. Connected = true,
  128. InLobby = true
  129. });
  130. pair.Server.CfgMan.SetCVar(CCVars.GameMap, _map);
  131. var ticker = pair.Server.System<GameTicker>();
  132. Assert.That(ticker.RunLevel, Is.EqualTo(GameRunLevel.PreRoundLobby));
  133. Assert.That(pair.Client.AttachedEntity, Is.Null);
  134. var captain = pair.Server.ProtoMan.Index(Captain);
  135. var engineer = pair.Server.ProtoMan.Index(Engineer);
  136. var passenger = pair.Server.ProtoMan.Index(Passenger);
  137. Assert.That(captain.Weight, Is.GreaterThan(engineer.Weight));
  138. Assert.That(engineer.Weight, Is.EqualTo(passenger.Weight));
  139. await pair.SetJobPriorities((Passenger, JobPriority.Medium), (Engineer, JobPriority.High), (Captain, JobPriority.Low));
  140. ticker.ToggleReadyAll(true);
  141. await pair.Server.WaitPost(() => ticker.StartRound());
  142. await pair.RunTicksSync(10);
  143. AssertJob(pair, Captain);
  144. await pair.Server.WaitPost(() => ticker.RestartRound());
  145. await pair.CleanReturnAsync();
  146. }
  147. /// <summary>
  148. /// Check that jobs are preferentially given to players that have marked those jobs as higher priority.
  149. /// </summary>
  150. [Test]
  151. public async Task JobPriorityTest()
  152. {
  153. await using var pair = await PoolManager.GetServerClient(new PoolSettings
  154. {
  155. DummyTicker = false,
  156. Connected = true,
  157. InLobby = true
  158. });
  159. pair.Server.CfgMan.SetCVar(CCVars.GameMap, _map);
  160. var ticker = pair.Server.System<GameTicker>();
  161. Assert.That(ticker.RunLevel, Is.EqualTo(GameRunLevel.PreRoundLobby));
  162. Assert.That(pair.Client.AttachedEntity, Is.Null);
  163. await pair.Server.AddDummySessions(5);
  164. await pair.RunTicksSync(5);
  165. var engineers = pair.Server.PlayerMan.Sessions.Select(x => x.UserId).ToList();
  166. var captain = engineers[3];
  167. engineers.RemoveAt(3);
  168. await pair.SetJobPriorities(captain, (Captain, JobPriority.High), (Engineer, JobPriority.Medium));
  169. foreach (var engi in engineers)
  170. {
  171. await pair.SetJobPriorities(engi, (Captain, JobPriority.Medium), (Engineer, JobPriority.High));
  172. }
  173. ticker.ToggleReadyAll(true);
  174. await pair.Server.WaitPost(() => ticker.StartRound());
  175. await pair.RunTicksSync(10);
  176. AssertJob(pair, Captain, captain);
  177. Assert.Multiple(() =>
  178. {
  179. foreach (var engi in engineers)
  180. {
  181. AssertJob(pair, Engineer, engi);
  182. }
  183. });
  184. await pair.Server.WaitPost(() => ticker.RestartRound());
  185. await pair.CleanReturnAsync();
  186. }
  187. }