Client LuaCsForBarotrauma
BarotraumaShared/SharedSource/Networking/KarmaManager.cs
1 using System;
2 using System.Collections.Generic;
3 using Barotrauma.IO;
4 using System.Linq;
5 using System.Text;
6 using System.Xml.Linq;
7 
8 namespace Barotrauma
9 {
11  {
12  public static readonly string ConfigFile = "Data" + Path.DirectorySeparatorChar + "karmasettings.xml";
13 
14  public string Name => "KarmaManager";
15 
16  public Dictionary<Identifier, SerializableProperty> SerializableProperties { get; private set; }
17 
18  [Serialize(true, IsPropertySaveable.Yes)]
19  public bool ResetKarmaBetweenRounds { get; set; }
20 
21  [Serialize(0.1f, IsPropertySaveable.Yes)]
22  public float KarmaDecay { get; set; }
23 
24  [Serialize(50.0f, IsPropertySaveable.Yes)]
25  public float KarmaDecayThreshold { get; set; }
26 
27  [Serialize(0.15f, IsPropertySaveable.Yes)]
28  public float KarmaIncrease { get; set; }
29 
30  [Serialize(50.0f, IsPropertySaveable.Yes)]
31  public float KarmaIncreaseThreshold { get; set; }
32 
33  [Serialize(0.05f, IsPropertySaveable.Yes)]
34  public float StructureRepairKarmaIncrease { get; set; }
35 
36  [Serialize(0.1f, IsPropertySaveable.Yes)]
37  public float StructureDamageKarmaDecrease { get; set; }
38 
39  [Serialize(15.0f, IsPropertySaveable.Yes)]
40  public float MaxStructureDamageKarmaDecreasePerSecond { get; set; }
41 
42  [Serialize(0.03f, IsPropertySaveable.Yes)]
43  public float ItemRepairKarmaIncrease { get; set; }
44 
45  [Serialize(0.5f, IsPropertySaveable.Yes)]
46  public float ReactorOverheatKarmaDecrease { get; set; }
47  [Serialize(30.0f, IsPropertySaveable.Yes)]
48  public float ReactorMeltdownKarmaDecrease { get; set; }
49 
50  [Serialize(0.1f, IsPropertySaveable.Yes)]
51  public float DamageEnemyKarmaIncrease { get; set; }
52  [Serialize(0.2f, IsPropertySaveable.Yes)]
53  public float HealFriendlyKarmaIncrease { get; set; }
54  [Serialize(0.25f, IsPropertySaveable.Yes)]
55  public float DamageFriendlyKarmaDecrease { get; set; }
56 
57  [Serialize(0.25f, IsPropertySaveable.Yes)]
58  public float StunFriendlyKarmaDecrease { get; set; }
59 
60  [Serialize(0.3f, IsPropertySaveable.Yes)]
61  public float StunFriendlyKarmaDecreaseThreshold { get; set; }
62 
63  [Serialize(1.0f, IsPropertySaveable.Yes)]
64  public float ExtinguishFireKarmaIncrease { get; set; }
65 
66  [Serialize(defaultValue: 15.0f, IsPropertySaveable.Yes)]
67  public float DangerousItemStealKarmaDecrease { get; set; }
68 
69  [Serialize(defaultValue: false, IsPropertySaveable.Yes)]
70  public bool DangerousItemStealBots { get; set; }
71 
72  [Serialize(defaultValue: 0.05f, IsPropertySaveable.Yes)]
73  public float BallastFloraKarmaIncrease { get; set; }
74 
75 
76  private int allowedWireDisconnectionsPerMinute;
79  {
80  get { return allowedWireDisconnectionsPerMinute; }
81  set { allowedWireDisconnectionsPerMinute = Math.Max(0, value); }
82  }
83 
84  [Serialize(6.0f, IsPropertySaveable.Yes)]
85  public float WireDisconnectionKarmaDecrease { get; set; }
86 
87  [Serialize(0.15f, IsPropertySaveable.Yes)]
88  public float SteerSubKarmaIncrease { get; set; }
89 
90  [Serialize(15.0f, IsPropertySaveable.Yes)]
91  public float SpamFilterKarmaDecrease { get; set; }
92 
93  [Serialize(40.0f, IsPropertySaveable.Yes)]
94  public float HerpesThreshold { get; set; }
95 
96  [Serialize(1.0f, IsPropertySaveable.Yes)]
97  public float KickBanThreshold { get; set; }
98 
100  public int KicksBeforeBan { get; set; }
101 
102  [Serialize(10.0f, IsPropertySaveable.Yes)]
103  public float KarmaNotificationInterval { get; set; }
104 
105  [Serialize(120.0f, IsPropertySaveable.Yes)]
106  public float AllowedRetaliationTime { get; set; }
107 
108  [Serialize(5.0f, IsPropertySaveable.Yes)]
109  public float DangerousItemContainKarmaDecrease { get; set; }
110 
111  [Serialize(defaultValue: true, IsPropertySaveable.Yes)]
113 
114  [Serialize(30.0f, IsPropertySaveable.Yes)]
115  public float MaxDangerousItemContainKarmaDecrease { get; set; }
116 
117  private readonly AfflictionPrefab herpesAffliction;
118 
119  public Dictionary<string, XElement> Presets = new Dictionary<string, XElement>();
120 
121  public KarmaManager()
122  {
123  XDocument doc = null;
124  int maxLoadRetries = 4;
125  for (int i = 0; i <= maxLoadRetries; i++)
126  {
127  try
128  {
129  doc = XMLExtensions.TryLoadXml(ConfigFile);
130  break;
131  }
132  catch (System.IO.IOException)
133  {
134  if (i == maxLoadRetries) { break; }
135  DebugConsole.NewMessage("Opening karma settings file \"" + ConfigFile + "\" failed, retrying in 250 ms...");
136  System.Threading.Thread.Sleep(250);
137  }
138  }
139 
141  if (doc?.Root != null)
142  {
143  Presets["custom"] = doc.Root;
144  foreach (var subElement in doc.Root.Elements())
145  {
146  string presetName = subElement.GetAttributeString("name", "");
147  Presets[presetName.ToLowerInvariant()] = subElement;
148  }
149  SelectPreset(GameMain.NetworkMember?.ServerSettings?.KarmaPreset ?? "default");
150  }
151  herpesAffliction = AfflictionPrefab.List.FirstOrDefault(ap => ap.Identifier == "spaceherpes");
152  }
153 
154  public void SelectPreset(string presetName)
155  {
156  if (string.IsNullOrEmpty(presetName)) { return; }
157  presetName = presetName.ToLowerInvariant();
158 
159  if (Presets.ContainsKey(presetName))
160  {
162  }
163  else if (Presets.ContainsKey("custom"))
164  {
166 
167  }
168  }
169 
170  public void SaveCustomPreset()
171  {
172  if (Presets.ContainsKey("custom"))
173  {
174  SerializableProperty.SerializeProperties(this, Presets["custom"], saveIfDefault: true);
175  }
176  }
177 
178  public void Save()
179  {
180  XDocument doc = new XDocument(new XElement(Name));
181 
182  foreach (KeyValuePair<string, XElement> preset in Presets)
183  {
184  doc.Root.Add(preset.Value);
185  }
186 
187  System.Xml.XmlWriterSettings settings = new System.Xml.XmlWriterSettings
188  {
189  Indent = true,
190  NewLineOnAttributes = true
191  };
192 
193  int maxLoadRetries = 4;
194  for (int i = 0; i <= maxLoadRetries; i++)
195  {
196  try
197  {
198  using (var writer = XmlWriter.Create(ConfigFile, settings))
199  {
200  doc.SaveSafe(writer);
201  }
202  break;
203  }
204  catch (System.IO.IOException)
205  {
206  if (i == maxLoadRetries) { throw; }
207 
208  DebugConsole.NewMessage("Saving karma settings file file \"" + ConfigFile + "\" failed, retrying in 250 ms...");
209  System.Threading.Thread.Sleep(250);
210  continue;
211  }
212  }
213  }
214  }
215 }
AfflictionPrefab is a prefab that defines a type of affliction that can be applied to a character....
static IEnumerable< AfflictionPrefab > List
static NetworkMember NetworkMember
Definition: GameMain.cs:190
static XmlWriter Create(string path, System.Xml.XmlWriterSettings settings)
Definition: SafeIO.cs:163
Dictionary< Identifier, SerializableProperty > SerializableProperties
static Dictionary< Identifier, SerializableProperty > DeserializeProperties(object obj, XElement element=null)
static void SerializeProperties(ISerializableEntity obj, XElement element, bool saveIfDefault=false, bool ignoreEditable=false)