Moves ID field to ListingData and adds BuyBeforeCondition (#22438)

This commit is contained in:
keronshb
2023-12-13 16:57:38 -05:00
committed by GitHub
parent 7a45f09c88
commit 9864cb0232
2 changed files with 62 additions and 3 deletions

View File

@@ -0,0 +1,56 @@
using Content.Server.Store.Components;
using Content.Server.Store.Systems;
using Content.Shared.Store;
using Robust.Shared.Prototypes;
namespace Content.Server.Store.Conditions;
public sealed partial class BuyBeforeCondition : ListingCondition
{
/// <summary>
/// Required listing(s) needed to purchase before this listing is available
/// </summary>
[DataField(required: true)]
public HashSet<ProtoId<ListingPrototype>> Whitelist;
/// <summary>
/// Listing(s) that if bought, block this purchase, if any.
/// </summary>
public HashSet<ProtoId<ListingPrototype>>? Blacklist;
public override bool Condition(ListingConditionArgs args)
{
if (!args.EntityManager.TryGetComponent<StoreComponent>(args.StoreEntity, out var storeComp))
return false;
var allListings = storeComp.Listings;
var purchasesFound = false;
if (Blacklist != null)
{
foreach (var blacklistListing in Blacklist)
{
foreach (var listing in allListings)
{
if (listing.ID == blacklistListing.Id && listing.PurchaseAmount > 0)
return false;
}
}
}
foreach (var requiredListing in Whitelist)
{
foreach (var listing in allListings)
{
if (listing.ID == requiredListing.Id)
{
purchasesFound = listing.PurchaseAmount > 0;
break;
}
}
}
return purchasesFound;
}
}

View File

@@ -18,6 +18,10 @@ namespace Content.Shared.Store;
[Virtual, DataDefinition] [Virtual, DataDefinition]
public partial class ListingData : IEquatable<ListingData>, ICloneable public partial class ListingData : IEquatable<ListingData>, ICloneable
{ {
[ViewVariables]
[IdDataField]
public string ID { get; private set; } = default!;
/// <summary> /// <summary>
/// The name of the listing. If empty, uses the entity's name (if present) /// The name of the listing. If empty, uses the entity's name (if present)
/// </summary> /// </summary>
@@ -131,6 +135,7 @@ public partial class ListingData : IEquatable<ListingData>, ICloneable
{ {
return new ListingData return new ListingData
{ {
ID = ID,
Name = Name, Name = Name,
Description = Description, Description = Description,
Categories = Categories, Categories = Categories,
@@ -156,7 +161,5 @@ public partial class ListingData : IEquatable<ListingData>, ICloneable
[DataDefinition] [DataDefinition]
public sealed partial class ListingPrototype : ListingData, IPrototype public sealed partial class ListingPrototype : ListingData, IPrototype
{ {
[ViewVariables]
[IdDataField]
public string ID { get; private set; } = default!;
} }