Files
OldThink/Content.Server/Store/Systems/StoreSystem.cs

189 lines
7.0 KiB
C#
Raw Normal View History

using Content.Server.Mind.Components;
2022-08-17 00:34:25 -04:00
using Content.Server.Store.Components;
using Content.Server.UserInterface;
2022-08-17 00:34:25 -04:00
using Content.Shared.FixedPoint;
using Content.Shared.Implants.Components;
2022-08-17 00:34:25 -04:00
using Content.Shared.Interaction;
using Content.Shared.Popups;
using Content.Shared.Stacks;
2022-08-17 00:34:25 -04:00
using Content.Shared.Store;
using JetBrains.Annotations;
using Robust.Server.GameObjects;
2022-08-17 00:34:25 -04:00
using Robust.Shared.Prototypes;
using System.Linq;
namespace Content.Server.Store.Systems;
/// <summary>
/// Manages general interactions with a store and different entities,
/// getting listings for stores, and interfacing with the store UI.
/// </summary>
public sealed partial class StoreSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _proto = default!;
[Dependency] private readonly SharedPopupSystem _popup = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<CurrencyComponent, AfterInteractEvent>(OnAfterInteract);
2023-02-12 07:39:14 -05:00
SubscribeLocalEvent<StoreComponent, BeforeActivatableUIOpenEvent>(BeforeActivatableUiOpen);
2022-08-17 00:34:25 -04:00
2023-02-12 07:39:14 -05:00
SubscribeLocalEvent<StoreComponent, MapInitEvent>(OnMapInit);
2022-08-17 00:34:25 -04:00
SubscribeLocalEvent<StoreComponent, ComponentStartup>(OnStartup);
SubscribeLocalEvent<StoreComponent, ComponentShutdown>(OnShutdown);
SubscribeLocalEvent<StoreComponent, OpenUplinkImplantEvent>(OnImplantActivate);
2022-08-17 00:34:25 -04:00
InitializeUi();
2023-03-30 23:02:39 -04:00
InitializeCommand();
2022-08-17 00:34:25 -04:00
}
2023-02-12 07:39:14 -05:00
private void OnMapInit(EntityUid uid, StoreComponent component, MapInitEvent args)
{
RefreshAllListings(component);
InitializeFromPreset(component.Preset, uid, component);
}
2022-08-17 00:34:25 -04:00
private void OnStartup(EntityUid uid, StoreComponent component, ComponentStartup args)
{
2023-02-12 07:39:14 -05:00
// for traitors, because the StoreComponent for the PDA can be added at any time.
if (MetaData(uid).EntityLifeStage == EntityLifeStage.MapInitialized)
{
RefreshAllListings(component);
InitializeFromPreset(component.Preset, uid, component);
}
var ev = new StoreAddedEvent();
RaiseLocalEvent(uid, ref ev, true);
2022-08-17 00:34:25 -04:00
}
private void OnShutdown(EntityUid uid, StoreComponent component, ComponentShutdown args)
{
2023-02-12 07:39:14 -05:00
var ev = new StoreRemovedEvent();
RaiseLocalEvent(uid, ref ev, true);
2022-08-17 00:34:25 -04:00
}
private void OnAfterInteract(EntityUid uid, CurrencyComponent component, AfterInteractEvent args)
{
if (args.Handled || !args.CanReach)
return;
if (args.Target == null || !TryComp<StoreComponent>(args.Target, out var store))
return;
// require the store to be open before inserting currency
var user = args.User;
if (!TryComp<ActorComponent>(user, out var actor) || !_ui.SessionHasOpenUi(uid, StoreUiKey.Key, actor.PlayerSession))
return;
2022-08-17 00:34:25 -04:00
2023-02-12 07:39:14 -05:00
args.Handled = TryAddCurrency(GetCurrencyValue(uid, component), args.Target.Value, store);
2022-08-17 00:34:25 -04:00
if (args.Handled)
{
var msg = Loc.GetString("store-currency-inserted", ("used", args.Used), ("target", args.Target));
_popup.PopupEntity(msg, args.Target.Value);
2022-08-17 00:34:25 -04:00
QueueDel(args.Used);
}
}
private void OnImplantActivate(EntityUid uid, StoreComponent component, OpenUplinkImplantEvent args)
{
ToggleUi(args.Performer, uid, component);
}
2022-08-17 00:34:25 -04:00
/// <summary>
/// Gets the value from an entity's currency component.
/// Scales with stacks.
/// </summary>
2023-02-12 07:39:14 -05:00
/// <param name="uid"></param>
2022-08-17 00:34:25 -04:00
/// <param name="component"></param>
/// <returns>The value of the currency</returns>
2023-02-12 07:39:14 -05:00
public Dictionary<string, FixedPoint2> GetCurrencyValue(EntityUid uid, CurrencyComponent component)
2022-08-17 00:34:25 -04:00
{
2023-02-12 07:39:14 -05:00
var amount = EntityManager.GetComponentOrNull<StackComponent>(uid)?.Count ?? 1;
2022-08-17 00:34:25 -04:00
return component.Price.ToDictionary(v => v.Key, p => p.Value * amount);
}
/// <summary>
/// Tries to add a currency to a store's balance.
/// </summary>
2023-02-12 07:39:14 -05:00
/// <param name="currencyEnt"></param>
/// <param name="storeEnt"></param>
/// <param name="currency">The currency to add</param>
2022-08-17 00:34:25 -04:00
/// <param name="store">The store to add it to</param>
/// <returns>Whether or not the currency was succesfully added</returns>
2023-02-12 07:39:14 -05:00
[PublicAPI]
public bool TryAddCurrency(EntityUid currencyEnt, EntityUid storeEnt, StoreComponent? store = null, CurrencyComponent? currency = null)
2022-08-17 00:34:25 -04:00
{
2023-02-12 07:39:14 -05:00
if (!Resolve(currencyEnt, ref currency) || !Resolve(storeEnt, ref store))
return false;
return TryAddCurrency(GetCurrencyValue(currencyEnt, currency), storeEnt, store);
2022-08-17 00:34:25 -04:00
}
/// <summary>
/// Tries to add a currency to a store's balance
/// </summary>
/// <param name="currency">The value to add to the store</param>
2023-02-12 07:39:14 -05:00
/// <param name="uid"></param>
2022-08-17 00:34:25 -04:00
/// <param name="store">The store to add it to</param>
/// <returns>Whether or not the currency was succesfully added</returns>
2023-02-12 07:39:14 -05:00
public bool TryAddCurrency(Dictionary<string, FixedPoint2> currency, EntityUid uid, StoreComponent? store = null)
2022-08-17 00:34:25 -04:00
{
2023-02-12 07:39:14 -05:00
if (!Resolve(uid, ref store))
return false;
2022-08-17 00:34:25 -04:00
//verify these before values are modified
foreach (var type in currency)
{
if (!store.CurrencyWhitelist.Contains(type.Key))
return false;
}
foreach (var type in currency)
{
if (!store.Balance.TryAdd(type.Key, type.Value))
store.Balance[type.Key] += type.Value;
}
2023-02-12 07:39:14 -05:00
UpdateUserInterface(null, uid, store);
2022-08-17 00:34:25 -04:00
return true;
}
/// <summary>
/// Initializes a store based on a preset ID
/// </summary>
/// <param name="preset">The ID of a store preset prototype</param>
2023-02-12 07:39:14 -05:00
/// <param name="uid"></param>
2022-08-17 00:34:25 -04:00
/// <param name="component">The store being initialized</param>
2023-02-12 07:39:14 -05:00
public void InitializeFromPreset(string? preset, EntityUid uid, StoreComponent component)
2022-08-17 00:34:25 -04:00
{
if (preset == null)
return;
if (!_proto.TryIndex<StorePresetPrototype>(preset, out var proto))
return;
2023-02-12 07:39:14 -05:00
InitializeFromPreset(proto, uid, component);
2022-08-17 00:34:25 -04:00
}
/// <summary>
/// Initializes a store based on a given preset
/// </summary>
/// <param name="preset">The StorePresetPrototype</param>
2023-02-12 07:39:14 -05:00
/// <param name="uid"></param>
2022-08-17 00:34:25 -04:00
/// <param name="component">The store being initialized</param>
2023-02-12 07:39:14 -05:00
public void InitializeFromPreset(StorePresetPrototype preset, EntityUid uid, StoreComponent component)
2022-08-17 00:34:25 -04:00
{
component.Preset = preset.ID;
component.CurrencyWhitelist.UnionWith(preset.CurrencyWhitelist);
component.Categories.UnionWith(preset.Categories);
if (component.Balance == new Dictionary<string, FixedPoint2>() && preset.InitialBalance != null) //if we don't have a value stored, use the preset
2023-02-12 07:39:14 -05:00
TryAddCurrency(preset.InitialBalance, uid, component);
2022-08-17 00:34:25 -04:00
2023-02-12 07:39:14 -05:00
var ui = _ui.GetUiOrNull(uid, StoreUiKey.Key);
2022-09-11 02:54:16 -04:00
if (ui != null)
_ui.SetUiState(ui, new StoreInitializeState(preset.StoreName));
2022-08-17 00:34:25 -04:00
}
}