SetAlertLevelCommand.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System.Linq;
  2. using Content.Server.Administration;
  3. using Content.Server.Station.Systems;
  4. using Content.Shared.Administration;
  5. using JetBrains.Annotations;
  6. using Robust.Shared.Console;
  7. namespace Content.Server.AlertLevel.Commands
  8. {
  9. [UsedImplicitly]
  10. [AdminCommand(AdminFlags.Fun)]
  11. public sealed class SetAlertLevelCommand : LocalizedCommands
  12. {
  13. [Dependency] private readonly IEntitySystemManager _entitySystems = default!;
  14. public override string Command => "setalertlevel";
  15. public override CompletionResult GetCompletion(IConsoleShell shell, string[] args)
  16. {
  17. var levelNames = new string[] {};
  18. var player = shell.Player;
  19. if (player?.AttachedEntity != null)
  20. {
  21. var stationUid = _entitySystems.GetEntitySystem<StationSystem>().GetOwningStation(player.AttachedEntity.Value);
  22. if (stationUid != null)
  23. {
  24. levelNames = GetStationLevelNames(stationUid.Value);
  25. }
  26. }
  27. return args.Length switch
  28. {
  29. 1 => CompletionResult.FromHintOptions(levelNames,
  30. LocalizationManager.GetString("cmd-setalertlevel-hint-1")),
  31. 2 => CompletionResult.FromHintOptions(CompletionHelper.Booleans,
  32. LocalizationManager.GetString("cmd-setalertlevel-hint-2")),
  33. _ => CompletionResult.Empty,
  34. };
  35. }
  36. public override void Execute(IConsoleShell shell, string argStr, string[] args)
  37. {
  38. if (args.Length < 1)
  39. {
  40. shell.WriteError(LocalizationManager.GetString("shell-wrong-arguments-number"));
  41. return;
  42. }
  43. var locked = false;
  44. if (args.Length > 1 && !bool.TryParse(args[1], out locked))
  45. {
  46. shell.WriteLine(LocalizationManager.GetString("shell-argument-must-be-boolean"));
  47. return;
  48. }
  49. var player = shell.Player;
  50. if (player?.AttachedEntity == null)
  51. {
  52. shell.WriteLine(LocalizationManager.GetString("shell-only-players-can-run-this-command"));
  53. return;
  54. }
  55. var stationUid = _entitySystems.GetEntitySystem<StationSystem>().GetOwningStation(player.AttachedEntity.Value);
  56. if (stationUid == null)
  57. {
  58. shell.WriteLine(LocalizationManager.GetString("cmd-setalertlevel-invalid-grid"));
  59. return;
  60. }
  61. var level = args[0];
  62. var levelNames = GetStationLevelNames(stationUid.Value);
  63. if (!levelNames.Contains(level))
  64. {
  65. shell.WriteLine(LocalizationManager.GetString("cmd-setalertlevel-invalid-level"));
  66. return;
  67. }
  68. _entitySystems.GetEntitySystem<AlertLevelSystem>().SetLevel(stationUid.Value, level, true, true, true, locked);
  69. }
  70. private string[] GetStationLevelNames(EntityUid station)
  71. {
  72. var entityManager = IoCManager.Resolve<IEntityManager>();
  73. if (!entityManager.TryGetComponent<AlertLevelComponent>(station, out var alertLevelComp))
  74. return new string[]{};
  75. if (alertLevelComp.AlertLevels == null)
  76. return new string[]{};
  77. return alertLevelComp.AlertLevels.Levels.Keys.ToArray();
  78. }
  79. }
  80. }