UI to edit silicon laws from admin verb (#28483)

* UI to edit silicon laws from admin verb

(peak shitcode)

* Improve UI

* Use Moderator admin flag

* Reviews
This commit is contained in:
Simon
2024-08-09 08:16:22 +02:00
committed by GitHub
parent 8d96c993a2
commit 4c4cdb5b06
11 changed files with 395 additions and 0 deletions

View File

@@ -35,6 +35,9 @@ using Robust.Shared.Toolshed;
using Robust.Shared.Utility;
using System.Linq;
using System.Numerics;
using Content.Server.Silicons.Laws;
using Content.Shared.Silicons.Laws.Components;
using Robust.Server.Player;
using Robust.Shared.Physics.Components;
using static Content.Shared.Configurable.ConfigurationComponent;
@@ -68,6 +71,8 @@ namespace Content.Server.Administration.Systems
[Dependency] private readonly StationSpawningSystem _spawning = default!;
[Dependency] private readonly ExamineSystemShared _examine = default!;
[Dependency] private readonly AdminFrozenSystem _freeze = default!;
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly SiliconLawSystem _siliconLawSystem = default!;
private readonly Dictionary<ICommonSession, List<EditSolutionsEui>> _openSolutionUis = new();
@@ -338,6 +343,25 @@ namespace Content.Server.Administration.Systems
Impact = LogImpact.Low
});
if (TryComp<SiliconLawBoundComponent>(args.Target, out var lawBoundComponent))
{
args.Verbs.Add(new Verb()
{
Text = Loc.GetString("silicon-law-ui-verb"),
Category = VerbCategory.Admin,
Act = () =>
{
var ui = new SiliconLawEui(_siliconLawSystem, EntityManager, _adminManager);
if (!_playerManager.TryGetSessionByEntity(args.User, out var session))
{
return;
}
_euiManager.OpenEui(ui, session);
ui.UpdateLaws(lawBoundComponent, args.Target);
},
Icon = new SpriteSpecifier.Rsi(new ResPath("/Textures/Interface/Actions/actions_borg.rsi"), "state-laws"),
});
}
}
}

View File

@@ -0,0 +1,70 @@
using Content.Server.Administration.Managers;
using Content.Server.EUI;
using Content.Shared.Administration;
using Content.Shared.Eui;
using Content.Shared.Silicons.Laws;
using Content.Shared.Silicons.Laws.Components;
namespace Content.Server.Silicons.Laws;
public sealed class SiliconLawEui : BaseEui
{
private readonly SiliconLawSystem _siliconLawSystem;
private readonly EntityManager _entityManager;
private readonly IAdminManager _adminManager;
private List<SiliconLaw> _laws = new();
private ISawmill _sawmill = default!;
private EntityUid _target;
public SiliconLawEui(SiliconLawSystem siliconLawSystem, EntityManager entityManager, IAdminManager manager)
{
_siliconLawSystem = siliconLawSystem;
_adminManager = manager;
_entityManager = entityManager;
_sawmill = Logger.GetSawmill("silicon-law-eui");
}
public override EuiStateBase GetNewState()
{
return new SiliconLawsEuiState(_laws, _entityManager.GetNetEntity(_target));
}
public void UpdateLaws(SiliconLawBoundComponent? lawBoundComponent, EntityUid player)
{
if (!IsAllowed())
return;
var laws = _siliconLawSystem.GetLaws(player, lawBoundComponent);
_laws = laws.Laws;
_target = player;
StateDirty();
}
public override void HandleMessage(EuiMessageBase msg)
{
if (msg is not SiliconLawsSaveMessage message)
{
return;
}
if (!IsAllowed())
return;
var player = _entityManager.GetEntity(message.Target);
_siliconLawSystem.SetLaws(message.Laws, player);
}
private bool IsAllowed()
{
var adminData = _adminManager.GetAdminData(Player);
if (adminData == null || !adminData.HasFlag(AdminFlags.Moderator))
{
_sawmill.Warning("Player {0} tried to open / use silicon law UI without permission.", Player.UserId);
return false;
}
return true;
}
}

View File

@@ -22,6 +22,7 @@ using Robust.Server.GameObjects;
using Robust.Shared.Player;
using Robust.Shared.Prototypes;
using Robust.Shared.Toolshed;
using Robust.Shared.Utility;
namespace Content.Server.Silicons.Laws;
@@ -278,6 +279,21 @@ public sealed class SiliconLawSystem : SharedSiliconLawSystem
return laws;
}
/// <summary>
/// Set the laws of a silicon entity while notifying the player.
/// </summary>
public void SetLaws(List<SiliconLaw> newLaws, EntityUid target)
{
if (!TryComp<SiliconLawProviderComponent>(target, out var component))
return;
if (component.Lawset == null)
component.Lawset = new SiliconLawset();
component.Lawset.Laws = newLaws;
NotifyLawsChanged(target);
}
}
[ToolshedCommand, AdminCommand(AdminFlags.Admin)]