Skip to content

Commit dade59a

Browse files
committed
small impovements
1 parent 78bed95 commit dade59a

File tree

8 files changed

+203
-149
lines changed

8 files changed

+203
-149
lines changed

CustomCommands/Interfaces/ILoadJson.cs

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,52 @@ namespace CustomCommands.Interfaces;
44

55
public interface ILoadJson
66
{
7-
List<Commands> GetCommandsFromJsonFiles(string path);
7+
/// <summary>
8+
/// Retrieves a list of commands from JSON files located in the specified path.
9+
/// </summary>
10+
/// <param name="path">The path where the JSON files are located.</param>
11+
/// <returns>A list of commands.</returns>
12+
Task<List<Commands>> GetCommandsFromJsonFiles(string path);
13+
14+
/// <summary>
15+
/// Checks if an .example file exists in the specified path and creates a default config file if it doesn't exist.
16+
/// </summary>
17+
/// <param name="path">The path to check for the example file.</param>
818
void CheckForExampleFile(string path);
9-
bool IsValidJsonSyntax(string jsonString);
19+
20+
/// <summary>
21+
/// Checks if the JSON file has a valid syntax.
22+
/// </summary>
23+
/// <param name="path">The path to the JSON file.</param>
24+
/// <param name="json">The json string.</param>
25+
/// <returns>True if the JSON syntax is valid, false otherwise.</returns>
26+
bool IsValidJsonSyntax(string json, string path);
27+
28+
/// <summary>
29+
/// Validates the list of commands loaded from a JSON file.
30+
/// </summary>
31+
/// <param name="comms">The list of commands to validate.</param>
32+
/// <param name="path">The path of the JSON file.</param>
33+
/// <returns>True if all commands are valid, false otherwise.</returns>
1034
bool ValidateObject(List<Commands>? comms, string path);
35+
36+
/// <summary>
37+
/// Logs the details of a command.
38+
/// </summary>
39+
/// <param name="comms">The command to log.</param>
1140
void LogCommandDetails(Commands comms);
41+
42+
/// <summary>
43+
/// Validates the PrintTo property of the Commands object and checks if the required message properties are set based on the PrintTo value.
44+
/// </summary>
45+
/// <param name="comms">The Commands object to validate.</param>
46+
/// <returns>True if the PrintTo property is valid and the required message properties are set; otherwise, false.</returns>
1247
bool PrintToCheck(Commands comms);
48+
49+
/// <summary>
50+
/// Validates a dynamic message by checking if it is a string or an array.
51+
/// </summary>
52+
/// <param name="message">The dynamic message to validate.</param>
53+
/// <returns>True if the message is a string or an array, otherwise false.</returns>
1354
bool ValidateMessage(dynamic message);
1455
}

CustomCommands/Interfaces/IMessageManager.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ namespace CustomCommands.Interfaces;
55

