Clean up StoreSystem (#14027)
This commit is contained in:
@@ -1,14 +1,13 @@
|
||||
using Content.Server.Stack;
|
||||
using Content.Server.Store.Components;
|
||||
using Content.Shared.FixedPoint;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Popups;
|
||||
using Content.Shared.Store;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Prototypes;
|
||||
using System.Linq;
|
||||
using Content.Server.UserInterface;
|
||||
using Content.Shared.Stacks;
|
||||
using JetBrains.Annotations;
|
||||
|
||||
namespace Content.Server.Store.Systems;
|
||||
|
||||
@@ -26,22 +25,38 @@ public sealed partial class StoreSystem : EntitySystem
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<CurrencyComponent, AfterInteractEvent>(OnAfterInteract);
|
||||
SubscribeLocalEvent<StoreComponent, BeforeActivatableUIOpenEvent>((_,c,a) => UpdateUserInterface(a.User, c));
|
||||
SubscribeLocalEvent<StoreComponent, BeforeActivatableUIOpenEvent>(BeforeActivatableUiOpen);
|
||||
|
||||
SubscribeLocalEvent<StoreComponent, MapInitEvent>(OnMapInit);
|
||||
SubscribeLocalEvent<StoreComponent, ComponentStartup>(OnStartup);
|
||||
SubscribeLocalEvent<StoreComponent, ComponentShutdown>(OnShutdown);
|
||||
|
||||
InitializeUi();
|
||||
}
|
||||
|
||||
private void OnMapInit(EntityUid uid, StoreComponent component, MapInitEvent args)
|
||||
{
|
||||
RefreshAllListings(component);
|
||||
InitializeFromPreset(component.Preset, uid, component);
|
||||
}
|
||||
|
||||
private void OnStartup(EntityUid uid, StoreComponent component, ComponentStartup args)
|
||||
{
|
||||
RaiseLocalEvent(uid, new StoreAddedEvent(), true);
|
||||
// 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);
|
||||
}
|
||||
|
||||
private void OnShutdown(EntityUid uid, StoreComponent component, ComponentShutdown args)
|
||||
{
|
||||
RaiseLocalEvent(uid, new StoreRemovedEvent(), true);
|
||||
var ev = new StoreRemovedEvent();
|
||||
RaiseLocalEvent(uid, ref ev, true);
|
||||
}
|
||||
|
||||
private void OnAfterInteract(EntityUid uid, CurrencyComponent component, AfterInteractEvent args)
|
||||
@@ -52,15 +67,7 @@ public sealed partial class StoreSystem : EntitySystem
|
||||
if (args.Target == null || !TryComp<StoreComponent>(args.Target, out var store))
|
||||
return;
|
||||
|
||||
//if you somehow are inserting cash before the store initializes.
|
||||
if (!store.Opened)
|
||||
{
|
||||
RefreshAllListings(store);
|
||||
InitializeFromPreset(store.Preset, store);
|
||||
store.Opened = true;
|
||||
}
|
||||
|
||||
args.Handled = TryAddCurrency(GetCurrencyValue(component), store);
|
||||
args.Handled = TryAddCurrency(GetCurrencyValue(uid, component), args.Target.Value, store);
|
||||
|
||||
if (args.Handled)
|
||||
{
|
||||
@@ -74,35 +81,43 @@ public sealed partial class StoreSystem : EntitySystem
|
||||
/// Gets the value from an entity's currency component.
|
||||
/// Scales with stacks.
|
||||
/// </summary>
|
||||
/// <param name="uid"></param>
|
||||
/// <param name="component"></param>
|
||||
/// <returns>The value of the currency</returns>
|
||||
public Dictionary<string, FixedPoint2> GetCurrencyValue(CurrencyComponent component)
|
||||
public Dictionary<string, FixedPoint2> GetCurrencyValue(EntityUid uid, CurrencyComponent component)
|
||||
{
|
||||
TryComp<StackComponent>(component.Owner, out var stack);
|
||||
var amount = stack?.Count ?? 1;
|
||||
|
||||
var amount = EntityManager.GetComponentOrNull<StackComponent>(uid)?.Count ?? 1;
|
||||
return component.Price.ToDictionary(v => v.Key, p => p.Value * amount);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to add a currency to a store's balance.
|
||||
/// </summary>
|
||||
/// <param name="component">The currency to add</param>
|
||||
/// <param name="currencyEnt"></param>
|
||||
/// <param name="storeEnt"></param>
|
||||
/// <param name="currency">The currency to add</param>
|
||||
/// <param name="store">The store to add it to</param>
|
||||
/// <returns>Whether or not the currency was succesfully added</returns>
|
||||
public bool TryAddCurrency(CurrencyComponent component, StoreComponent store)
|
||||
[PublicAPI]
|
||||
public bool TryAddCurrency(EntityUid currencyEnt, EntityUid storeEnt, StoreComponent? store = null, CurrencyComponent? currency = null)
|
||||
{
|
||||
return TryAddCurrency(GetCurrencyValue(component), store);
|
||||
if (!Resolve(currencyEnt, ref currency) || !Resolve(storeEnt, ref store))
|
||||
return false;
|
||||
return TryAddCurrency(GetCurrencyValue(currencyEnt, currency), storeEnt, store);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to add a currency to a store's balance
|
||||
/// </summary>
|
||||
/// <param name="currency">The value to add to the store</param>
|
||||
/// <param name="uid"></param>
|
||||
/// <param name="store">The store to add it to</param>
|
||||
/// <returns>Whether or not the currency was succesfully added</returns>
|
||||
public bool TryAddCurrency(Dictionary<string, FixedPoint2> currency, StoreComponent store)
|
||||
public bool TryAddCurrency(Dictionary<string, FixedPoint2> currency, EntityUid uid, StoreComponent? store = null)
|
||||
{
|
||||
if (!Resolve(uid, ref store))
|
||||
return false;
|
||||
|
||||
//verify these before values are modified
|
||||
foreach (var type in currency)
|
||||
{
|
||||
@@ -116,7 +131,7 @@ public sealed partial class StoreSystem : EntitySystem
|
||||
store.Balance[type.Key] += type.Value;
|
||||
}
|
||||
|
||||
UpdateUserInterface(null, store);
|
||||
UpdateUserInterface(null, uid, store);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -124,8 +139,9 @@ public sealed partial class StoreSystem : EntitySystem
|
||||
/// Initializes a store based on a preset ID
|
||||
/// </summary>
|
||||
/// <param name="preset">The ID of a store preset prototype</param>
|
||||
/// <param name="uid"></param>
|
||||
/// <param name="component">The store being initialized</param>
|
||||
public void InitializeFromPreset(string? preset, StoreComponent component)
|
||||
public void InitializeFromPreset(string? preset, EntityUid uid, StoreComponent component)
|
||||
{
|
||||
if (preset == null)
|
||||
return;
|
||||
@@ -133,23 +149,24 @@ public sealed partial class StoreSystem : EntitySystem
|
||||
if (!_proto.TryIndex<StorePresetPrototype>(preset, out var proto))
|
||||
return;
|
||||
|
||||
InitializeFromPreset(proto, component);
|
||||
InitializeFromPreset(proto, uid, component);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a store based on a given preset
|
||||
/// </summary>
|
||||
/// <param name="preset">The StorePresetPrototype</param>
|
||||
/// <param name="uid"></param>
|
||||
/// <param name="component">The store being initialized</param>
|
||||
public void InitializeFromPreset(StorePresetPrototype preset, StoreComponent component)
|
||||
public void InitializeFromPreset(StorePresetPrototype preset, EntityUid uid, StoreComponent component)
|
||||
{
|
||||
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
|
||||
TryAddCurrency(preset.InitialBalance, component);
|
||||
TryAddCurrency(preset.InitialBalance, uid, component);
|
||||
|
||||
var ui = _ui.GetUiOrNull(component.Owner, StoreUiKey.Key);
|
||||
var ui = _ui.GetUiOrNull(uid, StoreUiKey.Key);
|
||||
if (ui != null)
|
||||
_ui.SetUiState(ui, new StoreInitializeState(preset.StoreName));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user