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:
FL-OZ
2020-05-28 06:22:47 -05:00
committed by GitHub
parent 0171c3bd93
commit 4c20a504a5
117 changed files with 1569 additions and 41 deletions

View File

@@ -2,6 +2,7 @@
using Content.Server.Interfaces;
using Content.Server.Interfaces.Chat;
using Content.Server.Interfaces.GameTicking;
using Content.Server.Interfaces.PDA;
using Content.Server.Preferences;
using Content.Server.Sandbox;
using Content.Shared.Kitchen;
@@ -84,6 +85,7 @@ namespace Content.Server
IoCManager.Resolve<ISandboxManager>().Initialize();
IoCManager.Resolve<IServerPreferencesManager>().FinishInit();
IoCManager.Resolve<RecipeManager>().Initialize();
IoCManager.Resolve<IPDAUplinkManager>().Initialize();
}
public override void Update(ModUpdateLevel level, FrameEventArgs frameEventArgs)

View File

@@ -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;
}
}
}

View File

@@ -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;

View File

@@ -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 ?? "");
}

View 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);
}
}
}

View File

@@ -7,6 +7,7 @@ using Content.Server.GameObjects.Components.Access;
using Content.Server.GameObjects.Components.Markers;
using Content.Server.GameObjects.Components.Mobs;
using Content.Server.GameObjects.Components.Observer;
using Content.Server.GameObjects.Components.PDA;
using Content.Server.GameObjects.EntitySystems;
using Content.Server.GameTicking.GamePresets;
using Content.Server.Interfaces;
@@ -17,6 +18,7 @@ using Content.Server.Mobs.Roles;
using Content.Server.Players;
using Content.Shared;
using Content.Shared.Chat;
using Content.Shared.GameObjects.Components.PDA;
using Content.Shared.Jobs;
using Content.Shared.Preferences;
using Robust.Server.Interfaces;
@@ -627,20 +629,38 @@ namespace Content.Server.GameTicking
{
var inventory = mob.GetComponent<InventoryComponent>();
if (!inventory.TryGetSlotItem(Slots.IDCARD, out ItemComponent cardItem))
if (!inventory.TryGetSlotItem(Slots.IDCARD, out ItemComponent pdaItem))
{
return;
}
var card = cardItem.Owner;
var pda = pdaItem.Owner;
var cardComponent = card.GetComponent<IdCardComponent>();
cardComponent.FullName = characterName;
cardComponent.JobTitle = jobPrototype.Name;
var pdaComponent = pda.GetComponent<PDAComponent>();
if (pdaComponent.IdSlotEmpty)
{
return;
}
var card = pdaComponent.ContainedID;
card.FullName = characterName;
card.JobTitle = jobPrototype.Name;
var access = card.Owner.GetComponent<AccessComponent>();
var accessTags = access.GetTags();
accessTags.AddRange(jobPrototype.Access);
access.SetTags(accessTags);
pdaComponent.SetPDAOwner(mob);
var mindComponent = mob.GetComponent<MindComponent>();
if (mindComponent.HasMind)//Redundancy checks.
{
if (mindComponent.Mind.AllRoles.Any(role => role.Antag)) //Give antags a new uplinkaccount.
{
var uplinkAccount = new UplinkAccount(mob.Uid, 20); //TODO: make me into a variable based on server pop or something.
pdaComponent.InitUplinkAccount(uplinkAccount);
}
}
var access = card.GetComponent<AccessComponent>();
access.Tags.Clear();
access.Tags.AddRange(jobPrototype.Access);
}
private void AddManifestEntry(string characterName, string jobId)

View File

@@ -0,0 +1,11 @@
using System.Collections.Generic;
namespace Content.Server.Interfaces
{
public interface IAccess
{
public List<string> GetTags();
public void SetTags(List<string> newTags);
}
}

View File

