Client LuaCsForBarotrauma
CheckPurchasedItemsAction.cs
1 using System;
2 using System.Linq;
3 
4 namespace Barotrauma;
5 
9 class CheckPurchasedItemsAction : BinaryOptionAction
10 {
11  public enum TransactionType
12  {
13  Purchased,
14  Sold
15  }
16 
17  [Serialize(TransactionType.Purchased, IsPropertySaveable.Yes, description: "Do the items need to have been purchased or sold?")]
18  public TransactionType Type { get; set; }
19 
20  [Serialize("", IsPropertySaveable.Yes, description: "Identifier of the item that must have been purchased or sold.")]
21  public Identifier ItemIdentifier { get; set; }
22 
23  [Serialize("", IsPropertySaveable.Yes, description: "Tag of the item that must have been purchased or sold.")]
24  public Identifier ItemTag { get; set; }
25 
26  [Serialize(1, IsPropertySaveable.Yes, description: "Minimum number of matching items that must have been purchased or sold.")]
27  public int MinCount { get; set; }
28 
29  public CheckPurchasedItemsAction(ScriptedEvent parentEvent, ContentXElement element) : base(parentEvent, element)
30  {
31  MinCount = Math.Max(MinCount, 1);
32  }
33 
34  protected override bool? DetermineSuccess()
35  {
36  if (ItemIdentifier.IsEmpty && ItemTag.IsEmpty)
37  {
38  return false;
39  }
40  if (GameMain.GameSession?.Campaign?.CargoManager is not CargoManager cargoManager)
41  {
42  return false;
43  }
44  if (Type == TransactionType.Purchased)
45  {
46  int totalPurchased = 0;
47  foreach ((Identifier id, var items) in cargoManager.PurchasedItems)
48  {
49  if (!ItemIdentifier.IsEmpty)
50  {
51  totalPurchased += items.Find(i => i.ItemPrefabIdentifier == ItemIdentifier)?.Quantity ?? 0;
52  }
53  else if (!ItemTag.IsEmpty)
54  {
55  foreach (var item in items)
56  {
57  if (item.ItemPrefab.Tags.Contains(ItemTag))
58  {
59  totalPurchased += item.Quantity;
60  }
61  }
62  }
63  if (totalPurchased >= MinCount)
64  {
65  return true;
66  }
67  }
68  }
69  else
70  {
71  int totalSold = 0;
72  foreach ((Identifier id, var items) in cargoManager.SoldItems)
73  {
74  if (!ItemIdentifier.IsEmpty)
75  {
76  totalSold += items.Count(i => i.ItemPrefab.Identifier == ItemIdentifier);
77  }
78  else if (!ItemTag.IsEmpty)
79  {
80  totalSold += items.Count(i => i.ItemPrefab.Tags.Contains(ItemTag));
81  }
82  if (totalSold >= MinCount)
83  {
84  return true;
85  }
86  }
87  }
88  return false;
89  }
90 }
Check whether specific kinds of items have been purchased or sold during the round.
CheckPurchasedItemsAction(ScriptedEvent parentEvent, ContentXElement element)