mirror of
https://github.com/space-syndicate/space-station-14.git
synced 2026-10-09 18:31:17 +03:00
* i wanna explode * Revert "i wanna explode" This reverts commit 595dc4ea49aa81d9ca19fccc52279cf7ef52c13d. * fix * Update Content.Client/Store/Ui/StoreListingControl.xaml.cs Co-authored-by: ScarKy0 <106310278+ScarKy0@users.noreply.github.com> * Update Content.Server/Store/Conditions/BuyerBodyCountCondition.cs Co-authored-by: ScarKy0 <106310278+ScarKy0@users.noreply.github.com> * Update Content.Shared/Store/ListingCondition.cs Co-authored-by: ScarKy0 <106310278+ScarKy0@users.noreply.github.com> * Update Content.Shared/Store/ListingPrototype.cs Co-authored-by: ScarKy0 <106310278+ScarKy0@users.noreply.github.com> * frog * wewlad * cut mah life into pieces, this is my last resort * ewjqiorwerte * Apply suggestion from @ScarKy0 * works * Apply suggestion from @ScarKy0 * init * small cleanup * Revert "init" This reverts commit db5d86aa6d662463972539462bccb61ba441624b. --------- Co-authored-by: nomdéraisonnablementlong <you@example.com> Co-authored-by: ScarKy0 <106310278+ScarKy0@users.noreply.github.com> Co-authored-by: ScarKy0 <scarky0@onet.eu>
191 lines
6.7 KiB
C#
191 lines
6.7 KiB
C#
using System.Diagnostics.CodeAnalysis;
|
|
using Content.Shared.Mind;
|
|
using Content.Shared.Store.Components;
|
|
using Robust.Shared.Prototypes;
|
|
|
|
namespace Content.Shared.Store;
|
|
|
|
|
|
public abstract partial class SharedStoreSystem
|
|
{
|
|
/// <summary>
|
|
/// Refreshes all listings on a store.
|
|
/// Do not use if you don't know what you're doing.
|
|
/// </summary>
|
|
/// <param name="component">The store to refresh</param>
|
|
public void RefreshAllListings(StoreComponent component)
|
|
{
|
|
var previousState = component.FullListingsCatalog;
|
|
var newState = GetAllListings();
|
|
// if we refresh list with existing cost modifiers - they will be removed,
|
|
// need to restore them
|
|
if (previousState.Count != 0)
|
|
{
|
|
foreach (var previousStateListingItem in previousState)
|
|
{
|
|
if (!previousStateListingItem.IsCostModified
|
|
|| !TryGetListing(newState, previousStateListingItem.ID, out var found))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
foreach (var (modifierSourceId, costModifier) in previousStateListingItem.CostModifiersBySourceId)
|
|
{
|
|
found.AddCostModifier(modifierSourceId, costModifier);
|
|
}
|
|
}
|
|
}
|
|
|
|
component.FullListingsCatalog = newState;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets all listings from a prototype.
|
|
/// </summary>
|
|
/// <returns>All the listings</returns>
|
|
public HashSet<ListingDataWithCostModifiers> GetAllListings()
|
|
{
|
|
var clones = new HashSet<ListingDataWithCostModifiers>();
|
|
foreach (var prototype in ProtoMan.EnumeratePrototypes<ListingPrototype>())
|
|
{
|
|
clones.Add(new ListingDataWithCostModifiers(prototype));
|
|
}
|
|
|
|
return clones;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds a listing from an Id to a store
|
|
/// </summary>
|
|
/// <param name="component">The store to add the listing to</param>
|
|
/// <param name="listingId">The id of the listing</param>
|
|
/// <returns>Whether or not the listing was added successfully</returns>
|
|
public bool TryAddListing(StoreComponent component, string listingId)
|
|
{
|
|
if (!ProtoMan.TryIndex<ListingPrototype>(listingId, out var proto))
|
|
{
|
|
Log.Error("Attempted to add invalid listing.");
|
|
return false;
|
|
}
|
|
|
|
return TryAddListing(component, proto);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds a listing to a store
|
|
/// </summary>
|
|
/// <param name="component">The store to add the listing to</param>
|
|
/// <param name="listing">The listing</param>
|
|
/// <returns>Whether or not the listing was add successfully</returns>
|
|
public bool TryAddListing(StoreComponent component, ListingPrototype listing)
|
|
{
|
|
return component.FullListingsCatalog.Add(new ListingDataWithCostModifiers(listing));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 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<ListingDataWithCostModifiers> GetAvailableListings(EntityUid buyer, EntityUid store, StoreComponent component)
|
|
{
|
|
return GetAvailableListings(buyer, component.FullListingsCatalog, component.Categories, store);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the available listings for a user given an overall set of listings and categories to filter by.
|
|
/// </summary>
|
|
/// <param name="buyer">Either the account owner, user, or an inanimate object (e.g., surplus bundle)</param>
|
|
/// <param name="listings">All of the listings that are available. If null, will just get all listings from the prototypes.</param>
|
|
/// <param name="categories">What categories to filter by.</param>
|
|
/// <param name="storeEntity">The physial entity of the store. Can be null.</param>
|
|
/// <returns>The available listings.</returns>
|
|
public IEnumerable<ListingDataWithCostModifiers> GetAvailableListings(
|
|
EntityUid buyer,
|
|
IReadOnlyCollection<ListingDataWithCostModifiers>? listings,
|
|
HashSet<ProtoId<StoreCategoryPrototype>> categories,
|
|
EntityUid? storeEntity = null
|
|
)
|
|
{
|
|
listings ??= GetAllListings();
|
|
|
|
foreach (var listing in listings)
|
|
{
|
|
if (!ListingHasCategory(listing, categories))
|
|
continue;
|
|
|
|
if (listing.Conditions != null)
|
|
{
|
|
var args = new ListingConditionArgs(GetBuyerMind(buyer), storeEntity, listing, EntityManager);
|
|
var locked = false;
|
|
var failed = false;
|
|
|
|
foreach (var condition in listing.Conditions)
|
|
{
|
|
if (!condition.Condition(args))
|
|
{
|
|
if (condition.Lock)
|
|
locked = true;
|
|
else
|
|
failed = true;
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
listing.Locked = locked && !failed;
|
|
|
|
if (failed)
|
|
continue;
|
|
}
|
|
|
|
yield return listing;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the entity's mind entity, if it has one, to be used for listing conditions.
|
|
/// If it doesn't have one, or is a mind entity already, it returns itself.
|
|
/// </summary>
|
|
/// <param name="buyer">The buying entity.</param>
|
|
public EntityUid GetBuyerMind(EntityUid buyer)
|
|
{
|
|
if (!HasComp<MindComponent>(buyer) && Mind.TryGetMind(buyer, out var buyerMind, out _))
|
|
return buyerMind;
|
|
|
|
return buyer;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks if a listing appears in a list of given categories
|
|
/// </summary>
|
|
/// <param name="listing">The listing itself.</param>
|
|
/// <param name="categories">The categories to check through.</param>
|
|
/// <returns>If the listing was present in one of the categories.</returns>
|
|
public bool ListingHasCategory(ListingData listing, HashSet<ProtoId<StoreCategoryPrototype>> categories)
|
|
{
|
|
foreach (var cat in categories)
|
|
{
|
|
if (listing.Categories.Contains(cat))
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private bool TryGetListing(IReadOnlyCollection<ListingDataWithCostModifiers> collection, string listingId, [MaybeNullWhen(false)] out ListingDataWithCostModifiers found)
|
|
{
|
|
foreach(var current in collection)
|
|
{
|
|
if (current.ID == listingId)
|
|
{
|
|
found = current;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
found = null!;
|
|
return false;
|
|
}
|
|
}
|