@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using Content.Shared.GameObjects.Components.PDA;
using Content.Shared.Prototypes.PDA;
namespace Content.Server.Interfaces.PDA
{
public interface IPDAUplinkManager
{
public IReadOnlyList<UplinkListingData> FetchListings => null;
void Initialize();
public bool AddNewAccount(UplinkAccount acc);
public bool ChangeBalance(UplinkAccount acc, int amt);
public bool TryPurchaseItem(UplinkAccount acc, UplinkListingData listing);
}
}

View File

@@ -0,0 +1,107 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Content.Server.GameObjects;
using Content.Server.GameObjects.Components.Mobs;
using Content.Server.Interfaces.PDA;
using Content.Shared.GameObjects.Components.PDA;
using Content.Shared.Prototypes.PDA;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Prototypes;
namespace Content.Server.PDA
{
public class PDAUplinkManager : IPDAUplinkManager
{
#pragma warning disable 649
[Dependency] private readonly IPrototypeManager _prototypeManager;
[Dependency] private readonly IEntityManager _entityManager;
#pragma warning restore 649
private List<UplinkAccount> _accounts;
private List<UplinkListingData> _listings;
public IReadOnlyList<UplinkListingData> FetchListings => _listings;
public void Initialize()
{
_listings = new List<UplinkListingData>();
foreach (var item in _prototypeManager.EnumeratePrototypes<UplinkStoreListingPrototype>())
{
var newListing = new UplinkListingData(item.ListingName, item.ItemId, item.Price, item.Category,
item.Description, item.DisplayColor);
RegisterUplinkListing(newListing);
}
_accounts = new List<UplinkAccount>();
}
private void RegisterUplinkListing(UplinkListingData listing)
{
if (!ContainsListing(listing))
{
_listings.Add(listing);
}
}
private bool ContainsListing(UplinkListingData listing)
{
return _listings.Any(otherListing => listing.Equals(otherListing));
}
public bool AddNewAccount(UplinkAccount acc)
{
var entity = _entityManager.GetEntity(acc.AccountHolder);
if (entity.TryGetComponent(out MindComponent mindComponent))
{
if (mindComponent.Mind.AllRoles.Any(role => !role.Antag))
{
return false;
}
}
if (_accounts.Contains(acc))
{
return false;
}
_accounts.Add(acc);
return true;
}
public bool ChangeBalance(UplinkAccount acc, int amt)
{
var account = _accounts.Find(uplinkAccount => uplinkAccount.AccountHolder == acc.AccountHolder);
if (account != null && account.Balance + amt < 0)
{
return false;
}
account.ModifyAccountBalance(account.Balance + amt);
return true;
}
public bool TryPurchaseItem(UplinkAccount acc, UplinkListingData listing)
{
if (acc == null || listing == null)
{
return false;
}
if (!ContainsListing(listing) || acc.Balance < listing.Price)
{
return false;
}
var player = _entityManager.GetEntity(acc.AccountHolder);
var hands = player.GetComponent<HandsComponent>();
hands.PutInHandOrDrop(_entityManager.SpawnEntity(listing.ItemId,
player.Transform.GridPosition).GetComponent<ItemComponent>());
return ChangeBalance(acc, -listing.Price);
}
}
}

View File

@@ -4,6 +4,8 @@ using Content.Server.GameTicking;
using Content.Server.Interfaces;
using Content.Server.Interfaces.Chat;
using Content.Server.Interfaces.GameTicking;
using Content.Server.Interfaces.PDA;
using Content.Server.PDA;
using Content.Server.Preferences;
using Content.Server.Sandbox;
using Content.Server.Utility;
@@ -30,6 +32,7 @@ namespace Content.Server
IoCManager.Register<IModuleManager, ServerModuleManager>();
IoCManager.Register<IServerPreferencesManager, ServerPreferencesManager>();
IoCManager.Register<RecipeManager, RecipeManager>();
IoCManager.Register<IPDAUplinkManager,PDAUplinkManager>();
}
}
}