|
| 1 | +using System.Text.Json; |
1 | 2 | using System.Text.RegularExpressions; |
2 | 3 | using CounterStrikeSharp.API; |
3 | 4 | using CounterStrikeSharp.API.Core; |
@@ -56,4 +57,108 @@ public bool RequiresPermissions(CCSPlayerController player, Permission permissio |
56 | 57 | return true; |
57 | 58 | } |
58 | 59 | } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Checks if the command is on cooldown |
| 63 | + /// </summary> |
| 64 | + /// <param name="player"></param> |
| 65 | + /// <param name="cmd"></param> |
| 66 | + /// <returns></returns> |
| 67 | + public bool IsCommandOnCooldown(CCSPlayerController player, Commands cmd) |
| 68 | + { |
| 69 | + // Check global cooldown |
| 70 | + if (IsCommandOnCooldownWithCondition(x => x.IsGlobal == true && x.CommandID == cmd.ID, player, cmd)) |
| 71 | + return true; |
| 72 | + |
| 73 | + // Check player cooldown |
| 74 | + if (IsCommandOnCooldownWithCondition(x => x.PlayerID == player.UserId && x.CommandID == cmd.ID, player, cmd)) |
| 75 | + return true; |
| 76 | + |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + private bool IsCommandOnCooldownWithCondition(Func<CooldownTimer, bool> predicate, CCSPlayerController player, Commands cmd) |
| 81 | + { |
| 82 | + int index = PluginGlobals.CooldownTimer.FindIndex(x => predicate(x) && x.CooldownTime > DateTime.Now); |
| 83 | + |
| 84 | + if (index != -1) |
| 85 | + { |
| 86 | + string timeleft = PluginGlobals.CooldownTimer[index].CooldownTime.Subtract(DateTime.Now).Seconds.ToString(); |
| 87 | + player.PrintToChat($"{PluginGlobals.Config.Prefix}{cmd.Cooldown.CooldownMessage.Replace("{TIME}", timeleft) |
| 88 | + ?? $"This command is for {timeleft} seconds on cooldown"}"); |
| 89 | + |
| 90 | + return true; |
| 91 | + } |
| 92 | + |
| 93 | + return false; |
| 94 | + } |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// Adds the command to the cooldown list |
| 98 | + /// </summary> |
| 99 | + /// <param name="isGlobal"></param> |
| 100 | + /// <param name="playerID"></param> |
| 101 | + /// <param name="commandID"></param> |
| 102 | + /// <param name="cooldownTime"></param> |
| 103 | + public void AddToCooldownList(bool isGlobal, int playerID, Guid commandID, int cooldownTime) |
| 104 | + { |
| 105 | + var timer = new CooldownTimer() { |
| 106 | + IsGlobal = isGlobal, |
| 107 | + CommandID = commandID, |
| 108 | + CooldownTime = DateTime.Now.AddSeconds(cooldownTime) |
| 109 | + }; |
| 110 | + |
| 111 | + if (isGlobal) |
| 112 | + { |
| 113 | + int index = PluginGlobals.CooldownTimer.FindIndex(x => |
| 114 | + x.IsGlobal == true |
| 115 | + && x.CommandID == commandID); |
| 116 | + if (index != -1) |
| 117 | + PluginGlobals.CooldownTimer[index].CooldownTime = timer.CooldownTime; |
| 118 | + else |
| 119 | + PluginGlobals.CooldownTimer.Add(timer); |
| 120 | + } |
| 121 | + else |
| 122 | + { |
| 123 | + timer.PlayerID = playerID; |
| 124 | + int index = PluginGlobals.CooldownTimer.FindIndex(x => |
| 125 | + x.PlayerID == playerID |
| 126 | + && x.CommandID == commandID); |
| 127 | + if (index != -1) |
| 128 | + PluginGlobals.CooldownTimer[index].CooldownTime = timer.CooldownTime; |
| 129 | + else |
| 130 | + PluginGlobals.CooldownTimer.Add(timer); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + /// <summary> |
| 135 | + /// Sets the cooldown for the command |
| 136 | + /// </summary> |
| 137 | + /// <param name="player">Need to add the player if the Cooldown is only for a specific player</param> |
| 138 | + /// <param name="cmd"></param> |
| 139 | + public void SetCooldown(CCSPlayerController player, Commands cmd) |
| 140 | + { |
| 141 | + if (cmd.Cooldown is JsonElement jsonElement) |
| 142 | + { |
| 143 | + switch (jsonElement.ValueKind) |
| 144 | + { |
| 145 | + case JsonValueKind.Number: |
| 146 | + int cooldown = (int)cmd.Cooldown; |
| 147 | + if (cooldown == 0) |
| 148 | + break; |
| 149 | + |
| 150 | + AddToCooldownList(false, player.UserId ?? 0, cmd.ID, cooldown); |
| 151 | + break; |
| 152 | + |
| 153 | + case JsonValueKind.Object: |
| 154 | + Cooldown cooldownObject = (Cooldown)cmd.Cooldown; |
| 155 | + |
| 156 | + AddToCooldownList(cooldownObject.IsGlobal, player.UserId ?? 0, cmd.ID, cooldownObject.CooldownTime); |
| 157 | + break; |
| 158 | + |
| 159 | + default: |
| 160 | + break; |
| 161 | + } |
| 162 | + } |
| 163 | + } |
59 | 164 | } |
0 commit comments