Client LuaCsForBarotrauma
MoneyAction.cs
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Linq;
5 
6 namespace Barotrauma
7 {
12  {
13  public MoneyAction(ScriptedEvent parentEvent, ContentXElement element) : base(parentEvent, element) { }
14 
15  [Serialize(0, IsPropertySaveable.Yes, description: "Amount of money to give or remove.")]
16  public int Amount { get; set; }
17 
18  [Serialize("", IsPropertySaveable.Yes, description: "If set, the money is removed from character(s) with this tag.")]
19  public Identifier TargetTag { get; set; }
20 
21  private bool isFinished;
22 
23  public override bool IsFinished(ref string goTo)
24  {
25  return isFinished;
26  }
27  public override void Reset()
28  {
29  isFinished = false;
30  }
31 
32  public override void Update(float deltaTime)
33  {
34  if (isFinished) { return; }
35 
36 #if SERVER
37  bool hasTag = !TargetTag.IsEmpty;
38  List<Client> matchingClients = new List<Client>();
39  if (hasTag)
40  {
41  IEnumerable targets = ParentEvent.GetTargets(TargetTag);
42 
43  foreach (Entity entity in targets)
44  {
45  if (entity is Character && GameMain.Server?.ConnectedClients.FirstOrDefault(c => c.Character == entity) is { } matchingCharacter)
46  {
47  matchingClients.Add(matchingCharacter);
48  break;
49  }
50  }
51  }
52 #endif
53 
54  if (GameMain.GameSession?.GameMode is CampaignMode campaign)
55  {
56 #if SERVER
57  if (!hasTag)
58  {
59  campaign.Bank.Give(Amount);
60  }
61  else
62  {
63  foreach (Client client in matchingClients)
64  {
65  campaign.GetWallet(client).Give(Amount);
66  }
67  }
68 #else
69  campaign.Wallet.Give(Amount);
70 #endif
71  GameAnalyticsManager.AddMoneyGainedEvent(Amount, GameAnalyticsManager.MoneySource.Event, ParentEvent.Prefab.Identifier.Value);
72  }
73 
74  isFinished = true;
75  }
76 
77  public override string ToDebugString()
78  {
79  return $"{ToolBox.GetDebugSymbol(isFinished)} {nameof(SetDataAction)} -> (Amount: {Amount.ColorizeObject()})";
80  }
81  }
82 }
readonly ScriptedEvent ParentEvent
Definition: EventAction.cs:102
EventPrefab Prefab
Definition: Event.cs:16
static GameSession?? GameSession
Definition: GameMain.cs:88
Give or remove money from the crew or a specific character.
Definition: MoneyAction.cs:12
override string ToDebugString()
Rich test to display in debugdraw
Definition: MoneyAction.cs:77
override void Update(float deltaTime)
Definition: MoneyAction.cs:32
override bool IsFinished(ref string goTo)
Has the action finished.
Definition: MoneyAction.cs:23
override void Reset()
Definition: MoneyAction.cs:27
MoneyAction(ScriptedEvent parentEvent, ContentXElement element)
Definition: MoneyAction.cs:13
readonly Identifier Identifier
Definition: Prefab.cs:34
IEnumerable< Entity > GetTargets(Identifier tag)