Refactor set alert level command (#9794)

This commit is contained in:
Morber
2022-07-17 01:34:43 +03:00
committed by GitHub
parent 058d8371d9
commit 700f4aff57
3 changed files with 60 additions and 18 deletions

View File

@@ -1,6 +1,5 @@
using Content.Server.Administration; using System.Linq;
using Content.Server.AlertLevel; using Content.Server.Administration;
using Content.Server.Players;
using Content.Server.Station.Systems; using Content.Server.Station.Systems;
using Content.Shared.Administration; using Content.Shared.Administration;
using JetBrains.Annotations; using JetBrains.Annotations;
@@ -14,47 +13,82 @@ namespace Content.Server.AlertLevel.Commands
public sealed class SetAlertLevelCommand : IConsoleCommand public sealed class SetAlertLevelCommand : IConsoleCommand
{ {
public string Command => "setalertlevel"; public string Command => "setalertlevel";
public string Description => "Set current station alert level."; public string Description => Loc.GetString("cmd-setalertlevel-desc");
public string Help => "setalertlevel <level> [locked]"; public string Help => Loc.GetString("cmd-setalertlevel-help");
public CompletionResult GetCompletion(IConsoleShell shell, string[] args)
{
var levelNames = new string[] {};
var player = shell.Player as IPlayerSession;
if (player?.AttachedEntity != null)
{
var stationUid = EntitySystem.Get<StationSystem>().GetOwningStation(player.AttachedEntity.Value);
if (stationUid != null)
{
levelNames = GetStationLevelNames(stationUid.Value);
}
}
return args.Length switch
{
1 => CompletionResult.FromHintOptions(levelNames,
Loc.GetString("cmd-setalertlevel-hint-1")),
2 => CompletionResult.FromHintOptions(CompletionHelper.Booleans,
Loc.GetString("cmd-setalertlevel-hint-2")),
_ => CompletionResult.Empty,
};
}
public void Execute(IConsoleShell shell, string argStr, string[] args) public void Execute(IConsoleShell shell, string argStr, string[] args)
{ {
if (args.Length < 1) if (args.Length < 1)
{ {
shell.WriteError("Incorrect number of arguments. " + Help); shell.WriteError(Loc.GetString("shell-wrong-arguments-number"));
return; return;
} }
var locked = false; var locked = false;
if (args.Length > 1 && !bool.TryParse(args[1], out locked)) if (args.Length > 1 && !bool.TryParse(args[1], out locked))
{ {
shell.WriteLine("Invalid boolean flag"); shell.WriteLine(Loc.GetString("shell-argument-must-be-boolean"));
return; return;
} }
var player = shell.Player as IPlayerSession; var player = shell.Player as IPlayerSession;
if (player == null) if (player?.AttachedEntity == null)
{ {
shell.WriteLine("You cannot run this from the server or without an attached entity."); shell.WriteLine(Loc.GetString("shell-only-players-can-run-this-command"));
return; return;
} }
var playerEntityUid = player.AttachedEntity; var stationUid = EntitySystem.Get<StationSystem>().GetOwningStation(player.AttachedEntity.Value);
if (playerEntityUid == null)
{
shell.WriteLine("You cannot run this from the server or without an attached entity.");
return;
}
var stationUid = EntitySystem.Get<StationSystem>().GetOwningStation(playerEntityUid.Value);
if (stationUid == null) if (stationUid == null)
{ {
shell.WriteLine("You must be on grid of station code that you are going to change."); shell.WriteLine(Loc.GetString("cmd-setalertlevel-invalid-grid"));
return; return;
} }
var level = args[0]; var level = args[0];
var levelNames = GetStationLevelNames(stationUid.Value);
if (!levelNames.Contains(level))
{
shell.WriteLine(Loc.GetString("cmd-setalertlevel-invalid-level"));
return;
}
EntitySystem.Get<AlertLevelSystem>().SetLevel(stationUid.Value, level, true, true, true, locked); EntitySystem.Get<AlertLevelSystem>().SetLevel(stationUid.Value, level, true, true, true, locked);
} }
private string[] GetStationLevelNames(EntityUid station)
{
var entityManager = IoCManager.Resolve<IEntityManager>();
if (!entityManager.TryGetComponent<AlertLevelComponent>(station, out var alertLevelComp))
return new string[]{};
if (alertLevelComp.AlertLevels == null)
return new string[]{};
return alertLevelComp.AlertLevels.Levels.Keys.ToArray();
}
} }
} }

View File

@@ -0,0 +1,7 @@
cmd-setalertlevel-desc = Set current station alert level for grid on which the player is standing.
cmd-setalertlevel-help = Usage: setalertlevel <level> [locked]
cmd-setalertlevel-invalid-grid = You must be on grid of station code that you are going to change.
cmd-setalertlevel-invalid-level = Specified alert level does not exist on that grid.
cmd-setalertlevel-hint-1 = <level>
cmd-setalertlevel-hint-2 = [locked]

View File

@@ -14,6 +14,7 @@ shell-only-players-can-run-this-command = Only players can run this command.
shell-need-exactly-one-argument = Need exactly one argument. shell-need-exactly-one-argument = Need exactly one argument.
shell-wrong-arguments-number-need-specific = Need {$properAmount} arguments, there were {$currentAmount}. shell-wrong-arguments-number-need-specific = Need {$properAmount} arguments, there were {$currentAmount}.
shell-argument-must-be-number = Argument must be a number. shell-argument-must-be-number = Argument must be a number.
shell-argument-must-be-boolean = Argument must be a boolean.
shell-wrong-arguments-number = Wrong number of arguments. shell-wrong-arguments-number = Wrong number of arguments.
shell-need-between-arguments = Need {$lower} to {$upper} arguments! shell-need-between-arguments = Need {$lower} to {$upper} arguments!