Clean up StoreSystem (#14027)

This commit is contained in:
Nemanja
2023-02-12 07:39:14 -05:00
committed by GitHub
parent e7b18b33aa
commit 351c53e774
9 changed files with 108 additions and 101 deletions

View File

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