Add basic PDA/Syndicate Uplink. (#942)
Co-authored-by: FL-OZ <anotherscuffed@gmail.com> Co-authored-by: Pieter-Jan Briers <pieterjan.briers+git@gmail.com>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using Content.Server.Interfaces;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.ViewVariables;
|
||||
@@ -6,16 +7,26 @@ using Robust.Shared.ViewVariables;
|
||||
namespace Content.Server.GameObjects.Components.Access
|
||||
{
|
||||
[RegisterComponent]
|
||||
public class AccessComponent : Component
|
||||
[ComponentReference(typeof(IAccess))]
|
||||
public class AccessComponent : Component, IAccess
|
||||
{
|
||||
public override string Name => "Access";
|
||||
[ViewVariables]
|
||||
public List<string> Tags;
|
||||
private List<string> _tags;
|
||||
public override void ExposeData(ObjectSerializer serializer)
|
||||
{
|
||||
base.ExposeData(serializer);
|
||||
serializer.DataField(ref _tags, "tags", new List<string>());
|
||||
}
|
||||
|
||||
serializer.DataField(ref Tags, "tags", new List<string>());
|
||||
public List<string> GetTags()
|
||||
{
|
||||
return _tags;
|
||||
}
|
||||
|
||||
public void SetTags(List<string> newTags)
|
||||
{
|
||||
_tags = newTags;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Content.Server.Interfaces;
|
||||
using Content.Server.Interfaces.GameObjects;
|
||||
using Content.Shared.GameObjects.Components.Inventory;
|
||||
using JetBrains.Annotations;
|
||||
@@ -26,22 +28,22 @@ namespace Content.Server.GameObjects.Components.Access
|
||||
/// <param name="entity">The entity to be searched for access.</param>
|
||||
public bool IsAllowed(IEntity entity)
|
||||
{
|
||||
var accessProvider = FindAccessProvider(entity);
|
||||
return accessProvider != null && IsAllowed(accessProvider);
|
||||
var tags = FindAccessTags(entity);
|
||||
return tags != null && IsAllowed(tags);
|
||||
}
|
||||
|
||||
private bool IsAllowed(AccessComponent accessProvider)
|
||||
private bool IsAllowed(List<string> accessTags)
|
||||
{
|
||||
foreach (var sufficient in _sufficientTags)
|
||||
{
|
||||
if (accessProvider.Tags.Contains(sufficient))
|
||||
if (accessTags.Contains(sufficient))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
foreach (var necessary in _necessaryTags)
|
||||
{
|
||||
if (!accessProvider.Tags.Contains(necessary))
|
||||
if (!accessTags.Contains(necessary))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -50,20 +52,20 @@ namespace Content.Server.GameObjects.Components.Access
|
||||
}
|
||||
|
||||
[CanBeNull]
|
||||
private static AccessComponent FindAccessProvider(IEntity entity)
|
||||
private static List<string> FindAccessTags(IEntity entity)
|
||||
{
|
||||
if (entity.TryGetComponent(out AccessComponent accessComponent))
|
||||
if (entity.TryGetComponent(out IAccess accessComponent))
|
||||
{
|
||||
return accessComponent;
|
||||
return accessComponent.GetTags();
|
||||
}
|
||||
|
||||
if (entity.TryGetComponent(out IHandsComponent handsComponent))
|
||||
{
|
||||
var activeHandEntity = handsComponent.GetActiveHand?.Owner;
|
||||
if (activeHandEntity != null &&
|
||||
activeHandEntity.TryGetComponent(out AccessComponent handAccessComponent))
|
||||
activeHandEntity.TryGetComponent(out IAccess handAccessComponent))
|
||||
{
|
||||
return handAccessComponent;
|
||||
return handAccessComponent.GetTags();
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -74,10 +76,10 @@ namespace Content.Server.GameObjects.Components.Access
|
||||
{
|
||||
if (inventoryComponent.HasSlot(EquipmentSlotDefines.Slots.IDCARD) &&
|
||||
inventoryComponent.TryGetSlotItem(EquipmentSlotDefines.Slots.IDCARD, out ItemComponent item) &&
|
||||
item.Owner.TryGetComponent(out AccessComponent idAccessComponent)
|
||||
item.Owner.TryGetComponent(out IAccess idAccessComponent)
|
||||
)
|
||||
{
|
||||
return idAccessComponent;
|
||||
return idAccessComponent.GetTags();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
||||
@@ -101,7 +101,7 @@ namespace Content.Server.GameObjects.Components.Access
|
||||
return;
|
||||
}
|
||||
var targetIdAccess = targetIdEntity.GetComponent<AccessComponent>();
|
||||
targetIdAccess.Tags = newAccessList;
|
||||
targetIdAccess.SetTags(newAccessList);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -180,7 +180,7 @@ namespace Content.Server.GameObjects.Components.Access
|
||||
true,
|
||||
targetIdComponent.FullName,
|
||||
targetIdComponent.JobTitle,
|
||||
targetAccessComponent.Tags,
|
||||
targetAccessComponent.GetTags(),
|
||||
_privilegedIdContainer.ContainedEntity?.Name ?? "",
|
||||
_targetIdContainer.ContainedEntity?.Name ?? "");
|
||||
}
|
||||
|
||||
254
Content.Server/GameObjects/Components/PDA/PDAComponent.cs
Normal file
254
Content.Server/GameObjects/Components/PDA/PDAComponent.cs
Normal file
@@ -0,0 +1,254 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Content.Server.GameObjects.Components.Access;
|
||||
using Content.Server.GameObjects.EntitySystems;
|
||||
using Content.Server.Interfaces;
|
||||
using Content.Server.Interfaces.PDA;
|
||||
using Content.Shared.GameObjects;
|
||||
using Content.Shared.GameObjects.Components.PDA;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Server.GameObjects.Components.Container;
|
||||
using Robust.Server.GameObjects.Components.UserInterface;
|
||||
using Robust.Server.Interfaces.GameObjects;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
using Robust.Shared.Interfaces.Network;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Players;
|
||||
using Robust.Shared.Serialization;
|
||||
|
||||
namespace Content.Server.GameObjects.Components.PDA
|
||||
{
|
||||
[RegisterComponent]
|
||||
[ComponentReference(typeof(IActivate))]
|
||||
[ComponentReference(typeof(IAccess))]
|
||||
public class PDAComponent : SharedPDAComponent, IInteractUsing, IActivate, IUse, IAccess
|
||||
{
|
||||
#pragma warning disable 649
|
||||
[Dependency] protected readonly IPDAUplinkManager _uplinkManager;
|
||||
[Dependency] protected readonly IEntityManager _entityManager;
|
||||
#pragma warning restore 649
|
||||
|
||||
private Container _idSlot;
|
||||
private PointLightComponent _pdaLight;
|
||||
private bool _lightOn = false;
|
||||
private BoundUserInterface _interface;
|
||||
private string _startingIdCard;
|
||||
public bool IdSlotEmpty => _idSlot.ContainedEntities.Count < 1;
|
||||
public IEntity OwnerMob { get; private set; }
|
||||
|
||||
public IdCardComponent ContainedID { get; private set; }
|
||||
|
||||
private AppearanceComponent _appearance;
|
||||
|
||||
private UplinkAccount _syndicateUplinkAccount;
|
||||
|
||||
public override void ExposeData(ObjectSerializer serializer)
|
||||
{
|
||||
base.ExposeData(serializer);
|
||||
serializer.DataField(ref _startingIdCard, "idCard", "AssistantIDCard");
|
||||
}
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
_idSlot = ContainerManagerComponent.Ensure<Container>("pda_entity_container", Owner, out var existed);
|
||||
_pdaLight = Owner.GetComponent<PointLightComponent>();
|
||||
_appearance = Owner.GetComponent<AppearanceComponent>();
|
||||
_interface = Owner.GetComponent<ServerUserInterfaceComponent>()
|
||||
.GetBoundUserInterface(PDAUiKey.Key);
|
||||
_interface.OnReceiveMessage += UserInterfaceOnReceiveMessage;
|
||||
var idCard = _entityManager.SpawnEntity(_startingIdCard, Owner.Transform.GridPosition);
|
||||
var idCardComponent = idCard.GetComponent<IdCardComponent>();
|
||||
InsertIdCard(idCardComponent);
|
||||
UpdatePDAAppearance();
|
||||
}
|
||||
|
||||
private void UserInterfaceOnReceiveMessage(ServerBoundUserInterfaceMessage message)
|
||||
{
|
||||
switch (message.Message)
|
||||
{
|
||||
case PDARequestUpdateInterfaceMessage msg:
|
||||
{
|
||||
UpdatePDAUserInterface();
|
||||
break;
|
||||
}
|
||||
case PDAToggleFlashlightMessage msg:
|
||||
{
|
||||
ToggleLight();
|
||||
break;
|
||||
}
|
||||
|
||||
case PDAEjectIDMessage msg:
|
||||
{
|
||||
HandleIDEjection(message.Session.AttachedEntity);
|
||||
break;
|
||||
}
|
||||
|
||||
case PDAUplinkBuyListingMessage buyMsg:
|
||||
{
|
||||
|
||||
if (!_uplinkManager.TryPurchaseItem(_syndicateUplinkAccount, buyMsg.ListingToBuy))
|
||||
{
|
||||
//TODO: Send a message that tells the buyer they are too poor or something.
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdatePDAUserInterface()
|
||||
{
|
||||
var ownerInfo = new PDAIdInfoText
|
||||
{
|
||||
ActualOwnerName = OwnerMob?.Name,
|
||||
IdOwner = ContainedID?.FullName,
|
||||
JobTitle = ContainedID?.JobTitle
|
||||
};
|
||||
|
||||
//Do we have an account? If so provide the info.
|
||||
if (_syndicateUplinkAccount != null)
|
||||
{
|
||||
var accData = new UplinkAccountData(_syndicateUplinkAccount.AccountHolder, _syndicateUplinkAccount.Balance);
|
||||
var listings = _uplinkManager.FetchListings.ToArray();
|
||||
_interface.SetState(new PDAUpdateState(_lightOn,ownerInfo,accData,listings));
|
||||
}
|
||||
else
|
||||
{
|
||||
_interface.SetState(new PDAUpdateState(_lightOn,ownerInfo));
|
||||
}
|
||||
|
||||
UpdatePDAAppearance();
|
||||
}
|
||||
|
||||
private void UpdatePDAAppearance()
|
||||
{
|
||||
_appearance?.SetData(PDAVisuals.ScreenLit, _lightOn);
|
||||
}
|
||||
|
||||
public bool InteractUsing(InteractUsingEventArgs eventArgs)
|
||||
{
|
||||
var item = eventArgs.Using;
|
||||
if (!IdSlotEmpty)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!item.TryGetComponent<IdCardComponent>(out var idCardComponent) || _idSlot.Contains(item))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
InsertIdCard(idCardComponent);
|
||||
UpdatePDAUserInterface();
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
void IActivate.Activate(ActivateEventArgs eventArgs)
|
||||
{
|
||||
if (!eventArgs.User.TryGetComponent(out IActorComponent actor))
|
||||
{
|
||||
return;
|
||||
}
|
||||
_interface.Open(actor.playerSession);
|
||||
UpdatePDAAppearance();
|
||||
}
|
||||
|
||||
public bool UseEntity(UseEntityEventArgs eventArgs)
|
||||
{
|
||||
if (!eventArgs.User.TryGetComponent(out IActorComponent actor))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
_interface.Open(actor.playerSession);
|
||||
UpdatePDAAppearance();
|
||||
return true;
|
||||
}
|
||||
|
||||
public void SetPDAOwner(IEntity mob)
|
||||
{
|
||||
if (mob == OwnerMob)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
OwnerMob = mob;
|
||||
UpdatePDAUserInterface();
|
||||
}
|
||||
|
||||
private void InsertIdCard(IdCardComponent card)
|
||||
{
|
||||
_idSlot.Insert(card.Owner);
|
||||
ContainedID = card;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the PDA's syndicate uplink account.
|
||||
/// </summary>
|
||||
/// <param name="acc"></param>
|
||||
public void InitUplinkAccount(UplinkAccount acc)
|
||||
{
|
||||
_syndicateUplinkAccount = acc;
|
||||
_uplinkManager.AddNewAccount(_syndicateUplinkAccount);
|
||||
|
||||
_syndicateUplinkAccount.BalanceChanged += account =>
|
||||
{
|
||||
UpdatePDAUserInterface();
|
||||
};
|
||||
|
||||
UpdatePDAUserInterface();
|
||||
}
|
||||
|
||||
private void ToggleLight()
|
||||
{
|
||||
_lightOn = !_lightOn;
|
||||
_pdaLight.Enabled = _lightOn;
|
||||
UpdatePDAUserInterface();
|
||||
}
|
||||
|
||||
private void HandleIDEjection(IEntity pdaUser)
|
||||
{
|
||||
if (IdSlotEmpty)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var cardEntity = ContainedID.Owner;
|
||||
_idSlot.Remove(cardEntity);
|
||||
|
||||
var hands = pdaUser.GetComponent<HandsComponent>();
|
||||
var cardItemComponent = cardEntity.GetComponent<ItemComponent>();
|
||||
hands.PutInHandOrDrop(cardItemComponent);
|
||||
ContainedID = null;
|
||||
UpdatePDAUserInterface();
|
||||
}
|
||||
|
||||
[Verb]
|
||||
public sealed class EjectIDVerb : Verb<PDAComponent>
|
||||
{
|
||||
protected override void GetData(IEntity user, PDAComponent component, VerbData data)
|
||||
{
|
||||
data.Text = Loc.GetString("Eject ID");
|
||||
data.Visibility = component.IdSlotEmpty ? VerbVisibility.Invisible : VerbVisibility.Visible;
|
||||
}
|
||||
|
||||
protected override void Activate(IEntity user, PDAComponent component)
|
||||
{
|
||||
component.HandleIDEjection(user);
|
||||
}
|
||||
}
|
||||
|
||||
List<string> IAccess.GetTags()
|
||||
{
|
||||
return ContainedID?.Owner.GetComponent<AccessComponent>()?.GetTags();
|
||||
}
|
||||
|
||||
void IAccess.SetTags(List<string> newTags)
|
||||
{
|
||||
ContainedID?.Owner.GetComponent<AccessComponent>().SetTags(newTags);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user