Server LuaCsForBarotrauma
HealingCooldownServer.cs
1 #nullable enable
2 
3 using System;
4 using System.Collections.Generic;
6 
7 namespace Barotrauma
8 {
9  internal static class HealingCooldown
10  {
11  private static readonly Dictionary<Client, DateTimeOffset> HealingCooldowns = new();
12 
13  // Little bit less than client's 0.5 second cooldown to account for latency
14  private const float CooldownDuration = 0.4f;
15 
16  public static bool IsOnCooldown(Client client)
17  {
18  RemoveExpiredCooldowns();
19  return HealingCooldowns.ContainsKey(client);
20  }
21 
22  public static void SetCooldown(Client client)
23  {
24  RemoveExpiredCooldowns();
25  DateTimeOffset newCooldown = DateTimeOffset.UtcNow.AddSeconds(CooldownDuration);
26  HealingCooldowns[client] = newCooldown;
27  }
28 
29  private static void RemoveExpiredCooldowns()
30  {
31  HashSet<Client>? expiredCooldowns = null;
32 
33  DateTimeOffset now = DateTimeOffset.UtcNow;
34 
35  foreach (var (client, cooldown) in HealingCooldowns)
36  {
37  if (now < cooldown) { continue; }
38 
39  expiredCooldowns ??= new HashSet<Client>();
40  expiredCooldowns.Add(client);
41  }
42 
43  if (expiredCooldowns is null) { return; }
44 
45  foreach (Client expiredCooldown in expiredCooldowns)
46  {
47  HealingCooldowns.Remove(expiredCooldown);
48  }
49  }
50  }
51 }