Generalized Store System (#10201)
This commit is contained in:
63
Content.Server/Store/Conditions/BuyerAntagCondition.cs
Normal file
63
Content.Server/Store/Conditions/BuyerAntagCondition.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using Content.Server.Mind.Components;
|
||||
using Content.Server.Traitor;
|
||||
using Content.Shared.Roles;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.Set;
|
||||
|
||||
namespace Content.Shared.Store.Conditions;
|
||||
|
||||
/// <summary>
|
||||
/// Allows a store entry to be filtered out based on the user's antag role.
|
||||
/// Supports both blacklists and whitelists. This is copypaste because roles
|
||||
/// are absolute shitcode. Refactor this later. -emo
|
||||
/// </summary>
|
||||
public sealed class BuyerAntagCondition : ListingCondition
|
||||
{
|
||||
/// <summary>
|
||||
/// A whitelist of antag roles that can purchase this listing. Only one needs to be found.
|
||||
/// </summary>
|
||||
[DataField("whitelist", customTypeSerializer: typeof(PrototypeIdHashSetSerializer<AntagPrototype>))]
|
||||
public HashSet<string>? Whitelist;
|
||||
|
||||
/// <summary>
|
||||
/// A blacklist of antag roles that cannot purchase this listing. Only one needs to be found.
|
||||
/// </summary>
|
||||
[DataField("blacklist", customTypeSerializer: typeof(PrototypeIdHashSetSerializer<AntagPrototype>))]
|
||||
public HashSet<string>? Blacklist;
|
||||
|
||||
public override bool Condition(ListingConditionArgs args)
|
||||
{
|
||||
var ent = args.EntityManager;
|
||||
|
||||
if (!ent.TryGetComponent<MindComponent>(args.Buyer, out var mind) || mind.Mind == null)
|
||||
return true;
|
||||
|
||||
if (Blacklist != null)
|
||||
{
|
||||
foreach (var role in mind.Mind.AllRoles)
|
||||
{
|
||||
if (role is not TraitorRole blacklistantag)
|
||||
continue;
|
||||
|
||||
if (Blacklist.Contains(blacklistantag.Prototype.ID))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (Whitelist != null)
|
||||
{
|
||||
var found = false;
|
||||
foreach (var role in mind.Mind.AllRoles)
|
||||
{
|
||||
if (role is not TraitorRole antag)
|
||||
continue;
|
||||
|
||||
if (Whitelist.Contains(antag.Prototype.ID))
|
||||
found = true;
|
||||
}
|
||||
if (!found)
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
63
Content.Server/Store/Conditions/BuyerJobCondition.cs
Normal file
63
Content.Server/Store/Conditions/BuyerJobCondition.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using Content.Server.Mind.Components;
|
||||
using Content.Server.Roles;
|
||||
using Content.Shared.Roles;
|
||||
using Content.Shared.Store;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.Set;
|
||||
|
||||
namespace Content.Server.Store.Conditions;
|
||||
|
||||
/// <summary>
|
||||
/// Allows a store entry to be filtered out based on the user's job.
|
||||
/// Supports both blacklists and whitelists
|
||||
/// </summary>
|
||||
public sealed class BuyerJobCondition : ListingCondition
|
||||
{
|
||||
/// <summary>
|
||||
/// A whitelist of jobs prototypes that can purchase this listing. Only one needs to be found.
|
||||
/// </summary>
|
||||
[DataField("whitelist", customTypeSerializer: typeof(PrototypeIdHashSetSerializer<JobPrototype>))]
|
||||
public HashSet<string>? Whitelist;
|
||||
|
||||
/// <summary>
|
||||
/// A blacklist of job prototypes that can purchase this listing. Only one needs to be found.
|
||||
/// </summary>
|
||||
[DataField("blacklist", customTypeSerializer: typeof(PrototypeIdHashSetSerializer<JobPrototype>))]
|
||||
public HashSet<string>? Blacklist;
|
||||
|
||||
public override bool Condition(ListingConditionArgs args)
|
||||
{
|
||||
var ent = args.EntityManager;
|
||||
|
||||
if (!ent.TryGetComponent<MindComponent>(args.Buyer, out var mind) || mind.Mind == null)
|
||||
return true; //this is for things like surplus crate
|
||||
|
||||
if (Blacklist != null)
|
||||
{
|
||||
foreach (var role in mind.Mind.AllRoles)
|
||||
{
|
||||
if (role is not Job job)
|
||||
continue;
|
||||
|
||||
if (Blacklist.Contains(job.Prototype.ID))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (Whitelist != null)
|
||||
{
|
||||
var found = false;
|
||||
foreach (var role in mind.Mind.AllRoles)
|
||||
{
|
||||
if (role is not Job job)
|
||||
continue;
|
||||
|
||||
if (Whitelist.Contains(job.Prototype.ID))
|
||||
found = true;
|
||||
}
|
||||
if (!found)
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
41
Content.Server/Store/Conditions/BuyerWhitelistCondition.cs
Normal file
41
Content.Server/Store/Conditions/BuyerWhitelistCondition.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using Content.Shared.Store;
|
||||
using Content.Shared.Whitelist;
|
||||
|
||||
namespace Content.Server.Store.Conditions;
|
||||
|
||||
/// <summary>
|
||||
/// Filters out an entry based on the components or tags on an entity.
|
||||
/// </summary>
|
||||
public sealed class BuyerWhitelistCondition : ListingCondition
|
||||
{
|
||||
/// <summary>
|
||||
/// A whitelist of tags or components.
|
||||
/// </summary>
|
||||
[DataField("whitelist")]
|
||||
public EntityWhitelist? Whitelist;
|
||||
|
||||
/// <summary>
|
||||
/// A blacklist of tags or components.
|
||||
/// </summary>
|
||||
[DataField("blacklist")]
|
||||
public EntityWhitelist? Blacklist;
|
||||
|
||||
public override bool Condition(ListingConditionArgs args)
|
||||
{
|
||||
var ent = args.EntityManager;
|
||||
|
||||
if (Whitelist != null)
|
||||
{
|
||||
if (!Whitelist.IsValid(args.Buyer, ent))
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Blacklist != null)
|
||||
{
|
||||
if (Blacklist.IsValid(args.Buyer, ent))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using Content.Shared.Store;
|
||||
|
||||
namespace Content.Server.Store.Conditions;
|
||||
|
||||
/// <summary>
|
||||
/// Only allows a listing to be purchased a certain amount of times.
|
||||
/// </summary>
|
||||
public sealed class ListingLimitedStockCondition : ListingCondition
|
||||
{
|
||||
/// <summary>
|
||||
/// The amount of times this listing can be purchased.
|
||||
/// </summary>
|
||||
[DataField("stock", required: true)]
|
||||
public int Stock;
|
||||
|
||||
public override bool Condition(ListingConditionArgs args)
|
||||
{
|
||||
return args.Listing.PurchaseAmount < Stock;
|
||||
}
|
||||
}
|
||||
44
Content.Server/Store/Conditions/StoreWhitelistCondition.cs
Normal file
44
Content.Server/Store/Conditions/StoreWhitelistCondition.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using Content.Shared.Store;
|
||||
using Content.Shared.Whitelist;
|
||||
|
||||
namespace Content.Server.Store.Conditions;
|
||||
|
||||
/// <summary>
|
||||
/// Filters out an entry based on the components or tags on the store itself.
|
||||
/// </summary>
|
||||
public sealed class StoreWhitelistCondition : ListingCondition
|
||||
{
|
||||
/// <summary>
|
||||
/// A whitelist of tags or components.
|
||||
/// </summary>
|
||||
[DataField("whitelist")]
|
||||
public EntityWhitelist? Whitelist;
|
||||
|
||||
/// <summary>
|
||||
/// A blacklist of tags or components.
|
||||
/// </summary>
|
||||
[DataField("blacklist")]
|
||||
public EntityWhitelist? Blacklist;
|
||||
|
||||
public override bool Condition(ListingConditionArgs args)
|
||||
{
|
||||
if (args.StoreEntity == null)
|
||||
return false;
|
||||
|
||||
var ent = args.EntityManager;
|
||||
|
||||
if (Whitelist != null)
|
||||
{
|
||||
if (!Whitelist.IsValid(args.StoreEntity.Value, ent))
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Blacklist != null)
|
||||
{
|
||||
if (Blacklist.IsValid(args.StoreEntity.Value, ent))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user