2022-08-17 00:34:25 -04:00
|
|
|
using Content.Server.Store.Systems;
|
2022-03-17 20:13:31 +13:00
|
|
|
using Content.Shared.Hands.EntitySystems;
|
2021-12-30 22:56:10 +01:00
|
|
|
using Content.Shared.Inventory;
|
2021-12-16 23:42:02 +13:00
|
|
|
using Content.Shared.PDA;
|
2022-08-17 00:34:25 -04:00
|
|
|
using Content.Server.Store.Components;
|
|
|
|
|
using Content.Shared.FixedPoint;
|
2023-08-13 20:26:59 -04:00
|
|
|
using Content.Shared.Store;
|
2021-10-03 07:05:52 +03:00
|
|
|
|
2021-10-08 13:26:42 +03:00
|
|
|
namespace Content.Server.Traitor.Uplink
|
2021-10-03 07:05:52 +03:00
|
|
|
{
|
2022-02-16 00:23:23 -07:00
|
|
|
public sealed class UplinkSystem : EntitySystem
|
2021-10-03 07:05:52 +03:00
|
|
|
{
|
2021-12-30 22:56:10 +01:00
|
|
|
[Dependency] private readonly InventorySystem _inventorySystem = default!;
|
2022-03-17 20:13:31 +13:00
|
|
|
[Dependency] private readonly SharedHandsSystem _handsSystem = default!;
|
2022-08-17 00:34:25 -04:00
|
|
|
[Dependency] private readonly StoreSystem _store = default!;
|
2021-12-30 22:56:10 +01:00
|
|
|
|
2023-08-13 20:26:59 -04:00
|
|
|
[ValidatePrototypeId<CurrencyPrototype>]
|
2022-08-17 00:34:25 -04:00
|
|
|
public const string TelecrystalCurrencyPrototype = "Telecrystal";
|
2021-10-03 07:05:52 +03:00
|
|
|
|
2022-08-17 00:34:25 -04:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the amount of TC on an "uplink"
|
|
|
|
|
/// Mostly just here for legacy systems based on uplink.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="component"></param>
|
|
|
|
|
/// <returns>the amount of TC</returns>
|
|
|
|
|
public int GetTCBalance(StoreComponent component)
|
2021-10-03 07:05:52 +03:00
|
|
|
{
|
2022-08-17 00:34:25 -04:00
|
|
|
FixedPoint2? tcBalance = component.Balance.GetValueOrDefault(TelecrystalCurrencyPrototype);
|
2023-02-12 07:39:14 -05:00
|
|
|
return tcBalance?.Int() ?? 0;
|
2021-10-03 07:05:52 +03:00
|
|
|
}
|
|
|
|
|
|
2022-08-17 00:34:25 -04:00
|
|
|
/// <summary>
|
|
|
|
|
/// Adds an uplink to the target
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="user">The person who is getting the uplink</param>
|
|
|
|
|
/// <param name="balance">The amount of currency on the uplink. If null, will just use the amount specified in the preset.</param>
|
|
|
|
|
/// <param name="uplinkPresetId">The id of the storepreset</param>
|
|
|
|
|
/// <param name="uplinkEntity">The entity that will actually have the uplink functionality. Defaults to the PDA if null.</param>
|
|
|
|
|
/// <returns>Whether or not the uplink was added successfully</returns>
|
|
|
|
|
public bool AddUplink(EntityUid user, FixedPoint2? balance, string uplinkPresetId = "StorePresetUplink", EntityUid? uplinkEntity = null)
|
2021-10-03 07:05:52 +03:00
|
|
|
{
|
|
|
|
|
// Try to find target item
|
|
|
|
|
if (uplinkEntity == null)
|
|
|
|
|
{
|
|
|
|
|
uplinkEntity = FindUplinkTarget(user);
|
|
|
|
|
if (uplinkEntity == null)
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-17 00:34:25 -04:00
|
|
|
var store = EnsureComp<StoreComponent>(uplinkEntity.Value);
|
2023-02-12 07:39:14 -05:00
|
|
|
_store.InitializeFromPreset(uplinkPresetId, uplinkEntity.Value, store);
|
2022-08-17 00:34:25 -04:00
|
|
|
store.AccountOwner = user;
|
|
|
|
|
store.Balance.Clear();
|
2021-10-03 07:05:52 +03:00
|
|
|
|
2022-08-17 00:34:25 -04:00
|
|
|
if (balance != null)
|
|
|
|
|
{
|
|
|
|
|
store.Balance.Clear();
|
2023-02-12 07:39:14 -05:00
|
|
|
_store.TryAddCurrency(new Dictionary<string, FixedPoint2> { { TelecrystalCurrencyPrototype, balance.Value } }, uplinkEntity.Value, store);
|
2022-08-17 00:34:25 -04:00
|
|
|
}
|
2022-07-15 13:14:37 +12:00
|
|
|
|
|
|
|
|
// TODO add BUI. Currently can't be done outside of yaml -_-
|
|
|
|
|
|
2021-10-03 07:05:52 +03:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-23 21:00:42 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Finds the entity that can hold an uplink for a user.
|
|
|
|
|
/// Usually this is a pda in their pda slot, but can also be in their hands. (but not pockets or inside bag, etc.)
|
|
|
|
|
/// </summary>
|
|
|
|
|
public EntityUid? FindUplinkTarget(EntityUid user)
|
2021-10-03 07:05:52 +03:00
|
|
|
{
|
|
|
|
|
// Try to find PDA in inventory
|
2021-12-30 22:56:10 +01:00
|
|
|
if (_inventorySystem.TryGetContainerSlotEnumerator(user, out var containerSlotEnumerator))
|
2021-10-03 07:05:52 +03:00
|
|
|
{
|
2021-12-30 22:56:10 +01:00
|
|
|
while (containerSlotEnumerator.MoveNext(out var pdaUid))
|
|
|
|
|
{
|
2022-07-15 00:26:06 -04:00
|
|
|
if (!pdaUid.ContainedEntity.HasValue) continue;
|
2021-12-30 22:56:10 +01:00
|
|
|
|
2023-06-15 03:44:28 +02:00
|
|
|
if (HasComp<PdaComponent>(pdaUid.ContainedEntity.Value) || HasComp<StoreComponent>(pdaUid.ContainedEntity.Value))
|
2021-12-30 22:56:10 +01:00
|
|
|
return pdaUid.ContainedEntity.Value;
|
|
|
|
|
}
|
2021-10-03 07:05:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Also check hands
|
2022-03-17 20:13:31 +13:00
|
|
|
foreach (var item in _handsSystem.EnumerateHeld(user))
|
2021-10-03 07:05:52 +03:00
|
|
|
{
|
2023-06-15 03:44:28 +02:00
|
|
|
if (HasComp<PdaComponent>(item) || HasComp<StoreComponent>(item))
|
2022-03-17 20:13:31 +13:00
|
|
|
return item;
|
2021-10-03 07:05:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|