MakeRaffledGhostRoleCommand.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System.Linq;
  2. using Content.Server.Administration;
  3. using Content.Server.Ghost.Roles.Components;
  4. using Content.Server.Ghost.Roles.Raffles;
  5. using Content.Shared.Administration;
  6. using Content.Shared.Ghost.Roles.Raffles;
  7. using Content.Shared.Mind.Components;
  8. using Robust.Shared.Console;
  9. using Robust.Shared.Prototypes;
  10. namespace Content.Server.Ghost.Roles
  11. {
  12. [AdminCommand(AdminFlags.Admin)]
  13. public sealed class MakeRaffledGhostRoleCommand : IConsoleCommand
  14. {
  15. [Dependency] private readonly IPrototypeManager _protoManager = default!;
  16. [Dependency] private readonly IEntityManager _entManager = default!;
  17. public string Command => "makeghostroleraffled";
  18. public string Description => "Turns an entity into a raffled ghost role.";
  19. public string Help => $"Usage: {Command} <entity uid> <name> <description> (<settings prototype> | <initial duration> <extend by> <max duration>) [<rules>]\n" +
  20. $"Durations are in seconds.";
  21. public void Execute(IConsoleShell shell, string argStr, string[] args)
  22. {
  23. if (args.Length is < 4 or > 7)
  24. {
  25. shell.WriteLine($"Invalid amount of arguments.\n{Help}");
  26. return;
  27. }
  28. if (!NetEntity.TryParse(args[0], out var uidNet) || !_entManager.TryGetEntity(uidNet, out var uid))
  29. {
  30. shell.WriteLine($"{args[0]} is not a valid entity uid.");
  31. return;
  32. }
  33. if (!_entManager.TryGetComponent(uid, out MetaDataComponent? metaData))
  34. {
  35. shell.WriteLine($"No entity found with uid {uid}");
  36. return;
  37. }
  38. if (_entManager.TryGetComponent(uid, out MindContainerComponent? mind) &&
  39. mind.HasMind)
  40. {
  41. shell.WriteLine($"Entity {metaData.EntityName} with id {uid} already has a mind.");
  42. return;
  43. }
  44. if (_entManager.TryGetComponent(uid, out GhostRoleComponent? ghostRole))
  45. {
  46. shell.WriteLine($"Entity {metaData.EntityName} with id {uid} already has a {nameof(GhostRoleComponent)}");
  47. return;
  48. }
  49. if (_entManager.HasComponent<GhostTakeoverAvailableComponent>(uid))
  50. {
  51. shell.WriteLine($"Entity {metaData.EntityName} with id {uid} already has a {nameof(GhostTakeoverAvailableComponent)}");
  52. return;
  53. }
  54. var name = args[1];
  55. var description = args[2];
  56. // if the rules are specified then use those, otherwise use the default
  57. var rules = args.Length switch
  58. {
  59. 5 => args[4],
  60. 7 => args[6],
  61. _ => Loc.GetString("ghost-role-component-default-rules"),
  62. };
  63. // is it an invocation with a prototype ID and optional rules?
  64. var isProto = args.Length is 4 or 5;
  65. GhostRoleRaffleSettings settings;
  66. if (isProto)
  67. {
  68. if (!_protoManager.TryIndex<GhostRoleRaffleSettingsPrototype>(args[3], out var proto))
  69. {
  70. var validProtos = string.Join(", ",
  71. _protoManager.EnumeratePrototypes<GhostRoleRaffleSettingsPrototype>().Select(p => p.ID)
  72. );
  73. shell.WriteLine($"{args[3]} is not a valid raffle settings prototype. Valid options: {validProtos}");
  74. return;
  75. }
  76. settings = proto.Settings;
  77. }
  78. else
  79. {
  80. if (!uint.TryParse(args[3], out var initial)
  81. || !uint.TryParse(args[4], out var extends)
  82. || !uint.TryParse(args[5], out var max)
  83. || initial == 0 || max == 0)
  84. {
  85. shell.WriteLine($"The raffle initial/extends/max settings must be positive numbers.");
  86. return;
  87. }
  88. if (initial > max)
  89. {
  90. shell.WriteLine("The initial duration must be smaller than or equal to the maximum duration.");
  91. return;
  92. }
  93. settings = new GhostRoleRaffleSettings()
  94. {
  95. InitialDuration = initial,
  96. JoinExtendsDurationBy = extends,
  97. MaxDuration = max
  98. };
  99. }
  100. ghostRole = _entManager.AddComponent<GhostRoleComponent>(uid.Value);
  101. _entManager.AddComponent<GhostTakeoverAvailableComponent>(uid.Value);
  102. ghostRole.RoleName = name;
  103. ghostRole.RoleDescription = description;
  104. ghostRole.RoleRules = rules;
  105. ghostRole.RaffleConfig = new GhostRoleRaffleConfig(settings);
  106. shell.WriteLine($"Made entity {metaData.EntityName} a raffled ghost role.");
  107. }
  108. }
  109. }