2020-08-22 22:29:20 +02:00
|
|
|
using System.Collections.Generic;
|
2020-06-03 11:46:59 +02:00
|
|
|
using System.Linq;
|
2020-09-16 14:57:14 -07:00
|
|
|
using System.Threading.Tasks;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Server.Power.Components;
|
|
|
|
|
using Content.Server.UserInterface;
|
2019-09-06 08:12:44 +02:00
|
|
|
using Content.Shared.Access;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Shared.Acts;
|
2021-10-05 14:29:03 +11:00
|
|
|
using Content.Shared.Hands.Components;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Shared.Interaction;
|
2021-09-26 15:18:45 +02:00
|
|
|
using Content.Shared.Popups;
|
2021-02-11 01:13:03 -08:00
|
|
|
using Robust.Server.GameObjects;
|
2021-03-01 15:24:46 -08:00
|
|
|
using Robust.Shared.Containers;
|
2019-09-06 08:12:44 +02:00
|
|
|
using Robust.Shared.GameObjects;
|
|
|
|
|
using Robust.Shared.IoC;
|
|
|
|
|
using Robust.Shared.Localization;
|
|
|
|
|
using Robust.Shared.Log;
|
2020-01-19 18:29:49 +01:00
|
|
|
using Robust.Shared.Prototypes;
|
2020-08-22 22:29:20 +02:00
|
|
|
using Robust.Shared.ViewVariables;
|
2019-09-06 08:12:44 +02:00
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Server.Access.Components
|
2019-09-06 08:12:44 +02:00
|
|
|
{
|
|
|
|
|
[RegisterComponent]
|
|
|
|
|
[ComponentReference(typeof(IActivate))]
|
2020-10-08 17:41:23 +02:00
|
|
|
public class IdCardConsoleComponent : SharedIdCardConsoleComponent, IActivate, IInteractUsing, IBreakAct
|
2019-09-06 08:12:44 +02:00
|
|
|
{
|
2020-08-22 22:29:20 +02:00
|
|
|
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
2019-09-06 08:12:44 +02:00
|
|
|
|
2021-10-05 14:29:03 +11:00
|
|
|
public ContainerSlot PrivilegedIdContainer = default!;
|
|
|
|
|
public ContainerSlot TargetIdContainer = default!;
|
2020-08-22 22:29:20 +02:00
|
|
|
|
2020-08-24 20:47:17 +02:00
|
|
|
[ViewVariables] private BoundUserInterface? UserInterface => Owner.GetUIOrNull(IdCardConsoleUiKey.Key);
|
2021-07-04 18:11:52 +02:00
|
|
|
[ViewVariables] private bool Powered => !Owner.TryGetComponent(out ApcPowerReceiverComponent? receiver) || receiver.Powered;
|
2019-09-06 08:12:44 +02:00
|
|
|
|
2021-10-05 14:29:03 +11:00
|
|
|
public bool PrivilegedIDEmpty => PrivilegedIdContainer.ContainedEntities.Count < 1;
|
|
|
|
|
public bool TargetIDEmpty => TargetIdContainer.ContainedEntities.Count < 1;
|
2020-09-16 14:57:14 -07:00
|
|
|
|
2021-06-19 19:41:26 -07:00
|
|
|
protected override void Initialize()
|
2019-09-06 08:12:44 +02:00
|
|
|
{
|
|
|
|
|
base.Initialize();
|
|
|
|
|
|
2021-10-05 14:29:03 +11:00
|
|
|
PrivilegedIdContainer = ContainerHelpers.EnsureContainer<ContainerSlot>(Owner, $"{Name}-privilegedId");
|
|
|
|
|
TargetIdContainer = ContainerHelpers.EnsureContainer<ContainerSlot>(Owner, $"{Name}-targetId");
|
2019-09-06 08:12:44 +02:00
|
|
|
|
2020-12-04 13:26:54 +01:00
|
|
|
Owner.EnsureComponentWarn<AccessReader>();
|
|
|
|
|
Owner.EnsureComponentWarn<ServerUserInterfaceComponent>();
|
2020-08-22 22:29:20 +02:00
|
|
|
|
2020-12-04 13:26:54 +01:00
|
|
|
if (UserInterface != null)
|
2020-08-22 22:29:20 +02:00
|
|
|
{
|
|
|
|
|
UserInterface.OnReceiveMessage += OnUiReceiveMessage;
|
|
|
|
|
}
|
2019-09-06 08:12:44 +02:00
|
|
|
|
|
|
|
|
UpdateUserInterface();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnUiReceiveMessage(ServerBoundUserInterfaceMessage obj)
|
|
|
|
|
{
|
2020-08-22 22:29:20 +02:00
|
|
|
if (obj.Session.AttachedEntity == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-06 08:12:44 +02:00
|
|
|
switch (obj.Message)
|
|
|
|
|
{
|
|
|
|
|
case IdButtonPressedMessage msg:
|
|
|
|
|
switch (msg.Button)
|
|
|
|
|
{
|
|
|
|
|
case UiButton.PrivilegedId:
|
2021-10-05 14:29:03 +11:00
|
|
|
HandleId(obj.Session.AttachedEntity, PrivilegedIdContainer);
|
2019-09-06 08:12:44 +02:00
|
|
|
break;
|
|
|
|
|
case UiButton.TargetId:
|
2021-10-05 14:29:03 +11:00
|
|
|
HandleId(obj.Session.AttachedEntity, TargetIdContainer);
|
2019-09-06 08:12:44 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case WriteToTargetIdMessage msg:
|
|
|
|
|
TryWriteToTargetId(msg.FullName, msg.JobTitle, msg.AccessList);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-10-21 22:54:16 +02:00
|
|
|
|
|
|
|
|
UpdateUserInterface();
|
2019-09-06 08:12:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-10-05 14:29:03 +11:00
|
|
|
/// Returns true if there is an ID in <see cref="PrivilegedIdContainer"/> and said ID satisfies the requirements of <see cref="AccessReader"/>.
|
2019-09-06 08:12:44 +02:00
|
|
|
/// </summary>
|
|
|
|
|
private bool PrivilegedIdIsAuthorized()
|
|
|
|
|
{
|
2020-08-22 22:29:20 +02:00
|
|
|
if (!Owner.TryGetComponent(out AccessReader? reader))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-05 14:29:03 +11:00
|
|
|
var privilegedIdEntity = PrivilegedIdContainer.ContainedEntity;
|
2020-08-22 22:29:20 +02:00
|
|
|
return privilegedIdEntity != null && reader.IsAllowed(privilegedIdEntity);
|
2019-09-06 08:12:44 +02:00
|
|
|
}
|
2020-08-22 22:29:20 +02:00
|
|
|
|
2019-09-06 08:12:44 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Called when the "Submit" button in the UI gets pressed.
|
2021-10-05 14:29:03 +11:00
|
|
|
/// Writes data passed from the UI into the ID stored in <see cref="TargetIdContainer"/>, if present.
|
2019-09-06 08:12:44 +02:00
|
|
|
/// </summary>
|
|
|
|
|
private void TryWriteToTargetId(string newFullName, string newJobTitle, List<string> newAccessList)
|
|
|
|
|
{
|
2021-10-05 14:29:03 +11:00
|
|
|
if (!PrivilegedIdIsAuthorized() || TargetIdContainer.ContainedEntity == null)
|
2019-09-06 08:12:44 +02:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-05 14:29:03 +11:00
|
|
|
var targetIdEntity = TargetIdContainer.ContainedEntity;
|
2019-09-06 08:12:44 +02:00
|
|
|
|
|
|
|
|
var targetIdComponent = targetIdEntity.GetComponent<IdCardComponent>();
|
|
|
|
|
targetIdComponent.FullName = newFullName;
|
|
|
|
|
targetIdComponent.JobTitle = newJobTitle;
|
|
|
|
|
|
2020-01-19 18:29:49 +01:00
|
|
|
if (!newAccessList.TrueForAll(x => _prototypeManager.HasIndex<AccessLevelPrototype>(x)))
|
2019-09-06 08:12:44 +02:00
|
|
|
{
|
2020-09-13 14:23:52 +02:00
|
|
|
Logger.Warning("Tried to write unknown access tag.");
|
2019-09-06 08:12:44 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var targetIdAccess = targetIdEntity.GetComponent<AccessComponent>();
|
2020-05-28 06:22:47 -05:00
|
|
|
targetIdAccess.SetTags(newAccessList);
|
2019-09-06 08:12:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Called when one of the insert/remove ID buttons gets pressed.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void HandleId(IEntity user, ContainerSlot container)
|
|
|
|
|
{
|
2021-10-05 14:29:03 +11:00
|
|
|
if (!user.TryGetComponent(out SharedHandsComponent? hands))
|
2019-09-06 08:12:44 +02:00
|
|
|
{
|
2021-06-21 02:13:54 +02:00
|
|
|
Owner.PopupMessage(user, Loc.GetString("access-id-card-console-component-no-hands-error"));
|
2019-09-06 08:12:44 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (container.ContainedEntity == null)
|
|
|
|
|
{
|
|
|
|
|
InsertIdFromHand(user, container, hands);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
PutIdInHand(container, hands);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-05 14:29:03 +11:00
|
|
|
public void InsertIdFromHand(IEntity user, ContainerSlot container, SharedHandsComponent hands)
|
2019-09-06 08:12:44 +02:00
|
|
|
{
|
2021-10-05 14:29:03 +11:00
|
|
|
if (!hands.TryGetActiveHeldEntity(out var heldEntity))
|
2019-09-06 08:12:44 +02:00
|
|
|
return;
|
2020-08-22 22:29:20 +02:00
|
|
|
|
2021-10-05 14:29:03 +11:00
|
|
|
if (!heldEntity.HasComponent<IdCardComponent>())
|
2020-08-22 22:29:20 +02:00
|
|
|
return;
|
|
|
|
|
|
2021-10-05 14:29:03 +11:00
|
|
|
if (!hands.TryPutHandIntoContainer(hands.ActiveHand!, container))
|
2019-09-06 08:12:44 +02:00
|
|
|
{
|
2021-06-21 02:13:54 +02:00
|
|
|
Owner.PopupMessage(user, Loc.GetString("access-id-card-console-component-cannot-let-go-error"));
|
2019-09-06 08:12:44 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
UpdateUserInterface();
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-05 14:29:03 +11:00
|
|
|
public void PutIdInHand(ContainerSlot container, SharedHandsComponent hands)
|
2019-09-06 08:12:44 +02:00
|
|
|
{
|
|
|
|
|
var idEntity = container.ContainedEntity;
|
|
|
|
|
if (idEntity == null || !container.Remove(idEntity))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
UpdateUserInterface();
|
|
|
|
|
|
2021-10-05 14:29:03 +11:00
|
|
|
hands.TryPutInActiveHandOrAny(idEntity);
|
2019-09-06 08:12:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateUserInterface()
|
|
|
|
|
{
|
2021-10-05 14:29:03 +11:00
|
|
|
var isPrivilegedIdPresent = PrivilegedIdContainer.ContainedEntity != null;
|
|
|
|
|
var targetIdEntity = TargetIdContainer.ContainedEntity;
|
2019-09-06 08:12:44 +02:00
|
|
|
IdCardConsoleBoundUserInterfaceState newState;
|
|
|
|
|
// this could be prettier
|
|
|
|
|
if (targetIdEntity == null)
|
|
|
|
|
{
|
|
|
|
|
newState = new IdCardConsoleBoundUserInterfaceState(
|
|
|
|
|
isPrivilegedIdPresent,
|
|
|
|
|
PrivilegedIdIsAuthorized(),
|
|
|
|
|
false,
|
|
|
|
|
null,
|
|
|
|
|
null,
|
2019-10-21 22:54:16 +02:00
|
|
|
null,
|
2021-10-05 14:29:03 +11:00
|
|
|
PrivilegedIdContainer.ContainedEntity?.Name ?? string.Empty,
|
|
|
|
|
TargetIdContainer.ContainedEntity?.Name ?? string.Empty);
|
2019-09-06 08:12:44 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var targetIdComponent = targetIdEntity.GetComponent<IdCardComponent>();
|
|
|
|
|
var targetAccessComponent = targetIdEntity.GetComponent<AccessComponent>();
|
|
|
|
|
newState = new IdCardConsoleBoundUserInterfaceState(
|
|
|
|
|
isPrivilegedIdPresent,
|
|
|
|
|
PrivilegedIdIsAuthorized(),
|
|
|
|
|
true,
|
|
|
|
|
targetIdComponent.FullName,
|
|
|
|
|
targetIdComponent.JobTitle,
|
2020-06-03 11:46:59 +02:00
|
|
|
targetAccessComponent.Tags.ToArray(),
|
2021-10-05 14:29:03 +11:00
|
|
|
PrivilegedIdContainer.ContainedEntity?.Name ?? string.Empty,
|
|
|
|
|
TargetIdContainer.ContainedEntity?.Name ?? string.Empty);
|
2019-09-06 08:12:44 +02:00
|
|
|
}
|
2020-08-22 22:29:20 +02:00
|
|
|
UserInterface?.SetState(newState);
|
2019-09-06 08:12:44 +02:00
|
|
|
}
|
|
|
|
|
|
2021-02-04 17:44:49 +01:00
|
|
|
void IActivate.Activate(ActivateEventArgs eventArgs)
|
2019-09-06 08:12:44 +02:00
|
|
|
{
|
2021-06-21 02:21:20 -07:00
|
|
|
if (!eventArgs.User.TryGetComponent(out ActorComponent? actor))
|
2019-09-06 08:12:44 +02:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-06-21 02:21:20 -07:00
|
|
|
if (!Powered) return;
|
2019-09-06 08:12:44 +02:00
|
|
|
|
2021-05-12 13:42:18 +02:00
|
|
|
UserInterface?.Open(actor.PlayerSession);
|
2019-09-06 08:12:44 +02:00
|
|
|
}
|
2020-09-16 14:57:14 -07:00
|
|
|
|
2021-02-04 17:44:49 +01:00
|
|
|
async Task<bool> IInteractUsing.InteractUsing(InteractUsingEventArgs eventArgs)
|
2020-09-16 14:57:14 -07:00
|
|
|
{
|
|
|
|
|
var item = eventArgs.Using;
|
|
|
|
|
var user = eventArgs.User;
|
|
|
|
|
|
|
|
|
|
if (!PrivilegedIDEmpty && !TargetIDEmpty)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-05 14:29:03 +11:00
|
|
|
if (!item.HasComponent<IdCardComponent>() || !user.TryGetComponent(out SharedHandsComponent? hand))
|
2020-09-16 14:57:14 -07:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (PrivilegedIDEmpty)
|
|
|
|
|
{
|
2021-10-05 14:29:03 +11:00
|
|
|
InsertIdFromHand(user, PrivilegedIdContainer, hand);
|
2020-09-16 14:57:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (TargetIDEmpty)
|
|
|
|
|
{
|
2021-10-05 14:29:03 +11:00
|
|
|
InsertIdFromHand(user, TargetIdContainer, hand);
|
2020-09-16 14:57:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UpdateUserInterface();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-08 17:41:23 +02:00
|
|
|
public void OnBreak(BreakageEventArgs eventArgs)
|
|
|
|
|
{
|
2021-10-05 14:29:03 +11:00
|
|
|
var privileged = PrivilegedIdContainer.ContainedEntity;
|
2020-10-08 17:41:23 +02:00
|
|
|
if (privileged != null)
|
2021-10-05 14:29:03 +11:00
|
|
|
PrivilegedIdContainer.Remove(privileged);
|
2020-09-16 14:57:14 -07:00
|
|
|
|
2021-10-05 14:29:03 +11:00
|
|
|
var target = TargetIdContainer.ContainedEntity;
|
2020-10-08 17:41:23 +02:00
|
|
|
if (target != null)
|
2021-10-05 14:29:03 +11:00
|
|
|
TargetIdContainer.Remove(target);
|
2020-10-08 17:41:23 +02:00
|
|
|
}
|
2019-09-06 08:12:44 +02:00
|
|
|
}
|
|
|
|
|
}
|