66
public interface IMessageManager
77
{
8+
/// <summary>
9+
/// Sends a message based on the specified command and target receiver.
10+
/// </summary>
11+
/// <param name="player">The player who triggered the command.</param>
12+
/// <param name="cmd">The command containing the message and target receiver.</param>
813
void SendMessage(CCSPlayerController player, Commands cmd);
914
void PrintToCenterClient(CCSPlayerController player, Commands cmd);
1015
void PrintToAllCenter(Commands cmd);

CustomCommands/Interfaces/IReplaceTagsFunctions.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,40 @@ namespace CustomCommands.Interfaces;
44

55
public interface IReplaceTagsFunctions
66
{
7+
/// <summary>
8+
/// Replaces tags in the input array with their corresponding values.
9+
/// </summary>
10+
/// <param name="input">The array of strings containing tags to be replaced.</param>
11+
/// <param name="player">The CCSPlayerController object used for tag replacement.</param>
12+
/// <returns>The array of strings with tags replaced.</returns>
713
string[] ReplaceTags(dynamic input, CCSPlayerController player);
14+
15+
/// <summary>
16+
/// Replaces language tags in the input string with the corresponding localized value.
17+
/// Language tags are defined within curly braces, e.g. "{LANG=LocalizerTag}".
18+
/// If a language tag is found, it is replaced with the localized value from the CustomCommands plugin's Localizer.
19+
/// If the localized value is not found, a default message is returned.
20+
/// </summary>
21+
/// <param name="input">The input string to process.</param>
22+
/// <returns>The input string with language tags replaced with localized values.</returns>
823
string ReplaceLanguageTags(string input);
24+
25+
/// <summary>
26+
/// Replaces tags in the input string with corresponding values based on the provided player information.
27+
/// </summary>
28+
/// <param name="input">The input string containing tags to be replaced.</param>
29+
/// <param name="player">The CCSPlayerController object representing the player.</param>
30+
/// <param name="safety">A boolean value indicating whether to replace the {PLAYERNAME} tag. Default is true.</param>
31+
/// <returns>The modified string with replaced tags.</returns>
932
string ReplaceMessageTags(string input, CCSPlayerController player, bool safety = true);
1033
string ReplaceColorTags(string input);
34+
35+
/// <summary>
36+
/// Splits the input into an array of strings. If the input is a string, it will be split by newlines. If the input is an array, each element will be split by newlines.
37+
/// </summary>
38+
/// <param name="input">This should be a string[] or a string</param>
39+
/// <returns>An array of strings representing the lines of the input.</returns>
1140
List<string> WrappedLine(dynamic message);
41+
42+
string ReplaceRandomTags(string message);
1243
}

CustomCommands/Services/CooldownManager.cs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ namespace CustomCommands.Interfaces;
66

77
public class CooldownManager : ICooldownManager
88
{
9-
public IPluginGlobals PluginGlobals { get; }
10-
public IReplaceTagsFunctions ReplaceTagsFunctions { get; }
9+
public IPluginGlobals _pluginGlobals { get; }
10+
public IReplaceTagsFunctions _replaceTagsFunctions { get; }
11+
1112
public CooldownManager(IPluginGlobals PluginGlobals, IReplaceTagsFunctions ReplaceTagsFunctions)
1213
{
13-
this.PluginGlobals = PluginGlobals;
14-
this.ReplaceTagsFunctions = ReplaceTagsFunctions;
14+
_pluginGlobals = PluginGlobals;
15+
_replaceTagsFunctions = ReplaceTagsFunctions;
1516
}
1617

1718
/// <summary>
@@ -42,11 +43,11 @@ public bool IsCommandOnCooldown(CCSPlayerController player, Commands cmd)
4243
/// <returns>True if the command is on cooldown, false otherwise.</returns>
4344
public bool IsCommandOnCooldownWithCondition(Func<CooldownTimer, bool> predicate, CCSPlayerController player, Commands cmd)
4445
{
45-
var index = PluginGlobals.CooldownTimer.FindIndex(x => predicate(x) && x.CooldownTime > DateTime.Now);
46+
var index = _pluginGlobals.CooldownTimer.FindIndex(x => predicate(x) && x.CooldownTime > DateTime.Now);
4647

4748
if (index != -1)
4849
{
49-
var totalSeconds = (double)PluginGlobals.CooldownTimer[index].CooldownTime.Subtract(DateTime.Now).TotalSeconds;
50+
var totalSeconds = (double)_pluginGlobals.CooldownTimer[index].CooldownTime.Subtract(DateTime.Now).TotalSeconds;
5051
var totalSecondsRounded = (int)Math.Round(totalSeconds);
5152
var timeleft = totalSecondsRounded.ToString();
5253
var message = "";
@@ -57,14 +58,14 @@ public bool IsCommandOnCooldownWithCondition(Func<CooldownTimer, bool> predicate
5758
var cooldown = JsonSerializer.Deserialize<Cooldown>(cmd.Cooldown.GetRawText());
5859
Console.WriteLine(cooldown.CooldownMessage);
5960
string[] replaceTimeleft = {cooldown.CooldownMessage.Replace("{TIMELEFT}", timeleft)};
60-
message = ReplaceTagsFunctions.ReplaceTags(replaceTimeleft, player)[0];
61+
message = _replaceTagsFunctions.ReplaceTags(replaceTimeleft, player)[0];
6162
}
6263
catch (JsonException)
6364
{
6465
message = $"This command is for {timeleft} seconds on cooldown";
6566
}
6667

67-
player.PrintToChat($"{PluginGlobals.Config.Prefix}{message}");
68+
player.PrintToChat($"{_pluginGlobals.Config.Prefix}{message}");
6869

6970
return true;
7071
}
@@ -89,25 +90,25 @@ public void AddToCooldownList(bool isGlobal, int playerID, Guid commandID, int c
8990

9091
if (isGlobal)
9192
{
92-
int index = PluginGlobals.CooldownTimer.FindIndex(x =>
93+
int index = _pluginGlobals.CooldownTimer.FindIndex(x =>
9394
x.IsGlobal == true
9495
&& x.CommandID == commandID);
9596

9697
if (index != -1)
97-
PluginGlobals.CooldownTimer[index].CooldownTime = timer.CooldownTime;
98+
_pluginGlobals.CooldownTimer[index].CooldownTime = timer.CooldownTime;
9899
else
99-
PluginGlobals.CooldownTimer.Add(timer);
100+
_pluginGlobals.CooldownTimer.Add(timer);
100101
}
101102
else
102103
{
103104
timer.PlayerID = playerID;
104-
int index = PluginGlobals.CooldownTimer.FindIndex(x =>
105+
int index = _pluginGlobals.CooldownTimer.FindIndex(x =>
105106
x.PlayerID == playerID
106107
&& x.CommandID == commandID);
107108
if (index != -1)
108-
PluginGlobals.CooldownTimer[index].CooldownTime = timer.CooldownTime;
109+
_pluginGlobals.CooldownTimer[index].CooldownTime = timer.CooldownTime;
109110
else
110-
PluginGlobals.CooldownTimer.Add(timer);
111+
_pluginGlobals.CooldownTimer.Add(timer);
111112
}
112113
}
113114

@@ -124,7 +125,7 @@ public void SetCooldown(CCSPlayerController player, Commands cmd)
124125
{
125126
case JsonValueKind.Number:
126127

127-
int cooldown = cmd.Cooldown.GetInt32();
128+
var cooldown = cmd.Cooldown.GetInt32();
128129
if (cooldown == 0)
129130
break;
130131

CustomCommands/Services/EventManager.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,37 @@ namespace CustomCommands.Services;
88

99
public class EventManager : IEventManager
1010
{
11-
private readonly IPluginGlobals PluginGlobals;
12-
private readonly PluginContext PluginContext;
13-
private readonly IReplaceTagsFunctions ReplaceTagsFunctions;
11+
private readonly IPluginGlobals _pluginGlobals;
12+
private readonly PluginContext _pluginContext;
13+
private readonly IReplaceTagsFunctions _replaceTagsFunctions;
1414

1515
public EventManager(IPluginGlobals PluginGlobals, IPluginContext PluginContext, IReplaceTagsFunctions ReplaceTagsFunctions)
1616
{
17-
this.PluginGlobals = PluginGlobals;
18-
this.PluginContext = (PluginContext as PluginContext)!;
19-
this.ReplaceTagsFunctions = ReplaceTagsFunctions;
17+
_pluginGlobals = PluginGlobals;
18+
_pluginContext = (PluginContext as PluginContext)!;
19+
_replaceTagsFunctions = ReplaceTagsFunctions;
2020
}
2121

2222
[GameEventHandler]
2323
public HookResult OnPlayerDisconnect(EventPlayerDisconnect @event, GameEventInfo _)
2424
{
25-
PluginGlobals.centerClientOn.RemoveAll(p => p.ClientId == @event.Userid.UserId);
26-
PluginGlobals.CooldownTimer.RemoveAll(p => p.PlayerID == @event.Userid.UserId);
25+
_pluginGlobals.centerClientOn.RemoveAll(p => p.ClientId == @event.Userid.UserId);
26+
_pluginGlobals.CooldownTimer.RemoveAll(p => p.PlayerID == @event.Userid.UserId);
2727

2828
return HookResult.Continue;
2929
}
3030

3131
public void RegisterListeners()
3232
{
33-
CustomCommands plugin = (PluginContext.Plugin as CustomCommands)!;
33+
var plugin = (_pluginContext.Plugin as CustomCommands)!;
3434

3535
// Register the OnTick event for PrintToCenterHtml duration
3636
plugin.RegisterListener<Listeners.OnTick>(() =>
3737
{
3838
// Client Print To Center
39-
if(PluginGlobals.centerClientOn != null && PluginGlobals.centerClientOn.Count !> 0 )
39+
if(_pluginGlobals.centerClientOn != null && _pluginGlobals.centerClientOn.Count !> 0 )
4040
{
41-
foreach (var player in PluginGlobals.centerClientOn)
41+
foreach (var player in _pluginGlobals.centerClientOn)
4242
{
4343
var targetPlayer = Utilities.GetPlayerFromUserid(player.ClientId);
4444
if (player != null && targetPlayer != null)
@@ -49,23 +49,23 @@ public void RegisterListeners()
4949
}
5050

5151
// Server Print To Center
52-
if (PluginGlobals.centerServerOn.IsRunning)
52+
if (_pluginGlobals.centerServerOn.IsRunning)
5353
{
5454
Utilities.GetPlayers().ForEach(controller =>
5555
{
5656
if (!controller.IsValid || controller.SteamID == 0) return;
5757

58-
string message = ReplaceTagsFunctions.ReplaceLanguageTags(PluginGlobals.centerServerOn.Message);
59-
message = ReplaceTagsFunctions.ReplaceMessageTags(message, controller);
58+
var message = _replaceTagsFunctions.ReplaceLanguageTags(_pluginGlobals.centerServerOn.Message);
59+
message = _replaceTagsFunctions.ReplaceMessageTags(message, controller);
6060
controller.PrintToCenterHtml(message, 1);
6161
});
6262
}
6363
});
6464

6565
plugin.RegisterListener<Listeners.OnMapEnd>(() =>
6666
{
67-
PluginGlobals.centerClientOn.Clear();
68-
PluginGlobals.CooldownTimer.Clear();
67+
_pluginGlobals.centerClientOn.Clear();
68+
_pluginGlobals.CooldownTimer.Clear();
6969
});
7070
}
7171
}

0 commit comments

Comments
 (0)