1
0

JobTest.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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> Nomad = "Nomad";
  20. private static string _map = "JobTestMap";
  21. [TestPrototypes]
  22. private static readonly string JobTestMap = @$"
  23. - type: gameMap
  24. id: {_map}
  25. mapName: {_map}
  26. mapPath: /Maps/civ/nomads.yml
  27. minPlayers: 0
  28. stations:
  29. Empty:
  30. stationProto: StandardStationArena
  31. components:
  32. - type: StationNameSetup
  33. mapNameTemplate: ""Nomads""
  34. - type: StationJobs
  35. availableJobs:
  36. {Nomad}: [ -1, -1 ]
  37. ";
  38. private void AssertJob(TestPair pair, ProtoId<JobPrototype> job, NetUserId? user = null, bool isAntag = false)
  39. {
  40. var jobSys = pair.Server.System<SharedJobSystem>();
  41. var mindSys = pair.Server.System<MindSystem>();
  42. var roleSys = pair.Server.System<RoleSystem>();
  43. var ticker = pair.Server.System<GameTicker>();
  44. user ??= pair.Client.User!.Value;
  45. Assert.That(ticker.RunLevel, Is.EqualTo(GameRunLevel.InRound));
  46. Assert.That(ticker.PlayerGameStatuses[user.Value], Is.EqualTo(PlayerGameStatus.JoinedGame));
  47. var uid = pair.Server.PlayerMan.SessionsDict.GetValueOrDefault(user.Value)?.AttachedEntity;
  48. Assert.That(pair.Server.EntMan.EntityExists(uid));
  49. var mind = mindSys.GetMind(uid!.Value);
  50. Assert.That(pair.Server.EntMan.EntityExists(mind));
  51. Assert.That(jobSys.MindTryGetJobId(mind, out var actualJob));
  52. Assert.That(actualJob, Is.EqualTo(job));
  53. Assert.That(roleSys.MindIsAntagonist(mind), Is.EqualTo(isAntag));
  54. }
  55. /// <summary>
  56. /// Simple test that checks that starting the round spawns the player into the test map as a nomad.
  57. /// </summary>
  58. [Test]
  59. public async Task StartRoundTest()
  60. {
  61. await using var pair = await PoolManager.GetServerClient(new PoolSettings
  62. {
  63. DummyTicker = false,
  64. Connected = true,
  65. InLobby = true
  66. });
  67. pair.Server.CfgMan.SetCVar(CCVars.GameMap, _map);
  68. var ticker = pair.Server.System<GameTicker>();
  69. // Initially in the lobby
  70. Assert.That(ticker.RunLevel, Is.EqualTo(GameRunLevel.PreRoundLobby));
  71. Assert.That(pair.Client.AttachedEntity, Is.Null);
  72. Assert.That(ticker.PlayerGameStatuses[pair.Client.User!.Value], Is.EqualTo(PlayerGameStatus.NotReadyToPlay));
  73. // Ready up and start the round
  74. ticker.ToggleReadyAll(true);
  75. Assert.That(ticker.PlayerGameStatuses[pair.Client.User!.Value], Is.EqualTo(PlayerGameStatus.ReadyToPlay));
  76. await pair.Server.WaitPost(() => ticker.StartRound());
  77. await pair.RunTicksSync(10);
  78. AssertJob(pair, Nomad);
  79. await pair.Server.WaitPost(() => ticker.RestartRound());
  80. await pair.CleanReturnAsync();
  81. }
  82. /// <summary>
  83. /// Check that job preferences are respected.
  84. /// </summary>
  85. [Test]
  86. public async Task JobPreferenceTest()
  87. {
  88. await using var pair = await PoolManager.GetServerClient(new PoolSettings
  89. {
  90. DummyTicker = false,
  91. Connected = true,
  92. InLobby = true
  93. });
  94. pair.Server.CfgMan.SetCVar(CCVars.GameMap, _map);
  95. var ticker = pair.Server.System<GameTicker>();
  96. Assert.That(ticker.RunLevel, Is.EqualTo(GameRunLevel.PreRoundLobby));
  97. Assert.That(pair.Client.AttachedEntity, Is.Null);
  98. await pair.SetJobPriorities((Nomad, JobPriority.Medium));
  99. ticker.ToggleReadyAll(true);
  100. await pair.Server.WaitPost(() => ticker.StartRound());
  101. await pair.RunTicksSync(10);
  102. AssertJob(pair, Nomad);
  103. await pair.Server.WaitPost(() => ticker.RestartRound());
  104. await pair.CleanReturnAsync();
  105. }
  106. /// <summary>
  107. /// Check high priority jobs (e.g., captain) are selected before other roles, even if it means a player does not
  108. /// get their preferred job.
  109. /// </summary>
  110. [Test]
  111. public async Task JobWeightTest()
  112. {
  113. await using var pair = await PoolManager.GetServerClient(new PoolSettings
  114. {
  115. DummyTicker = false,
  116. Connected = true,
  117. InLobby = true
  118. });
  119. pair.Server.CfgMan.SetCVar(CCVars.GameMap, _map);
  120. var ticker = pair.Server.System<GameTicker>();
  121. Assert.That(ticker.RunLevel, Is.EqualTo(GameRunLevel.PreRoundLobby));
  122. Assert.That(pair.Client.AttachedEntity, Is.Null);
  123. var nomad = pair.Server.ProtoMan.Index(Nomad);
  124. await pair.SetJobPriorities((Nomad, JobPriority.Medium));
  125. ticker.ToggleReadyAll(true);
  126. await pair.Server.WaitPost(() => ticker.StartRound());
  127. await pair.RunTicksSync(10);
  128. AssertJob(pair, Nomad);
  129. await pair.Server.WaitPost(() => ticker.RestartRound());
  130. await pair.CleanReturnAsync();
  131. }
  132. }