Clean up StoreSystem (#14027)
This commit is contained in:
@@ -59,11 +59,6 @@ public sealed class StoreComponent : Component
|
||||
[ViewVariables]
|
||||
public HashSet<ListingData> LastAvailableListings = new();
|
||||
|
||||
/// <summary>
|
||||
/// checks whether or not the store has been opened yet.
|
||||
/// </summary>
|
||||
public bool Opened = false;
|
||||
|
||||
#region audio
|
||||
/// <summary>
|
||||
/// The sound played to the buyer when a purchase is succesfully made.
|
||||
@@ -76,8 +71,10 @@ public sealed class StoreComponent : Component
|
||||
/// <summary>
|
||||
/// Event that is broadcast when a store is added to an entity
|
||||
/// </summary>
|
||||
public sealed class StoreAddedEvent : EntityEventArgs { };
|
||||
[ByRefEvent]
|
||||
public readonly record struct StoreAddedEvent;
|
||||
/// <summary>
|
||||
/// Event that is broadcast when a store is removed from an entity
|
||||
/// </summary>
|
||||
public sealed class StoreRemovedEvent : EntityEventArgs { };
|
||||
[ByRefEvent]
|
||||
public readonly record struct StoreRemovedEvent;
|
||||
|
||||
@@ -3,7 +3,7 @@ using Content.Shared.Store;
|
||||
|
||||
namespace Content.Server.Store.Systems;
|
||||
|
||||
public sealed partial class StoreSystem : EntitySystem
|
||||
public sealed partial class StoreSystem
|
||||
{
|
||||
/// <summary>
|
||||
/// Refreshes all listings on a store.
|
||||
@@ -64,11 +64,12 @@ public sealed partial class StoreSystem : EntitySystem
|
||||
/// Gets the available listings for a store
|
||||
/// </summary>
|
||||
/// <param name="buyer">Either the account owner, user, or an inanimate object (e.g., surplus bundle)</param>
|
||||
/// <param name="store"></param>
|
||||
/// <param name="component">The store the listings are coming from.</param>
|
||||
/// <returns>The available listings.</returns>
|
||||
public IEnumerable<ListingData> GetAvailableListings(EntityUid buyer, StoreComponent component)
|
||||
public IEnumerable<ListingData> GetAvailableListings(EntityUid buyer, EntityUid store, StoreComponent component)
|
||||
{
|
||||
return GetAvailableListings(buyer, component.Listings, component.Categories, component.Owner);
|
||||
return GetAvailableListings(buyer, component.Listings, component.Categories, store);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -81,8 +82,7 @@ public sealed partial class StoreSystem : EntitySystem
|
||||
/// <returns>The available listings.</returns>
|
||||
public IEnumerable<ListingData> GetAvailableListings(EntityUid buyer, HashSet<ListingData>? listings, HashSet<string> categories, EntityUid? storeEntity = null)
|
||||
{
|
||||
if (listings == null)
|
||||
listings = GetAllListings();
|
||||
listings ??= GetAllListings();
|
||||
|
||||
foreach (var listing in listings)
|
||||
{
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
using Content.Server.Actions;
|
||||
using Content.Server.Administration.Logs;
|
||||
using Content.Server.Mind.Components;
|
||||
using Content.Server.Store.Components;
|
||||
using Content.Shared.Actions.ActionTypes;
|
||||
using Content.Shared.FixedPoint;
|
||||
@@ -10,11 +9,11 @@ using Content.Shared.Database;
|
||||
using Robust.Server.GameObjects;
|
||||
using System.Linq;
|
||||
using Content.Server.Stack;
|
||||
using Robust.Shared.Player;
|
||||
using Content.Server.UserInterface;
|
||||
|
||||
namespace Content.Server.Store.Systems;
|
||||
|
||||
public sealed partial class StoreSystem : EntitySystem
|
||||
public sealed partial class StoreSystem
|
||||
{
|
||||
[Dependency] private readonly IAdminLogManager _admin = default!;
|
||||
[Dependency] private readonly SharedHandsSystem _hands = default!;
|
||||
@@ -25,7 +24,7 @@ public sealed partial class StoreSystem : EntitySystem
|
||||
|
||||
private void InitializeUi()
|
||||
{
|
||||
SubscribeLocalEvent<StoreComponent, StoreRequestUpdateInterfaceMessage>((_,c,r) => UpdateUserInterface(r.Session.AttachedEntity, c));
|
||||
SubscribeLocalEvent<StoreComponent, StoreRequestUpdateInterfaceMessage>(OnRequestUpdate);
|
||||
SubscribeLocalEvent<StoreComponent, StoreBuyListingMessage>(OnBuyRequest);
|
||||
SubscribeLocalEvent<StoreComponent, StoreRequestWithdrawMessage>(OnRequestWithdraw);
|
||||
}
|
||||
@@ -34,45 +33,41 @@ public sealed partial class StoreSystem : EntitySystem
|
||||
/// Toggles the store Ui open and closed
|
||||
/// </summary>
|
||||
/// <param name="user">the person doing the toggling</param>
|
||||
/// <param name="component">the store being toggled</param>
|
||||
public void ToggleUi(EntityUid user, StoreComponent component)
|
||||
/// <param name="storeEnt">the store being toggled</param>
|
||||
/// <param name="component"></param>
|
||||
public void ToggleUi(EntityUid user, EntityUid storeEnt, StoreComponent? component = null)
|
||||
{
|
||||
if (!Resolve(storeEnt, ref component))
|
||||
return;
|
||||
|
||||
if (!TryComp<ActorComponent>(user, out var actor))
|
||||
return;
|
||||
|
||||
if (!_ui.TryToggleUi(component.Owner, StoreUiKey.Key, actor.PlayerSession))
|
||||
if (!_ui.TryToggleUi(storeEnt, StoreUiKey.Key, actor.PlayerSession))
|
||||
return;
|
||||
|
||||
UpdateUserInterface(user, component);
|
||||
UpdateUserInterface(user, storeEnt, component);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the user interface for a store and refreshes the listings
|
||||
/// </summary>
|
||||
/// <param name="user">The person who if opening the store ui. Listings are filtered based on this.</param>
|
||||
/// <param name="store">The store entity itself</param>
|
||||
/// <param name="component">The store component being refreshed.</param>
|
||||
/// <param name="ui"></param>
|
||||
public void UpdateUserInterface(EntityUid? user, StoreComponent component, BoundUserInterface? ui = null)
|
||||
public void UpdateUserInterface(EntityUid? user, EntityUid store, StoreComponent? component = null, BoundUserInterface? ui = null)
|
||||
{
|
||||
if (ui == null)
|
||||
{
|
||||
ui = _ui.GetUiOrNull(component.Owner, StoreUiKey.Key);
|
||||
if (ui == null)
|
||||
return;
|
||||
}
|
||||
if (!Resolve(store, ref component))
|
||||
return;
|
||||
|
||||
//if we haven't opened it before, initialize the shit
|
||||
if (!component.Opened)
|
||||
{
|
||||
RefreshAllListings(component);
|
||||
InitializeFromPreset(component.Preset, component);
|
||||
component.Opened = true;
|
||||
}
|
||||
if (ui == null && !_ui.TryGetUi(store, StoreUiKey.Key, out ui))
|
||||
return;
|
||||
|
||||
//this is the person who will be passed into logic for all listing filtering.
|
||||
if (user != null) //if we have no "buyer" for this update, then don't update the listings
|
||||
{
|
||||
component.LastAvailableListings = GetAvailableListings(component.AccountOwner ?? user.Value, component).ToHashSet();
|
||||
component.LastAvailableListings = GetAvailableListings(component.AccountOwner ?? user.Value, store, component).ToHashSet();
|
||||
}
|
||||
|
||||
//dictionary for all currencies, including 0 values for currencies on the whitelist
|
||||
@@ -92,12 +87,22 @@ public sealed partial class StoreSystem : EntitySystem
|
||||
_ui.SetUiState(ui, state);
|
||||
}
|
||||
|
||||
private void OnRequestUpdate(EntityUid uid, StoreComponent component, StoreRequestUpdateInterfaceMessage args)
|
||||
{
|
||||
UpdateUserInterface(args.Session.AttachedEntity, args.Entity, component);
|
||||
}
|
||||
|
||||
private void BeforeActivatableUiOpen(EntityUid uid, StoreComponent component, BeforeActivatableUIOpenEvent args)
|
||||
{
|
||||
UpdateUserInterface(args.User, uid, component);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles whenever a purchase was made.
|
||||
/// </summary>
|
||||
private void OnBuyRequest(EntityUid uid, StoreComponent component, StoreBuyListingMessage msg)
|
||||
{
|
||||
ListingData? listing = component.Listings.FirstOrDefault(x => x.Equals(msg.Listing));
|
||||
var listing = component.Listings.FirstOrDefault(x => x.Equals(msg.Listing));
|
||||
if (listing == null) //make sure this listing actually exists
|
||||
{
|
||||
Logger.Debug("listing does not exist");
|
||||
@@ -114,7 +119,7 @@ public sealed partial class StoreSystem : EntitySystem
|
||||
//condition checking because why not
|
||||
if (listing.Conditions != null)
|
||||
{
|
||||
var args = new ListingConditionArgs(component.AccountOwner ?? buyer, component.Owner, listing, EntityManager);
|
||||
var args = new ListingConditionArgs(component.AccountOwner ?? buyer, uid, listing, EntityManager);
|
||||
var conditionsMet = listing.Conditions.All(condition => condition.Condition(args));
|
||||
|
||||
if (!conditionsMet)
|
||||
@@ -156,16 +161,13 @@ public sealed partial class StoreSystem : EntitySystem
|
||||
}
|
||||
|
||||
//log dat shit.
|
||||
if (TryComp<MindComponent>(buyer, out var mind))
|
||||
{
|
||||
_admin.Add(LogType.StorePurchase, LogImpact.Low,
|
||||
$"{ToPrettyString(mind.Owner):player} purchased listing \"{Loc.GetString(listing.Name)}\" from {ToPrettyString(uid)}");
|
||||
}
|
||||
_admin.Add(LogType.StorePurchase, LogImpact.Low,
|
||||
$"{ToPrettyString(buyer):player} purchased listing \"{Loc.GetString(listing.Name)}\" from {ToPrettyString(uid)}");
|
||||
|
||||
listing.PurchaseAmount++; //track how many times something has been purchased
|
||||
_audio.PlayEntity(component.BuySuccessSound, msg.Session, uid); //cha-ching!
|
||||
|
||||
UpdateUserInterface(buyer, component);
|
||||
UpdateUserInterface(buyer, uid, component);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -206,6 +208,6 @@ public sealed partial class StoreSystem : EntitySystem
|
||||
}
|
||||
|
||||
component.Balance[msg.Currency] -= msg.Amount;
|
||||
UpdateUserInterface(buyer, component);
|
||||
UpdateUserInterface(buyer, uid, component);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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